diff --git a/qa/pull-tester/rpc-tests.py b/qa/pull-tester/rpc-tests.py index f07e55d44..7b220b5b1 100755 --- a/qa/pull-tester/rpc-tests.py +++ b/qa/pull-tester/rpc-tests.py @@ -116,6 +116,7 @@ testScripts = [ 'p2p-versionbits-warning.py', 'importprunedfunds.py', 'signmessages.py', + 'uptime.py', ] testScriptsExt = [ 'bip9-softforks.py', diff --git a/qa/rpc-tests/uptime.py b/qa/rpc-tests/uptime.py new file mode 100644 index 000000000..b20d6f5cb --- /dev/null +++ b/qa/rpc-tests/uptime.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +# Copyright (c) 2017 The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. +"""Test the RPC call related to the uptime command. + +Test corresponds to code in rpc/server.cpp. +""" + +import time + +from test_framework.test_framework import BitcoinTestFramework + + +class UptimeTest(BitcoinTestFramework): + def __init__(self): + super().__init__() + + self.num_nodes = 1 + self.setup_clean_chain = True + + def run_test(self): + self._test_uptime() + + def _test_uptime(self): + wait_time = 10 + self.nodes[0].setmocktime(int(time.time() + wait_time)) + assert(self.nodes[0].uptime() >= wait_time) + + +if __name__ == '__main__': + UptimeTest().main() diff --git a/src/qt/clientmodel.cpp b/src/qt/clientmodel.cpp index f77c161bd..ebb353a37 100644 --- a/src/qt/clientmodel.cpp +++ b/src/qt/clientmodel.cpp @@ -24,7 +24,6 @@ class CBlockIndex; -static const int64_t nClientStartupTime = GetTime(); static int64_t nLastHeaderTipUpdateNotification = 0; static int64_t nLastBlockTipUpdateNotification = 0; @@ -182,7 +181,7 @@ bool ClientModel::isReleaseVersion() const QString ClientModel::formatClientStartupTime() const { - return QDateTime::fromTime_t(nClientStartupTime).toString(); + return QDateTime::fromTime_t(GetStartupTime()).toString(); } QString ClientModel::dataDir() const diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index 1c85c48f1..f542f54f9 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -273,6 +273,22 @@ UniValue stop(const UniValue& params, bool fHelp) return "Blackcoin server stopping"; } +UniValue uptime(const JSONRPCRequest& jsonRequest) +{ + if (jsonRequest.fHelp || jsonRequest.params.size() > 1) + throw std::runtime_error( + "uptime\n" + "\nReturns the total uptime of the server.\n" + "\nResult:\n" + "ttt (numeric) The number of seconds that the server has been running\n" + "\nExamples:\n" + + HelpExampleCli("uptime", "") + + HelpExampleRpc("uptime", "") + ); + + return GetTime() - GetStartupTime(); +} + /** * Call Table */ @@ -282,6 +298,7 @@ static const CRPCCommand vRPCCommands[] = /* Overall control/query calls */ { "control", "help", &help, true }, { "control", "stop", &stop, true }, + { "control", "uptime", &uptime, true, }, }; CRPCTable::CRPCTable() diff --git a/src/util.cpp b/src/util.cpp index 95d392a2c..a9c085116 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -97,6 +97,9 @@ namespace boost { } // namespace boost +// Application startup time (used for uptime calculation) +const int64_t nStartupTime = GetTime(); + using namespace std; const char * const BITCOIN_CONF_FILENAME = "blackmore.conf"; @@ -836,3 +839,9 @@ int GetNumCores() #endif } +// Obtain the application startup time (used for uptime calculation) +int64_t GetStartupTime() +{ + return nStartupTime; +} + diff --git a/src/util.h b/src/util.h index 4d3c029e9..69e595076 100644 --- a/src/util.h +++ b/src/util.h @@ -5,7 +5,7 @@ /** * Server/client environment: argument handling, config file parsing, - * logging, thread wrappers + * logging, thread wrappers, startup time */ #ifndef BITCOIN_UTIL_H #define BITCOIN_UTIL_H @@ -28,6 +28,9 @@ #include #include +// Application startup time (used for uptime calculation) +int64_t GetStartupTime(); + static const bool DEFAULT_LOGTIMEMICROS = false; static const bool DEFAULT_LOGIPS = false; static const bool DEFAULT_LOGTIMESTAMPS = true;