Dash Core  0.12.2.1
P2P Digital Currency
keypool-hd.py
Go to the documentation of this file.
1 #!/usr/bin/env python2
2 # Copyright (c) 2014-2015 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 
6 # Exercise the wallet keypool, and interaction with wallet encryption/locking
7 
8 # Add python-bitcoinrpc to module search path:
9 
10 from test_framework.test_framework import BitcoinTestFramework
11 from test_framework.util import *
12 
14 
15  def run_test(self):
16  nodes = self.nodes
17  addr_before_encrypting = nodes[0].getnewaddress()
18  addr_before_encrypting_data = nodes[0].validateaddress(addr_before_encrypting)
19  wallet_info_old = nodes[0].getwalletinfo()
20  assert(addr_before_encrypting_data['hdchainid'] == wallet_info_old['hdchainid'])
21 
22  # Encrypt wallet and wait to terminate
23  nodes[0].encryptwallet('test')
24  bitcoind_processes[0].wait()
25  # Restart node 0
26  nodes[0] = start_node(0, self.options.tmpdir, ['-usehd=1'])
27  # Keep creating keys
28  addr = nodes[0].getnewaddress()
29  addr_data = nodes[0].validateaddress(addr)
30  wallet_info = nodes[0].getwalletinfo()
31  assert(addr_before_encrypting_data['hdchainid'] == wallet_info['hdchainid'])
32  assert(addr_data['hdchainid'] == wallet_info['hdchainid'])
33 
34  try:
35  addr = nodes[0].getnewaddress()
36  raise AssertionError('Keypool should be exhausted after one address')
37  except JSONRPCException as e:
38  assert(e.error['code']==-12)
39 
40  # put six (plus 2) new keys in the keypool (100% external-, +100% internal-keys, 1 in min)
41  nodes[0].walletpassphrase('test', 12000)
42  nodes[0].keypoolrefill(6)
43  nodes[0].walletlock()
44  wi = nodes[0].getwalletinfo()
45  assert_equal(wi['keypoolsize_hd_internal'], 6)
46  assert_equal(wi['keypoolsize'], 6)
47 
48  # drain the internal keys
49  nodes[0].getrawchangeaddress()
50  nodes[0].getrawchangeaddress()
51  nodes[0].getrawchangeaddress()
52  nodes[0].getrawchangeaddress()
53  nodes[0].getrawchangeaddress()
54  nodes[0].getrawchangeaddress()
55  # the next one should fail
56  try:
57  nodes[0].getrawchangeaddress()
58  raise AssertionError('Keypool should be exhausted after six addresses')
59  except JSONRPCException as e:
60  assert(e.error['code']==-12)
61 
62  addr = set()
63  # drain the external keys
64  addr.add(nodes[0].getnewaddress())
65  addr.add(nodes[0].getnewaddress())
66  addr.add(nodes[0].getnewaddress())
67  addr.add(nodes[0].getnewaddress())
68  addr.add(nodes[0].getnewaddress())
69  addr.add(nodes[0].getnewaddress())
70  assert(len(addr) == 6)
71  # the next one should fail
72  try:
73  addr = nodes[0].getnewaddress()
74  raise AssertionError('Keypool should be exhausted after six addresses')
75  except JSONRPCException as e:
76  assert(e.error['code']==-12)
77 
78  # refill keypool with three new addresses
79  nodes[0].walletpassphrase('test', 1)
80  nodes[0].keypoolrefill(3)
81  # test walletpassphrase timeout
82  time.sleep(1.1)
83  assert_equal(nodes[0].getwalletinfo()["unlocked_until"], 0)
84 
85  # drain them by mining
86  nodes[0].generate(1)
87  nodes[0].generate(1)
88  nodes[0].generate(1)
89  try:
90  nodes[0].generate(1)
91  raise AssertionError('Keypool should be exhausted after three addesses')
92  except JSONRPCException as e:
93  assert(e.error['code']==-12)
94 
95  nodes[0].walletpassphrase('test', 100)
96  nodes[0].keypoolrefill(100)
97  wi = nodes[0].getwalletinfo()
98  assert_equal(wi['keypoolsize_hd_internal'], 100)
99  assert_equal(wi['keypoolsize'], 100)
100 
101  def setup_chain(self):
102  print("Initializing test directory "+self.options.tmpdir)
103  initialize_chain_clean(self.options.tmpdir, 1)
104 
105  def setup_network(self):
106  self.nodes = start_nodes(1, self.options.tmpdir, [['-usehd=1']])
107 
108 if __name__ == '__main__':
109  KeyPoolTest().main()
UniValue getrawchangeaddress(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:224
UniValue walletpassphrase(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:2007
UniValue keypoolrefill(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:1963
UniValue validateaddress(const UniValue &params, bool fHelp)
Definition: misc.cpp:270
def start_nodes(num_nodes, dirname, extra_args=None, rpchost=None, binary=None)
Definition: util.py:305
UniValue getnewaddress(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:113
def initialize_chain_clean(test_dir, num_nodes)
Definition: util.py:252
UniValue getwalletinfo(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:2376
UniValue encryptwallet(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:2159
def start_node(i, dirname, extra_args=None, rpchost=None, timewait=None, binary=None)
Definition: util.py:281
UniValue generate(const UniValue &params, bool fHelp)
Definition: mining.cpp:122
def assert_equal(thing1, thing2)
Definition: util.py:461
UniValue walletlock(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:2120