Changeset View
Changeset View
Standalone View
Standalone View
src/timedata.cpp
Show All 9 Lines | |||||
#include <netaddress.h> | #include <netaddress.h> | ||||
#include <node/ui_interface.h> | #include <node/ui_interface.h> | ||||
#include <sync.h> | #include <sync.h> | ||||
#include <util/system.h> | #include <util/system.h> | ||||
#include <util/translation.h> | #include <util/translation.h> | ||||
#include <warnings.h> | #include <warnings.h> | ||||
static RecursiveMutex cs_nTimeOffset; | static Mutex g_timeoffset_mutex; | ||||
static int64_t nTimeOffset GUARDED_BY(cs_nTimeOffset) = 0; | static int64_t nTimeOffset GUARDED_BY(g_timeoffset_mutex) = 0; | ||||
/** | /** | ||||
* "Never go to sea with two chronometers; take one or three." | * "Never go to sea with two chronometers; take one or three." | ||||
* Our three time sources are: | * Our three time sources are: | ||||
* - System clock | * - System clock | ||||
* - Median of other nodes clocks | * - Median of other nodes clocks | ||||
* - The user (asking the user to fix the system clock if the first two | * - The user (asking the user to fix the system clock if the first two | ||||
* disagree) | * disagree) | ||||
*/ | */ | ||||
int64_t GetTimeOffset() { | int64_t GetTimeOffset() { | ||||
LOCK(cs_nTimeOffset); | LOCK(g_timeoffset_mutex); | ||||
return nTimeOffset; | return nTimeOffset; | ||||
} | } | ||||
int64_t GetAdjustedTime() { | int64_t GetAdjustedTime() { | ||||
return GetTime() + GetTimeOffset(); | return GetTime() + GetTimeOffset(); | ||||
} | } | ||||
static uint64_t abs64(int64_t n) { | static uint64_t abs64(int64_t n) { | ||||
const uint64_t un = n; | const uint64_t un = n; | ||||
return (n >= 0) ? un : -un; | return (n >= 0) ? un : -un; | ||||
} | } | ||||
#define BITCOIN_TIMEDATA_MAX_SAMPLES 200 | #define BITCOIN_TIMEDATA_MAX_SAMPLES 200 | ||||
void AddTimeData(const CNetAddr &ip, int64_t nOffsetSample) { | void AddTimeData(const CNetAddr &ip, int64_t nOffsetSample) { | ||||
LOCK(cs_nTimeOffset); | LOCK(g_timeoffset_mutex); | ||||
// Ignore duplicates | // Ignore duplicates | ||||
static std::set<CNetAddr> setKnown; | static std::set<CNetAddr> setKnown; | ||||
if (setKnown.size() == BITCOIN_TIMEDATA_MAX_SAMPLES) { | if (setKnown.size() == BITCOIN_TIMEDATA_MAX_SAMPLES) { | ||||
return; | return; | ||||
} | } | ||||
if (!setKnown.insert(ip).second) { | if (!setKnown.insert(ip).second) { | ||||
return; | return; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 73 Lines • Show Last 20 Lines |