Example #1
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.valid_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 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 removed already')
     self.save_data()
     return True
Example #2
0
    def add_block(self, block):
        transactions = [
            Transaction(tx[sender_str], tx[recipient_str], tx[signature_str],
                        tx[amount_str]) for tx in block[transactions_str]
        ]
        proof_is_valid = Verification.valid_proof(transactions[:-1],
                                                  block[previous_hash_str],
                                                  block[proof_str])
        hashes_match = hash_block(self.chain[-1]) == block[previous_hash_str]
        if not proof_is_valid or not hashes_match:
            return False
        converted_block = Block(block[index_str], block[previous_hash_str],
                                block[proof_str], transactions,
                                block[timestamp_str])
        self.__chain.append(converted_block)
        stored_transaction = self.__open_transactions[:]
        for itx in block[transactions_str]:
            for opentx in stored_transaction:
                if opentx.sender == itx[sender_str] and opentx.recipient == itx[recipient_str] and opentx.amount == \
                        itx[amount_str] and opentx.signature == itx[signature_str]:
                    try:
                        self.__open_transactions.remove(opentx)
                    except ValueError:
                        print('item is removed')

        self.save_data()
        return True
Example #3
0
 def proof_of_work(self):
     last_block = self.chain[-1]
     last_hash = hash_block(last_block)
     proof = 0
     while not Verification.valid_proof(self.kind, last_hash, proof):
         proof += 1
     return proof
Example #4
0
 def proof_of_work(self):
     last_block = self.__chain[-1]
     last_hash = hash_block(last_block)
     proof = 0
     while not Verification.valid_proof(self.__open_transactions, last_hash,
                                        proof):
         proof += 1
     return proof
Example #5
0
 def proof_of_work(self, transaction):
     # -1 for last block
     last_block = self.__chain[-1]
     last_hash = hash_block(last_block)
     proof = 0
     # just increase nonce for valid_proof
     while not Verification.valid_proof(transaction, last_hash, proof):
         proof += 1
     return proof
 def proof_of_work(self):
     """Generate a proof of work for the open transactions, the hash of the previous block and a random number (which is guessed until it fits)."""
     last_block = self.__chain[-1]
     last_hash = hash_block(last_block)
     proof = 0
     # Try different PoW numbers and return the first valid one
     while not Verification.valid_proof(self.__open_transactions, last_hash,
                                        proof):
         proof += 1
     return proof