def add_block(self, block): """ Add an already created Block when broadcasting Blocks from different peers. :param block: the Block that will be added. :return: if the addition was successfully. """ transactions = [ Transaction( transaction['sender'], transaction['recipient'], transaction['amount'], transaction['signature']) for transaction in block['transactions'] ] proof_is_valid = Verification.valid_proof(transactions[:-1], block['previous_hash'], block['proof']) hashes_match = hash_util.hash_block(self.get_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_transaction in block['transactions']: for open_transaction in stored_transactions: if open_transaction.sender == incoming_transaction['sender'] \ and open_transaction.recipient == incoming_transaction['recipient'] \ and open_transaction.amount == incoming_transaction['amount'] \ and open_transaction.signature == incoming_transaction['signature']: try: self.__open_transactions.remove(open_transaction) except ValueError: print('The transaction 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() return True
def add_block(self, block): # Create a list of transaction objects with the content in the dictionary key of the block 'transaction' transactions = [ Transaction(tx['sender'], tx['recipient'], tx['signature'], tx['amount']) for tx in block['transactions'] ] # call the valid_proof without the last transaction because is the MINING reward transaction proof_is_valid = Verification.valid_proof(transactions[:-1], block['previous_hash'], block['proof']) hashes_match = create_hash_block( self.get_blockchain()[-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.__blockchain.append(converted_block) stored_transactions = self.__open_transactions[:] # show if the incoming transactions are part of the open transactions and remove it if it´s the case 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
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
def proof_of_work(self) -> int: last_hash = hash_block(self.__chain[-1]) proof = 0 print('Proof of work...') while not Verification.valid_proof(self.__open_transactions, last_hash, proof): proof += 1 return proof
def proof_of_work(self): # get the first list element from the right last_block = self.__blockchain[-1] last_hash = create_hash_block(last_block) proof = 0 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 while not Verification.valid_proof(self.__open_transactions, last_hash, proof): proof += 1 return proof
def proof_of_work(self): """ Generates a proof of work for the open transactions, based on the last hashed block which is guessed until it fits. :return: a valid number for the proof of work. """ last_block = self.__chain[-1] last_hash = hash_util.hash_block(last_block) proof = 0 while not Verification.valid_proof(self.__open_transactions, last_hash, proof): proof += 1 return proof
def proof_of_work(self): """ Calculates proof of work for mining a new block """ last_block = self.get_last_blockchain_value() last_hash = hash_block(last_block) proof = 0 while not Verification.valid_proof(self.__open_transactions, last_hash, proof): proof += 1 return proof
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 self.__chain.append( Block(block['index'], block['previous_hash'], transactions, block['proof'], block['timestamp'])) self.save_data() return True
def proof_of_work(self): """ Function to find the valid proof of work that the proof validation condition Returns ------- proof (integer): Proof of work which satisfies condition """ logger.info('Finding proof of work') 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