aboutsummaryrefslogtreecommitdiff
path: root/src/timedata.cpp
diff options
context:
space:
mode:
authorVasil Dimov <vd@FreeBSD.org>2021-01-18 14:27:00 +0100
committerVasil Dimov <vd@FreeBSD.org>2022-03-02 15:40:30 +0100
commit60da1eaa1113e7318e273144e7ef9c8895d7ed54 (patch)
tree6d9b7096cf9f87344fe14c27f87f008f0a94d21f /src/timedata.cpp
parent08bcfa27675da5c65e4c9eab7e7764eab0599298 (diff)
downloadbitcoin-60da1eaa1113e7318e273144e7ef9c8895d7ed54.tar.xz
timedata: make it possible to reset the state
Add a new function `TestOnlyResetTimeData()` which would reset the internal state used by `GetTimeOffset()`, `GetAdjustedTime()` and `AddTimeData()`. This is needed so that unit tests that call `AddTimeData()` can restore the state in order not to confuse other tests that rely on it. Currently `timedata_tests/addtimedata` is the only test that modifies the state (via `AddTimeData()`) and also the only test that relies on that state.
Diffstat (limited to 'src/timedata.cpp')
-rw-r--r--src/timedata.cpp16
1 files changed, 13 insertions, 3 deletions
diff --git a/src/timedata.cpp b/src/timedata.cpp
index 541580b3ff..ed651e3e9e 100644
--- a/src/timedata.cpp
+++ b/src/timedata.cpp
@@ -39,18 +39,20 @@ 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;
+
void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
{
LOCK(g_timeoffset_mutex);
// Ignore duplicates
- static std::set<CNetAddr> setKnown;
if (setKnown.size() == BITCOIN_TIMEDATA_MAX_SAMPLES)
return;
if (!setKnown.insert(ip).second)
return;
// Add data
- static CMedianFilter<int64_t> vTimeOffsets(BITCOIN_TIMEDATA_MAX_SAMPLES, 0);
vTimeOffsets.input(nOffsetSample);
LogPrint(BCLog::NET, "added time data, samples %d, offset %+d (%+d minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample / 60);
@@ -81,7 +83,6 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
} else {
nTimeOffset = 0;
- static bool fDone;
if (!fDone) {
// If nobody has a time different than ours but within 5 minutes of ours, give a warning
bool fMatch = false;
@@ -108,3 +109,12 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
}
}
}
+
+void TestOnlyResetTimeData()
+{
+ LOCK(g_timeoffset_mutex);
+ nTimeOffset = 0;
+ setKnown.clear();
+ vTimeOffsets = CMedianFilter<int64_t>(BITCOIN_TIMEDATA_MAX_SAMPLES, 0);
+ fDone = false;
+}