Skip to content

Commit 3cf77f0

Browse files
committed
[tests] Remove deprecated addwitnessaddress call from wallet_dump.py
addwitnessaddress is deprecated. Remove the call to that RPC from wallet_dump.py and improve testing of all types of address (legacy, p2sh-segwit and bech32)
1 parent bdefc97 commit 3cf77f0

File tree

1 file changed

+41
-35
lines changed

1 file changed

+41
-35
lines changed

test/functional/wallet_dump.py

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ def read_dump(file_name, addrs, script_addrs, hd_master_addr_old):
1818
Also check that the old hd_master is inactive
1919
"""
2020
with open(file_name, encoding='utf8') as inputfile:
21-
found_addr = 0
21+
found_legacy_addr = 0
22+
found_p2sh_segwit_addr = 0
23+
found_bech32_addr = 0
2224
found_script_addr = 0
2325
found_addr_chg = 0
2426
found_addr_rsv = 0
25-
witness_addr_ret = None
2627
hd_master_addr_ret = None
2728
for line in inputfile:
2829
# only read non comment lines
@@ -59,14 +60,14 @@ def read_dump(file_name, addrs, script_addrs, hd_master_addr_old):
5960
# count key types
6061
for addrObj in addrs:
6162
if addrObj['address'] == addr.split(",")[0] and addrObj['hdkeypath'] == keypath and keytype == "label=":
62-
# a labeled entry in the wallet should contain both a native address
63-
# and the p2sh-p2wpkh address that was added at wallet setup
64-
if len(addr.split(",")) == 2:
65-
addr_list = addr.split(",")
66-
# the entry should be of the first key in the wallet
67-
assert_equal(addrs[0]['address'], addr_list[0])
68-
witness_addr_ret = addr_list[1]
69-
found_addr += 1
63+
if addr.startswith('m') or addr.startswith('n'):
64+
# P2PKH address
65+
found_legacy_addr += 1
66+
elif addr.startswith('2'):
67+
# P2SH-segwit address
68+
found_p2sh_segwit_addr += 1
69+
elif addr.startswith('bcrt1'):
70+
found_bech32_addr += 1
7071
break
7172
elif keytype == "change=1":
7273
found_addr_chg += 1
@@ -81,13 +82,13 @@ def read_dump(file_name, addrs, script_addrs, hd_master_addr_old):
8182
found_script_addr += 1
8283
break
8384

84-
return found_addr, found_script_addr, found_addr_chg, found_addr_rsv, hd_master_addr_ret, witness_addr_ret
85+
return found_legacy_addr, found_p2sh_segwit_addr, found_bech32_addr, found_script_addr, found_addr_chg, found_addr_rsv, hd_master_addr_ret
8586

8687

8788
class WalletDumpTest(BitcoinTestFramework):
8889
def set_test_params(self):
8990
self.num_nodes = 1
90-
self.extra_args = [["-keypool=90", "-addresstype=legacy", "-deprecatedrpc=addwitnessaddress"]]
91+
self.extra_args = [["-keypool=90", "-addresstype=legacy"]]
9192
self.rpc_timeout = 120
9293

9394
def skip_test_if_missing_module(self):
@@ -101,34 +102,38 @@ def run_test(self):
101102
wallet_unenc_dump = os.path.join(self.nodes[0].datadir, "wallet.unencrypted.dump")
102103
wallet_enc_dump = os.path.join(self.nodes[0].datadir, "wallet.encrypted.dump")
103104

104-
# generate 20 addresses to compare against the dump
105-
# but since we add a p2sh-p2wpkh address for the first pubkey in the
106-
# wallet, we will expect 21 addresses in the dump
107-
test_addr_count = 20
105+
# generate 30 addresses to compare against the dump
106+
# - 10 legacy P2PKH
107+
# - 10 P2SH-segwit
108+
# - 10 bech32
109+
test_addr_count = 10
108110
addrs = []
109-
for i in range(0, test_addr_count):
110-
addr = self.nodes[0].getnewaddress()
111-
vaddr = self.nodes[0].getaddressinfo(addr) # required to get hd keypath
112-
addrs.append(vaddr)
113-
# Should be a no-op:
114-
self.nodes[0].keypoolrefill()
111+
for address_type in ['legacy', 'p2sh-segwit', 'bech32']:
112+
for i in range(0, test_addr_count):
113+
addr = self.nodes[0].getnewaddress(address_type=address_type)
114+
vaddr = self.nodes[0].getaddressinfo(addr) # required to get hd keypath
115+
addrs.append(vaddr)
115116

116-
# Test scripts dump by adding a P2SH witness and a 1-of-1 multisig address
117-
witness_addr = self.nodes[0].addwitnessaddress(addrs[0]["address"], True)
117+
# Test scripts dump by adding a 1-of-1 multisig address
118118
multisig_addr = self.nodes[0].addmultisigaddress(1, [addrs[1]["address"]])["address"]
119-
script_addrs = [witness_addr, multisig_addr]
119+
120+
# Refill the keypool. getnewaddress() refills the keypool *before* taking a key from
121+
# the keypool, so the final call to getnewaddress leaves the keypool with one key below
122+
# its capacity
123+
self.nodes[0].keypoolrefill()
120124

121125
# dump unencrypted wallet
122126
result = self.nodes[0].dumpwallet(wallet_unenc_dump)
123127
assert_equal(result['filename'], wallet_unenc_dump)
124128

125-
found_addr, found_script_addr, found_addr_chg, found_addr_rsv, hd_master_addr_unenc, witness_addr_ret = \
126-
read_dump(wallet_unenc_dump, addrs, script_addrs, None)
127-
assert_equal(found_addr, test_addr_count) # all keys must be in the dump
128-
assert_equal(found_script_addr, 2) # all scripts must be in the dump
129+
found_legacy_addr, found_p2sh_segwit_addr, found_bech32_addr, found_script_addr, found_addr_chg, found_addr_rsv, hd_master_addr_unenc = \
130+
read_dump(wallet_unenc_dump, addrs, [multisig_addr], None)
131+
assert_equal(found_legacy_addr, test_addr_count) # all keys must be in the dump
132+
assert_equal(found_p2sh_segwit_addr, test_addr_count) # all keys must be in the dump
133+
assert_equal(found_bech32_addr, test_addr_count) # all keys must be in the dump
134+
assert_equal(found_script_addr, 1) # all scripts must be in the dump
129135
assert_equal(found_addr_chg, 0) # 0 blocks where mined
130136
assert_equal(found_addr_rsv, 90 * 2) # 90 keys plus 100% internal keys
131-
assert_equal(witness_addr_ret, witness_addr) # p2sh-p2wsh address added to the first key
132137

133138
# encrypt wallet, restart, unlock and dump
134139
self.nodes[0].encryptwallet('test')
@@ -137,13 +142,14 @@ def run_test(self):
137142
self.nodes[0].keypoolrefill()
138143
self.nodes[0].dumpwallet(wallet_enc_dump)
139144

140-
found_addr, found_script_addr, found_addr_chg, found_addr_rsv, _, witness_addr_ret = \
141-
read_dump(wallet_enc_dump, addrs, script_addrs, hd_master_addr_unenc)
142-
assert_equal(found_addr, test_addr_count)
143-
assert_equal(found_script_addr, 2)
145+
found_legacy_addr, found_p2sh_segwit_addr, found_bech32_addr, found_script_addr, found_addr_chg, found_addr_rsv, _ = \
146+
read_dump(wallet_enc_dump, addrs, [multisig_addr], hd_master_addr_unenc)
147+
assert_equal(found_legacy_addr, test_addr_count) # all keys must be in the dump
148+
assert_equal(found_p2sh_segwit_addr, test_addr_count) # all keys must be in the dump
149+
assert_equal(found_bech32_addr, test_addr_count) # all keys must be in the dump
150+
assert_equal(found_script_addr, 1)
144151
assert_equal(found_addr_chg, 90 * 2) # old reserve keys are marked as change now
145152
assert_equal(found_addr_rsv, 90 * 2)
146-
assert_equal(witness_addr_ret, witness_addr)
147153

148154
# Overwriting should fail
149155
assert_raises_rpc_error(-8, "already exists", lambda: self.nodes[0].dumpwallet(wallet_enc_dump))

0 commit comments

Comments
 (0)