3
3
# Distributed under the MIT software license, see the accompanying
4
4
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
5
"""Test the dumpwallet RPC."""
6
+ import datetime
6
7
import os
8
+ import time
7
9
8
10
from test_framework .test_framework import BitcoinTestFramework
9
11
from test_framework .util import (
@@ -18,6 +20,7 @@ def read_dump(file_name, addrs, script_addrs, hd_master_addr_old):
18
20
Also check that the old hd_master is inactive
19
21
"""
20
22
with open (file_name , encoding = 'utf8' ) as inputfile :
23
+ found_comments = []
21
24
found_legacy_addr = 0
22
25
found_p2sh_segwit_addr = 0
23
26
found_bech32_addr = 0
@@ -26,8 +29,12 @@ def read_dump(file_name, addrs, script_addrs, hd_master_addr_old):
26
29
found_addr_rsv = 0
27
30
hd_master_addr_ret = None
28
31
for line in inputfile :
29
- # only read non comment lines
30
- if line [0 ] != "#" and len (line ) > 10 :
32
+ line = line .strip ()
33
+ if not line :
34
+ continue
35
+ if line [0 ] == '#' :
36
+ found_comments .append (line )
37
+ else :
31
38
# split out some data
32
39
key_date_label , comment = line .split ("#" )
33
40
key_date_label = key_date_label .split (" " )
@@ -82,7 +89,7 @@ def read_dump(file_name, addrs, script_addrs, hd_master_addr_old):
82
89
found_script_addr += 1
83
90
break
84
91
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
92
+ return found_comments , found_legacy_addr , found_p2sh_segwit_addr , found_bech32_addr , found_script_addr , found_addr_chg , found_addr_rsv , hd_master_addr_ret
86
93
87
94
88
95
class WalletDumpTest (BitcoinTestFramework ):
@@ -122,12 +129,36 @@ def run_test(self):
122
129
# its capacity
123
130
self .nodes [0 ].keypoolrefill ()
124
131
125
- # dump unencrypted wallet
132
+ self .log .info ('Mine a block one second before the wallet is dumped' )
133
+ dump_time = int (time .time ())
134
+ self .nodes [0 ].setmocktime (dump_time - 1 )
135
+ self .nodes [0 ].generate (1 )
136
+ self .nodes [0 ].setmocktime (dump_time )
137
+ dump_time_str = '# * Created on {}Z' .format (
138
+ datetime .datetime .fromtimestamp (
139
+ dump_time ,
140
+ tz = datetime .timezone .utc ,
141
+ ).replace (tzinfo = None ).isoformat ())
142
+ dump_best_block_1 = '# * Best block at time of backup was {} ({}),' .format (
143
+ self .nodes [0 ].getblockcount (),
144
+ self .nodes [0 ].getbestblockhash (),
145
+ )
146
+ dump_best_block_2 = '# mined on {}Z' .format (
147
+ datetime .datetime .fromtimestamp (
148
+ dump_time - 1 ,
149
+ tz = datetime .timezone .utc ,
150
+ ).replace (tzinfo = None ).isoformat ())
151
+
152
+ self .log .info ('Dump unencrypted wallet' )
126
153
result = self .nodes [0 ].dumpwallet (wallet_unenc_dump )
127
154
assert_equal (result ['filename' ], wallet_unenc_dump )
128
155
129
- found_legacy_addr , found_p2sh_segwit_addr , found_bech32_addr , found_script_addr , found_addr_chg , found_addr_rsv , hd_master_addr_unenc = \
156
+ found_comments , found_legacy_addr , found_p2sh_segwit_addr , found_bech32_addr , found_script_addr , found_addr_chg , found_addr_rsv , hd_master_addr_unenc = \
130
157
read_dump (wallet_unenc_dump , addrs , [multisig_addr ], None )
158
+ assert '# End of dump' in found_comments # Check that file is not corrupt
159
+ assert_equal (dump_time_str , next (c for c in found_comments if c .startswith ('# * Created on' )))
160
+ assert_equal (dump_best_block_1 , next (c for c in found_comments if c .startswith ('# * Best block' )))
161
+ assert_equal (dump_best_block_2 , next (c for c in found_comments if c .startswith ('# mined on' )))
131
162
assert_equal (found_legacy_addr , test_addr_count ) # all keys must be in the dump
132
163
assert_equal (found_p2sh_segwit_addr , test_addr_count ) # all keys must be in the dump
133
164
assert_equal (found_bech32_addr , test_addr_count ) # all keys must be in the dump
@@ -142,8 +173,12 @@ def run_test(self):
142
173
self .nodes [0 ].keypoolrefill ()
143
174
self .nodes [0 ].dumpwallet (wallet_enc_dump )
144
175
145
- found_legacy_addr , found_p2sh_segwit_addr , found_bech32_addr , found_script_addr , found_addr_chg , found_addr_rsv , _ = \
176
+ found_comments , found_legacy_addr , found_p2sh_segwit_addr , found_bech32_addr , found_script_addr , found_addr_chg , found_addr_rsv , _ = \
146
177
read_dump (wallet_enc_dump , addrs , [multisig_addr ], hd_master_addr_unenc )
178
+ assert '# End of dump' in found_comments # Check that file is not corrupt
179
+ assert_equal (dump_time_str , next (c for c in found_comments if c .startswith ('# * Created on' )))
180
+ assert_equal (dump_best_block_1 , next (c for c in found_comments if c .startswith ('# * Best block' )))
181
+ assert_equal (dump_best_block_2 , next (c for c in found_comments if c .startswith ('# mined on' )))
147
182
assert_equal (found_legacy_addr , test_addr_count ) # all keys must be in the dump
148
183
assert_equal (found_p2sh_segwit_addr , test_addr_count ) # all keys must be in the dump
149
184
assert_equal (found_bech32_addr , test_addr_count ) # all keys must be in the dump
0 commit comments