Skip to content

Commit ad90dd9

Browse files
committed
Merge #20141: Avoid the use of abs64 in timedata
d1292f2 Avoid the use of abs64 in timedata (Pieter Wuille) Pull request description: Fixes #20135. ACKs for top commit: kallewoof: ACK d1292f2 jonatack: ACK d1292f2 code/logic review, verified there are no remaining callers of `abs64()`, verified no warnings in a debug build practicalswift: ACK d1292f2 MarcoFalke: ACK d1292f2 🎹 Tree-SHA512: d17e95c668eb5e02ea546433b3d1b5a0ccbfb2c9cec62fa67dad1844d7e278a2576fbc0b75bddbf4db9af7331e978148c7bef7fce7e6a07e0eb917ef1392f302
2 parents f79a4a8 + d1292f2 commit ad90dd9

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

src/timedata.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ int64_t GetAdjustedTime()
3636
return GetTime() + GetTimeOffset();
3737
}
3838

39-
static int64_t abs64(int64_t n)
40-
{
41-
return (n >= 0 ? n : -n);
42-
}
43-
4439
#define BITCOIN_TIMEDATA_MAX_SAMPLES 200
4540

4641
void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
@@ -79,7 +74,8 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
7974
int64_t nMedian = vTimeOffsets.median();
8075
std::vector<int64_t> vSorted = vTimeOffsets.sorted();
8176
// Only let other nodes change our time by so much
82-
if (abs64(nMedian) <= std::max<int64_t>(0, gArgs.GetArg("-maxtimeadjustment", DEFAULT_MAX_TIME_ADJUSTMENT))) {
77+
int64_t max_adjustment = std::max<int64_t>(0, gArgs.GetArg("-maxtimeadjustment", DEFAULT_MAX_TIME_ADJUSTMENT));
78+
if (nMedian >= -max_adjustment && nMedian <= max_adjustment) {
8379
nTimeOffset = nMedian;
8480
} else {
8581
nTimeOffset = 0;
@@ -89,7 +85,7 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
8985
// If nobody has a time different than ours but within 5 minutes of ours, give a warning
9086
bool fMatch = false;
9187
for (const int64_t nOffset : vSorted) {
92-
if (nOffset != 0 && abs64(nOffset) < 5 * 60) fMatch = true;
88+
if (nOffset != 0 && nOffset > -5 * 60 && nOffset < 5 * 60) fMatch = true;
9389
}
9490

9591
if (!fMatch) {

0 commit comments

Comments
 (0)