async def test_subscription(self): tx_hash = helpers.generate_object_hash() tx_contents = mock_eth_messages.get_dummy_transaction(1) self.gateway_node.get_tx_service().set_transaction_contents( tx_hash, rlp.encode(tx_contents)) tx_hash_2 = helpers.generate_object_hash() tx_contents_2 = mock_eth_messages.get_dummy_transaction(2) self.gateway_node.get_tx_service().set_transaction_contents( tx_hash_2, rlp.encode(tx_contents_2)) await self.eth_ws_server_message_queue.put( (TX_SUB_ID, f"0x{convert.bytes_to_hex(tx_hash.binary)}")) await self.eth_ws_server_message_queue.put( (TX_SUB_ID, f"0x{convert.bytes_to_hex(tx_hash_2.binary)}")) await asyncio.sleep(0.01) self.assertEqual(2, self.subscriber.messages.qsize()) tx_message_1 = await self.subscriber.receive() self.assertEqual(f"0x{convert.bytes_to_hex(tx_hash.binary)}", tx_message_1["tx_hash"]) self.assertEqual(tx_contents.to_json(), tx_message_1["tx_contents"]) tx_message_2 = await self.subscriber.receive() self.assertEqual(f"0x{convert.bytes_to_hex(tx_hash_2.binary)}", tx_message_2["tx_hash"]) self.assertEqual(tx_contents_2.to_json(), tx_message_2["tx_contents"])
def test_transactions_eth_message(self): self._test_msg_serialization( TransactionsEthProtocolMessage, False, [ mock_eth_messages.get_dummy_transaction(1), mock_eth_messages.get_dummy_transaction(2), mock_eth_messages.get_dummy_transaction(3), ], )
def test_tx_to_bx_tx__success(self): txs = [ mock_eth_messages.get_dummy_transaction(1), mock_eth_messages.get_dummy_transaction(2), mock_eth_messages.get_dummy_transaction(3), ] tx_msg = TransactionsEthProtocolMessage(None, txs) self.assertTrue(tx_msg.rawbytes()) self.validate_tx_to_bx_txs_conversion(tx_msg, txs)
def generate_new_eth_transaction(self) -> TxMessage: transaction = mock_eth_messages.get_dummy_transaction(1) transactions_eth_message = TransactionsEthProtocolMessage(None, [transaction]) tx_message = self.message_converter.tx_to_bx_txs( transactions_eth_message, 5 )[0][0] return tx_message
def test_bdn_stats_tx_new_from_node_low_fee(self): self.node.opts.blockchain_networks[ self.node.network_num].min_tx_network_fee = 500 blockchain_network = self.tx_blockchain_connection_protocol.connection.node.get_blockchain_network( ) blockchain_network.protocol = "ethereum" txs = [ mock_eth_messages.get_dummy_transaction(1, gas_price=5), ] tx_msg = TransactionsEthProtocolMessage(None, txs) self.assertEqual(1, len(tx_msg.get_transactions())) self.tx_blockchain_connection_protocol.msg_tx(tx_msg) self.assertEqual( 3, len(gateway_bdn_performance_stats_service.interval_data. blockchain_node_to_bdn_stats)) node_1_stats = gateway_bdn_performance_stats_service.interval_data.blockchain_node_to_bdn_stats[ self.node_1_endpoint] for endpoint, stats in gateway_bdn_performance_stats_service.interval_data.blockchain_node_to_bdn_stats.items( ): if endpoint == self.node_1_endpoint: continue self.assertEqual(0, stats.new_tx_received_from_bdn) self.assertEqual(0, node_1_stats.new_tx_received_from_blockchain_node) self.assertEqual( 1, node_1_stats.new_tx_received_from_blockchain_node_low_fee)
def generate_new_eth_transaction() -> TxMessage: transaction = mock_eth_messages.get_dummy_transaction(1) transactions_eth_message = TransactionsEthProtocolMessage( None, [transaction]) tx_message = EthNormalMessageConverter().tx_to_bx_txs( transactions_eth_message, 5)[0][0] return tx_message
def test_block_bodies_msg_from_bodies_bytes(self): txs = [] txs_bytes = [] txs_hashes = [] tx_count = 10 for i in range(1, tx_count): tx = mock_eth_messages.get_dummy_transaction(1) txs.append(tx) tx_bytes = rlp.encode(tx, Transaction) txs_bytes.append(tx_bytes) tx_hash = tx.hash() txs_hashes.append(tx_hash) uncles = [ mock_eth_messages.get_dummy_block_header(2), mock_eth_messages.get_dummy_block_header(3), ] block_body = TransientBlockBody(txs, uncles) block_body_bytes = memoryview( rlp.encode(TransientBlockBody.serialize(block_body))) block_bodies_msg = BlockBodiesEthProtocolMessage.from_body_bytes( block_body_bytes) self.assertEqual(1, len(block_bodies_msg.get_blocks())) self.assertEqual(1, len(block_bodies_msg.get_block_bodies_bytes())) self.assertEqual(block_body, block_bodies_msg.get_blocks()[0]) self.assertEqual(block_body_bytes, block_bodies_msg.get_block_bodies_bytes()[0])
def test_tx_stats_new_blockchain_node_new_compact_relay(self): blockchain_node_txs = [ mock_eth_messages.get_dummy_transaction(7), ] blockchain_node_tx_msg = TransactionsEthProtocolMessage( None, blockchain_node_txs) self.blockchain_connection_protocol.msg_tx(blockchain_node_tx_msg) bx_tx_message = self.node.message_converter.tx_to_bx_txs( blockchain_node_tx_msg, 1) for (msg, tx_hash, tx_bytes) in bx_tx_message: relay_compact_message = TxMessage(message_hash=tx_hash, network_num=1, short_id=1) self.relay_connection.msg_tx(relay_compact_message) self.assertEqual( 1, gateway_transaction_stats_service.interval_data. new_transactions_received_from_blockchain) self.assertEqual( 1, gateway_transaction_stats_service.interval_data. new_compact_transactions_received_from_relays) self.assertEqual( 1, gateway_transaction_stats_service.interval_data. short_id_assignments_processed) self.assertEqual( 0, gateway_transaction_stats_service.interval_data. redundant_transaction_content_messages)
def test_msg_tx(self): self.node.feed_manager.publish_to_feed = MagicMock() self.node.opts.ws = True transaction = mock_eth_messages.get_dummy_transaction(1) transaction_hash = transaction.hash() transaction_contents = transaction.contents() messages = TransactionsEthProtocolMessage(None, [transaction]) self.sut.msg_tx(messages) # published to both feeds self.node.feed_manager.publish_to_feed.assert_has_calls( [ call( EthNewTransactionFeed.NAME, EthRawTransaction(transaction_hash, transaction_contents) ), call( EthPendingTransactionFeed.NAME, EthRawTransaction(transaction_hash, transaction_contents) ), ] ) # broadcast transactions self.assertEqual(1, len(self.node.broadcast_messages))
def test_msg_tx(self): self.node.feed_manager.publish_to_feed = MagicMock() self.node.opts.ws = True self.node.opts.transaction_validation = False transaction = mock_eth_messages.get_dummy_transaction(1) transaction_hash = transaction.hash() transaction_contents = transaction.contents() messages = TransactionsEthProtocolMessage(None, [transaction]) self.sut.msg_tx(messages) # published to both feeds self.node.feed_manager.publish_to_feed.assert_has_calls( [ call( FeedKey(EthNewTransactionFeed.NAME), EthRawTransaction( transaction_hash, transaction_contents, FeedSource.BLOCKCHAIN_SOCKET, local_region=True ) ), call( FeedKey(EthPendingTransactionFeed.NAME), EthRawTransaction( transaction_hash, transaction_contents, FeedSource.BLOCKCHAIN_SOCKET, local_region=True ) ), ] ) # broadcast transactions self.assertEqual(1, len(self.broadcast_messages)) self.assertEqual(1, len(self.broadcast_to_node_messages))
def test_bdn_stats_tx_ignore_new_compact_from_bdn_log_new_from_node(self): blockchain_node_txs = [ mock_eth_messages.get_dummy_transaction(7), ] blockchain_node_tx_msg = TransactionsEthProtocolMessage( None, blockchain_node_txs) bx_tx_message = self.node.message_converter.tx_to_bx_txs( blockchain_node_tx_msg, 1) for (msg, tx_hash, tx_bytes) in bx_tx_message: relay_compact_message = TxMessage(message_hash=tx_hash, network_num=1, short_id=1) self.relay_connection.msg_tx(relay_compact_message) self.tx_blockchain_connection_protocol.msg_tx(blockchain_node_tx_msg) self.assertEqual( 3, len(gateway_bdn_performance_stats_service.interval_data. blockchain_node_to_bdn_stats)) node_1_stats = gateway_bdn_performance_stats_service.interval_data.blockchain_node_to_bdn_stats[ self.node_1_endpoint] for endpoint, stats in gateway_bdn_performance_stats_service.interval_data.blockchain_node_to_bdn_stats.items( ): if endpoint == self.node_1_endpoint: continue self.assertEqual(1, stats.new_tx_received_from_bdn) self.assertEqual(1, node_1_stats.new_tx_received_from_blockchain_node) self.assertEqual(0, node_1_stats.new_tx_received_from_bdn)
def test_bdn_stats_tx_new_full_from_bdn_ignore_from_node(self): blockchain_node_txs = [ mock_eth_messages.get_dummy_transaction(7), ] blockchain_node_tx_msg = TransactionsEthProtocolMessage( None, blockchain_node_txs) bx_tx_message = self.node.message_converter.tx_to_bx_txs( blockchain_node_tx_msg, 1) for (msg, tx_hash, tx_bytes) in bx_tx_message: relay_full_message = TxMessage(message_hash=tx_hash, network_num=1, short_id=1, tx_val=tx_bytes) self.relay_connection.msg_tx(relay_full_message) self.tx_blockchain_connection_protocol.msg_tx(blockchain_node_tx_msg) self.assertEqual( 3, len(gateway_bdn_performance_stats_service.interval_data. blockchain_node_to_bdn_stats)) for stats in gateway_bdn_performance_stats_service.interval_data.blockchain_node_to_bdn_stats.values( ): self.assertEqual(1, stats.new_tx_received_from_bdn) self.assertEqual(0, stats.new_tx_received_from_blockchain_node)
async def test_disconnect_server_reconnect(self): tx_hash = helpers.generate_object_hash() tx_contents = mock_eth_messages.get_dummy_transaction(1) self.gateway_node.get_tx_service().set_transaction_contents( tx_hash, rlp.encode(tx_contents)) self.eth_test_ws_server.close() await self.eth_test_ws_server.wait_closed() await self.eth_ws_server_message_queue.put( (TX_SUB_ID, f"0x{convert.bytes_to_hex(tx_hash.binary)}")) await asyncio.sleep(0.02) self.assertEqual(0, self.subscriber.messages.qsize()) self.assertTrue(self.eth_ws_proxy_publisher.running) self.assertFalse(self.eth_ws_proxy_publisher.connected_event.is_set()) await self.start_server() await asyncio.sleep(0.1) self.assertTrue(self.eth_ws_proxy_publisher.connected_event.is_set()) self.assertEqual(0, self.subscriber.messages.qsize()) await self.eth_ws_server_message_queue.put( (TX_SUB_ID, f"0x{convert.bytes_to_hex(tx_hash.binary)}")) await asyncio.sleep(0.01) self.assertEqual(1, self.subscriber.messages.qsize())
def test_tx_to_bx_tx__filter_low_fee(self): txs = [ mock_eth_messages.get_dummy_transaction(1, 20), mock_eth_messages.get_dummy_transaction(2, 25), mock_eth_messages.get_dummy_transaction(3, 10), ] tx_msg = TransactionsEthProtocolMessage(None, txs) results = self.eth_message_converter.tx_to_bx_txs( tx_msg, self.test_network_num, min_tx_network_fee=15) self.assertEqual(2, len(results)) for i, result in enumerate(results): bx_tx_message, tx_hash, tx_contents = result self.assertEqual(txs[i].hash(), tx_hash)
def test_new_block_parts_to_new_block_message(self): txs = [] txs_bytes = [] txs_hashes = [] tx_count = 10 for i in range(1, tx_count): tx = mock_eth_messages.get_dummy_transaction(1) txs.append(tx) tx_bytes = rlp.encode(tx, Transaction) txs_bytes.append(tx_bytes) tx_hash = tx.hash() txs_hashes.append(tx_hash) block_header = mock_eth_messages.get_dummy_block_header(1) uncles = [ mock_eth_messages.get_dummy_block_header(2), mock_eth_messages.get_dummy_block_header(3), ] block = Block(block_header, txs, uncles) dummy_chain_difficulty = 10 new_block_msg = NewBlockEthProtocolMessage(None, block, dummy_chain_difficulty) self.assertTrue(new_block_msg.rawbytes()) block_body = TransientBlockBody(txs, uncles) block_bytes = rlp.encode(Block.serialize(block)) block_header_bytes = memoryview( rlp.encode(BlockHeader.serialize(block_header))) block_body_bytes = memoryview( rlp.encode(TransientBlockBody.serialize(block_body))) self.assertEqual(len(block_bytes), len(block_header_bytes) + len(block_body_bytes)) internal_new_block_msg = InternalEthBlockInfo.from_new_block_msg( new_block_msg) new_block_parts = internal_new_block_msg.to_new_block_parts() new_block_msg_from_block_parts = NewBlockEthProtocolMessage.from_new_block_parts( new_block_parts, dummy_chain_difficulty) self.assertEqual(len(new_block_msg.rawbytes()), len(new_block_msg_from_block_parts.rawbytes())) self.assertEqual(new_block_msg.rawbytes(), new_block_msg_from_block_parts.rawbytes()) self.assertEqual(new_block_msg_from_block_parts.get_chain_difficulty(), dummy_chain_difficulty)
def test_transaction_updates_filters_based_on_factor(self): self.node.opts.filter_txs_factor = 1 self._set_bc_connection() transactions = [ mock_eth_messages.get_dummy_transaction(i, 10) for i in range(100) ] self.node.on_transactions_in_block(transactions) cheap_tx = self._convert_to_bx_message( TransactionsEthProtocolMessage( None, [mock_eth_messages.get_dummy_transaction(1, 5)])) self.relay_connection_1.msg_tx(cheap_tx) self.node.broadcast.assert_not_called() expensive_tx = self._convert_to_bx_message( TransactionsEthProtocolMessage( None, [mock_eth_messages.get_dummy_transaction(1, 15)])) self.relay_connection_1.msg_tx(expensive_tx) self._assert_tx_sent()
def test_tx_to_bx_tx__from_bytes_single_tx_success(self): txs = [ mock_eth_messages.get_dummy_transaction(1), ] tx_msg = TransactionsEthProtocolMessage(None, txs) tx_msg_bytes = tx_msg.rawbytes() self.assertTrue(tx_msg_bytes) tx_msg_from_bytes = TransactionsEthProtocolMessage(tx_msg_bytes) self.validate_tx_to_bx_txs_conversion(tx_msg_from_bytes, txs)
def _get_node_tx_message( self ) -> Tuple[Union[TxOntMessage, TransactionsEthProtocolMessage], List[Tuple[ Sha256Hash, Union[bytearray, memoryview]]]]: txs = [] for i in range(600): txs.append(mock_eth_messages.get_dummy_transaction(i + 1)) msg = TransactionsEthProtocolMessage(None, txs) return msg, list( map(lambda tx: (tx.hash(), tx.contents()), msg.get_transactions()))
def test_msg_confirmed_tx(self): self.node.feed_manager.publish_to_feed = MagicMock() tx_hash = helpers.generate_object_hash() message = ConfirmedTxMessage(tx_hash) self.connection.msg_confirmed_tx(message) self.node.feed_manager.publish_to_feed.assert_not_called() tx_hash = helpers.generate_object_hash() message = ConfirmedTxMessage( tx_hash, rlp.encode(mock_eth_messages.get_dummy_transaction(1))) self.connection.msg_confirmed_tx(message) self.node.feed_manager.publish_to_feed.assert_called_once()
def test_block_to_bx_block_then_bx_block_to_block__success(self): txs = [] txs_bytes = [] txs_hashes = [] short_ids = [] tx_count = 150 for i in range(1, tx_count): tx = mock_eth_messages.get_dummy_transaction(i) txs.append(tx) tx_bytes = rlp.encode(tx, Transaction) txs_bytes.append(tx_bytes) tx_hash = tx.hash() txs_hashes.append(tx_hash) self.tx_service.assign_short_id(tx_hash, i) self.tx_service.set_transaction_contents(tx_hash, tx_bytes) short_ids.append(i) block = Block(mock_eth_messages.get_dummy_block_header(100), txs, [mock_eth_messages.get_dummy_block_header(2)]) dummy_chain_difficulty = 40000000 block_msg = NewBlockEthProtocolMessage(None, block, dummy_chain_difficulty) block_msg_bytes = block_msg.rawbytes() self.assertTrue(block_msg_bytes) internal_new_block_msg = InternalEthBlockInfo.from_new_block_msg( block_msg) bx_block_msg, block_info = self.eth_message_converter.block_to_bx_block( internal_new_block_msg, self.tx_service, True, 0) self.assertIsNotNone(bx_block_msg) self.assertEqual(len(txs), block_info.txn_count) self.assertEqual(convert.bytes_to_hex(block.header.prev_hash), block_info.prev_block_hash) converted_block_msg, _, _, _ = self.eth_message_converter.bx_block_to_block( bx_block_msg, self.tx_service) self.assertIsNotNone(converted_block_msg) converted_new_block_msg = converted_block_msg.to_new_block_msg() converted_block_msg_bytes = converted_new_block_msg.rawbytes() self.assertEqual(len(converted_block_msg_bytes), len(block_msg_bytes)) self.assertEqual(converted_block_msg_bytes, block_msg_bytes)
def test_tx_stats_new_from_blockchain_node(self): txs = [ mock_eth_messages.get_dummy_transaction(1), ] tx_msg = TransactionsEthProtocolMessage(None, txs) self.blockchain_connection_protocol.msg_tx(tx_msg) self.assertEqual( 1, gateway_transaction_stats_service.interval_data. new_transactions_received_from_blockchain) self.assertEqual( 0, gateway_transaction_stats_service.interval_data. duplicate_transactions_received_from_blockchain)
def _get_node_tx_message( self ) -> Tuple[Union[TxOntMessage, TransactionsEthProtocolMessage], List[Tuple[ Sha256Hash, Union[bytearray, memoryview]]]]: txs = [ mock_eth_messages.get_dummy_transaction(1), mock_eth_messages.get_dummy_access_list_transaction(2) ] msg = TransactionsEthProtocolMessage(None, txs) # pyre-fixme [7]: Expected `Tuple[Union[TransactionsEthProtocolMessage, TxOntMessage], List[Tuple[Sha256Hash, Union[bytearray, memoryview]]]]` return msg, list( map(lambda tx: (tx.hash(), tx.contents()), msg.get_transactions()))
def test_new_block_internal_eth_message_to_from_new_block_parts(self): txs = [] txs_bytes = [] txs_hashes = [] tx_count = 10 for i in range(1, tx_count): tx = mock_eth_messages.get_dummy_transaction(1) txs.append(tx) tx_bytes = rlp.encode(tx, Transaction) txs_bytes.append(tx_bytes) tx_hash = tx.hash() txs_hashes.append(tx_hash) block_header = mock_eth_messages.get_dummy_block_header(1) uncles = [ mock_eth_messages.get_dummy_block_header(2), mock_eth_messages.get_dummy_block_header(3), ] block_number = 100000 block_body = TransientBlockBody(txs, uncles) block_header_bytes = memoryview( rlp.encode(BlockHeader.serialize(block_header))) block_body_bytes = memoryview( rlp.encode(TransientBlockBody.serialize(block_body))) new_block_parts = NewBlockParts(block_header_bytes, block_body_bytes, block_number) new_block_internal_eth_msg = InternalEthBlockInfo.from_new_block_parts( new_block_parts) self.assertIsNotNone(new_block_internal_eth_msg) self.assertTrue(new_block_internal_eth_msg.rawbytes()) parsed_new_block_parts = new_block_internal_eth_msg.to_new_block_parts( ) self.assertIsInstance(parsed_new_block_parts, NewBlockParts) self.assertEqual(block_header_bytes, parsed_new_block_parts.block_header_bytes) self.assertEqual(block_body_bytes, parsed_new_block_parts.block_body_bytes) self.assertEqual(block_number, parsed_new_block_parts.block_number)
def test_new_block_internal_eth_message_to_from_new_block_message(self): txs = [] txs_bytes = [] txs_hashes = [] tx_count = 10 for i in range(1, tx_count): tx = mock_eth_messages.get_dummy_transaction(1) txs.append(tx) tx_bytes = rlp.encode(tx, Transaction) txs_bytes.append(tx_bytes) tx_hash = tx.hash() txs_hashes.append(tx_hash) block_header = mock_eth_messages.get_dummy_block_header(1) uncles = [ mock_eth_messages.get_dummy_block_header(2), mock_eth_messages.get_dummy_block_header(3), ] block = Block(block_header, txs, uncles) dummy_chain_difficulty = 10 block_msg = NewBlockEthProtocolMessage(None, block, dummy_chain_difficulty) self.assertTrue(block_msg.rawbytes()) new_block_internal_eth_msg = InternalEthBlockInfo.from_new_block_msg( block_msg) self.assertIsNotNone(new_block_internal_eth_msg) self.assertTrue(new_block_internal_eth_msg.rawbytes()) parsed_new_block_message = new_block_internal_eth_msg.to_new_block_msg( ) self.assertIsInstance(parsed_new_block_message, NewBlockEthProtocolMessage) self.assertEqual(len(block_msg.rawbytes()), len(parsed_new_block_message.rawbytes())) self.assertEqual( convert.bytes_to_hex(block_msg.rawbytes()), convert.bytes_to_hex(parsed_new_block_message.rawbytes()), )
async def test_disconnect_server(self): tx_hash = helpers.generate_object_hash() tx_contents = mock_eth_messages.get_dummy_transaction(1) self.gateway_node.get_tx_service().set_transaction_contents( tx_hash, rlp.encode(tx_contents) ) self.eth_test_ws_server.close() await self.eth_test_ws_server.wait_closed() await self.eth_ws_server_message_queue.put(f"0x{convert.bytes_to_hex(tx_hash.binary)}") await asyncio.sleep(0.05) self.assertEqual(0, self.subscriber.messages.qsize()) self.assertFalse(self.eth_ws_subscriber.running)
def test_block_bodies_msg__get_block_transactions(self): txs = [] txs_bytes = [] txs_hashes = [] tx_count = 10 for i in range(1, tx_count): tx = mock_eth_messages.get_dummy_transaction(1) txs.append(tx) tx_bytes = rlp.encode(LegacyTransaction.serialize(tx)) txs_bytes.append(tx_bytes) tx_hash = tx.hash() txs_hashes.append(tx_hash) al_tx = mock_eth_messages.get_dummy_access_list_transaction(11) txs.append(al_tx) txs_bytes.append(Transaction.serialize(al_tx)) txs_hashes.append(al_tx.hash()) uncles = [ mock_eth_messages.get_dummy_block_header(2), mock_eth_messages.get_dummy_block_header(3), ] block_body = TransientBlockBody(txs, uncles) block_body_bytes = rlp.encode(block_body) block_bodies_msg = BlockBodiesEthProtocolMessage.from_body_bytes( block_body_bytes) parsed_txs_bytes = block_bodies_msg.get_block_transaction_bytes(0) self.assertEqual(len(parsed_txs_bytes), len(txs_hashes)) for index, parsed_tx_bytes in enumerate(parsed_txs_bytes): self.assertEqual( convert.bytes_to_hex(txs_bytes[index]), convert.bytes_to_hex(parsed_tx_bytes), f"failed at index {index}", ) parsed_txs_hashes = block_bodies_msg.get_block_transaction_hashes(0) self.assertEqual(len(parsed_txs_hashes), len(txs_hashes)) for index, parsed_tx_hash in enumerate(parsed_txs_hashes): self.assertEqual(parsed_tx_hash, txs_hashes[index])
def test_bx_tx_to_tx__success(self): tx = mock_eth_messages.get_dummy_transaction(1) tx_bytes = rlp.encode(tx, Transaction) tx_hash_bytes = hashlib.sha256(tx_bytes).digest() tx_hash = Sha256Hash(tx_hash_bytes) bx_tx_message = TxMessage(message_hash=tx_hash, network_num=self.test_network_num, tx_val=tx_bytes) tx_message = self.eth_message_converter.bx_tx_to_tx(bx_tx_message) self.assertIsNotNone(tx_message) self.assertIsInstance(tx_message, TransactionsEthProtocolMessage) self.assertTrue(tx_message.get_transactions()) self.assertEqual(1, len(tx_message.get_transactions())) tx_obj = tx_message.get_transactions()[0] self.assertEqual(tx, tx_obj)
async def setUp(self) -> None: crypto_utils.recover_public_key = MagicMock(return_value=bytes(32)) account_model = BdnAccountModelBase( "account_id", "account_name", "fake_certificate", new_transaction_streaming=BdnServiceModelConfigBase( expire_date=date(2999, 1, 1).isoformat())) self.eth_ws_port = helpers.get_free_port() self.eth_ws_uri = f"ws://127.0.0.1:{self.eth_ws_port}" self.eth_ws_server_message_queue = asyncio.Queue() await self.start_server() gateway_opts = gateway_helpers.get_gateway_opts( 8000, eth_ws_uri=self.eth_ws_uri, ws=True) gateway_opts.set_account_options(account_model) self.gateway_node = MockGatewayNode(gateway_opts) self.gateway_node.transaction_streamer_peer = OutboundPeerModel( "127.0.0.1", 8006, node_type=NodeType.INTERNAL_GATEWAY) self.gateway_node.feed_manager.register_feed( EthPendingTransactionFeed(self.gateway_node.alarm_queue)) self.eth_ws_proxy_publisher = EthWsProxyPublisher( self.eth_ws_uri, self.gateway_node.feed_manager, self.gateway_node.get_tx_service(), self.gateway_node) self.subscriber: Subscriber[ RawTransactionFeedEntry] = self.gateway_node.feed_manager.subscribe_to_feed( EthPendingTransactionFeed.NAME, {}) self.assertIsNotNone(self.subscriber) await self.eth_ws_proxy_publisher.start() await asyncio.sleep(0.01) self.assertEqual(len(self.eth_ws_proxy_publisher.receiving_tasks), 2) self.assertEqual(0, self.subscriber.messages.qsize()) self.sample_transactions = { i: mock_eth_messages.get_dummy_transaction(i) for i in range(10) }
def test_bdn_stats_tx_new_from_node(self): txs = [ mock_eth_messages.get_dummy_transaction(1), ] tx_msg = TransactionsEthProtocolMessage(None, txs) self.tx_blockchain_connection_protocol.msg_tx(tx_msg) self.assertEqual( 3, len(gateway_bdn_performance_stats_service.interval_data. blockchain_node_to_bdn_stats)) node_1_stats = gateway_bdn_performance_stats_service.interval_data.blockchain_node_to_bdn_stats[ self.node_1_endpoint] for endpoint, stats in gateway_bdn_performance_stats_service.interval_data.blockchain_node_to_bdn_stats.items( ): if endpoint == self.node_1_endpoint: continue self.assertEqual(1, stats.new_tx_received_from_bdn) self.assertEqual(1, node_1_stats.new_tx_received_from_blockchain_node)
def test_new_block_parts(self): txs = [] txs_bytes = [] txs_hashes = [] tx_count = 10 for i in range(1, tx_count): tx = mock_eth_messages.get_dummy_transaction(1) txs.append(tx) tx_bytes = rlp.encode(tx, Transaction) txs_bytes.append(tx_bytes) tx_hash = tx.hash() txs_hashes.append(tx_hash) block_header = mock_eth_messages.get_dummy_block_header(1) uncles = [ mock_eth_messages.get_dummy_block_header(2), mock_eth_messages.get_dummy_block_header(3), ] block_number = 100000 block_body = TransientBlockBody(txs, uncles) block_header_bytes = memoryview( rlp.encode(BlockHeader.serialize(block_header))) block_body_bytes = memoryview( rlp.encode(TransientBlockBody.serialize(block_body))) new_block_parts = NewBlockParts(block_header_bytes, block_body_bytes, block_number) self.assertIsInstance(new_block_parts.get_block_hash(), Sha256Hash) self.assertIsInstance(new_block_parts.get_previous_block_hash(), Sha256Hash) self.assertEqual(1, new_block_parts.get_block_difficulty())