From 69a7fc7cd67c44ec74c86a4f8871228fb30bc4b5 Mon Sep 17 00:00:00 2001 From: Ricardo Velhote Date: Sun, 14 May 2017 19:18:26 +0100 Subject: [PATCH] [RPC] Add an uptime command that displays the amount of time that bitcoind has been running Cherry-picked from: c07475294a Resolved conflict: - change path to qa\rpc-tests\uptime.py and qa\pull-tester\rpc-tests.py - src\util.cpp Manually executed: - qa\rpc-tests\uptime.py: overridden setup_network(), skip connecting nodes - doc\rpc-maturity.md: add uptime rpc Co-Authored-By: danielw86dev <67072678+danielw86dev@users.noreply.github.com> --- doc/rpc-maturity.md | 1 + qa/pull-tester/rpc-tests.py | 1 + qa/rpc-tests/uptime.py | 35 +++++++++++++++++++++++++++++++++++ src/qt/clientmodel.cpp | 3 +-- src/rpc/server.cpp | 17 +++++++++++++++++ src/util.cpp | 9 +++++++++ src/util.h | 5 ++++- 7 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 qa/rpc-tests/uptime.py diff --git a/doc/rpc-maturity.md b/doc/rpc-maturity.md index 93c265daa..d71d38959 100644 --- a/doc/rpc-maturity.md +++ b/doc/rpc-maturity.md @@ -119,6 +119,7 @@ Core RPC. Maturity is expressed over 3 stages: | stop | STABLE | | | submitauxblock | STABLE | | | submitblock | STABLE | | +| uptime | STABLE | Introduced in 1.15.0 | | validateaddress | STABLE | | | verifychain | STABLE | | | verifymessage | STABLE | | diff --git a/qa/pull-tester/rpc-tests.py b/qa/pull-tester/rpc-tests.py index b267c4a6c..0aeef688d 100755 --- a/qa/pull-tester/rpc-tests.py +++ b/qa/pull-tester/rpc-tests.py @@ -179,6 +179,7 @@ testScripts = [ 'getblockstats.py', 'addnode.py', 'getmocktime.py', + 'uptime.py', ] if ENABLE_ZMQ: testScripts.append('zmq_test.py') diff --git a/qa/rpc-tests/uptime.py b/qa/rpc-tests/uptime.py new file mode 100644 index 000000000..195079c03 --- /dev/null +++ b/qa/rpc-tests/uptime.py @@ -0,0 +1,35 @@ +#!/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 setup_network(self): + self.nodes = self.setup_nodes() + + 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 20ef602a9..88440c285 100644 --- a/src/qt/clientmodel.cpp +++ b/src/qt/clientmodel.cpp @@ -28,7 +28,6 @@ class CBlockIndex; -static const int64_t nClientStartupTime = GetTime(); static int64_t nLastHeaderTipUpdateNotification = 0; static int64_t nLastBlockTipUpdateNotification = 0; @@ -240,7 +239,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 32da9e777..051f736d5 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -277,6 +277,22 @@ UniValue stop(const JSONRPCRequest& jsonRequest) return "Dogecoin 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 */ @@ -286,6 +302,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 dc5d818ec..67fd923a8 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -104,6 +104,9 @@ namespace boost { using namespace std; +// Application startup time (used for uptime calculation) +const int64_t nStartupTime = GetTime(); + const char * const BITCOIN_CONF_FILENAME = "dogecoin.conf"; const char * const BITCOIN_PID_FILENAME = "dogecoind.pid"; @@ -893,3 +896,9 @@ std::string CopyrightHolders(const std::string& strPrefix) } return strCopyrightHolders; } + +// 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 6f0693053..20dd39b43 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 @@ -30,6 +30,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;