Skip to content

Commit 12242b1

Browse files
committed
cli: create initial -netinfo option, NetinfoRequestHandler class
1 parent 6eb97fb commit 12242b1

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/bitcoin-cli.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ static void SetupCliArgs(ArgsManager& argsman)
5656
argsman.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
5757
argsman.AddArg("-generate", strprintf("Generate blocks immediately, equivalent to RPC generatenewaddress followed by RPC generatetoaddress. Optional positional integer arguments are number of blocks to generate (default: %s) and maximum iterations to try (default: %s), equivalent to RPC generatetoaddress nblocks and maxtries arguments. Example: bitcoin-cli -generate 4 1000", DEFAULT_NBLOCKS, DEFAULT_MAX_TRIES), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
5858
argsman.AddArg("-getinfo", "Get general information from the remote server. Note that unlike server-side RPC calls, the results of -getinfo is the result of multiple non-atomic requests. Some entries in the result may represent results from different states (e.g. wallet balance may be as of a different block from the chain state reported)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
59+
argsman.AddArg("-netinfo", "Get network peer connection information from the remote server. An optional boolean argument can be passed for a detailed peers listing (default: false).", ArgsManager::ALLOW_BOOL, OptionsCategory::OPTIONS);
60+
5961
SetupChainParamsBaseOptions(argsman);
6062
argsman.AddArg("-named", strprintf("Pass named instead of positional arguments (default: %s)", DEFAULT_NAMED), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
6163
argsman.AddArg("-rpcclienttimeout=<n>", strprintf("Timeout in seconds during HTTP requests, or 0 for no timeout. (default: %d)", DEFAULT_HTTP_CLIENT_TIMEOUT), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
@@ -291,6 +293,37 @@ class GetinfoRequestHandler: public BaseRequestHandler
291293
}
292294
};
293295

296+
/** Process netinfo requests */
297+
class NetinfoRequestHandler : public BaseRequestHandler
298+
{
299+
private:
300+
bool m_verbose{false}; //!< Whether user requested verbose -netinfo report
301+
public:
302+
const int ID_PEERINFO = 0;
303+
const int ID_NETWORKINFO = 1;
304+
305+
UniValue PrepareRequest(const std::string& method, const std::vector<std::string>& args) override
306+
{
307+
if (!args.empty()) {
308+
const std::string arg{ToLower(args.at(0))};
309+
m_verbose = (arg == "true" || arg == "t");
310+
}
311+
UniValue result(UniValue::VARR);
312+
result.push_back(JSONRPCRequestObj("getpeerinfo", NullUniValue, ID_PEERINFO));
313+
result.push_back(JSONRPCRequestObj("getnetworkinfo", NullUniValue, ID_NETWORKINFO));
314+
return result;
315+
}
316+
317+
UniValue ProcessReply(const UniValue& batch_in) override
318+
{
319+
const std::vector<UniValue> batch{JSONRPCProcessBatchReply(batch_in)};
320+
if (!batch[ID_PEERINFO]["error"].isNull()) return batch[ID_PEERINFO];
321+
if (!batch[ID_NETWORKINFO]["error"].isNull()) return batch[ID_NETWORKINFO];
322+
std::string result;
323+
return JSONRPCReplyObj(UniValue{result}, NullUniValue, 1);
324+
}
325+
};
326+
294327
/** Process RPC generatetoaddress request. */
295328
class GenerateToAddressRequestHandler : public BaseRequestHandler
296329
{
@@ -618,6 +651,8 @@ static int CommandLineRPC(int argc, char *argv[])
618651
std::string method;
619652
if (gArgs.IsArgSet("-getinfo")) {
620653
rh.reset(new GetinfoRequestHandler());
654+
} else if (gArgs.GetBoolArg("-netinfo", false)) {
655+
rh.reset(new NetinfoRequestHandler());
621656
} else if (gArgs.GetBoolArg("-generate", false)) {
622657
const UniValue getnewaddress{GetNewAddress()};
623658
const UniValue& error{find_value(getnewaddress, "error")};

0 commit comments

Comments
 (0)