Inside Bitcoin
Opening the selected file…
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.5#ifndef BITCOIN_NODE_TIMEOFFSETS_H6#define BITCOIN_NODE_TIMEOFFSETS_H8#include <sync.h>10#include <chrono>11#include <cstddef>12#include <deque>14namespace node {15class Warnings;16} // namespace node18class TimeOffsets19{20public:21TimeOffsets(node::Warnings& warnings) : m_warnings{warnings} {}23private:24//! Maximum number of timeoffsets stored.25static constexpr size_t MAX_SIZE{50};26//! Minimum difference between system and network time for a warning to be raised.27static constexpr std::chrono::minutes WARN_THRESHOLD{10};29mutable Mutex m_mutex;30/** The observed time differences between our local clock and those of our outbound peers. A31* positive offset means our peer's clock is ahead of our local clock. */32std::deque<std::chrono::seconds> m_offsets GUARDED_BY(m_mutex){};34node::Warnings& m_warnings;36public:37/** Add a new time offset sample. */38void Add(std::chrono::seconds offset) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);40/** Compute and return the median of the collected time offset samples. The median is returned41* as 0 when there are less than 5 samples. */42std::chrono::seconds Median() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);44/** Raise warnings if the median time offset exceeds the warnings threshold. Returns true if45* warnings were raised. */46bool WarnIfOutOfSync() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);47};49#endif // BITCOIN_NODE_TIMEOFFSETS_H