2020-01-06 05:29:48 +01:00
|
|
|
import asyncio
|
|
|
|
import logging
|
|
|
|
|
2020-02-21 16:05:46 +01:00
|
|
|
from lbry.error import (
|
2022-09-16 18:01:13 +02:00
|
|
|
InsufficientFundsError,
|
2020-02-21 16:05:46 +01:00
|
|
|
ServerPaymentFeeAboveMaxAllowedError,
|
|
|
|
ServerPaymentInvalidAddressError,
|
|
|
|
ServerPaymentWalletLockedError
|
|
|
|
)
|
2020-01-06 05:29:48 +01:00
|
|
|
from lbry.wallet.dewies import lbc_to_dewies
|
2020-02-21 04:04:37 +01:00
|
|
|
from lbry.wallet.stream import StreamController
|
2020-01-06 05:29:48 +01:00
|
|
|
from lbry.wallet.transaction import Output, Transaction
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class WalletServerPayer:
|
2020-02-18 17:50:41 +01:00
|
|
|
def __init__(self, payment_period=24 * 60 * 60, max_fee='1.0', analytics_manager=None):
|
2020-02-12 02:05:49 +01:00
|
|
|
self.ledger = None
|
|
|
|
self.wallet = None
|
2020-01-06 05:29:48 +01:00
|
|
|
self.running = False
|
|
|
|
self.task = None
|
2020-02-12 02:05:49 +01:00
|
|
|
self.payment_period = payment_period
|
2020-02-18 17:50:41 +01:00
|
|
|
self.analytics_manager = analytics_manager
|
2020-02-17 17:35:46 +01:00
|
|
|
self.max_fee = max_fee
|
2020-02-21 04:04:37 +01:00
|
|
|
self._on_payment_controller = StreamController()
|
|
|
|
self.on_payment = self._on_payment_controller.stream
|
2022-09-16 18:01:13 +02:00
|
|
|
self.on_payment.listen(None, on_error=lambda e: log.warning(e.args[0]))
|
2020-01-06 05:29:48 +01:00
|
|
|
|
|
|
|
async def pay(self):
|
2022-09-15 23:53:40 +02:00
|
|
|
while self.running:
|
|
|
|
try:
|
|
|
|
await self._pay()
|
2022-09-16 18:01:13 +02:00
|
|
|
except (asyncio.TimeoutError, ConnectionError):
|
|
|
|
if not self.running:
|
|
|
|
break
|
|
|
|
delay = max(self.payment_period / 24, 10)
|
|
|
|
log.warning("Payement failed. Will retry after %g seconds.", delay)
|
|
|
|
asyncio.sleep(delay)
|
2022-09-19 15:44:13 +02:00
|
|
|
except BaseException as e:
|
|
|
|
if not isinstance(e, asyncio.CancelledError):
|
|
|
|
log.exception("Unexpected exception. Payment task exiting early.")
|
|
|
|
self.running = False
|
2022-09-15 23:53:40 +02:00
|
|
|
raise
|
|
|
|
|
|
|
|
async def _pay(self):
|
2020-01-06 05:29:48 +01:00
|
|
|
while self.running:
|
2020-02-12 02:05:49 +01:00
|
|
|
await asyncio.sleep(self.payment_period)
|
2022-09-16 18:01:13 +02:00
|
|
|
features = await self.ledger.network.get_server_features()
|
|
|
|
log.debug("pay loop: received server features: %s", str(features))
|
2020-01-06 05:29:48 +01:00
|
|
|
address = features['payment_address']
|
2020-01-06 09:07:37 +01:00
|
|
|
amount = str(features['daily_fee'])
|
|
|
|
if not address or not amount:
|
2022-09-16 18:01:13 +02:00
|
|
|
log.debug("pay loop: no address or no amount")
|
2020-01-06 09:07:37 +01:00
|
|
|
continue
|
2020-01-06 05:29:48 +01:00
|
|
|
|
2021-06-02 15:51:08 +02:00
|
|
|
if not self.ledger.is_pubkey_address(address):
|
2022-09-16 18:01:13 +02:00
|
|
|
log.info("pay loop: address not pubkey")
|
2020-02-21 16:05:46 +01:00
|
|
|
self._on_payment_controller.add_error(ServerPaymentInvalidAddressError(address))
|
2020-02-12 02:05:49 +01:00
|
|
|
continue
|
2020-02-21 16:05:46 +01:00
|
|
|
|
2020-01-06 05:29:48 +01:00
|
|
|
if self.wallet.is_locked:
|
2022-09-16 18:01:13 +02:00
|
|
|
log.info("pay loop: wallet is locked")
|
2020-02-21 16:05:46 +01:00
|
|
|
self._on_payment_controller.add_error(ServerPaymentWalletLockedError())
|
2020-02-12 02:05:49 +01:00
|
|
|
continue
|
2020-01-06 05:29:48 +01:00
|
|
|
|
|
|
|
amount = lbc_to_dewies(features['daily_fee']) # check that this is in lbc and not dewies
|
2020-02-17 17:35:46 +01:00
|
|
|
limit = lbc_to_dewies(self.max_fee)
|
|
|
|
if amount > limit:
|
2022-09-16 18:01:13 +02:00
|
|
|
log.info("pay loop: amount (%d) > limit (%d)", amount, limit)
|
2020-02-21 04:04:37 +01:00
|
|
|
self._on_payment_controller.add_error(
|
2020-02-21 16:05:46 +01:00
|
|
|
ServerPaymentFeeAboveMaxAllowedError(features['daily_fee'], self.max_fee)
|
2020-02-17 17:35:46 +01:00
|
|
|
)
|
|
|
|
continue
|
2020-01-06 05:29:48 +01:00
|
|
|
|
2022-09-16 18:01:13 +02:00
|
|
|
try:
|
|
|
|
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)
|
|
|
|
)
|
2022-09-19 15:44:13 +02:00
|
|
|
except InsufficientFundsError:
|
|
|
|
self._on_payment_controller.add_error(InsufficientFundsError())
|
2022-09-16 18:01:13 +02:00
|
|
|
continue
|
2020-01-06 05:29:48 +01:00
|
|
|
|
2022-09-16 16:40:52 +02:00
|
|
|
await self.ledger.broadcast_or_release(tx, blocking=True)
|
2020-02-12 02:05:49 +01:00
|
|
|
if self.analytics_manager:
|
|
|
|
await self.analytics_manager.send_credits_sent()
|
2020-02-21 04:04:37 +01:00
|
|
|
self._on_payment_controller.add(tx)
|
2020-01-06 05:29:48 +01:00
|
|
|
|
2020-02-18 17:50:41 +01:00
|
|
|
async def start(self, ledger=None, wallet=None):
|
2020-02-21 18:15:17 +01:00
|
|
|
if lbc_to_dewies(self.max_fee) < 1:
|
|
|
|
return
|
2020-02-12 02:05:49 +01:00
|
|
|
self.ledger = ledger
|
2020-02-18 17:50:41 +01:00
|
|
|
self.wallet = wallet
|
2020-01-06 05:29:48 +01:00
|
|
|
self.running = True
|
|
|
|
self.task = asyncio.ensure_future(self.pay())
|
2022-09-15 23:53:40 +02:00
|
|
|
self.task.add_done_callback(self._done_callback)
|
|
|
|
|
|
|
|
def _done_callback(self, f):
|
|
|
|
if f.cancelled():
|
|
|
|
reason = "Cancelled"
|
2022-09-20 15:10:38 +02:00
|
|
|
elif f.exception():
|
|
|
|
reason = f'Exception: {f.exception()}'
|
2022-09-15 23:53:40 +02:00
|
|
|
elif not self.running:
|
|
|
|
reason = "Stopped"
|
|
|
|
else:
|
2022-09-20 15:10:38 +02:00
|
|
|
reason = ""
|
2022-09-20 15:20:16 +02:00
|
|
|
log.info("Stopping wallet server payments. %s", reason)
|
2020-01-06 05:29:48 +01:00
|
|
|
|
|
|
|
async def stop(self):
|
|
|
|
if self.running:
|
|
|
|
self.running = False
|
|
|
|
self.task.cancel()
|