|
| 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 wallet. |
| 10 | +- Generate 110 keys (enough to drain the keypool). Store key 90 (in the initial keypool) and key 110 (beyond the initial keypool). Send funds to key 90 and key 110. |
| 11 | +- Stop node1, clear the datadir, move wallet file back into the datadir and restart node1. |
| 12 | +- connect node1 to node0. Verify that they sync and node1 receives its funds.""" |
| 13 | +import shutil |
| 14 | + |
| 15 | +from test_framework.test_framework import BitcoinTestFramework |
| 16 | +from test_framework.util import ( |
| 17 | + assert_equal, |
| 18 | + connect_nodes_bi, |
| 19 | + sync_blocks, |
| 20 | +) |
| 21 | + |
| 22 | +class KeypoolRestoreTest(BitcoinTestFramework): |
| 23 | + def __init__(self): |
| 24 | + super().__init__() |
| 25 | + self.setup_clean_chain = True |
| 26 | + self.num_nodes = 2 |
| 27 | + self.extra_args = [['-usehd=0'], ['-usehd=1', '-keypool=100', '-keypoolmin=20']] |
| 28 | + |
| 29 | + def run_test(self): |
| 30 | + self.tmpdir = self.options.tmpdir |
| 31 | + self.nodes[0].generate(101) |
| 32 | + |
| 33 | + self.log.info("Make backup of wallet") |
| 34 | + |
| 35 | + self.stop_node(1) |
| 36 | + |
| 37 | + shutil.copyfile(self.tmpdir + "/node1/regtest/wallet.dat", self.tmpdir + "/wallet.bak") |
| 38 | + self.nodes[1] = self.start_node(1, self.tmpdir, self.extra_args[1]) |
| 39 | + connect_nodes_bi(self.nodes, 0, 1) |
| 40 | + |
| 41 | + self.log.info("Generate keys for wallet") |
| 42 | + |
| 43 | + for _ in range(90): |
| 44 | + addr_oldpool = self.nodes[1].getnewaddress() |
| 45 | + for _ in range(20): |
| 46 | + addr_extpool = self.nodes[1].getnewaddress() |
| 47 | + |
| 48 | + self.log.info("Send funds to wallet") |
| 49 | + |
| 50 | + self.nodes[0].sendtoaddress(addr_oldpool, 10) |
| 51 | + self.nodes[0].generate(1) |
| 52 | + self.nodes[0].sendtoaddress(addr_extpool, 5) |
| 53 | + self.nodes[0].generate(1) |
| 54 | + sync_blocks(self.nodes) |
| 55 | + |
| 56 | + self.log.info("Restart node with wallet backup") |
| 57 | + |
| 58 | + self.stop_node(1) |
| 59 | + |
| 60 | + shutil.copyfile(self.tmpdir + "/wallet.bak", self.tmpdir + "/node1/regtest/wallet.dat") |
| 61 | + |
| 62 | + self.log.info("Verify keypool is restored and balance is correct") |
| 63 | + |
| 64 | + self.nodes[1] = self.start_node(1, self.tmpdir, self.extra_args[1]) |
| 65 | + connect_nodes_bi(self.nodes, 0, 1) |
| 66 | + self.sync_all() |
| 67 | + |
| 68 | + assert_equal(self.nodes[1].getbalance(), 15) |
| 69 | + assert_equal(self.nodes[1].listtransactions()[0]['category'], "receive") |
| 70 | + |
| 71 | + # Check that we have marked all keys up to the used keypool key as used |
| 72 | + assert_equal(self.nodes[1].validateaddress(self.nodes[1].getnewaddress())['hdkeypath'], "m/0'/0'/111'") |
| 73 | + |
| 74 | +if __name__ == '__main__': |
| 75 | + KeypoolRestoreTest().main() |
0 commit comments