Skip to content

Commit 288f6e2

Browse files
fanquakevijaydasmp
authored andcommitted
Merge 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
1 parent 26ea618 commit 288f6e2

File tree

8 files changed

+29
-12
lines changed

8 files changed

+29
-12
lines changed

src/chain.h

+6
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

@@ -259,6 +260,11 @@ class CBlockIndex
259260
*/
260261
bool HaveTxsDownloaded() const { return nChainTx != 0; }
261262

263+
NodeSeconds Time() const
264+
{
265+
return NodeSeconds{std::chrono::seconds{nTime}};
266+
}
267+
262268
int64_t GetBlockTime() const
263269
{
264270
return (int64_t)nTime;

src/consensus/params.h

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <uint256.h>
1010
#include <llmq/params.h>
1111

12+
#include <chrono>
1213
#include <limits>
1314
#include <vector>
1415

@@ -168,6 +169,10 @@ struct Params {
168169
int64_t nPowTargetTimespan;
169170
int nPowKGWHeight;
170171
int nPowDGWHeight;
172+
std::chrono::seconds PowTargetSpacing() const
173+
{
174+
return std::chrono::seconds{nPowTargetSpacing};
175+
}
171176
int64_t DifficultyAdjustmentInterval() const { return nPowTargetTimespan / nPowTargetSpacing; }
172177
uint256 nMinimumChainWork;
173178
uint256 defaultAssumeValid;

src/net_processing.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,7 @@ bool PeerManagerImpl::TipMayBeStale()
13021302

13031303
bool PeerManagerImpl::CanDirectFetch()
13041304
{
1305-
return m_chainman.ActiveChain().Tip()->GetBlockTime() > GetAdjustedTime() - m_chainparams.GetConsensus().nPowTargetSpacing * 20;
1305+
return m_chainman.ActiveChain().Tip()->Time() > GetAdjustedTime() - m_chainparams.GetConsensus().PowTargetSpacing() * 20;
13061306
}
13071307

13081308
static bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
@@ -5833,7 +5833,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
58335833

58345834
if (!state.fSyncStarted && CanServeBlocks(*peer) && !fImporting && !fReindex && pto->CanRelay()) {
58355835
// Only actively request headers from a single peer, unless we're close to end of initial download.
5836-
if ((nSyncStarted == 0 && sync_blocks_and_headers_from_peer) || m_chainman.m_best_header->GetBlockTime() > GetAdjustedTime() - nMaxTipAge) {
5836+
if ((nSyncStarted == 0 && sync_blocks_and_headers_from_peer) || m_chainman.m_best_header->Time() > GetAdjustedTime() - 24h) {
58375837
const CBlockIndex* pindexStart = m_chainman.m_best_header;
58385838
/* If possible, start at the block preceding the currently
58395839
best known header. This ensures that we always get a
@@ -5854,7 +5854,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
58545854
// Convert HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER to microseconds before scaling
58555855
// to maintain precision
58565856
std::chrono::microseconds{HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER} *
5857-
(GetAdjustedTime() - m_chainman.m_best_header->GetBlockTime()) / consensusParams.nPowTargetSpacing
5857+
Ticks<std::chrono::seconds>(GetAdjustedTime() - m_chainman.m_best_header->Time()) / consensusParams.nPowTargetSpacing
58585858
);
58595859
nSyncStarted++;
58605860
}
@@ -6233,7 +6233,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
62336233
// Check for headers sync timeouts
62346234
if (state.fSyncStarted && state.m_headers_sync_timeout < std::chrono::microseconds::max()) {
62356235
// Detect whether this is a stalling initial-headers-sync peer
6236-
if (m_chainman.m_best_header->GetBlockTime() <= GetAdjustedTime() - nMaxTipAge) {
6236+
if (m_chainman.m_best_header->Time() <= GetAdjustedTime() - 24h) {
62376237
if (current_time > state.m_headers_sync_timeout && nSyncStarted == 1 && (m_num_preferred_download_peers - state.fPreferredDownload >= 1)) {
62386238
// Disconnect a peer (without NetPermissionFlags::NoBan permission) if it is our only sync peer,
62396239
// and we have others we could be using instead.

src/node/miner.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
4747
{
4848
int64_t nOldTime = pblock->nTime;
49-
int64_t nNewTime = std::max(pindexPrev->GetMedianTimePast() + 1, GetAdjustedTime());
49+
int64_t nNewTime{std::max<int64_t>(pindexPrev->GetMedianTimePast() + 1, TicksSinceEpoch<std::chrono::seconds>(GetAdjustedTime()))};
5050

5151
if (nOldTime < nNewTime) {
5252
pblock->nTime = nNewTime;
@@ -158,7 +158,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
158158
pblock->nVersion = gArgs.GetIntArg("-blockversion", pblock->nVersion);
159159
}
160160

161-
pblock->nTime = GetAdjustedTime();
161+
pblock->nTime = TicksSinceEpoch<std::chrono::seconds>(GetAdjustedTime());
162162
m_lock_time_cutoff = pindexPrev->GetMedianTimePast();
163163

164164
if (fDIP0003Active_context) {

src/primitives/block.h

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <uint256.h>
1313
#include <cstddef>
1414
#include <type_traits>
15+
#include <util/time.h>
1516

1617
/** Nodes collect new transactions into a block, hash them into a hash tree,
1718
* and scan through nonce values to make the block's hash satisfy proof-of-work
@@ -55,6 +56,11 @@ class CBlockHeader
5556

5657
uint256 GetHash() const;
5758

59+
NodeSeconds Time() const
60+
{
61+
return NodeSeconds{std::chrono::seconds{nTime}};
62+
}
63+
5864
int64_t GetBlockTime() const
5965
{
6066
return (int64_t)nTime;

src/timedata.cpp

+2-2
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

+1-1
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

+3-3
Original file line numberDiff line numberDiff line change
@@ -3901,7 +3901,7 @@ bool CheckBlock(const CBlock& block, BlockValidationState& state, const Consensu
39013901
* in ConnectBlock().
39023902
* Note that -reindex-chainstate skips the validation that happens here!
39033903
*/
3904-
static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidationState& state, BlockManager& blockman, const CChainParams& params, const CBlockIndex* pindexPrev, int64_t nAdjustedTime) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
3904+
static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidationState& state, BlockManager& blockman, const CChainParams& params, const CBlockIndex* pindexPrev, NodeClock::time_point now) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
39053905
{
39063906
AssertLockHeld(::cs_main);
39073907
assert(pindexPrev != nullptr);
@@ -3941,9 +3941,9 @@ static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidatio
39413941
return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, "time-too-old", strprintf("block's timestamp is too early %d %d", block.GetBlockTime(), pindexPrev->GetMedianTimePast()));
39423942

39433943
// Check timestamp
3944-
if (block.GetBlockTime() > nAdjustedTime + MAX_FUTURE_BLOCK_TIME)
3944+
if (block.Time() > now + std::chrono::seconds{MAX_FUTURE_BLOCK_TIME}) {
39453945
return state.Invalid(BlockValidationResult::BLOCK_TIME_FUTURE, "time-too-new", strprintf("block timestamp too far in the future %d %d", block.GetBlockTime(), nAdjustedTime + 2 * 60 * 60));
3946-
3946+
}
39473947
// Reject blocks with outdated version
39483948
if ((block.nVersion < 2 && DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_HEIGHTINCB)) ||
39493949
(block.nVersion < 3 && DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_DERSIG)) ||

0 commit comments

Comments
 (0)