|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2017 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 HD Wallet keypool restore function. |
| 6 | +
|
| 7 | +Two nodes. Node1 is under test. Node0 is providing transactions and generating blocks. |
| 8 | +
|
| 9 | +- Start node1, shutdown and backup unencrypted wallet. Generate 110 keys (enough to drain the keypool). Store key |
| 10 | + 90 (in the initial keypool) and key 110 (beyond the initial keypool). Send funds to key 90 and key 110. |
| 11 | +- Restart node, encrypt wallet and backup encrypted wallet. Generate 110 keys (enough to drain the keypool). Store key |
| 12 | + 90 (in the initial keypool) and key 110 (beyond the initial keypool). Send funds to key 90 and key 110. |
| 13 | +- Stop node1, clear the datadir, move unencrypted wallet back into the datadir and restart node1. |
| 14 | +- connect node1 to node0. Verify that they sync and node1 receives its funds. |
| 15 | +- Stop node1, move encrypted wallet back into the datadir and restart node1. |
| 16 | +- Verify that node1 fails to start up because it can't topup its keypool. |
| 17 | +""" |
| 18 | +import shutil |
| 19 | + |
| 20 | +from test_framework.test_framework import BitcoinTestFramework, BITCOIND_PROC_WAIT_TIMEOUT |
| 21 | +from test_framework.util import ( |
| 22 | + assert_equal, |
| 23 | + assert_raises_jsonrpc, |
| 24 | + connect_nodes, |
| 25 | + connect_nodes_bi |
| 26 | +) |
| 27 | + |
| 28 | +class KeypoolRestoreTest(BitcoinTestFramework): |
| 29 | + def __init__(self): |
| 30 | + super().__init__() |
| 31 | + self.setup_clean_chain = True |
| 32 | + self.num_nodes = 2 |
| 33 | + self.extra_args = [['-usehd=0'], ['-usehd=1', '-keypool=100', '-keypoolmin=20']] |
| 34 | + |
| 35 | + def run_test(self): |
| 36 | + tmpdir = self.options.tmpdir |
| 37 | + self.nodes[0].generate(101) |
| 38 | + |
| 39 | + self.log.info("Make backup of unencrypted wallet") |
| 40 | + |
| 41 | + self.stop_node(1) |
| 42 | + shutil.copyfile(tmpdir + "/node1/regtest/wallet.dat", tmpdir + "/hd.bak") |
| 43 | + self.nodes[1] = self.start_node(1, self.options.tmpdir, self.extra_args[1]) |
| 44 | + |
| 45 | + self.log.info("Generate keys for unencrypted wallet") |
| 46 | + |
| 47 | + for _ in range(90): |
| 48 | + addr_unenc_oldpool = self.nodes[1].getnewaddress() |
| 49 | + for _ in range(20): |
| 50 | + addr_unenc_extpool = self.nodes[1].getnewaddress() |
| 51 | + |
| 52 | + self.stop_node(1) |
| 53 | + |
| 54 | + self.log.info("Make backup of encrypted wallet") |
| 55 | + |
| 56 | + shutil.copyfile(tmpdir + "/hd.bak", tmpdir + "/node1/regtest/wallet.dat") |
| 57 | + self.nodes[1] = self.start_node(1, self.options.tmpdir, self.extra_args[1]) |
| 58 | + self.nodes[1].encryptwallet('test') |
| 59 | + self.bitcoind_processes[1].wait(timeout=BITCOIND_PROC_WAIT_TIMEOUT) |
| 60 | + # node will be stopped during encryption, now do a backup |
| 61 | + shutil.copyfile(tmpdir + "/node1/regtest/wallet.dat", tmpdir + "/hd.enc.bak") |
| 62 | + |
| 63 | + self.log.info("Generate keys for encrypted wallet") |
| 64 | + |
| 65 | + self.nodes[1] = self.start_node(1, self.options.tmpdir, self.extra_args[1]) |
| 66 | + for _ in range(90): |
| 67 | + addr_enc_oldpool = self.nodes[1].getnewaddress() |
| 68 | + for _ in range(10): |
| 69 | + addr_enc_extpool = self.nodes[1].getnewaddress() |
| 70 | + # Keypool can't top up because the wallet is encrypted |
| 71 | + assert_raises_jsonrpc(-12, "Keypool ran out", self.nodes[1].getnewaddress) |
| 72 | + self.nodes[1].walletpassphrase("test", 10) |
| 73 | + for _ in range(10): |
| 74 | + addr_enc_extpool = self.nodes[1].getnewaddress() |
| 75 | + |
| 76 | + self.log.info("Send funds to encrypted and unencrypted wallets") |
| 77 | + |
| 78 | + self.nodes[0].sendtoaddress(addr_unenc_oldpool, 10) |
| 79 | + self.nodes[0].sendtoaddress(addr_enc_oldpool, 10) |
| 80 | + self.nodes[0].sendtoaddress(addr_unenc_extpool, 5) |
| 81 | + self.nodes[0].sendtoaddress(addr_enc_extpool, 5) |
| 82 | + self.nodes[0].generate(1) |
| 83 | + |
| 84 | + self.log.info("Restart with encrypted wallet - node should shut down") |
| 85 | + |
| 86 | + self.stop_node(1) |
| 87 | + shutil.rmtree(tmpdir + "/node1/regtest/chainstate") |
| 88 | + shutil.rmtree(tmpdir + "/node1/regtest/blocks") |
| 89 | + shutil.copyfile(tmpdir + "/hd.enc.bak", tmpdir + "/node1/regtest/wallet.dat") |
| 90 | + self.nodes[1] = self.start_node(1, self.options.tmpdir, self.extra_args[1]) |
| 91 | + connect_nodes(self.nodes[0], 1) |
| 92 | + |
| 93 | + # node1 should shutdown because it can't topup its keypool |
| 94 | + self.bitcoind_processes[1].wait(2) |
| 95 | + |
| 96 | + self.log.info("Restart with unencrypted wallet") |
| 97 | + |
| 98 | + shutil.rmtree(tmpdir + "/node1/regtest/chainstate") |
| 99 | + shutil.rmtree(tmpdir + "/node1/regtest/blocks") |
| 100 | + shutil.copyfile(tmpdir + "/hd.bak", tmpdir + "/node1/regtest/wallet.dat") |
| 101 | + self.nodes[1] = self.start_node(1, self.options.tmpdir, self.extra_args[1]) |
| 102 | + connect_nodes_bi(self.nodes, 0, 1) |
| 103 | + |
| 104 | + self.sync_all() |
| 105 | + |
| 106 | + assert_equal(self.nodes[1].getbalance(), 15) |
| 107 | + assert_equal(self.nodes[1].listtransactions()[0]['category'], "receive") |
| 108 | + |
| 109 | + # now check if we have marked all keys up to the used keypool key as used |
| 110 | + assert_equal(self.nodes[1].validateaddress(self.nodes[1].getnewaddress())['hdkeypath'], "m/0'/0'/111'") |
| 111 | + |
| 112 | +if __name__ == '__main__': |
| 113 | + KeypoolRestoreTest().main() |
0 commit comments