MCPcopy Create free account
hub / github.com/WaykiChain/WaykiChain / AddTimeData

Function AddTimeData

src/commons/util/util.cpp:860–905  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

858int64_t GetAdjustedTime() { return GetTime() + GetTimeOffset(); }
859
860void AddTimeData(const CNetAddr& ip, int64_t nTime) {
861 int64_t nOffsetSample = nTime - GetTime();
862
863 LOCK(cs_nTimeOffset);
864 // Ignore duplicates
865 static set<CNetAddr> setKnown;
866 if (!setKnown.insert(ip).second)
867 return;
868
869 // Add data
870 static CMedianFilter<int64_t> vTimeOffsets(200, 0);
871 vTimeOffsets.input(nOffsetSample);
872 LogPrint(BCLog::INFO, "samples size=%d, offset=%+d (%+d minutes)\n", vTimeOffsets.size(),
873 nOffsetSample, nOffsetSample / 60);
874
875 if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1) {
876 int64_t nMedian = vTimeOffsets.median();
877 vector<int64_t> vSorted = vTimeOffsets.sorted();
878
879 // As block interval is so short, i.e., 10 seconds or 3 seconds. It's not necessary to adjust
880 // out timestamp depending on other peers.
881 // Every block producer should make sure it's timestamp is exactly precise. Of course, if nobody
882 // has a time different than ours but within 1 seconds of ours, give a warning.
883
884 if (abs64(nMedian) > 1) {
885 static bool fDone;
886 if (!fDone) {
887 bool fMatch = false;
888 for (int64_t nOffset : vSorted)
889 if (abs64(nOffset) <= 1)
890 fMatch = true;
891
892 if (!fMatch) {
893 fDone = true;
894 string strMessage =
895 _("Warning: Please check that your computer's date and time "
896 "are correct! If your clock is wrong Coin will not work properly.");
897 strMiscWarning = strMessage;
898 LogPrint(BCLog::INFO, "*** %s\n", strMessage);
899 }
900 }
901 }
902
903 LogPrint(BCLog::INFO, "nTimeOffset = %+d (%+d minutes)\n", nTimeOffset, nTimeOffset / 60);
904 }
905}
906
907string FormatVersion(int nVersion) {
908 if (nVersion % 100 == 0)

Callers 1

ProcessVersionMessageFunction · 0.85

Calls 8

GetTimeFunction · 0.85
abs64Function · 0.85
_Function · 0.85
medianMethod · 0.80
sortedMethod · 0.80
insertMethod · 0.45
inputMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected