scripted-diff: Use the C++11 keyword nullptr to denote the pointer literal instead...
[bitcoinplatinum.git] / test / functional / wallet-dump.py
blob569cc46e6cf92cea3c3e3d991673f8710056c825
1 #!/usr/bin/env python3
2 # Copyright (c) 2016 The Bitcoin Core developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 """Test the dumpwallet RPC."""
7 import os
9 from test_framework.test_framework import BitcoinTestFramework
10 from test_framework.util import assert_equal
13 def read_dump(file_name, addrs, hd_master_addr_old):
14 """
15 Read the given dump, count the addrs that match, count change and reserve.
16 Also check that the old hd_master is inactive
17 """
18 with open(file_name, encoding='utf8') as inputfile:
19 found_addr = 0
20 found_addr_chg = 0
21 found_addr_rsv = 0
22 hd_master_addr_ret = None
23 for line in inputfile:
24 # only read non comment lines
25 if line[0] != "#" and len(line) > 10:
26 # split out some data
27 key_label, comment = line.split("#")
28 # key = key_label.split(" ")[0]
29 keytype = key_label.split(" ")[2]
30 if len(comment) > 1:
31 addr_keypath = comment.split(" addr=")[1]
32 addr = addr_keypath.split(" ")[0]
33 keypath = None
34 if keytype == "inactivehdmaster=1":
35 # ensure the old master is still available
36 assert(hd_master_addr_old == addr)
37 elif keytype == "hdmaster=1":
38 # ensure we have generated a new hd master key
39 assert(hd_master_addr_old != addr)
40 hd_master_addr_ret = addr
41 else:
42 keypath = addr_keypath.rstrip().split("hdkeypath=")[1]
44 # count key types
45 for addrObj in addrs:
46 if addrObj['address'] == addr and addrObj['hdkeypath'] == keypath and keytype == "label=":
47 found_addr += 1
48 break
49 elif keytype == "change=1":
50 found_addr_chg += 1
51 break
52 elif keytype == "reserve=1":
53 found_addr_rsv += 1
54 break
55 return found_addr, found_addr_chg, found_addr_rsv, hd_master_addr_ret
58 class WalletDumpTest(BitcoinTestFramework):
60 def __init__(self):
61 super().__init__()
62 self.setup_clean_chain = False
63 self.num_nodes = 1
64 self.extra_args = [["-keypool=90"]]
66 def setup_network(self, split=False):
67 # Use 1 minute timeout because the initial getnewaddress RPC can take
68 # longer than the default 30 seconds due to an expensive
69 # CWallet::TopUpKeyPool call, and the encryptwallet RPC made later in
70 # the test often takes even longer.
71 self.nodes = self.start_nodes(self.num_nodes, self.options.tmpdir, self.extra_args, timewait=60)
73 def run_test (self):
74 tmpdir = self.options.tmpdir
76 # generate 20 addresses to compare against the dump
77 test_addr_count = 20
78 addrs = []
79 for i in range(0,test_addr_count):
80 addr = self.nodes[0].getnewaddress()
81 vaddr= self.nodes[0].validateaddress(addr) #required to get hd keypath
82 addrs.append(vaddr)
83 # Should be a no-op:
84 self.nodes[0].keypoolrefill()
86 # dump unencrypted wallet
87 result = self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.unencrypted.dump")
88 assert_equal(result['filename'], os.path.abspath(tmpdir + "/node0/wallet.unencrypted.dump"))
90 found_addr, found_addr_chg, found_addr_rsv, hd_master_addr_unenc = \
91 read_dump(tmpdir + "/node0/wallet.unencrypted.dump", addrs, None)
92 assert_equal(found_addr, test_addr_count) # all keys must be in the dump
93 assert_equal(found_addr_chg, 50) # 50 blocks where mined
94 assert_equal(found_addr_rsv, 90*2) # 90 keys plus 100% internal keys
96 #encrypt wallet, restart, unlock and dump
97 self.nodes[0].encryptwallet('test')
98 self.bitcoind_processes[0].wait()
99 self.nodes[0] = self.start_node(0, self.options.tmpdir, self.extra_args[0])
100 self.nodes[0].walletpassphrase('test', 10)
101 # Should be a no-op:
102 self.nodes[0].keypoolrefill()
103 self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.encrypted.dump")
105 found_addr, found_addr_chg, found_addr_rsv, hd_master_addr_enc = \
106 read_dump(tmpdir + "/node0/wallet.encrypted.dump", addrs, hd_master_addr_unenc)
107 assert_equal(found_addr, test_addr_count)
108 assert_equal(found_addr_chg, 90*2 + 50) # old reserve keys are marked as change now
109 assert_equal(found_addr_rsv, 90*2)
111 if __name__ == '__main__':
112 WalletDumpTest().main ()