Merge #10400: [RPC] Add an uptime command that displays the amount of time (in seconds) bitcoind has been running

c074752 [RPC] Add an uptime command that displays the amount of time that bitcoind has been running (Ricardo Velhote)

Tree-SHA512: 8f59d4205042885f23f5b87a0eae0f5d386e9c6134e5324598e7ee304728d4275f383cd154bf1fb25350f5a88cc0ed9f97edb099e9b50c4a0ba72d63ec5ca5b4
This commit is contained in:
Wladimir J. van der Laan 2017-06-27 11:33:32 +02:00 committed by Pasta
parent 677c78516f
commit 41b3869031
No known key found for this signature in database
GPG Key ID: D362C9F7142766AE
6 changed files with 63 additions and 3 deletions

View File

@ -32,7 +32,6 @@
class CBlockIndex; class CBlockIndex;
static const int64_t nClientStartupTime = GetTime();
static int64_t nLastHeaderTipUpdateNotification = 0; static int64_t nLastHeaderTipUpdateNotification = 0;
static int64_t nLastBlockTipUpdateNotification = 0; static int64_t nLastBlockTipUpdateNotification = 0;
@ -275,7 +274,7 @@ bool ClientModel::isReleaseVersion() const
QString ClientModel::formatClientStartupTime() const QString ClientModel::formatClientStartupTime() const
{ {
return QDateTime::fromTime_t(nClientStartupTime).toString(); return QDateTime::fromTime_t(GetStartupTime()).toString();
} }
QString ClientModel::dataDir() const QString ClientModel::dataDir() const

View File

@ -314,6 +314,22 @@ UniValue stop(const JSONRPCRequest& jsonRequest)
return "Dash Core server stopping"; 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 * Call Table
*/ */
@ -323,6 +339,7 @@ static const CRPCCommand vRPCCommands[] =
/* Overall control/query calls */ /* Overall control/query calls */
{ "control", "help", &help, true, {"command"} }, { "control", "help", &help, true, {"command"} },
{ "control", "stop", &stop, true, {} }, { "control", "stop", &stop, true, {} },
{ "control", "uptime", &uptime, true, {} },
}; };
CRPCTable::CRPCTable() CRPCTable::CRPCTable()

View File

@ -92,6 +92,8 @@
#include <openssl/rand.h> #include <openssl/rand.h>
#include <openssl/conf.h> #include <openssl/conf.h>
// Application startup time (used for uptime calculation)
const int64_t nStartupTime = GetTime();
//Dash only features //Dash only features
bool fMasternodeMode = false; 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;
}

View File

@ -6,7 +6,7 @@
/** /**
* Server/client environment: argument handling, config file parsing, * Server/client environment: argument handling, config file parsing,
* logging, thread wrappers * logging, thread wrappers, startup time
*/ */
#ifndef BITCOIN_UTIL_H #ifndef BITCOIN_UTIL_H
#define BITCOIN_UTIL_H #define BITCOIN_UTIL_H
@ -49,6 +49,9 @@ extern bool fMasternodeMode;
extern bool fLiteMode; extern bool fLiteMode;
extern int nWalletBackups; extern int nWalletBackups;
// Application startup time (used for uptime calculation)
int64_t GetStartupTime();
static const bool DEFAULT_LOGTIMEMICROS = false; static const bool DEFAULT_LOGTIMEMICROS = false;
static const bool DEFAULT_LOGIPS = false; static const bool DEFAULT_LOGIPS = false;
static const bool DEFAULT_LOGTIMESTAMPS = true; static const bool DEFAULT_LOGTIMESTAMPS = true;

View File

@ -126,6 +126,7 @@ BASE_SCRIPTS= [
'sporks.py', 'sporks.py',
'p2p-fingerprint.py', 'p2p-fingerprint.py',
'wallet-encryption.py', 'wallet-encryption.py',
'uptime.py',
] ]
EXTENDED_SCRIPTS = [ EXTENDED_SCRIPTS = [

32
test/functional/uptime.py Executable file
View File

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