Skip to content

Commit

Permalink
CLI wallet-tool script syncs via WalletService
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamISZ committed Sep 30, 2019
1 parent c7b207b commit 8f594f0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
21 changes: 12 additions & 9 deletions jmclient/jmclient/blockchaininterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ class BlockchainInterface(object):
def __init__(self):
pass

def sync_wallet(self, wallet, restart_cb=None):
def sync_wallet(self, wallet):
"""Default behaviour is for Core and similar interfaces.
If address sync fails, flagged with wallet_synced value;
do not attempt to sync_unspent in that case.
"""
self.sync_addresses(wallet, restart_cb)
self.sync_addresses(wallet)
if self.wallet_synced:
self.sync_unspent(wallet)

Expand Down Expand Up @@ -308,7 +308,7 @@ def import_addresses(self, addr_list, wallet_name):
"".format(num_failed))
sys.exit(1)

def add_watchonly_addresses(self, addr_list, wallet_name, restart_cb=None):
def add_watchonly_addresses(self, addr_list, wallet_name):
"""For backwards compatibility, this fn name is preserved
as the case where we quit the program if a rescan is required;
but in some cases a rescan is not required (if the address is known
Expand All @@ -322,19 +322,22 @@ def add_watchonly_addresses(self, addr_list, wallet_name, restart_cb=None):
"`bitcoin-cli rescanblockchain` if you're "
"recovering an existing wallet from backup seed\n"
"Otherwise just restart this joinmarket application.")
if restart_cb:
restart_cb(restart_msg)
if self.restart_cb:
self.restart_cb(restart_msg)
else:
jmprint(restart_msg, "important")
sys.exit(0)

def sync_wallet(self, wallet, fast=True, restart_cb=None):
self.restart_cb = restart_cb
# choose fast sync (now the default) if triggered.
if fast:
self.sync_wallet_fast(wallet)
self.fast_sync_called = True
return
super(BitcoinCoreInterface, self).sync_wallet(wallet, restart_cb=restart_cb)
super(BitcoinCoreInterface, self).sync_wallet(wallet)
# don't remember callback status for next sync if it happens:
self.restart_cb = None
self.fast_sync_called = False

def sync_wallet_fast(self, wallet):
Expand Down Expand Up @@ -428,7 +431,7 @@ def get_address_usages(self, wallet):
wallet.rewind_wallet_indices(used_indices, saved_indices)
self.wallet_synced = True

def sync_addresses(self, wallet, restart_cb=None):
def sync_addresses(self, wallet):
# This more detailed version of wallet syncing must cover situations
# where the user has used the wallet on other Bitcoin Core instances
# and therefore the wallet label either does not have addresses
Expand All @@ -452,7 +455,7 @@ def sync_addresses(self, wallet, restart_cb=None):

if not addresses.issubset(imported_addresses):
self.add_watchonly_addresses(addresses - imported_addresses,
wallet_name, restart_cb)
wallet_name)
return

used_addresses_gen = (tx['address']
Expand All @@ -467,7 +470,7 @@ def sync_addresses(self, wallet, restart_cb=None):
if not new_addresses.issubset(imported_addresses):
log.debug("Syncing iteration finished, additional step required")
self.add_watchonly_addresses(new_addresses - imported_addresses,
wallet_name, restart_cb)
wallet_name)
self.wallet_synced = False
elif gap_limit_used:
log.debug("Syncing iteration finished, additional step required")
Expand Down
15 changes: 8 additions & 7 deletions jmclient/jmclient/wallet_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from collections import Counter
from itertools import islice
from jmclient import (get_network, WALLET_IMPLEMENTATIONS, Storage, podle,
jm_single, BitcoinCoreInterface, JsonRpcError, sync_wallet, WalletError,
jm_single, BitcoinCoreInterface, JsonRpcError, WalletError,
VolatileStorage, StoragePasswordError, is_segwit_mode, SegwitLegacyWallet,
LegacyWallet, SegwitWallet, is_native_segwit_mode)
from jmclient.wallet_service import WalletService
Expand Down Expand Up @@ -1182,17 +1182,18 @@ def wallet_tool_main(wallet_root_path):
wallet_path, seed, options.mixdepth, read_only=read_only,
gap_limit=options.gaplimit)

# this object is only to respect the layering,
# the service will not be started since this is a synchronous script:
ws = WalletService(wallet)

if method not in noscan_methods:
# if nothing was configured, we override bitcoind's options so that
# unconfirmed balance is included in the wallet display by default
if 'listunspent_args' not in jm_single().config.options('POLICY'):
jm_single().config.set('POLICY','listunspent_args', '[0]')
while not jm_single().bc_interface.wallet_synced:
sync_wallet(wallet, fast=not options.recoversync)

# this object is only to respect the layering,
# the service will not be started since this is a synchronous script:
ws = WalletService(wallet)
while True:
if ws.sync_wallet(fast = not options.recoversync):
break

#Now the wallet/data is prepared, execute the script according to the method
if method == "display":
Expand Down

0 comments on commit 8f594f0

Please sign in to comment.