From 45dd943d5d7a7c86ec7377152b18b0b31856ac15 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Thu, 3 Oct 2024 13:36:52 +0200 Subject: [PATCH] Add -pausebackgroundsync startup option When a UTXO snapshot is loaded, this option lets the user pause the verification of historical blocks in the background. --- doc/release-notes-31023.md | 4 ++++ src/init.cpp | 1 + src/kernel/chainstatemanager_opts.h | 2 ++ src/node/chainstatemanager_args.cpp | 2 ++ src/node/chainstatemanager_args.h | 2 ++ src/validation.cpp | 6 ++++++ src/validation.h | 4 +--- 7 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 doc/release-notes-31023.md diff --git a/doc/release-notes-31023.md b/doc/release-notes-31023.md new file mode 100644 index 0000000000000..51e4d582e6c5e --- /dev/null +++ b/doc/release-notes-31023.md @@ -0,0 +1,4 @@ +Updated settings +------ + +- When loading an assumeutxo snapshot, the background sync can be paused with `-pausebackgroundsync`. (#31023) diff --git a/src/init.cpp b/src/init.cpp index 504cab9ad77c7..300367e34ac1d 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -489,6 +489,7 @@ void SetupServerArgs(ArgsManager& argsman, bool can_listen_ipc) argsman.AddArg("-minimumchainwork=", strprintf("Minimum work assumed to exist on a valid chain in hex (default: %s, testnet3: %s, testnet4: %s, signet: %s)", defaultChainParams->GetConsensus().nMinimumChainWork.GetHex(), testnetChainParams->GetConsensus().nMinimumChainWork.GetHex(), testnet4ChainParams->GetConsensus().nMinimumChainWork.GetHex(), signetChainParams->GetConsensus().nMinimumChainWork.GetHex()), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::OPTIONS); argsman.AddArg("-par=", strprintf("Set the number of script verification threads (0 = auto, up to %d, <0 = leave that many cores free, default: %d)", MAX_SCRIPTCHECK_THREADS, DEFAULT_SCRIPTCHECK_THREADS), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); + argsman.AddArg("-pausebackgroundsync", strprintf("When a UTXO snapshot is loaded, pause the verification of historical blocks in the background (default: %u)", DEFAULT_PAUSE_BACKGROUND_SYNC), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-persistmempool", strprintf("Whether to save the mempool on shutdown and load on restart (default: %u)", DEFAULT_PERSIST_MEMPOOL), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-persistmempoolv1", strprintf("Whether a mempool.dat file created by -persistmempool or the savemempool RPC will be written in the legacy format " diff --git a/src/kernel/chainstatemanager_opts.h b/src/kernel/chainstatemanager_opts.h index 957c3ec31876a..b7948a91e6f05 100644 --- a/src/kernel/chainstatemanager_opts.h +++ b/src/kernel/chainstatemanager_opts.h @@ -52,6 +52,8 @@ struct ChainstateManagerOpts { int worker_threads_num{0}; size_t script_execution_cache_bytes{DEFAULT_SCRIPT_EXECUTION_CACHE_BYTES}; size_t signature_cache_bytes{DEFAULT_SIGNATURE_CACHE_BYTES}; + //! Whether to defer syncing the background chainstate after an assumeutxo snapshot is loaded + bool pause_background_sync{false}; }; } // namespace kernel diff --git a/src/node/chainstatemanager_args.cpp b/src/node/chainstatemanager_args.cpp index b86d0b2991325..683c9cef7a23c 100644 --- a/src/node/chainstatemanager_args.cpp +++ b/src/node/chainstatemanager_args.cpp @@ -73,6 +73,8 @@ util::Result ApplyArgsManOptions(const ArgsManager& args, ChainstateManage opts.signature_cache_bytes = clamped_size_each; } + opts.pause_background_sync = args.GetBoolArg("-pausebackgroundsync", DEFAULT_PAUSE_BACKGROUND_SYNC); + return {}; } } // namespace node diff --git a/src/node/chainstatemanager_args.h b/src/node/chainstatemanager_args.h index b2cdba68b8fe9..6a7bb27478d66 100644 --- a/src/node/chainstatemanager_args.h +++ b/src/node/chainstatemanager_args.h @@ -14,6 +14,8 @@ class ArgsManager; static constexpr int MAX_SCRIPTCHECK_THREADS{15}; /** -par default (number of script-checking threads, 0 = auto) */ static constexpr int DEFAULT_SCRIPTCHECK_THREADS{0}; +/** -pausebackgroundsync default */ +static const bool DEFAULT_PAUSE_BACKGROUND_SYNC{false}; namespace node { [[nodiscard]] util::Result ApplyArgsManOptions(const ArgsManager& args, ChainstateManager::Options& opts); diff --git a/src/validation.cpp b/src/validation.cpp index 5da3387ad296f..b6d848c23f358 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -6387,6 +6387,12 @@ std::optional ChainstateManager::GetSnapshotBaseHeight() const return base ? std::make_optional(base->nHeight) : std::nullopt; } +bool ChainstateManager::BackgroundSyncInProgress() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()) { + if (!IsUsable(m_snapshot_chainstate.get())) return false; + if (!IsUsable(m_ibd_chainstate.get())) return false; + return !m_options.pause_background_sync; +} + bool ChainstateManager::ValidatedSnapshotCleanup() { AssertLockHeld(::cs_main); diff --git a/src/validation.h b/src/validation.h index f6aeea3faaf75..f462354e5f81a 100644 --- a/src/validation.h +++ b/src/validation.h @@ -1113,9 +1113,7 @@ class ChainstateManager CBlockIndex* ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()) { return ActiveChain().Tip(); } //! The state of a background sync (for net processing) - bool BackgroundSyncInProgress() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()) { - return IsUsable(m_snapshot_chainstate.get()) && IsUsable(m_ibd_chainstate.get()); - } + bool BackgroundSyncInProgress() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()); //! The tip of the background sync chain const CBlockIndex* GetBackgroundSyncTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()) {