def test_queuing_messages_cleared_after_timeout(self): node = self._initialize_gateway(True, True, True) remote_blockchain_conn = next(iter(node.connection_pool.get_by_connection_types([ConnectionType.REMOTE_BLOCKCHAIN_NODE]))) remote_blockchain_conn.mark_for_close() queued_message = PingMessage(12345) node.send_msg_to_remote_node(queued_message) self.assertEqual(1, len(node.remote_node_msg_queue._queue)) self.assertEqual(queued_message, node.remote_node_msg_queue._queue[0]) # queue has been cleared time.time = MagicMock(return_value=time.time() + node.opts.remote_blockchain_message_ttl + 0.1) node.alarm_queue.fire_alarms() node.on_connection_added(MockSocketConnection(3, node, ip_address=LOCALHOST, port=8003)) next_conn = next(iter(node.connection_pool.get_by_connection_types([ConnectionType.REMOTE_BLOCKCHAIN_NODE]))) next_conn.outputbuf = OutputBuffer() # clear buffer node.on_remote_blockchain_connection_ready(next_conn) self.assertEqual(0, next_conn.outputbuf.length) next_conn.mark_for_close() queued_message = PingMessage(12345) node.send_msg_to_remote_node(queued_message) self.assertEqual(1, len(node.remote_node_msg_queue._queue)) self.assertEqual(queued_message, node.remote_node_msg_queue._queue[0]) node.on_connection_added(MockSocketConnection(4, node, ip_address=LOCALHOST, port=8003)) reestablished_conn = next(iter(node.connection_pool.get_by_connection_types([ConnectionType.REMOTE_BLOCKCHAIN_NODE]))) reestablished_conn.outputbuf = OutputBuffer() # clear buffer node.on_remote_blockchain_connection_ready(reestablished_conn) self.assertEqual(queued_message.rawbytes().tobytes(), reestablished_conn.outputbuf.get_buffer().tobytes())
def test_ping_pong(self): hello_msg = HelloMessage(protocol_version=protocol_version.PROTOCOL_VERSION, network_num=1) self.connection.add_received_bytes(hello_msg.rawbytes()) self.connection.process_message() hello_msg_bytes = self.connection.get_bytes_to_send() self.assertTrue(len(hello_msg_bytes) > 0) self.connection.advance_sent_bytes(len(hello_msg_bytes)) ack_msg = AckMessage() self.connection.add_received_bytes(ack_msg.rawbytes()) self.connection.process_message() ack_msg_bytes = self.connection.get_bytes_to_send() self.assertTrue(len(ack_msg_bytes) > 0) self.connection.advance_sent_bytes(len(ack_msg_bytes)) ping_msg = PingMessage(nonce=12345) self.connection.add_received_bytes(ping_msg.rawbytes()) self.connection.process_message() pong_msg_bytes = self.connection.get_bytes_to_send() self.assertTrue(len(pong_msg_bytes) > 0) msg_type, payload_len = AbstractBloxrouteMessage.unpack(pong_msg_bytes[:AbstractBloxrouteMessage.HEADER_LENGTH]) self.assertEqual(BloxrouteMessageType.PONG, msg_type) self.connection.advance_sent_bytes(len(pong_msg_bytes)) time.time = MagicMock(return_value=time.time() + constants.PING_INTERVAL_S) self.node.alarm_queue.fire_alarms() ping_msg_bytes = self.connection.get_bytes_to_send() self.assertTrue(len(ping_msg_bytes) > 0) msg_type, payload_len = AbstractBloxrouteMessage.unpack(ping_msg_bytes[:AbstractBloxrouteMessage.HEADER_LENGTH]) self.assertEqual(BloxrouteMessageType.PING, msg_type)
async def test_queuing_messages_no_blockchain_connection(self): node = self._initialize_gateway(True, True) blockchain_conn = next( iter( node.connection_pool.get_by_connection_types( [ConnectionType.BLOCKCHAIN_NODE]))) blockchain_conn.mark_for_close() self.assertIsNone(node.node_conn) queued_message = PingMessage(12345) node.send_msg_to_node(queued_message) self.assertEqual(1, len(node.node_msg_queue._queue)) self.assertEqual(queued_message, node.node_msg_queue._queue[0]) node.on_connection_added( MockSocketConnection(ip_address=LOCALHOST, port=8001)) next_conn = next( iter( node.connection_pool.get_by_connection_types( [ConnectionType.BLOCKCHAIN_NODE]))) next_conn.outputbuf = OutputBuffer() # clear buffer node.on_blockchain_connection_ready(next_conn) self.assertEqual(queued_message.rawbytes().tobytes(), next_conn.outputbuf.get_buffer().tobytes())
def compare_ping_old_to_current( self, converted_current_message: PingMessage, original_current_message: PingMessage, ): self.assertEqual( original_current_message.rawbytes(), converted_current_message.rawbytes(), )
def test_get_and_clear_after_timeout(self): message_1 = PingMessage(1) message_2 = PingMessage(2) self.blockchain_message_queue.append(message_1) self.assertIn(message_1, self.blockchain_message_queue._queue) self.blockchain_message_queue.append(message_2) self.assertIn(message_2, self.blockchain_message_queue._queue) time.time = MagicMock(return_value=time.time() + TTL + 1) items = self.blockchain_message_queue.pop_items() self.assertEqual(0, len(items))
def test_append(self): message_1 = PingMessage(1) message_2 = PingMessage(2) message_3 = PingMessage(3) self.blockchain_message_queue.append(message_1) self.assertIn(message_1, self.blockchain_message_queue._queue) self.blockchain_message_queue.append(message_2) self.assertIn(message_2, self.blockchain_message_queue._queue) time.time = MagicMock(return_value=time.time() + TTL + 1) # appends after timeout should be ignored self.blockchain_message_queue.append(message_3) self.assertNotIn(message_3, self.blockchain_message_queue._queue)
def __init__(self, *args, **kwargs): super(AbstractConnectionTest.TestAbstractConnection, self).__init__(*args, **kwargs) self.hello_messages = BLOXROUTE_HELLO_MESSAGES self.header_size = constants.STARTING_SEQUENCE_BYTES_LEN + constants.BX_HDR_COMMON_OFF self.message_factory = AbstractConnectionTest.TestMessageFactory() self.ping_message = PingMessage() self.pong_message = PongMessage() self.ack_message = AckMessage()
def test_get_and_clear_reenables_insert(self): message_1 = PingMessage(1) message_2 = PingMessage(2) message_3 = PingMessage(3) self.blockchain_message_queue.append(message_1) self.assertIn(message_1, self.blockchain_message_queue._queue) time.time = MagicMock(return_value=time.time() + TTL + 1) # appends after timeout should be ignored self.blockchain_message_queue.append(message_2) self.assertNotIn(message_2, self.blockchain_message_queue._queue) items = self.blockchain_message_queue.pop_items() self.assertEqual(0, len(items)) self.blockchain_message_queue.append(message_3) self.assertIn(message_3, self.blockchain_message_queue._queue)
def msg_ping(self, msg: PingMessage): nonce = msg.nonce() assumed_request_time = time.time( ) - nonce_generator.get_timestamp_from_nonce(nonce) self.inbound_peer_latency = assumed_request_time hooks.add_measurement(self.peer_desc, MeasurementType.PING_INCOMING, assumed_request_time, self.peer_id) self.enqueue_msg( PongMessage(nonce=nonce, timestamp=nonce_generator.get_nonce()))
def test_ping_pong_send_pings(self): self.connection.enqueue_msg = MagicMock() self.connection.can_send_pings = True self.connection.schedule_pings() self.assertIsNotNone(self.connection.ping_alarm_id) result = self.connection.send_ping() self.connection.enqueue_msg.assert_called_once_with(PingMessage()) self.assertEqual(self.connection.ping_interval_s, result)
def test_msg_ping(self): self.connection.msg_ping(PingMessage()) self.assertTrue(self.connection.outputbuf.length > 0) self.connection.outputbuf.flush() output_buf_msg = self.connection.outputbuf.get_buffer() pong_reply_msg = bloxroute_message_factory.create_message_from_buffer( output_buf_msg) self.assertTrue(pong_reply_msg) self.assertTrue(isinstance(pong_reply_msg, PongMessage))
def send_ping(self): """ Send a ping (and reschedule if called from alarm queue) """ if self.can_send_pings and not self.state & ConnectionState.MARK_FOR_CLOSE: nonce = nonce_generator.get_nonce() msg = PingMessage(nonce=nonce) self.enqueue_msg(msg) self.ping_message_timestamps.add(nonce, time.time()) return self.ping_interval_s return constants.CANCEL_ALARMS
def test_get_and_clear_before_timeout(self): message_1 = PingMessage(1) message_2 = PingMessage(2) self.blockchain_message_queue.append(message_1) self.assertIn(message_1, self.blockchain_message_queue._queue) self.blockchain_message_queue.append(message_2) self.assertIn(message_2, self.blockchain_message_queue._queue) items = self.blockchain_message_queue.pop_items() self.assertEqual(2, len(items)) self.assertIn(message_1, items) self.assertIn(message_2, items) next_items = self.blockchain_message_queue.pop_items() self.assertEqual(0, len(next_items)) message_3 = PingMessage(3) self.blockchain_message_queue.append(message_3) last_items = self.blockchain_message_queue.pop_items() self.assertEqual(1, len(last_items)) self.assertIn(message_3, last_items)
def __init__(self, sock, address, node, from_me=False): super(InternalNodeConnection, self).__init__(sock, address, node, from_me) # Enable buffering only on internal connections self.enable_buffered_send = node.opts.enable_buffered_send self.outputbuf = OutputBuffer(enable_buffering=self.enable_buffered_send) self.network_num = node.network_num self.version_manager = bloxroute_version_manager # Setting default protocol version and message factory; override when hello message received self.message_factory = bloxroute_message_factory self.protocol_version = self.version_manager.CURRENT_PROTOCOL_VERSION self.ping_message = PingMessage() self.pong_message = PongMessage() self.ack_message = AckMessage() self.can_send_pings = True self.ping_message_timestamps = ExpiringDict(self.node.alarm_queue, constants.REQUEST_EXPIRATION_TIME) self.message_validator = BloxrouteMessageValidator(None, self.protocol_version)
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 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)
def ping_message(self) -> AbstractMessage: return PingMessage()
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_ping_response_msg(self): ping = PingMessage(nonce=50) self.assertEqual(50, ping.nonce()) msg = bloxroute_message_factory.create_message_from_buffer(ping.buf) self.assertEqual(50, msg.nonce())
def ping_message(self) -> PingMessage: return PingMessage(nonce_generator.get_nonce())
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 test_ping_message(self): self._test_to_old_version(PingMessage(nonce=12345)) self._test_to_new_version(PingMessageV4(nonce=12345))
def ping_message(self) -> AbstractMessage: nonce = nonce_generator.get_nonce() self.ping_message_timestamps.add(nonce, time.time()) return PingMessage(nonce)