Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
node
timeoffsets.cpp
Go to the documentation of this file.
1
// Copyright (c) 2024-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <logging.h>
6
#include <
node/timeoffsets.h
>
7
#include <
node/warnings.h
>
8
#include <
sync.h
>
9
#include <
tinyformat.h
>
10
#include <util/time.h>
11
#include <
util/translation.h
>
12
13
#include <algorithm>
14
#include <chrono>
15
#include <cstdint>
16
#include <deque>
17
#include <limits>
18
#include <optional>
19
20
using namespace
std::chrono_literals;
21
22
void
TimeOffsets::Add
(std::chrono::seconds offset)
23
{
24
LOCK
(
m_mutex
);
25
26
if
(m_offsets.size() >=
MAX_SIZE
) {
27
m_offsets.pop_front();
28
}
29
m_offsets.push_back(offset);
30
LogDebug
(
BCLog::NET
,
"Added time offset %+ds, total samples %d\n"
,
31
Ticks<std::chrono::seconds>
(offset), m_offsets.size());
32
}
33
34
std::chrono::seconds
TimeOffsets::Median
()
const
35
{
36
LOCK
(
m_mutex
);
37
38
// Only calculate the median if we have 5 or more offsets
39
if
(m_offsets.size() < 5)
return
0s;
40
41
auto
sorted_copy = m_offsets;
42
std::sort(sorted_copy.begin(), sorted_copy.end());
43
return
sorted_copy[sorted_copy.size() / 2];
// approximate median is good enough, keep it simple
44
}
45
46
bool
TimeOffsets::WarnIfOutOfSync
()
const
47
{
48
// when median == std::numeric_limits<int64_t>::min(), calling std::chrono::abs is UB
49
auto
median{std::max(
Median
(), std::chrono::seconds(std::numeric_limits<int64_t>::min() + 1))};
50
if
(std::chrono::abs(median) <=
WARN_THRESHOLD
) {
51
m_warnings
.Unset(
node::Warning::CLOCK_OUT_OF_SYNC
);
52
return
false
;
53
}
54
55
bilingual_str
msg{
strprintf
(
_
(
56
"Your computer's date and time appear to be more than %d minutes out of sync with the network, "
57
"this may lead to consensus failure. After you've confirmed your computer's clock, this message "
58
"should no longer appear when you restart your node. Without a restart, it should stop showing "
59
"automatically after you've connected to a sufficient number of new outbound peers, which may "
60
"take some time. You can inspect the `timeoffset` field of the `getpeerinfo` and `getnetworkinfo` "
61
"RPC methods to get more info."
62
),
Ticks<std::chrono::minutes>
(
WARN_THRESHOLD
))};
63
LogWarning
(
"%s\n"
, msg.original);
64
m_warnings
.Set(
node::Warning::CLOCK_OUT_OF_SYNC
, msg);
65
return
true
;
66
}
TimeOffsets::Add
void Add(std::chrono::seconds offset) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Add a new time offset sample.
Definition
timeoffsets.cpp:22
TimeOffsets::MAX_SIZE
static constexpr size_t MAX_SIZE
Maximum number of timeoffsets stored.
Definition
timeoffsets.h:25
TimeOffsets::m_warnings
node::Warnings & m_warnings
Definition
timeoffsets.h:34
TimeOffsets::WarnIfOutOfSync
bool WarnIfOutOfSync() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Raise warnings if the median time offset exceeds the warnings threshold.
Definition
timeoffsets.cpp:46
TimeOffsets::m_mutex
Mutex m_mutex
Definition
timeoffsets.h:29
TimeOffsets::Median
std::chrono::seconds Median() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Compute and return the median of the collected time offset samples.
Definition
timeoffsets.cpp:34
TimeOffsets::WARN_THRESHOLD
static constexpr std::chrono::minutes WARN_THRESHOLD
Minimum difference between system and network time for a warning to be raised.
Definition
timeoffsets.h:27
LogWarning
#define LogWarning(...)
Definition
log.h:96
LogDebug
#define LogDebug(category,...)
Definition
log.h:115
BCLog::NET
@ NET
Definition
categories.h:16
node::Warning::CLOCK_OUT_OF_SYNC
@ CLOCK_OUT_OF_SYNC
Definition
warnings.h:24
bilingual_str
Bilingual messages:
Definition
translation.h:24
sync.h
LOCK
#define LOCK(cs)
Definition
sync.h:258
timeoffsets.h
tinyformat.h
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition
tinyformat.h:1172
translation.h
_
consteval auto _(util::TranslatedLiteral str)
Definition
translation.h:79
Ticks
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition
time.h:73
warnings.h
Generated on
for Bitcoin Core by
1.17.0