Merge #12079: Improve prioritisetransaction test coverage
[bitcoinplatinum.git] / test / functional / wallet-dump.py
blobfa51a2e9d74956c75ee6330052ada352701b9fe5
1 #!/usr/bin/env python3
2 # Copyright (c) 2016-2017 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, assert_raises_rpc_error)
13 def read_dump(file_name, addrs, script_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_script_addr = 0
21 found_addr_chg = 0
22 found_addr_rsv = 0
23 hd_master_addr_ret = None
24 for line in inputfile:
25 # only read non comment lines
26 if line[0] != "#" and len(line) > 10:
27 # split out some data
28 key_label, comment = line.split("#")
29 # key = key_label.split(" ")[0]
30 keytype = key_label.split(" ")[2]
31 if len(comment) > 1:
32 addr_keypath = comment.split(" addr=")[1]
33 addr = addr_keypath.split(" ")[0]
34 keypath = None
35 if keytype == "inactivehdmaster=1":
36 # ensure the old master is still available
37 assert(hd_master_addr_old == addr)
38 elif keytype == "hdmaster=1":
39 # ensure we have generated a new hd master key
40 assert(hd_master_addr_old != addr)
41 hd_master_addr_ret = addr
42 elif keytype == "script=1":
43 # scripts don't have keypaths
44 keypath = None
45 else:
46 keypath = addr_keypath.rstrip().split("hdkeypath=")[1]
48 # count key types
49 for addrObj in addrs:
50 if addrObj['address'] == addr and addrObj['hdkeypath'] == keypath and keytype == "label=":
51 found_addr += 1
52 break
53 elif keytype == "change=1":
54 found_addr_chg += 1
55 break
56 elif keytype == "reserve=1":
57 found_addr_rsv += 1
58 break
60 # count scripts
61 for script_addr in script_addrs:
62 if script_addr == addr.rstrip() and keytype == "script=1":
63 found_script_addr += 1
64 break
66 return found_addr, found_script_addr, found_addr_chg, found_addr_rsv, hd_master_addr_ret
69 class WalletDumpTest(BitcoinTestFramework):
70 def set_test_params(self):
71 self.num_nodes = 1
72 self.extra_args = [["-keypool=90"]]
74 def setup_network(self, split=False):
75 # Use 1 minute timeout because the initial getnewaddress RPC can take
76 # longer than the default 30 seconds due to an expensive
77 # CWallet::TopUpKeyPool call, and the encryptwallet RPC made later in
78 # the test often takes even longer.
79 self.add_nodes(self.num_nodes, self.extra_args, timewait=60)
80 self.start_nodes()
82 def run_test (self):
83 tmpdir = self.options.tmpdir
85 # generate 20 addresses to compare against the dump
86 test_addr_count = 20
87 addrs = []
88 for i in range(0,test_addr_count):
89 addr = self.nodes[0].getnewaddress()
90 vaddr= self.nodes[0].validateaddress(addr) #required to get hd keypath
91 addrs.append(vaddr)
92 # Should be a no-op:
93 self.nodes[0].keypoolrefill()
95 # Test scripts dump by adding a P2SH witness and a 1-of-1 multisig address
96 witness_addr = self.nodes[0].addwitnessaddress(addrs[0]["address"], True)
97 multisig_addr = self.nodes[0].addmultisigaddress(1, [addrs[1]["address"]])
98 script_addrs = [witness_addr, multisig_addr]
100 # dump unencrypted wallet
101 result = self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.unencrypted.dump")
102 assert_equal(result['filename'], os.path.abspath(tmpdir + "/node0/wallet.unencrypted.dump"))
104 found_addr, found_script_addr, found_addr_chg, found_addr_rsv, hd_master_addr_unenc = \
105 read_dump(tmpdir + "/node0/wallet.unencrypted.dump", addrs, script_addrs, None)
106 assert_equal(found_addr, test_addr_count) # all keys must be in the dump
107 assert_equal(found_script_addr, 2) # all scripts must be in the dump
108 assert_equal(found_addr_chg, 50) # 50 blocks where mined
109 assert_equal(found_addr_rsv, 90*2) # 90 keys plus 100% internal keys
111 #encrypt wallet, restart, unlock and dump
112 self.nodes[0].node_encrypt_wallet('test')
113 self.start_node(0)
114 self.nodes[0].walletpassphrase('test', 10)
115 # Should be a no-op:
116 self.nodes[0].keypoolrefill()
117 self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.encrypted.dump")
119 found_addr, found_script_addr, found_addr_chg, found_addr_rsv, _ = \
120 read_dump(tmpdir + "/node0/wallet.encrypted.dump", addrs, script_addrs, hd_master_addr_unenc)
121 assert_equal(found_addr, test_addr_count)
122 assert_equal(found_script_addr, 2)
123 assert_equal(found_addr_chg, 90*2 + 50) # old reserve keys are marked as change now
124 assert_equal(found_addr_rsv, 90*2)
126 # Overwriting should fail
127 assert_raises_rpc_error(-8, "already exists", self.nodes[0].dumpwallet, tmpdir + "/node0/wallet.unencrypted.dump")
129 # Restart node with new wallet, and test importwallet
130 self.stop_node(0)
131 self.start_node(0, ['-wallet=w2'])
133 # Make sure the address is not IsMine before import
134 result = self.nodes[0].validateaddress(multisig_addr)
135 assert(result['ismine'] == False)
137 self.nodes[0].importwallet(os.path.abspath(tmpdir + "/node0/wallet.unencrypted.dump"))
139 # Now check IsMine is true
140 result = self.nodes[0].validateaddress(multisig_addr)
141 assert(result['ismine'] == True)
143 if __name__ == '__main__':
144 WalletDumpTest().main ()