Skip to content

Commit eeee5ad

Browse files
author
MacroFake
committed
Make adjusted time type safe
1 parent fa3be79 commit eeee5ad

File tree

8 files changed

+19
-16
lines changed

8 files changed

+19
-16
lines changed

src/bitcoin-chainstate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ int main(int argc, char* argv[])
8282
// SETUP: Chainstate
8383
const ChainstateManager::Options chainman_opts{
8484
.chainparams = chainparams,
85-
.adjusted_time_callback = static_cast<int64_t (*)()>(GetTime),
85+
.adjusted_time_callback = NodeClock::now,
8686
};
8787
ChainstateManager chainman{chainman_opts};
8888

src/kernel/chainstatemanager_opts.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef BITCOIN_KERNEL_CHAINSTATEMANAGER_OPTS_H
66
#define BITCOIN_KERNEL_CHAINSTATEMANAGER_OPTS_H
77

8+
#include <util/time.h>
9+
810
#include <cstdint>
911
#include <functional>
1012

@@ -19,7 +21,7 @@ namespace kernel {
1921
*/
2022
struct ChainstateManagerOpts {
2123
const CChainParams& chainparams;
22-
const std::function<int64_t()> adjusted_time_callback{nullptr};
24+
const std::function<NodeClock::time_point()> adjusted_time_callback{nullptr};
2325
};
2426

2527
} // namespace kernel

src/net_processing.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ bool PeerManagerImpl::TipMayBeStale()
11451145

11461146
bool PeerManagerImpl::CanDirectFetch()
11471147
{
1148-
return m_chainman.ActiveChain().Tip()->GetBlockTime() > GetAdjustedTime() - m_chainparams.GetConsensus().nPowTargetSpacing * 20;
1148+
return m_chainman.ActiveChain().Tip()->Time() > GetAdjustedTime() - m_chainparams.GetConsensus().PowTargetSpacing() * 20;
11491149
}
11501150

11511151
static bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
@@ -4886,7 +4886,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
48864886

