2 # Copyright (c) 2014-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 wallet keypool and interaction with wallet encryption/locking."""
7 from test_framework
.test_framework
import BitcoinTestFramework
8 from test_framework
.util
import *
10 class KeyPoolTest(BitcoinTestFramework
):
11 def set_test_params(self
):
16 addr_before_encrypting
= nodes
[0].getnewaddress()
17 addr_before_encrypting_data
= nodes
[0].validateaddress(addr_before_encrypting
)
18 wallet_info_old
= nodes
[0].getwalletinfo()
19 assert(addr_before_encrypting_data
['hdmasterkeyid'] == wallet_info_old
['hdmasterkeyid'])
21 # Encrypt wallet and wait to terminate
22 nodes
[0].node_encrypt_wallet('test')
26 addr
= nodes
[0].getnewaddress()
27 addr_data
= nodes
[0].validateaddress(addr
)
28 wallet_info
= nodes
[0].getwalletinfo()
29 assert(addr_before_encrypting_data
['hdmasterkeyid'] != wallet_info
['hdmasterkeyid'])
30 assert(addr_data
['hdmasterkeyid'] == wallet_info
['hdmasterkeyid'])
31 assert_raises_rpc_error(-12, "Error: Keypool ran out, please call keypoolrefill first", nodes
[0].getnewaddress
)
33 # put six (plus 2) new keys in the keypool (100% external-, +100% internal-keys, 1 in min)
34 nodes
[0].walletpassphrase('test', 12000)
35 nodes
[0].keypoolrefill(6)
37 wi
= nodes
[0].getwalletinfo()
38 assert_equal(wi
['keypoolsize_hd_internal'], 6)
39 assert_equal(wi
['keypoolsize'], 6)
41 # drain the internal keys
42 nodes
[0].getrawchangeaddress()
43 nodes
[0].getrawchangeaddress()
44 nodes
[0].getrawchangeaddress()
45 nodes
[0].getrawchangeaddress()
46 nodes
[0].getrawchangeaddress()
47 nodes
[0].getrawchangeaddress()
49 # the next one should fail
50 assert_raises_rpc_error(-12, "Keypool ran out", nodes
[0].getrawchangeaddress
)
52 # drain the external keys
53 addr
.add(nodes
[0].getnewaddress())
54 addr
.add(nodes
[0].getnewaddress())
55 addr
.add(nodes
[0].getnewaddress())
56 addr
.add(nodes
[0].getnewaddress())
57 addr
.add(nodes
[0].getnewaddress())
58 addr
.add(nodes
[0].getnewaddress())
59 assert(len(addr
) == 6)
60 # the next one should fail
61 assert_raises_rpc_error(-12, "Error: Keypool ran out, please call keypoolrefill first", nodes
[0].getnewaddress
)
63 # refill keypool with three new addresses
64 nodes
[0].walletpassphrase('test', 1)
65 nodes
[0].keypoolrefill(3)
67 # test walletpassphrase timeout
69 assert_equal(nodes
[0].getwalletinfo()["unlocked_until"], 0)
71 # drain them by mining
75 assert_raises_rpc_error(-12, "Keypool ran out", nodes
[0].generate
, 1)
77 nodes
[0].walletpassphrase('test', 100)
78 nodes
[0].keypoolrefill(100)
79 wi
= nodes
[0].getwalletinfo()
80 assert_equal(wi
['keypoolsize_hd_internal'], 100)
81 assert_equal(wi
['keypoolsize'], 100)
83 if __name__
== '__main__':