Skip to content

Commit 1680ee0

Browse files
committed
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
2 parents 7878353 + c074752 commit 1680ee0

File tree

6 files changed

+63
-3
lines changed

6 files changed

+63
-3
lines changed

src/qt/clientmodel.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
class CBlockIndex;
2828

29-
static const int64_t nClientStartupTime = GetTime();
3029
static int64_t nLastHeaderTipUpdateNotification = 0;
3130
static int64_t nLastBlockTipUpdateNotification = 0;
3231

@@ -238,7 +237,7 @@ bool ClientModel::isReleaseVersion() const
238237

239238
QString ClientModel::formatClientStartupTime() const
240239
{
241-
return QDateTime::fromTime_t(nClientStartupTime).toString();
240+
return QDateTime::fromTime_t(GetStartupTime()).toString();
242241
}
243242

244243
QString ClientModel::dataDir() const

src/rpc/server.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,22 @@ UniValue stop(const JSONRPCRequest& jsonRequest)
258258
return "Bitcoin server stopping";
259259
}
260260

261+
UniValue uptime(const JSONRPCRequest& jsonRequest)
262+
{
263+
if (jsonRequest.fHelp || jsonRequest.params.size() > 1)
264+
throw std::runtime_error(
265+
"uptime\n"
266+
"\nReturns the total uptime of the server.\n"
267+
"\nResult:\n"
268+
"ttt (numeric) The number of seconds that the server has been running\n"
269+
"\nExamples:\n"
270+
+ HelpExampleCli("uptime", "")
271+
+ HelpExampleRpc("uptime", "")
272+
);
273+
274+
return GetTime() - GetStartupTime();
275+
}
276+
261277
/**
262278
* Call Table
263279
*/
@@ -267,6 +283,7 @@ static const CRPCCommand vRPCCommands[] =
267283
/* Overall control/query calls */
268284
{ "control", "help", &help, true, {"command"} },
269285
{ "control", "stop", &stop, true, {} },
286+
{ "control", "uptime", &uptime, true, {} },
270287
};
271288

272289
CRPCTable::CRPCTable()

src/util.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
#include <openssl/rand.h>
8585
#include <openssl/conf.h>
8686

87+
// Application startup time (used for uptime calculation)
88+
const int64_t nStartupTime = GetTime();
8789

8890
const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
8991
const char * const BITCOIN_PID_FILENAME = "bitcoind.pid";
@@ -889,3 +891,9 @@ std::string CopyrightHolders(const std::string& strPrefix)
889891
}
890892
return strCopyrightHolders;
891893
}
894+
895+
// Obtain the application startup time (used for uptime calculation)
896+
int64_t GetStartupTime()
897+
{
898+
return nStartupTime;
899+
}

src/util.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
/**
77
* Server/client environment: argument handling, config file parsing,
8-
* logging, thread wrappers
8+
* logging, thread wrappers, startup time
99
*/
1010
#ifndef BITCOIN_UTIL_H
1111
#define BITCOIN_UTIL_H
@@ -29,6 +29,9 @@
2929

3030
#include <boost/signals2/signal.hpp>
3131

32+
// Application startup time (used for uptime calculation)
33+
int64_t GetStartupTime();
34+
3235
static const bool DEFAULT_LOGTIMEMICROS = false;
3336
static const bool DEFAULT_LOGIPS = false;
3437
static const bool DEFAULT_LOGTIMESTAMPS = true;

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
'listsinceblock.py',
114114
'p2p-leaktests.py',
115115
'wallet-encryption.py',
116+
'uptime.py',
116117
]
117118

118119
EXTENDED_SCRIPTS = [

test/functional/uptime.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2017 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Test the RPC call related to the uptime command.
6+
7+
Test corresponds to code in rpc/server.cpp.
8+
"""
9+
10+
import time
11+
12+
from test_framework.test_framework import BitcoinTestFramework
13+
14+
15+
class UptimeTest(BitcoinTestFramework):
16+
def __init__(self):
17+
super().__init__()
18+
19+
self.num_nodes = 1
20+
self.setup_clean_chain = True
21+
22+
def run_test(self):
23+
self._test_uptime()
24+
25+
def _test_uptime(self):
26+
wait_time = 10
27+
self.nodes[0].setmocktime(int(time.time() + wait_time))
28+
assert(self.nodes[0].uptime() >= wait_time)
29+
30+
31+
if __name__ == '__main__':
32+
UptimeTest().main()

0 commit comments

Comments
 (0)