48874887
if (!state.fSyncStarted && CanServeBlocks(*peer) && !fImporting && !fReindex) {
48884888
// Only actively request headers from a single peer, unless we're close to today.
4889-
if ((nSyncStarted == 0 && sync_blocks_and_headers_from_peer) || m_chainman.m_best_header->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {
4889+
if ((nSyncStarted == 0 && sync_blocks_and_headers_from_peer) || m_chainman.m_best_header->Time() > GetAdjustedTime() - 24h) {
48904890
const CBlockIndex* pindexStart = m_chainman.m_best_header;
48914891
/* If possible, start at the block preceding the currently
48924892
best known header. This ensures that we always get a
@@ -4906,7 +4906,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
49064906
// Convert HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER to microseconds before scaling
49074907
// to maintain precision
49084908
std::chrono::microseconds{HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER} *
4909-
(GetAdjustedTime() - m_chainman.m_best_header->GetBlockTime()) / consensusParams.nPowTargetSpacing
4909+
Ticks<std::chrono::seconds>(GetAdjustedTime() - m_chainman.m_best_header->Time()) / consensusParams.nPowTargetSpacing
49104910
);
49114911
nSyncStarted++;
49124912
}
@@ -5223,7 +5223,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
52235223
// Check for headers sync timeouts
52245224
if (state.fSyncStarted && state.m_headers_sync_timeout < std::chrono::microseconds::max()) {
52255225
// Detect whether this is a stalling initial-headers-sync peer
5226-
if (m_chainman.m_best_header->GetBlockTime() <= GetAdjustedTime() - 24 * 60 * 60) {
5226+
if (m_chainman.m_best_header->Time() <= GetAdjustedTime() - 24h) {
52275227
if (current_time > state.m_headers_sync_timeout && nSyncStarted == 1 && (m_num_preferred_download_peers - state.fPreferredDownload >= 1)) {
52285228
// Disconnect a peer (without NetPermissionFlags::NoBan permission) if it is our only sync peer,
52295229
// and we have others we could be using instead.

src/node/miner.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace node {
3030
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
3131
{
3232
int64_t nOldTime = pblock->nTime;
33-
int64_t nNewTime = std::max(pindexPrev->GetMedianTimePast() + 1, GetAdjustedTime());
33+
int64_t nNewTime{std::max<int64_t>(pindexPrev->GetMedianTimePast() + 1, TicksSinceEpoch<std::chrono::seconds>(GetAdjustedTime()))};
3434

3535
if (nOldTime < nNewTime) {
3636
pblock->nTime = nNewTime;
@@ -133,7 +133,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
133133
pblock->nVersion = gArgs.GetIntArg("-blockversion", pblock->nVersion);
134134
}
135135

136-
pblock->nTime = GetAdjustedTime();
136+
pblock->nTime = TicksSinceEpoch<std::chrono::seconds>(GetAdjustedTime());
137137
m_lock_time_cutoff = pindexPrev->GetMedianTimePast();
138138

139139
int nPackagesSelected = 0;

src/timedata.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ int64_t GetTimeOffset()
3232
return nTimeOffset;
3333
}
3434

35-
int64_t GetAdjustedTime()
35+
NodeClock::time_point GetAdjustedTime()
3636
{
37-
return GetTime() + GetTimeOffset();
37+
return NodeClock::now() + std::chrono::seconds{GetTimeOffset()};
3838
}
3939

4040
#define BITCOIN_TIMEDATA_MAX_SAMPLES 200

src/timedata.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class CMedianFilter
7575

7676
/** Functions to keep track of adjusted P2P time */
7777
int64_t GetTimeOffset();
78-
int64_t GetAdjustedTime();
78+
NodeClock::time_point GetAdjustedTime();
7979
void AddTimeData(const CNetAddr& ip, int64_t nTime);
8080

8181
/**

src/validation.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3442,7 +3442,7 @@ std::vector<unsigned char> ChainstateManager::GenerateCoinbaseCommitment(CBlock&
34423442
* in ConnectBlock().
34433443
* Note that -reindex-chainstate skips the validation that happens here!
34443444
*/
3445-
static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidationState& state, BlockManager& blockman, const ChainstateManager& chainman, const CBlockIndex* pindexPrev, int64_t nAdjustedTime) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
3445+
static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidationState& state, BlockManager& blockman, const ChainstateManager& chainman, const CBlockIndex* pindexPrev, NodeClock::time_point now) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
34463446
{
34473447
AssertLockHeld(::cs_main);
34483448
assert(pindexPrev != nullptr);
@@ -3470,8 +3470,9 @@ static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidatio
34703470
return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, "time-too-old", "block's timestamp is too early");
34713471

34723472
// Check timestamp
3473-
if (block.GetBlockTime() > nAdjustedTime + MAX_FUTURE_BLOCK_TIME)
3473+
if (block.Time() > now + std::chrono::seconds{MAX_FUTURE_BLOCK_TIME}) {
34743474
return state.Invalid(BlockValidationResult::BLOCK_TIME_FUTURE, "time-too-new", "block timestamp too far in the future");
3475+
}
34753476

34763477
// Reject blocks with outdated version
34773478
if ((block.nVersion < 2 && DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_HEIGHTINCB)) ||
@@ -3832,7 +3833,7 @@ bool TestBlockValidity(BlockValidationState& state,
38323833
CChainState& chainstate,
38333834
const CBlock& block,
38343835
CBlockIndex* pindexPrev,
3835-
const std::function<int64_t()>& adjusted_time_callback,
3836+
const std::function<NodeClock::time_point()>& adjusted_time_callback,
38363837
bool fCheckPOW,
38373838
bool fCheckMerkleRoot)
38383839
{

src/validation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ bool TestBlockValidity(BlockValidationState& state,
336336
CChainState& chainstate,
337337
const CBlock& block,
338338
CBlockIndex* pindexPrev,
339-
const std::function<int64_t()>& adjusted_time_callback,
339+
const std::function<NodeClock::time_point()>& adjusted_time_callback,
340340
bool fCheckPOW = true,
341341
bool fCheckMerkleRoot = true) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
342342

@@ -840,7 +840,7 @@ class ChainstateManager
840840

841841
const CChainParams m_chainparams;
842842

843-
const std::function<int64_t()> m_adjusted_time_callback;
843+
const std::function<NodeClock::time_point()> m_adjusted_time_callback;
844844

845845
//! Internal helper for ActivateSnapshot().
846846
[[nodiscard]] bool PopulateAndValidateSnapshot(

0 commit comments

Comments
 (0)