From 6a4b97e8633c64593d7d0c9bd18079e52bc03e82 Mon Sep 17 00:00:00 2001 From: Gavin Andresen Date: Fri, 19 Jun 2015 10:42:39 -0400 Subject: [PATCH] Testing infrastructure: mocktime fixes New, undocumented-on-purpose -mocktime=timestamp command-line argument to startup with mocktime set. Needed because time-related blockchain sanity checks are done on startup, before a test has a chance to make a setmocktime RPC call. And changed the setmocktime RPC call so calling it will not result in currently connected peers being disconnected due to inactivity timeouts. --- src/init.cpp | 3 +++ src/rpcmisc.cpp | 11 ++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/init.cpp b/src/init.cpp index bcdd1f2f3dc..c4e3573de3d 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -868,6 +868,9 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) fAlerts = GetBoolArg("-alerts", DEFAULT_ALERTS); + // Option to startup with mocktime set (used for regression testing): + SetMockTime(GetArg("-mocktime", 0)); // SetMockTime(0) is a no-op + // ********************************************************* Step 4: application initialization: dir lock, daemonize, pidfile, debug log // Initialize elliptic curve code diff --git a/src/rpcmisc.cpp b/src/rpcmisc.cpp index 1d47bc06a51..cab57d7027a 100644 --- a/src/rpcmisc.cpp +++ b/src/rpcmisc.cpp @@ -378,10 +378,19 @@ UniValue setmocktime(const UniValue& params, bool fHelp) if (!Params().MineBlocksOnDemand()) throw runtime_error("setmocktime for regression testing (-regtest mode) only"); - LOCK(cs_main); + // cs_vNodes is locked and node send/receive times are updated + // atomically with the time change to prevent peers from being + // disconnected because we think we haven't communicated with them + // in a long time. + LOCK2(cs_main, cs_vNodes); RPCTypeCheck(params, boost::assign::list_of(UniValue::VNUM)); SetMockTime(params[0].get_int64()); + uint64_t t = GetTime(); + BOOST_FOREACH(CNode* pnode, vNodes) { + pnode->nLastSend = pnode->nLastRecv = t; + } + return NullUniValue; }