From e31e839c1da313d0e25c2d23ad330c398fce4843 Mon Sep 17 00:00:00 2001 From: Kristaps Kaupe Date: Fri, 21 Apr 2023 02:11:11 +0300 Subject: [PATCH] Add get_wallet_rescan_status() instead of getwalletinfo() for bci --- jmclient/jmclient/blockchaininterface.py | 20 +++++++++++++++++--- jmclient/jmclient/wallet_rpc.py | 6 +----- jmclient/jmclient/wallet_service.py | 9 +++------ 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/jmclient/jmclient/blockchaininterface.py b/jmclient/jmclient/blockchaininterface.py index 9637e7543..6283f7efb 100644 --- a/jmclient/jmclient/blockchaininterface.py +++ b/jmclient/jmclient/blockchaininterface.py @@ -4,7 +4,7 @@ import random import sys import time -from typing import Optional +from typing import Optional, Tuple from decimal import Decimal import binascii from twisted.internet import reactor, task @@ -56,6 +56,11 @@ def estimate_fee_per_kb(self, N): 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): """import addresses to the underlying blockchain interface if needed returns True if the sync call needs to do a system exit""" @@ -99,7 +104,7 @@ def __init__(self, jsonRpc, network, wallet_name): if not wallet_name in loaded_wallets: self._rpc("loadwallet", [wallet_name]) # We only support legacy wallets currently - wallet_info = self._rpc("getwalletinfo", []) + wallet_info = self._getwalletinfo() if "descriptors" in wallet_info and wallet_info["descriptors"]: raise Exception( "JoinMarket currently does not support Bitcoin Core " @@ -146,12 +151,21 @@ def rescan_in_thread(self, start_height: int) -> None: log.error("Failure of RPC connection to Bitcoin Core. " "Rescanning process not started.") - def getwalletinfo(self) -> dict: + def _getwalletinfo(self) -> dict: """ Returns detailed about currently loaded (see `loadwallet` call in __init__) Bitcoin Core wallet. """ 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): """ Returns the result of an rpc call to the Bitcoin Core RPC API. If the connection is permanently or unrecognizably broken, None diff --git a/jmclient/jmclient/wallet_rpc.py b/jmclient/jmclient/wallet_rpc.py index 7f273b23e..d844f5f8c 100644 --- a/jmclient/jmclient/wallet_rpc.py +++ b/jmclient/jmclient/wallet_rpc.py @@ -641,11 +641,7 @@ def session(self, request): if self.services["wallet"]: if self.services["wallet"].isRunning(): - winfo = self.services["wallet"].get_backend_walletinfo() - 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 + rescanning, _ = self.services["wallet"].get_backend_wallet_rescan_status() wallet_name = self.wallet_name # At this point if an `auth_header` is present, it has been checked # by the call to `check_cookie_if_present` above. diff --git a/jmclient/jmclient/wallet_service.py b/jmclient/jmclient/wallet_service.py index e396aba7a..892eb74a1 100644 --- a/jmclient/jmclient/wallet_service.py +++ b/jmclient/jmclient/wallet_service.py @@ -4,7 +4,7 @@ import itertools import time import sys -from typing import Optional +from typing import Optional, Tuple from decimal import Decimal from copy import deepcopy from twisted.internet import reactor @@ -733,11 +733,8 @@ def get_block_height(self, blockhash): def rescanblockchain(self, start_height: int, end_height: Optional[int] = None) -> None: self.bci.rescanblockchain(start_height, end_height) - def get_backend_walletinfo(self) -> dict: - """ 'Backend' wallet means the Bitcoin Core wallet, - which will always be loaded if self.bci is init-ed. - """ - return self.bci.getwalletinfo() + def get_backend_wallet_rescan_status(self) -> Tuple[bool, Optional[Decimal]]: + return self.bci.get_wallet_rescan_status() def get_transaction_block_height(self, tx): """ Given a CTransaction object tx, return