8 static CCriticalSection cs_nTimeOffset;
9 static uint32_t NOVACOIN_TIMEDATA_MAX_SAMPLES = 200;
11 // Trusted NTP offset or median of NTP samples.
12 extern int64_t nNtpOffset;
14 // Median of time samples given by other nodes.
15 static int64_t nNodesOffset = std::numeric_limits<int64_t>::max();
18 // "Never go to sea with two chronometers; take one or three."
19 // Our three time sources are:
21 // - Median of other nodes clocks
22 // - The user (asking the user to fix the system clock if the first two disagree)
25 // Select time offset:
26 int64_t GetTimeOffset()
29 // If NTP and system clock are in agreement within 40 minutes, then use NTP.
30 if (std::abs(nNtpOffset) < 40 * 60)
33 // If not, then choose between median peer time and system clock.
34 if (std::abs(nNodesOffset) < 70 * 60)
40 int64_t GetNodesOffset()
45 int64_t GetAdjustedTime()
47 return GetTime() + GetTimeOffset();
50 void AddTimeData(const CNetAddr& ip, int64_t nTime)
52 int64_t nOffsetSample = nTime - GetTime();
56 static std::set<CNetAddr> setKnown;
57 if (setKnown.size() == NOVACOIN_TIMEDATA_MAX_SAMPLES)
59 if (!setKnown.insert(ip).second)
63 static CMedianFilter<int64_t> vTimeOffsets(NOVACOIN_TIMEDATA_MAX_SAMPLES,0);
64 vTimeOffsets.input(nOffsetSample);
65 printf("Added time data, samples %d, offset %+" PRId64 " (%+" PRId64 " minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60);
66 if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1)
68 int64_t nMedian = vTimeOffsets.median();
69 std::vector<int64_t> vSorted = vTimeOffsets.sorted();
70 // Only let other nodes change our time by so much
71 if (std::abs(nMedian) < 70 * 60)
73 nNodesOffset = nMedian;
77 nNodesOffset = std::numeric_limits<int64_t>::max();
84 // If nobody has a time different than ours but within 5 minutes of ours, give a warning
85 for (int64_t nOffset : vSorted)
86 if (nOffset != 0 && std::abs(nOffset) < 5 * 60)
92 std::string strMessage = _("Warning: Please check that your computer's date and time are correct! If your clock is wrong NovaCoin will not work properly.");
93 strMiscWarning = strMessage;
94 printf("*** %s\n", strMessage.c_str());
95 uiInterface.ThreadSafeMessageBox(strMessage+" ", std::string("NovaCoin"), CClientUIInterface::OK | CClientUIInterface::ICON_EXCLAMATION);
100 for (int64_t n : vSorted)
101 printf("%+" PRId64 " ", n);
104 if (nNodesOffset != std::numeric_limits<int64_t>::max())
105 printf("nNodesOffset = %+" PRId64 " (%+" PRId64 " minutes)\n", nNodesOffset, nNodesOffset/60);