diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -3464,6 +3464,9 @@ uint64_t nExtraEntropy = 1; vRecv >> nVersion >> nServiceInt >> nTime >> addrMe; + if (nTime < 0) { + nTime = 0; + } nServices = ServiceFlags(nServiceInt); if (!pfrom.IsInboundConn()) { m_connman.SetServices(pfrom.addr, nServices); diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -445,9 +445,8 @@ "Set the local time to given timestamp (-regtest only)\n", { {"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, - UNIX_EPOCH_TIME + - "\n" - " Pass 0 to go back to using the system time."}, + UNIX_EPOCH_TIME + "\n" + "Pass 0 to go back to using the system time."}, }, RPCResult{RPCResult::Type::NONE, "", ""}, RPCExamples{""}, @@ -466,10 +465,11 @@ LOCK(cs_main); RPCTypeCheck(request.params, {UniValue::VNUM}); - int64_t time = request.params[0].get_int64(); + const int64_t time{request.params[0].get_int64()}; if (time < 0) { - throw JSONRPCError(RPC_INVALID_PARAMETER, - "Timestamp must be 0 or greater"); + throw JSONRPCError( + RPC_INVALID_PARAMETER, + strprintf("Mocktime can not be negative: %s.", time)); } SetMockTime(time); auto node_context = util::AnyPtr(request.context); diff --git a/src/util/time.cpp b/src/util/time.cpp --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -3,12 +3,14 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include + #if defined(HAVE_CONFIG_H) #include #endif #include -#include +#include #include @@ -22,7 +24,7 @@ std::this_thread::sleep_for(n); } -//! For unit testing +//! For testing static std::atomic nMockTime(0); int64_t GetTime() { @@ -49,7 +51,7 @@ template std::chrono::microseconds GetTime(); void SetMockTime(int64_t nMockTimeIn) { - assert(nMockTimeIn >= 0); + Assert(nMockTimeIn >= 0); nMockTime.store(nMockTimeIn, std::memory_order_relaxed); } diff --git a/test/functional/abc_rpc_mocktime.py b/test/functional/abc_rpc_mocktime.py deleted file mode 100755 --- a/test/functional/abc_rpc_mocktime.py +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) 2020 The Bitcoin developers -# Distributed under the MIT software license, see the accompanying -# file COPYING or http://www.opensource.org/licenses/mit-license.php. -""" -Test RPCs related to mock time. -""" - -from test_framework.test_framework import BitcoinTestFramework -from test_framework.util import assert_raises_rpc_error - - -class MocktimeTest(BitcoinTestFramework): - def set_test_params(self): - self.num_nodes = 1 - self.setup_clean_chain = True - - def run_test(self): - self.nodes[0].setmocktime(9223372036854775807) - self.nodes[0].setmocktime(0) - assert_raises_rpc_error(-8, "Timestamp must be 0 or greater", - self.nodes[0].setmocktime, -1) - assert_raises_rpc_error(-8, "Timestamp must be 0 or greater", - self.nodes[0].setmocktime, -9223372036854775808) - - -if __name__ == '__main__': - MocktimeTest().main() diff --git a/test/functional/rpc_uptime.py b/test/functional/rpc_uptime.py --- a/test/functional/rpc_uptime.py +++ b/test/functional/rpc_uptime.py @@ -10,6 +10,7 @@ import time from test_framework.test_framework import BitcoinTestFramework +from test_framework.util import assert_raises_rpc_error class UptimeTest(BitcoinTestFramework): @@ -18,8 +19,19 @@ self.setup_clean_chain = True def run_test(self): + self._test_negative_time() self._test_uptime() + def _test_negative_time(self): + self.nodes[0].setmocktime(9223372036854775807) + self.nodes[0].setmocktime(0) + assert_raises_rpc_error( + -8, "Mocktime can not be negative: -1.", + self.nodes[0].setmocktime, -1) + assert_raises_rpc_error( + -8, "Mocktime can not be negative: -9223372036854775808.", + self.nodes[0].setmocktime, -9223372036854775808) + def _test_uptime(self): wait_time = 10 self.nodes[0].setmocktime(int(time.time() + wait_time))