4
4
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
5
6
6
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
10
53
11
54
12
55
class WalletDumpTest (BitcoinTestFramework ):
@@ -15,106 +58,47 @@ def __init__(self):
15
58
super ().__init__ ()
16
59
self .setup_clean_chain = False
17
60
self .num_nodes = 1
61
+ self .extra_args = [["-keypool=90" ]]
18
62
19
63
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 )
22
65
23
66
def run_test (self ):
24
67
tmpdir = self .options .tmpdir
25
68
26
- #generate 20 addresses to compare against the dump
69
+ # generate 20 addresses to compare against the dump
27
70
test_addr_count = 20
28
71
addrs = []
29
72
for i in range (0 ,test_addr_count ):
30
73
addr = self .nodes [0 ].getnewaddress ()
31
74
vaddr = self .nodes [0 ].validateaddress (addr ) #required to get hd keypath
32
75
addrs .append (vaddr )
76
+ # Should be a no-op:
77
+ self .nodes [0 ].keypoolrefill ()
33
78
34
79
# dump unencrypted wallet
35
80
self .nodes [0 ].dumpwallet (tmpdir + "/node0/wallet.unencrypted.dump" )
36
81
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)
74
87
75
88
#encrypt wallet, restart, unlock and dump
76
89
self .nodes [0 ].encryptwallet ('test' )
77
90
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 ] )
79
92
self .nodes [0 ].walletpassphrase ('test' , 10 )
93
+ # Should be a no-op:
94
+ self .nodes [0 ].keypoolrefill ()
80
95
self .nodes [0 ].dumpwallet (tmpdir + "/node0/wallet.encrypted.dump" )
81
96
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)
118
102
119
103
if __name__ == '__main__' :
120
104
WalletDumpTest ().main ()
0 commit comments