AddTimeData() has poor test coverage but interesting logic (including a bug turned into a feature). This PR adds a unit test for it.
test: Add unit test for AddTimeData #16563
pull mzumsande wants to merge 1 commits into bitcoin:master from mzumsande:201908_test_timedata changing 1 files +64 −1-
mzumsande commented at 3:07 PM on August 7, 2019: member
- DrahtBot added the label Tests on Aug 7, 2019
-
practicalswift commented at 4:44 PM on August 7, 2019: contributor
Welcome as a contributor @mzumsande!
Would you mind adding a comment describing the bug-turned-feature to give some context to this change? :-)
-
MarcoFalke commented at 4:49 PM on August 7, 2019: member
Maybe this:
- AddTimeData will never update nTimeOffset past 199 samples #4521
-
mzumsande commented at 4:56 PM on August 7, 2019: member
Yes, I meant #4521 - there is also a comment about this in the source code of timedata.cpp which I referenced in the test.
Basically, the method of getting offsets from peers will become ineffective after it has reached 200 samples, because it only attempts to change
nTimeOffsetwhen it the filter size is odd. SonTimeOffsetwill never be changed after offsets from 200 peers have been received (unless you restart the node). -
practicalswift commented at 5:03 PM on August 7, 2019: contributor
@mzumsande Could you add a comment to
BOOST_AUTO_TEST_CASE(addtimedata)describing the intended behaviour and the rationale behind doing so? - mzumsande force-pushed on Aug 7, 2019
- mzumsande force-pushed on Aug 7, 2019
-
mzumsande commented at 6:52 PM on August 7, 2019: member
@practicalswift I added a comment about this and also adjusted the other comments to make it more clear what is tested.
-
in src/test/timedata_tests.cpp:57 in cf02a00427 outdated
52 | +BOOST_AUTO_TEST_CASE(addtimedata) 53 | +{ 54 | + BOOST_CHECK_EQUAL(GetTimeOffset(), 0); 55 | + 56 | + //Part 1: Add large offsets to test a warning message that our clock may be wrong. 57 | + MultiAddTimeData(3, DEFAULT_MAX_TIME_ADJUSTMENT + 1); //filter size 4, initialized with 0
ariard commented at 8:10 PM on August 7, 2019:You add 3, but filter size is 4, is vTimeOffsets seeded with a first sample somewhere in test setup ?
mzumsande commented at 10:09 PM on August 7, 2019:Not in the test, but in
timedata.cpp: vTimeOffsets is static and initialized with one element, offset 0:static CMedianFilter<int64_t> vTimeOffsets(BITCOIN_TIMEDATA_MAX_SAMPLES, 0); I later add the same number of positive and negative elements so that this initial 0 element stays the median at the end. Will change the commentinitialized with 0to a better wording.
ariard commented at 1:23 AM on August 8, 2019:You're right, still have to get familiar with cpp multiple ways of initializing arrays.
in src/test/timedata_tests.cpp:89 in cf02a00427 outdated
84 | + 85 | + // After the number of offsets has reached MAX_SAMPLES -1 (=199), nTimeOffset will never change 86 | + // because it is only updated when the number of elements in the filter becomes odd. It was decided 87 | + // not to fix this because it prevents possible attacks. See the comment in AddTimeData() or issue #4521 88 | + // for a more detailed explanation. 89 | + MultiAddTimeData(MAX_SAMPLES, 200); // filter median is 200, but nTimeOffset will not change
ariard commented at 8:12 PM on August 7, 2019:Shouldn't be better to try to increment by one instead of MAX_SAMPLES to see if threshold effect happens as expected?
mzumsande commented at 10:14 PM on August 7, 2019:I wanted to point out that you can replace as much elements in
vTimeOffsetsas you want withoutnTimeOffsetever changing, but yeah, MAX_SAMPLES is probably a bit overkill, so will change this to a lower value.
ariard commented at 1:26 AM on August 8, 2019:Yes, median doesn't change anymore at MAX_SAMPLES so that's this threshold effect you're interested to see I think (in this case nothing should change)
ariard commented at 8:14 PM on August 7, 2019: memberOtherwise, tested ACK cf02a00,
Thanks to give an opportunity to read about AddTimeData!
mzumsande force-pushed on Aug 7, 2019ariard commented at 1:27 AM on August 8, 2019: memberACK 50a9f58.
in src/test/timedata_tests.cpp:41 in 50a9f58052 outdated
37 | @@ -34,4 +38,62 @@ BOOST_AUTO_TEST_CASE(util_MedianFilter) 38 | BOOST_CHECK_EQUAL(filter.median(), 7); 39 | } 40 | 41 | +void MultiAddTimeData(int n, int64_t offset)
promag commented at 10:42 PM on August 12, 2019:nit, static function.
in src/test/timedata_tests.cpp:48 in 50a9f58052 outdated
37 | @@ -34,4 +38,62 @@ BOOST_AUTO_TEST_CASE(util_MedianFilter) 38 | BOOST_CHECK_EQUAL(filter.median(), 7); 39 | } 40 | 41 | +void MultiAddTimeData(int n, int64_t offset) 42 | +{ 43 | + static int cnt = 0; 44 | + for (int i = 0; i < n; ++i) { 45 | + CNetAddr addr; 46 | + addr.SetInternal(std::to_string(++cnt));
promag commented at 10:42 PM on August 12, 2019:nit, should
#include <string>- iwyu.in src/test/timedata_tests.cpp:65 in 50a9f58052 outdated
60 | + noui_suppress(); 61 | + MultiAddTimeData(1, DEFAULT_MAX_TIME_ADJUSTMENT + 1); //filter size 5 62 | + noui_reconnect(); 63 | + 64 | + std::string warning = GetWarnings("gui"); 65 | + BOOST_CHECK(warning.find("clock is wrong") != std::string::npos);
promag commented at 10:46 PM on August 12, 2019:nit, could avoid
warningvar by doingGetWarnings("gui").find("...").Add test for AddTimeData 7cd069d8efmzumsande force-pushed on Aug 13, 2019laanwj commented at 12:30 PM on August 14, 2019: memberACK 7cd069d8ef900c6c6b904ddd6fbd64e14bd0f53e, thanks for adding a test
laanwj merged this on Aug 14, 2019laanwj closed this on Aug 14, 2019laanwj referenced this in commit a7aa809027 on Aug 14, 2019mzumsande deleted the branch on Aug 16, 2019deadalnix referenced this in commit 60108a9eb9 on Apr 27, 2020ftrader referenced this in commit d9dae9dd35 on Aug 17, 2020PastaPastaPasta referenced this in commit 96ef3b1f3a on Nov 1, 2021PastaPastaPasta referenced this in commit 0cb1a9d39f on Nov 3, 2021pravblockc referenced this in commit 084a233631 on Nov 18, 2021DrahtBot locked this on Dec 16, 2021Labels
github-metadata-mirror
This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-17 03:14 UTC
This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me