|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2023-present The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or https://www.opensource.org/licenses/mit-license.php. |
| 5 | + |
| 6 | +"""Test wallet-reindex interaction""" |
| 7 | + |
| 8 | +import time |
| 9 | + |
| 10 | +from test_framework.blocktools import COINBASE_MATURITY |
| 11 | +from test_framework.test_framework import BitcoinTestFramework |
| 12 | +from test_framework.util import ( |
| 13 | + assert_equal, |
| 14 | +) |
| 15 | +BLOCK_TIME = 60 * 10 |
| 16 | + |
| 17 | +class WalletReindexTest(BitcoinTestFramework): |
| 18 | + def add_options(self, parser): |
| 19 | + self.add_wallet_options(parser) |
| 20 | + |
| 21 | + def set_test_params(self): |
| 22 | + self.num_nodes = 1 |
| 23 | + self.setup_clean_chain = True |
| 24 | + |
| 25 | + def skip_test_if_missing_module(self): |
| 26 | + self.skip_if_no_wallet() |
| 27 | + |
| 28 | + def advance_time(self, node, secs): |
| 29 | + self.node_time += secs |
| 30 | + node.setmocktime(self.node_time) |
| 31 | + |
| 32 | + # Verify the wallet updates the birth time accordingly when it detects a transaction |
| 33 | + # with a time older than the oldest descriptor timestamp. |
| 34 | + # This could happen when the user blindly imports a descriptor with 'timestamp=now'. |
| 35 | + def birthtime_test(self, node, miner_wallet): |
| 36 | + self.log.info("Test birth time update during tx scanning") |
| 37 | + # Fund address to test |
| 38 | + wallet_addr = miner_wallet.getnewaddress() |
| 39 | + tx_id = miner_wallet.sendtoaddress(wallet_addr, 2) |
| 40 | + |
| 41 | + # Generate 50 blocks, one every 10 min to surpass the 2 hours rescan window the wallet has |
| 42 | + for _ in range(50): |
| 43 | + self.generate(node, 1) |
| 44 | + self.advance_time(node, BLOCK_TIME) |
| 45 | + |
| 46 | + # Now create a new wallet, and import the descriptor |
| 47 | + node.createwallet(wallet_name='watch_only', disable_private_keys=True, load_on_startup=True) |
| 48 | + wallet_watch_only = node.get_wallet_rpc('watch_only') |
| 49 | + # Blank wallets don't have a birth time |
| 50 | + assert 'birthtime' not in wallet_watch_only.getwalletinfo() |
| 51 | + |
| 52 | + # For a descriptors wallet: Import address with timestamp=now. |
| 53 | + # For legacy wallet: There is no way of importing a script/address with a custom time. The wallet always imports it with birthtime=1. |
| 54 | + # In both cases, disable rescan to not detect the transaction. |
| 55 | + wallet_watch_only.importaddress(wallet_addr, rescan=False) |
| 56 | + assert_equal(len(wallet_watch_only.listtransactions()), 0) |
| 57 | + |
| 58 | + # Depending on the wallet type, the birth time changes. |
| 59 | + wallet_birthtime = wallet_watch_only.getwalletinfo()['birthtime'] |
| 60 | + if self.options.descriptors: |
| 61 | + # As blocks were generated every 10 min, the chain MTP timestamp is node_time - 60 min. |
| 62 | + assert_equal(self.node_time - BLOCK_TIME * 6, wallet_birthtime) |
| 63 | + else: |
| 64 | + # No way of importing scripts/addresses with a custom time on a legacy wallet. |
| 65 | + # It's always set to the beginning of time. |
| 66 | + assert_equal(wallet_birthtime, 1) |
| 67 | + |
| 68 | + # Rescan the wallet to detect the missing transaction |
| 69 | + wallet_watch_only.rescanblockchain() |
| 70 | + assert_equal(wallet_watch_only.gettransaction(tx_id)['confirmations'], 50) |
| 71 | + assert_equal(wallet_watch_only.getbalances()['mine' if self.options.descriptors else 'watchonly']['trusted'], 2) |
| 72 | + |
| 73 | + # Reindex and wait for it to finish |
| 74 | + with node.assert_debug_log(expected_msgs=["initload thread exit"]): |
| 75 | + self.restart_node(0, extra_args=['-reindex=1', f'-mocktime={self.node_time}']) |
| 76 | + node.syncwithvalidationinterfacequeue() |
| 77 | + |
| 78 | + # Verify the transaction is still 'confirmed' after reindex |
| 79 | + wallet_watch_only = node.get_wallet_rpc('watch_only') |
| 80 | + tx_info = wallet_watch_only.gettransaction(tx_id) |
| 81 | + assert_equal(tx_info['confirmations'], 50) |
| 82 | + |
| 83 | + # Depending on the wallet type, the birth time changes. |
| 84 | + if self.options.descriptors: |
| 85 | + # For descriptors, verify the wallet updated the birth time to the transaction time |
| 86 | + assert_equal(tx_info['time'], wallet_watch_only.getwalletinfo()['birthtime']) |
| 87 | + else: |
| 88 | + # For legacy, as the birth time was set to the beginning of time, verify it did not change |
| 89 | + assert_equal(wallet_birthtime, 1) |
| 90 | + |
| 91 | + wallet_watch_only.unloadwallet() |
| 92 | + |
| 93 | + def run_test(self): |
| 94 | + node = self.nodes[0] |
| 95 | + self.node_time = int(time.time()) |
| 96 | + node.setmocktime(self.node_time) |
| 97 | + |
| 98 | + # Fund miner |
| 99 | + node.createwallet(wallet_name='miner', load_on_startup=True) |
| 100 | + miner_wallet = node.get_wallet_rpc('miner') |
| 101 | + self.generatetoaddress(node, COINBASE_MATURITY + 10, miner_wallet.getnewaddress()) |
| 102 | + |
| 103 | + # Tests |
| 104 | + self.birthtime_test(node, miner_wallet) |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == '__main__': |
| 108 | + WalletReindexTest().main() |
0 commit comments