From c333cb502df64257687048d21334dbd719378da2 Mon Sep 17 00:00:00 2001 From: Carl Dong Date: Wed, 22 Sep 2021 15:36:10 -0400 Subject: [PATCH] node/chainstate: Decouple from GetTime Summary: ``` ...instead pass in a std::function Note that the static_cast is needed (apparently) for the compiler to know which overloaded GetTime to choose. ``` Partial backport of [[https://github.com/bitcoin/bitcoin/pull/23280 | core#23280]]: https://github.com/bitcoin/bitcoin/pull/23280/commits/05441c2dc5f60e2025476d8ec94c9025032d118c Depends on D12576. Test Plan: ninja all check-all Reviewers: #bitcoin_abc, sdulfari Reviewed By: #bitcoin_abc, sdulfari Differential Revision: https://reviews.bitcoinabc.org/D12577 --- src/init.cpp | 3 ++- src/node/chainstate.cpp | 7 ++++--- src/node/chainstate.h | 3 ++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 223b12512b..d114b94018 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -2535,7 +2535,8 @@ bool AppInitMain(Config &config, RPCServer &rpcServer, rv2 = VerifyLoadedChainstate( chainman, fReset, fReindexChainState, config, check_blocks, - args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL)); + args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL), + static_cast(GetTime)); } catch (const std::exception &e) { LogPrintf("%s\n", e.what()); rv2 = ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE; diff --git a/src/node/chainstate.cpp b/src/node/chainstate.cpp index 11e48cc747..f224649e01 100644 --- a/src/node/chainstate.cpp +++ b/src/node/chainstate.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include std::optional @@ -140,7 +139,8 @@ LoadChainstate(bool fReset, ChainstateManager &chainman, CTxMemPool *mempool, std::optional VerifyLoadedChainstate(ChainstateManager &chainman, bool fReset, bool fReindexChainState, const Config &config, - unsigned int check_blocks, unsigned int check_level) { + unsigned int check_blocks, unsigned int check_level, + std::function get_unix_time_seconds) { auto is_coinsview_empty = [&](CChainState *chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) { return fReset || fReindexChainState || @@ -153,7 +153,8 @@ VerifyLoadedChainstate(ChainstateManager &chainman, bool fReset, for (CChainState *chainstate : chainman.GetAll()) { if (!is_coinsview_empty(chainstate)) { const CBlockIndex *tip = chainstate->m_chain.Tip(); - if (tip && tip->nTime > GetTime() + MAX_FUTURE_BLOCK_TIME) { + if (tip && tip->nTime > get_unix_time_seconds() + + MAX_FUTURE_BLOCK_TIME) { return ChainstateLoadVerifyError::ERROR_BLOCK_FROM_FUTURE; } diff --git a/src/node/chainstate.h b/src/node/chainstate.h index 7a27eee6df..12404900c5 100644 --- a/src/node/chainstate.h +++ b/src/node/chainstate.h @@ -70,6 +70,7 @@ enum class ChainstateLoadVerifyError { std::optional VerifyLoadedChainstate(ChainstateManager &chainman, bool fReset, bool fReindexChainState, const Config &config, - unsigned int check_blocks, unsigned int check_level); + unsigned int check_blocks, unsigned int check_level, + std::function get_unix_time_seconds); #endif // BITCOIN_NODE_CHAINSTATE_H