wallet: keep an interrupted -zapwallettxes / -rescan from hiding the unscanned range
-zapwallettxes erases every wallet transaction but leaves the best-block locator at the old tip. The forced rescan that follows starts at genesis, and ScanForWalletTransactions only ever moves the locator forward, so when that rescan was cut short -- killed, or a clean shutdown -- the next normal start read the old locator, skipped the rescan, and the wallet came up without the erased transactions: zero balance and no notes until someone ran -rescan. Plain -rescan had the same gap. - Before a forced rescan that starts below the locator on disk, move the locator back to where the scan starts (RewindWalletLocatorForRescan). An interrupted scan then checkpoints forward from there, and a killed one restarts from there. With -zapwallettxes this is done right after the zap, so a startup that fails in between (a config error, say) cannot reopen the gap. - SetBestChainWith no longer stamps the chain tip while the startup rescan is running or was cut short and init has not seeded the wallet tip yet. That stamp comes from FlushStateToDisk (its hourly write, or the one in Shutdown) and would overwrite the scan's own locator the same way. Regtest, dev vs this branch on the same fixture (141 blocks, 26 notes, 146 txs, 430.0496418 DRGX), stopping the node under gdb as the zap rescan begins and then starting it normally: dev comes up empty (0 balance, 0 notes, 0 txs) after both SIGKILL and SIGTERM; this branch resumes the rescan from block 0 and restores exactly the baseline. New gtest WalletShutdownSave.DoesNotStampTheChainTipOverAnIncompleteStartupRescan. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -381,3 +381,46 @@ TEST_F(WalletShutdownSaveDb, AddingANoteNeedsASave) {
|
||||
ASSERT_TRUE(wallet.AddToWallet(wtx, false, &walletdb));
|
||||
EXPECT_EQ("nothing changed since the last save", Reason(wallet, true)) << "the same tx again";
|
||||
}
|
||||
|
||||
// While the startup rescan is running or was cut short, init has not seeded the wallet tip yet, and the
|
||||
// only stamp that can arrive is FlushStateToDisk's chain tip (its hourly write, or the one in Shutdown).
|
||||
// Writing that over the scan's own locator would mark the unscanned range as scanned -- after
|
||||
// -zapwallettxes, its transactions would be gone from the wallet. It must write nothing.
|
||||
TEST_F(WalletShutdownSave, DoesNotStampTheChainTipOverAnIncompleteStartupRescan) {
|
||||
CBlockLocator chainTip(std::vector<uint256>{uint256S("0def")});
|
||||
|
||||
SaveWallet wallet;
|
||||
wallet.NoteRescanStarted(0);
|
||||
{
|
||||
FakeWalletDB db;
|
||||
EXPECT_FALSE(wallet.SetBestChainWith(db, chainTip)) << "while the scan runs";
|
||||
EXPECT_TRUE(db.written.vHave.empty());
|
||||
}
|
||||
wallet.NoteRescanInterrupted(500);
|
||||
{
|
||||
FakeWalletDB db;
|
||||
EXPECT_FALSE(wallet.SetBestChainWith(db, chainTip)) << "after the scan was cut short";
|
||||
EXPECT_TRUE(db.written.vHave.empty());
|
||||
}
|
||||
|
||||
// Once init seeds the wallet tip (at the scan's checkpoint, when it was cut short), that is stamped.
|
||||
uint256 hash = uint256S("0abc");
|
||||
CBlockIndex tip;
|
||||
tip.phashBlock = &hash;
|
||||
wallet.SetWalletTip(&tip);
|
||||
{
|
||||
FakeWalletDB db;
|
||||
EXPECT_TRUE(wallet.SetBestChainWith(db, chainTip));
|
||||
ASSERT_FALSE(db.written.vHave.empty());
|
||||
EXPECT_EQ(hash, db.written.vHave.front());
|
||||
}
|
||||
|
||||
// A startup scan that completed leaves nothing to protect: init's own stamp goes through.
|
||||
SaveWallet completed;
|
||||
int before = completed.NoteRescanStarted(0);
|
||||
completed.NoteRescanCompleted(0, before);
|
||||
FakeWalletDB db;
|
||||
EXPECT_TRUE(completed.SetBestChainWith(db, chainTip));
|
||||
ASSERT_FALSE(db.written.vHave.empty());
|
||||
EXPECT_EQ(uint256S("0def"), db.written.vHave.front());
|
||||
}
|
||||
|
||||
@@ -1306,6 +1306,44 @@ static void AdjustCoinCacheForMemoryPressure()
|
||||
nCoinCacheUsage = (size_t)(newTargetMB << 20);
|
||||
}
|
||||
|
||||
#ifdef ENABLE_WALLET
|
||||
// Where a forced wallet rescan (-rescan, -zapwallettxes, a witness-cache reset) starts: genesis, or
|
||||
// -rescanheight capped at the tip.
|
||||
static CBlockIndex* ForcedRescanStart()
|
||||
{
|
||||
int rescanHeight = GetArg("-rescanheight", 0);
|
||||
if (rescanHeight > 0)
|
||||
return rescanHeight > chainActive.Tip()->GetHeight() ? chainActive.Tip() : chainActive[rescanHeight];
|
||||
return chainActive.Genesis();
|
||||
}
|
||||
|
||||
// A forced rescan starts below the locator on disk, which ScanForWalletTransactions only ever moves
|
||||
// forward. Left there, a scan cut short -- a shutdown, abortrescan, or a kill -- would leave it at the
|
||||
// old tip, and the next normal start would skip everything the scan had not reached. After
|
||||
// -zapwallettxes that range's transactions are simply gone from the wallet until someone rescans
|
||||
// again. So move the locator back to where the scan starts: an interrupted scan then checkpoints
|
||||
// forward from there, and a killed one restarts from there. Never moves the locator forward.
|
||||
static void RewindWalletLocatorForRescan(const std::string& strWalletFile, const CBlockIndex* pindexRescan)
|
||||
{
|
||||
if (pindexRescan == NULL)
|
||||
return;
|
||||
CWalletDB walletdb(strWalletFile);
|
||||
CBlockLocator locPersisted;
|
||||
if (!walletdb.ReadBestBlock(locPersisted))
|
||||
return;
|
||||
CBlockIndex* pindexPersisted = FindForkInGlobalIndex(chainActive, locPersisted);
|
||||
if (pindexPersisted == NULL || pindexPersisted->GetHeight() <= pindexRescan->GetHeight())
|
||||
return;
|
||||
if (walletdb.WriteBestBlock(chainActive.GetLocator(pindexRescan)))
|
||||
LogPrintf("%s: moved the wallet's best-block locator back from height %d to %d, where the rescan "
|
||||
"starts, so an interrupted rescan resumes instead of skipping the rest\n",
|
||||
__func__, pindexPersisted->GetHeight(), pindexRescan->GetHeight());
|
||||
else
|
||||
LogPrintf("%s: WARNING: could not move the wallet's best-block locator back to height %d; "
|
||||
"if the rescan is interrupted, restart with -rescan\n", __func__, pindexRescan->GetHeight());
|
||||
}
|
||||
#endif
|
||||
|
||||
bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
|
||||
{
|
||||
// ********************************************************* Step 1: setup
|
||||
@@ -2411,6 +2449,12 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
|
||||
|
||||
delete pwalletMain;
|
||||
pwalletMain = NULL;
|
||||
|
||||
// The zap is on disk now. Rewind the locator straight away rather than at the rescan below:
|
||||
// if startup fails in between (a config error, say), the next start must still rescan the
|
||||
// erased range rather than trust the old tip.
|
||||
if (chainActive.Tip() != NULL)
|
||||
RewindWalletLocatorForRescan(strWalletFile, ForcedRescanStart());
|
||||
}
|
||||
|
||||
uiInterface.InitMessage(_("Loading wallet..."));
|
||||
@@ -2876,6 +2920,10 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
|
||||
pindexRescan = chainActive[rescanHeight];
|
||||
}
|
||||
}
|
||||
|
||||
// See RewindWalletLocatorForRescan. (After -zapwallettxes this already happened, right after
|
||||
// the zap; doing it again is a no-op.)
|
||||
RewindWalletLocatorForRescan(strWalletFile, pindexRescan);
|
||||
} else {
|
||||
CWalletDB walletdb(strWalletFile);
|
||||
CBlockLocator locator;
|
||||
|
||||
@@ -1076,6 +1076,15 @@ protected:
|
||||
if (pindexWalletTip != nullptr) {
|
||||
locator = chainActive.GetLocator(pindexWalletTip);
|
||||
pindexStamped = pindexWalletTip;
|
||||
} else if (nInterruptedRescanHeight >= 0) {
|
||||
// No wallet tip yet, but a scan of this session is running or was cut short: this is
|
||||
// the startup rescan, and the caller is FlushStateToDisk stamping the chain tip (its
|
||||
// hourly write, or the one in Shutdown). Writing that would mark the unscanned range
|
||||
// as scanned, and the next start would skip it for good -- after -zapwallettxes, the
|
||||
// transactions in it would simply be gone. Keep the scan's own locator instead.
|
||||
LogPrintf("SetBestChain(): not stamping the chain tip over an incomplete rescan; the next "
|
||||
"start resumes it from height %d\n", nInterruptedRescanHeight);
|
||||
return false;
|
||||
} else if (!loc.vHave.empty()) {
|
||||
BlockMap::const_iterator mi = mapBlockIndex.find(loc.vHave.front());
|
||||
if (mi != mapBlockIndex.end())
|
||||
|
||||
Reference in New Issue
Block a user