示例#1
0
def create_chain_from_dump(chain_dump):
    bc = Blockchain()
    for idx, block_data in enumerate(chain_dump):
        block = Block(transactions=block_data["transactions"],
                      timestamp=block_data["timestamp"],
                      previous_hash=block_data["previous_hash"])
        block.block_hash = block_data["block_hash"]
        proof = block_data["block_hash"]

        if idx > 0:
            added = bc.add_block(block, proof)
            if not added:
                raise Exception("The chain dump is tampered!")
        else:  # the block is a genesis block, no verification needed
            bc.chain.append(block)  #TODO are you sure this makes sense?
    return bc
示例#2
0
def verify_and_add_block():
    """Endpoint to add a block mined by someone else to the node's chain. The node first verifies the block and then
    adds it to the chain."""

    print("New block received from network...", sys.stdout)

    block_data = request.get_json(force=True)
    print(block_data, sys.stdout)
    block = Block(transactions=block_data["transactions"],
                  timestamp=block_data["timestamp"],
                  previous_hash=block_data["previous_hash"])
    block.block_hash = block_data["block_hash"]
    #TODO why are we generating the block hash once and then redefining it? Stupid.
    proof = block_data['block_hash']
    added = blockchain.add_block(block, proof)

    if not added:
        print("block discarded by node", sys.stdout)
        return "The block was discarded by the node", 400

    print("block added to chain", sys.stdout)
    return "Block added to the chain", 201