def process_txs_message( self, msg: TxsMessage ) -> Set[MissingTransactions]: missing_transactions: Set[MissingTransactions] = set() transactions = msg.get_txs() missing: bool for transaction in transactions: transaction_hash, transaction_contents, short_id = transaction missing = False assert transaction_hash is not None assert short_id is not None transaction_key = self.get_transaction_key(transaction_hash) if not self.has_short_id(short_id): self.assign_short_id_by_key(transaction_key, short_id) missing = True if not self.has_transaction_contents_by_key(transaction_key): assert transaction_contents is not None self.set_transaction_contents_by_key(transaction_key, transaction_contents) missing = True if missing: missing_transactions.add(MissingTransactions(short_id, transaction_hash)) return missing_transactions
def test_txs_with_short_ids_message(self): txs_info = [ TransactionInfo(Sha256Hash(helpers.generate_bytearray(32)), helpers.generate_bytearray(200), 111), TransactionInfo(Sha256Hash(helpers.generate_bytearray(32)), helpers.generate_bytearray(300), 222), TransactionInfo(Sha256Hash(helpers.generate_bytearray(32)), helpers.generate_bytearray(400), 333) ] msg = TxsMessage(txs=txs_info) msg_bytes = msg.rawbytes() self.assertTrue(msg_bytes) parsed_msg = TxsMessage(buf=msg_bytes) self.assertTrue(parsed_msg) parsed_txs_info = parsed_msg.get_txs() self.assertEqual(len(parsed_txs_info), len(txs_info)) for index in range(len(txs_info)): self.assertEqual(parsed_txs_info[index].short_id, txs_info[index].short_id) self.assertEqual(parsed_txs_info[index].contents, txs_info[index].contents) self.assertEqual(parsed_txs_info[index].hash, txs_info[index].hash)
def msg_txs(self, msg: TxsMessage): if ConnectionType.RELAY_TRANSACTION not in self.CONNECTION_TYPE: self.log_error(log_messages.UNEXPECTED_TXS_ON_NON_RELAY_CONN, msg) return transactions = msg.get_txs() tx_service = self.node.get_tx_service() tx_stats.add_txs_by_short_ids_event( map(lambda x: x.short_id, transactions), TransactionStatEventType. TX_UNKNOWN_SHORT_IDS_REPLY_RECEIVED_BY_GATEWAY_FROM_RELAY, network_num=self.node.network_num, peer=stats_format.connection(self), found_tx_hashes=map(lambda x: convert.bytes_to_hex(x.hash.binary), transactions)) for transaction in transactions: tx_hash, transaction_contents, short_id = transaction assert tx_hash is not None assert short_id is not None self.node.block_recovery_service.check_missing_sid(short_id) if not tx_service.has_short_id(short_id): tx_service.assign_short_id(tx_hash, short_id) self.node.block_recovery_service.check_missing_tx_hash(tx_hash) if not tx_service.has_transaction_contents(tx_hash): assert transaction_contents is not None tx_service.set_transaction_contents(tx_hash, transaction_contents) tx_stats.add_tx_by_hash_event( tx_hash, TransactionStatEventType. TX_UNKNOWN_TRANSACTION_RECEIVED_BY_GATEWAY_FROM_RELAY, self.node.network_num, short_id, peer=stats_format.connection(self)) self.node.block_processing_service.retry_broadcast_recovered_blocks( self) for block_awaiting_recovery in self.node.block_recovery_service.get_blocks_awaiting_recovery( ): self.node.block_processing_service.schedule_recovery_retry( block_awaiting_recovery)
def msg_txs(self, msg: TxsMessage, recovered_txs_source: RecoveredTxsSource = RecoveredTxsSource. TXS_RECOVERED): transactions = msg.get_txs() tx_service = self.node.get_tx_service() missing_txs: Set[MissingTransactions] = tx_service.process_txs_message( msg) tx_stats.add_txs_by_short_ids_event( map(lambda x: x.short_id, transactions), TransactionStatEventType. TX_UNKNOWN_SHORT_IDS_REPLY_RECEIVED_BY_GATEWAY_FROM_RELAY, network_num=self.node.network_num, peers=[self], found_tx_hashes=map(lambda x: convert.bytes_to_hex(x.hash.binary), transactions)) for (short_id, transaction_hash) in missing_txs: self.node.block_recovery_service.check_missing_sid( short_id, recovered_txs_source) self.node.block_recovery_service.check_missing_tx_hash( transaction_hash, recovered_txs_source) tx_stats.add_tx_by_hash_event( transaction_hash, TransactionStatEventType. TX_UNKNOWN_TRANSACTION_RECEIVED_BY_GATEWAY_FROM_RELAY, self.node.network_num, short_id, peers=[self]) self.node.block_processing_service.retry_broadcast_recovered_blocks( self) for block_awaiting_recovery in self.node.block_recovery_service.get_blocks_awaiting_recovery( ): self.node.block_processing_service.schedule_recovery_retry( block_awaiting_recovery)