コード例 #1
0
 def test__block_genesis_block(self):
     # given
     bc = BlockChain()
     # when
     genesis_block = bc.last_block
     # then
     assert (genesis_block['previous_hash'] == 1)
     assert (genesis_block['proof'] == 1)
     assert (genesis_block['index'] == 1)
コード例 #2
0
    def do_get_json(self, arg):
        '''
        get "json_address"
        read genesis transaction from json file

        :param arg: string json_address
        :return:
        '''
        json_address = arg.strip('"')

        self.blockchain = BlockChain(BankSettings.objects.all()[0].difficulty)

        with open(json_address, 'r') as json_file:
            json_data = json.load(json_file)
            if type(json_data).__name__ == 'list':
                for item in json_data:
                    self._read_block_from_dict(item)
            else:  # it was a dict
                self._read_block_from_dict(json_data)
コード例 #3
0
def validator(**kwargs):
    return True


class TestTransaction(Transaction):
    @property
    def default(self):
        return TestTransaction(
            sender="0",
            recipient=str(uuid4()).replace('-', ''),
            amount=1,
        ).value


model = Model(transation_type=type(TestTransaction), model_validator=validator)
block_chain = BlockChain(model)
'''
BlockChain server:
    INPUT:
        Mother_node (with port no)
        Self_node (with port no)
        Model
'''

app = Flask(__name__)


@app.route('/mine', methods=['GET'])
def mine():
    last_block = block_chain.last_block
    last_proof = last_block['proof']
コード例 #4
0
from blockchain.blockchain import BlockChain
import blockchain_client
import blockchain_server

IP = None
PORT = None
CONNECTED_NODES = []  # list of addresses
BLOCKCHAIN = BlockChain()
BLOCK_INVITES = []  # list of {"block_id": block_id, "ip_address": ip_address}
TRANSACTION_INVITES = [
]  # list of {"transaction_id": transaction_id, "ip_address": ip_address}


def initialise():
    print("hi")
    global IP, PORT, CONNECTED_NODES, BLOCKCHAIN, BLOCK_INVITES, TRANSACTION_INVITES

    blockchain_client.IP = IP
    blockchain_client.PORT = PORT
    blockchain_client.CONNECTED_NODES = CONNECTED_NODES
    blockchain_client.BLOCKCHAIN = BLOCKCHAIN
    blockchain_client.BLOCK_INVITES = BLOCK_INVITES
    blockchain_client.TRANSACTION_INVITES = TRANSACTION_INVITES

    blockchain_server.IP = IP
    blockchain_server.PORT = PORT
    blockchain_server.CONNECTED_NODES = CONNECTED_NODES
    blockchain_server.BLOCKCHAIN = BLOCKCHAIN
    blockchain_server.BLOCK_INVITES = BLOCK_INVITES
    blockchain_server.TRANSACTION_INVITES = TRANSACTION_INVITES
コード例 #5
0
 def setUp(self):
     self.block_chain = BlockChain()
コード例 #6
0
from blockchain.blockchain import BlockChain

blockchain = BlockChain()

# create the genesis block
previous_block = blockchain.create_genesis_block()

#  How many blocks should we add to the chain after the genesis block
num_of_blocks_to_add = 20

for i in range(num_of_blocks_to_add):
    next_block = blockchain.next_block(previous_block)
    previous_block = next_block

# print the whole block chain
# blockchain.print_blockchain()

# don't change block in the blockchain, and print the validation result
bl = blockchain.validate_blockchain()
print(bl)

# change one block in the blockchain, and print the validation result
blockchain.set_block(4, 'Hey, I am block changed!')
bl = blockchain.validate_blockchain()
print(bl)
コード例 #7
0
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @createTime: 2019-12-08 12:53
# @author: scdev030

from uuid import uuid4

from flask import Flask, jsonify, request

from blockchain.blockchain import BlockChain
from blockchain.proof_of_work import ProofOfWork

app = Flask(__name__)

node_id = str(uuid4())
block_chain = BlockChain()
proof_of_work = ProofOfWork()


@app.route('/mine', methods=['GET'])
def mine():
    """
    创建新的区块,并添加到原本区块中
    """
    # 生成下一个proof
    last_block = block_chain.last_block
    last_proof = last_block['proof']
    proof = proof_of_work.proof(last_proof)
    # sender为0表示此节点开采了新硬币
    block_chain.new_transaction(sender="0", recipient=node_id, details='生成一个币')
    # 添加新块