Additional randomization of NTP requests. Also add timestamping information to output...
authorCryptoManiac <balthazar@yandex.ru>
Tue, 22 Sep 2015 00:41:24 +0000 (03:41 +0300)
committerCryptoManiac <balthazar@yandex.ru>
Tue, 22 Sep 2015 00:49:16 +0000 (03:49 +0300)
src/ntp.cpp
src/rpcwallet.cpp
src/util.cpp
src/util.h

index b069f39..2ce65e1 100644 (file)
@@ -442,11 +442,12 @@ void ThreadNtpSamples(void* parg) {
                 nNtpOffset = nClockOffset;
             }
             else {
-                // Something went wrong. Disable trusted offset sampling and wait 600 seconds.
+                // Something went wrong, disable trusted offset sampling.
                 nNtpOffset = INT64_MAX;
                 strTrustedUpstream = "localhost";
 
-                for (int i = 0; i < 600 && !fShutdown; i++) // Sleep for 5 minutes
+                int nSleepMinutes = 1 + GetRandInt(9); // Sleep for 1-10 minutes.
+                for (int i = 0; i < nSleepMinutes * 60 && !fShutdown; i++)
                     Sleep(1000);
 
                 continue;
@@ -469,9 +470,10 @@ void ThreadNtpSamples(void* parg) {
                 nNtpOffset = vTimeOffsets.median();
             }
             else {
-                // Not enough offsets yet, try again 300 seconds later.
+                // Not enough offsets yet, try again later.
                 nNtpOffset = INT64_MAX;
-                for (int i = 0; i < 300 && !fShutdown; i++) 
+                int nSleepMinutes = 1 + GetRandInt(4); // Sleep for 1-5 minutes.
+                for (int i = 0; i < nSleepMinutes * 60 && !fShutdown; i++) 
                     Sleep(1000);
                 continue;
             }
@@ -479,7 +481,8 @@ void ThreadNtpSamples(void* parg) {
 
         printf("nNtpOffset = %+" PRId64 "  (%+" PRId64 " minutes)\n", nNtpOffset, nNtpOffset/60);
 
-        for (int i = 0; i < 43200 && !fShutdown; i++) // Sleep for 12 hours
+        int nSleepHours = 1 + GetRandInt(5); // Sleep for 1-6 hours.
+        for (int i = 0; i < nSleepHours * 3600 && !fShutdown; i++)
             Sleep(1000);
     }
 
index 2414bdf..67c24e3 100644 (file)
@@ -7,6 +7,8 @@
 #include "walletdb.h"
 #include "bitcoinrpc.h"
 #include "init.h"
+#include "util.h"
+#include "ntp.h"
 #include "base58.h"
 
 using namespace json_spirit;
@@ -70,7 +72,7 @@ Value getinfo(const Array& params, bool fHelp)
     proxyType proxy;
     GetProxy(NET_IPV4, proxy);
 
-    Object obj, diff;
+    Object obj, diff, timestamping;
     obj.push_back(Pair("version",       FormatFullVersion()));
     obj.push_back(Pair("protocolversion",(int)PROTOCOL_VERSION));
     obj.push_back(Pair("walletversion", pwalletMain->GetVersion()));
@@ -79,7 +81,18 @@ Value getinfo(const Array& params, bool fHelp)
     obj.push_back(Pair("newmint",       ValueFromAmount(pwalletMain->GetNewMint())));
     obj.push_back(Pair("stake",         ValueFromAmount(pwalletMain->GetStake())));
     obj.push_back(Pair("blocks",        (int)nBestHeight));
-    obj.push_back(Pair("timeoffset",    (int64_t)GetTimeOffset()));
+
+    timestamping.push_back(Pair("systemclock", GetTime()));
+    timestamping.push_back(Pair("adjustedtime", GetAdjustedTime()));
+
+    int64_t nNtpOffset = GetNtpOffset(),
+            nP2POffset = GetNodesOffset();
+
+    timestamping.push_back(Pair("ntpoffset", nNtpOffset != INT64_MAX ? nNtpOffset : Value::null));
+    timestamping.push_back(Pair("p2poffset", nP2POffset != INT64_MAX ? nP2POffset : Value::null));
+
+    obj.push_back(Pair("timestamping", timestamping));
+
     obj.push_back(Pair("moneysupply",   ValueFromAmount(pindexBest->nMoneySupply)));
     obj.push_back(Pair("connections",   (int)vNodes.size()));
     obj.push_back(Pair("proxy",         (proxy.first.IsValid() ? proxy.first.ToStringIPPort() : string())));
index 63c684b..7e0f662 100644 (file)
@@ -1326,6 +1326,11 @@ int64_t GetTimeOffset()
     return 0;
 }
 
+int64_t GetNodesOffset()
+{
+        return nNodesOffset;
+}
+
 int64_t GetAdjustedTime()
 {
     return GetTime() + GetTimeOffset();
index 6ddc7a6..5835e90 100644 (file)
@@ -226,6 +226,7 @@ int64_t GetTime();
 void SetMockTime(int64_t nMockTimeIn);
 int64_t GetAdjustedTime();
 int64_t GetTimeOffset();
+int64_t GetNodesOffset();
 std::string FormatFullVersion();
 std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments);
 void AddTimeData(const CNetAddr& ip, int64_t nTime);