Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32# Copyright (c) 2023-present The Bitcoin Core developers3# Distributed under the MIT software license, see the accompanying4# file COPYING or https://www.opensource.org/licenses/mit-license.php.6"""Test wallet-reindex interaction"""8import time10from test_framework.blocktools import COINBASE_MATURITY11from test_framework.test_framework import BitcoinTestFramework12from test_framework.util import (13assert_equal,14)15BLOCK_TIME = 60 * 1017class WalletReindexTest(BitcoinTestFramework):18def set_test_params(self):19self.num_nodes = 120self.setup_clean_chain = True22def skip_test_if_missing_module(self):23self.skip_if_no_wallet()25def advance_time(self, node, secs):26self.node_time += secs27node.setmocktime(self.node_time)29# Verify the wallet updates the birth time accordingly when it detects a transaction30# with a time older than the oldest descriptor timestamp.31# This could happen when the user blindly imports a descriptor with 'timestamp=now'.32def birthtime_test(self, node, miner_wallet):33self.log.info("Test birth time update during tx scanning")34# Fund address to test35wallet_addr = miner_wallet.getnewaddress()36tx_id = miner_wallet.sendtoaddress(wallet_addr, 2)38# Generate 50 blocks, one every 10 min to surpass the 2 hours rescan window the wallet has39for _ in range(50):40self.generate(node, 1)41self.advance_time(node, BLOCK_TIME)43# Now create a new wallet, and import the descriptor44node.createwallet(wallet_name='watch_only', disable_private_keys=True, load_on_startup=True)45wallet_watch_only = node.get_wallet_rpc('watch_only')46# Blank wallets don't have a birth time47assert 'birthtime' not in wallet_watch_only.getwalletinfo()49# Import address with timestamp=now.50wallet_watch_only.importdescriptors([{"desc": miner_wallet.getaddressinfo(wallet_addr)["desc"], "timestamp": "now"}])51assert_equal(len(wallet_watch_only.listtransactions()), 0)53# Depending on the wallet type, the birth time changes.54wallet_birthtime = wallet_watch_only.getwalletinfo()['birthtime']55# As blocks were generated every 10 min, the chain MTP timestamp is node_time - 60 min.56assert_equal(self.node_time - BLOCK_TIME * 6, wallet_birthtime)58# Rescan the wallet to detect the missing transaction59wallet_watch_only.rescanblockchain()60assert_equal(wallet_watch_only.gettransaction(tx_id)['confirmations'], 50)61assert_equal(wallet_watch_only.getbalances()['mine']['trusted'], 2)63self.log.info("Reindex ...") # restart_node waits for it to finish64self.restart_node(0, extra_args=[ f'-mocktime={self.node_time}'])66# Verify the transaction is still 'confirmed' after reindex67wallet_watch_only = node.get_wallet_rpc('watch_only')68tx_info = wallet_watch_only.gettransaction(tx_id)69assert_equal(tx_info['confirmations'], 50)71# Depending on the wallet type, the birth time changes.72# For descriptors, verify the wallet updated the birth time to the transaction time73assert_equal(tx_info['time'], wallet_watch_only.getwalletinfo()['birthtime'])75wallet_watch_only.unloadwallet()77def run_test(self):78node = self.nodes[0]79self.node_time = int(time.time())80node.setmocktime(self.node_time)82# Fund miner83node.createwallet(wallet_name='miner', load_on_startup=True)84miner_wallet = node.get_wallet_rpc('miner')85self.generatetoaddress(node, COINBASE_MATURITY + 10, miner_wallet.getnewaddress())87# Tests88self.birthtime_test(node, miner_wallet)91if __name__ == '__main__':92WalletReindexTest(__file__).main()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.