From deb80048fa2a76ccdff12917482ce4d2a182d812 Mon Sep 17 00:00:00 2001 From: pasta Date: Fri, 13 Nov 2026 02:50:12 -0600 Subject: [PATCH 1/2] fix(wallet): read descriptors under their locks in the block-filter rescan FastWalletRescanFilter walked the wallet's descriptor managers without cs_wallet, and read each one's scripts and range end in separate critical sections. A rescan holds neither lock while it runs, so: - a descriptor imported meanwhile by ensureFriendshipReceivingKeychain(), which does not reserve the rescan, inserts into the map the filter is walking; - a descriptor topped up or rebuilt between the two reads is taken with its old scripts, or none, but its new range end. The filter then never picks the missing scripts up, skips the blocks that pay them, and the rescan still reports success. The filter now reads the descriptor list under cs_wallet and each descriptor's scripts and range end together under cs_desc_man. Both are held only for that read. No test is added because only a scheduling hook inside the wallet code can force the interleaving. Co-Authored-By: Claude Fable 5.2 --- src/wallet/wallet.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 21961d6bb8c3..eb598977405c 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -353,10 +353,14 @@ class FastWalletRescanFilter // fast rescanning via block filters is only supported by descriptor wallets right now assert(!m_wallet.IsLegacy()); + // Descriptors are added and rebuilt under cs_wallet while a rescan + // runs, so each one is read whole. + LOCK(m_wallet.cs_wallet); // create initial filter with scripts from all ScriptPubKeyMans for (auto spkm : m_wallet.GetAllScriptPubKeyMans()) { auto desc_spkm{dynamic_cast(spkm)}; assert(desc_spkm != nullptr); + LOCK(desc_spkm->cs_desc_man); AddScriptPubKeys(desc_spkm); // save each range descriptor's end for possible future filter updates if (desc_spkm->IsHDEnabled()) { @@ -368,9 +372,11 @@ class FastWalletRescanFilter void UpdateIfNeeded() { // repopulate filter with new scripts if top-up has happened since last iteration + LOCK(m_wallet.cs_wallet); for (const auto& [desc_spkm_id, last_range_end] : m_last_range_ends) { auto desc_spkm{dynamic_cast(m_wallet.GetScriptPubKeyMan(desc_spkm_id))}; assert(desc_spkm != nullptr); + LOCK(desc_spkm->cs_desc_man); int32_t current_range_end{desc_spkm->GetEndRange()}; if (current_range_end > last_range_end) { AddScriptPubKeys(desc_spkm, last_range_end); From e00bcafbfdf2889b7d06b424bc561dd81535b841 Mon Sep 17 00:00:00 2001 From: pasta Date: Sat, 14 Nov 2026 14:37:11 +0000 Subject: [PATCH 2/2] fix(wallet): scan descriptors added during a block-filter rescan FastWalletRescanFilter only revisited the descriptors it saw when it was built. A descriptor added while the rescan runs was never put into the filter set, so every later block paying it was skipped as a filter miss and the rescan still reported success. Re-read the full descriptor list on every update and add any descriptor not seen before, so the filter covers the wallet's current descriptors. Build the filter only for wallets flagged as descriptor wallets. A blank wallet without the flag has no managers, so IsLegacy() is false, but importprivkey can give it a legacy manager mid-scan, which the filter cannot read. Co-Authored-By: Claude Opus 5.5 --- src/wallet/test/wallet_tests.cpp | 49 ++++++++++++++++++++++++++++++++ src/wallet/wallet.cpp | 48 +++++++++++++------------------ 2 files changed, 69 insertions(+), 28 deletions(-) diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp index 78692d2913ca..b4b0f9e334a7 100644 --- a/src/wallet/test/wallet_tests.cpp +++ b/src/wallet/test/wallet_tests.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -20,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -28,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -294,6 +297,52 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions_abort, TestChain100Setup) BOOST_CHECK(result.last_failed_block.IsNull()); } +BOOST_FIXTURE_TEST_CASE(fast_rescan_finds_key_added_mid_scan, TestChain100Setup) +{ + BOOST_REQUIRE(InitBlockFilterIndex([&] { return interfaces::MakeChain(m_node); }, BlockFilterType::BASIC_FILTER, 1 << 20, /*f_memory=*/true)); + BlockFilterIndex& filter_index{*Assert(GetBlockFilterIndex(BlockFilterType::BASIC_FILTER))}; + BOOST_REQUIRE(filter_index.Start()); + IndexWaitSynced(filter_index); + + // The first progress report comes after the rescan has built its block + // filter, so the key paying the coinbases is added behind the filter. + const auto rescan_adding_key{[&](bool descriptors, const std::function& add_key) { + CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase()); + uint256 genesis_hash; + { + LOCK(wallet.cs_wallet); + LOCK(Assert(m_node.chainman)->GetMutex()); + if (descriptors) 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(); + } + bool key_added{false}; + auto progress_connection{wallet.ShowProgress.connect([&](const std::string&, int) { + if (!std::exchange(key_added, true)) add_key(wallet); + })}; + WalletRescanReserver reserver(wallet); + BOOST_REQUIRE(reserver.reserve()); + const CWallet::ScanResult result{wallet.ScanForWalletTransactions(genesis_hash, /*start_height=*/0, /*max_height=*/{}, reserver, /*fUpdate=*/false, /*save_progress=*/false)}; + progress_connection.disconnect(); + + BOOST_CHECK(key_added); + BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::SUCCESS); + BOOST_CHECK_EQUAL(GetBalance(wallet).m_mine_immature, 500 * COIN * COINBASE_MATURITY); + }}; + + rescan_adding_key(/*descriptors=*/true, [&](CWallet& wallet) { AddKey(wallet, coinbaseKey); }); + // A blank wallet that is not a descriptor wallet can gain a legacy + // manager mid-scan, as importprivkey with rescan=false does. + rescan_adding_key(/*descriptors=*/false, [&](CWallet& wallet) { + auto spk_man{wallet.GetOrCreateLegacyScriptPubKeyMan()}; + LOCK2(wallet.cs_wallet, spk_man->cs_KeyStore); + spk_man->AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey()); + }); + + filter_index.Stop(); + BOOST_CHECK(DestroyBlockFilterIndex(BlockFilterType::BASIC_FILTER)); +} + BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup) { // Cap last block file size, and mine new block in a new block file. diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index eb598977405c..c38fb57597ec 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -351,36 +351,27 @@ class FastWalletRescanFilter FastWalletRescanFilter(const CWallet& wallet) : m_wallet(wallet) { // fast rescanning via block filters is only supported by descriptor wallets right now - assert(!m_wallet.IsLegacy()); + assert(m_wallet.IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)); - // Descriptors are added and rebuilt under cs_wallet while a rescan - // runs, so each one is read whole. - LOCK(m_wallet.cs_wallet); - // create initial filter with scripts from all ScriptPubKeyMans - for (auto spkm : m_wallet.GetAllScriptPubKeyMans()) { - auto desc_spkm{dynamic_cast(spkm)}; - assert(desc_spkm != nullptr); - LOCK(desc_spkm->cs_desc_man); - AddScriptPubKeys(desc_spkm); - // save each range descriptor's end for possible future filter updates - if (desc_spkm->IsHDEnabled()) { - m_last_range_ends.emplace(desc_spkm->GetID(), desc_spkm->GetEndRange()); - } - } + UpdateIfNeeded(); } void UpdateIfNeeded() { - // repopulate filter with new scripts if top-up has happened since last iteration + // Descriptors can be added, topped up or rebuilt while a rescan runs, + // so every pass re-reads the full list, and each descriptor whole. LOCK(m_wallet.cs_wallet); - for (const auto& [desc_spkm_id, last_range_end] : m_last_range_ends) { - auto desc_spkm{dynamic_cast(m_wallet.GetScriptPubKeyMan(desc_spkm_id))}; + for (auto spkm : m_wallet.GetAllScriptPubKeyMans()) { + auto desc_spkm{dynamic_cast(spkm)}; assert(desc_spkm != nullptr); LOCK(desc_spkm->cs_desc_man); - int32_t current_range_end{desc_spkm->GetEndRange()}; - if (current_range_end > last_range_end) { - AddScriptPubKeys(desc_spkm, last_range_end); - m_last_range_ends.at(desc_spkm->GetID()) = current_range_end; + const int32_t current_range_end{desc_spkm->GetEndRange()}; + const auto [it, inserted]{m_last_range_ends.try_emplace(desc_spkm->GetID(), current_range_end)}; + if (inserted) { + AddScriptPubKeys(desc_spkm); + } else if (current_range_end > it->second) { + AddScriptPubKeys(desc_spkm, it->second); + it->second = current_range_end; } } } @@ -392,11 +383,12 @@ class FastWalletRescanFilter private: const CWallet& m_wallet; - /** Map for keeping track of each range descriptor's last seen end range. - * This information is used to detect whether new addresses were derived - * (that is, if the current end range is larger than the saved end range) - * after processing a block and hence a filter set update is needed to - * take possible keypool top-ups into account. + /** Map for keeping track of each descriptor's last seen end range. + * This information is used to detect descriptors added since the last + * update, and whether new addresses were derived (that is, if the current + * end range is larger than the saved end range) after processing a block + * and hence a filter set update is needed to take possible keypool + * top-ups into account. */ std::map m_last_range_ends; GCSFilter::ElementSet m_filter_set; @@ -2140,7 +2132,7 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc ScanResult result; std::unique_ptr fast_rescan_filter; - if (!IsLegacy() && chain().hasBlockFilterIndex(BlockFilterType::BASIC_FILTER)) fast_rescan_filter = std::make_unique(*this); + if (IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS) && chain().hasBlockFilterIndex(BlockFilterType::BASIC_FILTER)) fast_rescan_filter = std::make_unique(*this); WalletLogPrintf("Rescan started from block %s... (%s)\n", start_block.ToString(), fast_rescan_filter ? "fast variant using block filters" : "slow variant inspecting all blocks");