def compare_txs_old_to_current( self, converted_current_message: TxsMessage, original_current_message: TxsMessage, ): self.assertEqual( original_current_message.rawbytes(), converted_current_message.rawbytes(), )
def test_get_txs_multiple_sid_assignments(self): block = self.ont_block() transactions = self.ont_transactions(block) # assign short ids that the local connection won't know about until it gets the txs message remote_transaction_service1 = ExtensionTransactionService( MockNode(gateway_helpers.get_gateway_opts(8999)), 0) short_id_mapping1 = {} for i, transaction in enumerate(transactions): remote_transaction_service1.assign_short_id( transaction.tx_hash(), i + 1) remote_transaction_service1.set_transaction_contents( transaction.tx_hash(), transaction.tx()) short_id_mapping1[transaction.tx_hash()] = TransactionInfo( transaction.tx_hash(), transaction.tx(), i + 1) txs_message_1 = TxsMessage([tx for tx in short_id_mapping1.values()]) self.sut.msg_txs(txs_message_1) for transaction_hash, tx_info in short_id_mapping1.items(): transaction_key = self.gateway_node.get_tx_service( ).get_transaction_key(transaction_hash) self.assertEqual( tx_info.short_id, self.gateway_node.get_tx_service().get_short_id_by_key( transaction_key)) stored_hash, stored_content, _ = self.gateway_node.get_tx_service( ).get_transaction(tx_info.short_id) self.assertEqual(transaction_hash, stored_hash) self.assertEqual(tx_info.contents, stored_content) remote_transaction_service2 = ExtensionTransactionService( MockNode(gateway_helpers.get_gateway_opts(8999)), 0) short_id_mapping2 = {} for i, transaction in enumerate(transactions): remote_transaction_service2.assign_short_id( transaction.tx_hash(), i + 101) remote_transaction_service2.set_transaction_contents( transaction.tx_hash(), transaction.tx()) short_id_mapping2[transaction.tx_hash()] = TransactionInfo( transaction.tx_hash(), transaction.tx(), i + 101) txs_message_2 = TxsMessage([tx for tx in short_id_mapping2.values()]) self.sut.msg_txs(txs_message_2) for transaction_hash, tx_info in short_id_mapping2.items(): stored_hash, stored_content, _ = self.gateway_node.get_tx_service( ).get_transaction(tx_info.short_id) self.assertEqual(transaction_hash, stored_hash) self.assertEqual(tx_info.contents, stored_content)
def test_get_txs_block_recovery_encrypted(self): block: BlockOntMessage = self.ont_block() transactions: List[TxOntMessage] = self.ont_transactions(block) # assign short ids that the local connection won't know about until it gets the txs message remote_transaction_service = ExtensionTransactionService( MockNode(gateway_helpers.get_gateway_opts(8999)), 0) short_id_mapping = {} for i, transaction in enumerate(transactions): tx_hash = transaction.tx_hash() remote_transaction_service.assign_short_id(tx_hash, i + 1) remote_transaction_service.set_transaction_contents( tx_hash, transaction.rawbytes()) short_id_mapping[tx_hash] = TransactionInfo( tx_hash, transaction.rawbytes(), i + 1) bx_block = bytes( self.gateway_node.message_converter.block_to_bx_block( block, remote_transaction_service)[0]) self.gateway_node.block_recovery_service.add_block = \ MagicMock(wraps=self.gateway_node.block_recovery_service.add_block) self.gateway_node.send_msg_to_node = MagicMock() self.gateway_node.broadcast = MagicMock() key, ciphertext = symmetric_encrypt(bx_block) block_hash = crypto.double_sha256(ciphertext) key_message = KeyMessage(Sha256Hash(block_hash), DEFAULT_NETWORK_NUM, "", key) broadcast_message = BroadcastMessage(Sha256Hash(block_hash), DEFAULT_NETWORK_NUM, "", BroadcastMessageType.BLOCK, True, ciphertext) self.sut.msg_broadcast(broadcast_message) self.gateway_node.broadcast.reset_mock() self.sut.msg_key(key_message) self.gateway_node.block_recovery_service.add_block.assert_called_once() self.assertEqual(2, self.gateway_node.broadcast.call_count) recovery_broadcast = self.gateway_node.broadcast.call_args_list[0] ((gettxs_message, ), recovery_kwargs) = recovery_broadcast self.assertIsInstance(gettxs_message, GetTxsMessage) self.assertIn(ConnectionType.RELAY_TRANSACTION, recovery_kwargs["connection_types"]) key_broadcast = self.gateway_node.broadcast.call_args_list[1] ((key_message, _conn), recovery_kwargs) = key_broadcast self.assertIsInstance(key_message, KeyMessage) self.assertIn(ConnectionType.GATEWAY, recovery_kwargs["connection_types"]) txs = [tx for tx in short_id_mapping.values()] txs_message = TxsMessage(txs=txs) self.sut.msg_txs(txs_message) self._assert_block_sent(block)
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 txs_message(self) -> TxsMessage: return TxsMessage([ TransactionInfo( helpers.generate_object_hash(), helpers.generate_bytearray(250), i, ) for i in range(1, 10) ])
def _build_txs_message(self, short_ids: List[int]): txs: List[TransactionInfo] = [] for short_id in short_ids: transaction = self.transactions_by_short_id[short_id] txs.append( TransactionInfo(transaction.tx_hash(), transaction.tx_val(), transaction.short_id())) return TxsMessage(txs)
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 test_get_txs_block_recovery(self): block: BlockOntMessage = self.ont_block() transactions: List[TxOntMessage] = self.ont_transactions(block) # assign short ids that the local connection won't know about until it gets the txs message remote_transaction_service = ExtensionTransactionService( MockNode(gateway_helpers.get_gateway_opts(8999)), 0) short_id_mapping = {} for i, transaction in enumerate(transactions): tx_hash = transaction.tx_hash() remote_transaction_service.assign_short_id(tx_hash, i + 1) remote_transaction_service.set_transaction_contents( tx_hash, transaction.rawbytes()) short_id_mapping[tx_hash] = TransactionInfo( tx_hash, transaction.rawbytes(), i + 1) bx_block = bytes( self.gateway_node.message_converter.block_to_bx_block( block, remote_transaction_service, True, self.gateway_node.network.min_tx_age_seconds)[0]) self.gateway_node.block_recovery_service.add_block = \ MagicMock(wraps=self.gateway_node.block_recovery_service.add_block) self.gateway_node.broadcast = MagicMock() broadcast_message = BroadcastMessage(block.block_hash(), DEFAULT_NETWORK_NUM, "", BroadcastMessageType.BLOCK, False, bytearray(bx_block)) self.sut.msg_broadcast(broadcast_message) self.gateway_node.block_recovery_service.add_block.assert_called_once() self.assertEqual(1, self.gateway_node.broadcast.call_count) recovery_broadcast = self.gateway_node.broadcast.call_args_list[0] ((gettxs_message, ), recovery_kwargs) = recovery_broadcast self.assertIsInstance(gettxs_message, GetTxsMessage) self.assertIn(ConnectionType.RELAY_TRANSACTION, recovery_kwargs["connection_types"]) txs = [tx for tx in short_id_mapping.values()] txs_message = TxsMessage(txs=txs) self.sut.msg_txs(txs_message) self._assert_block_sent(block)
def process_txs_message( self, msg: TxsMessage ) -> Set[MissingTransactions]: msg_bytes = msg.rawbytes() missing_transactions: Set[MissingTransactions] = set() result_buffer = memoryview(self.proxy.process_txs_msg(tpe.InputBytes(msg_bytes))) # result_buffer is a bytearray that consists of # number of missing transactions - 2 bytes # a list of: # short_id - 4 bytes # Sha256Hash - 32 bytes # content length that was added to transaction service - 4 bytes offset = 0 missing_transactions_count, = struct.unpack_from("<H", result_buffer, offset) offset += constants.UL_SHORT_SIZE_IN_BYTES if missing_transactions_count == 0: return missing_transactions while offset < len(result_buffer): short_id, = struct.unpack_from("<L", result_buffer, offset) offset += constants.SID_LEN transaction_hash = Sha256Hash(result_buffer[offset:offset + crypto.SHA256_HASH_LEN]) offset += crypto.SHA256_HASH_LEN content_length, = struct.unpack_from("<L", result_buffer, offset) offset += constants.UL_INT_SIZE_IN_BYTES if content_length > 0: self.set_transaction_contents_base( transaction_hash, transaction_hash, True, 0, False, None, content_length ) missing_transactions.add(MissingTransactions(short_id, transaction_hash)) return missing_transactions
def test_message_preview_success_all_types(self): self.get_message_preview_successfully(HelloMessage(protocol_version=1, network_num=2), HelloMessage.MESSAGE_TYPE, VersionMessage.VERSION_MESSAGE_LENGTH + UL_INT_SIZE_IN_BYTES + NODE_ID_SIZE_IN_BYTES - UL_INT_SIZE_IN_BYTES) self.get_message_preview_successfully(AckMessage(), AckMessage.MESSAGE_TYPE, constants.CONTROL_FLAGS_LEN) self.get_message_preview_successfully(PingMessage(), PingMessage.MESSAGE_TYPE, 9) self.get_message_preview_successfully(PongMessage(), PongMessage.MESSAGE_TYPE, 9) blob = bytearray(1 for _ in range(4)) self.get_message_preview_successfully(BroadcastMessage(self.HASH, 1, self.NODE_ID, True, blob), BroadcastMessage.MESSAGE_TYPE, SHA256_HASH_LEN + NETWORK_NUM_LEN + BLOCK_ENCRYPTED_FLAG_LEN + constants.NODE_ID_SIZE_IN_BYTES + len(blob) + constants.CONTROL_FLAGS_LEN) self.get_message_preview_successfully(TxMessage(self.HASH, 1, self.NODE_ID, 12, blob), TxMessage.MESSAGE_TYPE, SHA256_HASH_LEN + NETWORK_NUM_LEN + UL_INT_SIZE_IN_BYTES + constants.NODE_ID_SIZE_IN_BYTES + len(blob) + constants.CONTROL_FLAGS_LEN) self.get_message_preview_successfully(KeyMessage(self.HASH, 1, self.NODE_ID, bytearray(1 for _ in range(KEY_SIZE))), KeyMessage.MESSAGE_TYPE, SHA256_HASH_LEN + KEY_SIZE + NETWORK_NUM_LEN + constants.NODE_ID_SIZE_IN_BYTES + constants.CONTROL_FLAGS_LEN) self.get_message_preview_successfully(BlockHoldingMessage(self.HASH, 1, self.NODE_ID), BlockHoldingMessage.MESSAGE_TYPE, SHA256_HASH_LEN + NETWORK_NUM_LEN + constants.NODE_ID_SIZE_IN_BYTES + constants.CONTROL_FLAGS_LEN) get_txs = [1, 2, 3] self.get_message_preview_successfully(GetTxsMessage(get_txs), GetTxsMessage.MESSAGE_TYPE, UL_INT_SIZE_IN_BYTES + UL_INT_SIZE_IN_BYTES * len( get_txs) + constants.CONTROL_FLAGS_LEN) txs = [TransactionInfo(crypto.double_sha256(b"123"), bytearray(4), 1), TransactionInfo(crypto.double_sha256(b"234"), bytearray(8), 2)] expected_length = (UL_INT_SIZE_IN_BYTES + sum(UL_INT_SIZE_IN_BYTES + SHA256_HASH_LEN + UL_INT_SIZE_IN_BYTES + len(tx.contents) for tx in txs) + constants.CONTROL_FLAGS_LEN) self.get_message_preview_successfully(TxsMessage(txs), TxsMessage.MESSAGE_TYPE, expected_length)
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)
def test_create_message_success_all_types(self): test_network_num = 10 test_protocol_version = bloxroute_version_manager.CURRENT_PROTOCOL_VERSION hello_message = self.create_message_successfully( HelloMessage(protocol_version=test_protocol_version, network_num=test_network_num, node_id=self.NODE_ID), HelloMessage) self.assertEqual(test_protocol_version, hello_message.protocol_version()) self.assertEqual(test_network_num, hello_message.network_num()) self.assertEqual(self.NODE_ID, hello_message.node_id()) self.create_message_successfully(AckMessage(), AckMessage) self.create_message_successfully(PingMessage(), PingMessage) self.create_message_successfully(PongMessage(), PongMessage) blob = bytearray(4) broadcast_message = self.create_message_successfully( BroadcastMessage(self.HASH, network_num=test_network_num, is_encrypted=True, source_id=self.NODE_ID, blob=blob), BroadcastMessage) self.assertEqual(self.HASH, broadcast_message.block_hash()) self.assertEqual(test_network_num, broadcast_message.network_num()) self.assertEqual(self.NODE_ID, broadcast_message.source_id()) self.assertTrue(broadcast_message.is_encrypted()) self.assertEqual(blob, broadcast_message.blob().tobytes()) sid = 12 tx_val = bytes(1 for _ in range(5)) tx_message = self.create_message_successfully( TxMessage(self.HASH, network_num=test_network_num, source_id=self.NODE_ID, short_id=sid, tx_val=tx_val), TxMessage) self.assertEqual(self.HASH, tx_message.tx_hash()) self.assertEqual(self.NODE_ID, tx_message.source_id()) self.assertEqual(sid, tx_message.short_id()) self.assertEqual(test_network_num, tx_message.network_num()) self.assertEqual(tx_val, tx_message.tx_val()) key = bytearray(1 for _ in range(KEY_SIZE)) key_message = self.create_message_successfully( KeyMessage(self.HASH, test_network_num, self.NODE_ID, bytearray(1 for _ in range(KEY_SIZE))), KeyMessage) self.assertEqual(key, key_message.key()) self.assertEqual(self.NODE_ID, key_message.source_id()) self.assertEqual(test_network_num, key_message.network_num()) self.assertEqual(self.HASH, key_message.block_hash()) block_holding_message = self.create_message_successfully( BlockHoldingMessage(self.HASH, test_network_num, self.NODE_ID), BlockHoldingMessage) self.assertEqual(self.NODE_ID, block_holding_message.source_id()) self.assertEqual(test_network_num, block_holding_message.network_num()) self.assertEqual(self.HASH, block_holding_message.block_hash()) get_txs = [1, 2, 3] get_txs_message = self.create_message_successfully( GetTxsMessage(get_txs), GetTxsMessage) self.assertEqual(get_txs, get_txs_message.get_short_ids()) txs = [ TransactionInfo(Sha256Hash(crypto.double_sha256(b"123")), bytearray(4), 1), TransactionInfo(Sha256Hash(crypto.double_sha256(b"234")), bytearray(8), 2) ] txs_message = self.create_message_successfully(TxsMessage(txs), TxsMessage) result_txs = txs_message.get_txs() for i, result_tx in enumerate(result_txs): self.assertEqual(txs[i].hash, result_tx.hash) self.assertEqual(txs[i].contents, result_tx.contents) self.assertEqual(txs[i].short_id, result_tx.short_id) get_tx_contents_message = self.create_message_successfully( GetTxContentsMessage(test_network_num, sid), GetTxContentsMessage) self.assertEqual(sid, get_tx_contents_message.get_short_id()) self.assertEqual(test_network_num, get_tx_contents_message.network_num()) tx_info = TransactionInfo(Sha256Hash(crypto.double_sha256(b"123")), bytearray(4), 1) tx_contents_message = self.create_message_successfully( TxContentsMessage(test_network_num, tx_info), TxContentsMessage) self.assertEqual(test_network_num, tx_contents_message.network_num()) result_tx_info = tx_contents_message.get_tx_info() self.assertEqual(tx_info.hash, result_tx_info.hash) self.assertEqual(tx_info.contents, result_tx_info.contents) self.assertEqual(tx_info.short_id, result_tx_info.short_id) short_ids = [1, 2, 33, 4444, 1234] block_hash = Sha256Hash(helpers.generate_bytearray(32)) get_block_txs_message: GetCompressedBlockTxsMessage = self.create_message_successfully( GetCompressedBlockTxsMessage(self.NETWORK_NUM, block_hash, short_ids), GetCompressedBlockTxsMessage) self.assertEqual(self.NETWORK_NUM, get_block_txs_message.network_num()) self.assertEqual(block_hash, get_block_txs_message.block_hash()) self.assertEqual(len(short_ids), len(get_block_txs_message)) self.assertEqual(short_ids, get_block_txs_message.get_short_ids()) 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) ] block_txs_message: CompressedBlockTxsMessage = self.create_message_successfully( CompressedBlockTxsMessage(self.NETWORK_NUM, block_hash, txs_info), CompressedBlockTxsMessage) self.assertEqual(self.NETWORK_NUM, block_txs_message.network_num()) self.assertEqual(block_hash, block_txs_message.block_hash()) self.assertEqual(len(txs_info), len(block_txs_message)) parsed_txs = block_txs_message.get_txs() for index in range(len(txs_info)): self.assertEqual(parsed_txs[index].short_id, txs_info[index].short_id) self.assertEqual(parsed_txs[index].contents, txs_info[index].contents) self.assertEqual(parsed_txs[index].hash, txs_info[index].hash)
def test_message_preview_success_all_types(self): self.get_message_preview_successfully( HelloMessage(protocol_version=1, network_num=2), HelloMessage.MESSAGE_TYPE, VersionMessage.VERSION_MESSAGE_LENGTH + UL_INT_SIZE_IN_BYTES + NODE_ID_SIZE_IN_BYTES - UL_INT_SIZE_IN_BYTES) self.get_message_preview_successfully(AckMessage(), AckMessage.MESSAGE_TYPE, constants.CONTROL_FLAGS_LEN) self.get_message_preview_successfully(PingMessage(), PingMessage.MESSAGE_TYPE, 9) self.get_message_preview_successfully(PongMessage(), PongMessage.MESSAGE_TYPE, 17) blob = bytearray(1 for _ in range(4)) self.get_message_preview_successfully( BroadcastMessage(self.HASH, 1, self.NODE_ID, self.BROADCAST_TYPE, True, blob), BroadcastMessage.MESSAGE_TYPE, SHA256_HASH_LEN + NETWORK_NUM_LEN + constants.BROADCAST_TYPE_LEN + BLOCK_ENCRYPTED_FLAG_LEN + constants.NODE_ID_SIZE_IN_BYTES + len(blob) + constants.CONTROL_FLAGS_LEN) self.get_message_preview_successfully( TxMessage(self.HASH, 1, self.NODE_ID, 12, blob), TxMessage.MESSAGE_TYPE, SHA256_HASH_LEN + NETWORK_NUM_LEN + UL_INT_SIZE_IN_BYTES + TRANSACTION_FLAG_LEN + constants.NODE_ID_SIZE_IN_BYTES + len(blob) + constants.DOUBLE_SIZE_IN_BYTES + constants.CONTROL_FLAGS_LEN) self.get_message_preview_successfully( KeyMessage(self.HASH, 1, self.NODE_ID, bytearray(1 for _ in range(KEY_SIZE))), KeyMessage.MESSAGE_TYPE, SHA256_HASH_LEN + KEY_SIZE + NETWORK_NUM_LEN + constants.NODE_ID_SIZE_IN_BYTES + constants.CONTROL_FLAGS_LEN) self.get_message_preview_successfully( BlockHoldingMessage(self.HASH, 1, self.NODE_ID), BlockHoldingMessage.MESSAGE_TYPE, SHA256_HASH_LEN + NETWORK_NUM_LEN + constants.NODE_ID_SIZE_IN_BYTES + constants.CONTROL_FLAGS_LEN) get_txs = [1, 2, 3] self.get_message_preview_successfully( GetTxsMessage(get_txs), GetTxsMessage.MESSAGE_TYPE, UL_INT_SIZE_IN_BYTES + UL_INT_SIZE_IN_BYTES * len(get_txs) + constants.CONTROL_FLAGS_LEN) txs = [ TransactionInfo(Sha256Hash(crypto.double_sha256(b"123")), bytearray(4), 1), TransactionInfo(Sha256Hash(crypto.double_sha256(b"234")), bytearray(8), 2) ] expected_length = (UL_INT_SIZE_IN_BYTES + sum(UL_INT_SIZE_IN_BYTES + SHA256_HASH_LEN + UL_INT_SIZE_IN_BYTES + len(tx.contents) for tx in txs) + constants.CONTROL_FLAGS_LEN) self.get_message_preview_successfully(TxsMessage(txs), TxsMessage.MESSAGE_TYPE, expected_length) expected_length = (2 * constants.DOUBLE_SIZE_IN_BYTES) + (5 * constants.UL_SHORT_SIZE_IN_BYTES) + \ (7 * constants.UL_INT_SIZE_IN_BYTES) + constants.IP_ADDR_SIZE_IN_BYTES + \ constants.CONTROL_FLAGS_LEN node_stats = {} helpers.add_stats_to_node_stats(node_stats, "127.0.0.1", 8001, 200, 300, 400, 500, 600, 700, 800, 100, 50) self.get_message_preview_successfully( BdnPerformanceStatsMessage(datetime.utcnow(), datetime.utcnow(), 100, node_stats), BdnPerformanceStatsMessage.MESSAGE_TYPE, expected_length) # multi node bdn stats message expected_length = ( constants.CONTROL_FLAGS_LEN + (2 * constants.DOUBLE_SIZE_IN_BYTES) + # start/end time constants.UL_SHORT_SIZE_IN_BYTES + # memory constants.UL_SHORT_SIZE_IN_BYTES + # num blockchain peers ( 3 * # num blockchain peers ( constants.IP_ADDR_SIZE_IN_BYTES + # ip constants.UL_SHORT_SIZE_IN_BYTES + # port (2 * constants.UL_SHORT_SIZE_IN_BYTES) + # original block stats (7 * constants.UL_INT_SIZE_IN_BYTES)))) # rest of stats node_stats = {} helpers.add_stats_to_node_stats(node_stats, "127.0.0.1", 8001, 200, 300, 400, 500, 600, 700, 800, 100, 50) helpers.add_stats_to_node_stats(node_stats, "127.0.0.2", 8002, 200, 300, 400, 500, 600, 700, 800, 100, 50) helpers.add_stats_to_node_stats(node_stats, "127.0.0.3", 8003, 200, 300, 400, 500, 600, 700, 800, 100, 50) self.get_message_preview_successfully( BdnPerformanceStatsMessage(datetime.utcnow(), datetime.utcnow(), 100, node_stats), BdnPerformanceStatsMessage.MESSAGE_TYPE, expected_length) tx_info = TransactionInfo(crypto.double_sha256(b"123"), bytearray(4), 1) expected_length = constants.NETWORK_NUM_LEN + constants.SID_LEN + SHA256_HASH_LEN + \ constants.UL_INT_SIZE_IN_BYTES + constants.CONTROL_FLAGS_LEN + len(tx_info.contents) self.get_message_preview_successfully(TxContentsMessage(5, tx_info), TxContentsMessage.MESSAGE_TYPE, expected_length) expected_length = constants.NETWORK_NUM_LEN + constants.SID_LEN + constants.CONTROL_FLAGS_LEN self.get_message_preview_successfully( GetTxContentsMessage(1, 2), GetTxContentsMessage.MESSAGE_TYPE, expected_length)
def to_txs_message(self) -> TxsMessage: txs_info_bytes = self.rawbytes()[self.txs_info_offset:] result_message_bytes = bytearray(TxsMessage.HEADER_LENGTH + len(txs_info_bytes)) result_message_bytes[TxsMessage.HEADER_LENGTH:] = txs_info_bytes return TxsMessage(buf=result_message_bytes)
def test_txs_message(self): tx_hash = Sha256Hash(helpers.generate_bytes(crypto.SHA256_HASH_LEN)) tx_contents = helpers.generate_bytes(250) self._test_to_old_version(TxsMessage([TransactionInfo(tx_hash, tx_contents, 1)])) self._test_to_new_version(TxsMessageV4([TransactionInfo(tx_hash, tx_contents, 1)]))
def test_create_message_success_all_types(self): test_network_num = 10 test_protocol_version = bloxroute_version_manager.CURRENT_PROTOCOL_VERSION hello_message = self.create_message_successfully(HelloMessage(protocol_version=test_protocol_version, network_num=test_network_num, node_id=self.NODE_ID ), HelloMessage) self.assertEqual(test_protocol_version, hello_message.protocol_version()) self.assertEqual(test_network_num, hello_message.network_num()) self.assertEqual(self.NODE_ID, hello_message.node_id()) self.create_message_successfully(AckMessage(), AckMessage) self.create_message_successfully(PingMessage(), PingMessage) self.create_message_successfully(PongMessage(), PongMessage) blob = bytearray(4) broadcast_message = self.create_message_successfully(BroadcastMessage(self.HASH, network_num=test_network_num, is_encrypted=True, source_id=self.NODE_ID, blob=blob), BroadcastMessage) self.assertEqual(self.HASH, broadcast_message.block_hash()) self.assertEqual(test_network_num, broadcast_message.network_num()) self.assertEqual(self.NODE_ID, broadcast_message.source_id()) self.assertTrue(broadcast_message.is_encrypted()) self.assertEqual(blob, broadcast_message.blob().tobytes()) sid = 12 tx_val = bytes(1 for _ in range(5)) tx_message = self.create_message_successfully(TxMessage(self.HASH, network_num=test_network_num, source_id=self.NODE_ID, short_id=sid, tx_val=tx_val), TxMessage) self.assertEqual(self.HASH, tx_message.tx_hash()) self.assertEqual(self.NODE_ID, tx_message.source_id()) self.assertEqual(sid, tx_message.short_id()) self.assertEqual(test_network_num, tx_message.network_num()) self.assertEqual(tx_val, tx_message.tx_val()) key = bytearray(1 for _ in range(KEY_SIZE)) key_message = self.create_message_successfully( KeyMessage(self.HASH, test_network_num, self.NODE_ID, bytearray(1 for _ in range(KEY_SIZE))), KeyMessage ) self.assertEqual(key, key_message.key()) self.assertEqual(self.NODE_ID, key_message.source_id()) self.assertEqual(test_network_num, key_message.network_num()) self.assertEqual(self.HASH, key_message.block_hash()) block_holding_message = self.create_message_successfully( BlockHoldingMessage(self.HASH, test_network_num, self.NODE_ID), BlockHoldingMessage ) self.assertEqual(self.NODE_ID, block_holding_message.source_id()) self.assertEqual(test_network_num, block_holding_message.network_num()) self.assertEqual(self.HASH, block_holding_message.block_hash()) get_txs = [1, 2, 3] get_txs_message = self.create_message_successfully(GetTxsMessage(get_txs), GetTxsMessage) self.assertEqual(get_txs, get_txs_message.get_short_ids()) txs = [TransactionInfo(Sha256Hash(crypto.double_sha256(b"123")), bytearray(4), 1), TransactionInfo(Sha256Hash(crypto.double_sha256(b"234")), bytearray(8), 2)] txs_message = self.create_message_successfully(TxsMessage(txs), TxsMessage) result_txs = txs_message.get_txs() for i, result_tx in enumerate(result_txs): self.assertEqual(txs[i].hash, result_tx.hash) self.assertEqual(txs[i].contents, result_tx.contents) self.assertEqual(txs[i].short_id, result_tx.short_id)