diff --git a/src/qt/clientmodel.cpp b/src/qt/clientmodel.cpp index 70520446cf..df2a88aaf5 100644 --- a/src/qt/clientmodel.cpp +++ b/src/qt/clientmodel.cpp @@ -32,7 +32,6 @@ class CBlockIndex; -static const int64_t nClientStartupTime = GetTime(); static int64_t nLastHeaderTipUpdateNotification = 0; static int64_t nLastBlockTipUpdateNotification = 0; @@ -275,7 +274,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 241c456f34..b02c4bacb1 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -314,6 +314,22 @@ UniValue stop(const JSONRPCRequest& jsonRequest) return "Dash Core 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 */ @@ -323,6 +339,7 @@ static const CRPCCommand vRPCCommands[] = /* Overall control/query calls */ { "control", "help", &help, true, {"command"} }, { "control", "stop", &stop, true, {} }, + { "control", "uptime", &uptime, true, {} }, }; CRPCTable::CRPCTable() diff --git a/src/util.cpp b/src/util.cpp index 78262772d2..a63a8afcb3 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -92,6 +92,8 @@ #include #include +// Application startup time (used for uptime calculation) +const int64_t nStartupTime = GetTime(); //Dash only features bool fMasternodeMode = false; @@ -1099,3 +1101,9 @@ std::string SafeIntVersionToString(uint32_t nVersion) } } + +// 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 b0ee0b2062..0804facb09 100644 --- a/src/util.h +++ b/src/util.h @@ -6,7 +6,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 @@ -49,6 +49,9 @@ extern bool fMasternodeMode; extern bool fLiteMode; extern int nWalletBackups; +// 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; diff --git a/test/functional/test_runner.py b/test/functional/test_runner.py index 08734e402d..0db0ca310a 100755 --- a/test/functional/test_runner.py +++ b/test/functional/test_runner.py @@ -126,6 +126,7 @@ BASE_SCRIPTS= [ 'sporks.py', 'p2p-fingerprint.py', 'wallet-encryption.py', + 'uptime.py', ] EXTENDED_SCRIPTS = [ diff --git a/test/functional/uptime.py b/test/functional/uptime.py new file mode 100755 index 0000000000..b20d6f5cb6 --- /dev/null +++ b/test/functional/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()