Skip to content

Commit

Permalink
node/chainstate: Decouple from GetTime
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dongcarl authored and Fabcien committed Nov 22, 2022
1 parent ef89e93 commit c333cb5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64_t (*)()>(GetTime));
} catch (const std::exception &e) {
LogPrintf("%s\n", e.what());
rv2 = ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE;
Expand Down
7 changes: 4 additions & 3 deletions src/node/chainstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <config.h>
#include <node/blockstorage.h>
#include <shutdown.h>
#include <util/time.h>
#include <validation.h>

std::optional<ChainstateLoadingError>
Expand Down Expand Up @@ -140,7 +139,8 @@ LoadChainstate(bool fReset, ChainstateManager &chainman, CTxMemPool *mempool,
std::optional<ChainstateLoadVerifyError>
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<int64_t()> get_unix_time_seconds) {
auto is_coinsview_empty =
[&](CChainState *chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
return fReset || fReindexChainState ||
Expand All @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion src/node/chainstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ enum class ChainstateLoadVerifyError {
std::optional<ChainstateLoadVerifyError>
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<int64_t()> get_unix_time_seconds);

#endif // BITCOIN_NODE_CHAINSTATE_H

0 comments on commit c333cb5

Please sign in to comment.