Skip to content

Commit 2eed09f

Browse files
committed
Merge #8442: [qa] Rework hd wallet dump test
fa4439d [qa] Rework hd wallet dump test (MarcoFalke)
2 parents 63c03dd + fa4439d commit 2eed09f

File tree

1 file changed

+64
-80
lines changed

1 file changed

+64
-80
lines changed

qa/rpc-tests/wallet-dump.py

Lines changed: 64 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,52 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
from test_framework.test_framework import BitcoinTestFramework
7-
from test_framework.util import *
8-
import os
9-
import shutil
7+
from test_framework.util import (start_nodes, start_node, assert_equal, bitcoind_processes)
8+
9+
10+
def read_dump(file_name, addrs, hd_master_addr_old):
11+
"""
12+
Read the given dump, count the addrs that match, count change and reserve.
13+
Also check that the old hd_master is inactive
14+
"""
15+
with open(file_name) as inputfile:
16+
found_addr = 0
17+
found_addr_chg = 0
18+
found_addr_rsv = 0
19+
hd_master_addr_ret = None
20+
for line in inputfile:
21+
# only read non comment lines
22+
if line[0] != "#" and len(line) > 10:
23+
# split out some data
24+
key_label, comment = line.split("#")
25+
# key = key_label.split(" ")[0]
26+
keytype = key_label.split(" ")[2]
27+
if len(comment) > 1:
28+
addr_keypath = comment.split(" addr=")[1]
29+
addr = addr_keypath.split(" ")[0]
30+
keypath = None
31+
if keytype == "inactivehdmaster=1":
32+
# ensure the old master is still available
33+
assert(hd_master_addr_old == addr)
34+
elif keytype == "hdmaster=1":
35+
# ensure we have generated a new hd master key
36+
assert(hd_master_addr_old != addr)
37+
hd_master_addr_ret = addr
38+
else:
39+
keypath = addr_keypath.rstrip().split("hdkeypath=")[1]
40+
41+
# count key types
42+
for addrObj in addrs:
43+
if addrObj['address'] == addr and addrObj['hdkeypath'] == keypath and keytype == "label=":
44+
found_addr += 1
45+
break
46+
elif keytype == "change=1":
47+
found_addr_chg += 1
48+
break
49+
elif keytype == "reserve=1":
50+
found_addr_rsv += 1
51+
break
52+
return found_addr, found_addr_chg, found_addr_rsv, hd_master_addr_ret
1053

1154

1255
class WalletDumpTest(BitcoinTestFramework):
@@ -15,106 +58,47 @@ def __init__(self):
1558
super().__init__()
1659
self.setup_clean_chain = False
1760
self.num_nodes = 1
61+
self.extra_args = [["-keypool=90"]]
1862

1963
def setup_network(self, split=False):
20-
extra_args = [["-keypool=100"]]
21-
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, extra_args)
64+
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, self.extra_args)
2265

2366
def run_test (self):
2467
tmpdir = self.options.tmpdir
2568

26-
#generate 20 addresses to compare against the dump
69+
# generate 20 addresses to compare against the dump
2770
test_addr_count = 20
2871
addrs = []
2972
for i in range(0,test_addr_count):
3073
addr = self.nodes[0].getnewaddress()
3174
vaddr= self.nodes[0].validateaddress(addr) #required to get hd keypath
3275
addrs.append(vaddr)
76+
# Should be a no-op:
77+
self.nodes[0].keypoolrefill()
3378

3479
# dump unencrypted wallet
3580
self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.unencrypted.dump")
3681

