Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2024-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.4//6#include <node/timeoffsets.h>7#include <node/warnings.h>8#include <test/util/setup_common.h>10#include <boost/test/unit_test.hpp>12#include <chrono>13#include <vector>15using namespace std::chrono_literals;17static void AddMulti(TimeOffsets& offsets, const std::vector<std::chrono::seconds>& to_add)18{19for (auto offset : to_add) {20offsets.Add(offset);21}22}24BOOST_FIXTURE_TEST_SUITE(timeoffsets_tests, BasicTestingSetup)26BOOST_AUTO_TEST_CASE(timeoffsets)27{28node::Warnings warnings{};29TimeOffsets offsets{warnings};30BOOST_CHECK(offsets.Median() == 0s);32AddMulti(offsets, {{0s, -1s, -2s, -3s}});33// median should be zero for < 5 offsets34BOOST_CHECK(offsets.Median() == 0s);36offsets.Add(-4s);37// we now have 5 offsets: [-4, -3, -2, -1, 0]38BOOST_CHECK(offsets.Median() == -2s);40AddMulti(offsets, {4, 5s});41// we now have 9 offsets: [-4, -3, -2, -1, 0, 5, 5, 5, 5]42BOOST_CHECK(offsets.Median() == 0s);44AddMulti(offsets, {41, 10s});45// the TimeOffsets is now at capacity with 50 offsets, oldest offsets is discarded for any additional offset46BOOST_CHECK(offsets.Median() == 10s);48AddMulti(offsets, {25, 15s});49// we now have 25 offsets of 10s followed by 25 offsets of 15s50BOOST_CHECK(offsets.Median() == 15s);51}53static bool IsWarningRaised(const std::vector<std::chrono::seconds>& check_offsets)54{55node::Warnings warnings{};56TimeOffsets offsets{warnings};57AddMulti(offsets, check_offsets);58return offsets.WarnIfOutOfSync();59}62BOOST_AUTO_TEST_CASE(timeoffsets_warning)63{64BOOST_CHECK(IsWarningRaised({{-60min, -40min, -30min, 0min, 10min}}));65BOOST_CHECK(IsWarningRaised({5, 11min}));67BOOST_CHECK(!IsWarningRaised({4, 60min}));68BOOST_CHECK(!IsWarningRaised({100, 3min}));69}72BOOST_AUTO_TEST_SUITE_END()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.