Browse Source

Merge #1088: Move Bitcoin RPC specific code out of JsonRpc class

3780188 Move Bitcoin RPC specific code out of JsonRpc class (Kristaps Kaupe)
master
Adam Gibson 4 years ago
parent
commit
69b564f7d2
No known key found for this signature in database
GPG Key ID: 141001A1AF77F20B
  1. 17
      jmclient/jmclient/blockchaininterface.py
  2. 12
      jmclient/jmclient/configure.py
  3. 13
      jmclient/jmclient/jsonrpc.py

17
jmclient/jmclient/blockchaininterface.py

@ -168,7 +168,7 @@ class ElectrumWalletInterface(BlockchainInterface): #pragma: no cover
class BitcoinCoreInterface(BlockchainInterface): class BitcoinCoreInterface(BlockchainInterface):
def __init__(self, jsonRpc, network): def __init__(self, jsonRpc, network, wallet_name):
super().__init__() super().__init__()
self.jsonRpc = jsonRpc self.jsonRpc = jsonRpc
blockchainInfo = self._rpc("getblockchaininfo", []) blockchainInfo = self._rpc("getblockchaininfo", [])
@ -188,6 +188,13 @@ class BitcoinCoreInterface(BlockchainInterface):
#special case of regtest and testnet having the same addr format #special case of regtest and testnet having the same addr format
raise Exception('wrong network configured') 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): def is_address_imported(self, addr):
return len(self._rpc('getaddressinfo', [addr])['labels']) > 0 return len(self._rpc('getaddressinfo', [addr])['labels']) > 0
@ -603,8 +610,8 @@ class RegtestBitcoinCoreMixin():
class BitcoinCoreNoHistoryInterface(BitcoinCoreInterface, RegtestBitcoinCoreMixin): class BitcoinCoreNoHistoryInterface(BitcoinCoreInterface, RegtestBitcoinCoreMixin):
def __init__(self, jsonRpc, network): def __init__(self, jsonRpc, network, wallet_name):
super().__init__(jsonRpc, network) super().__init__(jsonRpc, network, wallet_name)
self.import_addresses_call_count = 0 self.import_addresses_call_count = 0
self.wallet_name = None self.wallet_name = None
self.scan_result = None self.scan_result = None
@ -677,8 +684,8 @@ class BitcoinCoreNoHistoryInterface(BitcoinCoreInterface, RegtestBitcoinCoreMixi
# with > 100 blocks. # with > 100 blocks.
class RegtestBitcoinCoreInterface(BitcoinCoreInterface, RegtestBitcoinCoreMixin): #pragma: no cover class RegtestBitcoinCoreInterface(BitcoinCoreInterface, RegtestBitcoinCoreMixin): #pragma: no cover
def __init__(self, jsonRpc): def __init__(self, jsonRpc, wallet_name):
super().__init__(jsonRpc, 'regtest') super().__init__(jsonRpc, 'regtest', wallet_name)
self.pushtx_failure_prob = 0 self.pushtx_failure_prob = 0
self.tick_forward_chain_interval = -1 self.tick_forward_chain_interval = -1
self.absurd_fees = False self.absurd_fees = False

12
jmclient/jmclient/configure.py

@ -787,19 +787,21 @@ def get_blockchain_interface_instance(_config):
raise ValueError('wrong network configured: ' + network) raise ValueError('wrong network configured: ' + network)
rpc_user, rpc_password = get_bitcoin_rpc_credentials(_config) rpc_user, rpc_password = get_bitcoin_rpc_credentials(_config)
rpc_wallet_file = _config.get("BLOCKCHAIN", "rpc_wallet_file") rpc_wallet_file = _config.get("BLOCKCHAIN", "rpc_wallet_file")
rpc = JsonRpc(rpc_host, rpc_port, rpc_user, rpc_password, rpc = JsonRpc(rpc_host, rpc_port, rpc_user, rpc_password)
rpc_wallet_file)
if source == 'bitcoin-rpc': #pragma: no cover if source == 'bitcoin-rpc': #pragma: no cover
bc_interface = BitcoinCoreInterface(rpc, network) bc_interface = BitcoinCoreInterface(rpc, network,
rpc_wallet_file)
if testnet: if testnet:
btc.select_chain_params("bitcoin/testnet") btc.select_chain_params("bitcoin/testnet")
else: else:
btc.select_chain_params("bitcoin") btc.select_chain_params("bitcoin")
elif source == 'regtest': elif source == 'regtest':
bc_interface = RegtestBitcoinCoreInterface(rpc) bc_interface = RegtestBitcoinCoreInterface(rpc,
rpc_wallet_file)
btc.select_chain_params("bitcoin/regtest") btc.select_chain_params("bitcoin/regtest")
elif source == "bitcoin-rpc-no-history": elif source == "bitcoin-rpc-no-history":
bc_interface = BitcoinCoreNoHistoryInterface(rpc, network) bc_interface = BitcoinCoreNoHistoryInterface(rpc, network,
rpc_wallet_file)
if testnet or network == "regtest": if testnet or network == "regtest":
# in tests, for bech32 regtest addresses, for bc-no-history, # in tests, for bech32 regtest addresses, for bc-no-history,
# this will have to be reset manually: # this will have to be reset manually:

13
jmclient/jmclient/jsonrpc.py

@ -54,21 +54,16 @@ class JsonRpc(object):
to connect to Bitcoin. 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.host = host
self.port = int(port) self.port = int(port)
self.conn = http.client.HTTPConnection(self.host, self.port) self.conn = http.client.HTTPConnection(self.host, self.port)
self.authstr = "%s:%s" % (user, password) self.authstr = "%s:%s" % (user, password)
if len(wallet_name) > 0: self.url = url
self.url = "/wallet/" + wallet_name
else:
self.url = ""
self.queryId = 1 self.queryId = 1
# Check that RPC wallet is loaded. If not, try to load it. def setURL(self, url):
loaded_wallets = self.call("listwallets", []) self.url = url
if not wallet_name in loaded_wallets:
self.call("loadwallet", [wallet_name])
def queryHTTP(self, obj): def queryHTTP(self, obj):
""" """

Loading…
Cancel
Save