diff options
author | fanquake <fanquake@gmail.com> | 2022-05-20 07:35:26 +0100 |
---|---|---|
committer | fanquake <fanquake@gmail.com> | 2022-05-20 07:48:07 +0100 |
commit | 6407c0e8a33f5a04eae4ebfb5befdcc1c2ab2b57 (patch) | |
tree | e12de5d04c2be4f17c6601101a02633e6eb67d0e /src/util | |
parent | 0de36941eca1bff91420dd878eb097db2b1a596c (diff) | |
parent | fa305fd92c0a5a91831be3ccec0a5ef962a5fbcb (diff) |
Merge bitcoin/bitcoin#25101: Add mockable clock type
fa305fd92c0a5a91831be3ccec0a5ef962a5fbcb Add mockable clock type and TicksSinceEpoch helper (MarcoFalke)
Pull request description:
This will be used primarily by the addr time refactor (https://github.com/bitcoin/bitcoin/pull/24697) to make addr relay time type safe. However, it can also be used in other places, and can be reviewed independently, so I split it up.
ACKs for top commit:
jonatack:
ACK fa305fd92c0a5a91831be3ccec0a5ef962a5fbcb per `git range-diff 7b3343f fa20781 fa305fd`
ajtowns:
ACK fa305fd92c0a5a91831be3ccec0a5ef962a5fbcb
Tree-SHA512: da00200126833c1f55b1b1e68f596eab5c9254e82b188ad17779c08ffd685e198a7c5270791b4b69a858dc6ba4e051fe0c8b445d203d356d0c884f6365ee1cfe
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/time.cpp | 12 | ||||
-rw-r--r-- | src/util/time.h | 31 |
2 files changed, 28 insertions, 15 deletions
diff --git a/src/util/time.cpp b/src/util/time.cpp index 4ec44509ab..7d9d6bcff1 100644 --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -66,20 +66,16 @@ bool ChronoSanityCheck() return true; } -template <typename T> -T GetTime() +NodeClock::time_point NodeClock::now() noexcept { const std::chrono::seconds mocktime{nMockTime.load(std::memory_order_relaxed)}; const auto ret{ mocktime.count() ? mocktime : - std::chrono::duration_cast<T>(std::chrono::system_clock::now().time_since_epoch())}; + std::chrono::system_clock::now().time_since_epoch()}; assert(ret > 0s); - return ret; -} -template std::chrono::seconds GetTime(); -template std::chrono::milliseconds GetTime(); -template std::chrono::microseconds GetTime(); + return time_point{ret}; +}; template <typename T> static T GetSystemTime() diff --git a/src/util/time.h b/src/util/time.h index 72956ea0d7..14df3fe53a 100644 --- a/src/util/time.h +++ b/src/util/time.h @@ -14,6 +14,16 @@ using namespace std::chrono_literals; +/** Mockable clock in the context of tests, otherwise the system clock */ +struct NodeClock : public std::chrono::system_clock { + using time_point = std::chrono::time_point<NodeClock>; + /** Return current system time or mocked time, if set */ + static time_point now() noexcept; + static std::time_t to_time_t(const time_point&) = delete; // unused + static time_point from_time_t(std::time_t) = delete; // unused +}; +using NodeSeconds = std::chrono::time_point<NodeClock, std::chrono::seconds>; + using SteadySeconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::seconds>; using SteadyMilliseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds>; using SteadyMicroseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>; @@ -30,10 +40,10 @@ void UninterruptibleSleep(const std::chrono::microseconds& n); * This helper is used to convert durations/time_points before passing them over an * interface that doesn't support std::chrono (e.g. RPC, debug log, or the GUI) */ -template <typename Clock> -constexpr int64_t count_seconds(std::chrono::time_point<Clock, std::chrono::seconds> t) +template <typename Duration, typename Timepoint> +constexpr auto TicksSinceEpoch(Timepoint t) { - return t.time_since_epoch().count(); + return std::chrono::time_point_cast<Duration>(t).time_since_epoch().count(); } constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); } constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); } @@ -48,7 +58,11 @@ inline double CountSecondsDouble(SecondsDouble t) { return t.count(); } /** * DEPRECATED - * Use either GetTimeSeconds (not mockable) or GetTime<T> (mockable) + * Use either ClockType::now() or Now<TimePointType>() if a cast is needed. + * ClockType is + * - std::chrono::steady_clock for steady time + * - std::chrono::system_clock for system time + * - NodeClock for mockable system time */ int64_t GetTime(); @@ -71,9 +85,6 @@ void SetMockTime(std::chrono::seconds mock_time_in); /** For testing */ std::chrono::seconds GetMockTime(); -/** Return system time (or mocked time, if set) */ -template <typename T> -T GetTime(); /** * Return the current time point cast to the given precicion. Only use this * when an exact precicion is needed, otherwise use T::clock::now() directly. @@ -83,6 +94,12 @@ T Now() { return std::chrono::time_point_cast<typename T::duration>(T::clock::now()); } +/** DEPRECATED, see GetTime */ +template <typename T> +T GetTime() +{ + return Now<std::chrono::time_point<NodeClock, T>>().time_since_epoch(); +} /** * ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date} |