diff --git a/src/gtest/test_wallet_shutdownsave.cpp b/src/gtest/test_wallet_shutdownsave.cpp index a823539d4..9eb4f7d8a 100644 --- a/src/gtest/test_wallet_shutdownsave.cpp +++ b/src/gtest/test_wallet_shutdownsave.cpp @@ -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{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()); +} diff --git a/src/init.cpp b/src/init.cpp index 564519db5..0da56523a 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -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; diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 22fd7188a..2330b282b 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -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())