Skip to content

Commit 8234cda

Browse files
committed
Merge bitcoin/bitcoin#24587: test: use MiniWallet for rpc_createmultisig.py
2726b60 test: use MiniWallet for rpc_createmultisig.py (Ayush Sharma) Pull request description: This PR enables one of the non-wallet functional tests (rpc_createmultisig.py) to be run even with the Bitcoin Core wallet disabled by using the MiniWallet instead, as proposed in #20078 . ACKs for top commit: danielabrozzoni: re-ACK 2726b60 Tree-SHA512: fb0ef22d3f1c161ca5963cb19ce76533ac3941f15102fc0aa2286ef3bec48f219e5934d504b41976f9f295fb6ca582b737e0fea896df4eb964cdaba1b2c91650
2 parents 6fee9de + 2726b60 commit 8234cda

File tree

3 files changed

+63
-46
lines changed

3 files changed

+63
-46
lines changed

test/functional/rpc_createmultisig.py

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@
1818
assert_equal,
1919
)
2020
from test_framework.wallet_util import bytes_to_wif
21+
from test_framework.wallet import (
22+
MiniWallet,
23+
getnewdestination,
24+
)
2125

2226
class RpcCreateMultiSigTest(BitcoinTestFramework):
2327
def set_test_params(self):
2428
self.setup_clean_chain = True
2529
self.num_nodes = 3
2630
self.supports_cli = False
27-
28-
def skip_test_if_missing_module(self):
29-
self.skip_if_no_wallet()
31+
if self.is_bdb_compiled():
32+
self.requires_wallet = True
3033

3134
def get_keys(self):
3235
self.pub = []
@@ -37,57 +40,66 @@ def get_keys(self):
3740
k.generate()
3841
self.pub.append(k.get_pubkey().get_bytes().hex())
3942
self.priv.append(bytes_to_wif(k.get_bytes(), k.is_compressed))
40-
self.final = node2.getnewaddress()
43+
if self.is_bdb_compiled():
44+
self.final = node2.getnewaddress()
45+
else:
46+
self.final = getnewdestination()[2]
4147

4248
def run_test(self):
4349
node0, node1, node2 = self.nodes
50+
self.wallet = MiniWallet(test_node=node0)
4451

45-
self.check_addmultisigaddress_errors()
52+
if self.is_bdb_compiled():
53+
self.check_addmultisigaddress_errors()
4654

4755
self.log.info('Generating blocks ...')
48-
self.generate(node0, 149)
56+
self.generate(self.wallet, 149)
4957

5058
self.moved = 0
5159
for self.nkeys in [3, 5]:
5260
for self.nsigs in [2, 3]:
5361
for self.output_type in ["bech32", "p2sh-segwit", "legacy"]:
5462
self.get_keys()
5563
self.do_multisig()
56-
57-
self.checkbalances()
64+
if self.is_bdb_compiled():
65+
self.checkbalances()
5866

5967
# Test mixed compressed and uncompressed pubkeys
6068
self.log.info('Mixed compressed and uncompressed multisigs are not allowed')
61-
pk0 = node0.getaddressinfo(node0.getnewaddress())['pubkey']
62-
pk1 = node1.getaddressinfo(node1.getnewaddress())['pubkey']
63-
pk2 = node2.getaddressinfo(node2.getnewaddress())['pubkey']
69+
pk0 = getnewdestination()[0].hex()
70+
pk1 = getnewdestination()[0].hex()
71+
pk2 = getnewdestination()[0].hex()
6472

6573
# decompress pk2
6674
pk_obj = ECPubKey()
6775
pk_obj.set(bytes.fromhex(pk2))
6876
pk_obj.compressed = False
6977
pk2 = pk_obj.get_bytes().hex()
7078

71-
node0.createwallet(wallet_name='wmulti0', disable_private_keys=True)
72-
wmulti0 = node0.get_wallet_rpc('wmulti0')
79+
if self.is_bdb_compiled():
80+
node0.createwallet(wallet_name='wmulti0', disable_private_keys=True)
81+
wmulti0 = node0.get_wallet_rpc('wmulti0')
7382

7483
# Check all permutations of keys because order matters apparently
7584
for keys in itertools.permutations([pk0, pk1, pk2]):
7685
# Results should be the same as this legacy one
7786
legacy_addr = node0.createmultisig(2, keys, 'legacy')['address']
78-
result = wmulti0.addmultisigaddress(2, keys, '', 'legacy')
79-
assert_equal(legacy_addr, result['address'])
80-
assert 'warnings' not in result
87+
88+
if self.is_bdb_compiled():
89+
result = wmulti0.addmultisigaddress(2, keys, '', 'legacy')
90+
assert_equal(legacy_addr, result['address'])
91+
assert 'warnings' not in result
8192

8293
# Generate addresses with the segwit types. These should all make legacy addresses
8394
for addr_type in ['bech32', 'p2sh-segwit']:
84-
result = wmulti0.createmultisig(2, keys, addr_type)
95+
result = self.nodes[0].createmultisig(2, keys, addr_type)
8596
assert_equal(legacy_addr, result['address'])
8697
assert_equal(result['warnings'], ["Unable to make chosen address type, please ensure no uncompressed public keys are present."])
8798

88-
result = wmulti0.addmultisigaddress(2, keys, '', addr_type)
89-
assert_equal(legacy_addr, result['address'])
90-
assert_equal(result['warnings'], ["Unable to make chosen address type, please ensure no uncompressed public keys are present."])
99+
if self.is_bdb_compiled():
100+
result = wmulti0.addmultisigaddress(2, keys, '', addr_type)
101+
assert_equal(legacy_addr, result['address'])
102+
assert_equal(result['warnings'], ["Unable to make chosen address type, please ensure no uncompressed public keys are present."])
91103

