Skip to content

Commit 60da1ea

Browse files
committed
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.
1 parent 08bcfa2 commit 60da1ea

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

src/test/timedata_tests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ BOOST_AUTO_TEST_CASE(addtimedata)
9696
// not to fix this because it prevents possible attacks. See the comment in AddTimeData() or issue #4521
9797
// for a more detailed explanation.
9898
MultiAddTimeData(2, 100); // filter median is 100 now, but nTimeOffset will not change
99+
// We want this test to end with nTimeOffset==0, otherwise subsequent tests of the suite will fail.
99100
BOOST_CHECK_EQUAL(GetTimeOffset(), 0);
100101

101-
// We want this test to end with nTimeOffset==0, otherwise subsequent tests of the suite will fail.
102+
TestOnlyResetTimeData();
102103
}
103104

104105
BOOST_AUTO_TEST_SUITE_END()

src/timedata.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,20 @@ int64_t GetAdjustedTime()
3939

4040
#define BITCOIN_TIMEDATA_MAX_SAMPLES 200
4141

42+
static std::set<CNetAddr> setKnown;
43+
static CMedianFilter<int64_t> vTimeOffsets(BITCOIN_TIMEDATA_MAX_SAMPLES, 0);
44+
static bool fDone;
45+
4246
void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
4347
{
4448
LOCK(g_timeoffset_mutex);
4549
// Ignore duplicates
46-
static std::set<CNetAddr> setKnown;
4750
if (setKnown.size() == BITCOIN_TIMEDATA_MAX_SAMPLES)
4851
return;
4952
if (!setKnown.insert(ip).second)
5053
return;
5154

5255
// Add data
53-
static CMedianFilter<int64_t> vTimeOffsets(BITCOIN_TIMEDATA_MAX_SAMPLES, 0);
5456
vTimeOffsets.input(nOffsetSample);
5557
LogPrint(BCLog::NET, "added time data, samples %d, offset %+d (%+d minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample / 60);
5658

@@ -81,7 +83,6 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
8183
} else {
8284
nTimeOffset = 0;
8385

84-
static bool fDone;
8586
if (!fDone) {
8687
// If nobody has a time different than ours but within 5 minutes of ours, give a warning
8788
bool fMatch = false;
@@ -108,3 +109,12 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
108109
}
109110
}
110111
}
112+
113+
void TestOnlyResetTimeData()
114+
{
115+
LOCK(g_timeoffset_mutex);
116+
nTimeOffset = 0;
117+
setKnown.clear();
118+
vTimeOffsets = CMedianFilter<int64_t>(BITCOIN_TIMEDATA_MAX_SAMPLES, 0);
119+
fDone = false;
120+
}

src/timedata.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,9 @@ int64_t GetTimeOffset();
7575
int64_t GetAdjustedTime();
7676
void AddTimeData(const CNetAddr& ip, int64_t nTime);
7777

78+
/**
79+
* Reset the internal state of GetTimeOffset(), GetAdjustedTime() and AddTimeData().
80+
*/
81+
void TestOnlyResetTimeData();
82+
7883
#endif // BITCOIN_TIMEDATA_H

0 commit comments

Comments
 (0)