def add_block(self, block):
     block['transactions'] = [
         Transaction(tx['sender'], tx['recipient'], tx['signature'],
                     tx['amount']) for tx in block['transactions']
     ]
     block = Block(block['previous_hash'], block['index'],
                   block['transactions'], block['proof'])
     proof_is_valid = Verification.valid_proof(block.transactions[:-1],
                                               block.previous_hash,
                                               block.proof)
     hashes_match = hash_block(self.chain[-1]) == block.previous_hash
     if proof_is_valid and hashes_match:
         self.__chain.append(block)
         stored_transactions = self.__open_transactions[:]
         for itx in block.transactions:
             for opentx in stored_transactions:
                 if (itx.sender == opentx.sender
                         and itx.recipient == opentx.recipient
                         and itx.signature == opentx.signature
                         and itx.amount == opentx.amount):
                     try:
                         self.__open_transactions.remove(opentx)
                     except ValueError:
                         print('Item was already removed.')
         self.save_data()
         return True
     else:
         return False
Exemple #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.valid_proof(transactions[:-1],
                                               block['previous_hash'],
                                               block['proof'])
     # print(proof_is_valid)
     # we check if the hash of the last block is equal with the previous_hash of the incomming block
     hashes_match = hash_util.hash_block(
         self.chain[-1]) == block['previous_hash']
     print(hashes_match)
     if not hashes_match or not proof_is_valid:
         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']:
                 # remove all the transactions that are redendent (dans le premier et le 2eme serveur)
                 try:
                     self.open_transactions.remove(opentx)
                 except ValueError:
                     print('item was already removed')
     self.save_data()
     return True
Exemple #3
0
	def add_block(self, block):
		# Receiving broadcasting block
		# input block format is in DICT
		# valid_proof should be avoid MINING REWARD
		transactions_to_valid_proof = [Transaction(tx["sender"],tx["recipient"],tx["signature"],tx["amount"]) for tx in block["transactions"][:-1]]
		transactions_to_final_add = [Transaction(tx["sender"],tx["recipient"],tx["signature"],tx["amount"]) for tx in block["transactions"]]
		proof_is_valid = Verification.valid_proof(transactions_to_valid_proof , block["previous_hash"], block["proof"])
		hashes_match = hash_block(self.get_chain()[-1]) == block["previous_hash"]
		print(proof_is_valid)
		print(hashes_match)
		if proof_is_valid and hashes_match:
			self.__chain.append(Block(block["index"],block["previous_hash"],transactions_to_final_add,block["proof"],block["time_stamp"]))
			# Clean the Open_transactions
			# ISSUE: SAME SENDER & SAME RECIPIENT & SAME AMOUNT & SAME SIGNATURE
			# still need to solve!!
			stored_local_open_trans = self.__open_transactions[:]
			for in_trans in block["transactions"]:
				for op_tx in stored_local_open_trans:
					if in_trans["sender"] == op_tx.sender and in_trans["recipient"] == op_tx.recipient and in_trans["amount"] == op_tx.amount and in_trans["signature"] == op_tx.signature:
						try:
							self.__open_transactions.remove(op_tx)
						except ValueError:
							print("Transaction already removed.")
			self.save_data()
			return True
		else:
			return False
Exemple #4
0
    def add_block(self, block):
        transactions = [
            Transaction(tx['sender'], tx['receiver'], tx['signature'],
                        tx['details'], tx['timestamp'])
            for tx in block['transactions']
        ]
        proof_isvalid = Verification.valid_proof(transactions,
                                                 block['previous_hash'],
                                                 block['proof'])
        hashes_match = hash_block(self.__chain[-1]) != block['previous_hash']
        if not proof_isvalid 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 incomingtx in block['transactions']:
            for opentx in stored_transactions:
                print(opentx == incomingtx)
                if opentx.sender == incomingtx[
                        'sender'] and opentx.receiver == incomingtx[
                            'receiver'] and opentx.details == incomingtx[
                                'details'] and opentx.signature == incomingtx[
                                    'signature']:
                    try:
                        self.__open_transactions.remove(opentx)
                    except ValueError:
                        print('Item was already removed')

        self.save_data()
        return True
