Skip to content

Commit

Permalink
Implement no-history synchronization
Browse files Browse the repository at this point in the history
No-history is a method for synchronizing a wallet by scanning the UTXO
set. It can be useful for checking whether seed phrase backups have
money on them before committing the time and effort required to
rescanning the blockchain. No-history sync is compatible with pruning.
The sync method cannot tell which empty addresses have been used, so
cannot guarentee avoidance of address reuse. For this reason no-history
sync disables wallet address generation and can only be used with
wallet-tool and for sending coinjoins without change addresses.
  • Loading branch information
chris-belcher committed Nov 11, 2019
1 parent 6ec8b09 commit 142aa6a
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 81 deletions.
12 changes: 12 additions & 0 deletions docs/release-notes/release-notes-nohistory-sync.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Notable changes
===============

### No-history wallet synchronization

The no-history synchronization method is enabled by setting `blockchain_source = bitcoin-rpc-no-history` in the `joinmarket.cfg` file.

The method can be used to import a seed phrase to see whether it has any money on it within just 5-10 minutes. No-history sync doesn't require a long blockchain rescan, although it needs a full node which can be pruned.

No-history sync works by scanning the full node's UTXO set. The downside is that it cannot find the history but only the current unspent balance, so it cannot avoid address reuse. Therefore when using no-history synchronization the wallet cannot generate new addresses. Any found money can only be spent by fully-sweeping the funds but not partially spending them which requires a change address. When using the method make sure to increase the gap limit to a large amount to cover all the possible bitcoin addresses where coins might be.

The mode does not work with the Joinmarket-Qt GUI application but might do in future.
127 changes: 98 additions & 29 deletions jmclient/jmclient/blockchaininterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import abc
import random
import sys
import time
from decimal import Decimal
from twisted.internet import reactor, task

Expand Down Expand Up @@ -56,6 +57,11 @@ def estimate_fee_per_kb(self, N):
required for inclusion in the next N blocks.
'''

@abc.abstractmethod
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"""

def fee_per_kb_has_been_manually_set(self, N):
'''if the 'block' target is higher than 1000, interpret it
as manually set fee/Kb.
Expand All @@ -65,6 +71,13 @@ def fee_per_kb_has_been_manually_set(self, N):
else:
return False

def post_sync_wallet_callback(self, wallet):
"""
Callback which blockchain interfaces may use to obtain a reference to
the wallet. The function is called after sync'ing is done
"""
pass


