2016-03-19 16:58:06 -03:00
|
|
|
#!/usr/bin/env python3
|
2018-07-26 18:36:45 -04:00
|
|
|
# Copyright (c) 2014-2018 The Bitcoin Core developers
|
2014-10-22 22:48:19 -03:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
2014-06-16 08:45:32 -04:00
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-01-17 20:34:40 -03:00
|
|
|
"""Test the wallet keypool and interaction with wallet encryption/locking."""
|
2014-06-16 08:45:32 -04:00
|
|
|
|
2018-07-06 18:10:35 -04:00
|
|
|
import time
|
|
|
|
|
2015-11-15 13:58:01 -03:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2018-07-06 18:10:35 -04:00
|
|
|
from test_framework.util import assert_equal, assert_raises_rpc_error
|
2014-06-16 08:45:32 -04:00
|
|
|
|
2015-11-15 13:58:01 -03:00
|
|
|
class KeyPoolTest(BitcoinTestFramework):
|
2017-06-09 18:21:21 -04:00
|
|
|
def set_test_params(self):
|
|
|
|
self.num_nodes = 1
|
2015-11-15 13:58:01 -03:00
|
|
|
|
2018-09-09 14:32:37 -03:00
|
|
|
def skip_test_if_missing_module(self):
|
|
|
|
self.skip_if_no_wallet()
|
|
|
|
|
2015-11-15 13:58:01 -03:00
|
|
|
def run_test(self):
|
|
|
|
nodes = self.nodes
|
2016-07-21 15:19:02 -04:00
|
|
|
addr_before_encrypting = nodes[0].getnewaddress()
|
2018-02-09 13:12:27 -03:00
|
|
|
addr_before_encrypting_data = nodes[0].getaddressinfo(addr_before_encrypting)
|
2016-07-21 15:19:02 -04:00
|
|
|
wallet_info_old = nodes[0].getwalletinfo()
|
2018-04-17 18:42:31 -03:00
|
|
|
assert_equal(wallet_info_old['hdseedid'], wallet_info_old['hdmasterkeyid'])
|
2018-04-04 12:43:45 -03:00
|
|
|
assert(addr_before_encrypting_data['hdseedid'] == wallet_info_old['hdseedid'])
|
2018-04-16 06:13:07 -03:00
|
|
|
|
2015-11-15 14:48:18 -03:00
|
|
|
# Encrypt wallet and wait to terminate
|
2018-02-20 18:09:51 -03:00
|
|
|
nodes[0].encryptwallet('test')
|
2015-11-15 14:48:18 -03:00
|
|
|
# Keep creating keys
|
2014-06-16 08:45:32 -04:00
|
|
|
addr = nodes[0].getnewaddress()
|
2018-02-09 13:12:27 -03:00
|
|
|
addr_data = nodes[0].getaddressinfo(addr)
|
2016-07-21 15:19:02 -04:00
|
|
|
wallet_info = nodes[0].getwalletinfo()
|
2018-04-17 18:42:31 -03:00
|
|
|
assert_equal(wallet_info['hdseedid'], wallet_info['hdmasterkeyid'])
|
2018-04-04 12:43:45 -03:00
|
|
|
assert(addr_before_encrypting_data['hdseedid'] != wallet_info['hdseedid'])
|
|
|
|
assert(addr_data['hdseedid'] == wallet_info['hdseedid'])
|
2017-07-12 10:33:46 -04:00
|
|
|
assert_raises_rpc_error(-12, "Error: Keypool ran out, please call keypoolrefill first", nodes[0].getnewaddress)
|
2015-11-15 14:48:18 -03:00
|
|
|
|
2017-01-17 04:55:30 -03:00
|
|
|
# put six (plus 2) new keys in the keypool (100% external-, +100% internal-keys, 1 in min)
|
2015-11-15 14:48:18 -03:00
|
|
|
nodes[0].walletpassphrase('test', 12000)
|
2017-01-10 12:45:30 -03:00
|
|
|
nodes[0].keypoolrefill(6)
|
2015-11-15 14:48:18 -03:00
|
|
|
nodes[0].walletlock()
|
2017-01-10 12:45:30 -03:00
|
|
|
wi = nodes[0].getwalletinfo()
|
2017-01-17 04:55:30 -03:00
|
|
|
assert_equal(wi['keypoolsize_hd_internal'], 6)
|
2017-01-10 12:45:30 -03:00
|
|
|
assert_equal(wi['keypoolsize'], 6)
|
2015-11-15 14:48:18 -03:00
|
|
|
|
2017-01-10 12:45:30 -03:00
|
|
|
# drain the internal keys
|
|
|
|
nodes[0].getrawchangeaddress()
|
|
|
|
nodes[0].getrawchangeaddress()
|
2017-01-17 04:55:30 -03:00
|
|
|
nodes[0].getrawchangeaddress()
|
|
|
|
nodes[0].getrawchangeaddress()
|
|
|
|
nodes[0].getrawchangeaddress()
|
|
|
|
nodes[0].getrawchangeaddress()
|
2015-11-15 14:48:18 -03:00
|
|
|
addr = set()
|
|
|
|
# the next one should fail
|
2017-07-12 10:33:46 -04:00
|
|
|
assert_raises_rpc_error(-12, "Keypool ran out", nodes[0].getrawchangeaddress)
|
2015-11-15 14:48:18 -03:00
|
|
|
|
2017-01-10 12:45:30 -03:00
|
|
|
# drain the external keys
|
|
|
|
addr.add(nodes[0].getnewaddress())
|
|
|
|
addr.add(nodes[0].getnewaddress())
|
|
|
|
addr.add(nodes[0].getnewaddress())
|
|
|
|
addr.add(nodes[0].getnewaddress())
|
|
|
|
addr.add(nodes[0].getnewaddress())
|
|
|
|
addr.add(nodes[0].getnewaddress())
|
|
|
|
assert(len(addr) == 6)
|
|
|
|
# the next one should fail
|
2017-07-12 10:33:46 -04:00
|
|
|
assert_raises_rpc_error(-12, "Error: Keypool ran out, please call keypoolrefill first", nodes[0].getnewaddress)
|
2017-01-10 12:45:30 -03:00
|
|
|
|
2015-11-15 14:48:18 -03:00
|
|
|
# refill keypool with three new addresses
|
2016-01-08 09:12:16 -03:00
|
|
|
nodes[0].walletpassphrase('test', 1)
|
2015-11-15 14:48:18 -03:00
|
|
|
nodes[0].keypoolrefill(3)
|
2017-01-10 12:45:30 -03:00
|
|
|
|
2016-01-08 09:12:16 -03:00
|
|
|
# test walletpassphrase timeout
|
|
|
|
time.sleep(1.1)
|
|
|
|
assert_equal(nodes[0].getwalletinfo()["unlocked_until"], 0)
|
2015-11-15 14:48:18 -03:00
|
|
|
|
2018-10-11 12:04:06 -03:00
|
|
|
# drain the keypool
|
|
|
|
for _ in range(3):
|
|
|
|
nodes[0].getnewaddress()
|
|
|
|
assert_raises_rpc_error(-12, "Keypool ran out", nodes[0].getnewaddress)
|
2015-11-15 14:48:18 -03:00
|
|
|
|
2017-01-10 12:45:30 -03:00
|
|
|
nodes[0].walletpassphrase('test', 100)
|
|
|
|
nodes[0].keypoolrefill(100)
|
|
|
|
wi = nodes[0].getwalletinfo()
|
2017-01-17 04:55:30 -03:00
|
|
|
assert_equal(wi['keypoolsize_hd_internal'], 100)
|
2017-01-10 12:45:30 -03:00
|
|
|
assert_equal(wi['keypoolsize'], 100)
|
|
|
|
|
2014-06-16 08:45:32 -04:00
|
|
|
if __name__ == '__main__':
|
2015-11-15 13:58:01 -03:00
|
|
|
KeyPoolTest().main()
|