Bitcoin ABC  0.22.13
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(cs_nTimeOffset) = 0;
20 
29 int64_t GetTimeOffset() {
30  LOCK(cs_nTimeOffset);
31  return nTimeOffset;
32 }
33 
34 int64_t GetAdjustedTime() {
35  return GetTime() + GetTimeOffset();
36 }
37 
38 static uint64_t abs64(int64_t n) {
39  const uint64_t un = n;
40  return (n >= 0) ? un : -un;
41 }
42 
43 #define BITCOIN_TIMEDATA_MAX_SAMPLES 200
44 
45 void AddTimeData(const CNetAddr &ip, int64_t nOffsetSample) {
46  LOCK(cs_nTimeOffset);
47  // Ignore duplicates
48  static std::set<CNetAddr> setKnown;
49  if (setKnown.size() == BITCOIN_TIMEDATA_MAX_SAMPLES) {
50  return;
51  }
52  if (!setKnown.insert(ip).second) {
53  return;
54  }
55 
56  // Add data
58  vTimeOffsets.input(nOffsetSample);
60  "added time data, samples %d, offset %+d (%+d minutes)\n",
61  vTimeOffsets.size(), nOffsetSample, nOffsetSample / 60);
62 
63  // There is a known issue here (see issue #4521):
64  //
65  // - The structure vTimeOffsets contains up to 200 elements, after which any
66  // new element added to it will not increase its size, replacing the oldest
67  // element.
68  //
69  // - The condition to update nTimeOffset includes checking whether the
70  // number of elements in vTimeOffsets is odd, which will never happen after
71  // there are 200 elements.
72  //
73  // But in this case the 'bug' is protective against some attacks, and may
74  // actually explain why we've never seen attacks which manipulate the clock
75  // offset.
76  //
77  // So we should hold off on fixing this and clean it up as part of a timing
78  // cleanup that strengthens it in a number of other ways.
79  //
80  if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1) {
81  int64_t nMedian = vTimeOffsets.median();
82  std::vector<int64_t> vSorted = vTimeOffsets.sorted();
83  // Only let other nodes change our time by so much
84  if (abs64(nMedian) <=
85  uint64_t(std::max<int64_t>(
86  0, gArgs.GetArg("-maxtimeadjustment",
88  nTimeOffset = nMedian;
89  } else {
90  nTimeOffset = 0;
91 
92  static bool fDone;
93  if (!fDone) {
94  // If nobody has a time different than ours but within 5 minutes
95  // of ours, give a warning
96  bool fMatch = false;
97  for (const int64_t nOffset : vSorted) {
98  if (nOffset != 0 && abs64(nOffset) < 5 * 60) {
99  fMatch = true;
100  }
101  }
102 
103  if (!fMatch) {
104  fDone = true;
105  bilingual_str strMessage =
106  strprintf(_("Please check that your computer's date "
107  "and time are correct! If your clock is "
108  "wrong, %s will not work properly."),
109  PACKAGE_NAME);
110  SetMiscWarning(strMessage.translated);
111  uiInterface.ThreadSafeMessageBox(
112  strMessage, "", CClientUIInterface::MSG_WARNING);
113  }
114  }
115  }
116 
118  for (const int64_t n : vSorted) {
119  LogPrintToBeContinued(BCLog::NET, "%+d ", n);
120  }
121 
123  LogPrint(BCLog::NET, "nTimeOffset = %+d (%+d minutes)\n",
124  nTimeOffset, nTimeOffset / 60);
125  }
126  }
127 }
#define LogPrint(category,...)
Definition: logging.h:192
#define LogPrintToBeContinued
Definition: logging.h:205
Bilingual messages:
Definition: translation.h:17
#define BITCOIN_TIMEDATA_MAX_SAMPLES
Definition: timedata.cpp:43
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
Median filter over a stream of values.
Definition: timedata.h:21
std::string translated
Definition: translation.h:19
int64_t GetTimeOffset()
"Never go to sea with two chronometers; take one or three." Our three time sources are: ...
Definition: timedata.cpp:29
std::vector< T > sorted() const
Definition: timedata.h:61
static RecursiveMutex cs_nTimeOffset
Definition: timedata.cpp:18
static int64_t nTimeOffset GUARDED_BY(cs_nTimeOffset)=0
void input(T value)
Definition: timedata.h:34
#define LOCK(cs)
Definition: sync.h:230
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:55
static bool LogAcceptCategory(BCLog::LogFlags category)
Return true if log accepts specified category.
Definition: logging.h:163
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Definition: netaddress.h:30
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:479
int64_t GetAdjustedTime()
Definition: timedata.cpp:34
void SetMiscWarning(const std::string &strWarning)
Definition: warnings.cpp:18
ArgsManager gArgs
Definition: system.cpp:76
CClientUIInterface uiInterface
void AddTimeData(const CNetAddr &ip, int64_t nOffsetSample)
Definition: timedata.cpp:45
int64_t GetTime()
Return system time (or mocked time, if set)
Definition: time.cpp:27
int size() const
Definition: timedata.h:59
static const int64_t DEFAULT_MAX_TIME_ADJUSTMENT
Definition: timedata.h:13
T median() const
Definition: timedata.h:45
static uint64_t abs64(int64_t n)
Definition: timedata.cpp:38