aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorAva Chow <github@achow101.com>2024-04-30 18:41:45 -0400
committerAva Chow <github@achow101.com>2024-04-30 18:49:34 -0400
commit0c3a3c9394e608c4beb92722ad034648af81dee7 (patch)
treefe416c62d4190f28dc0f8edbb9df9598612bcb5f /src/test
parentd813ba1bc4b4da3ad1f3812b61ff125d1d664625 (diff)
parentc6be144c4b774a03a8bcab5a165768cf81e9b06b (diff)
downloadbitcoin-0c3a3c9394e608c4beb92722ad034648af81dee7.tar.xz
Merge bitcoin/bitcoin#29623: Simplify network-adjusted time warning logic
c6be144c4b774a03a8bcab5a165768cf81e9b06b Remove timedata (stickies-v) 92e72b5d0d49aa395e626c238bc28aba8e4c3d44 [net processing] Move IgnoresIncomingTxs to PeerManagerInfo (dergoegge) 7d9c3ec622d73a98d07ab3cee78751718982a5bc [net processing] Introduce PeerManagerInfo (dergoegge) ee178dfcc1175e0af8163216c9c024f4bfc97965 Add TimeOffsets helper class (stickies-v) 55361a15d1aa6984051441bce88112000688fb43 [net processing] Use std::chrono for type-safe time offsets (stickies-v) 038fd979effb54ee76ce1b7cf078e920c652326a [net processing] Move nTimeOffset to net_processing (dergoegge) Pull request description: [An earlier approach](https://github.com/bitcoin/bitcoin/commits/1d226ae1f984c5c808f5c24c431b959cdefa692e/) in #28956 involved simplifying and refactoring the network-adjusted time calculation logic, but this was eventually [left out](https://github.com/bitcoin/bitcoin/pull/28956#issuecomment-1904214370) of the PR to make it easier for reviewers to focus on consensus logic changes. Since network-adjusted time is now only used for warning/informational purposes, cleaning up the logic (building on @dergoegge's approach in #28956) should be quite straightforward and uncontroversial. The main changes are: - Previously, we would only calculate the time offset from the first 199 outbound peers that we connected to. This limitation is now removed, and we have a proper rolling calculation. I've reduced the set to 50 outbound peers, which seems plenty. - Previously, we would automatically use the network-adjusted time if the difference was < 70 mins, and warn the user if the difference was larger than that. Since there is no longer any automated time adjustment, I've changed the warning threshold to ~~20~~ 10 minutes (which is an arbitrary number). - Previously, a warning would only be raised once, and then never again until node restart. This behaviour is now updated to 1) warn to log for every new outbound peer for as long as we appear out of sync, 2) have the RPC warning toggled on/off whenever we go in/out of sync, and 3) have the GUI warn whenever we are out of sync (again), but limited to 1 messagebox per 60 minutes - no more globals - remove the `-maxtimeadjustment` startup arg Closes #4521 ACKs for top commit: sr-gi: Re-ACK [c6be144](https://github.com/bitcoin/bitcoin/pull/29623/commits/c6be144c4b774a03a8bcab5a165768cf81e9b06b) achow101: reACK c6be144c4b774a03a8bcab5a165768cf81e9b06b dergoegge: utACK c6be144c4b774a03a8bcab5a165768cf81e9b06b Tree-SHA512: 1063d639542e882186cdcea67d225ad1f97847f44253621a8c4b36c4d777e8f5cb0efe86bc279f01e819d33056ae4364c3300cc7400c087fb16c3f39b3e16b96
Diffstat (limited to 'src/test')
-rw-r--r--src/test/denialofservice_tests.cpp2
-rw-r--r--src/test/fuzz/timedata.cpp31
-rw-r--r--src/test/fuzz/timeoffsets.cpp28
-rw-r--r--src/test/net_tests.cpp5
-rw-r--r--src/test/timedata_tests.cpp105
-rw-r--r--src/test/timeoffsets_tests.cpp69
6 files changed, 97 insertions, 143 deletions
diff --git a/src/test/denialofservice_tests.cpp b/src/test/denialofservice_tests.cpp
index 0fef8c5906..5e9ae78681 100644
--- a/src/test/denialofservice_tests.cpp
+++ b/src/test/denialofservice_tests.cpp
@@ -16,7 +16,6 @@
#include <test/util/net.h>
#include <test/util/random.h>
#include <test/util/setup_common.h>
-#include <timedata.h>
#include <util/string.h>
#include <util/time.h>
#include <validation.h>
@@ -72,7 +71,6 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
/*local_services=*/ServiceFlags(NODE_NETWORK | NODE_WITNESS),
/*version=*/PROTOCOL_VERSION,
/*relay_txs=*/true);
- TestOnlyResetTimeData();
// This test requires that we have a chain with non-zero work.
{
diff --git a/src/test/fuzz/timedata.cpp b/src/test/fuzz/timedata.cpp
deleted file mode 100644
index f5d005296b..0000000000
--- a/src/test/fuzz/timedata.cpp
+++ /dev/null
@@ -1,31 +0,0 @@
-// Copyright (c) 2020-2021 The Bitcoin Core developers
-// Distributed under the MIT software license, see the accompanying
-// file COPYING or http://www.opensource.org/licenses/mit-license.php.
-
-#include <test/fuzz/FuzzedDataProvider.h>
-#include <test/fuzz/fuzz.h>
-#include <test/fuzz/util.h>
-#include <timedata.h>
-
-#include <cstdint>
-#include <string>
-#include <vector>
-
-FUZZ_TARGET(timedata)
-{
- FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
- const unsigned int max_size = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 1000);
- // A max_size of 0 implies no limit, so cap the max number of insertions to avoid timeouts
- auto max_to_insert = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 4000);
- // Divide by 2 to avoid signed integer overflow in .median()
- const int64_t initial_value = fuzzed_data_provider.ConsumeIntegral<int64_t>() / 2;
- CMedianFilter<int64_t> median_filter{max_size, initial_value};
- while (fuzzed_data_provider.remaining_bytes() > 0 && --max_to_insert >= 0) {
- (void)median_filter.median();
- assert(median_filter.size() > 0);
- assert(static_cast<size_t>(median_filter.size()) == median_filter.sorted().size());
- assert(static_cast<unsigned int>(median_filter.size()) <= max_size || max_size == 0);
- // Divide by 2 to avoid signed integer overflow in .median()
- median_filter.input(fuzzed_data_provider.ConsumeIntegral<int64_t>() / 2);
- }
-}
diff --git a/src/test/fuzz/timeoffsets.cpp b/src/test/fuzz/timeoffsets.cpp
new file mode 100644
index 0000000000..019337a94a
--- /dev/null
+++ b/src/test/fuzz/timeoffsets.cpp
@@ -0,0 +1,28 @@
+// Copyright (c) 2024-present The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#include <node/timeoffsets.h>
+#include <test/fuzz/FuzzedDataProvider.h>
+#include <test/fuzz/fuzz.h>
+#include <test/util/setup_common.h>
+
+#include <chrono>
+#include <cstdint>
+#include <functional>
+
+void initialize_timeoffsets()
+{
+ static const auto testing_setup = MakeNoLogFileContext<>(ChainType::MAIN);
+}
+
+FUZZ_TARGET(timeoffsets, .init = initialize_timeoffsets)
+{
+ FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
+ TimeOffsets offsets{};
+ LIMITED_WHILE(fuzzed_data_provider.remaining_bytes() > 0, 4'000) {
+ (void)offsets.Median();
+ offsets.Add(std::chrono::seconds{fuzzed_data_provider.ConsumeIntegral<std::chrono::seconds::rep>()});
+ offsets.WarnIfOutOfSync();
+ }
+}
diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp
index 70a8279f04..b9dff96610 100644
--- a/src/test/net_tests.cpp
+++ b/src/test/net_tests.cpp
@@ -19,7 +19,6 @@
#include <test/util/random.h>
#include <test/util/setup_common.h>
#include <test/util/validation.h>
-#include <timedata.h>
#include <util/strencodings.h>
#include <util/string.h>
#include <validation.h>
@@ -902,10 +901,6 @@ BOOST_AUTO_TEST_CASE(initial_advertise_from_version_message)
chainman.ResetIbd();
m_node.args->ForceSetArg("-capturemessages", "0");
m_node.args->ForceSetArg("-bind", "");
- // PeerManager::ProcessMessage() calls AddTimeData() which changes the internal state
- // in timedata.cpp and later confuses the test "timedata_tests/addtimedata". Thus reset
- // that state as it was before our test was run.
- TestOnlyResetTimeData();
}
diff --git a/src/test/timedata_tests.cpp b/src/test/timedata_tests.cpp
deleted file mode 100644
index 2814dbf4c0..0000000000
--- a/src/test/timedata_tests.cpp
+++ /dev/null
@@ -1,105 +0,0 @@
-// Copyright (c) 2011-2021 The Bitcoin Core developers
-// Distributed under the MIT software license, see the accompanying
-// file COPYING or http://www.opensource.org/licenses/mit-license.php.
-//
-
-#include <netaddress.h>
-#include <noui.h>
-#include <test/util/logging.h>
-#include <test/util/setup_common.h>
-#include <timedata.h>
-#include <util/string.h>
-#include <util/translation.h>
-#include <warnings.h>
-
-#include <string>
-
-#include <boost/test/unit_test.hpp>
-
-BOOST_FIXTURE_TEST_SUITE(timedata_tests, BasicTestingSetup)
-
-BOOST_AUTO_TEST_CASE(util_MedianFilter)
-{
- CMedianFilter<int> filter(5, 15);
-
- BOOST_CHECK_EQUAL(filter.median(), 15);
-
- filter.input(20); // [15 20]
- BOOST_CHECK_EQUAL(filter.median(), 17);
-
- filter.input(30); // [15 20 30]
- BOOST_CHECK_EQUAL(filter.median(), 20);
-
- filter.input(3); // [3 15 20 30]
- BOOST_CHECK_EQUAL(filter.median(), 17);
-
- filter.input(7); // [3 7 15 20 30]
- BOOST_CHECK_EQUAL(filter.median(), 15);
-
- filter.input(18); // [3 7 18 20 30]
- BOOST_CHECK_EQUAL(filter.median(), 18);
-
- filter.input(0); // [0 3 7 18 30]
- BOOST_CHECK_EQUAL(filter.median(), 7);
-}
-
-static void MultiAddTimeData(int n, int64_t offset)
-{
- static int cnt = 0;
- for (int i = 0; i < n; ++i) {
- CNetAddr addr;
- addr.SetInternal(ToString(++cnt));
- AddTimeData(addr, offset);
- }
-}
-
-
-BOOST_AUTO_TEST_CASE(addtimedata)
-{
- BOOST_CHECK_EQUAL(GetTimeOffset(), 0);
-
- //Part 1: Add large offsets to test a warning message that our clock may be wrong.
- MultiAddTimeData(3, DEFAULT_MAX_TIME_ADJUSTMENT + 1);
- // Filter size is 1 + 3 = 4: It is always initialized with a single element (offset 0)
-
- {
- ASSERT_DEBUG_LOG("Please check that your computer's date and time are correct!");
- MultiAddTimeData(1, DEFAULT_MAX_TIME_ADJUSTMENT + 1); //filter size 5
- }
-
- BOOST_CHECK(GetWarnings(true).original.find("clock is wrong") != std::string::npos);
-
- // nTimeOffset is not changed if the median of offsets exceeds DEFAULT_MAX_TIME_ADJUSTMENT
- BOOST_CHECK_EQUAL(GetTimeOffset(), 0);
-
- // Part 2: Test positive and negative medians by adding more offsets
- MultiAddTimeData(4, 100); // filter size 9
- BOOST_CHECK_EQUAL(GetTimeOffset(), 100);
- MultiAddTimeData(10, -100); //filter size 19
- BOOST_CHECK_EQUAL(GetTimeOffset(), -100);
-
- // Part 3: Test behaviour when filter has reached maximum number of offsets
- const int MAX_SAMPLES = 200;
- int nfill = (MAX_SAMPLES - 3 - 19) / 2; //89
- MultiAddTimeData(nfill, 100);
- MultiAddTimeData(nfill, -100); //filter size MAX_SAMPLES - 3
- BOOST_CHECK_EQUAL(GetTimeOffset(), -100);
-
- MultiAddTimeData(2, 100);
- //filter size MAX_SAMPLES -1, median is the initial 0 offset
- //since we added same number of positive/negative offsets
-
- BOOST_CHECK_EQUAL(GetTimeOffset(), 0);
-
- // After the number of offsets has reached MAX_SAMPLES -1 (=199), nTimeOffset will never change
- // because it is only updated when the number of elements in the filter becomes odd. It was decided
- // not to fix this because it prevents possible attacks. See the comment in AddTimeData() or issue #4521
- // for a more detailed explanation.
- MultiAddTimeData(2, 100); // filter median is 100 now, but nTimeOffset will not change
- // We want this test to end with nTimeOffset==0, otherwise subsequent tests of the suite will fail.
- BOOST_CHECK_EQUAL(GetTimeOffset(), 0);
-
- TestOnlyResetTimeData();
-}
-
-BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/timeoffsets_tests.cpp b/src/test/timeoffsets_tests.cpp
new file mode 100644
index 0000000000..008f1a9db9
--- /dev/null
+++ b/src/test/timeoffsets_tests.cpp
@@ -0,0 +1,69 @@
+// Copyright (c) 2024-present The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+//
+
+#include <node/timeoffsets.h>
+#include <test/util/setup_common.h>
+
+#include <boost/test/unit_test.hpp>
+
+#include <chrono>
+#include <vector>
+
+using namespace std::chrono_literals;
+
+static void AddMulti(TimeOffsets& offsets, const std::vector<std::chrono::seconds>& to_add)
+{
+ for (auto offset : to_add) {
+ offsets.Add(offset);
+ }
+}
+
+BOOST_FIXTURE_TEST_SUITE(timeoffsets_tests, BasicTestingSetup)
+
+BOOST_AUTO_TEST_CASE(timeoffsets)
+{
+ TimeOffsets offsets{};
+ BOOST_CHECK(offsets.Median() == 0s);
+
+ AddMulti(offsets, {{0s, -1s, -2s, -3s}});
+ // median should be zero for < 5 offsets
+ BOOST_CHECK(offsets.Median() == 0s);
+
+ offsets.Add(-4s);
+ // we now have 5 offsets: [-4, -3, -2, -1, 0]
+ BOOST_CHECK(offsets.Median() == -2s);
+
+ AddMulti(offsets, {4, 5s});
+ // we now have 9 offsets: [-4, -3, -2, -1, 0, 5, 5, 5, 5]
+ BOOST_CHECK(offsets.Median() == 0s);
+
+ AddMulti(offsets, {41, 10s});
+ // the TimeOffsets is now at capacity with 50 offsets, oldest offsets is discarded for any additional offset
+ BOOST_CHECK(offsets.Median() == 10s);
+
+ AddMulti(offsets, {25, 15s});
+ // we now have 25 offsets of 10s followed by 25 offsets of 15s
+ BOOST_CHECK(offsets.Median() == 15s);
+}
+
+static bool IsWarningRaised(const std::vector<std::chrono::seconds>& check_offsets)
+{
+ TimeOffsets offsets{};
+ AddMulti(offsets, check_offsets);
+ return offsets.WarnIfOutOfSync();
+}
+
+
+BOOST_AUTO_TEST_CASE(timeoffsets_warning)
+{
+ BOOST_CHECK(IsWarningRaised({{-60min, -40min, -30min, 0min, 10min}}));
+ BOOST_CHECK(IsWarningRaised({5, 11min}));
+
+ BOOST_CHECK(!IsWarningRaised({4, 60min}));
+ BOOST_CHECK(!IsWarningRaised({100, 3min}));
+}
+
+
+BOOST_AUTO_TEST_SUITE_END()