fixes from review and add analytics

This commit is contained in:
Victor Shyba 2020-02-11 22:05:49 -03:00
parent ffa5c20c88
commit ad6c6fbe35
5 changed files with 30 additions and 29 deletions

View file

@ -154,6 +154,7 @@ class WalletComponent(Component):
log.info("Starting wallet") log.info("Starting wallet")
self.wallet_manager = await WalletManager.from_lbrynet_config(self.conf) self.wallet_manager = await WalletManager.from_lbrynet_config(self.conf)
await self.wallet_manager.start() await self.wallet_manager.start()
self.wallet_manager.usage_payment_service.analytics_manager = self.component_manager.analytics_manager
async def stop(self): async def stop(self):
await self.wallet_manager.stop() await self.wallet_manager.stop()

View file

@ -34,7 +34,7 @@ class WalletManager:
self.ledgers = ledgers or {} self.ledgers = ledgers or {}
self.running = False self.running = False
self.config: Optional[Config] = None self.config: Optional[Config] = None
self.usage_payment_service: Optional[WalletServerPayer] = None self.usage_payment_service = WalletServerPayer()
@classmethod @classmethod
def from_config(cls, config: dict) -> 'WalletManager': def from_config(cls, config: dict) -> 'WalletManager':
@ -81,8 +81,7 @@ class WalletManager:
await asyncio.gather(*( await asyncio.gather(*(
l.start() for l in self.ledgers.values() l.start() for l in self.ledgers.values()
)) ))
self.usage_payment_service = WalletServerPayer(self.ledger, self.default_wallet) await self.usage_payment_service.start(self.ledger, self.default_wallet)
await self.usage_payment_service.start()
async def stop(self): async def stop(self):
await asyncio.gather(*( await asyncio.gather(*(

View file

@ -800,7 +800,7 @@ class LBRYElectrumX(SessionBase):
'blockchain.transaction.get_merkle': cls.transaction_merkle, 'blockchain.transaction.get_merkle': cls.transaction_merkle,
'server.add_peer': cls.add_peer, 'server.add_peer': cls.add_peer,
'server.banner': cls.banner, 'server.banner': cls.banner,
'server.donation_address': cls.donation_address, 'server.payment_address': cls.payment_address,
'server.features': cls.server_features_async, 'server.features': cls.server_features_async,
'server.peers.subscribe': cls.peers_subscribe, 'server.peers.subscribe': cls.peers_subscribe,
'server.version': cls.server_version, 'server.version': cls.server_version,

View file

@ -1,8 +1,6 @@
import asyncio import asyncio
import logging import logging
from lbry.wallet import Wallet
from lbry.wallet.ledger import Ledger
from lbry.wallet.dewies import lbc_to_dewies from lbry.wallet.dewies import lbc_to_dewies
from lbry.wallet.transaction import Output, Transaction from lbry.wallet.transaction import Output, Transaction
@ -10,17 +8,17 @@ log = logging.getLogger(__name__)
class WalletServerPayer: class WalletServerPayer:
PAYMENT_PERIOD = 24 * 60 * 60 def __init__(self, payment_period=24 * 60 * 60):
self.ledger = None
def __init__(self, ledger: Ledger, wallet: Wallet): self.wallet = None
self.ledger = ledger
self.wallet = wallet
self.running = False self.running = False
self.task = None self.task = None
self.payment_period = payment_period
self.analytics_manager = None
async def pay(self): async def pay(self):
while self.running: while self.running:
await asyncio.sleep(self.PAYMENT_PERIOD) await asyncio.sleep(self.payment_period)
features = await self.ledger.network.get_server_features() features = await self.ledger.network.get_server_features()
address = features['payment_address'] address = features['payment_address']
amount = str(features['daily_fee']) amount = str(features['daily_fee'])
@ -28,22 +26,29 @@ class WalletServerPayer:
continue continue
if not self.ledger.is_valid_address(address): 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: 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 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 # todo: check that amount is less than our max
tx = await Transaction.create([], tx = await Transaction.create(
[Output.pay_pubkey_hash(amount, self.ledger.address_to_hash160(address))], [],
self.wallet.get_accounts_or_all(None), [Output.pay_pubkey_hash(amount, self.ledger.address_to_hash160(address))],
self.wallet.get_account_or_default(None)) self.wallet.get_accounts_or_all(None),
self.wallet.get_account_or_default(None)
)
await self.ledger.broadcast(tx) 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.running = True
self.task = asyncio.ensure_future(self.pay()) self.task = asyncio.ensure_future(self.pay())
self.task.add_done_callback(lambda _: log.info("Stopping wallet server payments.")) self.task.add_done_callback(lambda _: log.info("Stopping wallet server payments."))

View file

@ -54,15 +54,11 @@ class TestSegwitServer(IntegrationTestCase):
class TestUsagePayment(CommandTestCase): 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): 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] address = (await self.account.receiving.get_addresses(limit=1, only_usable=True))[0]
_, history = await self.ledger.get_local_status_and_history(address) _, history = await self.ledger.get_local_status_and_history(address)
self.assertEqual(history, []) self.assertEqual(history, [])
@ -78,7 +74,7 @@ class TestUsagePayment(CommandTestCase):
self.assertEqual(features["daily_fee"], "1.1") self.assertEqual(features["daily_fee"], "1.1")
if len(history) == 0: 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) _, history = await self.ledger.get_local_status_and_history(address)
txid, nout = history[0] txid, nout = history[0]
tx_details = await self.daemon.jsonrpc_transaction_show(txid) tx_details = await self.daemon.jsonrpc_transaction_show(txid)