Skip to content

Commit c333cb5

Browse files
dongcarlFabcien
authored andcommitted
node/chainstate: Decouple from GetTime
Summary: ``` ...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. ``` Partial backport of [[bitcoin/bitcoin#23280 | core#23280]]: bitcoin/bitcoin@05441c2 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
1 parent ef89e93 commit c333cb5

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

src/init.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -2535,7 +2535,8 @@ bool AppInitMain(Config &config, RPCServer &rpcServer,
25352535

25362536
rv2 = VerifyLoadedChainstate(
25372537
chainman, fReset, fReindexChainState, config, check_blocks,
2538-
args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL));
2538+
args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL),
2539+
static_cast<int64_t (*)()>(GetTime));
25392540
} catch (const std::exception &e) {
25402541
LogPrintf("%s\n", e.what());
25412542
rv2 = ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE;

src/node/chainstate.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <config.h>
99
#include <node/blockstorage.h>
1010
#include <shutdown.h>
11-
#include <util/time.h>
1211
#include <validation.h>
1312

1413
std::optional<ChainstateLoadingError>
@@ -140,7 +139,8 @@ LoadChainstate(bool fReset, ChainstateManager &chainman, CTxMemPool *mempool,
140139
std::optional<ChainstateLoadVerifyError>
141140
VerifyLoadedChainstate(ChainstateManager &chainman, bool fReset,
142141
bool fReindexChainState, const Config &config,
143-
unsigned int check_blocks, unsigned int check_level) {
142+
unsigned int check_blocks, unsigned int check_level,
143+
std::function<int64_t()> get_unix_time_seconds) {
144144
auto is_coinsview_empty =
145145
[&](CChainState *chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
146146
return fReset || fReindexChainState ||
@@ -153,7 +153,8 @@ VerifyLoadedChainstate(ChainstateManager &chainman, bool fReset,
153153
for (CChainState *chainstate : chainman.GetAll()) {
154154
if (!is_coinsview_empty(chainstate)) {
155155
const CBlockIndex *tip = chainstate->m_chain.Tip();
156-
if (tip && tip->nTime > GetTime() + MAX_FUTURE_BLOCK_TIME) {
156+
if (tip && tip->nTime > get_unix_time_seconds() +
157+
MAX_FUTURE_BLOCK_TIME) {
157158
return ChainstateLoadVerifyError::ERROR_BLOCK_FROM_FUTURE;
158159
}
159160

src/node/chainstate.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ enum class ChainstateLoadVerifyError {
7070
std::optional<ChainstateLoadVerifyError>
7171
VerifyLoadedChainstate(ChainstateManager &chainman, bool fReset,
7272
bool fReindexChainState, const Config &config,
73-
unsigned int check_blocks, unsigned int check_level);
73+
unsigned int check_blocks, unsigned int check_level,
74+
std::function<int64_t()> get_unix_time_seconds);
7475

7576
#endif // BITCOIN_NODE_CHAINSTATE_H

0 commit comments

Comments
 (0)