Merge pull request #673 from mndrix/less-time-data
authorGavin Andresen <gavinandresen@gmail.com>
Tue, 20 Dec 2011 16:55:55 +0000 (08:55 -0800)
committerGavin Andresen <gavinandresen@gmail.com>
Tue, 20 Dec 2011 16:55:55 +0000 (08:55 -0800)
Store fewer time samples

1  2 
src/util.cpp
src/util.h

diff --combined src/util.cpp
@@@ -4,7 -4,6 +4,7 @@@
  // file license.txt or http://www.opensource.org/licenses/mit-license.php.
  #include "headers.h"
  #include "strlcpy.h"
 +#include <boost/algorithm/string/join.hpp>
  #include <boost/program_options/detail/config_file.hpp>
  #include <boost/program_options/parsers.hpp>
  #include <boost/filesystem.hpp>
@@@ -31,7 -30,7 +31,7 @@@ string strMiscWarning
  bool fTestNet = false;
  bool fNoListen = false;
  bool fLogTimestamps = false;
+ CMedianFilter<int64> vTimeOffsets(200,0);
  
  
  
@@@ -133,7 -132,7 +133,7 @@@ uint64 GetRand(uint64 nMax
  
      // The range of the random source must be a multiple of the modulus
      // to give every possible output value an equal possibility
 -    uint64 nRange = (UINT64_MAX / nMax) * nMax;
 +    uint64 nRange = (std::numeric_limits<uint64>::max() / nMax) * nMax;
      uint64 nRand = 0;
      do
          RAND_bytes((unsigned char*)&nRand, sizeof(nRand));
@@@ -941,15 -940,12 +941,12 @@@ void AddTimeData(unsigned int ip, int6
          return;
  
      // Add data
-     static vector<int64> vTimeOffsets;
-     if (vTimeOffsets.empty())
-         vTimeOffsets.push_back(0);
-     vTimeOffsets.push_back(nOffsetSample);
-     printf("Added time data, samples %d, offset %+"PRI64d" (%+"PRI64d" minutes)\n", vTimeOffsets.size(), vTimeOffsets.back(), vTimeOffsets.back()/60);
+     vTimeOffsets.input(nOffsetSample);
+     printf("Added time data, samples %d, offset %+"PRI64d" (%+"PRI64d" minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60);
      if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1)
      {
-         sort(vTimeOffsets.begin(), vTimeOffsets.end());
-         int64 nMedian = vTimeOffsets[vTimeOffsets.size()/2];
+         int64 nMedian = vTimeOffsets.median();
+         std::vector<int64> vSorted = vTimeOffsets.sorted();
          // Only let other nodes change our time by so much
          if (abs64(nMedian) < 70 * 60)
          {
              {
                  // If nobody has a time different than ours but within 5 minutes of ours, give a warning
                  bool fMatch = false;
-                 BOOST_FOREACH(int64 nOffset, vTimeOffsets)
+                 BOOST_FOREACH(int64 nOffset, vSorted)
                      if (nOffset != 0 && abs64(nOffset) < 5 * 60)
                          fMatch = true;
  
                  }
              }
          }
-         BOOST_FOREACH(int64 n, vTimeOffsets)
-             printf("%+"PRI64d"  ", n);
-         printf("|  nTimeOffset = %+"PRI64d"  (%+"PRI64d" minutes)\n", nTimeOffset, nTimeOffset/60);
+         if (fDebug) {
+             BOOST_FOREACH(int64 n, vSorted)
+                 printf("%+"PRI64d"  ", n);
+             printf("|  ");
+         }
+         printf("nTimeOffset = %+"PRI64d"  (%+"PRI64d" minutes)\n", nTimeOffset, nTimeOffset/60);
      }
  }
  
