2014-12-16 17:47:57 -08:00
|
|
|
// Copyright (c) 2014 The Bitcoin Core developers
|
2020-02-19 16:09:38 -08:00
|
|
|
// Copyright (c) 2020 The Zcash developers
|
2014-10-30 17:43:19 -07:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2019-07-18 07:16:09 -07:00
|
|
|
// file COPYING or https://www.opensource.org/licenses/mit-license.php .
|
2014-06-19 06:08:37 -07:00
|
|
|
|
|
|
|
#include "timedata.h"
|
|
|
|
|
|
|
|
#include "netbase.h"
|
|
|
|
#include "sync.h"
|
|
|
|
#include "ui_interface.h"
|
|
|
|
#include "util.h"
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 07:11:09 -07:00
|
|
|
#include "utilstrencodings.h"
|
2016-11-29 22:07:42 -08:00
|
|
|
#include "warnings.h"
|
2014-06-19 06:08:37 -07:00
|
|
|
|
|
|
|
|
2020-02-19 16:09:38 -08:00
|
|
|
CTimeWarning timeWarning;
|
2014-06-19 06:08:37 -07:00
|
|
|
|
2014-10-30 17:43:19 -07:00
|
|
|
/**
|
2020-02-19 16:09:38 -08:00
|
|
|
* Warn if we have seen TIMEDATA_WARNING_SAMPLES peer times, in the version messages of the
|
|
|
|
* first TIMEDATA_MAX_SAMPLES unique (by IP address) peers that connect, that are more than
|
|
|
|
* TIMEDATA_WARNING_THRESHOLD seconds but less than TIMEDATA_IGNORE_THRESHOLD seconds away
|
|
|
|
* from local time.
|
2014-10-30 17:43:19 -07:00
|
|
|
*/
|
2014-06-19 06:08:37 -07:00
|
|
|
|
2020-02-19 16:09:38 -08:00
|
|
|
int64_t CTimeWarning::AddTimeData(const CNetAddr& ip, int64_t nTime, int64_t now)
|
2014-06-19 06:08:37 -07:00
|
|
|
{
|
2020-02-19 16:09:38 -08:00
|
|
|
assert(now >= 0 && now <= INT64_MAX - TIMEDATA_IGNORE_THRESHOLD);
|
2014-06-19 06:08:37 -07:00
|
|
|
|
2020-02-19 16:09:38 -08:00
|
|
|
if (nTime <= now - TIMEDATA_IGNORE_THRESHOLD || nTime >= now + TIMEDATA_IGNORE_THRESHOLD) {
|
|
|
|
return 0;
|
|
|
|
}
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 07:11:09 -07:00
|
|
|
|
2020-02-19 16:09:38 -08:00
|
|
|
int64_t nTimeOffset = nTime - now;
|
2015-08-11 06:57:52 -07:00
|
|
|
|
2020-02-19 16:09:38 -08:00
|
|
|
LOCK(cs);
|
|
|
|
// Ignore duplicate IPs.
|
|
|
|
if (setKnown.size() == TIMEDATA_MAX_SAMPLES || !setKnown.insert(ip).second) {
|
|
|
|
return nTimeOffset;
|
|
|
|
}
|
2014-06-19 06:08:37 -07:00
|
|
|
|
2015-11-21 22:48:25 -08:00
|
|
|
LogPrint("net","added time data, samples %d, offset %+d (%+d minutes)\n", setKnown.size(), nTimeOffset, nTimeOffset/60);
|
2014-07-24 10:00:24 -07:00
|
|
|
|
2020-02-19 16:09:38 -08:00
|
|
|
if (nPeersBehind + nPeersAhead < TIMEDATA_WARNING_SAMPLES) {
|
|
|
|
if (nTimeOffset < -TIMEDATA_WARNING_THRESHOLD) {
|
|
|
|
nPeersBehind++;
|
|
|
|
} else if (nTimeOffset > TIMEDATA_WARNING_THRESHOLD) {
|
|
|
|
nPeersAhead++;
|
2014-06-19 06:08:37 -07:00
|
|
|
}
|
|
|
|
|
2020-02-19 16:09:38 -08:00
|
|
|
if (nPeersBehind + nPeersAhead == TIMEDATA_WARNING_SAMPLES) {
|
|
|
|
Warn(nPeersAhead, nPeersBehind);
|
2014-06-19 06:08:37 -07:00
|
|
|
}
|
|
|
|
}
|
2020-02-19 16:09:38 -08:00
|
|
|
return nTimeOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTimeWarning::Warn(size_t peersAhead, size_t peersBehind)
|
|
|
|
{
|
|
|
|
std::string strMessage;
|
|
|
|
if (peersBehind >= TIMEDATA_WARNING_MAJORITY) {
|
2015-09-27 07:40:16 -07:00
|
|
|
strMessage = _("Your computer's date and time may be ahead of the rest of the network! If your clock is wrong Zcash will not work properly.");
|
2020-02-19 16:09:38 -08:00
|
|
|
} else if (peersAhead >= TIMEDATA_WARNING_MAJORITY) {
|
2015-09-27 07:40:16 -07:00
|
|
|
strMessage = _("Your computer's date and time may be behind the rest of the network! If your clock is wrong Zcash will not work properly.");
|
2020-02-19 16:09:38 -08:00
|
|
|
} else {
|
2015-09-27 07:40:16 -07:00
|
|
|
strMessage = _("Please check that your computer's date and time are correct! If your clock is wrong Zcash will not work properly.");
|
2020-02-19 16:09:38 -08:00
|
|
|
}
|
2020-06-12 14:08:00 -07:00
|
|
|
SetMiscWarning(strMessage, GetTime());
|
2020-02-19 16:09:38 -08:00
|
|
|
uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::MSG_WARNING);
|
2014-06-19 06:08:37 -07:00
|
|
|
}
|
2020-02-19 16:09:38 -08:00
|
|
|
|