pay wallet server test and initial prototype

This commit is contained in:
Victor Shyba 2020-01-06 05:07:37 -03:00
parent d317a4042c
commit 15a2f048ac
3 changed files with 35 additions and 28 deletions

View file

@ -15,6 +15,7 @@ from .account import Account
from .ledger import Ledger, LedgerRegistry from .ledger import Ledger, LedgerRegistry
from .transaction import Transaction, Output from .transaction import Transaction, Output
from .database import Database from .database import Database
from .usage_payment import WalletServerPayer
from .wallet import Wallet, WalletStorage, ENCRYPT_ON_DISK from .wallet import Wallet, WalletStorage, ENCRYPT_ON_DISK
from .rpc.jsonrpc import CodeMessageError from .rpc.jsonrpc import CodeMessageError
@ -33,6 +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[WalletManager] = None
@classmethod @classmethod
def from_config(cls, config: dict) -> 'WalletManager': def from_config(cls, config: dict) -> 'WalletManager':
@ -79,11 +81,14 @@ 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()
async def stop(self): async def stop(self):
await asyncio.gather(*( await asyncio.gather(*(
l.stop() for l in self.ledgers.values() l.stop() for l in self.ledgers.values()
)) ))
await self.usage_payment_service.stop()
self.running = False self.running = False
def get_wallet_or_default(self, wallet_id: Optional[str]) -> Wallet: def get_wallet_or_default(self, wallet_id: Optional[str]) -> Wallet:

View file

@ -10,6 +10,7 @@ log = logging.getLogger(__name__)
class WalletServerPayer: class WalletServerPayer:
PAYMENT_PERIOD = 24 * 60 * 60
def __init__(self, ledger: Ledger, wallet: Wallet): def __init__(self, ledger: Ledger, wallet: Wallet):
self.ledger = ledger self.ledger = ledger
@ -19,9 +20,12 @@ class WalletServerPayer:
async def pay(self): async def pay(self):
while self.running: while self.running:
await asyncio.sleep(24 * 60 * 60) 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'])
if not address or not amount:
continue
if not self.ledger.is_valid_address(address): if not self.ledger.is_valid_address(address):
raise Exception(f"Invalid address: {address}") raise Exception(f"Invalid address: {address}")
@ -37,7 +41,7 @@ class WalletServerPayer:
self.wallet.get_account_or_default(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() # await self.analytics_manager.send_credits_sent() fixme: handle that
async def start(self): async def start(self):
self.running = True self.running = True

View file

@ -3,8 +3,9 @@ import asyncio
import lbry import lbry
import lbry.wallet import lbry.wallet
from lbry.wallet.network import ClientSession from lbry.wallet.network import ClientSession
from lbry.testcase import IntegrationTestCase, CommandTestCase from lbry.testcase import IntegrationTestCase, CommandTestCase, AdvanceTimeTestCase
from lbry.wallet.orchstr8.node import SPVNode from lbry.wallet.orchstr8.node import SPVNode
from lbry.wallet.usage_payment import WalletServerPayer
class TestSessions(IntegrationTestCase): class TestSessions(IntegrationTestCase):
@ -53,37 +54,34 @@ class TestSegwitServer(IntegrationTestCase):
class TestUsagePayment(CommandTestCase): class TestUsagePayment(CommandTestCase):
VERBOSITY = 'DEBUG'
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):
# create wallet server
# set payment address and fee rate on server
# connect to server
# fast forward 24 hours
# check that payment was sent to server
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)
self.assertEqual(history, [])
node = SPVNode(self.conductor.spv_module, node_number=2) node = SPVNode(self.conductor.spv_module, node_number=2)
await node.start(self.blockchain, extraconf={"PAYMENT_ADDRESS": address, "DAILY_FEE": "1"}) await node.start(self.blockchain, extraconf={"PAYMENT_ADDRESS": address, "DAILY_FEE": "1.1"})
self.daemon.jsonrpc_settings_set('lbryum_servers', [f"{node.hostname}:{node.port}"])
self.ledger.network.config['default_servers'] = [(node.hostname, node.port)] await self.daemon.jsonrpc_wallet_reconnect()
await self.ledger.stop()
await self.ledger.start()
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["daily_fee"], "1.1")
await asyncio.sleep(1) # fixme: wait on something better
_, history = await self.ledger.get_local_status_and_history(address)
pass self.assertNotEqual(history, [])
txid, nout = history[0]
# async def test_daily_payment(self): tx_details = await self.daemon.jsonrpc_transaction_show(txid)
# node2 = SPVNode(self.conductor.spv_module, node_number=2) self.assertEqual(tx_details.outputs[nout].amount, 110000000)
# self.ledger.network.config['default_servers'].append((node2.hostname, node2.port)) self.assertEqual(tx_details.outputs[nout].get_address(self.ledger), address)
# await asyncio.wait_for(self.ledger.stop(), timeout=1)
# await asyncio.wait_for(self.ledger.start(), timeout=1)
# self.ledger.network.session_pool.new_connection_event.clear()
# await node2.start(self.blockchain)
# # this is only to speed up the test as retrying would take 4+ seconds
# for session in self.ledger.network.session_pool.sessions:
# session.trigger_urgent_reconnect.set()
# await asyncio.wait_for(self.ledger.network.session_pool.new_connection_event.wait(), timeout=1)