@@@ -1002,7 -1001,7 +1002,7 @@@ string FormatVersion(int nVersion
  
  string FormatFullVersion()
  {
 -    string s = FormatVersion(VERSION) + pszSubVer;
 +    string s = FormatVersion(CLIENT_VERSION);
      if (VERSION_IS_BETA) {
          s += "-";
          s += _("beta");
      return s;
  }
  
 +// Format the subversion field according to BIP 14 spec (https://en.bitcoin.it/wiki/BIP_0014)
 +std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments)
 +{
 +    std::ostringstream ss;
 +    ss << "/";
 +    ss << name << ":" << FormatVersion(nClientVersion);
 +    if (!comments.empty())
 +        ss << "(" << boost::algorithm::join(comments, "; ") << ")";
 +    ss << "/";
 +    return ss.str();
 +}
  
  
  
diff --combined src/util.h
  #include <openssl/ripemd.h>
  
  
 -#if defined(_MSC_VER) || defined(__BORLANDC__)
 -typedef __int64  int64;
 -typedef unsigned __int64  uint64;
 -#else
  typedef long long  int64;
  typedef unsigned long long  uint64;
 -#endif
 -#if defined(_MSC_VER) && _MSC_VER < 1300
 -#define for  if (false) ; else for
 -#endif
 -#ifndef _MSC_VER
 -#define __forceinline  inline
 -#endif
  
  #define loop                for (;;)
  #define BEGIN(a)            ((char*)&(a))
@@@ -42,7 -53,7 +42,7 @@@
  #define snprintf my_snprintf
  
  #ifndef PRI64d
 -#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MSVCRT__)
 +#if defined(_MSC_VER) || defined(__MSVCRT__)
  #define PRI64d  "I64d"
  #define PRI64u  "I64u"
  #define PRI64x  "I64x"
@@@ -73,7 -84,11 +73,7 @@@ T* alignup(T* p
  #ifdef WIN32
  #define MSG_NOSIGNAL        0
  #define MSG_DONTWAIT        0
 -#ifndef UINT64_MAX
 -#define UINT64_MAX          _UI64_MAX
 -#define INT64_MAX           _I64_MAX
 -#define INT64_MIN           _I64_MIN
 -#endif
 +
  #ifndef S_IRUSR
  #define S_IRUSR             0400
  #define S_IWUSR             0200
@@@ -168,6 -183,7 +168,6 @@@ std::string DecodeBase64(const std::str
  std::string EncodeBase64(const unsigned char* pch, size_t len);
  std::string EncodeBase64(const std::string& str);
  void ParseParameters(int argc, char* argv[]);
 -const char* wxGetTranslation(const char* psz);
  bool WildcardMatch(const char* psz, const char* mask);
  bool WildcardMatch(const std::string& str, const std::string& mask);
  int GetFilesize(FILE* file);
@@@ -189,7 -205,7 +189,7 @@@ void SetMockTime(int64 nMockTimeIn)
  int64 GetAdjustedTime();
  void AddTimeData(unsigned int ip, int64 nTime);
  std::string FormatFullVersion();
 -
 +std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments);
  
  
  
@@@ -459,6 -475,15 +459,6 @@@ inline bool GetBoolArg(const std::strin
  
  
  
 -inline void heapchk()
 -{
 -#ifdef WIN32
 -    /// for debugging
 -    //if (_heapchk() != _HEAPOK)
 -    //    DebugBreak();
 -#endif
 -}
 -
  // Randomize the stack to help protect against buffer overrun exploits
  #define IMPLEMENT_RANDOMIZE_STACK(ThreadFn)     \
      {                                           \
@@@ -534,7 -559,7 +534,7 @@@ inline uint256 Hash(const T1 p1begin, c
  }
  
  template<typename T>
 -uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=VERSION)
 +uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
  {
      // Most of the time is spent allocating and deallocating CDataStream's
      // buffer.  If this ever needs to be optimized further, make a CStaticStream
@@@ -598,6 -623,16 +598,16 @@@ public
              return (vSorted[size/2-1] + vSorted[size/2]) / 2;
          }
      }
+     int size() const
+     {
+         return vValues.size();
+     }
+     std::vector<T> sorted () const
+     {
+         return vSorted;
+     }
  };