Skip to content

Commit acc2d2c

Browse files
committed
Merge pull request #4127
d387b8e rpc: add `getblockchaininfo` and `getnetworkinfo` (Wladimir J. van der Laan)
2 parents b733288 + d387b8e commit acc2d2c

File tree

6 files changed

+100
-7
lines changed

6 files changed

+100
-7
lines changed

src/net.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,13 @@ static const int MAX_OUTBOUND_CONNECTIONS = 8;
4444
bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false);
4545

4646

47-
struct LocalServiceInfo {
48-
int nScore;
49-
int nPort;
50-
};
51-
5247
//
5348
// Global state variables
5449
//
5550
bool fDiscover = true;
5651
uint64_t nLocalServices = NODE_NETWORK;
57-
static CCriticalSection cs_mapLocalHost;
58-
static map<CNetAddr, LocalServiceInfo> mapLocalHost;
52+
CCriticalSection cs_mapLocalHost;
53+
map<CNetAddr, LocalServiceInfo> mapLocalHost;
5954
static bool vfReachable[NET_MAX] = {};
6055
static bool vfLimited[NET_MAX] = {};
6156
static CNode* pnodeLocalHost = NULL;

src/net.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ extern CCriticalSection cs_vAddedNodes;
116116
extern NodeId nLastNodeId;
117117
extern CCriticalSection cs_nLastNodeId;
118118

119+
struct LocalServiceInfo {
120+
int nScore;
121+
int nPort;
122+
};
123+
124+
extern CCriticalSection cs_mapLocalHost;
125+
extern map<CNetAddr, LocalServiceInfo> mapLocalHost;
119126

120127
class CNodeStats
121128
{

src/rpcblockchain.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "rpcserver.h"
77
#include "main.h"
88
#include "sync.h"
9+
#include "checkpoints.h"
910

1011
#include <stdint.h>
1112

@@ -429,3 +430,38 @@ Value verifychain(const Array& params, bool fHelp)
429430
return VerifyDB(nCheckLevel, nCheckDepth);
430431
}
431432

