def test_from_dict(self): """Test transaction creation from dict""" d = {'sender': 's', 'receiver': 'r', 'payload': '1', 'signature': 'sig'} transaction = Transaction.from_dict(d) self.assertTrue(isinstance(transaction, Transaction)) self.assertEqual("r", transaction.receiver) self.assertEqual("sig", transaction.signature) self.assertEqual("1", transaction.payload) self.assertEqual("s", transaction.sender)
def from_dict(data_dict): """Instantiate a Block from a data dictionary.""" return Block(block_id=data_dict['nr'], merkle_tree_root=data_dict['merkleHash'], predecessor_hash=data_dict['predecessorBlock'], block_creator_id=data_dict['creator'], transactions=[ Transaction.from_dict(transaction_dict) for transaction_dict in data_dict['transactions'] ], nonce=data_dict['nonce'], timestamp=data_dict['timestamp'], difficulty=data_dict['difficulty'])
def requestTransaction(self, transaction_hash): """Returns the tuple (transaction, block hash of transaction).""" responses = self._bulk_send('requestTransaction', [transaction_hash], return_on_first_success=True) if responses: if responses[0]: transaction, block_hash = responses[0] return Transaction.from_dict(transaction), block_hash else: raise TransactionDoesNotExistException() else: raise NoPeersException( 'No nodes available to request the transaction from')
def from_dict(data_dict, consesnus_obj=None): """Instantiate a LogicalBlock from a data dictionary.""" _transactions = [] if data_dict['transactions']: _transactions = data_dict['transactions'] return LogicalBlock(block_id=data_dict['nr'], merkle_tree_root=data_dict['merkleHash'], predecessor_hash=data_dict['predecessorBlock'], block_creator_id=data_dict['creator'], transactions=[ Transaction.from_dict(transaction_dict) for transaction_dict in _transactions ], nonce=data_dict['nonce'], difficulty=data_dict['difficulty'], timestamp=data_dict['timestamp'], consensus_obj=consesnus_obj)
def __handle_send_transaction(self, transaction_data): transaction = Transaction.from_dict(transaction_data) transaction_hash = self.crypto_helper.hash(transaction.get_json()) transaction_in_pool, _ = self.get_transaction_callback( transaction_hash) self.on_transaction_received_callback(transaction) if not self.get_transaction_callback(transaction_hash)[0]: # if transaction still not present, it must have been delined self.__add_transaction_to_cache(transaction) if not transaction_in_pool == transaction and not self.__transaction_in_cache( transaction): logger.debug('Broadcasting transaction: {}'.format( str(transaction))) logger.debug('Broadcasting transaction hash: ' + str(transaction_hash)) try: self._call_threaded(self.sendTransaction, [transaction]) except NoPeersException: pass