[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>
This commit is contained in:
Ricardo Velhote 2017-05-14 19:18:26 +01:00 committed by danielw86dev
parent 5eaff1e096
commit 69a7fc7cd6
7 changed files with 68 additions and 3 deletions

View File

@ -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 | |

View File

@ -179,6 +179,7 @@ testScripts = [
'getblockstats.py',
'addnode.py',
'getmocktime.py',
'uptime.py',
]
if ENABLE_ZMQ:
testScripts.append('zmq_test.py')

35
qa/rpc-tests/uptime.py Normal file
View File

@ -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()

View File

@ -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

View File

@ -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()

View File

@ -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;
}

View File

@ -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 <boost/signals2/signal.hpp>
#include <boost/thread/exceptions.hpp>
// 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;