wallet: do not save over the locator of a rescan that did not complete

A rescan started by RPC (rescan, z_importkey, z_importviewingkey,
importprivkey) that is cut short by abortrescan or by a stop writes its
progress as the best-block locator, or leaves the older locator when it
may not advance it. The next start resumes the scan from there. The
shutdown save then stamped the wallet tip over that locator, so the next
start found nothing to rescan: for a newly imported key, its notes in the
unscanned blocks stayed out of the wallet, with no warning, until a
manual rescan. On dev a session shorter than an hour never wrote at
shutdown, so this is a regression of the shutdown save.

ScanForWalletTransactions now records, for the session, where it left
the locator on disk: the persisted height when it starts (so a scan that
throws counts as incomplete too), the checkpoint or the old locator when
it is cut short. A scan that reaches the tip clears this, unless an
earlier scan was interrupted and this one starts above where that one
left the locator. While it is set, the shutdown save is skipped, as dev
would, and logs why.

The hourly SetBestChain after abortrescan is unchanged: it stamps the
wallet tip as it does on dev.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-28 12:27:43 -05:00
co-authored by Claude Opus 5.5
parent b4513498e5
commit ed13277dcb
3 changed files with 108 additions and 3 deletions
+44 -3
View File
@@ -881,6 +881,11 @@ const char* CWallet::ShutdownSaveSkipReason(bool fInitCompleted)
LOCK(cs_wallet);
if (pindexWalletTip == nullptr)
return "the wallet tip is not set";
// A rescan (rescan, z_importkey, ...) that was cut short wrote its own locator, or could not and
// left the older one. Stamping the wallet tip over that would mark the unscanned range as scanned,
// and a key imported with that scan would never see its notes there.
if (nInterruptedRescanHeight >= 0)
return "a rescan did not complete; the next start resumes it from the locator it left";
if (pindexWalletTip == pindexPersistedTip && nWitnessGeneration == nPersistedWitnessGeneration)
return "nothing changed since the last save";
return nullptr;
@@ -910,6 +915,31 @@ void CWallet::PersistStateAtShutdown(bool fInitCompleted)
nHeight, nTxs, GetTimeMillis() - nStart);
}
int CWallet::NoteRescanStarted(int nPersistedHeight)
{
LOCK(cs_wallet);
// Until the scan completes, assume it ends early, even by throwing: the next start then resumes
// from the locator on disk, as it would without a shutdown save.
int nBefore = nInterruptedRescanHeight;
nInterruptedRescanHeight = std::max(nPersistedHeight, 0);
return nBefore;
}
void CWallet::NoteRescanInterrupted(int nResumeHeight)
{
LOCK(cs_wallet);
nInterruptedRescanHeight = std::max(nResumeHeight, 0);
}
void CWallet::NoteRescanCompleted(int nStartHeight, int nInterruptedBefore)
{
LOCK(cs_wallet);
// A scan that starts above the locator on disk covers what the caller asked for. It is still
// incomplete if an earlier scan was interrupted and this one starts above where that left off.
if (nInterruptedBefore < 0 || nStartHeight <= nInterruptedBefore + 1)
nInterruptedRescanHeight = -1;
}
bool CWallet::SetBestChainNoFlush(const CBlockLocator& loc)
{
CWalletDB walletdb(strWalletFile, "r+", false);
@@ -3796,20 +3826,30 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
// already present. No witness work is needed: witnesses are re-derived from each note's own
// witnessHeight, and witnessRootValidated is in-memory-only so every note is revalidated
// against hashFinalSaplingRoot on the next start.
auto checkpointProgress = [&](const CBlockIndex* pindexStopped) {
const int nInterruptedBefore = NoteRescanStarted(pindexPersisted ? pindexPersisted->GetHeight() : 0);
// Returns the height the locator on disk is left at, which the next start resumes from.
auto writeCheckpoint = [&](const CBlockIndex* pindexStopped) {
const int nPersisted = pindexPersisted ? pindexPersisted->GetHeight() : 0;
if (!fMayCheckpoint || !pindexStopped || !pindexStopped->pprev)
return;
return nPersisted;
const CBlockIndex* pindexDone = pindexStopped->pprev;
if (pindexDone->GetHeight() <= pindexPersisted->GetHeight())
return; // never move the locator backwards
return nPersisted; // never move the locator backwards
if (SetBestChainNoFlush(chainActive.GetLocator(pindexDone))) {
LogPrintf("%s: checkpointed scan progress at height %d\n", __func__, pindexDone->GetHeight());
return pindexDone->GetHeight();
} else {
LogPrintf("%s: FAILED to checkpoint scan progress at height %d; the scan will replay "
"from height %d on the next start\n", __func__, pindexDone->GetHeight(),
pindexPersisted->GetHeight());
return nPersisted;
}
};
// Also keeps the shutdown save from stamping the wallet tip over what was written here.
auto checkpointProgress = [&](const CBlockIndex* pindexStopped) {
NoteRescanInterrupted(writeCheckpoint(pindexStopped));
};
ShowProgress(_("Rescanning..."), 0); // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup
double dProgressStart = Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), pindex, false);
@@ -3887,6 +3927,7 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
//Update all witness caches
BuildWitnessCache(chainActive.Tip(), false);
NoteRescanCompleted(pindexStart->GetHeight(), nInterruptedBefore);
ShowProgress(_("Rescanning..."), 100); // hide progress dialog in GUI
}