Exemple #5
0
 def add_block(self,
               block):  #add existing block instead of mining a new one
     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']
     )  #check all but last because it is the reward transaction and is obviously not present before
     hashes_match = hash_block(self.chain[-1]) == block[
         'previous_hash']  #check if hash of last block matches the correspondent one from the incoming block
     if not proof_is_valid or not hashes_match:
         return False
     converted_block = Block(block['index'], block['previous_hash'],
                             transactions, block['proof'],
                             block['timestamp'])  #convert object
     self.__chain.append(converted_block)
     stored_transactions = self.__open_transactions[:]  #make a copy to be able to do a for loop without breaking it
     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 was already removed')
     self.save_data()  #update stored data for peer node
     return True
 def add_block(self, block):
     rezultate = [
         Rezultat(rez['emitator'], rez['receptor'], rez['info_didactic'],
                  rez['semnatura']) for rez in block['rezultate']
     ]
     proof_valid = Verification.valid_proof(rezultate,
                                            block['previous_hash'],
                                            block['proof'])
     hashes_match = hash_block(self.chain[-1]) == block['previous_hash']
     if not proof_valid or not hashes_match:
         return False
     bloc_convertit = Block(block['index'], block['previous_hash'],
                            rezultate, block['proof'], block['timestamp'])
     self.__chain.append(bloc_convertit)
     stored_rezultate = self.__date_de_introdus[:]
     for dDI in block['rezultate']:
         for openDate in stored_rezultate:
             if openDate.emitator == dDI[
                     'emitator'] and openDate.receptor == dDI[
                         'receptor'] and openDate.semnatura == dDI[
                             'semnatura']:
                 try:
                     self.__date_de_introdus.remove(openDate)
                 except ValueError:
                     print('Element deja eliminat')
     self.save_data()
     return True
Exemple #7
0
    def add_block(self, block):
        # converting the received dictionary transactions to transaction objects
        transactions = [
            Transaction(tx['sender'], tx['recipient'], tx['amount'],
                        tx['signature']) 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)
        # Removing transaction from open_transaction if they are in the mined 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 was alredy removed')

        self.save_data()
        return True
 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 incoming_tx in block["transactions"]:
         for opentx in stored_transactions:
             if opentx.sender == incoming_tx[
                     "sender"] and opentx.recipient == incoming_tx[
                         "recipient"] and opentx.signature == incoming_tx[
                             "signature"] and opentx.amount == incoming_tx[
                                 "amount"]:
                 try:
                     self.__open_transactions.remove(opentx)
                 except ValueError:
                     print("Item was already removed.")
     self.save_data()
     return True
Exemple #9
0
 def add_block(self, block):
     transactions = [
         Transaction(tx['sender'], tx['recipient'], tx['signature'],
                     tx['amount'], tx['meomo'])
         for tx in block['transactions']
     ]
     # check ours
     proof_is_valid = Verification.valid_proof(transactions[:-1],
                                               block['previous_hash'],
                                               block['proof'])
     # check peers
     hashes_match = hash_block(self.chain[-1]) == block['previous_hash']
     if not proof_is_valid or not hashes_match:
         return False
     # block is still a dictionary, need to convert first
     converted_block = Block(block['index'], block['previous_hash'],
                             transactions, block['proof'],
                             block['timestamp'])
     self.__chain.append(converted_block)
     # update open transactions before saving data
     stored_transactions = self.__open_transactions[:]
     for incoming_tx in block['transactions']:
         for opentx in stored_transactions:
             if (opentx.sender == incoming_tx['sender']
                     and opentx.recipient == incoming_tx['recipient']
                     and opentx.amount == incoming_tx['amount']
                     and opentx.signature == incoming_tx['signature']
                     and opentx.memo == incoming_tx['memo']):
                 try:
                     self.__open_transactions.remove(opentx)
                 except ValueError:
                     print('Item was already removed.')
     self.save_data()
     return True
Exemple #10
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:
         print('failed to validate', proof_is_valid, hashes_match)
         return False
     print('validated before adding')
     converted_block = Block(block['index'], block['previous_hash'],
                             transactions, block['proof'],
                             block['timestamp'])
     self.__chain.append(converted_block)
     stored_transaction = self.__open_transactions[:]
     for in_tx in transactions:
         for op_tx in stored_transaction:
             if in_tx.sender == op_tx.sender and in_tx.recipient == op_tx.recipient and in_tx.amount == op_tx.amount:
                 try:
                     self.__open_transactions.remove(op_tx)
                 except ValueError:
                     print('Value already removed')
                 finally:
                     break
     self.save_data()
     return True
