lbry-sdk/tests/integration/blockchain/test_wallet_server_sessions.py

102 lines
4.6 KiB
Python
Raw Normal View History

import asyncio
import lbry
2019-07-23 18:45:10 +02:00
import lbry.wallet
2020-02-21 16:05:46 +01:00
from lbry.error import ServerPaymentFeeAboveMaxAllowedError
2020-01-03 08:40:37 +01:00
from lbry.wallet.network import ClientSession
from lbry.testcase import IntegrationTestCase, CommandTestCase
2020-01-06 05:29:48 +01:00
from lbry.wallet.orchstr8.node import SPVNode
2019-11-08 16:52:39 +01:00
class TestSessions(IntegrationTestCase):
"""
2019-07-16 11:23:44 +02:00
Tests that server cleans up stale connections after session timeout and client times out too.
"""
LEDGER = lbry.wallet
async def test_session_bloat_from_socket_timeout(self):
2019-07-16 11:23:44 +02:00
await self.conductor.stop_spv()
2019-07-22 04:26:49 +02:00
await self.ledger.stop()
2019-07-16 11:23:44 +02:00
self.conductor.spv_node.session_timeout = 1
await self.conductor.start_spv()
2019-09-03 16:28:30 +02:00
session = ClientSession(
network=None, server=(self.conductor.spv_node.hostname, self.conductor.spv_node.port), timeout=0.2
)
2019-07-16 11:23:44 +02:00
await session.create_connection()
await session.send_request('server.banner', ())
self.assertEqual(len(self.conductor.spv_node.server.session_mgr.sessions), 1)
self.assertFalse(session.is_closing())
await asyncio.sleep(1.1)
with self.assertRaises(asyncio.TimeoutError):
await session.send_request('server.banner', ())
self.assertTrue(session.is_closing())
self.assertEqual(len(self.conductor.spv_node.server.session_mgr.sessions), 0)
2019-07-23 18:45:10 +02:00
2019-11-08 16:52:39 +01:00
async def test_proper_version(self):
info = await self.ledger.network.get_server_features()
self.assertEqual(lbry.__version__, info['server_version'])
2019-11-08 16:52:39 +01:00
2019-11-18 16:19:16 +01:00
async def test_client_errors(self):
# Goal is ensuring thsoe are raised and not trapped accidentally
with self.assertRaisesRegex(Exception, 'not a valid address'):
await self.ledger.network.get_history('of the world')
with self.assertRaisesRegex(Exception, 'rejected by network rules.*TX decode failed'):
await self.ledger.network.broadcast('13370042004200')
2019-07-23 18:45:10 +02:00
class TestSegwitServer(IntegrationTestCase):
LEDGER = lbry.wallet
ENABLE_SEGWIT = True
async def test_at_least_it_starts(self):
await asyncio.wait_for(self.ledger.network.get_headers(0, 1), 1.0)
2020-01-06 05:29:48 +01:00
class TestUsagePayment(CommandTestCase):
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)
2020-02-12 02:05:49 +01:00
2020-01-06 05:29:48 +01:00
address = (await self.account.receiving.get_addresses(limit=1, only_usable=True))[0]
_, history = await self.ledger.get_local_status_and_history(address)
self.assertEqual(history, [])
2020-01-06 05:29:48 +01:00
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")
2020-02-21 16:05:46 +01:00
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()
2020-01-06 05:29:48 +01:00
features = await self.ledger.network.get_server_features()
self.assertEqual(features["payment_address"], address)
self.assertEqual(features["daily_fee"], "1.0")
await asyncio.wait([
wallet_pay_service.on_payment.first,
self.on_address_update(address)
], timeout=3)
_, history = await self.ledger.get_local_status_and_history(address)
txid, nout = history[0]
tx_details = await self.daemon.jsonrpc_transaction_show(txid)
self.assertEqual(tx_details.outputs[nout].amount, 100000000)
self.assertEqual(tx_details.outputs[nout].get_address(self.ledger), address)