Skip to content

Commit 62efead

Browse files
committed
Merge #16046: util: Add type safe GetTime
fa01366 util: Add type safe GetTime (MarcoFalke) Pull request description: There are basically two ways to get the time in Bitcoin Core: * get the system time (via `GetSystemTimeInSeconds` or `GetTime{Millis,Micros}`) * get the mockable time (via `GetTime`) Both return the same type (a plain int). This can lead to (test-only) bugs such as 99464bc. Fix that by deprecating `GetTime` and adding a `GetTime<>` that returns the mockable time in a non-int type. The new util function is currently unused, but new code should it where possible. ACKs for commit fa0136: promag: utACK fa01366. Tree-SHA512: efab9c463f079fd8fd3030c479637c7b1e8be567a881234bd0f555c8f87e518e3b43ef2466128103db8fc40295aaf24e87ad76d91f338c631246fc703477e95c
2 parents 12ba656 + fa01366 commit 62efead

File tree

5 files changed

+96
-11
lines changed

5 files changed

+96
-11
lines changed

src/Makefile.bench.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ bench_bench_bitcoin_SOURCES = \
2828
bench/merkle_root.cpp \
2929
bench/mempool_eviction.cpp \
3030
bench/rpc_mempool.cpp \
31+
bench/util_time.cpp \
3132
bench/verify_script.cpp \
3233
bench/base58.cpp \
3334
bench/bech32.cpp \

src/bench/util_time.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2019 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <bench/bench.h>
6+
7+
#include <util/time.h>
8+
9+
static void BenchTimeDeprecated(benchmark::State& state)
10+
{
11+
while (state.KeepRunning()) {
12+
(void)GetTime();
13+
}
14+
}
15+
16+
static void BenchTimeMock(benchmark::State& state)
17+
{
18+
SetMockTime(111);
19+
while (state.KeepRunning()) {
20+
(void)GetTime<std::chrono::seconds>();
21+
}
22+
SetMockTime(0);
23+
}
24+
25+
static void BenchTimeMillis(benchmark::State& state)
26+
{
27+
while (state.KeepRunning()) {
28+
(void)GetTime<std::chrono::milliseconds>();
29+
}
30+
}
31+
32+
static void BenchTimeMillisSys(benchmark::State& state)
33+
{
34+
while (state.KeepRunning()) {
35+
(void)GetTimeMillis();
36+
}
37+
}
38+
39+
BENCHMARK(BenchTimeDeprecated, 100000000);
40+
BENCHMARK(BenchTimeMillis, 6000000);
41+
BENCHMARK(BenchTimeMillisSys, 6000000);
42+
BENCHMARK(BenchTimeMock, 300000000);

src/test/util_tests.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,27 @@ BOOST_AUTO_TEST_CASE(gettime)
10681068
BOOST_CHECK((GetTime() & ~0xFFFFFFFFLL) == 0);
10691069
}
10701070

1071+
BOOST_AUTO_TEST_CASE(util_time_GetTime)
1072+
{
1073+
SetMockTime(111);
1074+
// Check that mock time does not change after a sleep
1075+
for (const auto& num_sleep : {0, 1}) {
1076+
MilliSleep(num_sleep);
1077+
BOOST_CHECK_EQUAL(111, GetTime()); // Deprecated time getter
1078+
BOOST_CHECK_EQUAL(111, GetTime<std::chrono::seconds>().count());
1079+
BOOST_CHECK_EQUAL(111000, GetTime<std::chrono::milliseconds>().count());
1080+
BOOST_CHECK_EQUAL(111000000, GetTime<std::chrono::microseconds>().count());
1081+
}
1082+
1083+
SetMockTime(0);
1084+
// Check that system time changes after a sleep
1085+
const auto ms_0 = GetTime<std::chrono::milliseconds>();
1086+
const auto us_0 = GetTime<std::chrono::microseconds>();
1087+
MilliSleep(1);
1088+
BOOST_CHECK(ms_0 < GetTime<std::chrono::milliseconds>());
1089+
BOOST_CHECK(us_0 < GetTime<std::chrono::microseconds>());
1090+
}
1091+
10711092
BOOST_AUTO_TEST_CASE(test_IsDigit)
10721093
{
10731094
BOOST_CHECK_EQUAL(IsDigit('0'), true);

src/util/time.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2018 The Bitcoin Core developers
2+
// Copyright (c) 2009-2019 The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -27,6 +27,20 @@ int64_t GetTime()
2727
return now;
2828
}
2929

30+
template <typename T>
31+
T GetTime()
32+
{
33+
const std::chrono::seconds mocktime{nMockTime.load(std::memory_order_relaxed)};
34+
35+
return std::chrono::duration_cast<T>(
36+
mocktime.count() ?
37+
mocktime :
38+
std::chrono::microseconds{GetTimeMicros()});
39+
}
40+
template std::chrono::seconds GetTime();
41+
template std::chrono::milliseconds GetTime();
42+
template std::chrono::microseconds GetTime();
43+
3044
void SetMockTime(int64_t nMockTimeIn)
3145
{
3246
nMockTime.store(nMockTimeIn, std::memory_order_relaxed);

src/util/time.h

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2018 The Bitcoin Core developers
2+
// Copyright (c) 2009-2019 The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -8,27 +8,34 @@
88

99
#include <stdint.h>
1010
#include <string>
11+
#include <chrono>
1112

1213
/**
13-
* GetTimeMicros() and GetTimeMillis() both return the system time, but in
14-
* different units. GetTime() returns the system time in seconds, but also
15-
* supports mocktime, where the time can be specified by the user, eg for
16-
* testing (eg with the setmocktime rpc, or -mocktime argument).
17-
*
18-
* TODO: Rework these functions to be type-safe (so that we don't inadvertently
19-
* compare numbers with different units, or compare a mocktime to system time).
14+
* DEPRECATED
15+
* Use either GetSystemTimeInSeconds (not mockable) or GetTime<T> (mockable)
2016
*/
21-
2217
int64_t GetTime();
18+
19+
/** Returns the system time (not mockable) */
2320
int64_t GetTimeMillis();
21+
/** Returns the system time (not mockable) */
2422
int64_t GetTimeMicros();
23+
/** Returns the system time (not mockable) */
2524
int64_t GetSystemTimeInSeconds(); // Like GetTime(), but not mockable
25+
26+
/** For testing. Set e.g. with the setmocktime rpc, or -mocktime argument */
2627
void SetMockTime(int64_t nMockTimeIn);
28+
/** For testing */
2729
int64_t GetMockTime();
30+
2831
void MilliSleep(int64_t n);
2932

33+
/** Return system time (or mocked time, if set) */
34+
template <typename T>
35+
T GetTime();
36+
3037
/**
31-
* ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date,Time}
38+
* ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date}
3239
* helper functions if possible.
3340
*/
3441
std::string FormatISO8601DateTime(int64_t nTime);

0 commit comments

Comments
 (0)