Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/wallet/test/wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <wallet/wallet.h>

#include <algorithm>
#include <functional>
#include <future>
#include <iostream>
#include <memory>
Expand All @@ -20,6 +21,7 @@
#include <evo/deterministicmns.h>
#include <evo/dmn_types.h>
#include <evo/specialtx.h>
#include <index/blockfilterindex.h>
#include <interfaces/chain.h>
#include <interfaces/wallet.h>
#include <key_io.h>
Expand All @@ -28,6 +30,7 @@
#include <policy/settings.h>
#include <rpc/rawtransaction_util.h>
#include <rpc/server.h>
#include <test/util/index.h>
#include <test/util/logging.h>
#include <test/util/masternode.h>
#include <test/util/random.h>
Expand Down Expand Up @@ -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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if test is really needed, what exactly does it test? Does it prevent any possible bugs or regressions?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is the regression test for the second commit, and each half fails without part of it:

  • Descriptor wallet: the key paying the coinbases is added after the rescan has built its block filter. On develop the filter is only built from the descriptors present at the start, so every block is skipped as a filter miss and the rescan reports success having found nothing. With the fix it finds all 100 coinbases.
  • Blank non-descriptor wallet: it gains a legacy key mid-scan, as importprivkey with rescan=false does. Without the WALLET_FLAG_DESCRIPTORS gate the rescan aborts on assert(desc_spkm != nullptr); with it the wallet falls back to the slow scan and finds the coins.

So it guards the silent miss (a rescan that succeeds while skipping payments) and the abort. The lock-order change in the first commit has no test, since only a scheduling hook can force that interleaving.


🤖 Posted autonomously by Claude on behalf of pasta.

{
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<void(CWallet&)>& 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.
Expand Down
46 changes: 22 additions & 24 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<DescriptorScriptPubKeyMan*>(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<DescriptorScriptPubKeyMan*>(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<DescriptorScriptPubKeyMan*>(spkm)};
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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;
}
}
}
Expand All @@ -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<uint256, int32_t> m_last_range_ends;
GCSFilter::ElementSet m_filter_set;
Expand Down Expand Up @@ -2134,7 +2132,7 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
ScanResult result;

std::unique_ptr<FastWalletRescanFilter> fast_rescan_filter;
if (!IsLegacy() && chain().hasBlockFilterIndex(BlockFilterType::BASIC_FILTER)) fast_rescan_filter = std::make_unique<FastWalletRescanFilter>(*this);
if (IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS) && chain().hasBlockFilterIndex(BlockFilterType::BASIC_FILTER)) fast_rescan_filter = std::make_unique<FastWalletRescanFilter>(*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");
Expand Down
Loading