Bitcoin ABC  0.29.2
P2P Digital Currency
timedata.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2016 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 #if defined(HAVE_CONFIG_H)
6 #include <config/bitcoin-config.h>
7 #endif
8 
9 #include <timedata.h>
10 
11 #include <netaddress.h>
12 #include <node/ui_interface.h>
13 #include <sync.h>
14 #include <util/system.h>
15 #include <util/translation.h>
16 #include <warnings.h>
17 
19 static int64_t nTimeOffset GUARDED_BY(g_timeoffset_mutex) = 0;
20 
29 int64_t GetTimeOffset() {
31  return nTimeOffset;
32 }
33 
35  return NodeClock::now() + std::chrono::seconds{GetTimeOffset()};
36 }
37 
38 #define BITCOIN_TIMEDATA_MAX_SAMPLES 200
39 
40 static std::set<CNetAddr> g_sources;
42 static bool g_warning_emitted;
43 
44 void AddTimeData(const CNetAddr &ip, int64_t nOffsetSample) {
46  // Ignore duplicates
48  return;
49  }
50  if (!g_sources.insert(ip).second) {
51  return;
52  }
53 
54  // Add data
55  g_time_offsets.input(nOffsetSample);
57  "added time data, samples %d, offset %+d (%+d minutes)\n",
58  g_time_offsets.size(), nOffsetSample, nOffsetSample / 60);
59 
60  // There is a known issue here (see issue #4521):
61  //
62  // - The structure g_time_offsets contains up to 200 elements, after which
63  // any new element added to it will not increase its size, replacing the
64  // oldest element.
65  //
66  // - The condition to update nTimeOffset includes checking whether the
67  // number of elements in g_time_offsets is odd, which will never happen
68  // after there are 200 elements.
69  //
70  // But in this case the 'bug' is protective against some attacks, and may
71  // actually explain why we've never seen attacks which manipulate the clock
72  // offset.
73  //
74  // So we should hold off on fixing this and clean it up as part of a timing
75  // cleanup that strengthens it in a number of other ways.
76  //
77  if (g_time_offsets.size() >= 5 && g_time_offsets.size() % 2 == 1) {
78  int64_t nMedian = g_time_offsets.median();
79  std::vector<int64_t> vSorted = g_time_offsets.sorted();
80  // Only let other nodes change our time by so much
81 
82  int64_t max_adjustment =
83  std::max<int64_t>(0, gArgs.GetIntArg("-maxtimeadjustment",
85  if (nMedian >= -max_adjustment && nMedian <= max_adjustment) {
86  nTimeOffset = nMedian;
87  } else {
88  nTimeOffset = 0;
89 
90  if (!g_warning_emitted) {
91  // If nobody has a time different than ours but within 5 minutes
92  // of ours, give a warning
93  bool fMatch = false;
94  for (const int64_t nOffset : vSorted) {
95  if (nOffset != 0 && nOffset > -5 * 60 && nOffset < 5 * 60) {
96  fMatch = true;
97  }
98  }
99 
100  if (!fMatch) {
101  g_warning_emitted = true;
102  bilingual_str strMessage =
103  strprintf(_("Please check that your computer's date "
104  "and time are correct! If your clock is "
105  "wrong, %s will not work properly."),
106  PACKAGE_NAME);
107  SetMiscWarning(strMessage);
108  uiInterface.ThreadSafeMessageBox(
109  strMessage, "", CClientUIInterface::MSG_WARNING);
110  }
111  }
112  }
113 
115  for (const int64_t n : vSorted) {
116  LogPrintToBeContinued(BCLog::NET, "%+d ", n);
117  }
118 
120  LogPrint(BCLog::NET, "nTimeOffset = %+d (%+d minutes)\n",
121  nTimeOffset, nTimeOffset / 60);
122  }
123  }
124 }
125 
128  nTimeOffset = 0;
129  g_sources.clear();
131  g_warning_emitted = false;
132 }
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: system.cpp:635
Median filter over a stream of values.
Definition: timedata.h:24
Network address.
Definition: netaddress.h:121
Different type to mark Mutex at global scope.
Definition: sync.h:144
static bool LogAcceptCategory(BCLog::LogFlags category)
Return true if log accepts specified category.
Definition: logging.h:176
#define LogPrint(category,...)
Definition: logging.h:210
#define LogPrintToBeContinued
Definition: logging.h:223
@ NET
Definition: logging.h:40
static time_point now() noexcept
Return current system time or mocked time, if set.
Definition: time.cpp:71
std::chrono::time_point< NodeClock > time_point
Definition: time.h:19
Bilingual messages:
Definition: translation.h:17
#define LOCK(cs)
Definition: sync.h:306
ArgsManager gArgs
Definition: system.cpp:80
int64_t GetTimeOffset()
"Never go to sea with two chronometers; take one or three." Our three time sources are:
Definition: timedata.cpp:29
static CMedianFilter< int64_t > g_time_offsets(BITCOIN_TIMEDATA_MAX_SAMPLES, 0)
void TestOnlyResetTimeData()
Reset the internal state of GetTimeOffset(), GetAdjustedTime() and AddTimeData().
Definition: timedata.cpp:126
static GlobalMutex g_timeoffset_mutex
Definition: timedata.cpp:18
#define BITCOIN_TIMEDATA_MAX_SAMPLES
Definition: timedata.cpp:38
NodeClock::time_point GetAdjustedTime()
Definition: timedata.cpp:34
static std::set< CNetAddr > g_sources
Definition: timedata.cpp:40
void AddTimeData(const CNetAddr &ip, int64_t nOffsetSample)
Definition: timedata.cpp:44
static int64_t nTimeOffset GUARDED_BY(g_timeoffset_mutex)=0
static bool g_warning_emitted
Definition: timedata.cpp:42
static const int64_t DEFAULT_MAX_TIME_ADJUSTMENT
Definition: timedata.h:16
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:68
CClientUIInterface uiInterface
void SetMiscWarning(const bilingual_str &warning)
Definition: warnings.cpp:21