Skip to content

Commit 85c8a5e

Browse files
committed
Merge bitcoin/bitcoin#29647: Avoid divide-by-zero in header sync logs when NodeClock is behind
fa4d98b Avoid divide-by-zero in header sync logs when NodeClock is behind (MarcoFalke) fa58550 refactor: Modernize header sync logs (MarcoFalke) Pull request description: The log may be confusing, when the NodeClock is behind the current header tip. Fix it, by assuming the NodeClock is never behind the current header tip. ACKs for top commit: sipa: utACK fa4d98b sr-gi: tACK [fa4d98b](bitcoin/bitcoin@fa4d98b) achow101: ACK fa4d98b tdb3: ACK fa4d98b Tree-SHA512: 3c5aee4030af387695918c5238012c972ebf850b52e956b5f74590cd7fd4eff0b3e593d411e3eb2a0bb12294af8dc6fbe320f90e4c261399b65a404ff3c3cbd9
2 parents 2ffaa92 + fa4d98b commit 85c8a5e

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/validation.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4234,9 +4234,10 @@ bool ChainstateManager::ProcessNewBlockHeaders(const std::vector<CBlockHeader>&
42344234
if (NotifyHeaderTip(*this)) {
42354235
if (IsInitialBlockDownload() && ppindex && *ppindex) {
42364236
const CBlockIndex& last_accepted{**ppindex};
4237-
const int64_t blocks_left{(GetTime() - last_accepted.GetBlockTime()) / GetConsensus().nPowTargetSpacing};
4237+
int64_t blocks_left{(NodeClock::now() - last_accepted.Time()) / GetConsensus().PowTargetSpacing()};
4238+
blocks_left = std::max<int64_t>(0, blocks_left);
42384239
const double progress{100.0 * last_accepted.nHeight / (last_accepted.nHeight + blocks_left)};
4239-
LogPrintf("Synchronizing blockheaders, height: %d (~%.2f%%)\n", last_accepted.nHeight, progress);
4240+
LogInfo("Synchronizing blockheaders, height: %d (~%.2f%%)\n", last_accepted.nHeight, progress);
42404241
}
42414242
}
42424243
return true;
@@ -4260,9 +4261,10 @@ void ChainstateManager::ReportHeadersPresync(const arith_uint256& work, int64_t
42604261
bool initial_download = IsInitialBlockDownload();
42614262
GetNotifications().headerTip(GetSynchronizationState(initial_download), height, timestamp, /*presync=*/true);
42624263
if (initial_download) {
4263-
const int64_t blocks_left{(GetTime() - timestamp) / GetConsensus().nPowTargetSpacing};
4264+
int64_t blocks_left{(NodeClock::now() - NodeSeconds{std::chrono::seconds{timestamp}}) / GetConsensus().PowTargetSpacing()};
4265+
blocks_left = std::max<int64_t>(0, blocks_left);
42644266
const double progress{100.0 * height / (height + blocks_left)};
4265-
LogPrintf("Pre-synchronizing blockheaders, height: %d (~%.2f%%)\n", height, progress);
4267+
LogInfo("Pre-synchronizing blockheaders, height: %d (~%.2f%%)\n", height, progress);
42664268
}
42674269
}
42684270

0 commit comments

Comments
 (0)