def test_msg_hello_only_connection(self): main_node = create_node(40001) peer_node = create_node(40000) main_inbound_fileno = 1 main_inbound_connection = create_connection( 50000, main_node, file_no=main_inbound_fileno, from_me=False) peer_outbound_fileno = 2 peer_outbound_connection = create_connection( 40001, peer_node, file_no=peer_outbound_fileno, from_me=True) self.assertTrue( main_node.connection_pool.has_connection(LOCALHOST, 50000)) main_inbound_connection.msg_hello( GatewayHelloMessage( gateway_version_manager.CURRENT_PROTOCOL_VERSION, DEFAULT_NETWORK_NUM, LOCALHOST, 40000, 1, )) # update port reference self.assertFalse( main_node.connection_pool.has_connection(LOCALHOST, 50000)) self.assertTrue( main_node.connection_pool.has_connection(LOCALHOST, 40000)) self.assertTrue( peer_node.connection_pool.has_connection(LOCALHOST, 40001)) peer_outbound_connection.msg_ack(AckMessage()) self.assertTrue(main_inbound_connection.established) self.assertTrue(peer_outbound_connection.established)
def test_msg_hello_replace_other_connection(self): inbound_fileno = 1 inbound_connection = create_connection(40000, self.node, file_no=inbound_fileno, from_me=False) self.assertTrue( self.node.connection_pool.has_connection(LOCALHOST, 40000)) outbound_fileno = 2 outbound_connection = create_connection(8001, self.node, file_no=outbound_fileno, from_me=True) outbound_connection.ordering = 1 self.assertTrue( self.node.connection_pool.has_connection(LOCALHOST, 8001)) inbound_connection.msg_hello( GatewayHelloMessage( gateway_version_manager.CURRENT_PROTOCOL_VERSION, DEFAULT_NETWORK_NUM, LOCALHOST, 8001, 10, )) self.assertFalse(outbound_connection.is_alive()) self.assertFalse( self.node.connection_pool.has_connection(LOCALHOST, 40000)) self.assertTrue( self.node.connection_pool.has_connection(LOCALHOST, 8001))
def _initialize_ordered_handshake(self): self.ordering = random.getrandbits(constants.UL_INT_SIZE_IN_BYTES * 8) self.enqueue_msg( GatewayHelloMessage(self.protocol_version, self.network_num, self.node.opts.external_ip, self.node.opts.external_port, self.ordering, self.node.opts.node_id))
def test_msg_hello_not_rejecting_on_mismatched_ip(self): inbound_connection = create_connection(40000, self.node, from_me=False) self.assertTrue( self.node.connection_pool.has_connection(LOCALHOST, 40000)) inbound_connection.msg_hello( GatewayHelloMessage( gateway_version_manager.CURRENT_PROTOCOL_VERSION, DEFAULT_NETWORK_NUM, "192.168.1.1", 8001, 1, )) self.assertTrue(inbound_connection.is_alive())
def test_message_preview_success_all_gateway_types(self): self.get_message_preview_successfully( GatewayHelloMessage(123, 1, "127.0.0.1", 40000, 1), GatewayMessageType.HELLO, GatewayHelloMessage.PAYLOAD_LENGTH, ) self.get_message_preview_successfully( BlockReceivedMessage(self.HASH), GatewayMessageType.BLOCK_RECEIVED, BlockReceivedMessage.PAYLOAD_LENGTH, ) self.get_message_preview_successfully( BlockPropagationRequestMessage(self.BLOCK), GatewayMessageType.BLOCK_PROPAGATION_REQUEST, len(self.BLOCK) + constants.CONTROL_FLAGS_LEN, ) self.get_message_preview_successfully( BlockHoldingMessage(self.HASH, network_num=123), BloxrouteMessageType.BLOCK_HOLDING, BlockHoldingMessage.PAYLOAD_LENGTH, ) self.get_message_preview_successfully( ConfirmedTxMessage(self.HASH, self.TX_VAL), GatewayMessageType.CONFIRMED_TX, ConfirmedTxMessage.PAYLOAD_LENGTH + len(self.TX_VAL), ) self.get_message_preview_successfully( RequestTxStreamMessage(), GatewayMessageType.REQUEST_TX_STREAM, constants.CONTROL_FLAGS_LEN, ) hash_val = BtcObjectHash(buf=crypto.double_sha256(b"123"), length=crypto.SHA256_HASH_LEN) blockchain_message = GetBlocksBtcMessage(12345, 23456, [hash_val], hash_val).rawbytes() self.get_message_preview_successfully( BlockchainSyncRequestMessage(GetBlocksBtcMessage.MESSAGE_TYPE, blockchain_message), BlockchainSyncRequestMessage.MESSAGE_TYPE, MSG_TYPE_LEN + len(blockchain_message), ) self.get_message_preview_successfully( BlockchainSyncResponseMessage(GetBlocksBtcMessage.MESSAGE_TYPE, blockchain_message), BlockchainSyncResponseMessage.MESSAGE_TYPE, MSG_TYPE_LEN + len(blockchain_message), )
def test_msg_hello_calls_on_two_nodes_retry(self): main_port = 8000 peer_port = 8001 main_inbound_fileno = 1 main_inbound_port = 40000 main_inbound_connection = create_connection( main_inbound_port, self.node, file_no=main_inbound_fileno, from_me=False) main_inbound_connection.enqueue_msg = MagicMock() self.assertEqual(GatewayConnection.NULL_ORDERING, main_inbound_connection.ordering) self.assertTrue( self.node.connection_pool.has_connection(LOCALHOST, main_inbound_port)) main_outbound_fileno = 2 main_outbound_ordering = 10 main_outbound_connection = create_connection( peer_port, self.node, file_no=main_outbound_fileno, from_me=True) main_outbound_connection.ordering = main_outbound_ordering self.assertNotEqual(GatewayConnection.NULL_ORDERING, main_outbound_connection.ordering) self.assertTrue( self.node.connection_pool.has_connection(LOCALHOST, peer_port)) peer_node = create_node(peer_port) peer_inbound_fileno = 1 peer_inbound_port = 40001 peer_inbound_connection = create_connection( peer_inbound_port, peer_node, file_no=peer_inbound_fileno, from_me=False) peer_inbound_connection.enqueue_msg = MagicMock() self.assertEqual(GatewayConnection.NULL_ORDERING, peer_inbound_connection.ordering) self.assertTrue( peer_node.connection_pool.has_connection(LOCALHOST, peer_inbound_port)) peer_outbound_fileno = 2 peer_outbound_ordering = 10 peer_outbound_connection = create_connection( main_port, peer_node, file_no=peer_outbound_fileno, from_me=True) peer_outbound_connection.ordering = peer_outbound_ordering self.assertNotEqual(GatewayConnection.NULL_ORDERING, peer_outbound_connection.ordering) self.assertTrue( peer_node.connection_pool.has_connection(LOCALHOST, main_port)) main_inbound_connection.msg_hello( GatewayHelloMessage( gateway_version_manager.CURRENT_PROTOCOL_VERSION, DEFAULT_NETWORK_NUM, LOCALHOST, peer_port, peer_outbound_ordering, )) peer_inbound_connection.msg_hello( GatewayHelloMessage( gateway_version_manager.CURRENT_PROTOCOL_VERSION, DEFAULT_NETWORK_NUM, LOCALHOST, main_port, main_outbound_ordering, )) self.assertTrue(main_inbound_connection.is_alive()) self.assertTrue(main_outbound_connection.is_alive()) self.assertTrue(peer_inbound_connection.is_alive()) self.assertTrue(peer_outbound_connection.is_alive()) main_inbound_connection.enqueue_msg.assert_called_once() peer_inbound_connection.enqueue_msg.assert_called_once() main_inbound_connection.ordering = 11 peer_inbound_connection.ordering = 12 main_outbound_connection.msg_hello( GatewayHelloMessage( gateway_version_manager.CURRENT_PROTOCOL_VERSION, DEFAULT_NETWORK_NUM, LOCALHOST, peer_port, peer_inbound_connection.ordering, )) peer_outbound_connection.msg_hello( GatewayHelloMessage( gateway_version_manager.CURRENT_PROTOCOL_VERSION, DEFAULT_NETWORK_NUM, LOCALHOST, main_port, main_inbound_connection.ordering, )) self.assertFalse(main_inbound_connection.is_alive()) self.assertFalse(peer_outbound_connection.is_alive())
def test_create_message_success_all_gateway_types(self): hello_message = self.create_message_successfully( GatewayHelloMessage(123, 1, "127.0.0.1", 40001, 1), GatewayHelloMessage) self.assertEqual(123, hello_message.protocol_version()) self.assertEqual(1, hello_message.network_num()) self.assertEqual("127.0.0.1", hello_message.ip()) self.assertEqual(40001, hello_message.port()) self.assertEqual(1, hello_message.ordering()) block_recv_message = self.create_message_successfully( BlockReceivedMessage(self.HASH), BlockReceivedMessage) self.assertEqual(self.HASH, block_recv_message.block_hash()) block_holding_message = self.create_message_successfully( BlockHoldingMessage(self.HASH, network_num=123), BlockHoldingMessage) self.assertEqual(self.HASH, block_holding_message.block_hash()) block_propagation_request_message = self.create_message_successfully( BlockPropagationRequestMessage(self.BLOCK), BlockPropagationRequestMessage) self.assertEqual(self.BLOCK, block_propagation_request_message.blob()) hash_val = BtcObjectHash(buf=crypto.double_sha256(b"123"), length=crypto.SHA256_HASH_LEN) blockchain_message_in = GetBlocksBtcMessage(12345, 23456, [hash_val], hash_val).rawbytes() sync_request_message = self.create_message_successfully( BlockchainSyncRequestMessage(GetBlocksBtcMessage.MESSAGE_TYPE, blockchain_message_in), BlockchainSyncRequestMessage, ) blockchain_message_out = GetBlocksBtcMessage( buf=sync_request_message.payload()) self.assertEqual(12345, blockchain_message_out.magic()) self.assertEqual(23456, blockchain_message_out.version()) self.assertEqual(1, blockchain_message_out.hash_count()) self.assertEqual(hash_val, blockchain_message_out.hash_stop()) self.create_message_successfully( BlockchainSyncResponseMessage(GetBlocksBtcMessage.MESSAGE_TYPE, blockchain_message_in), BlockchainSyncResponseMessage, ) blockchain_message_out = GetBlocksBtcMessage( buf=sync_request_message.payload()) self.assertEqual(12345, blockchain_message_out.magic()) self.assertEqual(23456, blockchain_message_out.version()) self.assertEqual(1, blockchain_message_out.hash_count()) self.assertEqual(hash_val, blockchain_message_out.hash_stop()) confirmed_tx = self.create_message_successfully( ConfirmedTxMessage(self.HASH, self.TX_VAL), ConfirmedTxMessage) self.assertEqual(self.HASH, confirmed_tx.tx_hash()) self.assertEqual(self.TX_VAL, confirmed_tx.tx_val()) confirmed_tx_no_content = self.create_message_successfully( ConfirmedTxMessage(self.HASH), ConfirmedTxMessage) self.assertEqual(self.HASH, confirmed_tx_no_content.tx_hash()) self.assertEqual(TxMessage.EMPTY_TX_VAL, confirmed_tx_no_content.tx_val()) self.create_message_successfully(RequestTxStreamMessage(), RequestTxStreamMessage)