From 948d80b1988264b0fb6ef009b1a989511f08227e Mon Sep 17 00:00:00 2001 From: Ricardo Velhote Date: Mon, 3 Jul 2017 00:04:40 +0100 Subject: [PATCH] New RPC command to display the uptime of the server Version 0.15.0 of Bitcoin Core will include a new RPC command that will allow us to obtain the amount of time (in seconds) that the server has been running. --- btcjson/chainsvrcmds.go | 9 +++++++++ btcjson/chainsvrcmds_test.go | 11 +++++++++++ rpcserver.go | 7 +++++++ rpcserverhelp.go | 5 +++++ server.go | 4 ++++ 5 files changed, 36 insertions(+) diff --git a/btcjson/chainsvrcmds.go b/btcjson/chainsvrcmds.go index ad68467a..83c417eb 100644 --- a/btcjson/chainsvrcmds.go +++ b/btcjson/chainsvrcmds.go @@ -671,6 +671,14 @@ func NewSubmitBlockCmd(hexBlock string, options *SubmitBlockOptions) *SubmitBloc } } +// UptimeCmd defines the uptime JSON-RPC command. +type UptimeCmd struct{} + +// NewUptimeCmd returns a new instance which can be used to issue an uptime JSON-RPC command. +func NewUptimeCmd() *UptimeCmd { + return &UptimeCmd{} +} + // ValidateAddressCmd defines the validateaddress JSON-RPC command. type ValidateAddressCmd struct { Address string @@ -777,6 +785,7 @@ func init() { MustRegisterCmd("setgenerate", (*SetGenerateCmd)(nil), flags) MustRegisterCmd("stop", (*StopCmd)(nil), flags) MustRegisterCmd("submitblock", (*SubmitBlockCmd)(nil), flags) + MustRegisterCmd("uptime", (*UptimeCmd)(nil), flags) MustRegisterCmd("validateaddress", (*ValidateAddressCmd)(nil), flags) MustRegisterCmd("verifychain", (*VerifyChainCmd)(nil), flags) MustRegisterCmd("verifymessage", (*VerifyMessageCmd)(nil), flags) diff --git a/btcjson/chainsvrcmds_test.go b/btcjson/chainsvrcmds_test.go index 43bf84a8..d623012b 100644 --- a/btcjson/chainsvrcmds_test.go +++ b/btcjson/chainsvrcmds_test.go @@ -959,6 +959,17 @@ func TestChainSvrCmds(t *testing.T) { }, }, }, + { + name: "uptime", + newCmd: func() (interface{}, error) { + return btcjson.NewCmd("uptime") + }, + staticCmd: func() interface{} { + return btcjson.NewUptimeCmd() + }, + marshalled: `{"jsonrpc":"1.0","method":"uptime","params":[],"id":1}`, + unmarshalled: &btcjson.UptimeCmd{}, + }, { name: "validateaddress", newCmd: func() (interface{}, error) { diff --git a/rpcserver.go b/rpcserver.go index 46fbe35e..0da988a0 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -163,6 +163,7 @@ var rpcHandlersBeforeInit = map[string]commandHandler{ "setgenerate": handleSetGenerate, "stop": handleStop, "submitblock": handleSubmitBlock, + "uptime": handleUptime, "validateaddress": handleValidateAddress, "verifychain": handleVerifyChain, "verifymessage": handleVerifyMessage, @@ -267,6 +268,7 @@ var rpcLimited = map[string]struct{}{ "searchrawtransactions": {}, "sendrawtransaction": {}, "submitblock": {}, + "uptime": {}, "validateaddress": {}, "verifymessage": {}, "version": {}, @@ -3313,6 +3315,11 @@ func handleSubmitBlock(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) return nil, nil } +// handleUptime implements the uptime command. +func handleUptime(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) { + return time.Now().Unix() - s.server.startupTime, nil +} + // handleValidateAddress implements the validateaddress command. func handleValidateAddress(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) { c := cmd.(*btcjson.ValidateAddressCmd) diff --git a/rpcserverhelp.go b/rpcserverhelp.go index c090413c..03d927d2 100644 --- a/rpcserverhelp.go +++ b/rpcserverhelp.go @@ -615,6 +615,10 @@ var helpDescsEnUS = map[string]string{ "rescannedblock-hash": "Hash of the matching block.", "rescannedblock-transactions": "List of matching transactions, serialized and hex-encoded.", + // Uptime help. + "uptime--synopsis": "Returns the total uptime of the server.", + "uptime--result0": "The number of seconds that the server has been running", + // Version help. "version--synopsis": "Returns the JSON-RPC API version (semver)", "version--result0--desc": "Version objects keyed by the program or API name", @@ -672,6 +676,7 @@ var rpcResultTypes = map[string][]interface{}{ "setgenerate": nil, "stop": {(*string)(nil)}, "submitblock": {nil, (*string)(nil)}, + "uptime": {(*int64)(nil)}, "validateaddress": {(*btcjson.ValidateAddressChainResult)(nil)}, "verifychain": {(*bool)(nil)}, "verifymessage": {(*bool)(nil)}, diff --git a/server.go b/server.go index 0f3fad74..97c20bac 100644 --- a/server.go +++ b/server.go @@ -167,6 +167,7 @@ type server struct { started int32 shutdown int32 shutdownSched int32 + startupTime int64 chainParams *chaincfg.Params addrManager *addrmgr.AddrManager @@ -2003,6 +2004,9 @@ func (s *server) Start() { srvrLog.Trace("Starting server") + // Server startup time. Used for the uptime command for uptime calculation. + s.startupTime = time.Now().Unix() + // Start the peer handler which in turn starts the address and block // managers. s.wg.Add(1)