Keep old GetAdjustedTime() behavior if no NTP data is available;
[novacoin.git] / src / ntp.cpp
index 60cc309..4cc422b 100644 (file)
@@ -13,7 +13,8 @@
 
 #include "netbase.h"
 #include "net.h"
-#include "util.h"
+//#include "util.h"
+#include "ui_interface.h"
 
 extern int GetRandInt(int nMax);
 
@@ -420,9 +421,7 @@ int64_t GetNtpOffset() {
 }
 
 void ThreadNtpSamples(void* parg) {
-
-    // Maximum offset is 2 hours.
-    const int64_t nMaxOffset = 7200;
+    const int64_t nMaxOffset = 86400; // Not a real limit, just sanity threshold.
 
     printf("Trying to find NTP server at localhost...\n");
 
@@ -478,11 +477,11 @@ void ThreadNtpSamples(void* parg) {
                 }
             }
 
-            if (vTimeOffsets.size() > 2) {
+            if (vTimeOffsets.size() > 1) {
                 nNtpOffset = vTimeOffsets.median();
             }
             else {
-                // Not enough offsets yet, try again later.
+                // Not enough offsets yet, try to collect additional samples later.
                 nNtpOffset = INT64_MAX;
                 int nSleepMinutes = 1 + GetRandInt(4); // Sleep for 1-5 minutes.
                 for (int i = 0; i < nSleepMinutes * 60 && !fShutdown; i++) 
@@ -491,6 +490,15 @@ void ThreadNtpSamples(void* parg) {
             }
         }
 
+        if (GetNodesOffset() == INT_MAX && abs64(nNtpOffset) > 40 * 60)
+        {
+            // If there is not enough node offsets data and NTP time offset is greater than 40 minutes then give a warning.
+            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.");
+            strMiscWarning = strMessage;
+            printf("*** %s\n", strMessage.c_str());
+            uiInterface.ThreadSafeMessageBox(strMessage+" ", std::string("NovaCoin"), CClientUIInterface::OK | CClientUIInterface::ICON_EXCLAMATION);
+        }
+
         printf("nNtpOffset = %+" PRId64 "  (%+" PRId64 " minutes)\n", nNtpOffset, nNtpOffset/60);
 
         int nSleepHours = 1 + GetRandInt(5); // Sleep for 1-6 hours.