Browse Source

Merge JoinMarket-Org/joinmarket-clientserver#1478: Refactor: Add `get_wallet_rescan_status()` instead of `getwalletinfo()` for `bci`

e31e839c1d Add get_wallet_rescan_status() instead of getwalletinfo() for bci (Kristaps Kaupe)

Pull request description:

  Noticed this when tried to rebase #1462 after merging of #1477. #1461 added public `getwalletinfo()` method to `BitcoinCoreInterface`, which was used by code outside of `jmclient/jmclient/blockchaininterface.py`. This is bad approach, as it relies on Bitcoin Core RPC `getwalletinfo` returned `dict`, which contains a lots of different stuff too, could lead to more problems in future introducing other blockchain interface classes. Let's instead have generic method returning just wallet rescan status. Also it now returns `Tuple[bool, Optional[Decimal]]` with rescan status percentage, if rescan is in progress, although that's not used by any other code for now.

ACKs for top commit:
  AdamISZ:
    utACK e31e839c1d , very much agree with the thinking here.

Tree-SHA512: 2d8c9b8157847e713838099d0f62dfcd5321c9498cf8453a9087407e2cd9c32906739c8e71460fc6ac6426662d2ac501261080fea08388d928933f788bda9a8d
master
Kristaps Kaupe 3 years ago
parent
commit
2289fcbd72
No known key found for this signature in database
GPG Key ID: 33E472FE870C7E5D
  1. 20
      jmclient/jmclient/blockchaininterface.py
  2. 6
      jmclient/jmclient/wallet_rpc.py
  3. 9
      jmclient/jmclient/wallet_service.py

20
jmclient/jmclient/blockchaininterface.py

@ -4,7 +4,7 @@ import ast
import random import random
import sys import sys
import time import time
from typing import Optional from typing import Optional, Tuple
from decimal import Decimal from decimal import Decimal
import binascii import binascii
from twisted.internet import reactor, task from twisted.internet import reactor, task
@ -56,6 +56,11 @@ class BlockchainInterface(object):
required for inclusion in the next N blocks. required for inclusion in the next N blocks.
''' '''
@abc.abstractmethod
def get_wallet_rescan_status(self) -> Tuple[bool, Optional[Decimal]]:
"""Returns pair of True/False is wallet currently rescanning and
Optional[Decimal] with current rescan progress status."""
def import_addresses_if_needed(self, addresses, wallet_name): def import_addresses_if_needed(self, addresses, wallet_name):
"""import addresses to the underlying blockchain interface if needed """import addresses to the underlying blockchain interface if needed
returns True if the sync call needs to do a system exit""" returns True if the sync call needs to do a system exit"""
@ -99,7 +104,7 @@ class BitcoinCoreInterface(BlockchainInterface):
if not wallet_name in loaded_wallets: if not wallet_name in loaded_wallets:
self._rpc("loadwallet", [wallet_name]) self._rpc("loadwallet", [wallet_name])
# We only support legacy wallets currently # We only support legacy wallets currently
wallet_info = self._rpc("getwalletinfo", []) wallet_info = self._getwalletinfo()
if "descriptors" in wallet_info and wallet_info["descriptors"]: if "descriptors" in wallet_info and wallet_info["descriptors"]:
raise Exception( raise Exception(
"JoinMarket currently does not support Bitcoin Core " "JoinMarket currently does not support Bitcoin Core "
@ -146,12 +151,21 @@ class BitcoinCoreInterface(BlockchainInterface):
log.error("Failure of RPC connection to Bitcoin Core. " log.error("Failure of RPC connection to Bitcoin Core. "
"Rescanning process not started.") "Rescanning process not started.")
def getwalletinfo(self) -> dict: def _getwalletinfo(self) -> dict:
""" Returns detailed about currently loaded (see `loadwallet` """ Returns detailed about currently loaded (see `loadwallet`
call in __init__) Bitcoin Core wallet. call in __init__) Bitcoin Core wallet.
""" """
return self._rpc("getwalletinfo", []) return self._rpc("getwalletinfo", [])
def get_wallet_rescan_status(self) -> Tuple[bool, Optional[Decimal]]:
winfo = self._getwalletinfo()
if "scanning" in winfo and winfo["scanning"]:
# If not 'false', it contains info that looks like:
# {'duration': 1, 'progress': Decimal('0.04665404082350701')}
return True, winfo["scanning"]["progress"]
else:
return False, None
def _rpc(self, method, args): def _rpc(self, method, args):
""" Returns the result of an rpc call to the Bitcoin Core RPC API. """ Returns the result of an rpc call to the Bitcoin Core RPC API.
If the connection is permanently or unrecognizably broken, None If the connection is permanently or unrecognizably broken, None

6
jmclient/jmclient/wallet_rpc.py

@ -641,11 +641,7 @@ class JMWalletDaemon(Service):
if self.services["wallet"]: if self.services["wallet"]:
if self.services["wallet"].isRunning(): if self.services["wallet"].isRunning():
winfo = self.services["wallet"].get_backend_walletinfo() rescanning, _ = self.services["wallet"].get_backend_wallet_rescan_status()
if "scanning" in winfo and winfo["scanning"]:
# Note that if not 'false', it contains info
# that looks like: {'duration': 1, 'progress': Decimal('0.04665404082350701')}
rescanning = True
wallet_name = self.wallet_name wallet_name = self.wallet_name
# At this point if an `auth_header` is present, it has been checked # At this point if an `auth_header` is present, it has been checked
# by the call to `check_cookie_if_present` above. # by the call to `check_cookie_if_present` above.

9
jmclient/jmclient/wallet_service.py

@ -4,7 +4,7 @@ import collections
import itertools import itertools
import time import time
import sys import sys
from typing import Optional from typing import Optional, Tuple
from decimal import Decimal from decimal import Decimal
from copy import deepcopy from copy import deepcopy
from twisted.internet import reactor from twisted.internet import reactor
@ -733,11 +733,8 @@ class WalletService(Service):
def rescanblockchain(self, start_height: int, end_height: Optional[int] = None) -> None: def rescanblockchain(self, start_height: int, end_height: Optional[int] = None) -> None:
self.bci.rescanblockchain(start_height, end_height) self.bci.rescanblockchain(start_height, end_height)
def get_backend_walletinfo(self) -> dict: def get_backend_wallet_rescan_status(self) -> Tuple[bool, Optional[Decimal]]:
""" 'Backend' wallet means the Bitcoin Core wallet, return self.bci.get_wallet_rescan_status()
which will always be loaded if self.bci is init-ed.
"""
return self.bci.getwalletinfo()
def get_transaction_block_height(self, tx): def get_transaction_block_height(self, tx):
""" Given a CTransaction object tx, return """ Given a CTransaction object tx, return

Loading…
Cancel
Save