Skip to content

Commit 3c13f5d

Browse files
committed
Merge bitcoin/bitcoin#28956: Nuke adjusted time from validation (attempt 2)
ff9039f Remove GetAdjustedTime (dergoegge) Pull request description: This picks up parts of #25908. The use of adjusted time is removed from validation code while the warning to users if their clock is out of sync with the rest of the network remains. ACKs for top commit: naumenkogs: ACK ff9039f achow101: ACK ff9039f maflcko: lgtm ACK ff9039f 🤽 stickies-v: ACK ff9039f Tree-SHA512: d1f6b9445c236915503fd2ea828f0d3b92285a5dbc677b168453276115e349972edbad37194d8becd9136d8e7219b576af64ec51c72bdb1923e57e405c0483fc
2 parents 3c63c2f + ff9039f commit 3c13f5d

13 files changed

+15
-29
lines changed

src/bitcoin-chainstate.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ int main(int argc, char* argv[])
117117
const ChainstateManager::Options chainman_opts{
118118
.chainparams = *chainparams,
119119
.datadir = abs_datadir,
120-
.adjusted_time_callback = NodeClock::now,
121120
.notifications = *notifications,
122121
};
123122
const node::BlockManager::Options blockman_opts{

src/headerssync.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ HeadersSyncState::HeadersSyncState(NodeId id, const Consensus::Params& consensus
4141
// exceeds this bound, because it's not possible for a consensus-valid
4242
// chain to be longer than this (at the current time -- in the future we
4343
// could try again, if necessary, to sync a longer chain).
44-
m_max_commitments = 6*(Ticks<std::chrono::seconds>(GetAdjustedTime() - NodeSeconds{std::chrono::seconds{chain_start->GetMedianTimePast()}}) + MAX_FUTURE_BLOCK_TIME) / HEADER_COMMITMENT_PERIOD;
44+
m_max_commitments = 6*(Ticks<std::chrono::seconds>(NodeClock::now() - NodeSeconds{std::chrono::seconds{chain_start->GetMedianTimePast()}}) + MAX_FUTURE_BLOCK_TIME) / HEADER_COMMITMENT_PERIOD;
4545

4646
LogPrint(BCLog::NET, "Initial headers sync started with peer=%d: height=%i, max_commitments=%i, min_work=%s\n", m_id, m_current_height, m_max_commitments, m_minimum_required_work.ToString());
4747
}

src/init.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1448,7 +1448,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14481448
ChainstateManager::Options chainman_opts{
14491449
.chainparams = chainparams,
14501450
.datadir = args.GetDataDirNet(),
1451-
.adjusted_time_callback = GetAdjustedTime,
14521451
.notifications = *node.notifications,
14531452
};
14541453
Assert(ApplyArgsManOptions(args, chainman_opts)); // no error can happen, already checked in AppInitParameterInteraction

src/kernel/chainstatemanager_opts.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ namespace kernel {
3232
struct ChainstateManagerOpts {
3333
const CChainParams& chainparams;
3434
fs::path datadir;
35-
const std::function<NodeClock::time_point()> adjusted_time_callback{nullptr};
3635
std::optional<bool> check_block_index{};
3736
bool checkpoints_enabled{DEFAULT_CHECKPOINTS_ENABLED};
3837
//! If set, it will override the minimum work we will assume exists on some valid chain.

src/net_processing.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,7 @@ int64_t PeerManagerImpl::ApproximateBestBlockDepth() const
13341334

13351335
bool PeerManagerImpl::CanDirectFetch()
13361336
{
1337-
return m_chainman.ActiveChain().Tip()->Time() > GetAdjustedTime() - m_chainparams.GetConsensus().PowTargetSpacing() * 20;
1337+
return m_chainman.ActiveChain().Tip()->Time() > NodeClock::now() - m_chainparams.GetConsensus().PowTargetSpacing() * 20;
13381338
}
13391339

13401340
static bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
@@ -5592,7 +5592,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
55925592

55935593
if (!state.fSyncStarted && CanServeBlocks(*peer) && !m_chainman.m_blockman.LoadingBlocks()) {
55945594
// Only actively request headers from a single peer, unless we're close to today.
5595-
if ((nSyncStarted == 0 && sync_blocks_and_headers_from_peer) || m_chainman.m_best_header->Time() > GetAdjustedTime() - 24h) {
5595+
if ((nSyncStarted == 0 && sync_blocks_and_headers_from_peer) || m_chainman.m_best_header->Time() > NodeClock::now() - 24h) {
55965596
const CBlockIndex* pindexStart = m_chainman.m_best_header;
55975597
/* If possible, start at the block preceding the currently
55985598
best known header. This ensures that we always get a
@@ -5612,7 +5612,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
56125612
// Convert HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER to microseconds before scaling
56135613
// to maintain precision
56145614
std::chrono::microseconds{HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER} *
5615-
Ticks<std::chrono::seconds>(GetAdjustedTime() - m_chainman.m_best_header->Time()) / consensusParams.nPowTargetSpacing
5615+
Ticks<std::chrono::seconds>(NodeClock::now() - m_chainman.m_best_header->Time()) / consensusParams.nPowTargetSpacing
56165616
);
56175617
nSyncStarted++;
56185618
}
@@ -5916,7 +5916,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
59165916
// Check for headers sync timeouts
59175917
if (state.fSyncStarted && peer->m_headers_sync_timeout < std::chrono::microseconds::max()) {
59185918
// Detect whether this is a stalling initial-headers-sync peer
5919-
if (m_chainman.m_best_header->Time() <= GetAdjustedTime() - 24h) {
5919+
if (m_chainman.m_best_header->Time() <= NodeClock::now() - 24h) {
59205920
if (current_time > peer->m_headers_sync_timeout && nSyncStarted == 1 && (m_num_preferred_download_peers - state.fPreferredDownload >= 1)) {
59215921
// Disconnect a peer (without NetPermissionFlags::NoBan permission) if it is our only sync peer,
59225922
// and we have others we could be using instead.

src/node/miner.cpp

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

3636
if (nOldTime < nNewTime) {
3737
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 = TicksSinceEpoch<std::chrono::seconds>(GetAdjustedTime());
136+
pblock->nTime = TicksSinceEpoch<std::chrono::seconds>(NodeClock::now());
137137
m_lock_time_cutoff = pindexPrev->GetMedianTimePast();
138138

139139
int nPackagesSelected = 0;
@@ -171,7 +171,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
171171

172172
BlockValidationState state;
173173
if (m_options.test_block_validity && !TestBlockValidity(state, chainparams, m_chainstate, *pblock, pindexPrev,
174-
GetAdjustedTime, /*fCheckPOW=*/false, /*fCheckMerkleRoot=*/false)) {
174+
/*fCheckPOW=*/false, /*fCheckMerkleRoot=*/false)) {
175175
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, state.ToString()));
176176
}
177177
const auto time_2{SteadyClock::now()};

src/rpc/mining.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ static RPCHelpMan generateblock()
383383
LOCK(cs_main);
384384

385385
BlockValidationState state;
386-
if (!TestBlockValidity(state, chainman.GetParams(), chainman.ActiveChainstate(), block, chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock), GetAdjustedTime, false, false)) {
386+
if (!TestBlockValidity(state, chainman.GetParams(), chainman.ActiveChainstate(), block, chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock), false, false)) {
387387
throw JSONRPCError(RPC_VERIFY_ERROR, strprintf("TestBlockValidity failed: %s", state.ToString()));
388388
}
389389
}
@@ -697,7 +697,7 @@ static RPCHelpMan getblocktemplate()
697697
if (block.hashPrevBlock != pindexPrev->GetBlockHash())
698698
return "inconclusive-not-best-prevblk";
699699
BlockValidationState state;
700-
TestBlockValidity(state, chainman.GetParams(), active_chainstate, block, pindexPrev, GetAdjustedTime, false, true);
700+
TestBlockValidity(state, chainman.GetParams(), active_chainstate, block, pindexPrev, false, true);
701701
return BIP22ValidationResult(state);
702702
}
703703

