Skip to content

Commit 05441c2

Browse files
committed
node/chainstate: Decouple from GetTime
...instead pass in a std::function<int64_t()> Note that the static_cast is needed (apparently) for the compiler to know which overloaded GetTime to choose.
1 parent 2414ebc commit 05441c2

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

src/init.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,8 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14871487
fReindexChainState,
14881488
chainparams,
14891489
check_blocks,
1490-
args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL));
1490+
args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL),
1491+
static_cast<int64_t(*)()>(GetTime));
14911492
} catch (const std::exception& e) {
14921493
LogPrintf("%s\n", e.what());
14931494
rv2 = ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE;

src/node/chainstate.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <node/chainstate.h>
66

77
#include <chainparams.h> // for CChainParams
8-
#include <util/time.h> // for GetTime
98
#include <node/blockstorage.h> // for CleanupBlockRevFiles, fHavePruned, fReindex
109
#include <shutdown.h> // for ShutdownRequested
1110
#include <validation.h> // for a lot of things
@@ -131,7 +130,8 @@ std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManage
131130
bool fReindexChainState,
132131
const CChainParams& chainparams,
133132
unsigned int check_blocks,
134-
unsigned int check_level)
133+
unsigned int check_level,
134+
std::function<int64_t()> get_unix_time_seconds)
135135
{
136136
auto is_coinsview_empty = [&](CChainState* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
137137
return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull();
@@ -143,7 +143,7 @@ std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManage
143143
for (CChainState* chainstate : chainman.GetAll()) {
144144
if (!is_coinsview_empty(chainstate)) {
145145
const CBlockIndex* tip = chainstate->m_chain.Tip();
146-
if (tip && tip->nTime > GetTime() + MAX_FUTURE_BLOCK_TIME) {
146+
if (tip && tip->nTime > get_unix_time_seconds() + 2 * 60 * 60) {
147147
return ChainstateLoadVerifyError::ERROR_BLOCK_FROM_FUTURE;
148148
}
149149

src/node/chainstate.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManage
7474
bool fReindexChainState,
7575
const CChainParams& chainparams,
7676
unsigned int check_blocks,
77-
unsigned int check_level);
77+
unsigned int check_level,
78+
std::function<int64_t()> get_unix_time_seconds);
7879

7980
#endif // BITCOIN_NODE_CHAINSTATE_H

0 commit comments

Comments
 (0)