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