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); bool ChronoSanityCheck() { @@ -90,7 +92,7 @@ } 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 --- a/test/functional/abc_rpc_mocktime.py +++ b/test/functional/abc_rpc_mocktime.py @@ -18,10 +18,11 @@ 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", + assert_raises_rpc_error(-8, "Mocktime can not be negative: -1.", self.nodes[0].setmocktime, -1) - assert_raises_rpc_error(-8, "Timestamp must be 0 or greater", - self.nodes[0].setmocktime, -9223372036854775808) + assert_raises_rpc_error( + -8, "Mocktime can not be negative: -9223372036854775808.", + self.nodes[0].setmocktime, -9223372036854775808) if __name__ == '__main__':