Dash Core  0.12.2.1
P2P Digital Currency
keypool.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 
18  # Encrypt wallet and wait to terminate
19  nodes[0].encryptwallet('test')
20  bitcoind_processes[0].wait()
21  # Restart node 0
22  nodes[0] = start_node(0, self.options.tmpdir, ['-usehd=0'])
23  # Keep creating keys
24  addr = nodes[0].getnewaddress()
25 
26  try:
27  addr = nodes[0].getnewaddress()
28  raise AssertionError('Keypool should be exhausted after one address')
29  except JSONRPCException as e:
30  assert(e.error['code']==-12)
31 
32  # put three new keys in the keypool
33  nodes[0].walletpassphrase('test', 12000)
34  nodes[0].keypoolrefill(3)
35  nodes[0].walletlock()
36 
37  # drain the keys
38  addr = set()
39  addr.add(nodes[0].getrawchangeaddress())
40  addr.add(nodes[0].getrawchangeaddress())
41  addr.add(nodes[0].getrawchangeaddress())
42  # assert that three unique addresses were returned
43  assert(len(addr) == 3)
44  # the next one should fail
45  try:
46  addr = nodes[0].getrawchangeaddress()
47  raise AssertionError('Keypool should be exhausted after three addresses')
48  except JSONRPCException as e:
49  assert(e.error['code']==-12)
50 
51  # refill keypool with three new addresses
52  nodes[0].walletpassphrase('test', 1)
53  nodes[0].keypoolrefill(3)
54  # test walletpassphrase timeout
55  time.sleep(1.1)
56  assert_equal(nodes[0].getwalletinfo()["unlocked_until"], 0)
57 
58  # drain them by mining
59  nodes[0].generate(1)
60  nodes[0].generate(1)
61  nodes[0].generate(1)
62  try:
63  nodes[0].generate(1)
64  raise AssertionError('Keypool should be exhausted after three addesses')
65  except JSONRPCException as e:
66  assert(e.error['code']==-12)
67 
68  def setup_chain(self):
69  print("Initializing test directory "+self.options.tmpdir)
70  initialize_chain_clean(self.options.tmpdir, 1)
71 
72  def setup_network(self):
73  self.nodes = start_nodes(1, self.options.tmpdir, [['-usehd=0']])
74 
75 if __name__ == '__main__':
76  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
def start_nodes(num_nodes, dirname, extra_args=None, rpchost=None, binary=None)
Definition: util.py:305
def run_test(self)
Definition: keypool.py:15
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 setup_chain(self)
Definition: keypool.py:68
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 setup_network(self)
Definition: keypool.py:72
def assert_equal(thing1, thing2)
Definition: util.py:461
UniValue walletlock(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:2120