433+
Value getblockchaininfo(const Array& params, bool fHelp)
434+
{
435+
if (fHelp || params.size() != 0)
436+
throw runtime_error(
437+
"getblockchaininfo\n"
438+
"Returns an object containing various state info regarding block chain processing.\n"
439+
"\nResult:\n"
440+
"{\n"
441+
" \"chain\": \"xxxx\", (string) current chain (main, testnet3, regtest)\n"
442+
" \"blocks\": xxxxxx, (numeric) the current number of blocks processed in the server\n"
443+
" \"bestblockhash\": \"...\", (string) the hash of the currently best block\n"
444+
" \"difficulty\": xxxxxx, (numeric) the current difficulty\n"
445+
" \"verificationprogress\": xxxx, (numeric) estimate of verification progress [0..1]\n"
446+
" \"chainwork\": \"xxxx\" (string) total amount of work in active chain, in hexadecimal\n"
447+
"}\n"
448+
"\nExamples:\n"
449+
+ HelpExampleCli("getblockchaininfo", "")
450+
+ HelpExampleRpc("getblockchaininfo", "")
451+
);
452+
453+
proxyType proxy;
454+
GetProxy(NET_IPV4, proxy);
455+
456+
Object obj;
457+
std::string chain = Params().DataDir();
458+
if(chain.empty())
459+
chain = "main";
460+
obj.push_back(Pair("chain", chain));
461+
obj.push_back(Pair("blocks", (int)chainActive.Height()));
462+
obj.push_back(Pair("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex()));
463+
obj.push_back(Pair("difficulty", (double)GetDifficulty()));
464+
obj.push_back(Pair("verificationprogress", Checkpoints::GuessVerificationProgress(chainActive.Tip())));
465+
obj.push_back(Pair("chainwork", chainActive.Tip()->nChainWork.GetHex()));
466+
return obj;
467+
}

src/rpcnet.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,54 @@ Value getnettotals(const Array& params, bool fHelp)
333333
obj.push_back(Pair("timemillis", static_cast<boost::int64_t>(GetTimeMillis())));
334334
return obj;
335335
}
336+
337+
Value getnetworkinfo(const Array& params, bool fHelp)
338+
{
339+
if (fHelp || params.size() != 0)
340+
throw runtime_error(
341+
"getnetworkinfo\n"
342+
"Returns an object containing various state info regarding P2P networking.\n"
343+
"\nResult:\n"
344+
"{\n"
345+
" \"version\": xxxxx, (numeric) the server version\n"
346+
" \"protocolversion\": xxxxx, (numeric) the protocol version\n"
347+
" \"timeoffset\": xxxxx, (numeric) the time offset\n"
348+
" \"connections\": xxxxx, (numeric) the number of connections\n"
349+
" \"proxy\": \"host:port\", (string, optional) the proxy used by the server\n"
350+
" \"relayfee\": x.xxxx, (numeric) minimum relay fee for non-free transactions in btc/kb\n"
351+
" \"localaddresses\": [, (array) list of local addresses\n"
352+
" \"address\": \"xxxx\", (string) network address\n"
353+
" \"port\": xxx, (numeric) network port\n"
354+
" \"score\": xxx (numeric) relative score\n"
355+
" ]\n"
356+
"}\n"
357+
"\nExamples:\n"
358+
+ HelpExampleCli("getnetworkinfo", "")
359+
+ HelpExampleRpc("getnetworkinfo", "")
360+
);
361+
362+
proxyType proxy;
363+
GetProxy(NET_IPV4, proxy);
364+
365+
Object obj;
366+
obj.push_back(Pair("version", (int)CLIENT_VERSION));
367+
obj.push_back(Pair("protocolversion",(int)PROTOCOL_VERSION));
368+
obj.push_back(Pair("timeoffset", (boost::int64_t)GetTimeOffset()));
369+
obj.push_back(Pair("connections", (int)vNodes.size()));
370+
obj.push_back(Pair("proxy", (proxy.first.IsValid() ? proxy.first.ToStringIPPort() : string())));
371+
obj.push_back(Pair("relayfee", ValueFromAmount(CTransaction::nMinRelayTxFee)));
372+
Array localAddresses;
373+
{
374+
LOCK(cs_mapLocalHost);
375+
BOOST_FOREACH(const PAIRTYPE(CNetAddr, LocalServiceInfo) &item, mapLocalHost)
376+
{
377+
Object rec;
378+
rec.push_back(Pair("address", item.first.ToString()));
379+
rec.push_back(Pair("port", item.second.nPort));
380+
rec.push_back(Pair("score", item.second.nScore));
381+
localAddresses.push_back(rec);
382+
}
383+
}
384+
obj.push_back(Pair("localaddresses", localAddresses));
385+
return obj;
386+
}

src/rpcserver.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ static const CRPCCommand vRPCCommands[] =
228228
{ "stop", &stop, true, true, false },
229229

230230
/* P2P networking */
231+
{ "getnetworkinfo", &getnetworkinfo, true, false, false },
231232
{ "addnode", &addnode, true, true, false },
232233
{ "getaddednodeinfo", &getaddednodeinfo, true, true, false },
233234
{ "getconnectioncount", &getconnectioncount, true, false, false },
@@ -236,6 +237,7 @@ static const CRPCCommand vRPCCommands[] =
236237
{ "ping", &ping, true, false, false },
237238

238239
/* Block chain and UTXO */
240+
{ "getblockchaininfo", &getblockchaininfo, true, false, false },
239241
{ "getbestblockhash", &getbestblockhash, true, false, false },
240242
{ "getblockcount", &getblockcount, true, false, false },
241243
{ "getblock", &getblock, false, false, false },

src/rpcserver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ extern json_spirit::Value encryptwallet(const json_spirit::Array& params, bool f
164164
extern json_spirit::Value validateaddress(const json_spirit::Array& params, bool fHelp);
165165
extern json_spirit::Value getinfo(const json_spirit::Array& params, bool fHelp);
166166
extern json_spirit::Value getwalletinfo(const json_spirit::Array& params, bool fHelp);
167+
extern json_spirit::Value getblockchaininfo(const json_spirit::Array& params, bool fHelp);
168+
extern json_spirit::Value getnetworkinfo(const json_spirit::Array& params, bool fHelp);
167169

168170
extern json_spirit::Value getrawtransaction(const json_spirit::Array& params, bool fHelp); // in rcprawtransaction.cpp
169171
extern json_spirit::Value listunspent(const json_spirit::Array& params, bool fHelp);

0 commit comments

Comments
 (0)