Skip to content

Commit

Permalink
timedata: rename variables to match the coding style
Browse files Browse the repository at this point in the history
Rename the local variables in `src/timedata.cpp`:
`setKnown` -> `g_sources`
`vTimeOffsets` -> `g_time_offsets`
`fDone` -> `g_warning_emitted`
  • Loading branch information
vasild committed Apr 9, 2021
1 parent b73511d commit cdba849
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/timedata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,31 @@ int64_t GetAdjustedTime()

#define BITCOIN_TIMEDATA_MAX_SAMPLES 200

static std::set<CNetAddr> setKnown;
static CMedianFilter<int64_t> vTimeOffsets(BITCOIN_TIMEDATA_MAX_SAMPLES, 0);
static bool fDone;
static std::set<CNetAddr> g_sources;
static CMedianFilter<int64_t> g_time_offsets(BITCOIN_TIMEDATA_MAX_SAMPLES, 0);
static bool g_warning_emitted;

void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
{
LOCK(g_timeoffset_mutex);
// Ignore duplicates
if (setKnown.size() == BITCOIN_TIMEDATA_MAX_SAMPLES)
if (g_sources.size() == BITCOIN_TIMEDATA_MAX_SAMPLES)
return;
if (!setKnown.insert(ip).second)
if (!g_sources.insert(ip).second)
return;

// Add data
vTimeOffsets.input(nOffsetSample);
LogPrint(BCLog::NET, "added time data, samples %d, offset %+d (%+d minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample / 60);
g_time_offsets.input(nOffsetSample);
LogPrint(BCLog::NET, "added time data, samples %d, offset %+d (%+d minutes)\n", g_time_offsets.size(), nOffsetSample, nOffsetSample / 60);

// There is a known issue here (see issue #4521):
//
// - The structure vTimeOffsets contains up to 200 elements, after which
// - The structure g_time_offsets contains up to 200 elements, after which
// any new element added to it will not increase its size, replacing the
// oldest element.
//
// - The condition to update nTimeOffset includes checking whether the
// number of elements in vTimeOffsets is odd, which will never happen after
// number of elements in g_time_offsets is odd, which will never happen after
// there are 200 elements.
//
// But in this case the 'bug' is protective against some attacks, and may
Expand All @@ -72,25 +72,25 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
// So we should hold off on fixing this and clean it up as part of
// a timing cleanup that strengthens it in a number of other ways.
//
if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1) {
int64_t nMedian = vTimeOffsets.median();
std::vector<int64_t> vSorted = vTimeOffsets.sorted();
if (g_time_offsets.size() >= 5 && g_time_offsets.size() % 2 == 1) {
int64_t nMedian = g_time_offsets.median();
std::vector<int64_t> vSorted = g_time_offsets.sorted();
// Only let other nodes change our time by so much
int64_t max_adjustment = std::max<int64_t>(0, gArgs.GetArg("-maxtimeadjustment", DEFAULT_MAX_TIME_ADJUSTMENT));
if (nMedian >= -max_adjustment && nMedian <= max_adjustment) {
nTimeOffset = nMedian;
} else {
nTimeOffset = 0;

if (!fDone) {
if (!g_warning_emitted) {
// If nobody has a time different than ours but within 5 minutes of ours, give a warning
bool fMatch = false;
for (const int64_t nOffset : vSorted) {
if (nOffset != 0 && nOffset > -5 * 60 && nOffset < 5 * 60) fMatch = true;
}

if (!fMatch) {
fDone = true;
g_warning_emitted = true;
bilingual_str strMessage = strprintf(_("Please check that your computer's date and time are correct! If your clock is wrong, %s will not work properly."), PACKAGE_NAME);
SetMiscWarning(strMessage);
uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::MSG_WARNING);
Expand All @@ -112,7 +112,7 @@ void TestOnlyResetTimeData()
{
LOCK(g_timeoffset_mutex);
nTimeOffset = 0;
setKnown.clear();
vTimeOffsets = CMedianFilter<int64_t>(BITCOIN_TIMEDATA_MAX_SAMPLES, 0);
fDone = false;
g_sources.clear();
g_time_offsets = CMedianFilter<int64_t>(BITCOIN_TIMEDATA_MAX_SAMPLES, 0);
g_warning_emitted = false;
}

0 comments on commit cdba849

Please sign in to comment.