cache server features

This commit is contained in:
Jack Robison 2021-01-16 16:27:42 -05:00
parent a4ec430ac0
commit 36fd1b91ae
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
3 changed files with 24 additions and 19 deletions

View file

@ -864,6 +864,7 @@ class LBRYElectrumX(SessionBase):
max_errors = math.inf # don't disconnect people for errors! let them happen... max_errors = math.inf # don't disconnect people for errors! let them happen...
session_mgr: LBRYSessionManager session_mgr: LBRYSessionManager
version = lbry.__version__ version = lbry.__version__
cached_server_features = {}
@classmethod @classmethod
def initialize_request_handlers(cls): def initialize_request_handlers(cls):
@ -910,6 +911,8 @@ class LBRYElectrumX(SessionBase):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
if not LBRYElectrumX.request_handlers: if not LBRYElectrumX.request_handlers:
LBRYElectrumX.initialize_request_handlers() LBRYElectrumX.initialize_request_handlers()
if not LBRYElectrumX.cached_server_features:
LBRYElectrumX.set_server_features(self.env)
self.subscribe_headers = False self.subscribe_headers = False
self.subscribe_headers_raw = False self.subscribe_headers_raw = False
self.connection.max_response_size = self.env.max_send self.connection.max_response_size = self.env.max_send
@ -927,10 +930,10 @@ class LBRYElectrumX(SessionBase):
for ver in (cls.PROTOCOL_MIN, cls.PROTOCOL_MAX)] for ver in (cls.PROTOCOL_MIN, cls.PROTOCOL_MAX)]
@classmethod @classmethod
def server_features(cls, env): def set_server_features(cls, env):
"""Return the server features dictionary.""" """Return the server features dictionary."""
min_str, max_str = cls.protocol_min_max_strings() min_str, max_str = cls.protocol_min_max_strings()
return { cls.cached_server_features.update({
'hosts': env.hosts_dict(), 'hosts': env.hosts_dict(),
'pruning': None, 'pruning': None,
'server_version': cls.version, 'server_version': cls.version,
@ -943,10 +946,10 @@ class LBRYElectrumX(SessionBase):
'daily_fee': env.daily_fee, 'daily_fee': env.daily_fee,
'hash_function': 'sha256', 'hash_function': 'sha256',
'trending_algorithm': env.trending_algorithms[0] 'trending_algorithm': env.trending_algorithms[0]
} })
async def server_features_async(self): async def server_features_async(self):
return self.server_features(self.env) return self.cached_server_features
@classmethod @classmethod
def server_version_args(cls): def server_version_args(cls):

View file

@ -32,18 +32,17 @@ class NetworkTests(IntegrationTestCase):
'server_version': lbry.__version__, 'server_version': lbry.__version__,
'trending_algorithm': 'zscore', 'trending_algorithm': 'zscore',
}, await self.ledger.network.get_server_features()) }, await self.ledger.network.get_server_features())
await self.conductor.spv_node.stop() # await self.conductor.spv_node.stop()
payment_address, donation_address = await self.account.get_addresses(limit=2) payment_address, donation_address = await self.account.get_addresses(limit=2)
await self.conductor.spv_node.start( self.conductor.spv_node.server.env.payment_address = payment_address
self.conductor.blockchain_node, self.conductor.spv_node.server.env.donation_address = donation_address
extraconf={ self.conductor.spv_node.server.env.description = 'Fastest server in the west.'
'DESCRIPTION': 'Fastest server in the west.', self.conductor.spv_node.server.env.daily_fee = '42'
'PAYMENT_ADDRESS': payment_address,
'DONATION_ADDRESS': donation_address, from lbry.wallet.server.session import LBRYElectrumX
'DAILY_FEE': '42' LBRYElectrumX.set_server_features(self.conductor.spv_node.server.env)
}
) # await self.ledger.network.on_connected.first
await self.ledger.network.on_connected.first
self.assertDictEqual({ self.assertDictEqual({
'genesis_hash': self.conductor.spv_node.coin_class.GENESIS_HASH, 'genesis_hash': self.conductor.spv_node.coin_class.GENESIS_HASH,
'hash_function': 'sha256', 'hash_function': 'sha256',

View file

@ -4,6 +4,7 @@ import lbry
import lbry.wallet import lbry.wallet
from lbry.error import ServerPaymentFeeAboveMaxAllowedError from lbry.error import ServerPaymentFeeAboveMaxAllowedError
from lbry.wallet.network import ClientSession from lbry.wallet.network import ClientSession
from lbry.wallet.server.session import LBRYElectrumX
from lbry.testcase import IntegrationTestCase, CommandTestCase from lbry.testcase import IntegrationTestCase, CommandTestCase
from lbry.wallet.orchstr8.node import SPVNode from lbry.wallet.orchstr8.node import SPVNode
@ -69,20 +70,22 @@ class TestUsagePayment(CommandTestCase):
self.daemon.jsonrpc_settings_set('lbryum_servers', [f"{node.hostname}:{node.port}"]) self.daemon.jsonrpc_settings_set('lbryum_servers', [f"{node.hostname}:{node.port}"])
await self.daemon.jsonrpc_wallet_reconnect() await self.daemon.jsonrpc_wallet_reconnect()
LBRYElectrumX.set_server_features(node.server.env)
features = await self.ledger.network.get_server_features() features = await self.ledger.network.get_server_features()
self.assertEqual(features["payment_address"], address) self.assertEqual(features["payment_address"], address)
self.assertEqual(features["daily_fee"], "1.1") self.assertEqual(features["daily_fee"], "1.1")
with self.assertRaises(ServerPaymentFeeAboveMaxAllowedError): with self.assertRaises(ServerPaymentFeeAboveMaxAllowedError):
await asyncio.wait_for(wallet_pay_service.on_payment.first, timeout=8) await asyncio.wait_for(wallet_pay_service.on_payment.first, timeout=8)
await node.stop(False) node.server.env.daily_fee = "1.0"
await node.start(self.blockchain, extraconf={"PAYMENT_ADDRESS": address, "DAILY_FEE": "1.0"}) node.server.env.payment_address = address
self.daemon.jsonrpc_settings_set('lbryum_servers', [f"{node.hostname}:{node.port}"]) LBRYElectrumX.set_server_features(node.server.env)
# self.daemon.jsonrpc_settings_set('lbryum_servers', [f"{node.hostname}:{node.port}"])
await self.daemon.jsonrpc_wallet_reconnect() await self.daemon.jsonrpc_wallet_reconnect()
features = await self.ledger.network.get_server_features() features = await self.ledger.network.get_server_features()
self.assertEqual(features["payment_address"], address) self.assertEqual(features["payment_address"], address)
self.assertEqual(features["daily_fee"], "1.0") self.assertEqual(features["daily_fee"], "1.0")
tx = await asyncio.wait_for(wallet_pay_service.on_payment.first, timeout=8) tx = await asyncio.wait_for(wallet_pay_service.on_payment.first, timeout=30)
self.assertIsNotNone(await self.blockchain.get_raw_transaction(tx.id)) # verify its broadcasted 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].amount, 100000000)
self.assertEqual(tx.outputs[0].get_address(self.ledger), address) self.assertEqual(tx.outputs[0].get_address(self.ledger), address)