async def test_single_server_payment(self): wallet_pay_service = self.daemon.component_manager.get_component( 'wallet_server_payments') wallet_pay_service.payment_period = 1 # only starts with a positive max key fee wallet_pay_service.max_fee = "0.0" await wallet_pay_service.start(ledger=self.ledger, wallet=self.wallet) self.assertFalse(wallet_pay_service.running) wallet_pay_service.max_fee = "1.0" await wallet_pay_service.start(ledger=self.ledger, wallet=self.wallet) self.assertTrue(wallet_pay_service.running) await wallet_pay_service.stop() await wallet_pay_service.start(ledger=self.ledger, wallet=self.wallet) address = await self.blockchain.get_raw_change_address() _, history = await self.ledger.get_local_status_and_history(address) self.assertEqual(history, []) node = SPVNode(self.conductor.spv_module, node_number=2) await node.start(self.blockchain, extraconf={ "PAYMENT_ADDRESS": address, "DAILY_FEE": "1.1" }) self.addCleanup(node.stop) self.daemon.jsonrpc_settings_set('lbryum_servers', [f"{node.hostname}:{node.port}"]) await self.daemon.jsonrpc_wallet_reconnect() features = await self.ledger.network.get_server_features() self.assertEqual(features["payment_address"], address) self.assertEqual(features["daily_fee"], "1.1") with self.assertRaises(ServerPaymentFeeAboveMaxAllowedError): await asyncio.wait_for(wallet_pay_service.on_payment.first, timeout=3) await node.stop(False) await node.start(self.blockchain, extraconf={ "PAYMENT_ADDRESS": address, "DAILY_FEE": "1.0" }) self.daemon.jsonrpc_settings_set('lbryum_servers', [f"{node.hostname}:{node.port}"]) await self.daemon.jsonrpc_wallet_reconnect() features = await self.ledger.network.get_server_features() self.assertEqual(features["payment_address"], address) self.assertEqual(features["daily_fee"], "1.0") tx = await asyncio.wait_for(wallet_pay_service.on_payment.first, timeout=3) self.assertIsNotNone(await self.blockchain.get_raw_transaction( tx.id)) # verify its broadcasted self.assertEqual(tx.outputs[0].amount, 100000000) self.assertEqual(tx.outputs[0].get_address(self.ledger), address)
async def test_multiple_servers(self): # we have a secondary node that connects later, so node2 = SPVNode(self.conductor.spv_module, node_number=2) await node2.start(self.blockchain) self.ledger.network.config['explicit_servers'].append((node2.hostname, node2.port)) self.ledger.network.config['explicit_servers'].reverse() self.assertEqual(50002, self.ledger.network.client.server[1]) await self.ledger.stop() await self.ledger.start() self.assertTrue(self.ledger.network.is_connected) self.assertEqual(50003, self.ledger.network.client.server[1]) await node2.stop(True) self.assertFalse(self.ledger.network.is_connected) await self.ledger.resolve([], ['derp']) self.assertEqual(50002, self.ledger.network.client.server[1])
async def test_multiple_servers(self): # we have a secondary node that connects later, so node2 = SPVNode(self.conductor.spv_module, node_number=2) self.ledger.network.config['default_servers'].append((node2.hostname, node2.port)) await asyncio.wait_for(self.ledger.stop(), timeout=1) await asyncio.wait_for(self.ledger.start(), timeout=1) self.ledger.network.session_pool.new_connection_event.clear() await node2.start(self.blockchain) # this is only to speed up the test as retrying would take 4+ seconds for session in self.ledger.network.session_pool.sessions: session.trigger_urgent_reconnect.set() await asyncio.wait_for(self.ledger.network.session_pool.new_connection_event.wait(), timeout=1) self.assertEqual(2, len(list(self.ledger.network.session_pool.available_sessions))) self.assertTrue(self.ledger.network.is_connected) switch_event = self.ledger.network.on_connected.first await node2.stop(True) # secondary down, but primary is ok, do not switch! (switches trigger new on_connected events) with self.assertRaises(asyncio.TimeoutError): await asyncio.wait_for(switch_event, timeout=1)
async def test_hub_discovery(self): us_final_node = SPVNode(node_number=2) await us_final_node.start(self.blockchain, extraconf={"country": "US"}) self.addCleanup(us_final_node.stop) final_node_host = f"{us_final_node.hostname}:{us_final_node.port}" kp_final_node = SPVNode(node_number=3) await kp_final_node.start(self.blockchain, extraconf={"country": "KP"}) self.addCleanup(kp_final_node.stop) kp_final_node_host = f"{kp_final_node.hostname}:{kp_final_node.port}" relay_node = SPVNode(node_number=4) await relay_node.start(self.blockchain, extraconf={ "country": "FR", "peer_hubs": ",".join( [kp_final_node_host, final_node_host]) }) relay_node_host = f"{relay_node.hostname}:{relay_node.port}" self.addCleanup(relay_node.stop) self.assertEqual(list(self.daemon.conf.known_hubs), []) self.assertEqual( self.daemon.ledger.network.client.server_address_and_port, ('127.0.0.1', 50002)) # connect to relay hub which will tell us about the final hubs self.daemon.jsonrpc_settings_set('lbryum_servers', [relay_node_host]) await self.daemon.jsonrpc_wallet_reconnect() self.assertEqual( self.daemon.conf.known_hubs.filter(), { (relay_node.hostname, relay_node.port): { "country": "FR" }, (us_final_node.hostname, us_final_node.port): {}, # discovered from relay but not contacted yet (kp_final_node.hostname, kp_final_node.port): {}, # discovered from relay but not contacted yet }) self.assertEqual( self.daemon.ledger.network.client.server_address_and_port, ('127.0.0.1', relay_node.port)) # use known_hubs to connect to final US hub self.daemon.jsonrpc_settings_clear('lbryum_servers') self.daemon.conf.jurisdiction = "US" await self.daemon.jsonrpc_wallet_reconnect() self.assertEqual( self.daemon.conf.known_hubs.filter(), { (relay_node.hostname, relay_node.port): { "country": "FR" }, (us_final_node.hostname, us_final_node.port): { "country": "US" }, (kp_final_node.hostname, kp_final_node.port): { "country": "KP" }, }) self.assertEqual( self.daemon.ledger.network.client.server_address_and_port, ('127.0.0.1', us_final_node.port)) # connection to KP jurisdiction self.daemon.conf.jurisdiction = "KP" await self.daemon.jsonrpc_wallet_reconnect() self.assertEqual( self.daemon.ledger.network.client.server_address_and_port, ('127.0.0.1', kp_final_node.port)) kp_final_node.server.session_manager._notify_peer('127.0.0.1:9988') await self.daemon.ledger.network.on_hub.first await asyncio.sleep( 0.5) # wait for above event to be processed by other listeners self.assertEqual( self.daemon.conf.known_hubs.filter(), { (relay_node.hostname, relay_node.port): { "country": "FR" }, (us_final_node.hostname, us_final_node.port): { "country": "US" }, (kp_final_node.hostname, kp_final_node.port): { "country": "KP" }, ('127.0.0.1', 9988): {} })