src/test/util/setup_common.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ ChainTestingSetup::ChainTestingSetup(const ChainType chainType, const std::vecto
184184
const ChainstateManager::Options chainman_opts{
185185
.chainparams = chainparams,
186186
.datadir = m_args.GetDataDirNet(),
187-
.adjusted_time_callback = GetAdjustedTime,
188187
.check_block_index = true,
189188
.notifications = *m_node.notifications,
190189
.worker_threads_num = 2,

src/test/validation_chainstatemanager_tests.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,6 @@ struct SnapshotTestSetup : TestChain100Setup {
383383
const ChainstateManager::Options chainman_opts{
384384
.chainparams = ::Params(),
385385
.datadir = chainman.m_options.datadir,
386-
.adjusted_time_callback = GetAdjustedTime,
387386
.notifications = *m_node.notifications,
388387
};
389388
const BlockManager::Options blockman_opts{

src/timedata.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ int64_t GetTimeOffset()
3333
return nTimeOffset;
3434
}
3535

36-
NodeClock::time_point GetAdjustedTime()
37-
{
38-
return NodeClock::now() + std::chrono::seconds{GetTimeOffset()};
39-
}
40-
4136
#define BITCOIN_TIMEDATA_MAX_SAMPLES 200
4237

4338
static std::set<CNetAddr> g_sources;

0 commit comments

Comments
 (0)