mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-08 05:39:38 +02:00
Merge bitcoin/bitcoin#34328: rpc: make uptime monotonic across NTP jumps
14f99cfe53rpc: make `uptime` monotonic across NTP jumps (Lőrinc)a9440b1595util: add `TicksSeconds` (Lőrinc) Pull request description: ### Problem `bitcoin-cli uptime` was derived from wall-clock time, so it could jump by large amounts when the system clock is corrected after `bitcoind` starts (e.g. on RTC-less systems syncing NTP). This breaks the expectation that uptime reflects process runtime. ### Fix Compute uptime from a [monotonic clock](https://en.cppreference.com/w/cpp/chrono/steady_clock.html) so it is immune to wall-clock jumps, and use that monotonic uptime for the RPC. GUI startup time is derived from wall clock time minus monotonic uptime so it remains sensible after clock corrections. ### Reproducer Revert the fix commit and run the `rpc_uptime` functional test (it should fail with `AssertionError: uptime should not jump with wall clock`): Or alternatively: ```bash cmake -B build && cmake --build build --target bitcoind bitcoin-cli -j$(nproc) DATA_DIR=$(mktemp -d) ./build/bin/bitcoind -regtest -datadir="$DATA_DIR" -connect=0 -daemon ./build/bin/bitcoin-cli -regtest -datadir="$DATA_DIR" -rpcwait uptime sleep 1 ./build/bin/bitcoin-cli -regtest -datadir="$DATA_DIR" setmocktime $(( $(date +%s) + 20000000 )) ./build/bin/bitcoin-cli -regtest -datadir="$DATA_DIR" uptime ./build/bin/bitcoin-cli -regtest -datadir="$DATA_DIR" stop ``` <details> <summary>Before (uptime jumps with wall clock)</summary> ```bash Bitcoin Core starting 0 20000001 Bitcoin Core stopping ``` </details> <details> <summary>After (uptime stays monotonic)</summary> ```bash Bitcoin Core starting 0 1 Bitcoin Core stopping ``` </details> ---------- Issue: https://github.com/bitcoin/bitcoin/issues/34326 ACKs for top commit: maflcko: review ACK14f99cfe53🎦 willcl-ark: tACK14f99cfe53w0xlt: ACK14f99cfe53sedited: ACK14f99cfe53Tree-SHA512: 3909973f58666ffa0b784a6df087031b9e34d2022d354900a4dbb6cbe1d36285cd92770ee71350ebf64d6e8ab212d8ff0cd851f7dca1ec46ee2f19b417f53984
This commit is contained in:
@@ -26,9 +26,12 @@ class UptimeTest(BitcoinTestFramework):
|
||||
assert_raises_rpc_error(-8, "Mocktime must be in the range [0, 9223372036], not -1.", self.nodes[0].setmocktime, -1)
|
||||
|
||||
def _test_uptime(self):
|
||||
wait_time = 10
|
||||
self.nodes[0].setmocktime(int(time.time() + wait_time))
|
||||
assert self.nodes[0].uptime() >= wait_time
|
||||
wait_time = 20_000
|
||||
uptime_before = self.nodes[0].uptime()
|
||||
self.nodes[0].setmocktime(int(time.time()) + wait_time)
|
||||
uptime_after = self.nodes[0].uptime()
|
||||
self.nodes[0].setmocktime(0)
|
||||
assert uptime_after - uptime_before < wait_time, "uptime should not jump with wall clock"
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user