def valid_chain(self, chain: []) -> bool: """ Determine if a given blockchain is valid :param chain: <list> A blockchain :return: <bool> True if valid, False if not """ last_block = Block() last_block.from_dict(chain[0]) current_index = 1 while current_index < len(chain): # block_dict = chain[current_index] block = Block() block.from_dict(chain[current_index]) # print(f'{last_block.__dict__}') # print(f'{block.__dict__}') # print("\n-----------\n") # Check that the hash of the block is correct if block.previous_hash != last_block.hash: return False # Check that the Proof of Work is correct if not self.valid_proof(last_block.proof, block.proof): return False last_block = block current_index += 1 return True
def total_amount(self, node_id) -> int: self.resolve_conflicts() current_index = 1 total_amount = 0 while current_index < len(self.chain): block = Block() block.from_dict(self.chain[current_index]) for transaction in block.transactions: if transaction['recipient'] == node_id: total_amount += transaction['amount']; elif transaction['sender'] == node_id: total_amount -= transaction['amount'] current_index += 1 return total_amount
def last_block(self) -> Block: # last_block_dict = self.chain[-1] last_block = Block() last_block.from_dict(self.chain[-1]) return last_block