Skip to content

Commit faf4487

Browse files
author
MacroFake
committed
Move ::nMaxTipAge into ChainstateManager
1 parent ba441d4 commit faf4487

File tree

8 files changed

+53
-14
lines changed

8 files changed

+53
-14
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ BITCOIN_CORE_H = \
198198
node/blockstorage.h \
199199
node/caches.h \
200200
node/chainstate.h \
201+
node/chainstatemanager_args.h \
201202
node/coin.h \
202203
node/connection_types.h \
203204
node/context.h \
@@ -381,6 +382,7 @@ libbitcoin_node_a_SOURCES = \
381382
node/blockstorage.cpp \
382383
node/caches.cpp \
383384
node/chainstate.cpp \
385+
node/chainstatemanager_args.cpp \
384386
node/coin.cpp \
385387
node/connection_types.cpp \
386388
node/context.cpp \

src/init.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <node/blockstorage.h>
4141
#include <node/caches.h>
4242
#include <node/chainstate.h>
43+
#include <node/chainstatemanager_args.h>
4344
#include <node/context.h>
4445
#include <node/interface_ui.h>
4546
#include <node/mempool_args.h>
@@ -554,7 +555,10 @@ void SetupServerArgs(ArgsManager& argsman)
554555
argsman.AddArg("-capturemessages", "Capture all P2P messages to disk", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
555556
argsman.AddArg("-mocktime=<n>", "Replace actual time with " + UNIX_EPOCH_TIME + " (default: 0)", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
556557
argsman.AddArg("-maxsigcachesize=<n>", strprintf("Limit sum of signature cache and script execution cache sizes to <n> MiB (default: %u)", DEFAULT_MAX_SIG_CACHE_BYTES >> 20), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
557-
argsman.AddArg("-maxtipage=<n>", strprintf("Maximum tip age in seconds to consider node in initial block download (default: %u)", DEFAULT_MAX_TIP_AGE), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
558+
argsman.AddArg("-maxtipage=<n>",
559+
strprintf("Maximum tip age in seconds to consider node in initial block download (default: %u)",
560+
Ticks<std::chrono::seconds>(DEFAULT_MAX_TIP_AGE)),
561+
ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
558562
argsman.AddArg("-printpriority", strprintf("Log transaction fee rate in " + CURRENCY_UNIT + "/kvB when mining blocks (default: %u)", DEFAULT_PRINTPRIORITY), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
559563
argsman.AddArg("-uacomment=<cmt>", "Append comment to the user agent string", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
560564

@@ -995,8 +999,6 @@ bool AppInitParameterInteraction(const ArgsManager& args, bool use_syscall_sandb
995999
if (args.GetIntArg("-rpcserialversion", DEFAULT_RPC_SERIALIZE_VERSION) > 1)
9961000
return InitError(Untranslated("Unknown rpcserialversion requested."));
9971001

998-
nMaxTipAge = args.GetIntArg("-maxtipage", DEFAULT_MAX_TIP_AGE);
999-
10001002
if (args.GetBoolArg("-reindex-chainstate", false)) {
10011003
// indexes that must be deactivated to prevent index corruption, see #24630
10021004
if (args.GetBoolArg("-coinstatsindex", DEFAULT_COINSTATSINDEX)) {
@@ -1435,6 +1437,11 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14351437

14361438
fReindex = args.GetBoolArg("-reindex", false);
14371439
bool fReindexChainState = args.GetBoolArg("-reindex-chainstate", false);
1440+
ChainstateManager::Options chainman_opts{
1441+
.chainparams = chainparams,
1442+
.adjusted_time_callback = GetAdjustedTime,
1443+
};
1444+
ApplyArgsManOptions(args, chainman_opts);
14381445

14391446
// cache size calculations
14401447
CacheSizes cache_sizes = CalculateCacheSizes(args, g_enabled_filter_types.size());
@@ -1471,10 +1478,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14711478
for (bool fLoaded = false; !fLoaded && !ShutdownRequested();) {
14721479
node.mempool = std::make_unique<CTxMemPool>(mempool_opts);
14731480

1474-
const ChainstateManager::Options chainman_opts{
1475-
.chainparams = chainparams,
1476-
.adjusted_time_callback = GetAdjustedTime,
1477-
};
14781481
node.chainman = std::make_unique<ChainstateManager>(chainman_opts);
14791482
ChainstateManager& chainman = *node.chainman;
14801483

src/kernel/chainstatemanager_opts.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
class CChainParams;
1414

15+
static constexpr auto DEFAULT_MAX_TIP_AGE{24h};
16+
1517
namespace kernel {
1618

1719
/**
@@ -22,6 +24,8 @@ namespace kernel {
2224
struct ChainstateManagerOpts {
2325
const CChainParams& chainparams;
2426
const std::function<NodeClock::time_point()> adjusted_time_callback{nullptr};
27+
//! If the tip is older than this, the node is considered to be in initial block download.
28+
std::chrono::seconds max_tip_age{DEFAULT_MAX_TIP_AGE};
2529
};
2630

2731
} // namespace kernel

src/node/chainstatemanager_args.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <node/chainstatemanager_args.h>
6+
7+
#include <util/system.h>
8+
9+
#include <chrono>
10+
#include <optional>
11+
12+
namespace node {
13+
void ApplyArgsManOptions(const ArgsManager& args, ChainstateManager::Options& opts)
14+
{
15+
if (auto value{args.GetIntArg("-maxtipage")}) opts.max_tip_age = std::chrono::seconds{*value};
16+
}
17+
} // namespace node

src/node/chainstatemanager_args.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_NODE_CHAINSTATEMANAGER_ARGS_H
6+
#define BITCOIN_NODE_CHAINSTATEMANAGER_ARGS_H
7+
8+
#include <validation.h>
9+
10+
class ArgsManager;
11+
12+
namespace node {
13+
void ApplyArgsManOptions(const ArgsManager& args, ChainstateManager::Options& opts);
14+
} // namespace node
15+
16+
#endif // BITCOIN_NODE_CHAINSTATEMANAGER_ARGS_H

src/validation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ uint256 g_best_block;
123123
bool g_parallel_script_checks{false};
124124
bool fCheckBlockIndex = false;
125125
bool fCheckpointsEnabled = DEFAULT_CHECKPOINTS_ENABLED;
126-
int64_t nMaxTipAge = DEFAULT_MAX_TIP_AGE;
127126

128127
uint256 hashAssumeValid;
129128
arith_uint256 nMinimumChainWork;
@@ -1548,8 +1547,9 @@ bool Chainstate::IsInitialBlockDownload() const
15481547
return true;
15491548
if (m_chain.Tip()->nChainWork < nMinimumChainWork)
15501549
return true;
1551-
if (m_chain.Tip()->GetBlockTime() < (GetTime() - nMaxTipAge))
1550+
if (m_chain.Tip()->Time() < NodeClock::now() - m_chainman.m_options.max_tip_age) {
15521551
return true;
1552+
}
15531553
LogPrintf("Leaving InitialBlockDownload (latching to false)\n");
15541554
m_cached_finished_ibd.store(true, std::memory_order_relaxed);
15551555
return false;

src/validation.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ struct Params;
6363
static const int MAX_SCRIPTCHECK_THREADS = 15;
6464
/** -par default (number of script-checking threads, 0 = auto) */
6565
static const int DEFAULT_SCRIPTCHECK_THREADS = 0;
66-
static const int64_t DEFAULT_MAX_TIP_AGE = 24 * 60 * 60;
6766
static const bool DEFAULT_CHECKPOINTS_ENABLED = true;
6867
/** Default for -stopatheight */
6968
static const int DEFAULT_STOPATHEIGHT = 0;
@@ -99,8 +98,6 @@ extern uint256 g_best_block;
9998
extern bool g_parallel_script_checks;
10099
extern bool fCheckBlockIndex;
101100
extern bool fCheckpointsEnabled;
102-
/** If the tip is older than this (in seconds), the node is considered to be in initial block download. */
103-
extern int64_t nMaxTipAge;
104101

105102
/** Block hash whose ancestors we will assume to have valid scripts without checking them. */
106103
extern uint256 hashAssumeValid;

test/functional/feature_maxtipage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
# Copyright (c) 2022 The Bitcoin Core developers
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5-
"""Test logic for setting nMaxTipAge on command line.
5+
"""Test logic for setting -maxtipage on command line.
66
77
Nodes don't consider themselves out of "initial block download" as long as
8-
their best known block header time is more than nMaxTipAge in the past.
8+
their best known block header time is more than -maxtipage in the past.
99
"""
1010

1111
import time

0 commit comments

Comments
 (0)