Exemplo n.º 1
0
 def proof_of_work(self, block: Block) -> str:
     '''Proof-of-Work to verify a valid block and find the corresponding nonce value'''
     block.nonce = 0
     computed_hash = block.compute_hash()
     # Keep trying and increasing the nonce value
     # until the new hash value meets the difficulty level restriction (Solve the hash puzzle)
     while not computed_hash.startswith('0' * block.difficulty):
         block.nonce += 1
         computed_hash = block.compute_hash()
     return computed_hash
Exemplo n.º 2
0
def lightweight():
    fullchain = [json.loads(block) for block in blockchain.chain]
    lightweight = []
    for block in fullchain:
        block_object = Block(block['index'], block['transaction'], block['timestamp'], block['previous_hash'])
        block_object.merkle_root = block['merkle_root']
        block_object.nonce = block['nonce']
        block_object.difficulty = block['difficulty']
        lightweight.append(block_object.to_dict())
    response = {
        'chain': json.dumps(lightweight),
        'length': len(lightweight)
    }
    return jsonify(response), 200
Exemplo n.º 3
0
    def valid_chain(self, chain: List[str]) -> bool:
        '''check if a blockchain (all blocks) is valid'''
        current_index = 0
        chain = json.loads(chain)

        # load and check every block from the blockchain
        while current_index < len(chain):
            block = json.loads(chain[current_index])
            current_block = Block(block['index'], block['transaction'],
                                  block['timestamp'], block['previous_hash'])
            current_block.merkle_root = block['merkle_root']
            current_block.nonce = block['nonce']
            current_block.difficulty = block['difficulty']

            # if the current block is NOT the last block
            if current_index + 1 < len(chain):
                # if the hash value of the current block != previous block's hash value of the next block, then reject
                if current_block.compute_hash() != json.loads(
                        chain[current_index + 1])['previous_hash']:
                    return False
            # check if the current block is an instance from the list of chain
            if isinstance(current_block.transaction, list):
                for transaction in current_block.transaction:
                    transaction = json.loads(transaction)
                    # Skip block reward because it does not have signature
                    if transaction['sender'] == 'Block_Reward':
                        continue
                    current_transaction = Transaction(transaction['sender'],
                                                      transaction['recipient'],
                                                      transaction['value'])
                    current_transaction.signature = transaction['signature']
                    # check if digital signature of each transaction is valid, if not then reject
                    if not current_transaction.verify_transaction_signature():
                        return False
                # check if hash value of the current block is not valid, if yes then reject
                if not self.is_valid_proof(current_block, block['hash']):
                    return False
            current_index += 1
        return True