Timedata changes from KMD 0.6
This commit is contained in:
149
src/timedata.cpp
149
src/timedata.cpp
@@ -1,10 +1,11 @@
|
|||||||
// Copyright (c) 2014 The Bitcoin Core developers
|
// Copyright (c) 2014 The Bitcoin Core developers
|
||||||
// Copyright (c) 2019-2020 The Hush developers
|
// Copyright (c) 2019-2020 The Hush developers
|
||||||
|
// Copyright (c) 2020 The Zcash developers
|
||||||
// Distributed under the MIT software license, see the accompanying
|
// Distributed under the MIT software license, see the accompanying
|
||||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
// file COPYING or https://www.opensource.org/licenses/mit-license.php
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Copyright © 2014-2019 The SuperNET Developers. *
|
* Copyright © 2014-2020 The SuperNET Developers. *
|
||||||
* *
|
* *
|
||||||
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
|
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
|
||||||
* the top-level directory of this distribution for the individual copyright *
|
* the top-level directory of this distribution for the individual copyright *
|
||||||
@@ -26,114 +27,56 @@
|
|||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "utilstrencodings.h"
|
#include "utilstrencodings.h"
|
||||||
|
|
||||||
#include <boost/foreach.hpp>
|
CTimeWarning timeWarning;
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
static CCriticalSection cs_nTimeOffset;
|
|
||||||
static int64_t nTimeOffset = 0;
|
|
||||||
#define KOMODO_ASSETCHAIN_MAXLEN 65
|
|
||||||
extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* "Never go to sea with two chronometers; take one or three."
|
* Warn if we have seen TIMEDATA_WARNING_SAMPLES peer times, in the version messages of the
|
||||||
* Our three time sources are:
|
* first TIMEDATA_MAX_SAMPLES unique (by IP address) peers that connect, that are more than
|
||||||
* - System clock
|
* TIMEDATA_WARNING_THRESHOLD seconds but less than TIMEDATA_IGNORE_THRESHOLD seconds away
|
||||||
* - Median of other nodes clocks
|
* from local time.
|
||||||
* - The user (asking the user to fix the system clock if the first two disagree)
|
|
||||||
*/
|
*/
|
||||||
int64_t GetTimeOffset()
|
|
||||||
|
int64_t CTimeWarning::AddTimeData(const CNetAddr& ip, int64_t nTime, int64_t now)
|
||||||
{
|
{
|
||||||
LOCK(cs_nTimeOffset);
|
assert(now >= 0 && now <= INT64_MAX - TIMEDATA_IGNORE_THRESHOLD);
|
||||||
|
|
||||||
|
if (nTime <= now - TIMEDATA_IGNORE_THRESHOLD || nTime >= now + TIMEDATA_IGNORE_THRESHOLD) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t nTimeOffset = nTime - now;
|
||||||
|
|
||||||
|
LOCK(cs);
|
||||||
|
// Ignore duplicate IPs.
|
||||||
|
if (setKnown.size() == TIMEDATA_MAX_SAMPLES || !setKnown.insert(ip).second) {
|
||||||
|
return nTimeOffset;
|
||||||
|
}
|
||||||
|
|
||||||
|
LogPrintf("Added time data, samples %d, offset %+d (%+d minutes)\n", setKnown.size(), nTimeOffset, nTimeOffset/60);
|
||||||
|
|
||||||
|
if (nPeersBehind + nPeersAhead < TIMEDATA_WARNING_SAMPLES) {
|
||||||
|
if (nTimeOffset < -TIMEDATA_WARNING_THRESHOLD) {
|
||||||
|
nPeersBehind++;
|
||||||
|
} else if (nTimeOffset > TIMEDATA_WARNING_THRESHOLD) {
|
||||||
|
nPeersAhead++;
|
||||||
|
}
|
||||||
|
if (nPeersBehind + nPeersAhead == TIMEDATA_WARNING_SAMPLES) {
|
||||||
|
Warn(nPeersAhead, nPeersBehind);
|
||||||
|
}
|
||||||
|
}
|
||||||
return nTimeOffset;
|
return nTimeOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t GetAdjustedTime()
|
void CTimeWarning::Warn(size_t peersAhead, size_t peersBehind)
|
||||||
{
|
{
|
||||||
return GetTime() + GetTimeOffset();
|
std::string strMessage;
|
||||||
}
|
if (peersBehind >= TIMEDATA_WARNING_MAJORITY) {
|
||||||
|
strMessage = _("Warning: Your computer's date and time may be ahead of the rest of the network! If your clock is wrong Hush will not work properly.");
|
||||||
static int64_t abs64(int64_t n)
|
} else if (peersAhead >= TIMEDATA_WARNING_MAJORITY) {
|
||||||
{
|
strMessage = _("Warning: Your computer's date and time may be behind the rest of the network! If your clock is wrong Hush will not work properly.");
|
||||||
return (n >= 0 ? n : -n);
|
} else {
|
||||||
}
|
strMessage = _("Warning: Please check that your computer's date and time are correct! If your clock is wrong Hush will not work properly.");
|
||||||
|
|
||||||
#define BITCOIN_TIMEDATA_MAX_SAMPLES 200
|
|
||||||
|
|
||||||
void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
|
|
||||||
{
|
|
||||||
LOCK(cs_nTimeOffset);
|
|
||||||
// Ignore duplicates
|
|
||||||
static set<CNetAddr> setKnown;
|
|
||||||
if (setKnown.size() == BITCOIN_TIMEDATA_MAX_SAMPLES)
|
|
||||||
return;
|
|
||||||
if (!setKnown.insert(ip).second)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Add data
|
|
||||||
static CMedianFilter<int64_t> vTimeOffsets(BITCOIN_TIMEDATA_MAX_SAMPLES, 0);
|
|
||||||
vTimeOffsets.input(nOffsetSample);
|
|
||||||
LogPrintf("Added time data, samples %d, offset %+d (%+d minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60);
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
//
|
|
||||||
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) < 30) // thanks to zawy for pointing this out!! zcash issues 4021 //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;
|
|
||||||
BOOST_FOREACH(int64_t nOffset, vSorted)
|
|
||||||
if (nOffset != 0 && abs64(nOffset) < 5 * 60)
|
|
||||||
fMatch = true;
|
|
||||||
|
|
||||||
if (!fMatch)
|
|
||||||
{
|
|
||||||
fDone = true;
|
|
||||||
string strMessage;
|
|
||||||
if( strncmp(ASSETCHAINS_SYMBOL, "HUSH3",5) == 0 ) {
|
|
||||||
strMessage = _("Warning: Please check that your computer's date and time are correct! If your clock is wrong Hush will not work properly.");
|
|
||||||
} else {
|
|
||||||
strMessage = _("Warning: Please check that your computer's date and time are correct! If your clock is wrong Komodo will not work properly.");
|
|
||||||
}
|
|
||||||
strMiscWarning = strMessage;
|
|
||||||
LogPrintf("*** %s\n", strMessage);
|
|
||||||
uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::MSG_WARNING);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (fDebug) {
|
|
||||||
BOOST_FOREACH(int64_t n, vSorted)
|
|
||||||
LogPrintf("%+d ", n);
|
|
||||||
LogPrintf("| ");
|
|
||||||
}
|
|
||||||
LogPrintf("nTimeOffset = %+d (%+d minutes)\n", nTimeOffset, nTimeOffset/60);
|
|
||||||
}
|
}
|
||||||
|
LogPrintf("*** %s\n", strMessage);
|
||||||
|
uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::MSG_WARNING);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,77 +1,39 @@
|
|||||||
// Copyright (c) 2014 The Bitcoin Core developers
|
// Copyright (c) 2014 The Bitcoin Core developers
|
||||||
// Copyright (c) 2019-2020 The Hush developers
|
// Copyright (c) 2019-2020 The Hush developers
|
||||||
|
// Copyright (c) 2020 The Zcash developers
|
||||||
// Distributed under the MIT software license, see the accompanying
|
// Distributed under the MIT software license, see the accompanying
|
||||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
// file COPYING or https://www.opensource.org/licenses/mit-license.php
|
||||||
|
|
||||||
#ifndef BITCOIN_TIMEDATA_H
|
#ifndef BITCOIN_TIMEDATA_H
|
||||||
#define BITCOIN_TIMEDATA_H
|
#define BITCOIN_TIMEDATA_H
|
||||||
|
|
||||||
#include <algorithm>
|
#include <set>
|
||||||
#include <assert.h>
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <vector>
|
#include "netbase.h"
|
||||||
|
#include "sync.h"
|
||||||
|
|
||||||
class CNetAddr;
|
class CTimeWarning
|
||||||
|
|
||||||
/**
|
|
||||||
* Median filter over a stream of values.
|
|
||||||
* Returns the median of the last N numbers
|
|
||||||
*/
|
|
||||||
template <typename T>
|
|
||||||
class CMedianFilter
|
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::vector<T> vValues;
|
CCriticalSection cs;
|
||||||
std::vector<T> vSorted;
|
std::set<CNetAddr> setKnown;
|
||||||
unsigned int nSize;
|
size_t nPeersAhead;
|
||||||
|
size_t nPeersBehind;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CMedianFilter(unsigned int size, T initial_value) : nSize(size)
|
static const size_t TIMEDATA_WARNING_SAMPLES = 8;
|
||||||
{
|
static const size_t TIMEDATA_WARNING_MAJORITY = 6;
|
||||||
vValues.reserve(size);
|
static const size_t TIMEDATA_MAX_SAMPLES = 20;
|
||||||
vValues.push_back(initial_value);
|
static const int64_t TIMEDATA_WARNING_THRESHOLD = 10 * 60;
|
||||||
vSorted = vValues;
|
static const int64_t TIMEDATA_IGNORE_THRESHOLD = 10 * 24 * 60 * 60;
|
||||||
}
|
|
||||||
|
|
||||||
void input(T value)
|
CTimeWarning() : nPeersBehind(0), nPeersAhead(0) {}
|
||||||
{
|
virtual ~CTimeWarning() {}
|
||||||
if (vValues.size() == nSize) {
|
|
||||||
vValues.erase(vValues.begin());
|
|
||||||
}
|
|
||||||
vValues.push_back(value);
|
|
||||||
|
|
||||||
vSorted.resize(vValues.size());
|
int64_t AddTimeData(const CNetAddr& ip, int64_t nTime, int64_t now);
|
||||||
std::copy(vValues.begin(), vValues.end(), vSorted.begin());
|
virtual void Warn(size_t peersAhead, size_t peersBehind);
|
||||||
std::sort(vSorted.begin(), vSorted.end());
|
|
||||||
}
|
|
||||||
|
|
||||||
T median() const
|
|
||||||
{
|
|
||||||
int size = vSorted.size();
|
|
||||||
assert(size > 0);
|
|
||||||
if (size & 1) // Odd number of elements
|
|
||||||
{
|
|
||||||
return vSorted[size / 2];
|
|
||||||
} else // Even number of elements
|
|
||||||
{
|
|
||||||
return (vSorted[size / 2 - 1] + vSorted[size / 2]) / 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int size() const
|
|
||||||
{
|
|
||||||
return vValues.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<T> sorted() const
|
|
||||||
{
|
|
||||||
return vSorted;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Functions to keep track of adjusted P2P time */
|
extern CTimeWarning timeWarning;
|
||||||
int64_t GetTimeOffset();
|
|
||||||
int64_t GetAdjustedTime();
|
|
||||||
void AddTimeData(const CNetAddr& ip, int64_t nTime);
|
|
||||||
|
|
||||||
#endif // BITCOIN_TIMEDATA_H
|
#endif // BITCOIN_TIMEDATA_H
|
||||||
|
|||||||
Reference in New Issue
Block a user