zcashd/src/timedata.cpp

71 lines
2.5 KiB
C++
Raw Normal View History

// Copyright (c) 2014 The Bitcoin Core developers
// Copyright (c) 2020-2023 The Zcash developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://www.opensource.org/licenses/mit-license.php .
#include "timedata.h"
#include "netbase.h"
#include "sync.h"
#include "ui_interface.h"
scripted-diff: Move util files to separate directory. -BEGIN VERIFY SCRIPT- mkdir -p src/util git mv src/util.h src/util/system.h git mv src/util.cpp src/util/system.cpp git mv src/utilmoneystr.h src/util/moneystr.h git mv src/utilmoneystr.cpp src/util/moneystr.cpp git mv src/utilstrencodings.h src/util/strencodings.h git mv src/utilstrencodings.cpp src/util/strencodings.cpp git mv src/utiltime.h src/util/time.h git mv src/utiltime.cpp src/util/time.cpp sed -i -e 's/"util\.h"/"util\/system\.h"/g' $(git ls-files 'src/*.h' 'src/*.cpp') git checkout HEAD -- src/secp256k1 # exclude secp256k1, which has its own "util.h" sed -i -e 's/"utilmoneystr\.h"/"util\/moneystr\.h"/g' $(git ls-files 'src/*.h' 'src/*.cpp') sed -i -e 's/"utilstrencodings\.h"/"util\/strencodings\.h"/g' $(git ls-files 'src/*.h' 'src/*.cpp') sed -i -e 's/<utilstrencodings\.h>/<util\/strencodings\.h>/g' $(git ls-files 'src/*.h' 'src/*.cpp') sed -i -e 's/"utiltime\.h"/"util\/time\.h"/g' $(git ls-files 'src/*.h' 'src/*.cpp') sed -i -e 's/BITCOIN_UTIL_H/BITCOIN_UTIL_SYSTEM_H/g' src/util/system.h sed -i -e 's/BITCOIN_UTILMONEYSTR_H/BITCOIN_UTIL_MONEYSTR_H/g' src/util/moneystr.h sed -i -e 's/BITCOIN_UTILSTRENCODINGS_H/BITCOIN_UTIL_STRENCODINGS_H/g' src/util/strencodings.h sed -i -e 's/BITCOIN_UTILTIME_H/BITCOIN_UTIL_TIME_H/g' src/util/time.h sed -i -e 's/ util\.\(h\|cpp\)/ util\/system\.\1/g' src/Makefile.am sed -i -e 's/utilmoneystr\.\(h\|cpp\)/util\/moneystr\.\1/g' src/Makefile.am sed -i -e 's/utilstrencodings\.\(h\|cpp\)/util\/strencodings\.\1/g' src/Makefile.am sed -i -e 's/utiltime\.\(h\|cpp\)/util\/time\.\1/g' src/Makefile.am sed -i -e 's/src\/util\.cpp/src\/util\/system\.cpp/g' test/lint/lint-locale-dependence.sh sed -i -e 's/src\/utilmoneystr\.cpp/src\/util\/moneystr\.cpp/g' test/lint/lint-locale-dependence.sh sed -i -e 's/src\/utilstrencodings\.\(h\|cpp\)/src\/util\/strencodings\.\1/g' test/lint/lint-locale-dependence.sh -END VERIFY SCRIPT-
2018-10-22 15:51:11 -07:00
#include "util/system.h"
#include "util/strencodings.h"
#include "warnings.h"
CTimeWarning timeWarning;
/**
* 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.
*/
int64_t CTimeWarning::AddTimeData(const CNetAddr& ip, int64_t nTime, int64_t now)
{
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;
}
LogPrint("net","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;
}
void CTimeWarning::Warn(size_t peersAhead, size_t peersBehind)
{
std::string strMessage;
if (peersBehind >= TIMEDATA_WARNING_MAJORITY) {
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.");
} else if (peersAhead >= TIMEDATA_WARNING_MAJORITY) {
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.");
} else {
strMessage = _("Please check that your computer's date and time are correct! If your clock is wrong Zcash will not work properly.");
}
2020-06-12 14:08:00 -07:00
SetMiscWarning(strMessage, GetTime());
uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::MSG_WARNING);
}