2017-05-03 16:51:51 +02:00
|
|
|
#!/usr/bin/env python3
|
2019-02-21 02:03:13 +01:00
|
|
|
# Copyright (c) 2017-2019 The Bitcoin Core developers
|
2017-05-03 16:51:51 +02:00
|
|
|
# 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-01-02 14:57:27 +01:00
|
|
|
import os
|
2017-05-03 16:51:51 +02:00
|
|
|
import shutil
|
2019-09-19 16:31:07 +02:00
|
|
|
from decimal import Decimal
|
2017-05-03 16:51:51 +02:00
|
|
|
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
from test_framework.util import (
|
|
|
|
assert_equal,
|
2019-08-27 21:13:56 +02:00
|
|
|
connect_nodes,
|
2017-05-03 16:51:51 +02:00
|
|
|
)
|
|
|
|
|
2018-01-02 14:57:27 +01:00
|
|
|
|
2017-05-03 16:51:51 +02:00
|
|
|
class KeypoolRestoreTest(BitcoinTestFramework):
|
2017-06-10 00:21:21 +02:00
|
|
|
def set_test_params(self):
|
2017-05-03 16:51:51 +02:00
|
|
|
self.setup_clean_chain = True
|
2018-12-02 01:52:08 +01:00
|
|
|
self.num_nodes = 4
|
|
|
|
self.extra_args = [[], ['-keypool=100'], ['-keypool=100'], ['-keypool=100']]
|
2017-05-03 16:51:51 +02:00
|
|
|
|
2020-03-26 23:58:46 +01:00
|
|
|
# whitelist all peers to speed up tx relay / mempool sync
|
|
|
|
for args in self.extra_args:
|
|
|
|
args.append("-whitelist=127.0.0.1")
|
2018-09-09 19:32:37 +02:00
|
|
|
|
2018-09-09 19:32:37 +02:00
|
|
|
def skip_test_if_missing_module(self):
|
|
|
|
self.skip_if_no_wallet()
|
|
|
|
|
2017-05-03 16:51:51 +02:00
|
|
|
def run_test(self):
|
2018-01-02 14:57:27 +01:00
|
|
|
wallet_path = os.path.join(self.nodes[1].datadir, "regtest", "wallets", "wallet.dat")
|
|
|
|
wallet_backup_path = os.path.join(self.nodes[1].datadir, "wallet.bak")
|
2017-05-03 16:51:51 +02:00
|
|
|
self.nodes[0].generate(101)
|
|
|
|
|
|
|
|
self.log.info("Make backup of wallet")
|
|
|
|
self.stop_node(1)
|
2018-01-02 14:57:27 +01:00
|
|
|
shutil.copyfile(wallet_path, wallet_backup_path)
|
2017-06-09 22:35:17 +02:00
|
|
|
self.start_node(1, self.extra_args[1])
|
2019-08-27 21:13:56 +02:00
|
|
|
connect_nodes(self.nodes[0], 1)
|
|
|
|
connect_nodes(self.nodes[0], 2)
|
|
|
|
connect_nodes(self.nodes[0], 3)
|
2017-05-03 16:51:51 +02:00
|
|
|
|
2018-12-02 01:52:08 +01:00
|
|
|
for i, output_type in enumerate(["legacy", "p2sh-segwit", "bech32"]):
|
2017-05-03 16:51:51 +02:00
|
|
|
|
2018-12-02 01:52:08 +01:00
|
|
|
self.log.info("Generate keys for wallet with address type: {}".format(output_type))
|
|
|
|
idx = i+1
|
|
|
|
for _ in range(90):
|
|
|
|
addr_oldpool = self.nodes[idx].getnewaddress(address_type=output_type)
|
|
|
|
for _ in range(20):
|
|
|
|
addr_extpool = self.nodes[idx].getnewaddress(address_type=output_type)
|
2017-05-03 16:51:51 +02:00
|
|
|
|
2018-12-02 01:52:08 +01:00
|
|
|
# Make sure we're creating the outputs we expect
|
|
|
|
address_details = self.nodes[idx].validateaddress(addr_extpool)
|
|
|
|
if i == 0:
|
2019-02-19 23:43:44 +01:00
|
|
|
assert not address_details["isscript"] and not address_details["iswitness"]
|
2018-12-02 01:52:08 +01:00
|
|
|
elif i == 1:
|
2019-02-19 23:43:44 +01:00
|
|
|
assert address_details["isscript"] and not address_details["iswitness"]
|
2018-12-02 01:52:08 +01:00
|
|
|
else:
|
2019-02-19 23:43:44 +01:00
|
|
|
assert not address_details["isscript"] and address_details["iswitness"]
|
2018-12-02 01:52:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
self.log.info("Send funds to wallet")
|
2019-09-19 16:31:07 +02:00
|
|
|
self.nodes[0].sendtoaddress(addr_oldpool, 0.1)
|
2018-12-02 01:52:08 +01:00
|
|
|
self.nodes[0].generate(1)
|
2019-09-19 16:31:07 +02:00
|
|
|
self.nodes[0].sendtoaddress(addr_extpool, 0.05)
|
2018-12-02 01:52:08 +01:00
|
|
|
self.nodes[0].generate(1)
|
2019-04-07 00:19:45 +02:00
|
|
|
self.sync_blocks()
|
2018-12-02 01:52:08 +01:00
|
|
|
|
|
|
|
self.log.info("Restart node with wallet backup")
|
|
|
|
self.stop_node(idx)
|
|
|
|
shutil.copyfile(wallet_backup_path, wallet_path)
|
|
|
|
self.start_node(idx, self.extra_args[idx])
|
2019-08-27 21:13:56 +02:00
|
|
|
connect_nodes(self.nodes[0], idx)
|
2018-12-02 01:52:08 +01:00
|
|
|
self.sync_all()
|
2017-05-03 16:51:51 +02:00
|
|
|
|
2018-12-02 01:52:08 +01:00
|
|
|
self.log.info("Verify keypool is restored and balance is correct")
|
2019-09-19 16:31:07 +02:00
|
|
|
assert_equal(self.nodes[idx].getbalance(), Decimal("0.15"))
|
2018-12-02 01:52:08 +01:00
|
|
|
assert_equal(self.nodes[idx].listtransactions()[0]['category'], "receive")
|
|
|
|
# Check that we have marked all keys up to the used keypool key as used
|
|
|
|
assert_equal(self.nodes[idx].getaddressinfo(self.nodes[idx].getnewaddress())['hdkeypath'], "m/0'/0'/110'")
|
2017-05-03 16:51:51 +02:00
|
|
|
|
2018-01-02 14:57:27 +01:00
|
|
|
|
2017-05-03 16:51:51 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
KeypoolRestoreTest().main()
|