From 378018840ff8304c148033289d61a9644bdfef56 Mon Sep 17 00:00:00 2001 From: Kristaps Kaupe Date: Mon, 29 Nov 2021 03:51:47 +0200 Subject: [PATCH] Move Bitcoin RPC specific code out of JsonRpc class --- jmclient/jmclient/blockchaininterface.py | 17 ++++++++++++----- jmclient/jmclient/configure.py | 12 +++++++----- jmclient/jmclient/jsonrpc.py | 13 ++++--------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/jmclient/jmclient/blockchaininterface.py b/jmclient/jmclient/blockchaininterface.py index e15a4f0..935aacf 100644 --- a/jmclient/jmclient/blockchaininterface.py +++ b/jmclient/jmclient/blockchaininterface.py @@ -168,7 +168,7 @@ class ElectrumWalletInterface(BlockchainInterface): #pragma: no cover class BitcoinCoreInterface(BlockchainInterface): - def __init__(self, jsonRpc, network): + def __init__(self, jsonRpc, network, wallet_name): super().__init__() self.jsonRpc = jsonRpc blockchainInfo = self._rpc("getblockchaininfo", []) @@ -188,6 +188,13 @@ class BitcoinCoreInterface(BlockchainInterface): #special case of regtest and testnet having the same addr format raise Exception('wrong network configured') + if wallet_name: + self.jsonRpc.setURL("/wallet/" + wallet_name) + # Check that RPC wallet is loaded. If not, try to load it. + loaded_wallets = self._rpc("listwallets", []) + if not wallet_name in loaded_wallets: + self._rpc("loadwallet", [wallet_name]) + def is_address_imported(self, addr): return len(self._rpc('getaddressinfo', [addr])['labels']) > 0 @@ -608,8 +615,8 @@ class RegtestBitcoinCoreMixin(): class BitcoinCoreNoHistoryInterface(BitcoinCoreInterface, RegtestBitcoinCoreMixin): - def __init__(self, jsonRpc, network): - super().__init__(jsonRpc, network) + def __init__(self, jsonRpc, network, wallet_name): + super().__init__(jsonRpc, network, wallet_name) self.import_addresses_call_count = 0 self.wallet_name = None self.scan_result = None @@ -682,8 +689,8 @@ class BitcoinCoreNoHistoryInterface(BitcoinCoreInterface, RegtestBitcoinCoreMixi # with > 100 blocks. class RegtestBitcoinCoreInterface(BitcoinCoreInterface, RegtestBitcoinCoreMixin): #pragma: no cover - def __init__(self, jsonRpc): - super().__init__(jsonRpc, 'regtest') + def __init__(self, jsonRpc, wallet_name): + super().__init__(jsonRpc, 'regtest', wallet_name) self.pushtx_failure_prob = 0 self.tick_forward_chain_interval = -1 self.absurd_fees = False diff --git a/jmclient/jmclient/configure.py b/jmclient/jmclient/configure.py index 1d03ef6..471b102 100644 --- a/jmclient/jmclient/configure.py +++ b/jmclient/jmclient/configure.py @@ -778,19 +778,21 @@ def get_blockchain_interface_instance(_config): raise ValueError('wrong network configured: ' + network) rpc_user, rpc_password = get_bitcoin_rpc_credentials(_config) rpc_wallet_file = _config.get("BLOCKCHAIN", "rpc_wallet_file") - rpc = JsonRpc(rpc_host, rpc_port, rpc_user, rpc_password, - rpc_wallet_file) + rpc = JsonRpc(rpc_host, rpc_port, rpc_user, rpc_password) if source == 'bitcoin-rpc': #pragma: no cover - bc_interface = BitcoinCoreInterface(rpc, network) + bc_interface = BitcoinCoreInterface(rpc, network, + rpc_wallet_file) if testnet: btc.select_chain_params("bitcoin/testnet") else: btc.select_chain_params("bitcoin") elif source == 'regtest': - bc_interface = RegtestBitcoinCoreInterface(rpc) + bc_interface = RegtestBitcoinCoreInterface(rpc, + rpc_wallet_file) btc.select_chain_params("bitcoin/regtest") elif source == "bitcoin-rpc-no-history": - bc_interface = BitcoinCoreNoHistoryInterface(rpc, network) + bc_interface = BitcoinCoreNoHistoryInterface(rpc, network, + rpc_wallet_file) if testnet or network == "regtest": # in tests, for bech32 regtest addresses, for bc-no-history, # this will have to be reset manually: diff --git a/jmclient/jmclient/jsonrpc.py b/jmclient/jmclient/jsonrpc.py index 7dd7381..cebdfec 100644 --- a/jmclient/jmclient/jsonrpc.py +++ b/jmclient/jmclient/jsonrpc.py @@ -54,21 +54,16 @@ class JsonRpc(object): to connect to Bitcoin. """ - def __init__(self, host, port, user, password, wallet_name=""): + def __init__(self, host, port, user, password, url=""): self.host = host self.port = int(port) self.conn = http.client.HTTPConnection(self.host, self.port) self.authstr = "%s:%s" % (user, password) - if len(wallet_name) > 0: - self.url = "/wallet/" + wallet_name - else: - self.url = "" + self.url = url self.queryId = 1 - # Check that RPC wallet is loaded. If not, try to load it. - loaded_wallets = self.call("listwallets", []) - if not wallet_name in loaded_wallets: - self.call("loadwallet", [wallet_name]) + def setURL(self, url): + self.url = url def queryHTTP(self, obj): """