diff --git a/lbry/extras/daemon/components.py b/lbry/extras/daemon/components.py index 2af376904..55e2c30bd 100644 --- a/lbry/extras/daemon/components.py +++ b/lbry/extras/daemon/components.py @@ -154,6 +154,7 @@ class WalletComponent(Component): log.info("Starting wallet") self.wallet_manager = await WalletManager.from_lbrynet_config(self.conf) await self.wallet_manager.start() + self.wallet_manager.usage_payment_service.analytics_manager = self.component_manager.analytics_manager async def stop(self): await self.wallet_manager.stop() diff --git a/lbry/wallet/manager.py b/lbry/wallet/manager.py index d833884ee..5005f3732 100644 --- a/lbry/wallet/manager.py +++ b/lbry/wallet/manager.py @@ -34,7 +34,7 @@ class WalletManager: self.ledgers = ledgers or {} self.running = False self.config: Optional[Config] = None - self.usage_payment_service: Optional[WalletServerPayer] = None + self.usage_payment_service = WalletServerPayer() @classmethod def from_config(cls, config: dict) -> 'WalletManager': @@ -81,8 +81,7 @@ class WalletManager: await asyncio.gather(*( l.start() for l in self.ledgers.values() )) - self.usage_payment_service = WalletServerPayer(self.ledger, self.default_wallet) - await self.usage_payment_service.start() + await self.usage_payment_service.start(self.ledger, self.default_wallet) async def stop(self): await asyncio.gather(*( diff --git a/lbry/wallet/server/session.py b/lbry/wallet/server/session.py index e8aca5cc9..40e2fc032 100644 --- a/lbry/wallet/server/session.py +++ b/lbry/wallet/server/session.py @@ -800,7 +800,7 @@ class LBRYElectrumX(SessionBase): 'blockchain.transaction.get_merkle': cls.transaction_merkle, 'server.add_peer': cls.add_peer, 'server.banner': cls.banner, - 'server.donation_address': cls.donation_address, + 'server.payment_address': cls.payment_address, 'server.features': cls.server_features_async, 'server.peers.subscribe': cls.peers_subscribe, 'server.version': cls.server_version, diff --git a/lbry/wallet/usage_payment.py b/lbry/wallet/usage_payment.py index 3a4f25fec..d480fd566 100644 --- a/lbry/wallet/usage_payment.py +++ b/lbry/wallet/usage_payment.py @@ -1,8 +1,6 @@ import asyncio import logging -from lbry.wallet import Wallet -from lbry.wallet.ledger import Ledger from lbry.wallet.dewies import lbc_to_dewies from lbry.wallet.transaction import Output, Transaction @@ -10,17 +8,17 @@ log = logging.getLogger(__name__) class WalletServerPayer: - PAYMENT_PERIOD = 24 * 60 * 60 - - def __init__(self, ledger: Ledger, wallet: Wallet): - self.ledger = ledger - self.wallet = wallet + def __init__(self, payment_period=24 * 60 * 60): + self.ledger = None + self.wallet = None self.running = False self.task = None + self.payment_period = payment_period + self.analytics_manager = None async def pay(self): while self.running: - await asyncio.sleep(self.PAYMENT_PERIOD) + await asyncio.sleep(self.payment_period) features = await self.ledger.network.get_server_features() address = features['payment_address'] amount = str(features['daily_fee']) @@ -28,22 +26,29 @@ class WalletServerPayer: continue if not self.ledger.is_valid_address(address): - raise Exception(f"Invalid address: {address}") + log.warning("Invalid address from wallet server: '%s' - skipping payment round.", address) + continue if self.wallet.is_locked: - raise Exception("Cannot spend funds with locked wallet") + log.warning("Cannot spend funds with locked wallet, skipping payment round.") + continue amount = lbc_to_dewies(features['daily_fee']) # check that this is in lbc and not dewies # todo: check that amount is less than our max - tx = await Transaction.create([], - [Output.pay_pubkey_hash(amount, self.ledger.address_to_hash160(address))], - self.wallet.get_accounts_or_all(None), - self.wallet.get_account_or_default(None)) + tx = await Transaction.create( + [], + [Output.pay_pubkey_hash(amount, self.ledger.address_to_hash160(address))], + self.wallet.get_accounts_or_all(None), + self.wallet.get_account_or_default(None) + ) await self.ledger.broadcast(tx) - # await self.analytics_manager.send_credits_sent() fixme: handle that + if self.analytics_manager: + await self.analytics_manager.send_credits_sent() - async def start(self): + async def start(self, ledger, default_wallet): + self.ledger = ledger + self.wallet = default_wallet self.running = True self.task = asyncio.ensure_future(self.pay()) self.task.add_done_callback(lambda _: log.info("Stopping wallet server payments.")) diff --git a/tests/integration/blockchain/test_wallet_server_sessions.py b/tests/integration/blockchain/test_wallet_server_sessions.py index 18c7b9504..460fac052 100644 --- a/tests/integration/blockchain/test_wallet_server_sessions.py +++ b/tests/integration/blockchain/test_wallet_server_sessions.py @@ -54,15 +54,11 @@ class TestSegwitServer(IntegrationTestCase): class TestUsagePayment(CommandTestCase): - LEDGER = lbry.wallet - - def setUp(self) -> None: - WalletServerPayer.PAYMENT_PERIOD = 1 - - def tearDown(self) -> None: - WalletServerPayer.PAYMENT_PERIOD = 24 * 60 * 60 - async def test_single_server_payment(self): + self.manager.usage_payment_service.payment_period = 1 + await self.manager.usage_payment_service.stop() + await self.manager.usage_payment_service.start(self.ledger, self.wallet) + 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, []) @@ -78,7 +74,7 @@ class TestUsagePayment(CommandTestCase): self.assertEqual(features["daily_fee"], "1.1") if len(history) == 0: - await self.on_address_update(address) + await asyncio.wait_for(self.on_address_update(address), timeout=1) _, history = await self.ledger.get_local_status_and_history(address) txid, nout = history[0] tx_details = await self.daemon.jsonrpc_transaction_show(txid)