def add_block(self, block): transactions = [Transaction(transaction["sender"], transaction["recipient"], transaction["signature"], transaction["amount"]) for transaction in block["transactions"]] proof_is_valid = Verification.check_valid_proof(transactions[:-1], block["previous_hash"], block["proof"]) hashes_match = hash_util.hash_block(self.chain[-1]) == block["previous_hash"] if not proof_is_valid or not hashes_match: return False converted_block = JBlock(block["index"], block["previous_hash"], transactions, block["proof"], block["timestamp"]) self.__chain.append(converted_block) stored_transactions = self.open_transactions[:] for incoming in block["transactions"]: for open_transaction in stored_transactions: sender_and_recipient_equal = open_transaction.sender == incoming["sender"] and open_transaction.recipient == incoming["recipient"] signature_equal = open_transaction.signature == incoming["signature"] amount_equal = open_transaction.amount == incoming["amount"] if sender_and_recipient_equal and signature_equal and amount_equal: try: self.open_transactions.remove(open_transaction) except ValueError: print("item already removed") FileHandler.save_j_chain(self) return True
def proof_of_work(self, hash_value): nounce = 0 while not Verification.check_valid_proof(self.open_transactions, hash_value, nounce): nounce += 1 return nounce