mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 15:04:44 +01:00
tests: Add RPCOverloadWrapper which overloads some disabled RPCs
RPCOverloadWrapper overloads some deprecated or disabled RPCs with an implementation using other RPCs to avoid having a ton of code churn around replacing those RPCs.
This commit is contained in:
parent
cf06062859
commit
869f7ab30a
3 changed files with 125 additions and 24 deletions
|
@ -94,7 +94,7 @@ class BackwardsCompatibilityTest(BitcoinTestFramework):
|
||||||
|
|
||||||
# w1: regular wallet, created on master: update this test when default
|
# w1: regular wallet, created on master: update this test when default
|
||||||
# wallets can no longer be opened by older versions.
|
# wallets can no longer be opened by older versions.
|
||||||
node_master.createwallet(wallet_name="w1")
|
node_master.rpc.createwallet(wallet_name="w1")
|
||||||
wallet = node_master.get_wallet_rpc("w1")
|
wallet = node_master.get_wallet_rpc("w1")
|
||||||
info = wallet.getwalletinfo()
|
info = wallet.getwalletinfo()
|
||||||
assert info['private_keys_enabled']
|
assert info['private_keys_enabled']
|
||||||
|
@ -120,17 +120,17 @@ class BackwardsCompatibilityTest(BitcoinTestFramework):
|
||||||
self.nodes[1].abandontransaction(tx3_id)
|
self.nodes[1].abandontransaction(tx3_id)
|
||||||
|
|
||||||
# w1_v19: regular wallet, created with v0.19
|
# w1_v19: regular wallet, created with v0.19
|
||||||
node_v19.createwallet(wallet_name="w1_v19")
|
node_v19.rpc.createwallet(wallet_name="w1_v19")
|
||||||
wallet = node_v19.get_wallet_rpc("w1_v19")
|
wallet = node_v19.get_wallet_rpc("w1_v19")
|
||||||
info = wallet.getwalletinfo()
|
info = wallet.getwalletinfo()
|
||||||
assert info['private_keys_enabled']
|
assert info['private_keys_enabled']
|
||||||
assert info['keypoolsize'] > 0
|
assert info['keypoolsize'] > 0
|
||||||
# Use addmultisigaddress (see #18075)
|
# Use addmultisigaddress (see #18075)
|
||||||
address_18075 = wallet.addmultisigaddress(1, ["0296b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52", "037211a824f55b505228e4c3d5194c1fcfaa15a456abdf37f9b9d97a4040afc073"], "", "legacy")["address"]
|
address_18075 = wallet.rpc.addmultisigaddress(1, ["0296b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52", "037211a824f55b505228e4c3d5194c1fcfaa15a456abdf37f9b9d97a4040afc073"], "", "legacy")["address"]
|
||||||
assert wallet.getaddressinfo(address_18075)["solvable"]
|
assert wallet.getaddressinfo(address_18075)["solvable"]
|
||||||
|
|
||||||
# w1_v18: regular wallet, created with v0.18
|
# w1_v18: regular wallet, created with v0.18
|
||||||
node_v18.createwallet(wallet_name="w1_v18")
|
node_v18.rpc.createwallet(wallet_name="w1_v18")
|
||||||
wallet = node_v18.get_wallet_rpc("w1_v18")
|
wallet = node_v18.get_wallet_rpc("w1_v18")
|
||||||
info = wallet.getwalletinfo()
|
info = wallet.getwalletinfo()
|
||||||
assert info['private_keys_enabled']
|
assert info['private_keys_enabled']
|
||||||
|
@ -139,21 +139,21 @@ class BackwardsCompatibilityTest(BitcoinTestFramework):
|
||||||
# w2: wallet with private keys disabled, created on master: update this
|
# w2: wallet with private keys disabled, created on master: update this
|
||||||
# test when default wallets private keys disabled can no longer be
|
# test when default wallets private keys disabled can no longer be
|
||||||
# opened by older versions.
|
# opened by older versions.
|
||||||
node_master.createwallet(wallet_name="w2", disable_private_keys=True)
|
node_master.rpc.createwallet(wallet_name="w2", disable_private_keys=True)
|
||||||
wallet = node_master.get_wallet_rpc("w2")
|
wallet = node_master.get_wallet_rpc("w2")
|
||||||
info = wallet.getwalletinfo()
|
info = wallet.getwalletinfo()
|
||||||
assert info['private_keys_enabled'] == False
|
assert info['private_keys_enabled'] == False
|
||||||
assert info['keypoolsize'] == 0
|
assert info['keypoolsize'] == 0
|
||||||
|
|
||||||
# w2_v19: wallet with private keys disabled, created with v0.19
|
# w2_v19: wallet with private keys disabled, created with v0.19
|
||||||
node_v19.createwallet(wallet_name="w2_v19", disable_private_keys=True)
|
node_v19.rpc.createwallet(wallet_name="w2_v19", disable_private_keys=True)
|
||||||
wallet = node_v19.get_wallet_rpc("w2_v19")
|
wallet = node_v19.get_wallet_rpc("w2_v19")
|
||||||
info = wallet.getwalletinfo()
|
info = wallet.getwalletinfo()
|
||||||
assert info['private_keys_enabled'] == False
|
assert info['private_keys_enabled'] == False
|
||||||
assert info['keypoolsize'] == 0
|
assert info['keypoolsize'] == 0
|
||||||
|
|
||||||
# w2_v18: wallet with private keys disabled, created with v0.18
|
# w2_v18: wallet with private keys disabled, created with v0.18
|
||||||
node_v18.createwallet(wallet_name="w2_v18", disable_private_keys=True)
|
node_v18.rpc.createwallet(wallet_name="w2_v18", disable_private_keys=True)
|
||||||
wallet = node_v18.get_wallet_rpc("w2_v18")
|
wallet = node_v18.get_wallet_rpc("w2_v18")
|
||||||
info = wallet.getwalletinfo()
|
info = wallet.getwalletinfo()
|
||||||
assert info['private_keys_enabled'] == False
|
assert info['private_keys_enabled'] == False
|
||||||
|
@ -161,21 +161,21 @@ class BackwardsCompatibilityTest(BitcoinTestFramework):
|
||||||
|
|
||||||
# w3: blank wallet, created on master: update this
|
# w3: blank wallet, created on master: update this
|
||||||
# test when default blank wallets can no longer be opened by older versions.
|
# test when default blank wallets can no longer be opened by older versions.
|
||||||
node_master.createwallet(wallet_name="w3", blank=True)
|
node_master.rpc.createwallet(wallet_name="w3", blank=True)
|
||||||
wallet = node_master.get_wallet_rpc("w3")
|
wallet = node_master.get_wallet_rpc("w3")
|
||||||
info = wallet.getwalletinfo()
|
info = wallet.getwalletinfo()
|
||||||
assert info['private_keys_enabled']
|
assert info['private_keys_enabled']
|
||||||
assert info['keypoolsize'] == 0
|
assert info['keypoolsize'] == 0
|
||||||
|
|
||||||
# w3_v19: blank wallet, created with v0.19
|
# w3_v19: blank wallet, created with v0.19
|
||||||
node_v19.createwallet(wallet_name="w3_v19", blank=True)
|
node_v19.rpc.createwallet(wallet_name="w3_v19", blank=True)
|
||||||
wallet = node_v19.get_wallet_rpc("w3_v19")
|
wallet = node_v19.get_wallet_rpc("w3_v19")
|
||||||
info = wallet.getwalletinfo()
|
info = wallet.getwalletinfo()
|
||||||
assert info['private_keys_enabled']
|
assert info['private_keys_enabled']
|
||||||
assert info['keypoolsize'] == 0
|
assert info['keypoolsize'] == 0
|
||||||
|
|
||||||
# w3_v18: blank wallet, created with v0.18
|
# w3_v18: blank wallet, created with v0.18
|
||||||
node_v18.createwallet(wallet_name="w3_v18", blank=True)
|
node_v18.rpc.createwallet(wallet_name="w3_v18", blank=True)
|
||||||
wallet = node_v18.get_wallet_rpc("w3_v18")
|
wallet = node_v18.get_wallet_rpc("w3_v18")
|
||||||
info = wallet.getwalletinfo()
|
info = wallet.getwalletinfo()
|
||||||
assert info['private_keys_enabled']
|
assert info['private_keys_enabled']
|
||||||
|
@ -318,7 +318,7 @@ class BackwardsCompatibilityTest(BitcoinTestFramework):
|
||||||
|
|
||||||
self.log.info("Test wallet upgrade path...")
|
self.log.info("Test wallet upgrade path...")
|
||||||
# u1: regular wallet, created with v0.17
|
# u1: regular wallet, created with v0.17
|
||||||
node_v17.createwallet(wallet_name="u1_v17")
|
node_v17.rpc.createwallet(wallet_name="u1_v17")
|
||||||
wallet = node_v17.get_wallet_rpc("u1_v17")
|
wallet = node_v17.get_wallet_rpc("u1_v17")
|
||||||
address = wallet.getnewaddress("bech32")
|
address = wallet.getnewaddress("bech32")
|
||||||
info = wallet.getaddressinfo(address)
|
info = wallet.getaddressinfo(address)
|
||||||
|
|
|
@ -22,6 +22,7 @@ import shlex
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from .authproxy import JSONRPCException
|
from .authproxy import JSONRPCException
|
||||||
|
from .descriptors import descsum_create
|
||||||
from .util import (
|
from .util import (
|
||||||
MAX_NODES,
|
MAX_NODES,
|
||||||
append_config,
|
append_config,
|
||||||
|
@ -170,10 +171,10 @@ class TestNode():
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
"""Dispatches any unrecognised messages to the RPC connection or a CLI instance."""
|
"""Dispatches any unrecognised messages to the RPC connection or a CLI instance."""
|
||||||
if self.use_cli:
|
if self.use_cli:
|
||||||
return getattr(self.cli, name)
|
return getattr(RPCOverloadWrapper(self.cli, True), name)
|
||||||
else:
|
else:
|
||||||
assert self.rpc_connected and self.rpc is not None, self._node_msg("Error: no RPC connection")
|
assert self.rpc_connected and self.rpc is not None, self._node_msg("Error: no RPC connection")
|
||||||
return getattr(self.rpc, name)
|
return getattr(RPCOverloadWrapper(self.rpc), name)
|
||||||
|
|
||||||
def start(self, extra_args=None, *, cwd=None, stdout=None, stderr=None, **kwargs):
|
def start(self, extra_args=None, *, cwd=None, stdout=None, stderr=None, **kwargs):
|
||||||
"""Start the node."""
|
"""Start the node."""
|
||||||
|
@ -265,11 +266,11 @@ class TestNode():
|
||||||
|
|
||||||
def get_wallet_rpc(self, wallet_name):
|
def get_wallet_rpc(self, wallet_name):
|
||||||
if self.use_cli:
|
if self.use_cli:
|
||||||
return self.cli("-rpcwallet={}".format(wallet_name))
|
return RPCOverloadWrapper(self.cli("-rpcwallet={}".format(wallet_name)), True)
|
||||||
else:
|
else:
|
||||||
assert self.rpc_connected and self.rpc, self._node_msg("RPC not connected")
|
assert self.rpc_connected and self.rpc, self._node_msg("RPC not connected")
|
||||||
wallet_path = "wallet/{}".format(urllib.parse.quote(wallet_name))
|
wallet_path = "wallet/{}".format(urllib.parse.quote(wallet_name))
|
||||||
return self.rpc / wallet_path
|
return RPCOverloadWrapper(self.rpc / wallet_path)
|
||||||
|
|
||||||
def stop_node(self, expected_stderr='', wait=0):
|
def stop_node(self, expected_stderr='', wait=0):
|
||||||
"""Stop the node."""
|
"""Stop the node."""
|
||||||
|
@ -595,3 +596,103 @@ class TestNodeCLI():
|
||||||
return json.loads(cli_stdout, parse_float=decimal.Decimal)
|
return json.loads(cli_stdout, parse_float=decimal.Decimal)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
return cli_stdout.rstrip("\n")
|
return cli_stdout.rstrip("\n")
|
||||||
|
|
||||||
|
class RPCOverloadWrapper():
|
||||||
|
def __init__(self, rpc, cli=False):
|
||||||
|
self.rpc = rpc
|
||||||
|
self.is_cli = cli
|
||||||
|
|
||||||
|
def __getattr__(self, name):
|
||||||
|
return getattr(self.rpc, name)
|
||||||
|
|
||||||
|
def importprivkey(self, privkey, label=None, rescan=None):
|
||||||
|
wallet_info = self.getwalletinfo()
|
||||||
|
if self.is_cli:
|
||||||
|
if label is None:
|
||||||
|
label = 'null'
|
||||||
|
if rescan is None:
|
||||||
|
rescan = 'null'
|
||||||
|
if 'descriptors' not in wallet_info or ('descriptors' in wallet_info and not wallet_info['descriptors']):
|
||||||
|
return self.__getattr__('importprivkey')(privkey, label, rescan)
|
||||||
|
desc = descsum_create('combo(' + privkey + ')')
|
||||||
|
req = [{
|
||||||
|
'desc': desc,
|
||||||
|
'timestamp': 0 if rescan else 'now',
|
||||||
|
'label': label if label else ''
|
||||||
|
}]
|
||||||
|
import_res = self.importdescriptors(req)
|
||||||
|
if not import_res[0]['success']:
|
||||||
|
raise JSONRPCException(import_res[0]['error'])
|
||||||
|
|
||||||
|
def addmultisigaddress(self, nrequired, keys, label=None, address_type=None):
|
||||||
|
wallet_info = self.getwalletinfo()
|
||||||
|
if self.is_cli:
|
||||||
|
if label is None:
|
||||||
|
label = 'null'
|
||||||
|
if address_type is None:
|
||||||
|
address_type = 'null'
|
||||||
|
if 'descriptors' not in wallet_info or ('descriptors' in wallet_info and not wallet_info['descriptors']):
|
||||||
|
return self.__getattr__('addmultisigaddress')(nrequired, keys, label, address_type)
|
||||||
|
cms = self.createmultisig(nrequired, keys, address_type)
|
||||||
|
req = [{
|
||||||
|
'desc': cms['descriptor'],
|
||||||
|
'timestamp': 0,
|
||||||
|
'label': label if label else ''
|
||||||
|
}]
|
||||||
|
import_res = self.importdescriptors(req)
|
||||||
|
if not import_res[0]['success']:
|
||||||
|
raise JSONRPCException(import_res[0]['error'])
|
||||||
|
return cms
|
||||||
|
|
||||||
|
def importpubkey(self, pubkey, label=None, rescan=None):
|
||||||
|
wallet_info = self.getwalletinfo()
|
||||||
|
if self.is_cli:
|
||||||
|
if label is None:
|
||||||
|
label = 'null'
|
||||||
|
if rescan is None:
|
||||||
|
rescan = 'null'
|
||||||
|
if 'descriptors' not in wallet_info or ('descriptors' in wallet_info and not wallet_info['descriptors']):
|
||||||
|
return self.__getattr__('importpubkey')(pubkey, label, rescan)
|
||||||
|
desc = descsum_create('combo(' + pubkey + ')')
|
||||||
|
req = [{
|
||||||
|
'desc': desc,
|
||||||
|
'timestamp': 0 if rescan else 'now',
|
||||||
|
'label': label if label else ''
|
||||||
|
}]
|
||||||
|
import_res = self.importdescriptors(req)
|
||||||
|
if not import_res[0]['success']:
|
||||||
|
raise JSONRPCException(import_res[0]['error'])
|
||||||
|
|
||||||
|
def importaddress(self, address, label=None, rescan=None, p2sh=None):
|
||||||
|
wallet_info = self.getwalletinfo()
|
||||||
|
if self.is_cli:
|
||||||
|
if label is None:
|
||||||
|
label = 'null'
|
||||||
|
if rescan is None:
|
||||||
|
rescan = 'null'
|
||||||
|
if p2sh is None:
|
||||||
|
p2sh = 'null'
|
||||||
|
if 'descriptors' not in wallet_info or ('descriptors' in wallet_info and not wallet_info['descriptors']):
|
||||||
|
return self.__getattr__('importaddress')(address, label, rescan, p2sh)
|
||||||
|
is_hex = False
|
||||||
|
try:
|
||||||
|
int(address ,16)
|
||||||
|
is_hex = True
|
||||||
|
desc = descsum_create('raw(' + address + ')')
|
||||||
|
except:
|
||||||
|
desc = descsum_create('addr(' + address + ')')
|
||||||
|
reqs = [{
|
||||||
|
'desc': desc,
|
||||||
|
'timestamp': 0 if rescan else 'now',
|
||||||
|
'label': label if label else ''
|
||||||
|
}]
|
||||||
|
if is_hex and p2sh:
|
||||||
|
reqs.append({
|
||||||
|
'desc': descsum_create('p2sh(raw(' + address + '))'),
|
||||||
|
'timestamp': 0 if rescan else 'now',
|
||||||
|
'label': label if label else ''
|
||||||
|
})
|
||||||
|
import_res = self.importdescriptors(reqs)
|
||||||
|
for res in import_res:
|
||||||
|
if not res['success']:
|
||||||
|
raise JSONRPCException(res['error'])
|
||||||
|
|
|
@ -81,15 +81,15 @@ class WalletDescriptorTest(BitcoinTestFramework):
|
||||||
|
|
||||||
# Make sure things are disabled
|
# Make sure things are disabled
|
||||||
self.log.info("Test disabled RPCs")
|
self.log.info("Test disabled RPCs")
|
||||||
assert_raises_rpc_error(-4, "This type of wallet does not support this command", recv_wrpc.importprivkey, "cVpF924EspNh8KjYsfhgY96mmxvT6DgdWiTYMtMjuM74hJaU5psW")
|
assert_raises_rpc_error(-4, "This type of wallet does not support this command", recv_wrpc.rpc.importprivkey, "cVpF924EspNh8KjYsfhgY96mmxvT6DgdWiTYMtMjuM74hJaU5psW")
|
||||||
assert_raises_rpc_error(-4, "This type of wallet does not support this command", recv_wrpc.importpubkey, send_wrpc.getaddressinfo(send_wrpc.getnewaddress()))
|
assert_raises_rpc_error(-4, "This type of wallet does not support this command", recv_wrpc.rpc.importpubkey, send_wrpc.getaddressinfo(send_wrpc.getnewaddress()))
|
||||||
assert_raises_rpc_error(-4, "This type of wallet does not support this command", recv_wrpc.importaddress, recv_wrpc.getnewaddress())
|
assert_raises_rpc_error(-4, "This type of wallet does not support this command", recv_wrpc.rpc.importaddress, recv_wrpc.getnewaddress())
|
||||||
assert_raises_rpc_error(-4, "This type of wallet does not support this command", recv_wrpc.importmulti, [])
|
assert_raises_rpc_error(-4, "This type of wallet does not support this command", recv_wrpc.rpc.importmulti, [])
|
||||||
assert_raises_rpc_error(-4, "This type of wallet does not support this command", recv_wrpc.addmultisigaddress, 1, [recv_wrpc.getnewaddress()])
|
assert_raises_rpc_error(-4, "This type of wallet does not support this command", recv_wrpc.rpc.addmultisigaddress, 1, [recv_wrpc.getnewaddress()])
|
||||||
assert_raises_rpc_error(-4, "This type of wallet does not support this command", recv_wrpc.dumpprivkey, recv_wrpc.getnewaddress())
|
assert_raises_rpc_error(-4, "This type of wallet does not support this command", recv_wrpc.rpc.dumpprivkey, recv_wrpc.getnewaddress())
|
||||||
assert_raises_rpc_error(-4, "This type of wallet does not support this command", recv_wrpc.dumpwallet, 'wallet.dump')
|
assert_raises_rpc_error(-4, "This type of wallet does not support this command", recv_wrpc.rpc.dumpwallet, 'wallet.dump')
|
||||||
assert_raises_rpc_error(-4, "This type of wallet does not support this command", recv_wrpc.importwallet, 'wallet.dump')
|
assert_raises_rpc_error(-4, "This type of wallet does not support this command", recv_wrpc.rpc.importwallet, 'wallet.dump')
|
||||||
assert_raises_rpc_error(-4, "This type of wallet does not support this command", recv_wrpc.sethdseed)
|
assert_raises_rpc_error(-4, "This type of wallet does not support this command", recv_wrpc.rpc.sethdseed)
|
||||||
|
|
||||||
self.log.info("Test encryption")
|
self.log.info("Test encryption")
|
||||||
# Get the master fingerprint before encrypt
|
# Get the master fingerprint before encrypt
|
||||||
|
|
Loading…
Add table
Reference in a new issue