92104
self.log.info('Testing sortedmulti descriptors with BIP 67 test vectors')
93105
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data/rpc_bip67.json'), encoding='utf-8') as f:
@@ -126,26 +138,29 @@ def checkbalances(self):
126138
bal0 = node0.getbalance()
127139
bal1 = node1.getbalance()
128140
bal2 = node2.getbalance()
141+
balw = self.wallet.get_balance()
129142

130143
height = node0.getblockchaininfo()["blocks"]
131144
assert 150 < height < 350
132145
total = 149 * 50 + (height - 149 - 100) * 25
133146
assert bal1 == 0
134147
assert bal2 == self.moved
135-
assert bal0 + bal1 + bal2 == total
148+
assert_equal(bal0 + bal1 + bal2 + balw, total)
136149

137150
def do_multisig(self):
138151
node0, node1, node2 = self.nodes
139-
if 'wmulti' not in node1.listwallets():
140-
try:
141-
node1.loadwallet('wmulti')
142-
except JSONRPCException as e:
143-
path = os.path.join(self.options.tmpdir, "node1", "regtest", "wallets", "wmulti")
144-
if e.error['code'] == -18 and "Wallet file verification failed. Failed to load database path '{}'. Path does not exist.".format(path) in e.error['message']:
145-
node1.createwallet(wallet_name='wmulti', disable_private_keys=True)
146-
else:
147-
raise
148-
wmulti = node1.get_wallet_rpc('wmulti')
152+
153+
if self.is_bdb_compiled():
154+
if 'wmulti' not in node1.listwallets():
155+
try:
156+
node1.loadwallet('wmulti')
157+
except JSONRPCException as e:
158+
path = os.path.join(self.options.tmpdir, "node1", "regtest", "wallets", "wmulti")
159+
if e.error['code'] == -18 and "Wallet file verification failed. Failed to load database path '{}'. Path does not exist.".format(path) in e.error['message']:
160+
node1.createwallet(wallet_name='wmulti', disable_private_keys=True)
161+
else:
162+
raise
163+
wmulti = node1.get_wallet_rpc('wmulti')
149164

150165
# Construct the expected descriptor
151166
desc = 'multi({},{})'.format(self.nsigs, ','.join(self.pub))
@@ -164,17 +179,19 @@ def do_multisig(self):
164179
if self.output_type == 'bech32':
165180
assert madd[0:4] == "bcrt" # actually a bech32 address
166181

167-
# compare against addmultisigaddress
168-
msigw = wmulti.addmultisigaddress(self.nsigs, self.pub, None, self.output_type)
169-
maddw = msigw["address"]
170-
mredeemw = msigw["redeemScript"]
171-
assert_equal(desc, drop_origins(msigw['descriptor']))
172-
# addmultisigiaddress and createmultisig work the same
173-
assert maddw == madd
174-
assert mredeemw == mredeem
175-
176-
txid = node0.sendtoaddress(madd, 40)
177-
182+
if self.is_bdb_compiled():
183+
# compare against addmultisigaddress
184+
msigw = wmulti.addmultisigaddress(self.nsigs, self.pub, None, self.output_type)
185+
maddw = msigw["address"]
186+
mredeemw = msigw["redeemScript"]
187+
assert_equal(desc, drop_origins(msigw['descriptor']))
188+
# addmultisigiaddress and createmultisig work the same
189+
assert maddw == madd
190+
assert mredeemw == mredeem
191+
wmulti.unloadwallet()
192+
193+
spk = bytes.fromhex(node0.validateaddress(madd)["scriptPubKey"])
194+
txid, _ = self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=spk, amount=1300)
178195
tx = node0.getrawtransaction(txid, True)
179196
vout = [v["n"] for v in tx["vout"] if madd == v["scriptPubKey"]["address"]]
180197
assert len(vout) == 1
@@ -225,8 +242,6 @@ def do_multisig(self):
225242
txinfo = node0.getrawtransaction(tx, True, blk)
226243
self.log.info("n/m=%d/%d %s size=%d vsize=%d weight=%d" % (self.nsigs, self.nkeys, self.output_type, txinfo["size"], txinfo["vsize"], txinfo["weight"]))
227244

228-
wmulti.unloadwallet()
229-
230245

231246
if __name__ == '__main__':
232247
RpcCreateMultiSigTest().main()

test/functional/test_framework/wallet.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ def __init__(self, test_node, *, mode=MiniWalletMode.ADDRESS_OP_TRUE):
9696
self._address, self._internal_key = create_deterministic_address_bcrt1_p2tr_op_true()
9797
self._scriptPubKey = bytes.fromhex(self._test_node.validateaddress(self._address)['scriptPubKey'])
9898

99+
def get_balance(self):
100+
return sum(u['value'] for u in self._utxos)
101+
99102
def rescan_utxos(self):
100103
"""Drop all utxos and rescan the utxo set"""
101104
self._utxos = []

test/functional/test_runner.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@
224224
'feature_rbf.py --descriptors',
225225
'mempool_packages.py',
226226
'mempool_package_onemore.py',
227-
'rpc_createmultisig.py --legacy-wallet',
228-
'rpc_createmultisig.py --descriptors',
227+
'rpc_createmultisig.py',
229228
'rpc_packages.py',
230229
'mempool_package_limits.py',
231230
'feature_versionbits_warning.py',

0 commit comments

Comments
 (0)