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)
def test_create_message_failure(self): message = HelloMessage(protocol_version=1, network_num=2) with self.assertRaises(PayloadLenError): bloxroute_message_factory.create_message_from_buffer( message.rawbytes()[:-1]) bloxroute_message_factory.create_message_from_buffer( message.rawbytes()[:-1])
def _test_version_over_v4(self, version): hello_msg = HelloMessage(protocol_version=version, network_num=1) input_buffer = InputBuffer() input_buffer.add_bytes(hello_msg.rawbytes()) self.assertEqual( version, self.version_manager.get_connection_protocol_version(input_buffer))
def compare_hello_old_to_current( self, converted_current_message: HelloMessage, original_current_message: HelloMessage, ): self.assertEqual( original_current_message.rawbytes(), converted_current_message.rawbytes(), )
def test_get_connection_protocol_version__v4(self): hello_msg = HelloMessage(protocol_version=4, network_num=constants.DEFAULT_NETWORK_NUM) input_buffer = InputBuffer() input_buffer.add_bytes(hello_msg.rawbytes()) self.assertEqual( 4, self.version_manager.get_connection_protocol_version(input_buffer))
def test_message_preview_incomplete(self): message = HelloMessage(protocol_version=1, network_num=2) is_full_message, command, payload_length = bloxroute_message_factory.get_message_header_preview_from_input_buffer( create_input_buffer_with_bytes(message.rawbytes()[:-1])) self.assertFalse(is_full_message) self.assertEqual(b"hello", command) self.assertEqual( VersionMessage.VERSION_MESSAGE_LENGTH + UL_INT_SIZE_IN_BYTES + NODE_ID_SIZE_IN_BYTES - UL_INT_SIZE_IN_BYTES, payload_length) is_full_message, command, payload_length = bloxroute_message_factory.get_message_header_preview_from_input_buffer( create_input_buffer_with_bytes(message.rawbytes()[:1])) self.assertFalse(is_full_message) self.assertIsNone(command) self.assertIsNone(payload_length)
def test_hello_schedules_pings(self): self.connection.msg_hello(HelloMessage(1, 1, helpers.generate_node_id())) self.assertEqual(1, len(self.alarm_queue.alarms)) self.assertIsNotNone(self.connection.ping_alarm_id) self.assertEqual(self.alarm_queue.alarms[0], self.connection.ping_alarm_id)
def __init__(self, sock: AbstractSocketConnectionProtocol, node: "AbstractGatewayNode"): super(AbstractRelayConnection, self).__init__(sock, node) hello_msg = HelloMessage(protocol_version=self.protocol_version, network_num=self.network_num, node_id=self.node.opts.node_id) self.enqueue_msg(hello_msg) self.hello_messages = constants.BLOXROUTE_HELLO_MESSAGES self.header_size = constants.STARTING_SEQUENCE_BYTES_LEN + constants.BX_HDR_COMMON_OFF self.message_handlers = { BloxrouteMessageType.HELLO: self.msg_hello, BloxrouteMessageType.PING: self.msg_ping, BloxrouteMessageType.PONG: self.msg_pong, BloxrouteMessageType.ACK: self.msg_ack, BloxrouteMessageType.BROADCAST: self.msg_broadcast, BloxrouteMessageType.KEY: self.msg_key, BloxrouteMessageType.TRANSACTION: self.msg_tx, BloxrouteMessageType.TRANSACTIONS: self.msg_txs, BloxrouteMessageType.COMPRESSED_BLOCK_TXS: self.msg_compressed_block_txs, BloxrouteMessageType.BLOCK_HOLDING: self.msg_block_holding, BloxrouteMessageType.DISCONNECT_RELAY_PEER: self.msg_disconnect_relay_peer, BloxrouteMessageType.TX_SERVICE_SYNC_TXS: self.tx_sync_service.msg_tx_service_sync_txs, BloxrouteMessageType.TX_SERVICE_SYNC_COMPLETE: self.tx_sync_service.msg_tx_service_sync_complete, BloxrouteMessageType.BLOCK_CONFIRMATION: self.msg_cleanup, BloxrouteMessageType.TRANSACTION_CLEANUP: self.msg_cleanup, BloxrouteMessageType.NOTIFICATION: self.msg_notify, BloxrouteMessageType.REFRESH_BLOCKCHAIN_NETWORK: self.msg_refresh_blockchain_network } msg_size_validation_settings = MessageSizeValidationSettings( self.node.network.max_block_size_bytes, self.node.network.max_tx_size_bytes) self.message_validator = BloxrouteMessageValidator( msg_size_validation_settings, self.protocol_version) self.check_matching_relay_connection_alarm_id: Optional[AlarmId] = None
def test_hello_message(self): self._test_to_old_version(HelloMessage(protocol_version=bloxroute_version_manager.CURRENT_PROTOCOL_VERSION, network_num=constants.DEFAULT_NETWORK_NUM)) old_version_msg = HelloMessageV4( protocol_version=bloxroute_version_manager.CURRENT_PROTOCOL_VERSION, network_num=constants.DEFAULT_NETWORK_NUM) new_version_msg = bloxroute_version_manager.convert_message_from_older_version(4, old_version_msg) self.assertEqual(len(old_version_msg.rawbytes()), len(new_version_msg.rawbytes()[ constants.STARTING_SEQUENCE_BYTES_LEN:-constants.CONTROL_FLAGS_LEN])) self.assertEqual(new_version_msg.rawbytes()[:constants.STARTING_SEQUENCE_BYTES_LEN].tobytes(), constants.STARTING_SEQUENCE_BYTES) self.assertEqual(1, new_version_msg.rawbytes()[-1])
def test_process_message_handler(self): mock_pong = MagicMock() self.connection.message_handlers = { b"hello": self.connection.msg_hello, b"pong": mock_pong } self.connection.state = ConnectionState.ESTABLISHED self.connection.inputbuf.add_bytes( HelloMessage(protocol_version=1, network_num=2).rawbytes()) self.connection.inputbuf.add_bytes(PongMessage().rawbytes()) self.connection.process_message() mock_pong.assert_called_once()
def test_process_message_handler_abort_in_between_messages(self): mock_pong = MagicMock() self.connection.message_handlers = { b"hello": self.connection.msg_hello, b"ack": lambda _msg: self.connection.mark_for_close(), b"pong": mock_pong } self.connection.state = ConnectionState.ESTABLISHED self.connection.inputbuf.add_bytes( HelloMessage(protocol_version=1, network_num=2).rawbytes()) self.connection.inputbuf.add_bytes(AckMessage().rawbytes()) self.connection.inputbuf.add_bytes(PongMessage().rawbytes()) self.connection.process_message() mock_pong.assert_not_called()
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 hello_message(self) -> HelloMessage: return HelloMessage(protocol_version.PROTOCOL_VERSION, self.NETWORK_NUMBER, self.NODE_ID)
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_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 hello_message(): return HelloMessage(protocol_version=bloxroute_version_manager.CURRENT_PROTOCOL_VERSION, network_num=DEFAULT_NETWORK_NUM)
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)