Skip to content

Commit

Permalink
wallet_status no longer fails if wallet component has not started
Browse files Browse the repository at this point in the history
  • Loading branch information
eukreign committed Mar 31, 2020
1 parent d5e5d90 commit a5d06fb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
7 changes: 5 additions & 2 deletions lbry/extras/daemon/componentmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,14 @@ def get_components_status(self):
for component in self.components
}

def get_component(self, component_name):
def get_actual_component(self, component_name):
for component in self.components:
if component.component_name == component_name:
return component.component
return component
raise NameError(component_name)

def get_component(self, component_name):
return self.get_actual_component(component_name).component

def has_component(self, component_name):
return any(component for component in self.components if component_name == component.component_name)
2 changes: 2 additions & 0 deletions lbry/extras/daemon/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,8 @@ def jsonrpc_wallet_status(self, wallet_id=None):
Returns:
Dictionary of wallet status information.
"""
if self.wallet_manager is None:
return {'is_encrypted': None, 'is_syncing': True, 'is_locked': None}
wallet = self.wallet_manager.get_wallet_or_default(wallet_id)
return {
'is_encrypted': wallet.is_encrypted,
Expand Down
4 changes: 4 additions & 0 deletions lbry/wallet/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ def __init__(self, loop=None):
self._loop = loop or get_event_loop()
self._tasks = set()
self.done = Event()
self.started = Event()

def __len__(self):
return len(self._tasks)

def add(self, coro):
task = self._loop.create_task(coro)
self._tasks.add(task)
self.started.set()
self.done.clear()
task.add_done_callback(self._remove)
return task
Expand All @@ -22,8 +24,10 @@ def _remove(self, task):
self._tasks.remove(task)
if len(self._tasks) < 1:
self.done.set()
self.started.clear()

def cancel(self):
for task in self._tasks:
task.cancel()
self.done.set()
self.started.clear()
29 changes: 20 additions & 9 deletions tests/integration/blockchain/test_wallet_commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import asyncio
import json
import os

from lbry.wallet import ENCRYPT_ON_DISK
from lbry.error import InvalidPasswordError
Expand All @@ -22,14 +21,26 @@ async def test_wallet_create_and_add_subscribe(self):

async def test_wallet_syncing_status(self):
address = await self.daemon.jsonrpc_address_unused()
sendtxid = await self.blockchain.send_to_address(address, 1)

async def eventually_will_sync():
while not self.daemon.jsonrpc_wallet_status()['is_syncing']:
await asyncio.sleep(0)
check_sync = asyncio.create_task(eventually_will_sync())
await self.confirm_tx(sendtxid, self.ledger)
await asyncio.wait_for(check_sync, timeout=10)
self.assertFalse(self.daemon.jsonrpc_wallet_status()['is_syncing'])
await self.blockchain.send_to_address(address, 1)
await self.ledger._update_tasks.started.wait()
self.assertTrue(self.daemon.jsonrpc_wallet_status()['is_syncing'])
await self.ledger._update_tasks.done.wait()
self.assertFalse(self.daemon.jsonrpc_wallet_status()['is_syncing'])

wallet = self.daemon.component_manager.get_actual_component('wallet')
wallet_manager = wallet.wallet_manager
# when component manager hasn't started yet
wallet.wallet_manager = None
self.assertEqual(
{'is_encrypted': None, 'is_syncing': True, 'is_locked': None},
self.daemon.jsonrpc_wallet_status()
)
wallet.wallet_manager = wallet_manager
self.assertEqual(
{'is_encrypted': False, 'is_syncing': False, 'is_locked': False},
self.daemon.jsonrpc_wallet_status()
)

async def test_wallet_reconnect(self):
await self.conductor.spv_node.stop(True)
Expand Down

0 comments on commit a5d06fb

Please sign in to comment.