2019-02-12 22:50:43 +01:00
|
|
|
import asyncio
|
2019-12-31 20:52:57 +01:00
|
|
|
|
|
|
|
import lbry
|
2019-06-03 21:47:08 +02:00
|
|
|
from unittest.mock import Mock
|
2018-10-17 19:32:45 +02:00
|
|
|
|
2022-03-04 16:37:01 +01:00
|
|
|
from lbry_schema.coin import LBCRegTest
|
|
|
|
|
2020-01-03 08:40:37 +01:00
|
|
|
from lbry.wallet.network import Network
|
2021-03-12 17:00:30 +01:00
|
|
|
from lbry.wallet.orchstr8 import Conductor
|
2019-12-31 20:52:57 +01:00
|
|
|
from lbry.wallet.orchstr8.node import SPVNode
|
|
|
|
from lbry.wallet.rpc import RPCSession
|
2022-03-05 22:40:32 +01:00
|
|
|
from lbry.wallet.udp import StatusServer
|
2019-12-31 21:30:13 +01:00
|
|
|
from lbry.testcase import IntegrationTestCase, AsyncioTestCase
|
2021-03-13 06:31:03 +01:00
|
|
|
from lbry.conf import Config
|
2018-10-11 05:07:38 +02:00
|
|
|
|
|
|
|
|
2019-06-26 08:41:35 +02:00
|
|
|
class NetworkTests(IntegrationTestCase):
|
|
|
|
|
|
|
|
async def test_remote_height_updated_automagically(self):
|
|
|
|
initial_height = self.ledger.network.remote_height
|
2022-01-19 16:10:54 +01:00
|
|
|
await self.blockchain.generate(1)
|
2019-06-26 08:41:35 +02:00
|
|
|
await self.ledger.network.on_header.first
|
|
|
|
self.assertEqual(self.ledger.network.remote_height, initial_height + 1)
|
|
|
|
|
2019-09-17 06:29:00 +02:00
|
|
|
async def test_server_features(self):
|
2019-10-08 17:01:38 +02:00
|
|
|
self.assertDictEqual({
|
2022-03-04 16:37:01 +01:00
|
|
|
'genesis_hash': LBCRegTest.GENESIS_HASH,
|
2019-09-17 21:30:27 +02:00
|
|
|
'hash_function': 'sha256',
|
|
|
|
'hosts': {},
|
2021-06-22 19:32:41 +02:00
|
|
|
'protocol_max': '0.199.0',
|
2020-02-03 19:39:36 +01:00
|
|
|
'protocol_min': '0.54.0',
|
2019-09-17 21:30:27 +02:00
|
|
|
'pruning': None,
|
|
|
|
'description': '',
|
|
|
|
'payment_address': '',
|
2020-02-12 02:19:13 +01:00
|
|
|
'donation_address': '',
|
2020-01-06 05:53:51 +01:00
|
|
|
'daily_fee': '0',
|
2020-03-04 02:26:07 +01:00
|
|
|
'server_version': lbry.__version__,
|
2021-11-16 01:13:07 +01:00
|
|
|
'trending_algorithm': 'fast_ar',
|
2020-03-04 02:26:07 +01:00
|
|
|
}, await self.ledger.network.get_server_features())
|
2021-01-16 22:27:42 +01:00
|
|
|
# await self.conductor.spv_node.stop()
|
2020-02-12 02:19:13 +01:00
|
|
|
payment_address, donation_address = await self.account.get_addresses(limit=2)
|
2022-02-20 23:58:08 +01:00
|
|
|
|
|
|
|
original_address = self.conductor.spv_node.server.env.payment_address
|
|
|
|
original_donation_address = self.conductor.spv_node.server.env.donation_address
|
|
|
|
original_description = self.conductor.spv_node.server.env.description
|
|
|
|
original_daily_fee = self.conductor.spv_node.server.env.daily_fee
|
|
|
|
|
2021-01-16 22:27:42 +01:00
|
|
|
self.conductor.spv_node.server.env.payment_address = payment_address
|
|
|
|
self.conductor.spv_node.server.env.donation_address = donation_address
|
|
|
|
self.conductor.spv_node.server.env.description = 'Fastest server in the west.'
|
|
|
|
self.conductor.spv_node.server.env.daily_fee = '42'
|
|
|
|
|
2022-03-05 22:41:46 +01:00
|
|
|
from scribe.server.session import LBRYElectrumX
|
2021-01-16 22:27:42 +01:00
|
|
|
LBRYElectrumX.set_server_features(self.conductor.spv_node.server.env)
|
|
|
|
|
|
|
|
# await self.ledger.network.on_connected.first
|
2019-10-08 17:01:38 +02:00
|
|
|
self.assertDictEqual({
|
2022-03-04 16:37:01 +01:00
|
|
|
'genesis_hash': LBCRegTest.GENESIS_HASH,
|
2019-09-17 21:30:27 +02:00
|
|
|
'hash_function': 'sha256',
|
|
|
|
'hosts': {},
|
2021-06-22 19:32:41 +02:00
|
|
|
'protocol_max': '0.199.0',
|
2020-02-03 19:39:36 +01:00
|
|
|
'protocol_min': '0.54.0',
|
2019-09-17 21:30:27 +02:00
|
|
|
'pruning': None,
|
|
|
|
'description': 'Fastest server in the west.',
|
2020-02-12 02:19:13 +01:00
|
|
|
'payment_address': payment_address,
|
|
|
|
'donation_address': donation_address,
|
2020-01-06 05:53:51 +01:00
|
|
|
'daily_fee': '42',
|
2020-03-04 02:26:07 +01:00
|
|
|
'server_version': lbry.__version__,
|
2021-11-16 01:13:07 +01:00
|
|
|
'trending_algorithm': 'fast_ar',
|
2020-03-04 02:26:07 +01:00
|
|
|
}, await self.ledger.network.get_server_features())
|
2019-09-17 06:29:00 +02:00
|
|
|
|
2022-02-20 23:58:08 +01:00
|
|
|
# cleanup the changes since the attributes are set on the class
|
|
|
|
self.conductor.spv_node.server.env.payment_address = original_address
|
|
|
|
self.conductor.spv_node.server.env.donation_address = original_donation_address
|
|
|
|
self.conductor.spv_node.server.env.description = original_description
|
|
|
|
self.conductor.spv_node.server.env.daily_fee = original_daily_fee
|
|
|
|
LBRYElectrumX.set_server_features(self.conductor.spv_node.server.env)
|
|
|
|
|
2019-06-26 08:41:35 +02:00
|
|
|
|
2020-03-30 23:02:08 +02:00
|
|
|
class ReconnectTests(IntegrationTestCase):
|
2018-10-11 05:07:38 +02:00
|
|
|
|
2019-08-29 06:12:49 +02:00
|
|
|
async def test_multiple_servers(self):
|
|
|
|
# we have a secondary node that connects later, so
|
2022-03-04 16:37:01 +01:00
|
|
|
node2 = SPVNode(node_number=2)
|
2019-08-29 06:12:49 +02:00
|
|
|
await node2.start(self.blockchain)
|
2021-01-21 22:20:01 +01:00
|
|
|
|
2021-06-14 19:54:32 +02:00
|
|
|
self.ledger.network.config['explicit_servers'].append((node2.hostname, node2.port))
|
|
|
|
self.ledger.network.config['explicit_servers'].reverse()
|
2021-01-21 22:20:01 +01:00
|
|
|
self.assertEqual(50002, self.ledger.network.client.server[1])
|
|
|
|
await self.ledger.stop()
|
|
|
|
await self.ledger.start()
|
|
|
|
|
2019-08-29 06:12:49 +02:00
|
|
|
self.assertTrue(self.ledger.network.is_connected)
|
2021-01-21 22:20:01 +01:00
|
|
|
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])
|
2019-08-29 06:12:49 +02:00
|
|
|
|
2019-10-09 02:45:47 +02:00
|
|
|
async def test_direct_sync(self):
|
|
|
|
await self.ledger.stop()
|
2019-10-09 03:17:09 +02:00
|
|
|
initial_height = self.ledger.local_height_including_downloaded_height
|
2022-01-19 16:10:54 +01:00
|
|
|
await self.blockchain.generate(100)
|
2022-01-12 17:59:44 +01:00
|
|
|
while self.conductor.spv_node.server.session_manager.notified_height < initial_height + 99: # off by 1
|
2019-10-09 02:45:47 +02:00
|
|
|
await asyncio.sleep(0.1)
|
2019-10-09 03:17:09 +02:00
|
|
|
self.assertEqual(initial_height, self.ledger.local_height_including_downloaded_height)
|
2020-03-21 08:32:03 +01:00
|
|
|
await self.ledger.headers.open()
|
|
|
|
await self.ledger.network.start()
|
|
|
|
await self.ledger.network.on_connected.first
|
|
|
|
await self.ledger.initial_headers_sync()
|
|
|
|
self.assertEqual(initial_height + 100, self.ledger.local_height_including_downloaded_height)
|
2019-10-09 02:45:47 +02:00
|
|
|
|
2018-10-11 05:07:38 +02:00
|
|
|
async def test_connection_drop_still_receives_events_after_reconnected(self):
|
2018-10-15 06:45:21 +02:00
|
|
|
address1 = await self.account.receiving.get_or_create_usable_address()
|
2019-08-06 07:17:39 +02:00
|
|
|
# disconnect and send a new tx, should reconnect and get it
|
2021-01-21 22:20:01 +01:00
|
|
|
self.ledger.network.client.transport.close()
|
2019-08-06 07:17:39 +02:00
|
|
|
self.assertFalse(self.ledger.network.is_connected)
|
2021-01-21 22:20:01 +01:00
|
|
|
await self.ledger.resolve([], 'derp')
|
2021-12-17 04:50:03 +01:00
|
|
|
sendtxid = await self.send_to_address_and_wait(address1, 1.1337, 1)
|
2019-08-12 23:16:53 +02:00
|
|
|
self.assertLess(self.ledger.network.client.response_time, 1) # response time properly set lower, we are fine
|
2018-10-11 05:07:38 +02:00
|
|
|
|
2018-11-19 05:17:39 +01:00
|
|
|
await self.assertBalance(self.account, '1.1337')
|
2018-10-11 05:07:38 +02:00
|
|
|
# is it real? are we rich!? let me see this tx...
|
|
|
|
d = self.ledger.network.get_transaction(sendtxid)
|
|
|
|
# what's that smoke on my ethernet cable? oh no!
|
2019-08-29 06:12:49 +02:00
|
|
|
master_client = self.ledger.network.client
|
2018-10-17 19:32:45 +02:00
|
|
|
self.ledger.network.client.connection_lost(Exception())
|
2019-08-12 23:16:53 +02:00
|
|
|
with self.assertRaises(asyncio.TimeoutError):
|
2019-08-31 11:37:17 +02:00
|
|
|
await d
|
2019-08-29 06:12:49 +02:00
|
|
|
self.assertIsNone(master_client.response_time) # response time unknown as it failed
|
2018-10-11 05:07:38 +02:00
|
|
|
# rich but offline? no way, no water, let's retry
|
|
|
|
with self.assertRaisesRegex(ConnectionError, 'connection is not available'):
|
2018-10-15 06:45:21 +02:00
|
|
|
await self.ledger.network.get_transaction(sendtxid)
|
2018-10-11 05:07:38 +02:00
|
|
|
# * goes to pick some water outside... * time passes by and another donation comes in
|
|
|
|
sendtxid = await self.blockchain.send_to_address(address1, 42)
|
2022-01-19 16:10:54 +01:00
|
|
|
await self.blockchain.generate(1)
|
2019-10-02 20:04:30 +02:00
|
|
|
# (this is just so the test doesn't hang forever if it doesn't reconnect)
|
2019-08-12 18:32:20 +02:00
|
|
|
if not self.ledger.network.is_connected:
|
2021-01-21 22:20:01 +01:00
|
|
|
await asyncio.wait_for(self.ledger.network.on_connected.first, timeout=10.0)
|
2018-10-11 05:07:38 +02:00
|
|
|
# omg, the burned cable still works! torba is fire proof!
|
2018-10-15 06:45:21 +02:00
|
|
|
await self.ledger.network.get_transaction(sendtxid)
|
2019-02-12 22:50:43 +01:00
|
|
|
|
|
|
|
async def test_timeout_then_reconnect(self):
|
2019-08-06 07:17:39 +02:00
|
|
|
# tests that it connects back after some failed attempts
|
2019-06-04 07:34:58 +02:00
|
|
|
await self.conductor.spv_node.stop()
|
|
|
|
self.assertFalse(self.ledger.network.is_connected)
|
2019-08-06 07:17:39 +02:00
|
|
|
await asyncio.sleep(0.2) # let it retry and fail once
|
2021-11-09 01:19:36 +01:00
|
|
|
await self.conductor.spv_node.start(self.conductor.lbcwallet_node)
|
2019-06-04 07:34:58 +02:00
|
|
|
await self.ledger.network.on_connected.first
|
2019-06-03 10:21:57 +02:00
|
|
|
self.assertTrue(self.ledger.network.is_connected)
|
|
|
|
|
2021-03-13 06:49:33 +01:00
|
|
|
async def test_timeout_and_concurrency_propagated_from_config(self):
|
2021-03-13 06:31:03 +01:00
|
|
|
conf = Config()
|
|
|
|
self.assertEqual(self.ledger.network.client.timeout, 30)
|
2021-03-13 06:49:33 +01:00
|
|
|
self.assertEqual(self.ledger.network.client.concurrency, 32)
|
2021-03-13 06:31:03 +01:00
|
|
|
conf.hub_timeout = 123.0
|
2021-03-13 06:49:33 +01:00
|
|
|
conf.concurrent_hub_requests = 42
|
|
|
|
conf.known_hubs = self.ledger.config['known_hubs']
|
|
|
|
conf.wallet_dir = self.ledger.config['data_path']
|
2021-03-13 06:31:03 +01:00
|
|
|
self.manager.config = conf
|
|
|
|
await self.manager.reset()
|
|
|
|
self.assertEqual(self.ledger.network.client.timeout, 123)
|
2021-03-13 06:49:33 +01:00
|
|
|
self.assertEqual(self.ledger.network.client.concurrency, 42)
|
2021-03-13 06:31:03 +01:00
|
|
|
|
2021-01-21 22:20:01 +01:00
|
|
|
# async def test_online_but_still_unavailable(self):
|
|
|
|
# # Edge case. See issue #2445 for context
|
|
|
|
# self.assertIsNotNone(self.ledger.network.session_pool.fastest_session)
|
|
|
|
# for session in self.ledger.network.session_pool.sessions:
|
|
|
|
# session.response_time = None
|
|
|
|
# self.assertIsNone(self.ledger.network.session_pool.fastest_session)
|
2019-09-24 08:23:09 +02:00
|
|
|
|
2019-06-03 21:47:08 +02:00
|
|
|
|
2021-03-12 17:00:30 +01:00
|
|
|
class UDPServerFailDiscoveryTest(AsyncioTestCase):
|
|
|
|
async def test_wallet_connects_despite_lack_of_udp(self):
|
|
|
|
conductor = Conductor()
|
|
|
|
conductor.spv_node.udp_port = '0'
|
2021-11-09 01:19:36 +01:00
|
|
|
await conductor.start_lbcd()
|
|
|
|
self.addCleanup(conductor.stop_lbcd)
|
|
|
|
await conductor.start_lbcwallet()
|
|
|
|
self.addCleanup(conductor.stop_lbcwallet)
|
2021-03-12 17:00:30 +01:00
|
|
|
await conductor.start_spv()
|
|
|
|
self.addCleanup(conductor.stop_spv)
|
2022-01-25 05:17:07 +01:00
|
|
|
self.assertFalse(conductor.spv_node.server.status_server.is_running)
|
2021-03-12 17:00:30 +01:00
|
|
|
await asyncio.wait_for(conductor.start_wallet(), timeout=5)
|
|
|
|
self.addCleanup(conductor.stop_wallet)
|
2021-03-12 18:35:39 +01:00
|
|
|
self.assertTrue(conductor.wallet_node.ledger.network.is_connected)
|
2021-03-12 17:00:30 +01:00
|
|
|
|
|
|
|
|
2019-06-03 21:47:08 +02:00
|
|
|
class ServerPickingTestCase(AsyncioTestCase):
|
2021-05-21 19:12:23 +02:00
|
|
|
async def _make_udp_server(self, port, latency) -> StatusServer:
|
2021-01-21 22:20:01 +01:00
|
|
|
s = StatusServer()
|
2021-07-16 20:43:33 +02:00
|
|
|
await s.start(0, b'\x00' * 32, 'US', '127.0.0.1', port, True)
|
2021-05-21 19:12:23 +02:00
|
|
|
s.set_available()
|
|
|
|
sendto = s._protocol.transport.sendto
|
|
|
|
|
|
|
|
def mock_sendto(data, addr):
|
|
|
|
self.loop.call_later(latency, sendto, data, addr)
|
|
|
|
|
|
|
|
s._protocol.transport.sendto = mock_sendto
|
|
|
|
|
2021-01-21 22:20:01 +01:00
|
|
|
self.addCleanup(s.stop)
|
2021-05-21 19:12:23 +02:00
|
|
|
return s
|
2019-07-20 01:03:18 +02:00
|
|
|
|
|
|
|
async def _make_fake_server(self, latency=1.0, port=1):
|
2019-06-03 21:03:20 +02:00
|
|
|
# local fake server with artificial latency
|
2019-07-20 01:03:18 +02:00
|
|
|
class FakeSession(RPCSession):
|
|
|
|
async def handle_request(self, request):
|
|
|
|
await asyncio.sleep(latency)
|
2020-01-15 18:57:37 +01:00
|
|
|
if request.method == 'server.version':
|
|
|
|
return tuple(request.args)
|
|
|
|
return {'height': 1}
|
2019-07-20 01:03:18 +02:00
|
|
|
server = await self.loop.create_server(lambda: FakeSession(), host='127.0.0.1', port=port)
|
|
|
|
self.addCleanup(server.close)
|
2021-05-21 19:12:23 +02:00
|
|
|
await self._make_udp_server(port, latency)
|
2019-07-20 01:03:18 +02:00
|
|
|
return '127.0.0.1', port
|
2019-07-13 00:54:04 +02:00
|
|
|
|
2019-07-20 01:03:18 +02:00
|
|
|
async def _make_bad_server(self, port=42420):
|
|
|
|
async def echo(reader, writer):
|
|
|
|
while True:
|
|
|
|
writer.write(await reader.read())
|
2021-05-21 19:12:23 +02:00
|
|
|
|
2019-07-20 01:03:18 +02:00
|
|
|
server = await asyncio.start_server(echo, host='127.0.0.1', port=port)
|
2019-06-03 21:03:20 +02:00
|
|
|
self.addCleanup(server.close)
|
2021-05-21 19:12:23 +02:00
|
|
|
await self._make_udp_server(port, 0)
|
2019-07-13 00:54:04 +02:00
|
|
|
return '127.0.0.1', port
|
2019-06-03 21:03:20 +02:00
|
|
|
|
2021-05-21 19:12:23 +02:00
|
|
|
async def test_pick_fastest(self):
|
2019-06-03 21:47:08 +02:00
|
|
|
ledger = Mock(config={
|
|
|
|
'default_servers': [
|
2019-07-20 01:03:18 +02:00
|
|
|
# fast but unhealthy, should be discarded
|
2021-05-21 19:12:23 +02:00
|
|
|
# await self._make_bad_server(),
|
2019-07-20 01:03:18 +02:00
|
|
|
('localhost', 1),
|
|
|
|
('example.that.doesnt.resolve', 9000),
|
2019-08-06 07:17:39 +02:00
|
|
|
await self._make_fake_server(latency=1.0, port=1340),
|
|
|
|
await self._make_fake_server(latency=0.1, port=1337),
|
|
|
|
await self._make_fake_server(latency=0.4, port=1339),
|
2019-06-03 21:47:08 +02:00
|
|
|
],
|
2019-07-20 01:03:18 +02:00
|
|
|
'connect_timeout': 3
|
2019-06-03 21:47:08 +02:00
|
|
|
})
|
2019-06-03 21:03:20 +02:00
|
|
|
|
2020-01-03 08:40:37 +01:00
|
|
|
network = Network(ledger)
|
2019-06-03 21:10:41 +02:00
|
|
|
self.addCleanup(network.stop)
|
2021-01-21 22:20:01 +01:00
|
|
|
await network.start()
|
|
|
|
await asyncio.wait_for(network.on_connected.first, timeout=10)
|
2019-06-03 21:03:20 +02:00
|
|
|
self.assertTrue(network.is_connected)
|
2019-10-08 17:01:38 +02:00
|
|
|
self.assertTupleEqual(network.client.server, ('127.0.0.1', 1337))
|
2021-05-21 19:12:23 +02:00
|
|
|
# self.assertTrue(all([not session.is_closing() for session in network.session_pool.available_sessions]))
|
2019-08-06 07:17:39 +02:00
|
|
|
# ensure we are connected to all of them after a while
|
2021-05-21 19:12:23 +02:00
|
|
|
# await asyncio.sleep(1)
|
|
|
|
# self.assertEqual(len(list(network.session_pool.available_sessions)), 3)
|