From 9bd2fd2b92f5d7a3d706c8c5ccbff22f84606eb7 Mon Sep 17 00:00:00 2001 From: DanS Date: Wed, 28 Oct 2026 12:39:25 -0500 Subject: [PATCH] wallet: add -walletsaveatshutdown to turn the shutdown save off The shutdown save writes every note-bearing tx with its witness cache, which takes tens of seconds on a large wallet: about 15 s unloaded and 30-40 s under load for a 631 MB regtest wallet, and more for a mainnet wallet of 1-2 GB. A deployment that kills the daemon sooner (an old GUI with a 20 s kill, a short service stop timeout) gains nothing from it: the kill rolls the save back, and the next start spends extra time on BDB recovery. -walletsaveatshutdown=0 skips the save and logs why. The default stays on. The shipped init scripts run with -disablewallet, so their stop timeouts are left as they are. Co-Authored-By: Claude Opus 5.5 --- src/gtest/test_wallet_shutdownsave.cpp | 12 ++++++++++++ src/init.cpp | 2 ++ src/wallet/wallet.cpp | 4 ++++ 3 files changed, 18 insertions(+) diff --git a/src/gtest/test_wallet_shutdownsave.cpp b/src/gtest/test_wallet_shutdownsave.cpp index 78dda74ba..7a7f34356 100644 --- a/src/gtest/test_wallet_shutdownsave.cpp +++ b/src/gtest/test_wallet_shutdownsave.cpp @@ -58,6 +58,18 @@ TEST_F(WalletShutdownSave, SkipsAfterAWalletFailure) { EXPECT_EQ("", Reason(wallet, true)); } +TEST_F(WalletShutdownSave, CanBeTurnedOff) { + CWallet wallet; + CBlockIndex tip; + wallet.SetWalletTip(&tip); + mapArgs["-walletsaveatshutdown"] = "0"; + EXPECT_EQ("disabled by -walletsaveatshutdown=0", Reason(wallet, true)); + mapArgs["-walletsaveatshutdown"] = "1"; + EXPECT_EQ("", Reason(wallet, true)); + mapArgs.erase("-walletsaveatshutdown"); + EXPECT_EQ("", Reason(wallet, true)) << "on by default"; +} + // A failed chainstate flush (typically a full disk) leaves the block the locator would name possibly missing from the // chainstate on disk. TEST_F(WalletShutdownSave, SkipsWhenTheChainstateFlushFailed) { diff --git a/src/init.cpp b/src/init.cpp index 2f2a682dc..5248a6231 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -529,6 +529,8 @@ std::string HelpMessage(HelpMessageMode mode) if (showDebug) strUsage += HelpMessageOpt("-checknotecache", "On every balance or note-listing call, decrypt each cached note it considers again and log and repair any difference from the cache (slow; default: 0)"); strUsage += HelpMessageOpt("-rescan", _("Rescan the block chain for missing wallet transactions") + " " + _("on startup")); + strUsage += HelpMessageOpt("-walletsaveatshutdown", "Save the note witnesses and best block at a clean shutdown, so the next start does not rescan the session's blocks. " + "On a large wallet this can add a minute or more to the stop (default: 1)"); strUsage += HelpMessageOpt("-rescanheight", _("Rescan from specified height when rescan=1 on startup")); strUsage += HelpMessageOpt("-salvagewallet", _("Attempt to recover private keys from a corrupt wallet.dat") + " " + _("on startup")); strUsage += HelpMessageOpt("-sendfreetransactions", strprintf(_("Send transactions as zero-fee transactions if possible (default: %u)"), 0)); diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 430a5c729..51f8d3691 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -874,6 +874,10 @@ const char* CWallet::ShutdownSaveSkipReason(bool fInitCompleted, bool fChainstat // interrupted rescan's checkpoint and the next start would skip the unscanned range. if (!fInitCompleted) return "startup did not complete"; + // For deployments whose stop timeout is shorter than the save takes on their wallet: a kill + // during the save rolls it back and only lengthens the next start. + if (!GetBoolArg("-walletsaveatshutdown", true)) + return "disabled by -walletsaveatshutdown=0"; if (fWalletNotifyFailed) return "the wallet failed to process a block"; // The locator would then name a block the chainstate and block index on disk may not have, and