Exemple #11
0
    def add_block(self, block):
        transactions = [
            Transaction(tx['sender'], tx['recipient'], tx['signature'],
                        tx['amount']) for tx in block['transactions']
        ]
        # Excluding the last transaction, because that is the "reward" transaction
        valid_proof = Verification.valid_proof(transactions[:-1],
                                               block['previous_hash'],
                                               block['proof'])
        hashes_match = hash_block(self.chain[-1]) == block['previous_hash']
        if not valid_proof 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 was already removed')
        self.save_data()
        return True
Exemple #12
0
    def add_block(self, block):
        transactions = [Transaction(
            tx['sender'], tx['recipient'], tx['amount'], tx['signature'])
            for tx in block['transactions']]

        proof_is_valid = Verification.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:
            raise BlockError()

        current_block = Block(block['index'], block['previous_hash'],
                              transactions, block['proof'], block['timestamp'])

        self.__chain.append(current_block)
        stored_transactions = self.__open_tansactions[:]

        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_tansactions.remove(opentx)
                    except ValueError:
                        print('items was already remove')
                        continue

        self.save_data()
Exemple #13
0
 def add_block(self, block):
     # Validate block, check pow and store
     # Must convert transactions to a list, because verification expects data in that format. <dictionary to a list of transaction objects>
     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']
     )  #Passing in a list of all transactions of block being received when validating pow.
     #Avoid last block in chain as it contains the reward transaction and will invalidate it.
     # #Usually calculate pow before adding reward transaction (e.g. mine_block).
     # using [:-1] avoids using the reward transaction as part of the transactions used to validate the incoming pow which wont work.
     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 all fields are equal it is the same transaction
             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
Exemple #14
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[:]
     # looping through all the incoming transactions
     for itx in block['transactions']:
         for opentx in stored_transactions:
             # opentx is an object and itx is an dictionary
             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 allready removed')
     self.save_data()
     return True
Exemple #15
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
Exemple #16
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'],
                                               GUESS_HASS_PROOF_STRING)
     hashes_match = HashUtil.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:
                     Log.log_error('ValueError', 'Item was already removed',
                                   self.node_id)
     self.save_data()
     return True
 def add_block(self, block):
     """Add a block which was received via broadcasting to the local blockchain."""
     # Create a list of transaction objects
     transactions = [Transaction(
         tx['sender'], tx['recipient'], tx['signature'], tx['amount']) for tx in block['transactions']]
     # Validate the proof of work of the block and store the result (True or False) in a variable
     proof_is_valid = Verification.valid_proof(
         transactions[:-1], block['previous_hash'], block['proof'])
     # Check if previous_hash stored in the block is equal to the local blockchain's last block's hash and store the result in a block
     hashes_match = hash_block(self.chain[-1]) == block['previous_hash']
     if not proof_is_valid or not hashes_match:
         return False
     # Create a Block object
     converted_block = Block(
         block['index'], block['previous_hash'], transactions, block['proof'], block['timestamp'])
     self.__chain.append(converted_block)
     stored_transactions = self.__open_transactions[:]
     # Check which open transactions were included in the received block and remove them
     # This could be improved by giving each transaction an ID that would uniquely identify it
     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 was already removed')
     self.save_data()
     return True
Exemple #18
0
 def add_block(self, block):
     """Adds a blockto the local copy of the peer nodes"""
     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[:]
     #check if an incoming transaction is an open transaction or not
     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 was already removed')
     self.save_data()
     return True
Exemple #19
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'])
     hashees_match = hash_block(self.chain[-1]) == block['previous_hash']
     if not proof_is_valid or not hashees_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 já foi removido!')
     self.save_data()
     return True
Exemple #20
0
    def add_block(self, block):
        transactions = [
            Transaction(tx['sender'], tx['recipient'], tx['signature'],
                        tx['amount']) for tx in block['transactions']
        ]
        is_valid_prood = Verification.valid_proof(transactions[:-1],
                                                  block['previous_hash'],
                                                  block['proof'])
        hashes_match = hash_block(self.chain[-1]) == block['previous_hash']
        if not is_valid_prood or not hashes_match:
            return False
        block_object = Block(block['index'], block['previous_hash'],
                             transactions, block['proof'], block['timestamp'])
        self.__chain.append(block_object)

        # Make a copy becuaes we are manipkauting the original and dont wan to iterate on it
        open_trns = self.__open_transactions[:]

        # Could possibly refactor for better perfomance
        # Update the open trnasaction on the peer node when a new block is braodcast
        for incoming_trn in block['transactions']:
            for open_trn in open_trns:
                if (open_trn.sender == incoming_trn['sender']
                        and open_trn.recipient == incoming_trn['recipient']
                        and open_trn.amount == incoming_trn['amount']
                        and open_trn.signature == incoming_trn['signature']):
                    try:
                        self.__open_transactions.remove(open_trn)
                    except ValueError:
                        print("Item is already removed")

        self.save_data()
        return True
