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) The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_TEST_UTIL_TIME_H6#define BITCOIN_TEST_UTIL_TIME_H8#include <util/check.h>9#include <util/time.h>11/// CRTP Helper to limit a class to at most one at a time.12template <class T>13class LimitOne14{15public:16LimitOne() { Assert(g_T_available) = false; }17~LimitOne() { g_T_available = true; }18LimitOne(const LimitOne&) = delete;19LimitOne& operator=(const LimitOne&) = delete;21private:22static inline bool g_T_available{true};23};26/// Helper to initialize the global MockableSteadyClock, let a duration elapse,27/// and reset it after use in a test.28class FakeSteadyClock : public LimitOne<FakeSteadyClock>29{30MockableSteadyClock::mock_time_point::duration t{MockableSteadyClock::INITIAL_MOCK_TIME};32public:33/** Initialize with INITIAL_MOCK_TIME. */34explicit FakeSteadyClock() { (*this) += 0s; }36/** Unset mocktime */37~FakeSteadyClock() { MockableSteadyClock::ClearMockTime(); }39FakeSteadyClock(const FakeSteadyClock&) = delete;40FakeSteadyClock& operator=(const FakeSteadyClock&) = delete;42/** Change mocktime by the given duration delta */43void operator+=(std::chrono::milliseconds d)44{45Assert(d >= 0s); // Steady time can only increase monotonically.46t += d;47MockableSteadyClock::SetMockTime(t);48}49};51/// Helper to initialize the global NodeClock, let a duration elapse,52/// and reset it after use in a test.53class FakeNodeClock : public LimitOne<FakeNodeClock>54{55NodeSeconds m_t{std::chrono::seconds::max()};57public:58/// Initialize with the given time.59explicit FakeNodeClock(NodeSeconds init_time) { set(init_time); }60explicit FakeNodeClock(std::chrono::seconds init_time) { set(init_time); }61/// Initialize with current time.62explicit FakeNodeClock() { set(Now<NodeSeconds>()); }64/// Unset mocktime.65~FakeNodeClock() { set(0s); }67FakeNodeClock(const FakeNodeClock&) = delete;68FakeNodeClock& operator=(const FakeNodeClock&) = delete;70/// Set mocktime.71void set(NodeSeconds t) { SetMockTime(m_t = t); }72void set(std::chrono::seconds t) { set(NodeSeconds{t}); }74/// Change mocktime by the given duration delta.75void operator+=(std::chrono::seconds d) { set(m_t += d); }76void operator-=(std::chrono::seconds d) { set(m_t -= d); }77};79#endif // BITCOIN_TEST_UTIL_TIME_H
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.