class ElectrumWalletInterface(BlockchainInterface): #pragma: no cover
"""A pseudo-blockchain interface using the existing
Expand Down Expand Up @@ -166,22 +179,16 @@ def __init__(self, jsonRpc, network):
actualNet = blockchainInfo['chain']

netmap = {'main': 'mainnet', 'test': 'testnet', 'regtest': 'regtest'}
if netmap[actualNet] != network:
if netmap[actualNet] != network and \
(not (actualNet == "regtest" and network == "testnet")):
#special case of regtest and testnet having the same addr format
raise Exception('wrong network configured')

def get_block(self, blockheight):
"""Returns full serialized block at a given height.
"""
block_hash = self.rpc('getblockhash', [blockheight])
block = self.rpc('getblock', [block_hash, False])
if not block:
return False
return block

def rpc(self, method, args):
if method not in ['importaddress', 'walletpassphrase', 'getaccount',
'gettransaction', 'getrawtransaction', 'gettxout',
'importmulti', 'listtransactions', 'getblockcount']:
'importmulti', 'listtransactions', 'getblockcount',
'scantxoutset']:
log.debug('rpc: ' + method + " " + str(args))
res = self.jsonRpc.call(method, args)
return res
Expand Down Expand Up @@ -229,25 +236,20 @@ def import_addresses(self, addr_list, wallet_name, restart_cb=None):
jmprint(fatal_msg, "important")
sys.exit(1)

def add_watchonly_addresses(self, addr_list, wallet_name, restart_cb=None):
"""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
to be new/unused). For that case use import_addresses instead.
"""
self.import_addresses(addr_list, wallet_name)
if jm_single().config.get("BLOCKCHAIN",
"blockchain_source") != 'regtest': #pragma: no cover
#Exit conditions cannot be included in tests
restart_msg = ("restart Bitcoin Core with -rescan or use "
"`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)
def import_addresses_if_needed(self, addresses, wallet_name):
try:
imported_addresses = set(self.rpc('getaddressesbyaccount',
[wallet_name]))
except JsonRpcError:
if wallet_name in self.rpc('listlabels', []):
imported_addresses = set(self.rpc('getaddressesbylabel',
[wallet_name]).keys())
else:
jmprint(restart_msg, "important")
sys.exit(0)
imported_addresses = set()
import_needed = not addresses.issubset(imported_addresses)
if import_needed:
self.import_addresses(addresses - imported_addresses, wallet_name)
return import_needed

def _yield_transactions(self, wallet_name):
batch_size = 1000
Expand Down Expand Up @@ -386,6 +388,73 @@ def estimate_fee_per_kb(self, N):
return 10000
return int(Decimal(1e8) * Decimal(estimate))

class BitcoinCoreNoHistoryInterface(BitcoinCoreInterface):

def __init__(self, jsonRpc, network):
super(BitcoinCoreNoHistoryInterface, self).__init__(jsonRpc, network)
self.import_addresses_call_count = 0
self.wallet_name = None
self.scan_result = None

def import_addresses_if_needed(self, addresses, wallet_name):
self.import_addresses_call_count += 1
if self.import_addresses_call_count == 1:
self.wallet_name = wallet_name
addr_list = ["addr(" + a + ")" for a in addresses]
log.debug("Starting scan of UTXO set")
st = time.time()
self.rpc("scantxoutset", ["abort"])
self.scan_result = self.rpc("scantxoutset", ["start", addr_list])
et = time.time()
log.debug("UTXO set scan took " + str(et - st) + "sec")
elif self.import_addresses_call_count > 4:
#called twice for the first call of sync_addresses(), then two
# more times for the second call. the second call happens because
# sync_addresses() re-runs in order to have gap_limit new addresses
assert False
return False

def _get_addr_from_desc(self, desc_str):
#example
#'desc': 'addr(2MvAfRVvRAeBS18NT7mKVc1gFim169GkFC5)#h5yn9eq4',
assert desc_str.startswith("addr(")
return desc_str[5:desc_str.find(")")]

def _yield_transactions(self, wallet_name):
for u in self.scan_result["unspents"]:
tx = {"category": "receive", "address":
self._get_addr_from_desc(u["desc"])}
yield tx

def list_transactions(self, num):
return []

def rpc(self, method, args):
if method == "listaddressgroupings":
raise RuntimeError("default sync not supported by bitcoin-rpc-nohistory, use --recoversync")
elif method == "listunspent":
minconf = 0 if len(args) < 1 else args[0]
maxconf = 9999999 if len(args) < 2 else args[1]
return [{
"address": self._get_addr_from_desc(u["desc"]),
"label": self.wallet_name,
"height": u["height"],
"txid": u["txid"],
"vout": u["vout"],
"scriptPubKey": u["scriptPubKey"],
"amount": u["amount"]
} for u in self.scan_result["unspents"]]
else:
return super(BitcoinCoreNoHistoryInterface, self).rpc(method, args)

def post_sync_wallet_callback(self, wallet):
#make wallet-tool not display any new addresses
#because no-history cant tell if an address is used and empty#
#so this is necessary to avoid address reuse
wallet.gap_limit = 0
#disable generating change addresses, also because cant guarentee
# avoidance of address reuse
wallet.disable_new_scripts = True

# class for regtest chain access
# running on local daemon. Only
Expand Down
12 changes: 8 additions & 4 deletions jmclient/jmclient/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ def jm_single():
use_ssl = false
[BLOCKCHAIN]
#options: bitcoin-rpc, regtest, electrum-server
#options: bitcoin-rpc, regtest, electrum-server, bitcoin-rpc-no-history
# for instructions on bitcoin-rpc read
# https://github.com/chris-belcher/joinmarket/wiki/Running-JoinMarket-with-Bitcoin-Core-full-node
# when using bitcoin-rpc-no-history remember to increase the gap limit to scan for more addresses, try -g 5000
blockchain_source = bitcoin-rpc
network = mainnet
rpc_host = localhost
Expand Down Expand Up @@ -517,12 +518,13 @@ def get_blockchain_interface_instance(_config):
# todo: refactor joinmarket module to get rid of loops
# importing here is necessary to avoid import loops
from jmclient.blockchaininterface import BitcoinCoreInterface, \
RegtestBitcoinCoreInterface, ElectrumWalletInterface
RegtestBitcoinCoreInterface, ElectrumWalletInterface, \
BitcoinCoreNoHistoryInterface
from jmclient.electruminterface import ElectrumInterface
source = _config.get("BLOCKCHAIN", "blockchain_source")
network = get_network()
testnet = network == 'testnet'
if source in ('bitcoin-rpc', 'regtest'):
if source in ('bitcoin-rpc', 'regtest', 'bitcoin-rpc-no-history'):
rpc_host = _config.get("BLOCKCHAIN", "rpc_host")
rpc_port = _config.get("BLOCKCHAIN", "rpc_port")
rpc_user, rpc_password = get_bitcoin_rpc_credentials(_config)
Expand All @@ -531,8 +533,10 @@ def get_blockchain_interface_instance(_config):
rpc_wallet_file)
if source == 'bitcoin-rpc': #pragma: no cover
bc_interface = BitcoinCoreInterface(rpc, network)
else:
elif source == 'regtest':
bc_interface = RegtestBitcoinCoreInterface(rpc)
else:
bc_interface = BitcoinCoreNoHistoryInterface(rpc, network)
elif source == 'electrum':
bc_interface = ElectrumWalletInterface(testnet)
elif source == 'electrum-server':
Expand Down
9 changes: 8 additions & 1 deletion jmclient/jmclient/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,7 @@ def __init__(self, storage, **kwargs):
self.get_bip32_priv_export(0, 0).encode('ascii')).digest())\
.digest()[:3]
self._populate_script_map()
self.disable_new_scripts = False

@classmethod
def initialize(cls, storage, network, max_mixdepth=2, timestamp=None,
Expand Down Expand Up @@ -1372,7 +1373,7 @@ def get_script_path(self, path):
current_index = self._index_cache[md][int_type]

if index == current_index:
return self.get_new_script(md, int_type)
return self.get_new_script_override_disable(md, int_type)

priv, engine = self._get_priv_from_path(path)
script = engine.privkey_to_script(priv)
Expand Down Expand Up @@ -1454,6 +1455,12 @@ def _is_my_bip32_path(self, path):
return path[0] == self._key_ident

def get_new_script(self, mixdepth, internal):
if self.disable_new_scripts:
raise RuntimeError("Obtaining new wallet addresses "
+ "disabled, due to nohistory mode")
return self.get_new_script_override_disable(mixdepth, internal)

def get_new_script_override_disable(self, mixdepth, internal):
# This is called by get_script_path and calls back there. We need to
# ensure all conditions match to avoid endless recursion.
int_type = self._get_internal_type(internal)
Expand Down
79 changes: 47 additions & 32 deletions jmclient/jmclient/wallet_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import time
import ast
import binascii
import sys
from decimal import Decimal
from copy import deepcopy
from twisted.internet import reactor
Expand All @@ -14,8 +15,8 @@
from numbers import Integral
from jmclient.configure import jm_single, get_log
from jmclient.output import fmt_tx_data
from jmclient.jsonrpc import JsonRpcError
from jmclient.blockchaininterface import INF_HEIGHT
from jmclient.blockchaininterface import INF_HEIGHT, BitcoinCoreInterface
from jmbase.support import jmprint
"""Wallet service
The purpose of this independent service is to allow
Expand Down Expand Up @@ -330,6 +331,7 @@ def sync_wallet(self, fast=True):
# Don't attempt updates on transactions that existed
# before startup
self.old_txs = [x['txid'] for x in self.bci.list_transactions(100)]
self.bci.post_sync_wallet_callback(self.wallet)
return self.synced