Exemple #21
0
 def add_block(self, block):
     """Add a block received as dictionary to the blockchain."""
     transactions = [
         Transaction(tx['sender'],
                     tx['recipient'],
                     tx['signature'],
                     tx['amount'])
         for tx in block['transactions']
     ]
     proof_is_valid = verifier.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 was already removed")
     self.save_data()
     return True
Exemple #22
0
 def add_block(self, block):  # block is dict here
     # transactions is a list of transaction object
     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)
     """
     update open transaction on peer node, when broadcast block to peer node
     the some open transactions on peer node should be removed because it will be store in new block
     """
     stored_transactions = self.__open_transactions[:]
     for itx in block['transactions']:  # itx is incoming tx
         for opentx in stored_transactions:  # opentx is open transaction of node
             # for every incoming transaction, check if it is part of my open transaction
             if opentx.sender == itx['sender'] and opentx.recipient == itx[
                     'recipient'] and opentx.amount == itx[
                         'amount'] and opentx.signature == itx['signature']:
                 # if same, try removing it from peer node to prevent encountering second time
                 try:
                     self.__open_transactions.remove(opentx)
                 except ValueError:
                     print('Item was already removed')
     self.save_data()
     return True
    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 was already removed')

        self.save_data()
        for node in self.__peer_nodes:
            url = 'http://{}/broadcast-block'.format(node)
            converted_block = block.__dict__.copy()
            converted_block['transactions'] = [
                tx.__dict__ for tx in converted_block['transactions']]
            try:
                response = requests.post(url, json={'block': converted_block})
                if response == 400 or response.status_code == 500:
                    print('Block declined, needs resolving')
                if response.status_code == 409:
                    self.resolve_conflicts = True
            except requests.exceptions.ConnectionError:
                continue
        return block
Exemple #24
0
 def proof_of_work(self):
     """Generate a proof of work to commit a new block to the blockchain."""
     last_block = self.__chain[-1]
     last_hash = hash_block(last_block)
     proof = 0
     while not verifier.valid_proof(self.__open_transactions, last_hash, proof):
         proof += 1
     return proof
Exemple #25
0
 def proof_of_work(self):
     last_block = self.__chain[-1]
     last_hash = hash_block(last_block)
     Nonce = 0
     while not Verification.valid_proof(self.__open_transactions, last_hash,
                                        Nonce):
         Nonce += 1
     return Nonce
Exemple #26
0
 def proof_of_work(self):
     last_block = self.__chain[-1]
     last_hash = HashUtil.hash_block(last_block)
     proof = 0
     while not Verification.valid_proof(self.__open_transactions, last_hash,
                                        proof, GUESS_HASS_PROOF_STRING):
         proof += 1
     return proof
 def proof_of_work(self):
     last_block = self.__chain[-1]
     last_hash = hash_block(last_block)
     proof = 0
     while not Verification.valid_proof(self.__date_de_introdus, last_hash,
                                        proof):
         proof += 1
     return proof
Exemple #28
0
 def proof_of_work(self):
     last_block = self.get_last_blockchain_value()
     last_hash = hash_block(last_block)
     proof = 0
     while not Verification.valid_proof(self.__open_votes, last_hash,
                                        proof):
         proof += 1
     return proof
 def proof_of_work(self):
     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
Exemple #30
0
 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
Exemple #31
0
 def proof_of_work(self):
     """ Generate a proof of work for the open transactions, the hash of the previous block and the proof number """
     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
Exemple #32
0
 def proof_of_work(self):
     last_block = self.__chain[-1]
     last_hash = hash_block(last_block)
     proof = 0
     # This is how we can access the valid_proof function from our verification script
     while not Verification.valid_proof(self.__open_transactions, 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
     # verifier = Verification()
     while not Verification.valid_proof(self.__open_transactions, last_hash, proof):
         proof += 1
     return proof