2017-08-14 16:03:26 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2017 The Bitcoin Core developers
|
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
"""Test HD Wallet keypool restore function.
|
|
|
|
|
|
|
|
Two nodes. Node1 is under test. Node0 is providing transactions and generating blocks.
|
|
|
|
|
|
|
|
- Start node1, shutdown and backup wallet.
|
|
|
|
- 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.
|
|
|
|
- Stop node1, clear the datadir, move wallet file back into the datadir and restart node1.
|
|
|
|
- connect node1 to node0. Verify that they sync and node1 receives its funds."""
|
2018-03-22 11:04:37 +01:00
|
|
|
import os
|
2017-08-14 16:03:26 +02:00
|
|
|
import shutil
|
|
|
|
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
from test_framework.util import (
|
|
|
|
assert_equal,
|
2019-09-18 20:41:14 +02:00
|
|
|
connect_nodes,
|
2017-08-14 16:03:26 +02:00
|
|
|
)
|
|
|
|
|
2018-03-22 11:04:37 +01:00
|
|
|
|
2017-08-14 16:03:26 +02:00
|
|
|
class KeypoolRestoreTest(BitcoinTestFramework):
|
2017-09-01 18:47:13 +02:00
|
|
|
def set_test_params(self):
|
2017-08-14 16:03:26 +02:00
|
|
|
self.setup_clean_chain = True
|
|
|
|
self.num_nodes = 2
|
2021-06-19 16:34:39 +02:00
|
|
|
self.extra_args = [['-usehd=0'], ['-usehd=1', '-keypool=100']]
|
2019-09-20 15:12:07 +02:00
|
|
|
|
2018-09-13 12:33:15 +02:00
|
|
|
def skip_test_if_missing_module(self):
|
|
|
|
self.skip_if_no_wallet()
|
|
|
|
|
2017-08-14 16:03:26 +02:00
|
|
|
def run_test(self):
|
2021-01-22 15:58:07 +01:00
|
|
|
wallet_path = os.path.join(self.nodes[1].datadir, self.chain, "wallets", "wallet.dat")
|
2018-03-22 11:04:37 +01:00
|
|
|
wallet_backup_path = os.path.join(self.nodes[1].datadir, "wallet.bak")
|
2017-08-14 16:03:26 +02:00
|
|
|
self.nodes[0].generate(101)
|
|
|
|
|
|
|
|
self.log.info("Make backup of wallet")
|
|
|
|
self.stop_node(1)
|
2018-03-22 11:04:37 +01:00
|
|
|
shutil.copyfile(wallet_path, wallet_backup_path)
|
2019-09-24 00:54:00 +02:00
|
|
|
self.start_node(1, self.extra_args[1])
|
2019-09-18 20:41:14 +02:00
|
|
|
connect_nodes(self.nodes[0], 1)
|
2017-08-14 16:03:26 +02:00
|
|
|
|
|
|
|
self.log.info("Generate keys for wallet")
|
|
|
|
for _ in range(90):
|
|
|
|
addr_oldpool = self.nodes[1].getnewaddress()
|
|
|
|
for _ in range(20):
|
|
|
|
addr_extpool = self.nodes[1].getnewaddress()
|
|
|
|
|
|
|
|
self.log.info("Send funds to wallet")
|
|
|
|
self.nodes[0].sendtoaddress(addr_oldpool, 10)
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.nodes[0].sendtoaddress(addr_extpool, 5)
|
|
|
|
self.nodes[0].generate(1)
|
2020-04-14 12:00:16 +02:00
|
|
|
self.sync_blocks()
|
2017-08-14 16:03:26 +02:00
|
|
|
|
|
|
|
self.log.info("Restart node with wallet backup")
|
|
|
|
self.stop_node(1)
|
2018-03-22 11:04:37 +01:00
|
|
|
shutil.copyfile(wallet_backup_path, wallet_path)
|
2019-09-24 00:54:00 +02:00
|
|
|
self.start_node(1, self.extra_args[1])
|
2019-09-18 20:41:14 +02:00
|
|
|
connect_nodes(self.nodes[0], 1)
|
2017-08-14 16:03:26 +02:00
|
|
|
self.sync_all()
|
|
|
|
|
2018-03-22 11:04:37 +01:00
|
|
|
self.log.info("Verify keypool is restored and balance is correct")
|
2017-08-14 16:03:26 +02:00
|
|
|
assert_equal(self.nodes[1].getbalance(), 15)
|
|
|
|
assert_equal(self.nodes[1].listtransactions()[0]['category'], "receive")
|
|
|
|
# Check that we have marked all keys up to the used keypool key as used
|
2020-12-17 13:46:20 +01:00
|
|
|
assert_equal(self.nodes[1].getaddressinfo(self.nodes[1].getnewaddress())['hdkeypath'], "m/44'/1'/0'/0/110")
|
2017-08-14 16:03:26 +02:00
|
|
|
|
2018-03-22 11:04:37 +01:00
|
|
|
|
2017-08-14 16:03:26 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
KeypoolRestoreTest().main()
|