Skip to content

Commit 08dada8

Browse files
author
MarcoFalke
committed
util: Disallow negative mocktime
Signed-off-by: practicalswift <[email protected]> Github-Pull: #21043 Rebased-From: 3ddbf22
1 parent 95218ee commit 08dada8

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

src/rpc/misc.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,13 @@ static RPCHelpMan signmessagewithprivkey()
360360
static RPCHelpMan setmocktime()
361361
{
362362
return RPCHelpMan{"setmocktime",
363-
"\nSet the local time to given timestamp (-regtest only)\n",
364-
{
365-
{"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, UNIX_EPOCH_TIME + "\n"
366-
" Pass 0 to go back to using the system time."},
367-
},
368-
RPCResult{RPCResult::Type::NONE, "", ""},
369-
RPCExamples{""},
363+
"\nSet the local time to given timestamp (-regtest only)\n",
364+
{
365+
{"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, UNIX_EPOCH_TIME + "\n"
366+
"Pass 0 to go back to using the system time."},
367+
},
368+
RPCResult{RPCResult::Type::NONE, "", ""},
369+
RPCExamples{""},
370370
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
371371
{
372372
if (!Params().IsMockableChain()) {
@@ -381,7 +381,10 @@ static RPCHelpMan setmocktime()
381381
LOCK(cs_main);
382382

383383
RPCTypeCheck(request.params, {UniValue::VNUM});
384-
int64_t time = request.params[0].get_int64();
384+
const int64_t time{request.params[0].get_int64()};
385+
if (time < 0) {
386+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Mocktime can not be negative: %s.", time));
387+
}
385388
SetMockTime(time);
386389
if (request.context.Has<NodeContext>()) {
387390
for (const auto& chain_client : request.context.Get<NodeContext>().chain_clients) {

src/util/time.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include <util/time.h>
1111

12+
#include <util/check.h>
13+
1214
#include <atomic>
1315
#include <boost/date_time/posix_time/posix_time.hpp>
1416
#include <ctime>
@@ -18,7 +20,7 @@
1820

1921
void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
2022

21-
static std::atomic<int64_t> nMockTime(0); //!< For unit testing
23+
static std::atomic<int64_t> nMockTime(0); //!< For testing
2224

2325
int64_t GetTime()
2426
{
@@ -46,6 +48,7 @@ template std::chrono::microseconds GetTime();
4648

4749
void SetMockTime(int64_t nMockTimeIn)
4850
{
51+
Assert(nMockTimeIn >= 0);
4952
nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
5053
}
5154

test/functional/rpc_uptime.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import time
1111

1212
from test_framework.test_framework import BitcoinTestFramework
13+
from test_framework.util import assert_raises_rpc_error
1314

1415

1516
class UptimeTest(BitcoinTestFramework):
@@ -18,8 +19,12 @@ def set_test_params(self):
1819
self.setup_clean_chain = True
1920

2021
def run_test(self):
22+
self._test_negative_time()
2123
self._test_uptime()
2224

25+
def _test_negative_time(self):
26+
assert_raises_rpc_error(-8, "Mocktime can not be negative: -1.", self.nodes[0].setmocktime, -1)
27+
2328
def _test_uptime(self):
2429
wait_time = 10
2530
self.nodes[0].setmocktime(int(time.time() + wait_time))

0 commit comments

Comments
 (0)