Skip to content

Commit 0f35f4d

Browse files
committed
Merge bitcoin/bitcoin#25786: refactor: Make adjusted time type safe
eeee5ad Make adjusted time type safe (MacroFake) fa3be79 Add time helpers (MacroFake) Pull request description: This makes follow-ups easier to review. Also, it makes sense by itself. ACKs for top commit: ryanofsky: Code review ACK eeee5ad. Confirmed type changes and equivalent code changes only. Tree-SHA512: 51bf1ae5428552177286113babdd49e82459d6c710a07b6e80a0a045d373cf51045ee010461aba98e0151d8d71b9b3b5f8f73e302d46ba4558e0b55201f99e9f
2 parents 027b672 + eeee5ad commit 0f35f4d

File tree

11 files changed

+36
-16
lines changed

11 files changed

+36
-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/chain.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <primitives/block.h>
1313
#include <sync.h>
1414
#include <uint256.h>
15+
#include <util/time.h>
1516

1617
#include <vector>
1718

@@ -275,6 +276,11 @@ class CBlockIndex
275276
*/
276277
bool HaveTxsDownloaded() const { return nChainTx != 0; }
277278

279+
NodeSeconds Time() const
280+
{
281+
return NodeSeconds{std::chrono::seconds{nTime}};
282+
}
283+
278284
int64_t GetBlockTime() const
279285
{
280286
return (int64_t)nTime;

src/consensus/params.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <uint256.h>
1010

11+
#include <chrono>
1112
#include <limits>
1213
#include <map>
1314

@@ -109,6 +110,10 @@ struct Params {
109110
bool fPowNoRetargeting;
110111
int64_t nPowTargetSpacing;
111112
int64_t nPowTargetTimespan;
113+
std::chrono::seconds PowTargetSpacing() const
114+
{
115+
return std::chrono::seconds{nPowTargetSpacing};
116+
}
112117
int64_t DifficultyAdjustmentInterval() const { return nPowTargetTimespan / nPowTargetSpacing; }
113118
/** The best chain should have at least this much work */
114119
uint256 nMinimumChainWork;

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
@@ -1151,7 +1151,7 @@ bool PeerManagerImpl::TipMayBeStale()
11511151

11521152
bool PeerManagerImpl::CanDirectFetch()
11531153
{
1154-
return m_chainman.ActiveChain().Tip()->GetBlockTime() > GetAdjustedTime() - m_chainparams.GetConsensus().nPowTargetSpacing * 20;
1154+
return m_chainman.ActiveChain().Tip()->Time() > GetAdjustedTime() - m_chainparams.GetConsensus().PowTargetSpacing() * 20;
11551155
}
11561156

11571157
static bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
@@ -4913,7 +4913,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
49134913

49144914
if (!state.fSyncStarted && CanServeBlocks(*peer) && !fImporting && !fReindex) {
49154915
// Only actively request headers from a single peer, unless we're close to today.
4916-
if ((nSyncStarted == 0 && sync_blocks_and_headers_from_peer) || m_chainman.m_best_header->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {
4916+
if ((nSyncStarted == 0 && sync_blocks_and_headers_from_peer) || m_chainman.m_best_header->Time() > GetAdjustedTime() - 24h) {
49174917
const CBlockIndex* pindexStart = m_chainman.m_best_header;
49184918
/* If possible, start at the block preceding the currently
49194919
best known header. This ensures that we always get a
@@ -4933,7 +4933,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
49334933
// Convert HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER to microseconds before scaling
49344934
// to maintain precision
49354935
std::chrono::microseconds{HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER} *
4936-
(GetAdjustedTime() - m_chainman.m_best_header->GetBlockTime()) / consensusParams.nPowTargetSpacing
4936+
Ticks<std::chrono::seconds>(GetAdjustedTime() - m_chainman.m_best_header->Time()) / consensusParams.nPowTargetSpacing
49374937
);
49384938
nSyncStarted++;
49394939
}
@@ -5250,7 +5250,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
52505250
// Check for headers sync timeouts
52515251
if (state.fSyncStarted && state.m_headers_sync_timeout < std::chrono::microseconds::max()) {
52525252
// Detect whether this is a stalling initial-headers-sync peer
5253-
if (m_chainman.m_best_header->GetBlockTime() <= GetAdjustedTime() - 24 * 60 * 60) {
5253+
if (m_chainman.m_best_header->Time() <= GetAdjustedTime() - 24h) {
52545254
if (current_time > state.m_headers_sync_timeout && nSyncStarted == 1 && (m_num_preferred_download_peers - state.fPreferredDownload >= 1)) {
52555255
// Disconnect a peer (without NetPermissionFlags::NoBan permission) if it is our only sync peer,
52565256
// 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/primitives/block.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <primitives/transaction.h>
1010
#include <serialize.h>
1111
#include <uint256.h>
12+
#include <util/time.h>
1213

1314
/** Nodes collect new transactions into a block, hash them into a hash tree,
1415
* and scan through nonce values to make the block's hash satisfy proof-of-work
@@ -52,6 +53,11 @@ class CBlockHeader
5253

5354
uint256 GetHash() const;
5455

56+
NodeSeconds Time() const
57+
{
58+
return NodeSeconds{std::chrono::seconds{nTime}};
59+
}
60+
5561
int64_t GetBlockTime() const
5662
{
5763
return (int64_t)nTime;

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
@@ -3441,7 +3441,7 @@ std::vector<unsigned char> ChainstateManager::GenerateCoinbaseCommitment(CBlock&
34413441
* in ConnectBlock().
34423442
* Note that -reindex-chainstate skips the validation that happens here!
34433443
*/
3444-
static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidationState& state, BlockManager& blockman, const ChainstateManager& chainman, const CBlockIndex* pindexPrev, int64_t nAdjustedTime) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
3444+
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)
34453445
{
34463446
AssertLockHeld(::cs_main);
34473447
assert(pindexPrev != nullptr);
@@ -3469,8 +3469,9 @@ static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidatio
34693469
return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, "time-too-old", "block's timestamp is too early");
34703470

34713471
// Check timestamp
3472-
if (block.GetBlockTime() > nAdjustedTime + MAX_FUTURE_BLOCK_TIME)
3472+
if (block.Time() > now + std::chrono::seconds{MAX_FUTURE_BLOCK_TIME}) {
34733473
return state.Invalid(BlockValidationResult::BLOCK_TIME_FUTURE, "time-too-new", "block timestamp too far in the future");
3474+
}
34743475

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

0 commit comments

Comments
 (0)