def resync_wallet(self, fast=True):
Expand Down Expand Up @@ -432,6 +434,25 @@ def get_address_usages(self):
self.rewind_wallet_indices(used_indices, saved_indices)
self.synced = True

def display_rescan_message_and_system_exit(self, restart_cb):
#TODO using system exit should be avoided as it makes the code
# harder to understand and maintain
#the whole idea of a restart callback is only needed because of exit
#one day we should remove sys.exit() and throw exceptions instead
#until that day, the exit call is moved here
#theres also a sys.exit() in BitcoinCoreInterface.import_addresses()
if self.bci.__class__ == BitcoinCoreInterface:
#Exit conditions cannot be included in tests
restart_msg = ("restart Bitcoin Core with -rescan or use "
"`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)
else:
jmprint(restart_msg, "important")
sys.exit(0)

def sync_addresses(self):
""" Triggered by use of --recoversync option in scripts,
attempts a full scan of the blockchain without assuming
Expand All @@ -441,19 +462,11 @@ def sync_addresses(self):
jlog.debug("requesting detailed wallet history")
wallet_name = self.get_wallet_name()
addresses, saved_indices = self.collect_addresses_init()
try:
imported_addresses = set(self.bci.rpc('getaddressesbyaccount',
[wallet_name]))
except JsonRpcError:
if wallet_name in self.bci.rpc('listlabels', []):
imported_addresses = set(self.bci.rpc('getaddressesbylabel',
[wallet_name]).keys())
else:
imported_addresses = set()

if not addresses.issubset(imported_addresses):
self.bci.add_watchonly_addresses(addresses - imported_addresses,
wallet_name, self.restart_callback)
import_needed = self.bci.import_addresses_if_needed(addresses,
wallet_name)
if import_needed:
self.display_rescan_message_and_system_exit(self.restart_callback)
return

used_addresses_gen = (tx['address']
Expand All @@ -465,13 +478,12 @@ def sync_addresses(self):
self.rewind_wallet_indices(used_indices, saved_indices)

new_addresses = self.collect_addresses_gap()
if not new_addresses.issubset(imported_addresses):
jlog.debug("Syncing iteration finished, additional step required")
self.bci.add_watchonly_addresses(new_addresses - imported_addresses,
wallet_name, self.restart_callback)
if self.bci.import_addresses_if_needed(new_addresses, wallet_name):
jlog.debug("Syncing iteration finished, additional step required (more address import required)")
self.synced = False
self.display_rescan_message_and_system_exit(self.restart_callback)
elif gap_limit_used:
jlog.debug("Syncing iteration finished, additional step required")
jlog.debug("Syncing iteration finished, additional step required (gap limit used)")
self.synced = False
else:
jlog.debug("Wallet successfully synced")
Expand All @@ -498,14 +510,26 @@ def sync_unspent(self):
our_unspent_list = [x for x in unspent_list if (
self.bci.is_address_labeled(x, wallet_name) or
self.bci.is_address_labeled(x, self.EXTERNAL_WALLET_LABEL))]
for u in our_unspent_list:
if not self.is_known_addr(u['address']):
for utxo in our_unspent_list:
if not self.is_known_addr(utxo['address']):
continue
self._add_unspent_utxo(u, current_blockheight)
if "height" in utxo:
height = utxo["height"]
else:
height = None
# wallet's utxo database needs to store an absolute rather
# than relative height measure:
confs = int(utxo['confirmations'])
if confs < 0:
jlog.warning("Utxo not added, has a conflict: " + str(utxo))
continue
if confs >= 1:
height = current_blockheight - confs + 1
self._add_unspent_txo(utxo, height)
et = time.time()
jlog.debug('bitcoind sync_unspent took ' + str((et - st)) + 'sec')

def _add_unspent_utxo(self, utxo, current_blockheight):
def _add_unspent_txo(self, utxo, height):
"""
Add a UTXO as returned by rpc's listunspent call to the wallet.
Expand All @@ -517,15 +541,6 @@ def _add_unspent_utxo(self, utxo, current_blockheight):
txid = binascii.unhexlify(utxo['txid'])
script = binascii.unhexlify(utxo['scriptPubKey'])
value = int(Decimal(str(utxo['amount'])) * Decimal('1e8'))
confs = int(utxo['confirmations'])
# wallet's utxo database needs to store an absolute rather
# than relative height measure:
height = None
if confs < 0:
jlog.warning("Utxo not added, has a conflict: " + str(utxo))
return
if confs >=1 :
height = current_blockheight - confs + 1
self.add_utxo(txid, int(utxo['vout']), script, value, height)


Expand Down
Loading

0 comments on commit 142aa6a

Please sign in to comment.