aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2014-05-06 16:34:31 +0200
committerWladimir J. van der Laan <laanwj@gmail.com>2014-05-06 16:36:55 +0200
commitacc2d2ca5f7dadcf9807ed1bfcac6c010fe3649d (patch)
tree0a60cf794c0f9568b9ced1797c137b2878819c51
parentb733288d95842d13a6559477bc9e8b5a93457ef0 (diff)
parentd387b8ec15e553db9b9c370314ee359e981fb374 (diff)
Merge pull request #4127
d387b8e rpc: add `getblockchaininfo` and `getnetworkinfo` (Wladimir J. van der Laan)
-rw-r--r--src/net.cpp9
-rw-r--r--src/net.h7
-rw-r--r--src/rpcblockchain.cpp36
-rw-r--r--src/rpcnet.cpp51
-rw-r--r--src/rpcserver.cpp2
-rw-r--r--src/rpcserver.h2
6 files changed, 100 insertions, 7 deletions
diff --git a/src/net.cpp b/src/net.cpp
index 6369dd2924..6bde1e7999 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -44,18 +44,13 @@ static const int MAX_OUTBOUND_CONNECTIONS = 8;
bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false);
-struct LocalServiceInfo {
- int nScore;
- int nPort;
-};
-
//
// Global state variables
//
bool fDiscover = true;
uint64_t nLocalServices = NODE_NETWORK;
-static CCriticalSection cs_mapLocalHost;
-static map<CNetAddr, LocalServiceInfo> mapLocalHost;
+CCriticalSection cs_mapLocalHost;
+map<CNetAddr, LocalServiceInfo> mapLocalHost;
static bool vfReachable[NET_MAX] = {};
static bool vfLimited[NET_MAX] = {};
static CNode* pnodeLocalHost = NULL;
diff --git a/src/net.h b/src/net.h
index 6210ed80c1..729b1bcd57 100644
--- a/src/net.h
+++ b/src/net.h
@@ -116,6 +116,13 @@ extern CCriticalSection cs_vAddedNodes;
extern NodeId nLastNodeId;
extern CCriticalSection cs_nLastNodeId;
+struct LocalServiceInfo {
+ int nScore;
+ int nPort;
+};
+
+extern CCriticalSection cs_mapLocalHost;
+extern map<CNetAddr, LocalServiceInfo> mapLocalHost;
class CNodeStats
{
diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp
index 78a92ce1e9..1ed8a8e866 100644
--- a/src/rpcblockchain.cpp
+++ b/src/rpcblockchain.cpp
@@ -6,6 +6,7 @@
#include "rpcserver.h"
#include "main.h"
#include "sync.h"
+#include "checkpoints.h"
#include <stdint.h>
@@ -429,3 +430,38 @@ Value verifychain(const Array& params, bool fHelp)
return VerifyDB(nCheckLevel, nCheckDepth);
}
+Value getblockchaininfo(const Array& params, bool fHelp)
+{
+ if (fHelp || params.size() != 0)
+ throw runtime_error(
+ "getblockchaininfo\n"
+ "Returns an object containing various state info regarding block chain processing.\n"
+ "\nResult:\n"
+ "{\n"
+ " \"chain\": \"xxxx\", (string) current chain (main, testnet3, regtest)\n"
+ " \"blocks\": xxxxxx, (numeric) the current number of blocks processed in the server\n"
+ " \"bestblockhash\": \"...\", (string) the hash of the currently best block\n"
+ " \"difficulty\": xxxxxx, (numeric) the current difficulty\n"
+ " \"verificationprogress\": xxxx, (numeric) estimate of verification progress [0..1]\n"
+ " \"chainwork\": \"xxxx\" (string) total amount of work in active chain, in hexadecimal\n"
+ "}\n"
+ "\nExamples:\n"
+ + HelpExampleCli("getblockchaininfo", "")
+ + HelpExampleRpc("getblockchaininfo", "")
+ );
+
+ proxyType proxy;
+ GetProxy(NET_IPV4, proxy);
+
+ Object obj;
+ std::string chain = Params().DataDir();
+ if(chain.empty())
+ chain = "main";
+ obj.push_back(Pair("chain", chain));
+ obj.push_back(Pair("blocks", (int)chainActive.Height()));
+ obj.push_back(Pair("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex()));
+ obj.push_back(Pair("difficulty", (double)GetDifficulty()));
+ obj.push_back(Pair("verificationprogress", Checkpoints::GuessVerificationProgress(chainActive.Tip())));
+ obj.push_back(Pair("chainwork", chainActive.Tip()->nChainWork.GetHex()));
+ return obj;
+}
diff --git a/src/rpcnet.cpp b/src/rpcnet.cpp
index 1b56d9ae7d..573d6cd3f6 100644
--- a/src/rpcnet.cpp
+++ b/src/rpcnet.cpp
@@ -333,3 +333,54 @@ Value getnettotals(const Array& params, bool fHelp)
obj.push_back(Pair("timemillis", static_cast<boost::int64_t>(GetTimeMillis())));
return obj;
}
+
+Value getnetworkinfo(const Array& params, bool fHelp)
+{
+ if (fHelp || params.size() != 0)
+ throw runtime_error(
+ "getnetworkinfo\n"
+ "Returns an object containing various state info regarding P2P networking.\n"
+ "\nResult:\n"
+ "{\n"
+ " \"version\": xxxxx, (numeric) the server version\n"
+ " \"protocolversion\": xxxxx, (numeric) the protocol version\n"
+ " \"timeoffset\": xxxxx, (numeric) the time offset\n"
+ " \"connections\": xxxxx, (numeric) the number of connections\n"
+ " \"proxy\": \"host:port\", (string, optional) the proxy used by the server\n"
+ " \"relayfee\": x.xxxx, (numeric) minimum relay fee for non-free transactions in btc/kb\n"
+ " \"localaddresses\": [, (array) list of local addresses\n"
+ " \"address\": \"xxxx\", (string) network address\n"
+ " \"port\": xxx, (numeric) network port\n"
+ " \"score\": xxx (numeric) relative score\n"
+ " ]\n"
+ "}\n"
+ "\nExamples:\n"
+ + HelpExampleCli("getnetworkinfo", "")
+ + HelpExampleRpc("getnetworkinfo", "")
+ );
+
+ proxyType proxy;
+ GetProxy(NET_IPV4, proxy);
+
+ Object obj;
+ obj.push_back(Pair("version", (int)CLIENT_VERSION));
+ obj.push_back(Pair("protocolversion",(int)PROTOCOL_VERSION));
+ obj.push_back(Pair("timeoffset", (boost::int64_t)GetTimeOffset()));
+ obj.push_back(Pair("connections", (int)vNodes.size()));
+ obj.push_back(Pair("proxy", (proxy.first.IsValid() ? proxy.first.ToStringIPPort() : string())));
+ obj.push_back(Pair("relayfee", ValueFromAmount(CTransaction::nMinRelayTxFee)));
+ Array localAddresses;
+ {
+ LOCK(cs_mapLocalHost);
+ BOOST_FOREACH(const PAIRTYPE(CNetAddr, LocalServiceInfo) &item, mapLocalHost)
+ {
+ Object rec;
+ rec.push_back(Pair("address", item.first.ToString()));
+ rec.push_back(Pair("port", item.second.nPort));
+ rec.push_back(Pair("score", item.second.nScore));
+ localAddresses.push_back(rec);
+ }
+ }
+ obj.push_back(Pair("localaddresses", localAddresses));
+ return obj;
+}
diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp
index d771a42427..f78cb420f4 100644
--- a/src/rpcserver.cpp
+++ b/src/rpcserver.cpp
@@ -228,6 +228,7 @@ static const CRPCCommand vRPCCommands[] =
{ "stop", &stop, true, true, false },
/* P2P networking */
+ { "getnetworkinfo", &getnetworkinfo, true, false, false },
{ "addnode", &addnode, true, true, false },
{ "getaddednodeinfo", &getaddednodeinfo, true, true, false },
{ "getconnectioncount", &getconnectioncount, true, false, false },
@@ -236,6 +237,7 @@ static const CRPCCommand vRPCCommands[] =
{ "ping", &ping, true, false, false },
/* Block chain and UTXO */
+ { "getblockchaininfo", &getblockchaininfo, true, false, false },
{ "getbestblockhash", &getbestblockhash, true, false, false },
{ "getblockcount", &getblockcount, true, false, false },
{ "getblock", &getblock, false, false, false },
diff --git a/src/rpcserver.h b/src/rpcserver.h
index ea03c09bf6..1092c691be 100644
--- a/src/rpcserver.h
+++ b/src/rpcserver.h
@@ -164,6 +164,8 @@ extern json_spirit::Value encryptwallet(const json_spirit::Array& params, bool f
extern json_spirit::Value validateaddress(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value getinfo(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value getwalletinfo(const json_spirit::Array& params, bool fHelp);
+extern json_spirit::Value getblockchaininfo(const json_spirit::Array& params, bool fHelp);
+extern json_spirit::Value getnetworkinfo(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value getrawtransaction(const json_spirit::Array& params, bool fHelp); // in rcprawtransaction.cpp
extern json_spirit::Value listunspent(const json_spirit::Array& params, bool fHelp);