Merge bitcoin/bitcoin#36447: test: Misc fixes for pending validation events races
What changed, and why it matters
This commit fixes timing problems in Bitcoin Core's own unit and benchmark tests. It does not change how the live Bitcoin node behaves. The changes make test setups wait for background 'validation' notifications to finish before tests run or shut down, which removes random test failures and unsafe shutdown races during testing. A large block of wallet rescan tests was also moved to a new file with no functional changes.
No production action needed. This is a test-hardening change. Developers running CI or local tests should ensure the updated test utilities are used; downstream backports should include all three commits to avoid intermittent test failures.
Security signals we found
Race condition in test shutdown sequence (validation callbacks vs. index destruction)
Non-deterministic test failures due to pending validation-interface events
Unsafe memory races during test index teardown
Test-only synchronization hardening
Evidence from the diff
The merge addresses non-determinism in tests that use TestChain100Setup, which enqueues 100 BlockConnected validation-interface events. The fix drains those pending events in the fixture constructor and in mineBlocks()/CreateAndProcessBlock() when sync=true. A new test helper StopIndex() drains the validation queue before calling BaseIndex::Stop(), replacing ad-hoc SyncWithValidationInterfaceQueue() calls across index tests and benchmarks. Several wallet rescan tests are moved from wallet_tests.cpp to a new rescan_tests.cpp, and AddKey() is extracted to a shared helper. No production consensus, networking, or wallet code is changed.
Changed components
src/test/util/setup_common.cpp/hsrc/test/util/validation.cpp/hsrc/test/baseindex_tests.cppsrc/test/blockfilter_index_tests.cppsrc/test/coinstatsindex_tests.cppsrc/test/txindex_tests.cppsrc/test/txospenderindex_tests.cppsrc/bench/index_blockfilter.cppsrc/wallet/test/wallet_tests.cppsrc/wallet/test/rescan_tests.cppsrc/wallet/test/util.cpp/hsrc/wallet/test/CMakeLists.txtInspect captured patch +655 / −612
### src/bench/index_blockfilter.cpp
@@ -16,6 +16,7 @@
#include <sync.h>
#include <test/util/setup_common.h>
#include <test/util/time.h>
+#include <test/util/validation.h>
#include <uint256.h>
#include <util/check.h>
#include <util/strencodings.h>
@@ -56,8 +57,7 @@ static void BlockFilterIndexSync(benchmark::Bench& bench)
assert(summary.synced);
assert(summary.best_block_hash == WITH_LOCK(::cs_main, return test_setup->m_node.chainman->ActiveTip()->GetBlockHash()));
- // Shutdown sequence (c.f. Shutdown() in init.cpp)
- filter_index.Stop();
+ StopIndex(filter_index, test_setup->m_node);
});
}
### src/test/baseindex_tests.cpp
@@ -85,10 +85,7 @@ BOOST_FIXTURE_TEST_CASE(baseindex_no_commit_ahead_of_flush, TestChain100Setup)
BOOST_REQUIRE(index->Init());
BOOST_CHECK_EQUAL(index->GetSummary().best_block_height, expected_commit_height);
- // Drain in-flight validation callbacks before destroying the index.
- m_node.chain->context()->validation_signals->SyncWithValidationInterfaceQueue();
- // shutdown sequence (c.f. Shutdown() in init.cpp)
- index->Stop();
+ StopIndex(*index, m_node);
};
// Part 1: Sync, then "crash" (stop without flushing). Models a node that
@@ -235,10 +232,7 @@ BOOST_FIXTURE_TEST_CASE(index_reorg_crash, TestChain100Setup)
// Wait for the index to reach the new tip
func_wait_until(blocking_height + 2, 5s);
- // Drain unused BlockConnected events, to avoid unsafe memory races during destruction
- m_node.chain->context()->validation_signals->SyncWithValidationInterfaceQueue();
- // shutdown sequence (c.f. Shutdown() in init.cpp)
- index.Stop();
+ StopIndex(index, m_node);
}
BOOST_AUTO_TEST_SUITE_END()
### src/test/blockfilter_index_tests.cpp
@@ -16,6 +16,7 @@
#include <test/util/common.h>
#include <test/util/mining.h>
#include <test/util/setup_common.h>
+#include <test/util/validation.h>
#include <uint256.h>
#include <util/check.h>
#include <validation.h>
@@ -223,7 +224,7 @@ BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, TestChain100Setup)
filter_hashes.clear();
filter_index.Interrupt();
- filter_index.Stop();
+ StopIndex(filter_index, m_node);
}
BOOST_FIXTURE_TEST_CASE(blockfilter_index_init_destroy, BasicTestingSetup)
### src/test/coinstatsindex_tests.cpp
@@ -12,6 +12,7 @@
#include <script/script.h>
#include <sync.h>
#include <test/util/setup_common.h>
+#include <test/util/validation.h>
#include <util/check.h>
#include <validation.h>
@@ -71,8 +72,7 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
BOOST_CHECK(block_index != new_block_index);
- // Shutdown sequence (c.f. Shutdown() in init.cpp)
- coin_stats_index.Stop();
+ StopIndex(coin_stats_index, m_node);
}
BOOST_AUTO_TEST_SUITE_END()
### src/test/txindex_tests.cpp
@@ -22,6 +22,7 @@
#include <streams.h>
#include <sync.h>
#include <test/util/setup_common.h>
+#include <test/util/validation.h>
#include <util/byte_units.h>
#include <util/check.h>
#include <util/strencodings.h>
@@ -170,8 +171,7 @@ BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup)
LookupTx(txindex, txn.GetHash());
}
- // shutdown sequence (c.f. Shutdown() in init.cpp)
- txindex.Stop();
+ StopIndex(txindex, m_node);
}
BOOST_FIXTURE_TEST_CASE(txindex_collision_scan_path, TestChain100Setup)
@@ -223,7 +223,7 @@ BOOST_FIXTURE_TEST_CASE(txindex_collision_scan_path, TestChain100Setup)
db.Write(txindex::LegacyTxKey(fake_txid), fake_physical);
BOOST_CHECK(!txindex.FindTx(fake_txid));
- txindex.Stop();
+ StopIndex(txindex, m_node);
}
BOOST_FIXTURE_TEST_CASE(txindex_legacy_fallback, TestChain100Setup)
@@ -253,7 +253,7 @@ BOOST_FIXTURE_TEST_CASE(txindex_legacy_fallback, TestChain100Setup)
LookupTx(txindex, legacy_txid);
- txindex.Stop();
+ StopIndex(txindex, m_node);
}
BOOST_FIXTURE_TEST_CASE(txindex_locator_upgrade, TestChain100Setup)
@@ -349,7 +349,7 @@ BOOST_FIXTURE_TEST_CASE(txindex_reorg_keeps_stale_entries, TestChain100Setup)
BOOST_REQUIRE_EQUAL(reorg_bucket.size(), 2U);
BOOST_CHECK(reorg_bucket.front() == original_bucket.front());
- txindex.Stop();
+ StopIndex(txindex, m_node);
}
BOOST_AUTO_TEST_SUITE_END()
### src/test/txospenderindex_tests.cpp
@@ -5,6 +5,7 @@
#include <index/txospenderindex.h>
#include <test/util/common.h>
#include <test/util/setup_common.h>
+#include <test/util/validation.h>
#include <validation.h>
#include <boost/test/unit_test.hpp>
@@ -70,8 +71,7 @@ BOOST_FIXTURE_TEST_CASE(txospenderindex_initial_sync, TestChain100Setup)
BOOST_CHECK_EQUAL((*tx_spender)->block_hash, tip_hash);
}
- // Shutdown sequence (c.f. Shutdown() in init.cpp)
- txospenderindex.Stop();
+ StopIndex(txospenderindex, m_node);
}
BOOST_AUTO_TEST_SUITE_END()
### src/test/util/setup_common.cpp
@@ -444,10 +444,11 @@ void TestChain100Setup::mineBlocks(int num_blocks)
CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
for (int i = 0; i < num_blocks; i++) {
std::vector<CMutableTransaction> noTxns;
- CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey);
+ CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey, /*sync=*/false);
m_clock += 1s;
m_coinbase_txns.push_back(b.vtx[0]);
}
+ if (m_node.validation_signals) m_node.validation_signals->SyncWithValidationInterfaceQueue();
}
CBlock TestChain100Setup::CreateBlock(
@@ -475,11 +476,13 @@ CBlock TestChain100Setup::CreateBlock(
CBlock TestChain100Setup::CreateAndProcessBlock(
const std::vector<CMutableTransaction>& txns,
- const CScript& scriptPubKey)
+ const CScript& scriptPubKey,
+ bool sync)
{
CBlock block = this->CreateBlock(txns, scriptPubKey);
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
Assert(m_node.chainman)->ProcessNewBlock(shared_pblock, true, true, nullptr);
+ if (sync && m_node.validation_signals) m_node.validation_signals->SyncWithValidationInterfaceQueue();
return block;
}
### src/test/util/setup_common.h
@@ -145,12 +145,12 @@ struct TestChain100Setup : public TestingSetup {
ChainType chain_type = ChainType::REGTEST,
TestOpts = {});
- /**
- * Create a new block with just given transactions, coinbase paying to
- * scriptPubKey, and try to add it to the current chain.
- */
+ /// Create a new block with just the given transactions, coinbase paying to
+ /// scriptPubKey, and try to add it to the current chain.
+ /// By default, wait for validation-interface notifications to be processed.
CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
- const CScript& scriptPubKey);
+ const CScript& scriptPubKey,
+ bool sync = true);
/**
* Create a new block with just given transactions, coinbase paying to
@@ -160,7 +160,8 @@ struct TestChain100Setup : public TestingSetup {
const std::vector<CMutableTransaction>& txns,
const CScript& scriptPubKey);
- //! Mine a series of new blocks on the active chain.
+ /// Mine a series of new blocks on the active chain.
+ /// Wait for validation-interface notifications to be processed.
void mineBlocks(int num_blocks);
/**
### src/test/util/validation.cpp
@@ -6,8 +6,10 @@
#include <coins.h>
#include <consensus/consensus.h>
+#include <index/base.h>
#include <node/block_template_manager.h>
#include <node/blockstorage.h>
+#include <node/context.h>
#include <node/mining_types.h>
#include <test/util/mining.h>
#include <test/util/script.h>
@@ -135,3 +137,9 @@ std::vector<std::pair<COutPoint, CAmount>> ResetChainmanAndMempool(TestingSetup&
}
return mature_coinbase;
}
+
+void StopIndex(BaseIndex& index, node::NodeContext& node)
+{
+ Assert(node.validation_signals)->SyncWithValidationInterfaceQueue();
+ index.Stop();
+}
### src/test/util/validation.h
@@ -18,7 +18,9 @@
namespace node {
class BlockManager;
+struct NodeContext;
}
+class BaseIndex;
class CValidationInterface;
class FakeNodeClock;
struct TestingSetup;
@@ -63,4 +65,7 @@ class ValidationInterfaceTest
std::vector<std::pair<COutPoint, CAmount>> ResetChainmanAndMempool(TestingSetup& setup, FakeNodeClock& node_clock);
+/// Drain validation callbacks before stopping an index, matching Shutdown() in init.cpp.
+void StopIndex(BaseIndex& index, node::NodeContext& node);
+
#endif // BITCOIN_TEST_UTIL_VALIDATION_H
### src/wallet/test/CMakeLists.txt
@@ -16,6 +16,7 @@ target_sources(test_bitcoin
init_tests.cpp
ismine_tests.cpp
psbt_wallet_tests.cpp
+ rescan_tests.cpp
scriptpubkeyman_tests.cpp
spend_tests.cpp
wallet_crypto_tests.cpp
### src/wallet/test/rescan_tests.cpp
@@ -0,0 +1,597 @@
+// Copyright (c) 2012-present The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or https://opensource.org/license/mit.
+
+#include <blockfilter.h>
+#include <chain.h>
+#include <index/blockfilterindex.h>
+#include <interfaces/chain.h>
+#include <node/blockstorage.h>
+#include <test/util/common.h>
+#include <test/util/logging.h>
+#include <test/util/setup_common.h>
+#include <test/util/validation.h>
+#include <util/byte_units.h>
+#include <util/check.h>
+#include <validation.h>
+#include <wallet/context.h>
+#include <wallet/receive.h>
+#include <wallet/scan.h>
+#include <wallet/test/util.h>
+#include <wallet/test/wallet_test_fixture.h>
+#include <wallet/wallet.h>
+#include <wallet/walletdb.h>
+
+#include <boost/test/unit_test.hpp>
+
+#include <cassert>
+#include <chrono>
+#include <memory>
+#include <string>
+#include <utility>
+
+using node::MAX_BLOCKFILE_SIZE;
+
+namespace wallet {
+
+BOOST_FIXTURE_TEST_SUITE(rescan_tests, WalletTestingSetup)
+
+BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
+{
+ // Cap last block file size, and mine new block in a new block file.
+ CBlockIndex* oldTip = WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain().Tip());
+ WITH_LOCK(::cs_main, m_node.chainman->m_blockman.GetBlockFileInfo(oldTip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE);
+ CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
+ CBlockIndex* newTip = WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain().Tip());
+
+ // Verify Scan fails to read an unknown start block.
+ {
+ CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
+ {
+ LOCK(wallet.cs_wallet);
+ LOCK(Assert(m_node.chainman)->GetMutex());
+ wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
+ wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
+ }
+ AddKey(wallet, coinbaseKey);
+ WalletRescanReserver reserver(wallet);
+ reserver.reserve();
+ ScanResult result = wallet.Scanner().Scan(/*start_block=*/{}, /*start_height=*/0, /*max_height=*/{}, reserver, /*save_progress=*/false);
+ BOOST_CHECK_EQUAL(result.status, ScanResult::FAILURE);
+ BOOST_CHECK(result.last_failed_block.IsNull());
+ BOOST_CHECK(result.last_scanned_block.IsNull());
+ BOOST_CHECK(!result.last_scanned_height);
+ BOOST_CHECK_EQUAL(GetBalance(wallet).m_mine_immature, 0);
+ }
+
+ // Verify Scan picks up transactions in both the old
+ // and new block files.
+ {
+ CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
+ {
+ LOCK(wallet.cs_wallet);
+ LOCK(Assert(m_node.chainman)->GetMutex());
+ wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
+ wallet.SetLastBlockProcessed(newTip->nHeight, newTip->GetBlockHash());
+ }
+ AddKey(wallet, coinbaseKey);
+ WalletRescanReserver reserver(wallet);
+ std::chrono::steady_clock::time_point fake_time;
+ reserver.setNow([&] { fake_time += 60s; return fake_time; });
+ reserver.reserve();
+
+ {
+ CBlockLocator locator;
+ BOOST_CHECK(WalletBatch{wallet.GetDatabase()}.ReadBestBlock(locator));
+ BOOST_REQUIRE(!locator.IsNull());
+ BOOST_CHECK(locator.vHave.front() == newTip->GetBlockHash());
+ }
+
+ ScanResult result = wallet.Scanner().Scan(/*start_block=*/oldTip->GetBlockHash(), /*start_height=*/oldTip->nHeight, /*max_height=*/{}, reserver, /*save_progress=*/true);
+ BOOST_CHECK_EQUAL(result.status, ScanResult::SUCCESS);
+ BOOST_CHECK(result.last_failed_block.IsNull());
+ BOOST_CHECK_EQUAL(result.last_scanned_block, newTip->GetBlockHash());
+ BOOST_CHECK_EQUAL(*result.last_scanned_height, newTip->nHeight);
+ BOOST_CHECK_EQUAL(GetBalance(wallet).m_mine_immature, 100 * COIN);
+
+ {
+ CBlockLocator locator;
+ BOOST_CHECK(WalletBatch{wallet.GetDatabase()}.ReadBestBlock(locator));
+ BOOST_REQUIRE(!locator.IsNull());
+ BOOST_CHECK(locator.vHave.front() == newTip->GetBlockHash());
+ }
+ }
+
+ // Prune the older block file.
+ int file_number;
+ {
+ LOCK(cs_main);
+ file_number = oldTip->GetBlockPos().nFile;
+ Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(file_number);
+ }
+ m_node.chainman->m_blockman.UnlinkPrunedFiles({file_number});
+
+ // Verify Scan only picks transactions in the new block
+ // file.
+ {
+ CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
+ {
+ LOCK(wallet.cs_wallet);
+ LOCK(Assert(m_node.chainman)->GetMutex());
+ wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
+ wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
+ }
+ AddKey(wallet, coinbaseKey);
+ WalletRescanReserver reserver(wallet);
+ reserver.reserve();
+ ScanResult result = wallet.Scanner().Scan(/*start_block=*/oldTip->GetBlockHash(), /*start_height=*/oldTip->nHeight, /*max_height=*/{}, reserver, /*save_progress=*/false);
+ BOOST_CHECK_EQUAL(result.status, ScanResult::FAILURE);
+ BOOST_CHECK_EQUAL(result.last_failed_block, oldTip->GetBlockHash());
+ BOOST_CHECK_EQUAL(result.last_scanned_block, newTip->GetBlockHash());
+ BOOST_CHECK_EQUAL(*result.last_scanned_height, newTip->nHeight);
+ BOOST_CHECK_EQUAL(GetBalance(wallet).m_mine_immature, 50 * COIN);
+ }
+
+ // Prune the remaining block file.
+ {
+ LOCK(cs_main);
+ file_number = newTip->GetBlockPos().nFile;
+ Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(file_number);
+ }
+ m_node.chainman->m_blockman.UnlinkPrunedFiles({file_number});
+
+ // Verify Scan scans no blocks.
+ {
+ CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
+ {
+ LOCK(wallet.cs_wallet);
+ LOCK(Assert(m_node.chainman)->GetMutex());
+ wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
+ wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
+ }
+ AddKey(wallet, coinbaseKey);
+ WalletRescanReserver reserver(wallet);
+ reserver.reserve();
+ ScanResult result = wallet.Scanner().Scan(/*start_block=*/oldTip->GetBlockHash(), /*start_height=*/oldTip->nHeight, /*max_height=*/{}, reserver, /*save_progress=*/false);
+ BOOST_CHECK_EQUAL(result.status, ScanResult::FAILURE);
+ BOOST_CHECK_EQUAL(result.last_failed_block, newTip->GetBlockHash());
+ BOOST_CHECK(result.last_scanned_block.IsNull());
+ BOOST_CHECK(!result.last_scanned_height);
+ BOOST_CHECK_EQUAL(GetBalance(wallet).m_mine_immature, 0);
+ }
+}
+
+BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions_reorged_block, TestChain100Setup)
+{
+ BOOST_REQUIRE(InitBlockFilterIndex([&]{ return interfaces::MakeChain(m_node); }, BlockFilterType::BASIC, 1_MiB, /*f_memory=*/true));
+ BlockFilterIndex& filter_index{*Assert(GetBlockFilterIndex(BlockFilterType::BASIC))};
+ BOOST_REQUIRE(filter_index.Init());
+ filter_index.Sync();
+
+ // Reorg the tip out of the active chain: invalidate it, then mine a
+ // longer replacement branch paying a script unrelated to the wallets
+ // below.
+ CBlockIndex* stale_block = WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain().Tip());
+ const uint256 stale_hash{stale_block->GetBlockHash()};
+ const int stale_height{stale_block->nHeight};
+ BlockValidationState state;
+ BOOST_REQUIRE(m_node.chainman->ActiveChainstate().InvalidateBlock(state, stale_block));
+ const CScript replacement_script{GetScriptForRawPubKey(GenerateRandomKey().GetPubKey())};
+ CreateAndProcessBlock({}, replacement_script);
+ CreateAndProcessBlock({}, replacement_script);
+ BOOST_REQUIRE(filter_index.BlockUntilSyncedToCurrentChain());
+ {
+ LOCK(Assert(m_node.chainman)->GetMutex());
+ BOOST_REQUIRE(!m_node.chainman->ActiveChain().Contains(*stale_block));
+ BOOST_REQUIRE_EQUAL(m_node.chainman->ActiveChain().Height(), stale_height + 1);
+ }
+
+ {
+ BlockFilter filter;
+ BOOST_REQUIRE(filter_index.LookupFilter(stale_block, filter));
+ }
+
+ // Test wallet whose scripts do not match the stale block's filter.
+ {
+ CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
+ {
+ LOCK(wallet.cs_wallet);
+ LOCK(Assert(m_node.chainman)->GetMutex());
+ wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
+ wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
+ }
+ WalletRescanReserver reserver(wallet);
+ reserver.reserve();
+ ScanResult result = wallet.Scanner().Scan(stale_hash, stale_height, /*max_height=*/{}, reserver, /*save_progress=*/false);
+ BOOST_CHECK_EQUAL(result.status, ScanResult::SUCCESS);
+ BOOST_CHECK(result.last_failed_block.IsNull());
+ BOOST_CHECK_EQUAL(result.last_scanned_block, stale_hash);
+ BOOST_CHECK_EQUAL(*result.last_scanned_height, stale_height);
+ }
+
+ // Test wallet whose scripts do match the stale block's filter.
+ {
+ CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
+ {
+ LOCK(wallet.cs_wallet);
+ LOCK(Assert(m_node.chainman)->GetMutex());
+ wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
+ wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
+ }
+ AddKey(wallet, coinbaseKey); // the stale block's coinbase pays coinbaseKey
+ WalletRescanReserver reserver(wallet);
+ reserver.reserve();
+ ScanResult result = wallet.Scanner().Scan(stale_hash, stale_height, /*max_height=*/{}, reserver, /*save_progress=*/false);
+ BOOST_CHECK_EQUAL(result.status, ScanResult::FAILURE);
+ BOOST_CHECK_EQUAL(result.last_failed_block, stale_hash);
+ BOOST_CHECK(result.last_scanned_block.IsNull());
+ BOOST_CHECK(!result.last_scanned_height);
+ BOOST_CHECK(WITH_LOCK(wallet.cs_wallet, return wallet.mapWallet.empty()));
+ }
+
+ // Prune the stale block's file — the block is now not active AND unreadable.
+ int file_number;
+ {
+ LOCK(cs_main);
+ file_number = stale_block->GetBlockPos().nFile;
+ Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(file_number);
+ }
+ m_node.chainman->m_blockman.UnlinkPrunedFiles({file_number});
+
+ {
+ CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
+ {
+ LOCK(wallet.cs_wallet);
+ LOCK(Assert(m_node.chainman)->GetMutex());
+ wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
+ wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
+ }
+ AddKey(wallet, coinbaseKey);
+ WalletRescanReserver reserver(wallet);
+ reserver.reserve();
+ ScanResult result = wallet.Scanner().Scan(stale_hash, stale_height, /*max_height=*/{}, reserver, /*save_progress=*/false);
+ BOOST_CHECK_EQUAL(result.status, ScanResult::FAILURE);
+ BOOST_CHECK_EQUAL(result.last_failed_block, stale_hash);
+ BOOST_CHECK(result.last_scanned_block.IsNull());
+ BOOST_CHECK(!result.last_scanned_height);
+ BOOST_CHECK(WITH_LOCK(wallet.cs_wallet, return wallet.mapWallet.empty()));
+ }
+
+ StopIndex(filter_index, m_node);
+ BOOST_REQUIRE(DestroyBlockFilterIndex(BlockFilterType::BASIC));
+}
+
+BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions_abort, TestChain100Setup)
+{
+ CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
+ uint256 genesis_hash;
+ {
+ LOCK(wallet.cs_wallet);
+ LOCK(Assert(m_node.chainman)->GetMutex());
+ wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
+ wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
+ genesis_hash = m_node.chainman->ActiveChain().Genesis()->GetBlockHash();
+ }
+
+ // An abort requested while no rescan is held is stale and must
+ // not cancel a later scan.
+ wallet.Scanner().Abort();
+ WalletRescanReserver reserver(wallet);
+ BOOST_CHECK(reserver.reserve());
+ BOOST_CHECK(!wallet.Scanner().IsAborting());
+
+ // An abort requested after the reservation but before the scan starts
+ // (e.g. while importdescriptors is still deriving keys) must cancel the
+ // scan.
+ wallet.Scanner().Abort();
+ ScanResult result = wallet.Scanner().Scan(genesis_hash, /*start_height=*/0, /*max_height=*/{}, reserver, /*save_progress=*/false);
+ BOOST_CHECK_EQUAL(result.status, ScanResult::USER_ABORT);
+ BOOST_CHECK(result.last_scanned_block.IsNull());
+ BOOST_CHECK(!result.last_scanned_height);
+ BOOST_CHECK(result.last_failed_block.IsNull());
+}
+
+BOOST_FIXTURE_TEST_CASE(wallet_rescan_reserver, TestingSetup)
+{
+ CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
+
+ // No scan in progress: accessors report idle state.
+ BOOST_CHECK(!wallet.Scanner().IsScanning());
+ BOOST_CHECK(wallet.Scanner().ScanningDuration() == SteadyClock::duration{});
+ BOOST_CHECK_EQUAL(wallet.Scanner().ScanningProgress(), 0.0);
+
+ {
+ WalletRescanReserver first_reserver(wallet);
+ BOOST_CHECK(first_reserver.reserve());
+ BOOST_CHECK(first_reserver.isReserved());
+ BOOST_CHECK(wallet.Scanner().IsScanning());
+ BOOST_CHECK(!wallet.Scanner().IsScanningWithPassphrase());
+ BOOST_CHECK_EQUAL(wallet.Scanner().ScanningProgress(), 0.0);
+
+ // Only one reservation can be held at a time.
+ WalletRescanReserver second_reserver(wallet);
+ BOOST_CHECK(!second_reserver.reserve());
+ BOOST_CHECK(!second_reserver.isReserved());
+ }
+ // Destroying the reserver (RAII) clears the scanning state.
+ BOOST_CHECK(!wallet.Scanner().IsScanning());
+
+ {
+ WalletRescanReserver passphrase_reserver(wallet);
+ BOOST_CHECK(passphrase_reserver.reserve(/*with_passphrase=*/true));
+ BOOST_CHECK(wallet.Scanner().IsScanningWithPassphrase());
+ }
+ BOOST_CHECK(!wallet.Scanner().IsScanningWithPassphrase());
+}
+
+BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions_bounded, TestChain100Setup)
+{
+ uint256 genesis_hash, max_hash, tip_hash;
+ int max_height, tip_height;
+ {
+ LOCK(Assert(m_node.chainman)->GetMutex());
+ genesis_hash = m_node.chainman->ActiveChain().Genesis()->GetBlockHash();
+ tip_height = m_node.chainman->ActiveChain().Height();
+ tip_hash = m_node.chainman->ActiveChain().Tip()->GetBlockHash();
+ max_height = tip_height - 2;
+ max_hash = m_node.chainman->ActiveChain()[max_height]->GetBlockHash();
+ }
+
+ // A scan with max_height set stops exactly at max_height and does not
+ // sync any blocks beyond it.
+ {
+ CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
+ {
+ LOCK(wallet.cs_wallet);
+ wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
+ wallet.SetLastBlockProcessed(tip_height, tip_hash);
+ }
+ AddKey(wallet, coinbaseKey);
+ WalletRescanReserver reserver(wallet);
+ reserver.reserve();
+ ScanResult result = wallet.Scanner().Scan(genesis_hash, /*start_height=*/0, max_height, reserver, /*save_progress=*/false);
+ BOOST_CHECK_EQUAL(result.status, ScanResult::SUCCESS);
+ BOOST_CHECK(result.last_failed_block.IsNull());
+ BOOST_CHECK_EQUAL(result.last_scanned_block, max_hash);
+ BOOST_CHECK_EQUAL(*result.last_scanned_height, max_height);
+ // One coinbase per block from height 1 through max_height.
+ BOOST_CHECK_EQUAL(WITH_LOCK(wallet.cs_wallet, return wallet.mapWallet.size()), static_cast<size_t>(max_height));
+ }
+
+ // A single-block range (start == max_height == tip) scans exactly that
+ // block.
+ {
+ CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
+ {
+ LOCK(wallet.cs_wallet);
+ wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
+ wallet.SetLastBlockProcessed(tip_height, tip_hash);
+ }
+ AddKey(wallet, coinbaseKey);
+ WalletRescanReserver reserver(wallet);
+ reserver.reserve();
+ ScanResult result = wallet.Scanner().Scan(tip_hash, tip_height, tip_height, reserver, /*save_progress=*/false);
+ BOOST_CHECK_EQUAL(result.status, ScanResult::SUCCESS);
+ BOOST_CHECK(result.last_failed_block.IsNull());
+ BOOST_CHECK_EQUAL(result.last_scanned_block, tip_hash);
+ BOOST_CHECK_EQUAL(*result.last_scanned_height, tip_height);
+ BOOST_CHECK_EQUAL(WITH_LOCK(wallet.cs_wallet, return wallet.mapWallet.size()), 1U);
+ }
+}
+
+BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions_tip_extension, TestChain100Setup)
+{
+ CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
+ uint256 genesis_hash;
+ int start_tip_height{0};
+ {
+ LOCK(wallet.cs_wallet);
+ LOCK(Assert(m_node.chainman)->GetMutex());
+ wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
+ start_tip_height = m_node.chainman->ActiveChain().Height();
+ wallet.SetLastBlockProcessed(start_tip_height, m_node.chainman->ActiveChain().Tip()->GetBlockHash());
+ genesis_hash = m_node.chainman->ActiveChain().Genesis()->GetBlockHash();
+ }
+ AddKey(wallet, coinbaseKey);
+
+ // Connect a block while the scan is running (the handler fires on the
+ // scanning thread as the scan starts) and advance the wallet's tip, as
+ // the blockConnected notification would. The scan must pick up the new
+ // tip instead of stopping at the height it started with.
+ uint256 new_tip_hash;
+ bool extended{false};
+ auto handler = wallet.ShowProgress.connect([&](const std::string&, int) {
+ if (extended) return;
+ extended = true;
+ CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
+ LOCK(wallet.cs_wallet);
+ LOCK(Assert(m_node.chainman)->GetMutex());
+ const CBlockIndex* new_tip = m_node.chainman->ActiveChain().Tip();
+ new_tip_hash = new_tip->GetBlockHash();
+ wallet.SetLastBlockProcessed(new_tip->nHeight, new_tip_hash);
+ });
+
+ WalletRescanReserver reserver(wallet);
+ reserver.reserve();
+ ScanResult result = wallet.Scanner().Scan(genesis_hash, /*start_height=*/0, /*max_height=*/{}, reserver, /*save_progress=*/false);
+ handler.disconnect();
+ BOOST_CHECK_EQUAL(result.status, ScanResult::SUCCESS);
+ BOOST_CHECK_EQUAL(result.last_scanned_block, new_tip_hash);
+ BOOST_CHECK_EQUAL(*result.last_scanned_height, start_tip_height + 1);
+}
+
+BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions_no_progress_saved, TestChain100Setup)
+{
+ CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
+ uint256 genesis_hash, tip_hash;
+ int max_height;
+ {
+ LOCK(wallet.cs_wallet);
+ LOCK(Assert(m_node.chainman)->GetMutex());
+ wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
+ tip_hash = m_node.chainman->ActiveChain().Tip()->GetBlockHash();
+ wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), tip_hash);
+ genesis_hash = m_node.chainman->ActiveChain().Genesis()->GetBlockHash();
+ max_height = m_node.chainman->ActiveChain().Height() - 2;
+ }
+ AddKey(wallet, coinbaseKey);
+
+ WalletRescanReserver reserver(wallet);
+ // Advance the clock on every call so that every scanned block would be
+ // eligible for a progress write if save_progress were set.
+ std::chrono::steady_clock::time_point fake_time;
+ reserver.setNow([&] { fake_time += 60s; return fake_time; });
+ reserver.reserve();
+
+ ScanResult result = wallet.Scanner().Scan(genesis_hash, /*start_height=*/0, max_height, reserver, /*save_progress=*/false);
+ BOOST_CHECK_EQUAL(result.status, ScanResult::SUCCESS);
+
+ // With save_progress=false the scan must not touch the wallet's best
+ // block record: it still points at the tip written when the descriptor
+ // was added, not at any block the scan visited.
+ CBlockLocator locator;
+ BOOST_CHECK(WalletBatch{wallet.GetDatabase()}.ReadBestBlock(locator));
+ BOOST_CHECK(!locator.IsNull());
+ BOOST_CHECK_EQUAL(locator.vHave.front(), tip_hash);
+}
+
+BOOST_FIXTURE_TEST_CASE(rescan_from_time, TestChain100Setup)
+{
+ // Cap last block file size, and mine new block in a new block file.
+ CBlockIndex* old_tip = WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain().Tip());
+ WITH_LOCK(::cs_main, m_node.chainman->m_blockman.GetBlockFileInfo(old_tip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE);
+ CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
+ CBlockIndex* new_tip = WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain().Tip());
+
+ // Prune the older block file.
+ int file_number;
+ {
+ LOCK(cs_main);
+ file_number = old_tip->GetBlockPos().nFile;
+ Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(file_number);
+ }
+ m_node.chainman->m_blockman.UnlinkPrunedFiles({file_number});
+
+ CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
+ {
+ LOCK(wallet.cs_wallet);
+ LOCK(Assert(m_node.chainman)->GetMutex());
+ wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
+ wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
+ }
+ AddKey(wallet, coinbaseKey);
+ WalletRescanReserver reserver(wallet);
+ reserver.reserve();
+
+ // Blocks before the prune point cannot be read: the returned timestamp
+ // is moved past the last unreadable block, telling the caller from when
+ // the rescan is actually complete.
+ const int64_t genesis_time{WITH_LOCK(::cs_main, return m_node.chainman->ActiveChain().Genesis()->GetBlockTime())};
+ BOOST_CHECK_EQUAL(wallet.Scanner().ScanFromTime(genesis_time, reserver),
+ WITH_LOCK(::cs_main, return old_tip->GetBlockTimeMax()) + TIMESTAMP_WINDOW + 1);
+
+ bool scan_logged{false};
+ DebugLogHelper scan_check{"Rescan started from block", [&](const std::string* s) {
+ if (s) scan_logged = true;
+ return false;
+ }};
+ // A timestamp past the tip requires no scanning and is returned unchanged.
+ const int64_t future_time{WITH_LOCK(::cs_main, return new_tip->GetBlockTimeMax()) + TIMESTAMP_WINDOW + 1};
+ BOOST_CHECK(!scan_logged);
+ BOOST_CHECK_EQUAL(wallet.Scanner().ScanFromTime(future_time, reserver), future_time);
+}
+
+BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions_missing_filter, TestChain100Setup)
+{
+ // Enable the block filter index but do not sync it: no filters are
+ // available, so the scan must inspect every block rather than treat
+ // the missing filters as misses and skip blocks.
+ BOOST_REQUIRE(InitBlockFilterIndex([&]{ return interfaces::MakeChain(m_node); }, BlockFilterType::BASIC, 1_MiB, /*f_memory=*/true));
+ BlockFilterIndex& filter_index{*Assert(GetBlockFilterIndex(BlockFilterType::BASIC))};
+ BOOST_REQUIRE(filter_index.Init());
+
+ {
+ CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
+ uint256 genesis_hash, tip_hash;
+ int tip_height;
+ {
+ LOCK(wallet.cs_wallet);
+ LOCK(Assert(m_node.chainman)->GetMutex());
+ wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
+ genesis_hash = m_node.chainman->ActiveChain().Genesis()->GetBlockHash();
+ tip_height = m_node.chainman->ActiveChain().Height();
+ auto tip{m_node.chainman->ActiveChain().Tip()};
+ tip_hash = tip->GetBlockHash();
+ wallet.SetLastBlockProcessed(tip_height, tip_hash);
+ BlockFilter filter;
+ BOOST_REQUIRE(!filter_index.LookupFilter(tip, filter));
+ }
+ AddKey(wallet, coinbaseKey);
+ WalletRescanReserver reserver(wallet);
+ reserver.reserve();
+ bool fast_scan_logged{false};
+ DebugLogHelper scan_check{"fast variant using block filters", [&](const std::string* s) {
+ if (s) fast_scan_logged = true;
+ return false;
+ }};
+ ScanResult result = wallet.Scanner().Scan(genesis_hash, /*start_height=*/0, /*max_height=*/{}, reserver, /*save_progress=*/false);
+ BOOST_REQUIRE(fast_scan_logged);
+ BOOST_CHECK_EQUAL(result.status, ScanResult::SUCCESS);
+ BOOST_CHECK(result.last_failed_block.IsNull());
+ BOOST_CHECK_EQUAL(result.last_scanned_block, tip_hash);
+ BOOST_CHECK_EQUAL(*result.last_scanned_height, tip_height);
+ // One coinbase per block from height 1 through the tip.
+ BOOST_CHECK_EQUAL(WITH_LOCK(wallet.cs_wallet, return wallet.mapWallet.size()), static_cast<size_t>(tip_height));
+ }
+
+ StopIndex(filter_index, m_node);
+ BOOST_REQUIRE(DestroyBlockFilterIndex(BlockFilterType::BASIC));
+}
+
+//! Test the rescan that loading a wallet performs when the wallet is behind
+//! the chain tip: it scans from the wallet's recorded best block - a
+//! mid-chain start - with cs_wallet held.
+BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions_attach_chain, TestChain100Setup)
+{
+ // Do not wait for sqlite to flush data to disk to improve performance
+ m_args.ForceSetArg("-unsafesqlitesync", "1");
+
+ // Create a wallet owning the coinbases, and unload it at the current tip.
+ WalletContext context;
+ context.args = &m_args;
+ context.chain = m_node.chain.get();
+ auto wallet = TestCreateWallet(context);
+ AddKey(*wallet, coinbaseKey);
+ TestUnloadWallet(std::move(wallet));
+
+ // Extend the chain while the wallet is not loaded.
+ constexpr int NEW_BLOCKS{5};
+ for (int i = 0; i < NEW_BLOCKS; ++i) {
+ CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
+ }
+
+ int tip_height;
+ uint256 tip_hash;
+ {
+ LOCK(Assert(m_node.chainman)->GetMutex());
+ tip_height = m_node.chainman->ActiveChain().Height();
+ tip_hash = m_node.chainman->ActiveChain().Tip()->GetBlockHash();
+ }
+
+ // Loading the wallet must rescan the extension from the recorded best
+ // block and find its coinbases.
+ wallet = TestLoadWallet(context);
+ {
+ LOCK(wallet->cs_wallet);
+ BOOST_CHECK_EQUAL(wallet->GetLastBlockHeight(), tip_height);
+ BOOST_CHECK_EQUAL(wallet->GetLastBlockHash(), tip_hash);
+ // The extension's coinbases plus the one of the recorded best block:
+ // the load rescan starts mid-chain, at that block inclusive.
+ BOOST_CHECK_EQUAL(wallet->mapWallet.size(), static_cast<size_t>(NEW_BLOCKS + 1));
+ }
+ TestUnloadWallet(std::move(wallet));
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace wallet
### src/wallet/test/util.cpp
@@ -30,15 +30,8 @@ std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cc
LOCK(wallet->cs_wallet);
wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
wallet->SetupDescriptorScriptPubKeyMans();
-
- FlatSigningProvider provider;
- std::string error;
- auto descs = Parse("combo(" + EncodeSecret(key) + ")", provider, error, /* require_checksum=*/ false);
- assert(descs.size() == 1);
- auto& desc = descs.at(0);
- WalletDescriptor w_desc(std::move(desc), 0, 0, 1, 1);
- Assert(wallet->AddWalletDescriptor(w_desc, provider, "", false));
}
+ AddKey(*wallet, key);
WalletRescanReserver reserver(*wallet);
reserver.reserve();
ScanResult result = wallet->Scanner().Scan(cchain.Genesis()->GetBlockHash(), /*start_height=*/0, /*max_height=*/{}, reserver, /*save_progress=*/false);
@@ -49,6 +42,18 @@ std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cc
return wallet;
}
+void AddKey(CWallet& wallet, const CKey& key)
+{
+ LOCK(wallet.cs_wallet);
+ FlatSigningProvider provider;
+ std::string error;
+ auto descs = Parse("combo(" + EncodeSecret(key) + ")", provider, error, /* require_checksum=*/ false);
+ assert(descs.size() == 1);
+ auto& desc = descs.at(0);
+ WalletDescriptor w_desc(std::move(desc), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/1, /*next_index=*/1);
+ Assert(wallet.AddWalletDescriptor(w_desc, provider, /*label=*/"", /*internal=*/false));
+}
+
std::shared_ptr<CWallet> TestCreateWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags)
{
bilingual_str _error;
### src/wallet/test/util.h
@@ -37,6 +37,8 @@ inline const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqq
std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key);
+void AddKey(CWallet& wallet, const CKey& key);
+
std::shared_ptr<CWallet> TestCreateWallet(WalletContext& context);
std::shared_ptr<CWallet> TestCreateWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags);
std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context);
### src/wallet/test/wallet_tests.cpp
@@ -2,7 +2,6 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
-#include <wallet/scan.h>
#include <wallet/wallet.h>
#include <array>
@@ -18,14 +17,11 @@
#include <vector>
#include <addresstype.h>
-#include <blockfilter.h>
#include <chain.h>
#include <consensus/tx_verify.h>
#include <consensus/validation.h>
-#include <index/blockfilterindex.h>
#include <interfaces/chain.h>
#include <key_io.h>
-#include <node/blockstorage.h>
#include <node/types.h>
#include <policy/feerate.h>
#include <policy/policy.h>
@@ -37,7 +33,6 @@
#include <test/util/random.h>
#include <test/util/setup_common.h>
#include <test/util/time.h>
-#include <util/byte_units.h>
#include <util/check.h>
#include <util/strencodings.h>
#include <util/translation.h>
@@ -53,8 +48,6 @@
#include <boost/test/unit_test.hpp>
-using node::MAX_BLOCKFILE_SIZE;
-
namespace wallet {
// Ensure that fee levels defined in the wallet are at least as high
@@ -90,18 +83,6 @@ static bool BroadcastTestSimpleSpend(interfaces::Chain& chain, ChainstateManager
return chain.broadcastTransaction(tx_ref, DEFAULT_TRANSACTION_MAXFEE, tx_feerate_limit, node::TxBroadcast::MEMPOOL_NO_BROADCAST, error);
}
-static void AddKey(CWallet& wallet, const CKey& key)
-{
- LOCK(wallet.cs_wallet);
- FlatSigningProvider provider;
- std::string error;
- auto descs = Parse("combo(" + EncodeSecret(key) + ")", provider, error, /* require_checksum=*/ false);
- assert(descs.size() == 1);
- auto& desc = descs.at(0);
- WalletDescriptor w_desc(std::move(desc), 0, 0, 1, 1);
- Assert(wallet.AddWalletDescriptor(w_desc, provider, "", false));
-}
-
BOOST_AUTO_TEST_CASE(reject_invalid_descriptor_ranges)
{
const int height{*Assert(m_node.chain->getHeight())};
@@ -338,561 +319,6 @@ BOOST_FIXTURE_TEST_CASE(update_non_range_descriptor, TestingSetup)
}
}
-BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
-{
- // Cap last block file size, and mine new block in a new block file.
- CBlockIndex* oldTip = WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain().Tip());
- WITH_LOCK(::cs_main, m_node.chainman->m_blockman.GetBlockFileInfo(oldTip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE);
- CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
- CBlockIndex* newTip = WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain().Tip());
-
- // Verify Scan fails to read an unknown start block.
- {
- CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
- {
- LOCK(wallet.cs_wallet);
- LOCK(Assert(m_node.chainman)->GetMutex());
- wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
- wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
- }
- AddKey(wallet, coinbaseKey);
- WalletRescanReserver reserver(wallet);
- reserver.reserve();
- ScanResult result = wallet.Scanner().Scan(/*start_block=*/{}, /*start_height=*/0, /*max_height=*/{}, reserver, /*save_progress=*/false);
- BOOST_CHECK_EQUAL(result.status, ScanResult::FAILURE);
- BOOST_CHECK(result.last_failed_block.IsNull());
- BOOST_CHECK(result.last_scanned_block.IsNull());
- BOOST_CHECK(!result.last_scanned_height);
- BOOST_CHECK_EQUAL(GetBalance(wallet).m_mine_immature, 0);
- }
-
- // Verify Scan picks up transactions in both the old
- // and new block files.
- {
- CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
- {
- LOCK(wallet.cs_wallet);
- LOCK(Assert(m_node.chainman)->GetMutex());
- wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
- wallet.SetLastBlockProcessed(newTip->nHeight, newTip->GetBlockHash());
- }
- AddKey(wallet, coinbaseKey);
- WalletRescanReserver reserver(wallet);
- std::chrono::steady_clock::time_point fake_time;
- reserver.setNow([&] { fake_time += 60s; return fake_time; });
- reserver.reserve();
-
- {
- CBlockLocator locator;
- BOOST_CHECK(WalletBatch{wallet.GetDatabase()}.ReadBestBlock(locator));
- BOOST_REQUIRE(!locator.IsNull());
- BOOST_CHECK(locator.vHave.front() == newTip->GetBlockHash());
- }
-
- ScanResult result = wallet.Scanner().Scan(/*start_block=*/oldTip->GetBlockHash(), /*start_height=*/oldTip->nHeight, /*max_height=*/{}, reserver, /*save_progress=*/true);
- BOOST_CHECK_EQUAL(result.status, ScanResult::SUCCESS);
- BOOST_CHECK(result.last_failed_block.IsNull());
- BOOST_CHECK_EQUAL(result.last_scanned_block, newTip->GetBlockHash());
- BOOST_CHECK_EQUAL(*result.last_scanned_height, newTip->nHeight);
- BOOST_CHECK_EQUAL(GetBalance(wallet).m_mine_immature, 100 * COIN);
-
- {
- CBlockLocator locator;
- BOOST_CHECK(WalletBatch{wallet.GetDatabase()}.ReadBestBlock(locator));
- BOOST_REQUIRE(!locator.IsNull());
- BOOST_CHECK(locator.vHave.front() == newTip->GetBlockHash());
- }
- }
-
- // Prune the older block file.
- int file_number;
- {
- LOCK(cs_main);
- file_number = oldTip->GetBlockPos().nFile;
- Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(file_number);
- }
- m_node.chainman->m_blockman.UnlinkPrunedFiles({file_number});
-
- // Verify Scan only picks transactions in the new block
- // file.
- {
- CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
- {
- LOCK(wallet.cs_wallet);
- LOCK(Assert(m_node.chainman)->GetMutex());
- wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
- wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
- }
- AddKey(wallet, coinbaseKey);
- WalletRescanReserver reserver(wallet);
- reserver.reserve();
- ScanResult result = wallet.Scanner().Scan(/*start_block=*/oldTip->GetBlockHash(), /*start_height=*/oldTip->nHeight, /*max_height=*/{}, reserver, /*save_progress=*/false);
- BOOST_CHECK_EQUAL(result.status, ScanResult::FAILURE);
- BOOST_CHECK_EQUAL(result.last_failed_block, oldTip->GetBlockHash());
- BOOST_CHECK_EQUAL(result.last_scanned_block, newTip->GetBlockHash());
- BOOST_CHECK_EQUAL(*result.last_scanned_height, newTip->nHeight);
- BOOST_CHECK_EQUAL(GetBalance(wallet).m_mine_immature, 50 * COIN);
- }
-
- // Prune the remaining block file.
- {
- LOCK(cs_main);
- file_number = newTip->GetBlockPos().nFile;
- Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(file_number);
- }
- m_node.chainman->m_blockman.UnlinkPrunedFiles({file_number});
-
- // Verify Scan scans no blocks.
- {
- CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
- {
- LOCK(wallet.cs_wallet);
- LOCK(Assert(m_node.chainman)->GetMutex());
- wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
- wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
- }
- AddKey(wallet, coinbaseKey);
- WalletRescanReserver reserver(wallet);
- reserver.reserve();
- ScanResult result = wallet.Scanner().Scan(/*start_block=*/oldTip->GetBlockHash(), /*start_height=*/oldTip->nHeight, /*max_height=*/{}, reserver, /*save_progress=*/false);
- BOOST_CHECK_EQUAL(result.status, ScanResult::FAILURE);
- BOOST_CHECK_EQUAL(result.last_failed_block, newTip->GetBlockHash());
- BOOST_CHECK(result.last_scanned_block.IsNull());
- BOOST_CHECK(!result.last_scanned_height);
- BOOST_CHECK_EQUAL(GetBalance(wallet).m_mine_immature, 0);
- }
-}
-
-BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions_reorged_block, TestChain100Setup)
-{
- BOOST_REQUIRE(InitBlockFilterIndex([&]{ return interfaces::MakeChain(m_node); }, BlockFilterType::BASIC, 1_MiB, /*f_memory=*/true));
- BlockFilterIndex& filter_index{*Assert(GetBlockFilterIndex(BlockFilterType::BASIC))};
- BOOST_REQUIRE(filter_index.Init());
- filter_index.Sync();
-
- // Reorg the tip out of the active chain: invalidate it, then mine a
- // longer replacement branch paying a script unrelated to the wallets
- // below.
- CBlockIndex* stale_block = WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain().Tip());
- const uint256 stale_hash{stale_block->GetBlockHash()};
- const int stale_height{stale_block->nHeight};
- BlockValidationState state;
- BOOST_REQUIRE(m_node.chainman->ActiveChainstate().InvalidateBlock(state, stale_block));
- const CScript replacement_script{GetScriptForRawPubKey(GenerateRandomKey().GetPubKey())};
- CreateAndProcessBlock({}, replacement_script);
- CreateAndProcessBlock({}, replacement_script);
- BOOST_REQUIRE(filter_index.BlockUntilSyncedToCurrentChain());
- {
- LOCK(Assert(m_node.chainman)->GetMutex());
- BOOST_REQUIRE(!m_node.chainman->ActiveChain().Contains(*stale_block));
- BOOST_REQUIRE_EQUAL(m_node.chainman->ActiveChain().Height(), stale_height + 1);
- }
-
- {
- BlockFilter filter;
- BOOST_REQUIRE(filter_index.LookupFilter(stale_block, filter));
- }
-
- // Test wallet whose scripts do not match the stale block's filter.
- {
- CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
- {
- LOCK(wallet.cs_wallet);
- LOCK(Assert(m_node.chainman)->GetMutex());
- wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
- wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
- }
- WalletRescanReserver reserver(wallet);
- reserver.reserve();
- ScanResult result = wallet.Scanner().Scan(stale_hash, stale_height, /*max_height=*/{}, reserver, /*save_progress=*/false);
- BOOST_CHECK_EQUAL(result.status, ScanResult::SUCCESS);
- BOOST_CHECK(result.last_failed_block.IsNull());
- BOOST_CHECK_EQUAL(result.last_scanned_block, stale_hash);
- BOOST_CHECK_EQUAL(*result.last_scanned_height, stale_height);
- }
-
- // Test wallet whose scripts do match the stale block's filter.
- {
- CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
- {
- LOCK(wallet.cs_wallet);
- LOCK(Assert(m_node.chainman)->GetMutex());
- wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
- wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
- }
- AddKey(wallet, coinbaseKey); // the stale block's coinbase pays coinbaseKey
- WalletRescanReserver reserver(wallet);
- reserver.reserve();
- ScanResult result = wallet.Scanner().Scan(stale_hash, stale_height, /*max_height=*/{}, reserver, /*save_progress=*/false);
- BOOST_CHECK_EQUAL(result.status, ScanResult::FAILURE);
- BOOST_CHECK_EQUAL(result.last_failed_block, stale_hash);
- BOOST_CHECK(result.last_scanned_block.IsNull());
- BOOST_CHECK(!result.last_scanned_height);
- BOOST_CHECK(WITH_LOCK(wallet.cs_wallet, return wallet.mapWallet.empty()));
- }
-
- // Prune the stale block's file — the block is now not active AND unreadable.
- int file_number;
- {
- LOCK(cs_main);
- file_number = stale_block->GetBlockPos().nFile;
- Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(file_number);
- }
- m_node.chainman->m_blockman.UnlinkPrunedFiles({file_number});
-
- {
- CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
- {
- LOCK(wallet.cs_wallet);
- LOCK(Assert(m_node.chainman)->GetMutex());
- wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
- wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
- }
- AddKey(wallet, coinbaseKey);
- WalletRescanReserver reserver(wallet);
- reserver.reserve();
- ScanResult result = wallet.Scanner().Scan(stale_hash, stale_height, /*max_height=*/{}, reserver, /*save_progress=*/false);
- BOOST_CHECK_EQUAL(result.status, ScanResult::FAILURE);
- BOOST_CHECK_EQUAL(result.last_failed_block, stale_hash);
- BOOST_CHECK(result.last_scanned_block.IsNull());
- BOOST_CHECK(!result.last_scanned_height);
- BOOST_CHECK(WITH_LOCK(wallet.cs_wallet, return wallet.mapWallet.empty()));
- }
-
- filter_index.Stop();
- BOOST_REQUIRE(DestroyBlockFilterIndex(BlockFilterType::BASIC));
-}
-
-BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions_abort, TestChain100Setup)
-{
- CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
- uint256 genesis_hash;
- {
- LOCK(wallet.cs_wallet);
- LOCK(Assert(m_node.chainman)->GetMutex());
- wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
- wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
- genesis_hash = m_node.chainman->ActiveChain().Genesis()->GetBlockHash();
- }
-
- // An abort requested while no rescan is held is stale and must
- // not cancel a later scan.
- wallet.Scanner().Abort();
- WalletRescanReserver reserver(wallet);
- BOOST_CHECK(reserver.reserve());
- BOOST_CHECK(!wallet.Scanner().IsAborting());
-
- // An abort requested after the reservation but before the scan starts
- // (e.g. while importdescriptors is still deriving keys) must cancel the
- // scan.
- wallet.Scanner().Abort();
- ScanResult result = wallet.Scanner().Scan(genesis_hash, /*start_height=*/0, /*max_height=*/{}, reserver, /*save_progress=*/false);
- BOOST_CHECK_EQUAL(result.status, ScanResult::USER_ABORT);
- BOOST_CHECK(result.last_scanned_block.IsNull());
- BOOST_CHECK(!result.last_scanned_height);
- BOOST_CHECK(result.last_failed_block.IsNull());
-}
-
-BOOST_FIXTURE_TEST_CASE(wallet_rescan_reserver, TestingSetup)
-{
- CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
-
- // No scan in progress: accessors report idle state.
- BOOST_CHECK(!wallet.Scanner().IsScanning());
- BOOST_CHECK(wallet.Scanner().ScanningDuration() == SteadyClock::duration{});
- BOOST_CHECK_EQUAL(wallet.Scanner().ScanningProgress(), 0.0);
-
- {
- WalletRescanReserver first_reserver(wallet);
- BOOST_CHECK(first_reserver.reserve());
- BOOST_CHECK(first_reserver.isReserved());
- BOOST_CHECK(wallet.Scanner().IsScanning());
- BOOST_CHECK(!wallet.Scanner().IsScanningWithPassphrase());
- BOOST_CHECK_EQUAL(wallet.Scanner().ScanningProgress(), 0.0);
-
- // Only one reservation can be held at a time.
- WalletRescanReserver second_reserver(wallet);
- BOOST_CHECK(!second_reserver.reserve());
- BOOST_CHECK(!second_reserver.isReserved());
- }
- // Destroying the reserver (RAII) clears the scanning state.
- BOOST_CHECK(!wallet.Scanner().IsScanning());
-
- {
- WalletRescanReserver passphrase_reserver(wallet);
- BOOST_CHECK(passphrase_reserver.reserve(/*with_passphrase=*/true));
- BOOST_CHECK(wallet.Scanner().IsScanningWithPassphrase());
- }
- BOOST_CHECK(!wallet.Scanner().IsScanningWithPassphrase());
-}
-
-BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions_bounded, TestChain100Setup)
-{
- uint256 genesis_hash, max_hash, tip_hash;
- int max_height, tip_height;
- {
- LOCK(Assert(m_node.chainman)->GetMutex());
- genesis_hash = m_node.chainman->ActiveChain().Genesis()->GetBlockHash();
- tip_height = m_node.chainman->ActiveChain().Height();
- tip_hash = m_node.chainman->ActiveChain().Tip()->GetBlockHash();
- max_height = tip_height - 2;
- max_hash = m_node.chainman->ActiveChain()[max_height]->GetBlockHash();
- }
-
- // A scan with max_height set stops exactly at max_height and does not
- // sync any blocks beyond it.
- {
- CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
- {
- LOCK(wallet.cs_wallet);
- wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
- wallet.SetLastBlockProcessed(tip_height, tip_hash);
- }
- AddKey(wallet, coinbaseKey);
- WalletRescanReserver reserver(wallet);
- reserver.reserve();
- ScanResult result = wallet.Scanner().Scan(genesis_hash, /*start_height=*/0, max_height, reserver, /*save_progress=*/false);
- BOOST_CHECK_EQUAL(result.status, ScanResult::SUCCESS);
- BOOST_CHECK(result.last_failed_block.IsNull());
- BOOST_CHECK_EQUAL(result.last_scanned_block, max_hash);
- BOOST_CHECK_EQUAL(*result.last_scanned_height, max_height);
- // One coinbase per block from height 1 through max_height.
- BOOST_CHECK_EQUAL(WITH_LOCK(wallet.cs_wallet, return wallet.mapWallet.size()), static_cast<size_t>(max_height));
- }
-
- // A single-block range (start == max_height == tip) scans exactly that
- // block.
- {
- CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
- {
- LOCK(wallet.cs_wallet);
- wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
- wallet.SetLastBlockProcessed(tip_height, tip_hash);
- }
- AddKey(wallet, coinbaseKey);
- WalletRescanReserver reserver(wallet);
- reserver.reserve();
- ScanResult result = wallet.Scanner().Scan(tip_hash, tip_height, tip_height, reserver, /*save_progress=*/false);
- BOOST_CHECK_EQUAL(result.status, ScanResult::SUCCESS);
- BOOST_CHECK(result.last_failed_block.IsNull());
- BOOST_CHECK_EQUAL(result.last_scanned_block, tip_hash);
- BOOST_CHECK_EQUAL(*result.last_scanned_height, tip_height);
- BOOST_CHECK_EQUAL(WITH_LOCK(wallet.cs_wallet, return wallet.mapWallet.size()), 1U);
- }
-}
-
-BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions_tip_extension, TestChain100Setup)
-{
- CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
- uint256 genesis_hash;
- int start_tip_height{0};
- {
- LOCK(wallet.cs_wallet);
- LOCK(Assert(m_node.chainman)->GetMutex());
- wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
- start_tip_height = m_node.chainman->ActiveChain().Height();
- wallet.SetLastBlockProcessed(start_tip_height, m_node.chainman->ActiveChain().Tip()->GetBlockHash());
- genesis_hash = m_node.chainman->ActiveChain().Genesis()->GetBlockHash();
- }
- AddKey(wallet, coinbaseKey);
-
- // Connect a block while the scan is running (the handler fires on the
- // scanning thread as the scan starts) and advance the wallet's tip, as
- // the blockConnected notification would. The scan must pick up the new
- // tip instead of stopping at the height it started with.
- uint256 new_tip_hash;
- bool extended{false};
- auto handler = wallet.ShowProgress.connect([&](const std::string&, int) {
- if (extended) return;
- extended = true;
- CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
- LOCK(wallet.cs_wallet);
- LOCK(Assert(m_node.chainman)->GetMutex());
- const CBlockIndex* new_tip = m_node.chainman->ActiveChain().Tip();
- new_tip_hash = new_tip->GetBlockHash();
- wallet.SetLastBlockProcessed(new_tip->nHeight, new_tip_hash);
- });
-
- WalletRescanReserver reserver(wallet);
- reserver.reserve();
- ScanResult result = wallet.Scanner().Scan(genesis_hash, /*start_height=*/0, /*max_height=*/{}, reserver, /*save_progress=*/false);
- handler.disconnect();
- BOOST_CHECK_EQUAL(result.status, ScanResult::SUCCESS);
- BOOST_CHECK_EQUAL(result.last_scanned_block, new_tip_hash);
- BOOST_CHECK_EQUAL(*result.last_scanned_height, start_tip_height + 1);
-}
-
-BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions_no_progress_saved, TestChain100Setup)
-{
- CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
- uint256 genesis_hash, tip_hash;
- int max_height;
- {
- LOCK(wallet.cs_wallet);
- LOCK(Assert(m_node.chainman)->GetMutex());
- wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
- tip_hash = m_node.chainman->ActiveChain().Tip()->GetBlockHash();
- wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), tip_hash);
- genesis_hash = m_node.chainman->ActiveChain().Genesis()->GetBlockHash();
- max_height = m_node.chainman->ActiveChain().Height() - 2;
- }
- AddKey(wallet, coinbaseKey);
-
- WalletRescanReserver reserver(wallet);
- // Advance the clock on every call so that every scanned block would be
- // eligible for a progress write if save_progress were set.
- std::chrono::steady_clock::time_point fake_time;
- reserver.setNow([&] { fake_time += 60s; return fake_time; });
- reserver.reserve();
-
- ScanResult result = wallet.Scanner().Scan(genesis_hash, /*start_height=*/0, max_height, reserver, /*save_progress=*/false);
- BOOST_CHECK_EQUAL(result.status, ScanResult::SUCCESS);
-
- // With save_progress=false the scan must not touch the wallet's best
- // block record: it still points at the tip written when the descriptor
- // was added, not at any block the scan visited.
- CBlockLocator locator;
- BOOST_CHECK(WalletBatch{wallet.GetDatabase()}.ReadBestBlock(locator));
- BOOST_CHECK(!locator.IsNull());
- BOOST_CHECK_EQUAL(locator.vHave.front(), tip_hash);
-}
-
-BOOST_FIXTURE_TEST_CASE(rescan_from_time, TestChain100Setup)
-{
- // Cap last block file size, and mine new block in a new block file.
- CBlockIndex* old_tip = WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain().Tip());
- WITH_LOCK(::cs_main, m_node.chainman->m_blockman.GetBlockFileInfo(old_tip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE);
- CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
- CBlockIndex* new_tip = WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain().Tip());
-
- // Prune the older block file.
- int file_number;
- {
- LOCK(cs_main);
- file_number = old_tip->GetBlockPos().nFile;
- Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(file_number);
- }
- m_node.chainman->m_blockman.UnlinkPrunedFiles({file_number});
-
- CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
- {
- LOCK(wallet.cs_wallet);
- LOCK(Assert(m_node.chainman)->GetMutex());
- wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
- wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
- }
- AddKey(wallet, coinbaseKey);
- WalletRescanReserver reserver(wallet);
- reserver.reserve();
-
- // Blocks before the prune point cannot be read: the returned timestamp
- // is moved past the last unreadable block, telling the caller from when
- // the rescan is actually complete.
- const int64_t genesis_time{WITH_LOCK(::cs_main, return m_node.chainman->ActiveChain().Genesis()->GetBlockTime())};
- BOOST_CHECK_EQUAL(wallet.Scanner().ScanFromTime(genesis_time, reserver),
- WITH_LOCK(::cs_main, return old_tip->GetBlockTimeMax()) + TIMESTAMP_WINDOW + 1);
-
- bool scan_logged{false};
- DebugLogHelper scan_check{"Rescan started from block", [&](const std::string* s) {
- if (s) scan_logged = true;
- return false;
- }};
- // A timestamp past the tip requires no scanning and is returned unchanged.
- const int64_t future_time{WITH_LOCK(::cs_main, return new_tip->GetBlockTimeMax()) + TIMESTAMP_WINDOW + 1};
- BOOST_CHECK(!scan_logged);
- BOOST_CHECK_EQUAL(wallet.Scanner().ScanFromTime(future_time, reserver), future_time);
-}
-
-BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions_missing_filter, TestChain100Setup)
-{
- // Enable the block filter index but do not sync it: no filters are
- // available, so the scan must inspect every block rather than treat
- // the missing filters as misses and skip blocks.
- BOOST_REQUIRE(InitBlockFilterIndex([&]{ return interfaces::MakeChain(m_node); }, BlockFilterType::BASIC, 1_MiB, /*f_memory=*/true));
- BlockFilterIndex& filter_index{*Assert(GetBlockFilterIndex(BlockFilterType::BASIC))};
- BOOST_REQUIRE(filter_index.Init());
-
- {
- CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
- uint256 genesis_hash, tip_hash;
- int tip_height;
- {
- LOCK(wallet.cs_wallet);
- LOCK(Assert(m_node.chainman)->GetMutex());
- wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
- genesis_hash = m_node.chainman->ActiveChain().Genesis()->GetBlockHash();
- tip_height = m_node.chainman->ActiveChain().Height();
- auto tip{m_node.chainman->ActiveChain().Tip()};
- tip_hash = tip->GetBlockHash();
- wallet.SetLastBlockProcessed(tip_height, tip_hash);
- BlockFilter filter;
- BOOST_REQUIRE(!filter_index.LookupFilter(tip, filter));
- }
- AddKey(wallet, coinbaseKey);
- WalletRescanReserver reserver(wallet);
- reserver.reserve();
- bool fast_scan_logged{false};
- DebugLogHelper scan_check{"fast variant using block filters", [&](const std::string* s) {
- if (s) fast_scan_logged = true;
- return false;
- }};
- ScanResult result = wallet.Scanner().Scan(genesis_hash, /*start_height=*/0, /*max_height=*/{}, reserver, /*save_progress=*/false);
- BOOST_REQUIRE(fast_scan_logged);
- BOOST_CHECK_EQUAL(result.status, ScanResult::SUCCESS);
- BOOST_CHECK(result.last_failed_block.IsNull());
- BOOST_CHECK_EQUAL(result.last_scanned_block, tip_hash);
- BOOST_CHECK_EQUAL(*result.last_scanned_height, tip_height);
- // One coinbase per block from height 1 through the tip.
- BOOST_CHECK_EQUAL(WITH_LOCK(wallet.cs_wallet, return wallet.mapWallet.size()), static_cast<size_t>(tip_height));
- }
-
- filter_index.Stop();
- BOOST_REQUIRE(DestroyBlockFilterIndex(BlockFilterType::BASIC));
-}
-
-//! Test the rescan that loading a wallet performs when the wallet is behind
-//! the chain tip: it scans from the wallet's recorded best block - a
-//! mid-chain start - with cs_wallet held.
-BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions_attach_chain, TestChain100Setup)
-{
- // Do not wait for sqlite to flush data to disk to improve performance
- m_args.ForceSetArg("-unsafesqlitesync", "1");
-
- // Create a wallet owning the coinbases, and unload it at the current tip.
- WalletContext context;
- context.args = &m_args;
- context.chain = m_node.chain.get();
- auto wallet = TestCreateWallet(context);
- AddKey(*wallet, coinbaseKey);
- TestUnloadWallet(std::move(wallet));
-
- // Extend the chain while the wallet is not loaded.
- constexpr int NEW_BLOCKS{5};
- for (int i = 0; i < NEW_BLOCKS; ++i) {
- CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
- }
-
- int tip_height;
- uint256 tip_hash;
- {
- LOCK(Assert(m_node.chainman)->GetMutex());
- tip_height = m_node.chainman->ActiveChain().Height();
- tip_hash = m_node.chainman->ActiveChain().Tip()->GetBlockHash();
- }
-
- // Loading the wallet must rescan the extension from the recorded best
- // block and find its coinbases.
- wallet = TestLoadWallet(context);
- {
- LOCK(wallet->cs_wallet);
- BOOST_CHECK_EQUAL(wallet->GetLastBlockHeight(), tip_height);
- BOOST_CHECK_EQUAL(wallet->GetLastBlockHash(), tip_hash);
- // The extension's coinbases plus the one of the recorded best block:
- // the load rescan starts mid-chain, at that block inclusive.
- BOOST_CHECK_EQUAL(wallet->mapWallet.size(), static_cast<size_t>(NEW_BLOCKS + 1));
- }
- TestUnloadWallet(std::move(wallet));
-}
// This test verifies that wallet settings can be added and removed
// concurrently, ensuring no race conditions occur during either process.
@@ -1297,9 +723,9 @@ BOOST_FIXTURE_TEST_CASE(CreateWallet, TestChain100Setup)
promise.get_future().wait();
});
std::string error;
- m_coinbase_txns.push_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
+ m_coinbase_txns.push_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()), /*sync=*/false).vtx[0]);
auto block_tx = TestSimpleSpend(*m_coinbase_txns[0], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey()));
- m_coinbase_txns.push_back(CreateAndProcessBlock({block_tx}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
+ m_coinbase_txns.push_back(CreateAndProcessBlock({block_tx}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()), /*sync=*/false).vtx[0]);
auto mempool_tx = TestSimpleSpend(*m_coinbase_txns[1], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey()));
BOOST_CHECK(BroadcastTestSimpleSpend(*m_node.chain, *Assert(m_node.chainman), mempool_tx, error));
Why this scored 17/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.