X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Futil.cpp;h=63c684bb0b28c52856fbd93351da0dedcf952370;hb=b60dd5749ca05390bceaf3256efc2a036a91f2e8;hp=8cc62eb672d5d0ed60229d8687845e209fdbd821;hpb=3814af1ef1a10f25e7a76929edfd3abdb0ab791d;p=novacoin.git diff --git a/src/util.cpp b/src/util.cpp index 8cc62eb..63c684b 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1155,7 +1155,7 @@ string randomStrGen(int length) { void createConf() { - srand(time(NULL)); + srand(static_cast(time(NULL))); ofstream pConf; #if BOOST_FILESYSTEM_VERSION >= 3 @@ -1235,7 +1235,7 @@ bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest) { #ifdef WIN32 return MoveFileExA(src.string().c_str(), dest.string().c_str(), - MOVEFILE_REPLACE_EXISTING); + MOVEFILE_REPLACE_EXISTING) != 0; #else int rc = std::rename(src.string().c_str(), dest.string().c_str()); return (rc == 0); @@ -1271,7 +1271,7 @@ void ShrinkDebugFile() { // Restart the file with some of the end char pch[200000]; - fseek(file, (long long)-sizeof(pch), SEEK_END); + fseek(file, -((long long)sizeof(pch)), SEEK_END); int nBytes = fread(pch, 1, sizeof(pch), file); fclose(file); @@ -1298,25 +1298,32 @@ void ShrinkDebugFile() // - Median of other nodes clocks // - The user (asking the user to fix the system clock if the first two disagree) // -static int64_t nMockTime = 0; // For unit testing +// System clock int64_t GetTime() { - if (nMockTime) return nMockTime; - return time(NULL); } -void SetMockTime(int64_t nMockTimeIn) -{ - nMockTime = nMockTimeIn; -} +// Trusted NTP offset or median of NTP samples. +extern int64_t nNtpOffset; -static int64_t nTimeOffset = 0; +// Median of time samples given by other nodes. +static int64_t nNodesOffset = INT64_MAX; +// Select time offset: +// +// * If NTP and system clock are in agreement within 40 minutes, then use NTP. +// * If not, then choose between median peer time and system clock using the same condition. int64_t GetTimeOffset() { - return nTimeOffset; + if (abs64(nNtpOffset) < 40 * 60) + return nNtpOffset; + + if (abs64(nNodesOffset) < 40 * 60) + return nNodesOffset; + + return 0; } int64_t GetAdjustedTime() @@ -1343,17 +1350,18 @@ void AddTimeData(const CNetAddr& ip, int64_t nTime) // Only let other nodes change our time by so much if (abs64(nMedian) < 70 * 60) { - nTimeOffset = nMedian; + nNodesOffset = nMedian; } else { - nTimeOffset = 0; + nNodesOffset = INT64_MAX; static bool fDone; if (!fDone) { - // If nobody has a time different than ours but within 5 minutes of ours, give a warning bool fMatch = false; + + // If nobody has a time different than ours but within 5 minutes of ours, give a warning BOOST_FOREACH(int64_t nOffset, vSorted) if (nOffset != 0 && abs64(nOffset) < 5 * 60) fMatch = true; @@ -1373,17 +1381,11 @@ void AddTimeData(const CNetAddr& ip, int64_t nTime) printf("%+" PRId64 " ", n); printf("| "); } - printf("nTimeOffset = %+" PRId64 " (%+" PRId64 " minutes)\n", nTimeOffset, nTimeOffset/60); + if (nNodesOffset != INT64_MAX) + printf("nNodesOffset = %+" PRId64 " (%+" PRId64 " minutes)\n", nNodesOffset, nNodesOffset/60); } } - - - - - - - string FormatVersion(int nVersion) { if (nVersion%100 == 0)