Skip to content

Commit 14f99cf

Browse files
l0rincMarcoFalke
andcommitted
rpc: make uptime monotonic across NTP jumps
Compute `uptime` from `SteadyClock` so it is unaffected by system time changes after startup. Derive GUI startup time by subtracting the monotonic uptime from the wall clock time. Add a functional test covering a large `setmocktime` jump. Co-authored-by: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz>
1 parent a9440b1 commit 14f99cf

File tree

5 files changed

+15
-13
lines changed

5 files changed

+15
-13
lines changed

src/common/system.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@
3737

3838
using util::ReplaceAll;
3939

40-
// Application startup time (used for uptime calculation)
41-
const int64_t nStartupTime = GetTime();
42-
4340
#ifndef WIN32
4441
std::string ShellEscape(const std::string& arg)
4542
{
@@ -130,8 +127,8 @@ std::optional<size_t> GetTotalRAM()
130127
return std::nullopt;
131128
}
132129

133-
// Obtain the application startup time (used for uptime calculation)
134-
int64_t GetStartupTime()
130+
SteadyClock::duration GetUptime()
135131
{
136-
return nStartupTime;
132+
static const auto g_startup_time{SteadyClock::now()};
133+
return SteadyClock::now() - g_startup_time;
137134
}

src/common/system.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
#define BITCOIN_COMMON_SYSTEM_H
88

99
#include <bitcoin-build-config.h> // IWYU pragma: keep
10+
#include <util/time.h>
1011

12+
#include <chrono>
1113
#include <cstdint>
1214
#include <optional>
1315
#include <string>
1416

15-
// Application startup time (used for uptime calculation)
16-
int64_t GetStartupTime();
17+
/// Monotonic uptime (not affected by system time changes).
18+
SteadyClock::duration GetUptime();
1719

1820
void SetupEnvironment();
1921
[[nodiscard]] bool SetupNetworking();

src/qt/clientmodel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ bool ClientModel::isReleaseVersion() const
210210

211211
QString ClientModel::formatClientStartupTime() const
212212
{
213-
return QDateTime::fromSecsSinceEpoch(GetStartupTime()).toString();
213+
return QDateTime::currentDateTime().addSecs(-TicksSeconds(GetUptime())).toString();
214214
}
215215

216216
QString ClientModel::dataDir() const

src/rpc/server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ static RPCHelpMan uptime()
184184
},
185185
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
186186
{
187-
return GetTime() - GetStartupTime();
187+
return TicksSeconds(GetUptime());
188188
}
189189
};
190190
}

test/functional/rpc_uptime.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ def _test_negative_time(self):
2626
assert_raises_rpc_error(-8, "Mocktime must be in the range [0, 9223372036], not -1.", self.nodes[0].setmocktime, -1)
2727

2828
def _test_uptime(self):
29-
wait_time = 10
30-
self.nodes[0].setmocktime(int(time.time() + wait_time))
31-
assert self.nodes[0].uptime() >= wait_time
29+
wait_time = 20_000
30+
uptime_before = self.nodes[0].uptime()
31+
self.nodes[0].setmocktime(int(time.time()) + wait_time)
32+
uptime_after = self.nodes[0].uptime()
33+
self.nodes[0].setmocktime(0)
34+
assert uptime_after - uptime_before < wait_time, "uptime should not jump with wall clock"
3235

3336

3437
if __name__ == '__main__':

0 commit comments

Comments
 (0)