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 21961d6bb8c3..c38fb57597ec 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -351,30 +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)); - // create initial filter with scripts from all ScriptPubKeyMans - for (auto spkm : m_wallet.GetAllScriptPubKeyMans()) { - auto desc_spkm{dynamic_cast(spkm)}; - assert(desc_spkm != nullptr); - 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 - for (const auto& [desc_spkm_id, last_range_end] : m_last_range_ends) { - auto desc_spkm{dynamic_cast(m_wallet.GetScriptPubKeyMan(desc_spkm_id))}; + // 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 (auto spkm : m_wallet.GetAllScriptPubKeyMans()) { + auto desc_spkm{dynamic_cast(spkm)}; assert(desc_spkm != nullptr); - 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; + LOCK(desc_spkm->cs_desc_man); + 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; } } } @@ -386,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; @@ -2134,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");