diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -653,6 +653,7 @@ node/blockstorage.cpp node/caches.cpp node/chainstate.cpp + node/chainstatemanager_args.cpp node/coin.cpp node/coinstats.cpp node/context.cpp diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -46,6 +46,7 @@ #include #include #include +#include #include #include #include @@ -1095,7 +1096,7 @@ argsman.AddArg("-maxtipage=", strprintf("Maximum tip age in seconds to consider node in " "initial block download (default: %u)", - DEFAULT_MAX_TIP_AGE), + Ticks(DEFAULT_MAX_TIP_AGE)), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST); @@ -1965,8 +1966,6 @@ nLocalServices = ServiceFlags(nLocalServices | NODE_BLOOM); } - nMaxTipAge = args.GetIntArg("-maxtipage", DEFAULT_MAX_TIP_AGE); - if (args.IsArgSet("-proxy") && args.GetArg("-proxy", "").empty()) { return InitError(_( "No proxy server specified. Use -proxy= or -proxy=.")); @@ -2396,6 +2395,12 @@ fReindex = args.GetBoolArg("-reindex", false); bool fReindexChainState = args.GetBoolArg("-reindex-chainstate", false); + ChainstateManager::Options chainman_opts{ + .config = config, + .adjusted_time_callback = GetAdjustedTime, + }; + ApplyArgsManOptions(args, chainman_opts); + // cache size calculations CacheSizes cache_sizes = CalculateCacheSizes(args, g_enabled_filter_types.size()); @@ -2446,10 +2451,6 @@ for (bool fLoaded = false; !fLoaded && !ShutdownRequested();) { node.mempool = std::make_unique(mempool_opts); - const ChainstateManager::Options chainman_opts{ - .config = config, - .adjusted_time_callback = GetAdjustedTime, - }; node.chainman = std::make_unique(chainman_opts); ChainstateManager &chainman = *node.chainman; diff --git a/src/kernel/chainstatemanager_opts.h b/src/kernel/chainstatemanager_opts.h --- a/src/kernel/chainstatemanager_opts.h +++ b/src/kernel/chainstatemanager_opts.h @@ -12,6 +12,8 @@ class Config; +static constexpr auto DEFAULT_MAX_TIP_AGE{24h}; + namespace kernel { /** @@ -23,6 +25,9 @@ const Config &config; const std::function adjusted_time_callback{ nullptr}; + //! If the tip is older than this, the node is considered to be in initial + //! block download. + std::chrono::seconds max_tip_age{DEFAULT_MAX_TIP_AGE}; }; } // namespace kernel diff --git a/src/node/chainstatemanager_args.h b/src/node/chainstatemanager_args.h new file mode 100644 --- /dev/null +++ b/src/node/chainstatemanager_args.h @@ -0,0 +1,17 @@ +// Copyright (c) 2022 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_NODE_CHAINSTATEMANAGER_ARGS_H +#define BITCOIN_NODE_CHAINSTATEMANAGER_ARGS_H + +#include + +class ArgsManager; + +namespace node { +void ApplyArgsManOptions(const ArgsManager &args, + ChainstateManager::Options &opts); +} // namespace node + +#endif // BITCOIN_NODE_CHAINSTATEMANAGER_ARGS_H diff --git a/src/node/chainstatemanager_args.cpp b/src/node/chainstatemanager_args.cpp new file mode 100644 --- /dev/null +++ b/src/node/chainstatemanager_args.cpp @@ -0,0 +1,19 @@ +// Copyright (c) 2022 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include + +#include + +#include +#include + +namespace node { +void ApplyArgsManOptions(const ArgsManager &args, + ChainstateManager::Options &opts) { + if (auto value{args.GetIntArg("-maxtipage")}) { + opts.max_tip_age = std::chrono::seconds{*value}; + } +} +} // namespace node diff --git a/src/validation.h b/src/validation.h --- a/src/validation.h +++ b/src/validation.h @@ -82,7 +82,6 @@ static const int MAX_SCRIPTCHECK_THREADS = 15; /** -par default (number of script-checking threads, 0 = auto) */ static const int DEFAULT_SCRIPTCHECK_THREADS = 0; -static const int64_t DEFAULT_MAX_TIP_AGE = 24 * 60 * 60; static const bool DEFAULT_CHECKPOINTS_ENABLED = true; static const bool DEFAULT_PEERBLOOMFILTERS = true; @@ -121,12 +120,6 @@ extern bool fCheckBlockIndex; extern bool fCheckpointsEnabled; -/** - * If the tip is older than this (in seconds), the node is considered to be in - * initial block download. - */ -extern int64_t nMaxTipAge; - /** * Block hash whose ancestors we will assume to have valid scripts without * checking them. diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -111,7 +111,6 @@ uint256 g_best_block; bool fCheckBlockIndex = false; bool fCheckpointsEnabled = DEFAULT_CHECKPOINTS_ENABLED; -int64_t nMaxTipAge = DEFAULT_MAX_TIP_AGE; BlockHash hashAssumeValid; arith_uint256 nMinimumChainWork; @@ -1176,7 +1175,8 @@ if (m_chain.Tip()->nChainWork < nMinimumChainWork) { return true; } - if (m_chain.Tip()->GetBlockTime() < (GetTime() - nMaxTipAge)) { + if (m_chain.Tip()->Time() < + Now() - m_chainman.m_options.max_tip_age) { return true; } LogPrintf("Leaving InitialBlockDownload (latching to false)\n");