gui: fix ban from qt console
[bitcoinplatinum.git] / src / timedata.cpp
blob25fc494121d4debbd00f91fb8af6674beb600dca
1 // Copyright (c) 2014-2015 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.
5 #if defined(HAVE_CONFIG_H)
6 #include "config/bitcoin-config.h"
7 #endif
9 #include "timedata.h"
11 #include "netaddress.h"
12 #include "sync.h"
13 #include "ui_interface.h"
14 #include "util.h"
15 #include "utilstrencodings.h"
17 #include <boost/foreach.hpp>
19 using namespace std;
21 static CCriticalSection cs_nTimeOffset;
22 static int64_t nTimeOffset = 0;
24 /**
25 * "Never go to sea with two chronometers; take one or three."
26 * Our three time sources are:
27 * - System clock
28 * - Median of other nodes clocks
29 * - The user (asking the user to fix the system clock if the first two disagree)
31 int64_t GetTimeOffset()
33 LOCK(cs_nTimeOffset);
34 return nTimeOffset;
37 int64_t GetAdjustedTime()
39 return GetTime() + GetTimeOffset();
42 static int64_t abs64(int64_t n)
44 return (n >= 0 ? n : -n);
47 #define BITCOIN_TIMEDATA_MAX_SAMPLES 200
49 void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
51 LOCK(cs_nTimeOffset);
52 // Ignore duplicates
53 static set<CNetAddr> setKnown;
54 if (setKnown.size() == BITCOIN_TIMEDATA_MAX_SAMPLES)
55 return;
56 if (!setKnown.insert(ip).second)
57 return;
59 // Add data
60 static CMedianFilter<int64_t> vTimeOffsets(BITCOIN_TIMEDATA_MAX_SAMPLES, 0);
61 vTimeOffsets.input(nOffsetSample);
62 LogPrint("net","added time data, samples %d, offset %+d (%+d minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60);
64 // There is a known issue here (see issue #4521):
66 // - The structure vTimeOffsets contains up to 200 elements, after which
67 // any new element added to it will not increase its size, replacing the
68 // oldest element.
70 // - The condition to update nTimeOffset includes checking whether the
71 // number of elements in vTimeOffsets is odd, which will never happen after
72 // there are 200 elements.
74 // But in this case the 'bug' is protective against some attacks, and may
75 // actually explain why we've never seen attacks which manipulate the
76 // clock offset.
78 // So we should hold off on fixing this and clean it up as part of
79 // a timing cleanup that strengthens it in a number of other ways.
81 if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1)
83 int64_t nMedian = vTimeOffsets.median();
84 std::vector<int64_t> vSorted = vTimeOffsets.sorted();
85 // Only let other nodes change our time by so much
86 if (abs64(nMedian) <= std::max<int64_t>(0, GetArg("-maxtimeadjustment", DEFAULT_MAX_TIME_ADJUSTMENT)))
88 nTimeOffset = nMedian;
90 else
92 nTimeOffset = 0;
94 static bool fDone;
95 if (!fDone)
97 // If nobody has a time different than ours but within 5 minutes of ours, give a warning
98 bool fMatch = false;
99 BOOST_FOREACH(int64_t nOffset, vSorted)
100 if (nOffset != 0 && abs64(nOffset) < 5 * 60)
101 fMatch = true;
103 if (!fMatch)
105 fDone = true;
106 string strMessage = strprintf(_("Please check that your computer's date and time are correct! If your clock is wrong, %s will not work properly."), _(PACKAGE_NAME));
107 strMiscWarning = strMessage;
108 uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::MSG_WARNING);
113 BOOST_FOREACH(int64_t n, vSorted)
114 LogPrint("net", "%+d ", n);
115 LogPrint("net", "| ");
117 LogPrint("net", "nTimeOffset = %+d (%+d minutes)\n", nTimeOffset, nTimeOffset/60);