2017-11-09 19:34:51 +01:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
|
|
|
|
* Copyright (C) 2014-2015 The Bitcoin Core developers
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2014-06-19 15:08:37 +02:00
|
|
|
|
|
|
|
|
#include "timedata.h"
|
|
|
|
|
|
2019-08-24 13:20:37 +02:00
|
|
|
#include <netbase.h>
|
|
|
|
|
#include <sync.h>
|
2018-02-10 22:48:07 +01:00
|
|
|
#include "UiInterface.h"
|
2019-08-24 13:20:37 +02:00
|
|
|
#include "serverutil.h"
|
|
|
|
|
#include <utilstrencodings.h>
|
2026-05-14 13:13:40 +02:00
|
|
|
#include <utiltime.h>
|
2014-06-19 15:08:37 +02:00
|
|
|
|
|
|
|
|
static CCriticalSection cs_nTimeOffset;
|
|
|
|
|
static int64_t nTimeOffset = 0;
|
|
|
|
|
|
2014-10-31 08:43:19 +08:00
|
|
|
/**
|
|
|
|
|
* "Never go to sea with two chronometers; take one or three."
|
|
|
|
|
* Our three time sources are:
|
|
|
|
|
* - System clock
|
|
|
|
|
* - Median of other nodes clocks
|
|
|
|
|
* - The user (asking the user to fix the system clock if the first two disagree)
|
|
|
|
|
*/
|
2014-06-19 15:08:37 +02:00
|
|
|
int64_t GetTimeOffset()
|
|
|
|
|
{
|
|
|
|
|
LOCK(cs_nTimeOffset);
|
|
|
|
|
return nTimeOffset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int64_t GetAdjustedTime()
|
|
|
|
|
{
|
|
|
|
|
return GetTime() + GetTimeOffset();
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-21 16:11:09 +02:00
|
|
|
static int64_t abs64(int64_t n)
|
|
|
|
|
{
|
|
|
|
|
return (n >= 0 ? n : -n);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-11 15:57:52 +02:00
|
|
|
#define BITCOIN_TIMEDATA_MAX_SAMPLES 200
|
|
|
|
|
|
2014-12-15 11:06:15 +01:00
|
|
|
void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
|
2014-06-19 15:08:37 +02:00
|
|
|
{
|
|
|
|
|
LOCK(cs_nTimeOffset);
|
|
|
|
|
// Ignore duplicates
|
2017-08-04 19:02:56 +05:30
|
|
|
static std::set<CNetAddr> setKnown;
|
2015-08-11 15:57:52 +02:00
|
|
|
if (setKnown.size() == BITCOIN_TIMEDATA_MAX_SAMPLES)
|
|
|
|
|
return;
|
2014-06-19 15:08:37 +02:00
|
|
|
if (!setKnown.insert(ip).second)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Add data
|
2015-08-11 15:57:52 +02:00
|
|
|
static CMedianFilter<int64_t> vTimeOffsets(BITCOIN_TIMEDATA_MAX_SAMPLES, 0);
|
2014-06-19 15:08:37 +02:00
|
|
|
vTimeOffsets.input(nOffsetSample);
|
2017-07-23 22:16:30 +02:00
|
|
|
logDebug(Log::Net) << "added time data, samples" << vTimeOffsets.size() << "offset" << nOffsetSample << "minutes:" << nOffsetSample / 60;
|
2014-07-24 19:00:24 +02:00
|
|
|
|
|
|
|
|
// There is a known issue here (see issue #4521):
|
|
|
|
|
//
|
|
|
|
|
// - The structure vTimeOffsets contains up to 200 elements, after which
|
|
|
|
|
// any new element added to it will not increase its size, replacing the
|
|
|
|
|
// oldest element.
|
|
|
|
|
//
|
|
|
|
|
// - The condition to update nTimeOffset includes checking whether the
|
|
|
|
|
// number of elements in vTimeOffsets is odd, which will never happen after
|
|
|
|
|
// there are 200 elements.
|
|
|
|
|
//
|
|
|
|
|
// But in this case the 'bug' is protective against some attacks, and may
|
|
|
|
|
// actually explain why we've never seen attacks which manipulate the
|
|
|
|
|
// clock offset.
|
|
|
|
|
//
|
|
|
|
|
// So we should hold off on fixing this and clean it up as part of
|
|
|
|
|
// a timing cleanup that strengthens it in a number of other ways.
|
|
|
|
|
//
|
2014-06-19 15:08:37 +02:00
|
|
|
if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1)
|
|
|
|
|
{
|
|
|
|
|
int64_t nMedian = vTimeOffsets.median();
|
|
|
|
|
std::vector<int64_t> vSorted = vTimeOffsets.sorted();
|
|
|
|
|
// Only let other nodes change our time by so much
|
|
|
|
|
if (abs64(nMedian) < 70 * 60)
|
|
|
|
|
{
|
|
|
|
|
nTimeOffset = nMedian;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
nTimeOffset = 0;
|
|
|
|
|
|
|
|
|
|
static bool fDone;
|
|
|
|
|
if (!fDone)
|
|
|
|
|
{
|
|
|
|
|
// If nobody has a time different than ours but within 5 minutes of ours, give a warning
|
|
|
|
|
bool fMatch = false;
|
2018-12-30 15:33:11 +01:00
|
|
|
for (int64_t nOffset : vSorted)
|
2014-06-19 15:08:37 +02:00
|
|
|
if (nOffset != 0 && abs64(nOffset) < 5 * 60)
|
|
|
|
|
fMatch = true;
|
|
|
|
|
|
|
|
|
|
if (!fMatch)
|
|
|
|
|
{
|
|
|
|
|
fDone = true;
|
2017-11-13 23:09:33 +01:00
|
|
|
std::string strMessage = _("Please check that your computer's date and time are correct! If your clock is wrong Flowee will not work properly.");
|
2014-06-19 15:08:37 +02:00
|
|
|
strMiscWarning = strMessage;
|
|
|
|
|
uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::MSG_WARNING);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-13 23:09:33 +01:00
|
|
|
|
2017-07-23 22:16:30 +02:00
|
|
|
logDebug(Log::Net) << "TimeData:" << vSorted;
|
|
|
|
|
logDebug(Log::Net) << "nTimeOffset" << nTimeOffset << "minutes:" << nTimeOffset / 60;
|
2014-06-19 15:08:37 +02:00
|
|
|
}
|
|
|
|
|
}
|