37-
#open file
38-
inputfile = open(tmpdir + "/node0/wallet.unencrypted.dump")
39-
found_addr = 0
40-
found_addr_chg = 0
41-
found_addr_rsv = 0
42-
hdmasteraddr = ""
43-
for line in inputfile:
44-
#only read non comment lines
45-
if line[0] != "#" and len(line) > 10:
46-
#split out some data
47-
keyLabel, comment = line.split("#")
48-
key = keyLabel.split(" ")[0]
49-
keytype = keyLabel.split(" ")[2]
50-
if len(comment) > 1:
51-
addrKeypath = comment.split(" addr=")[1]
52-
addr = addrKeypath.split(" ")[0]
53-
keypath = ""
54-
if keytype != "hdmaster=1":
55-
keypath = addrKeypath.rstrip().split("hdkeypath=")[1]
56-
else:
57-
#keep hd master for later comp.
58-
hdmasteraddr = addr
59-
60-
#count key types
61-
for addrObj in addrs:
62-
if (addrObj['address'] == addr and addrObj['hdkeypath'] == keypath and keytype == "label="):
63-
found_addr+=1
64-
break
65-
elif (keytype == "change=1"):
66-
found_addr_chg+=1
67-
break
68-
elif (keytype == "reserve=1"):
69-
found_addr_rsv+=1
70-
break
71-
assert(found_addr == test_addr_count) #all keys must be in the dump
72-
assert(found_addr_chg == 50) #50 blocks where mined
73-
assert(found_addr_rsv == 100) #100 reserve keys (keypool)
82+
found_addr, found_addr_chg, found_addr_rsv, hd_master_addr_unenc = \
83+
read_dump(tmpdir + "/node0/wallet.unencrypted.dump", addrs, None)
84+
assert_equal(found_addr, test_addr_count) # all keys must be in the dump
85+
assert_equal(found_addr_chg, 50) # 50 blocks where mined
86+
assert_equal(found_addr_rsv, 90 + 1) # keypool size (TODO: fix off-by-one)
7487

7588
#encrypt wallet, restart, unlock and dump
7689
self.nodes[0].encryptwallet('test')
7790
bitcoind_processes[0].wait()
78-
self.nodes[0] = start_node(0, self.options.tmpdir)
91+
self.nodes[0] = start_node(0, self.options.tmpdir, self.extra_args[0])
7992
self.nodes[0].walletpassphrase('test', 10)
93+
# Should be a no-op:
94+
self.nodes[0].keypoolrefill()
8095
self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.encrypted.dump")
8196

82-
#open dump done with an encrypted wallet
83-
inputfile = open(tmpdir + "/node0/wallet.encrypted.dump")
84-
found_addr = 0
85-
found_addr_chg = 0
86-
found_addr_rsv = 0
87-
for line in inputfile:
88-
if line[0] != "#" and len(line) > 10:
89-
keyLabel, comment = line.split("#")
90-
key = keyLabel.split(" ")[0]
91-
keytype = keyLabel.split(" ")[2]
92-
if len(comment) > 1:
93-
addrKeypath = comment.split(" addr=")[1]
94-
addr = addrKeypath.split(" ")[0]
95-
keypath = ""
96-
if keytype != "hdmaster=1":
97-
keypath = addrKeypath.rstrip().split("hdkeypath=")[1]
98-
else:
99-
#ensure we have generated a new hd master key
100-
assert(hdmasteraddr != addr)
101-
if keytype == "inactivehdmaster=1":
102-
#ensure the old master is still available
103-
assert(hdmasteraddr == addr)
104-
for addrObj in addrs:
105-
if (addrObj['address'] == addr and addrObj['hdkeypath'] == keypath and keytype == "label="):
106-
found_addr+=1
107-
break
108-
elif (keytype == "change=1"):
109-
found_addr_chg+=1
110-
break
111-
elif (keytype == "reserve=1"):
112-
found_addr_rsv+=1
113-
break
114-
115-
assert(found_addr == test_addr_count)
116-
assert(found_addr_chg == 150) #old reserve keys are marked as change now
117-
assert(found_addr_rsv == 100) #keypool size
97+
found_addr, found_addr_chg, found_addr_rsv, hd_master_addr_enc = \
98+
read_dump(tmpdir + "/node0/wallet.encrypted.dump", addrs, hd_master_addr_unenc)
99+
assert_equal(found_addr, test_addr_count)
100+
assert_equal(found_addr_chg, 90 + 1 + 50) # old reserve keys are marked as change now
101+
assert_equal(found_addr_rsv, 90 + 1) # keypool size (TODO: fix off-by-one)
118102

119103
if __name__ == '__main__':
120104
WalletDumpTest().main ()

0 commit comments

Comments
 (0)