2019-11-23 18:47:54 -08:00
|
|
|
#!/usr/bin/env python3
|
2016-03-19 12:58:06 -07:00
|
|
|
# Copyright (c) 2014-2016 The Bitcoin Core developers
|
2022-05-11 16:23:09 -07:00
|
|
|
# Copyright (c) 2016-2022 The Zcash developers
|
2014-10-22 18:48:19 -07:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
2019-07-18 07:16:09 -07:00
|
|
|
# file COPYING or https://www.opensource.org/licenses/mit-license.php .
|
2014-06-16 05:45:32 -07:00
|
|
|
|
|
|
|
# Exercise the wallet keypool, and interaction with wallet encryption/locking
|
|
|
|
|
2017-06-20 13:09:33 -07:00
|
|
|
from test_framework.authproxy import JSONRPCException
|
2015-11-15 08:58:01 -08:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2016-05-14 04:01:31 -07:00
|
|
|
from test_framework.util import assert_equal, \
|
2015-11-15 08:58:01 -08:00
|
|
|
start_nodes, start_node, bitcoind_processes
|
2014-06-16 05:45:32 -07:00
|
|
|
|
|
|
|
def check_array_result(object_array, to_match, expected):
|
|
|
|
"""
|
|
|
|
Pass in array of JSON objects, a dictionary with key/value pairs
|
|
|
|
to match against, and another dictionary with expected key/value
|
|
|
|
pairs.
|
|
|
|
"""
|
|
|
|
num_matched = 0
|
|
|
|
for item in object_array:
|
|
|
|
all_match = True
|
|
|
|
for key,value in to_match.items():
|
|
|
|
if item[key] != value:
|
|
|
|
all_match = False
|
|
|
|
if not all_match:
|
|
|
|
continue
|
|
|
|
for key,value in expected.items():
|
|
|
|
if item[key] != value:
|
|
|
|
raise AssertionError("%s : expected %s=%s"%(str(item), str(key), str(value)))
|
|
|
|
num_matched = num_matched+1
|
|
|
|
if num_matched == 0:
|
|
|
|
raise AssertionError("No objects matched %s"%(str(to_match)))
|
|
|
|
|
2015-11-15 08:58:01 -08:00
|
|
|
class KeyPoolTest(BitcoinTestFramework):
|
|
|
|
|
|
|
|
def run_test(self):
|
|
|
|
nodes = self.nodes
|
2015-11-15 09:48:18 -08:00
|
|
|
# Encrypt wallet and wait to terminate
|
|
|
|
nodes[0].encryptwallet('test')
|
|
|
|
bitcoind_processes[0].wait()
|
|
|
|
# Restart node 0
|
2022-12-02 21:49:43 -08:00
|
|
|
nodes[0] = start_node(0, self.options.tmpdir, extra_args=[
|
|
|
|
'-allowdeprecated=getnewaddress',
|
|
|
|
'-allowdeprecated=getrawchangeaddress',
|
|
|
|
])
|
2022-02-10 11:18:57 -08:00
|
|
|
# We can't create any external addresses, which don't use the keypool.
|
|
|
|
# We should get an error that we need to unlock the wallet.
|
2015-11-15 09:48:18 -08:00
|
|
|
try:
|
|
|
|
addr = nodes[0].getnewaddress()
|
2022-02-10 11:18:57 -08:00
|
|
|
raise AssertionError('Wallet should be locked.')
|
2015-11-15 09:48:18 -08:00
|
|
|
except JSONRPCException as e:
|
2022-02-10 11:18:57 -08:00
|
|
|
assert_equal(e.error['code'], -13)
|
2015-11-15 09:48:18 -08:00
|
|
|
|
|
|
|
# put three new keys in the keypool
|
|
|
|
nodes[0].walletpassphrase('test', 12000)
|
|
|
|
nodes[0].keypoolrefill(3)
|
|
|
|
nodes[0].walletlock()
|
|
|
|
|
|
|
|
# drain the keys
|
|
|
|
addr = set()
|
|
|
|
addr.add(nodes[0].getrawchangeaddress())
|
|
|
|
addr.add(nodes[0].getrawchangeaddress())
|
|
|
|
addr.add(nodes[0].getrawchangeaddress())
|
|
|
|
addr.add(nodes[0].getrawchangeaddress())
|
|
|
|
# assert that four unique addresses were returned
|
|
|
|
assert(len(addr) == 4)
|
|
|
|
# the next one should fail
|
|
|
|
try:
|
|
|
|
addr = nodes[0].getrawchangeaddress()
|
|
|
|
raise AssertionError('Keypool should be exhausted after three addresses')
|
|
|
|
except JSONRPCException as e:
|
|
|
|
assert(e.error['code']==-12)
|
|
|
|
|
|
|
|
# refill keypool with three new addresses
|
|
|
|
nodes[0].walletpassphrase('test', 12000)
|
|
|
|
nodes[0].keypoolrefill(3)
|
|
|
|
nodes[0].walletlock()
|
|
|
|
|
|
|
|
# drain them by mining
|
2015-08-18 00:07:33 -07:00
|
|
|
nodes[0].generate(1)
|
2015-11-15 09:48:18 -08:00
|
|
|
nodes[0].generate(1)
|
|
|
|
nodes[0].generate(1)
|
|
|
|
nodes[0].generate(1)
|
|
|
|
try:
|
|
|
|
nodes[0].generate(1)
|
|
|
|
raise AssertionError('Keypool should be exhausted after three addesses')
|
|
|
|
except JSONRPCException as e:
|
|
|
|
assert_equal(e.error['code'], -12)
|
|
|
|
|
2016-05-14 04:01:31 -07:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.num_nodes = 1
|
2015-11-15 09:48:18 -08:00
|
|
|
|
2015-11-15 08:58:01 -08:00
|
|
|
def setup_network(self):
|
2016-05-14 04:01:31 -07:00
|
|
|
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, extra_args=[['-experimentalfeatures', '-developerencryptwallet']])
|
2014-06-16 05:45:32 -07:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-11-15 08:58:01 -08:00
|
|
|
KeyPoolTest().main()
|