def valid_chain(chain): ''' Determine if a given blockchain is valid by looping through each block and verifying both hash and the proof :param chain: <list> A blockchain to verify :return: <bool> True if valid, else False ''' last_block = chain[0] current_index = 1 while current_index < len(chain): block = chain[current_index] print(f'{last_block}') print(f'{block}') print('\n-------\n') # Check of the current block is correct if block['previous_hash'] != Hash.hash(last_block): return False # Check that the proof of work is correct # by validating the last blocks proof against the current blocks proof if not Proof.valid_proof(last_block['proof'], block['proof']): return False last_block = block current_index += 1 # If all above checks pass, then the block is valid. return True
def test_valid_proof(self): ''' Ensures that the validation method is working correctly. ''' self.assertTrue(Proof.valid_proof(1, 72608))