예제 #1
0
    def proof_of_work(self):
        last_block = self.__chain[-1]
        last_hash = hash_block(last_block)
        proof = 0
        while not Verification.verify_proof(self.__open_transactions, last_hash, proof): #Will keep executing until the verify_proof return true
            proof += 1

        return proof
예제 #2
0
 def add_block(self, block):
     transactions = [Transaction(tx['sender'], tx['recipient'], tx['signature'], tx['amount']) for tx in block['transactions']]
     proof_is_valid = Verification.verify_proof(transactions[:-1], block['previous_hash'], block['proof'])
     hashes_match = hash_block(self.chain[-1]) == block['previous_hash']
     if not proof_is_valid or not hashes_match:
         return False
     converted_block = Block(block['index'], block['previous_hash'], transactions, block['proof'],block['timestamp'])
     self.__chain.append(converted_block)
     stored_transactions = self.__open_transactions[:]
     for itx in block['transactions']: #for incoming transactions in block['transactions]
         for opentx in stored_transactions:
             if opentx.sender == itx['sender'] and opentx.recipient == itx['recipient'] and opentx.amount == itx['amount'] and opentx.signature == itx['signature']:
                 try:
                     self.__open_transactions.remove(opentx)
                 except ValueError:
                     print('Item was already removed')
     self.save_data()
     return True