def setUp(self): self.node = MockGatewayNode(gateway_helpers.get_gateway_opts(8000)) self.node_conn = cast( AbstractGatewayBlockchainConnection, MockConnection( MockSocketConnection(1, self.node, ip_address=LOCALHOST, port=8002), self.node)) self.node.blockchain_peers.add( BlockchainPeerInfo(self.node_conn.peer_ip, self.node_conn.peer_port)) self.node_conn_2 = cast( AbstractGatewayBlockchainConnection, MockConnection( MockSocketConnection(2, self.node, ip_address=LOCALHOST, port=8003), self.node)) self.node.blockchain_peers.add( BlockchainPeerInfo(self.node_conn_2.peer_ip, self.node_conn_2.peer_port)) self.node_conn_3 = cast( AbstractGatewayBlockchainConnection, MockConnection( MockSocketConnection(3, self.node, ip_address=LOCALHOST, port=8004), self.node)) self.node.blockchain_peers.add( BlockchainPeerInfo(self.node_conn_3.peer_ip, self.node_conn_3.peer_port))
def test_split_relay_connection(self): relay_connections = [ OutboundPeerModel(LOCALHOST, 8001, node_type=NodeType.RELAY_BLOCK) ] network_latency.get_best_relays_by_ping_latency_one_per_country = MagicMock( return_value=[relay_connections[0]]) opts = gateway_helpers.get_gateway_opts(8000, split_relays=True, include_default_btc_args=True) if opts.use_extensions: helpers.set_extensions_parallelism() node = GatewayNode(opts) node.enqueue_connection = MagicMock() node._register_potential_relay_peers( node._find_best_relay_peers( network_latency. get_best_relays_by_ping_latency_one_per_country())) self.assertEqual(1, len(node.peer_relays)) self.assertEqual(1, len(node.peer_transaction_relays)) self.assertEqual(8002, next(iter(node.peer_transaction_relays)).port) node.enqueue_connection.assert_has_calls([ call(LOCALHOST, 8001, ConnectionType.RELAY_BLOCK), call(LOCALHOST, 8002, ConnectionType.RELAY_TRANSACTION), ], any_order=True) node.on_connection_added( MockSocketConnection(1, node, ip_address=LOCALHOST, port=8001)) self._check_connection_pool(node, 1, 1, 0, 1) node.on_connection_added( MockSocketConnection(2, node, ip_address=LOCALHOST, port=8002)) self._check_connection_pool(node, 2, 1, 1, 2)
def setUp(self): self.conn_pool1 = ConnectionPool() self.fileno1 = 1 self.ip1 = "123.123.123.123" self.port1 = 1000 self.node1 = MockNode( helpers.get_common_opts(1001, external_ip="128.128.128.128")) self.node_id1 = str(uuid.uuid1()) self.conn1 = MockConnection( MockSocketConnection(self.fileno1, ip_address=self.ip1, port=self.port1), self.node1) self.fileno2 = 5 self.ip2 = "234.234.234.234" self.port2 = 2000 self.node2 = MockNode( helpers.get_common_opts(1003, external_ip="321.321.321.321")) self.node_id2 = str(uuid.uuid1()) self.conn2 = MockConnection( MockSocketConnection(self.fileno2, ip_address=self.ip2, port=self.port2), self.node2) self.fileno3 = 6 self.ip3 = "234.234.234.234" self.port3 = 3000 self.node3 = MockNode( helpers.get_common_opts(1003, external_ip="213.213.213.213.")) self.node_id3 = str(uuid.uuid1()) self.conn3 = MockConnection( MockSocketConnection(self.fileno3, ip_address=self.ip3, port=self.port3), self.node3)
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 setUp(self): self.node = MockGatewayNode( gateway_helpers.get_gateway_opts(8000, include_default_btc_args=True, include_default_eth_args=True)) self.relay_connection = AbstractRelayConnection( MockSocketConnection(node=self.node, ip_address="127.0.0.1", port=12345), self.node) self.blockchain_connection = EthBaseConnection( MockSocketConnection(node=self.node, ip_address="127.0.0.1", port=12345), self.node) self.node.message_converter = converter_factory.create_eth_message_converter( self.node.opts) dummy_private_key = crypto_utils.make_private_key( helpers.generate_bytearray(111)) dummy_public_key = crypto_utils.private_to_public_key( dummy_private_key) self.blockchain_connection_protocol = EthNodeConnectionProtocol( self.blockchain_connection, True, dummy_private_key, dummy_public_key) self.blockchain_connection.network_num = 0 self.blockchain_connection_protocol.publish_transaction = MagicMock() self.relay_connection.state = ConnectionState.INITIALIZED gateway_transaction_stats_service.set_node(self.node)
def setUp(self): opts = gateway_helpers.get_gateway_opts(8000, include_default_btc_args=True) if opts.use_extensions: helpers.set_extensions_parallelism(opts.thread_pool_parallelism_degree) node_ssl_service = MockNodeSSLService(BtcGatewayNode.NODE_TYPE, MagicMock()) self.gateway_node = BtcGatewayNode(opts, node_ssl_service) self.gateway_node.opts.has_fully_updated_tx_service = True self.sut = BtcRelayConnection( MockSocketConnection(1, node=self.gateway_node, ip_address=LOCALHOST, port=8001), self.gateway_node ) self.gateway_node.message_converter = converter_factory.create_btc_message_converter( 12345, self.gateway_node.opts ) self.node_conn = MockConnection( MockSocketConnection(1, self.gateway_node, ip_address=LOCALHOST, port=8002), self.gateway_node ) self.gateway_node.connection_pool.add(1, LOCALHOST, 8002, self.node_conn) gateway_helpers.add_blockchain_peer(self.gateway_node, self.node_conn) self.node_conn_2 = MockConnection( MockSocketConnection(1, self.gateway_node, ip_address=LOCALHOST, port=8003), self.gateway_node ) self.gateway_node.connection_pool.add(1, LOCALHOST, 8003, self.node_conn_2) gateway_helpers.add_blockchain_peer(self.gateway_node, self.node_conn_2) self.blockchain_connections = [self.node_conn, self.node_conn_2] self.node_conn.enqueue_msg = MagicMock() self.node_conn_2.enqueue_msg = MagicMock() self.gateway_node.broadcast = MagicMock() self.sut.enqueue_msg = MagicMock() gateway_transaction_stats_service.set_node(self.gateway_node) self.gateway_node.has_active_blockchain_peer = MagicMock(return_value=True)
def setUp(self): opts = gateway_helpers.get_gateway_opts(8000, include_default_ont_args=True) if opts.use_extensions: helpers.set_extensions_parallelism( opts.thread_pool_parallelism_degree) node_ssl_service = MockNodeSSLService(OntGatewayNode.NODE_TYPE, MagicMock()) self.gateway_node = OntGatewayNode(opts, node_ssl_service) self.gateway_node.opts.has_fully_updated_tx_service = True self.gateway_node.opts.is_consensus = False self.sut = OntRelayConnection( MockSocketConnection(node=self.gateway_node, ip_address=LOCALHOST, port=8001), self.gateway_node) self.gateway_node.node_conn = MockConnection( MockSocketConnection(1, self.gateway_node, ip_address=LOCALHOST, port=8002), self.gateway_node) self.gateway_node_sut = OntNodeConnectionProtocol( self.gateway_node.node_conn) self.gateway_node.message_converter = converter_factory.create_ont_message_converter( 12345, self.gateway_node.opts) self.gateway_node.node_conn.state = ConnectionState.ESTABLISHED self.gateway_node.send_msg_to_node = MagicMock() self.sut.enqueue_msg = MagicMock() gateway_transaction_stats_service.set_node(self.gateway_node)
def initialize_split_relay_node(): relay_connections = [ OutboundPeerModel(LOCALHOST, 8001, node_type=NodeType.RELAY_BLOCK) ] network_latency.get_best_relays_by_ping_latency_one_per_country = MagicMock( return_value=[relay_connections[0]]) opts = gateway_helpers.get_gateway_opts(8000, split_relays=True, include_default_btc_args=True) if opts.use_extensions: helpers.set_extensions_parallelism() node = GatewayNode(opts) node.enqueue_connection = MagicMock() node._register_potential_relay_peers( node._find_best_relay_peers( network_latency.get_best_relays_by_ping_latency_one_per_country())) node.enqueue_connection.assert_has_calls([ call(LOCALHOST, 8001, ConnectionType.RELAY_BLOCK), call(LOCALHOST, 8002, ConnectionType.RELAY_TRANSACTION), ], any_order=True) node.on_connection_added( MockSocketConnection(1, node, ip_address=LOCALHOST, port=8001)) node.on_connection_added( MockSocketConnection(2, node, ip_address=LOCALHOST, port=8002)) node.alarm_queue = AlarmQueue() node.enqueue_connection.reset_mock() return node
def setUp(self): self.node = MockGatewayNode( gateway_helpers.get_gateway_opts( 8000, max_block_interval_s=gateway_constants.MAX_INTERVAL_BETWEEN_BLOCKS_S, blockchain_message_ttl=TTL ) ) self.node_conn = MockConnection( MockSocketConnection(1, self.node, ip_address=LOCALHOST, port=8002), self.node ) self.node.blockchain_peers.add(BlockchainPeerInfo(self.node_conn.peer_ip, self.node_conn.peer_port)) self.block_queuing_service = TestPushBlockQueuingService(self.node, self.node_conn) self.node.block_queuing_service_manager.add_block_queuing_service(self.node_conn, self.block_queuing_service) self.node_conn.enqueue_msg = MagicMock() self.node_conn_2 = MockConnection( MockSocketConnection(2, self.node, ip_address=LOCALHOST, port=8003), self.node ) self.node.blockchain_peers.add(BlockchainPeerInfo(self.node_conn_2.peer_ip, self.node_conn_2.peer_port)) self.block_queuing_service_2 = TestPushBlockQueuingService(self.node, self.node_conn_2) self.node.block_queuing_service_manager.add_block_queuing_service(self.node_conn_2, self.block_queuing_service_2) self.node_conn_2.enqueue_msg = MagicMock() self.blockchain_connections = [self.node_conn, self.node_conn_2] self.block_queuing_services = [self.block_queuing_service, self.block_queuing_service_2]
def of_type(gateway_class: Type[AbstractGatewayNode], opts: GatewayOpts) -> FullGatewayInfo: # probably pyre bug # pyre-fixme[45]: Cannot instantiate abstract class `AbstractGatewayNode`. gateway = gateway_class(opts, MockNodeSSLService(gateway_class.NODE_TYPE, MagicMock())) gateway.requester = MagicMock() blockchain_fileno = 1 relay_fileno = 2 gateway_fileno = 3 blockchain_socket = MockSocketConnection(file_no=blockchain_fileno, ip_address=constants.LOCALHOST, port=7000) relay_socket = MockSocketConnection(file_no=relay_fileno, ip_address=constants.LOCALHOST, port=7001) gateway_socket = MockSocketConnection(file_no=gateway_fileno, ip_address=constants.LOCALHOST, port=7002) gateway.on_connection_added(blockchain_socket) gateway.peer_relays.add(OutboundPeerModel(constants.LOCALHOST, 7001)) gateway.on_connection_added(relay_socket) gateway.peer_gateways.add(OutboundPeerModel(constants.LOCALHOST, 7002)) gateway.on_connection_added(gateway_socket) blockchain_connection = gateway.connection_pool.get_by_fileno(blockchain_fileno) assert blockchain_connection is not None assert isinstance(blockchain_connection, AbstractGatewayBlockchainConnection) relay_connection = gateway.connection_pool.get_by_fileno(relay_fileno) assert relay_connection is not None assert isinstance(relay_connection, AbstractRelayConnection) gateway_connection = gateway.connection_pool.get_by_fileno(gateway_fileno) assert gateway_connection is not None assert isinstance(gateway_connection, GatewayConnection) gateway.on_blockchain_connection_ready(blockchain_connection) blockchain_connection.state |= ConnectionState.ESTABLISHED relay_connection.state |= ConnectionState.ESTABLISHED gateway_connection.state |= ConnectionState.ESTABLISHED # clear output buffers if blockchain_connection.outputbuf.length: blockchain_connection.advance_sent_bytes(blockchain_connection.outputbuf.length) if relay_connection.outputbuf.length: relay_connection.advance_sent_bytes(relay_connection.outputbuf.length) if gateway_connection.outputbuf.length: gateway_connection.advance_sent_bytes(gateway_connection.outputbuf.length) return FullGatewayInfo( gateway, blockchain_fileno, relay_fileno, gateway_fileno, blockchain_socket, relay_socket, gateway_socket, blockchain_connection, relay_connection, gateway_connection )
def test_last_active_blockchain_peer_queuing_service_not_destroyed(self): opts = gateway_helpers.get_gateway_opts(8000, blockchain_address=(LOCALHOST, 8001)) node = GatewayNode(opts) conn = MockSocketConnection(1, node, ip_address=LOCALHOST, port=8001) node.on_connection_added(conn) self.assertEqual( 1, len( list( node.connection_pool.get_by_connection_types( [ConnectionType.BLOCKCHAIN_NODE])))) blockchain_conn = next( iter( node.connection_pool.get_by_connection_types( [ConnectionType.BLOCKCHAIN_NODE]))) node.on_blockchain_connection_ready(blockchain_conn) blockchain_conn.on_connection_established() self.assertTrue(node.has_active_blockchain_peer()) self.assertTrue(blockchain_conn in node.block_queuing_service_manager. blockchain_peer_to_block_queuing_service) blockchain_conn2 = node.build_blockchain_connection( MockSocketConnection(2, node, ip_address=LOCALHOST, port=8002)) blockchain_conn2.CONNECTION_TYPE = ConnectionType.BLOCKCHAIN_NODE node.connection_pool.add(2, LOCALHOST, 8002, blockchain_conn2) self.assertEqual( 2, len( list( node.connection_pool.get_by_connection_types( [ConnectionType.BLOCKCHAIN_NODE])))) blockchain_conn2 = node.connection_pool.get_by_ipport(LOCALHOST, 8002) node.on_blockchain_connection_ready(blockchain_conn2) blockchain_conn2.on_connection_established() self.assertIsNotNone( node.block_queuing_service_manager.get_block_queuing_service( blockchain_conn2)) self.assertTrue(blockchain_conn2 in node.block_queuing_service_manager. blockchain_peer_to_block_queuing_service) node.connection_pool.delete(blockchain_conn2) node.on_blockchain_connection_destroyed(blockchain_conn2) self.assertFalse(blockchain_conn2 in node.block_queuing_service_manager .blockchain_peer_to_block_queuing_service) node.connection_pool.delete(blockchain_conn) node.on_blockchain_connection_destroyed(blockchain_conn) self.assertTrue(blockchain_conn in node.block_queuing_service_manager. blockchain_peer_to_block_queuing_service) node.block_queuing_service_manager.add_block_queuing_service = MagicMock( ) node.on_blockchain_connection_ready(blockchain_conn) node.block_queuing_service_manager.add_block_queuing_service.assert_not_called( )
def test_set_node_public_key(self): dummy_con_fileno = 123 dummy_con_ip = "0.0.0.0" dummy_con_port = 12345 node = self._set_up_test_node(False, generate_pub_key=True) discovery_connection = EthNodeDiscoveryConnection( MockSocketConnection(dummy_con_fileno, node, ip_address=dummy_con_ip, port=dummy_con_port), node) node.connection_pool.add(dummy_con_fileno, dummy_con_ip, dummy_con_port, discovery_connection) self.assertEqual(1, len(self.node.connection_pool)) node_public_key = node.get_node_public_key(self.blockchain_ip, self.blockchain_port) self.assertIsNotNone(node_public_key) new_node_public_key = self._get_dummy_public_key() node.set_node_public_key(discovery_connection, new_node_public_key) self.assertTrue(SocketConnectionState.MARK_FOR_CLOSE in discovery_connection.socket_connection.state) self.assertTrue(SocketConnectionState.DO_NOT_RETRY in discovery_connection.socket_connection.state) updated_node_public_key = node.get_node_public_key( self.blockchain_ip, self.blockchain_port) self.assertIsNotNone(updated_node_public_key)
def test_gateway_peer_never_destroy_cli_peer(self): peer_gateways = [OutboundPeerModel(LOCALHOST, 8001)] opts = gateway_helpers.get_gateway_opts(8000, peer_gateways=peer_gateways) if opts.use_extensions: helpers.set_extensions_parallelism() node = GatewayNode(opts) node.enqueue_connection = MagicMock() node.alarm_queue.register_alarm = MagicMock() node.alarm_queue.register_approx_alarm = MagicMock() mock_socket = MockSocketConnection(7, node, ip_address=LOCALHOST, port=8001) node.on_connection_added(mock_socket) cli_peer_conn = node.connection_pool.get_by_ipport(LOCALHOST, 8001) node.num_retries_by_ip[(LOCALHOST, 8001)] = MAX_CONNECT_RETRIES cli_peer_conn.state = ConnectionState.CONNECTING node._connection_timeout(cli_peer_conn) self.assertFalse(mock_socket.alive) node.on_connection_closed(mock_socket.fileno()) # timeout is fib(3) == 3 node.alarm_queue.register_alarm.assert_has_calls([ call(3, node._retry_init_client_socket, LOCALHOST, 8001, ConnectionType.EXTERNAL_GATEWAY) ]) node._retry_init_client_socket(LOCALHOST, 8001, ConnectionType.EXTERNAL_GATEWAY) self.assertEqual(MAX_CONNECT_RETRIES + 1, node.num_retries_by_ip[(LOCALHOST, 8001)])
def test_has_active_blockchain_peer(self): opts = gateway_helpers.get_gateway_opts( 8000, blockchain_address=(LOCALHOST, 8001), ) node = GatewayNode(opts) self.assertFalse(node.has_active_blockchain_peer()) node.on_connection_added( MockSocketConnection(1, node, ip_address=LOCALHOST, port=8001)) self.assertEqual( 1, len( list( node.connection_pool.get_by_connection_types( [ConnectionType.BLOCKCHAIN_NODE])))) blockchain_conn = next( iter( node.connection_pool.get_by_connection_types( [ConnectionType.BLOCKCHAIN_NODE]))) node.on_blockchain_connection_ready(blockchain_conn) # connection added, but inactive self.assertFalse(node.has_active_blockchain_peer()) blockchain_conn.on_connection_established() self.assertTrue(node.has_active_blockchain_peer())
def setUp(self): self.node = MockGatewayNode(gateway_helpers.get_gateway_opts( 8000, include_default_btc_args=True, compact_block_min_tx_count=5 )) self.node.block_processing_service = MagicMock() self.connection = BtcNodeConnection( MockSocketConnection(node=self.node, ip_address=LOCALHOST, port=123), self.node ) self.connection.node = self.node self.connection.peer_ip = LOCALHOST self.connection.peer_port = 8001 self.connection.network_num = 2 self.sut = BtcNodeConnectionProtocol(self.connection) full_block_msg = BlockBtcMessage( buf=bytearray(convert.hex_to_bytes(self.FULL_BLOCK_BYTES_HEX)) ) if self.node.opts.use_extensions: transaction_service = ExtensionTransactionService(self.node, 0) else: transaction_service = TransactionService(self.node, 0) short_id = 1 for tx in full_block_msg.txns(): tx_hash = btc_common_utils.get_txid(tx) transaction_service.set_transaction_contents(tx_hash, tx) transaction_service.assign_short_id(tx_hash, short_id) short_id += 1 self.sut.connection.node._tx_service = transaction_service
async def test_remove_blockchain_peer(self): conn = MockConnection( MockSocketConnection(9, self.gateway_node, ip_address="127.0.0.1", port=30302), self.gateway_node) conn.mark_for_close = MagicMock() self.gateway_node.connection_pool.add(9, "127.0.0.1", 30302, conn) self.gateway_node.blockchain_peers.add( BlockchainPeerInfo( "127.0.0.1", 30302, "d76d7d11a822fab02836f8b0ea462205916253eb630935d15191fb6f9d218cd94a768fc5b3d5516b9ed5010a4765f95aea7124a39d0ab8aaf6fa3d57e21ef396" )) result = await self.request( BxJsonRpcRequest( "9", RpcRequestType.REMOVE_BLOCKCHAIN_PEER, { "peer": "enode://d76d7d11a822fab02836f8b0ea462205916253eb630935d15191fb6f9d218cd94a768fc5b3d5516b9ed5010a4765f95aea7124a39d0ab8aaf6fa3d57e21ef396@127.0.0.1:30302" })) self.assertNotIn(BlockchainPeerInfo("127.0.0.1", 30302), self.gateway_node.blockchain_peers) conn.mark_for_close.assert_called_once_with(False) self.assertEqual("9", result.id) self.assertIsNone(result.error) self.assertEqual({"removed_peer": "127.0.0.1:30302"}, result.result)
def test_get_gateway_connection_class__initiate_handshake_no_remote_pub_key( self): node = self._set_up_test_node(True, generate_pub_key=True) connection = node.build_blockchain_connection( MockSocketConnection(ip_address=self.blockchain_ip, port=self.blockchain_port)) self.assertIsInstance(connection, EthNodeDiscoveryConnection)
def setUp(self): opts = gateway_helpers.get_gateway_opts(8000, include_default_ont_args=True) if opts.use_extensions: helpers.set_extensions_parallelism() self.node = MockOntGatewayNode(opts) self.node.neutrality_service = MagicMock() self.connection = OntNodeConnection( MockSocketConnection(1, node=self.node, ip_address=LOCALHOST, port=123), self.node) gateway_helpers.add_blockchain_peer(self.node, self.connection) self.tx_hash = OntObjectHash(buf=helpers.generate_bytearray(32), length=ONT_HASH_LEN) self.block_hash = OntObjectHash(buf=helpers.generate_bytearray(32), length=ONT_HASH_LEN) self.sut = OntNodeConnectionProtocol(self.connection) while self.connection.outputbuf.length > 0: initial_bytes = self.connection.get_bytes_to_send() self.connection.advance_sent_bytes(len(initial_bytes))
def test_remote_sync_response_preferred_response(self): self.node.send_msg_to_node = MagicMock() responding_connection = GatewayConnection(MockSocketConnection(), (LOCALHOST, 8002), self.node) request = GetHeadersBtcMessage(self.MAGIC, self.VERSION, self.HASHES, self.HASH2) self.blockchain_sync_service.make_sync_request(request.command(), request) remote_response = HeadersBtcMessage(self.MAGIC, headers=[]) self.blockchain_sync_service.process_remote_sync_response( remote_response.command(), remote_response, responding_connection) time.time = MagicMock( return_value=time.time() + gateway_constants.BLOCKCHAIN_SYNC_BROADCAST_DELAY_S) self.node.alarm_queue.fire_alarms() self.node.send_msg_to_node.assert_called_once_with(remote_response) self.assertEqual( 0, len(self.blockchain_sync_service. awaiting_local_callback_connections)) self.assertEqual(0, len(self.node.broadcast_messages))
def test_remove_blockchain_connection_and_add_new(self): self._add_connections() new_desc6 = "1.1.1.1 80" new_ip6 = new_desc6.split()[0] new_port6 = new_desc6.split()[1] new_fileno6 = "10" new_node6 = MockNode(helpers.get_common_opts(1004, external_ip="214.215.216.217")) new_conn6 = MockConnection( MockSocketConnection(new_fileno6, new_node6, ip_address=new_ip6, port=int(new_port6)), new_node6 ) self.conn_pool.delete(self.conn3) self.blockchain_peers.add(BlockchainPeerInfo(new_ip6, int(new_port6))) self.conn_pool.add(int(new_fileno6), new_ip6, int(new_port6), new_conn6) update(self.conn_pool, True, self.source_version, self.ip_address, self.continent, self.country, False, self.blockchain_peers, self.account_id, self.quota_level) summary_loaded, _, _, network_loaded = self._load_status_file() self.assertEqual(summary_loaded, network_loaded.get_summary(self.ip_address, self.continent, self.country, False, self.account_id, self.quota_level)) self.assertEqual(summary_loaded.gateway_status, GatewayStatus.WITH_ERRORS) self.assertEqual(summary_loaded.account_info, summary.gateway_status_get_account_info(None)) self.assertEqual(summary_loaded.quota_level, summary.gateway_status_get_quota_level(self.quota_level)) self.assertEqual( summary_loaded.blockchain_node_connection_states, { f"{new_ip6} {new_port6}": ConnectionState.ESTABLISHED, f"{self.ip3} {self.port3}": ConnectionState.DISCONNECTED, f"{self.ip5} {self.port5}": ConnectionState.ESTABLISHED, } )
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 test_delete_removes_multiple_types(self): class TestConnectionType(SerializableFlag): A = auto() B = auto() AB = A | B conn = MockConnection( MockSocketConnection(1, node=self.node1, ip_address=LOCALHOST, port=8000), self.node1) conn.CONNECTION_TYPE = TestConnectionType.AB self.conn_pool1.add(self.fileno1, LOCALHOST, 8000, conn) self.assertIn( conn, self.conn_pool1.get_by_connection_types([TestConnectionType.A])) self.assertIn( conn, self.conn_pool1.get_by_connection_types([TestConnectionType.B])) self.conn_pool1.delete(conn) self.assertNotIn( conn, self.conn_pool1.get_by_connection_types([TestConnectionType.A])) self.assertNotIn( conn, self.conn_pool1.get_by_connection_types([TestConnectionType.B]))
def test_add_special_objects(self): node1 = MockNode(get_common_opts(1001, external_ip="128.128.128.128")) conn1 = MockConnection( MockSocketConnection(1, node1, ip_address="123.123.123.123", port=1000), node1) conn1.inputbuf.add_bytes(bytearray(b"0000" * 10)) conn1.outputbuf.prepend_msgbytes(bytearray(b"1111" * 100)) total_special_size, ids = memory_utils.get_special_size(conn1) self.assertTrue(ids) self.assertTrue(id(conn1.inputbuf.input_list) in ids) self.assertTrue(id(conn1.outputbuf.output_msgs) in ids) expected_special_size = memory_utils.get_special_size( conn1.inputbuf.input_list).size expected_special_size += memory_utils.get_special_size( conn1.outputbuf.output_msgs).size self.assertEqual(total_special_size, expected_special_size) s, s_id = memory_utils.get_special_size(conn1.outputbuf.output_msgs) self.assertNotEqual(s, 0) s, ids = memory_utils.get_special_size(conn1.outputbuf.output_msgs, ids) self.assertEqual(s, 0)
def create_connection(connection_cls: Type[Connection], node: Optional[AbstractNode] = None, node_opts: Optional[Namespace] = None, fileno: int = 1, ip: str = constants.LOCALHOST, port: int = 8001, from_me: bool = False, add_to_pool: bool = True) -> Connection: if node_opts is None: node_opts = get_common_opts(8002) if node is None: node = MockNode(node_opts) if isinstance(node, MockNode): add_to_pool = False test_address = (ip, port) test_socket_connection = MockSocketConnection(fileno, node) connection = connection_cls(test_socket_connection, test_address, node, from_me) if add_to_pool: node.connection_pool.add(fileno, ip, port, connection) return connection
def test_propagate_block_to_network_unencrypted_block(self): self.node.opts.encrypt_blocks = False block_message = helpers.generate_bytearray(50) block_info = BlockInfo( Sha256Hash(helpers.generate_bytearray(crypto.SHA256_HASH_LEN)), [], datetime.datetime.utcnow(), datetime.datetime.utcnow(), 0, 1, helpers.generate_bytearray(crypto.SHA256_HASH_LEN), helpers.generate_bytearray(crypto.SHA256_HASH_LEN), 0, 0, 0, []) connection = MockConnection( MockSocketConnection(1, self.node, ip_address=LOCALHOST, port=9000), self.node) self.neutrality_service.propagate_block_to_network( block_message, connection, block_info) self.assertEqual(1, len(self.node.broadcast_messages)) broadcast_message, connection_types = self.node.broadcast_messages[0] # self.assertTrue(any(ConnectionType.RELAY_BLOCK & connection_type for connection_type in connection_types)) self.assertTrue( all(ConnectionType.RELAY_BLOCK in connection_type for connection_type in connection_types)) self.assertEqual(block_info.block_hash, broadcast_message.block_hash()) self.assertNotIn(block_info.block_hash, self.node.in_progress_blocks._cache) self.assertNotIn(broadcast_message.block_hash(), self.neutrality_service._receipt_tracker)
def reinitialize_gateways(self, opts1, opts2): node_ssl_service = MockNodeSSLService(BtcGatewayNode.NODE_TYPE, MagicMock()) self.node1 = BtcGatewayNode(opts1, node_ssl_service) self.node1.opts.has_fully_updated_tx_service = True self.node1.requester.send_threaded_request = MagicMock() self.node2 = BtcGatewayNode(opts2, node_ssl_service) self.node2.opts.has_fully_updated_tx_service = True self.node2.requester.send_threaded_request = MagicMock() self.node1.peer_gateways = {OutboundPeerModel(LOCALHOST, 7002)} self.node1.peer_relays = {OutboundPeerModel(LOCALHOST, 7001)} self.node2.peer_gateways = {OutboundPeerModel(LOCALHOST, 7002)} self.node2.peer_relays = {OutboundPeerModel(LOCALHOST, 7001)} self.blockchain_fileno = 1 self.relay_fileno = 2 self.gateway_fileno = 3 self.blockchain_connection = MockSocketConnection( self.blockchain_fileno, self.node1, ip_address=LOCALHOST, port=7000 ) self.relay_connection = MockSocketConnection(self.relay_fileno, self.node1, ip_address=LOCALHOST, port=7001) self.gateway_connection = MockSocketConnection(self.gateway_fileno, self.node1, ip_address=LOCALHOST, port=7002) # add node1 connections self.node1.on_connection_added(self.blockchain_connection) self.node1.on_connection_added(self.relay_connection) self.node1.on_connection_added(self.gateway_connection) # add node 2 connections self.node2.on_connection_added(self.blockchain_connection) self.node2.on_connection_added(self.relay_connection) self.node2.on_connection_added(self.gateway_connection) # initialize node1 connections helpers.receive_node_message(self.node1, self.blockchain_fileno, btc_version_message().rawbytes()) helpers.receive_node_message(self.node1, self.relay_fileno, hello_message().rawbytes()) helpers.receive_node_message(self.node1, self.relay_fileno, AckMessage().rawbytes()) helpers.receive_node_message(self.node1, self.gateway_fileno, AckMessage().rawbytes()) # initialize node2 connections helpers.receive_node_message(self.node2, self.blockchain_fileno, btc_version_message().rawbytes()) helpers.receive_node_message(self.node2, self.relay_fileno, hello_message().rawbytes()) helpers.receive_node_message(self.node2, self.relay_fileno, AckMessage().rawbytes()) helpers.receive_node_message(self.node2, self.gateway_fileno, AckMessage().rawbytes()) self.clear_all_buffers()
def test_remote_sync_response_broadcast_response(self): self.node.send_msg_to_node = MagicMock() not_responding_connection = GatewayConnection(MockSocketConnection(1), (LOCALHOST, 8002), self.node) not_responding_connection.enqueue_msg = MagicMock() not_responding_connection.state |= ConnectionState.ESTABLISHED responding_connection = GatewayConnection(MockSocketConnection(2), (LOCALHOST, 8003), self.node) responding_connection.enqueue_msg = MagicMock() responding_connection.state |= ConnectionState.ESTABLISHED self.node.connection_pool.add(1, LOCALHOST, 8002, not_responding_connection) self.node.connection_pool.add(2, LOCALHOST, 8003, responding_connection) self.node.peer_gateways.add( OutboundPeerModel(LOCALHOST, 8002, is_internal=True)) self.node.peer_gateways.add( OutboundPeerModel(LOCALHOST, 8003, is_internal=False)) request = GetHeadersBtcMessage(self.MAGIC, self.VERSION, self.HASHES, self.HASH2) self.blockchain_sync_service.make_sync_request(request.command(), request) not_responding_connection.enqueue_msg.assert_called_once() responding_connection.enqueue_msg.assert_not_called() time.time = MagicMock( return_value=time.time() + gateway_constants.BLOCKCHAIN_SYNC_BROADCAST_DELAY_S) self.node.alarm_queue.fire_alarms() self.assertEqual(1, len(self.node.broadcast_messages)) remote_response = HeadersBtcMessage(self.MAGIC, headers=[]) self.blockchain_sync_service.process_remote_sync_response( remote_response.command(), remote_response, responding_connection) self.node.send_msg_to_node.assert_called_once_with(remote_response) self.assertEqual( 0, len(self.blockchain_sync_service. awaiting_local_callback_connections)) self.assertEqual(responding_connection, self.node.get_preferred_gateway_connection())
def _initialize_gateway(self, initialize_blockchain_conn: bool, initialize_relay_conn: bool) -> GatewayNode: opts = gateway_helpers.get_gateway_opts( 8000, blockchain_address=(LOCALHOST, 8001), peer_relays=[OutboundPeerModel(LOCALHOST, 8002)], include_default_btc_args=True) if opts.use_extensions: helpers.set_extensions_parallelism() node = GatewayNode(opts) if initialize_blockchain_conn: node.on_connection_added( MockSocketConnection(1, node, ip_address=LOCALHOST, port=8001)) self.assertEqual( 1, len( list( node.connection_pool.get_by_connection_types( [ConnectionType.BLOCKCHAIN_NODE])))) blockchain_conn = next( iter( node.connection_pool.get_by_connection_types( [ConnectionType.BLOCKCHAIN_NODE]))) node.on_blockchain_connection_ready(blockchain_conn) self.assertIsNone(node._blockchain_liveliness_alarm) if initialize_relay_conn: node.on_connection_added( MockSocketConnection(2, node, ip_address=LOCALHOST, port=8002)) self.assertEqual( 1, len( list( node.connection_pool.get_by_connection_types( [ConnectionType.RELAY_ALL])))) relay_conn = next( iter( node.connection_pool.get_by_connection_types( [ConnectionType.RELAY_ALL]))) node.on_relay_connection_ready() self.assertIsNone(node._relay_liveliness_alarm) return node
def _set_bc_connection(self) -> None: self.node.on_connection_added(MockSocketConnection(1, self.node, ip_address=LOCALHOST, port=7000)) self.assertEqual(1, len(list(self.node.connection_pool.get_by_connection_types([ConnectionType.BLOCKCHAIN_NODE])))) self.assertEqual(1, len(self.node.blockchain_peers)) blockchain_conn = next(iter(self.node.connection_pool.get_by_connection_types([ConnectionType.BLOCKCHAIN_NODE]))) self.node.requester.send_threaded_request = MagicMock() self.node.on_blockchain_connection_ready(blockchain_conn) self.assertIsNone(self.node._blockchain_liveliness_alarm)
def setUp(self): self.node = MockGatewayNode( gateway_helpers.get_gateway_opts( 8000, include_default_btc_args=True, include_default_eth_args=True, split_relays=True, ) ) self.connection = AbstractRelayConnection( MockSocketConnection(node=self.node, ip_address=constants.LOCALHOST, port=12345), self.node ) self.connection.state = ConnectionState.INITIALIZED self.blockchain_connecton = AbstractGatewayBlockchainConnection( MockSocketConnection(node=self.node, ip_address=constants.LOCALHOST, port=333), self.node) self.blockchain_connecton.state = ConnectionState.ESTABLISHED