Skip to content

Commit 7afdd72

Browse files
committed
cli: enable -netinfo help to run without a remote server
1 parent 4dc1ff8 commit 7afdd72

File tree

1 file changed

+65
-69
lines changed

1 file changed

+65
-69
lines changed

src/bitcoin-cli.cpp

Lines changed: 65 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,7 @@ class NetinfoRequestHandler : public BaseRequestHandler
314314
}
315315
return UNKNOWN_NETWORK;
316316
}
317-
uint8_t m_details_level{0}; //!< Optional user-supplied arg to set dashboard details level
318-
bool m_is_help_requested{false}; //!< Optional user-supplied arg to print help documentation
317+
uint8_t m_details_level{0}; //!< Optional user-supplied arg to set dashboard details level
319318
bool DetailsRequested() const { return m_details_level > 0 && m_details_level < 5; }
320319
bool IsAddressSelected() const { return m_details_level == 2 || m_details_level == 4; }
321320
bool IsVersionSelected() const { return m_details_level == 3 || m_details_level == 4; }
@@ -367,68 +366,6 @@ class NetinfoRequestHandler : public BaseRequestHandler
367366
if (conn_type == "addr-fetch") return "addr";
368367
return "";
369368
}
370-
const UniValue NetinfoHelp()
371-
{
372-
return std::string{
373-
"-netinfo level|\"help\" \n\n"
374-
"Returns a network peer connections dashboard with information from the remote server.\n"
375-
"Under the hood, -netinfo fetches the data by calling getpeerinfo and getnetworkinfo.\n"
376-
"An optional integer argument from 0 to 4 can be passed for different peers listings.\n"
377-
"Pass \"help\" to see this detailed help documentation.\n"
378-
"If more than one argument is passed, only the first one is read and parsed.\n"
379-
"Suggestion: use with the Linux watch(1) command for a live dashboard; see example below.\n\n"
380-
"Arguments:\n"
381-
"1. level (integer 0-4, optional) Specify the info level of the peers dashboard (default 0):\n"
382-
" 0 - Connection counts and local addresses\n"
383-
" 1 - Like 0 but with a peers listing (without address or version columns)\n"
384-
" 2 - Like 1 but with an address column\n"
385-
" 3 - Like 1 but with a version column\n"
386-
" 4 - Like 1 but with both address and version columns\n"
387-
"2. help (string \"help\", optional) Print this help documentation instead of the dashboard.\n\n"
388-
"Result:\n\n"
389-
"* The peers listing in levels 1-4 displays all of the peers sorted by direction and minimum ping time:\n\n"
390-
" Column Description\n"
391-
" ------ -----------\n"
392-
" <-> Direction\n"
393-
" \"in\" - inbound connections are those initiated by the peer\n"
394-
" \"out\" - outbound connections are those initiated by us\n"
395-
" type Type of peer connection\n"
396-
" \"full\" - full relay, the default\n"
397-
" \"block\" - block relay; like full relay but does not relay transactions or addresses\n"
398-
" \"manual\" - peer we manually added using RPC addnode or the -addnode/-connect config options\n"
399-
" \"feeler\" - short-lived connection for testing addresses\n"
400-
" \"addr\" - address fetch; short-lived connection for requesting addresses\n"
401-
" net Network the peer connected through (\"ipv4\", \"ipv6\", \"onion\", \"i2p\", or \"cjdns\")\n"
402-
" mping Minimum observed ping time, in milliseconds (ms)\n"
403-
" ping Last observed ping time, in milliseconds (ms)\n"
404-
" send Time since last message sent to the peer, in seconds\n"
405-
" recv Time since last message received from the peer, in seconds\n"
406-
" txn Time since last novel transaction received from the peer and accepted into our mempool, in minutes\n"
407-
" blk Time since last novel block passing initial validity checks received from the peer, in minutes\n"
408-
" hb High-bandwidth BIP152 compact block relay\n"
409-
" \".\" (to) - we selected the peer as a high-bandwidth peer\n"
410-
" \"*\" (from) - the peer selected us as a high-bandwidth peer\n"
411-
" age Duration of connection to the peer, in minutes\n"
412-
" asmap Mapped AS (Autonomous System) number in the BGP route to the peer, used for diversifying\n"
413-
" peer selection (only displayed if the -asmap config option is set)\n"
414-
" id Peer index, in increasing order of peer connections since node startup\n"
415-
" address IP address and port of the peer\n"
416-
" version Peer version and subversion concatenated, e.g. \"70016/Satoshi:21.0.0/\"\n\n"
417-
"* The connection counts table displays the number of peers by direction, network, and the totals\n"
418-
" for each, as well as two special outbound columns for block relay peers and manual peers.\n\n"
419-
"* The local addresses table lists each local address broadcast by the node, the port, and the score.\n\n"
420-
"Examples:\n\n"
421-
"Connection counts and local addresses only\n"
422-
"> bitcoin-cli -netinfo\n\n"
423-
"Compact peers listing\n"
424-
"> bitcoin-cli -netinfo 1\n\n"
425-
"Full dashboard\n"
426-
"> bitcoin-cli -netinfo 4\n\n"
427-
"Full live dashboard, adjust --interval or --no-title as needed (Linux)\n"
428-
"> watch --interval 1 --no-title bitcoin-cli -netinfo 4\n\n"
429-
"See this help\n"
430-
"> bitcoin-cli -netinfo help\n"};
431-
}
432369
const int64_t m_time_now{GetSystemTimeInSeconds()};
433370

434371
public:
@@ -441,8 +378,6 @@ class NetinfoRequestHandler : public BaseRequestHandler
441378
uint8_t n{0};
442379
if (ParseUInt8(args.at(0), &n)) {
443380
m_details_level = std::min(n, MAX_DETAIL_LEVEL);
444-
} else if (args.at(0) == "help") {
445-
m_is_help_requested = true;
446381
} else {
447382
throw std::runtime_error(strprintf("invalid -netinfo argument: %s", args.at(0)));
448383
}
@@ -455,9 +390,6 @@ class NetinfoRequestHandler : public BaseRequestHandler
455390

456391
UniValue ProcessReply(const UniValue& batch_in) override
457392
{
458-
if (m_is_help_requested) {
459-
return JSONRPCReplyObj(NetinfoHelp(), NullUniValue, 1);
460-
}
461393
const std::vector<UniValue> batch{JSONRPCProcessBatchReply(batch_in)};
462394
if (!batch[ID_PEERINFO]["error"].isNull()) return batch[ID_PEERINFO];
463395
if (!batch[ID_NETWORKINFO]["error"].isNull()) return batch[ID_NETWORKINFO];
@@ -576,6 +508,66 @@ class NetinfoRequestHandler : public BaseRequestHandler
576508

577509
return JSONRPCReplyObj(UniValue{result}, NullUniValue, 1);
578510
}
511+
512+
const std::string m_help_doc{
513+
"-netinfo level|\"help\" \n\n"
514+
"Returns a network peer connections dashboard with information from the remote server.\n"
515+
"Under the hood, -netinfo fetches the data by calling getpeerinfo and getnetworkinfo.\n"
516+
"An optional integer argument from 0 to 4 can be passed for different peers listings.\n"
517+
"Pass \"help\" to see this detailed help documentation.\n"
518+
"If more than one argument is passed, only the first one is read and parsed.\n"
519+
"Suggestion: use with the Linux watch(1) command for a live dashboard; see example below.\n\n"
520+
"Arguments:\n"
521+
"1. level (integer 0-4, optional) Specify the info level of the peers dashboard (default 0):\n"
522+
" 0 - Connection counts and local addresses\n"
523+
" 1 - Like 0 but with a peers listing (without address or version columns)\n"
524+
" 2 - Like 1 but with an address column\n"
525+
" 3 - Like 1 but with a version column\n"
526+
" 4 - Like 1 but with both address and version columns\n"
527+
"2. help (string \"help\", optional) Print this help documentation instead of the dashboard.\n\n"
528+
"Result:\n\n"
529+
"* The peers listing in levels 1-4 displays all of the peers sorted by direction and minimum ping time:\n\n"
530+
" Column Description\n"
531+
" ------ -----------\n"
532+
" <-> Direction\n"
533+
" \"in\" - inbound connections are those initiated by the peer\n"
534+
" \"out\" - outbound connections are those initiated by us\n"
535+
" type Type of peer connection\n"
536+
" \"full\" - full relay, the default\n"
537+
" \"block\" - block relay; like full relay but does not relay transactions or addresses\n"
538+
" \"manual\" - peer we manually added using RPC addnode or the -addnode/-connect config options\n"
539+
" \"feeler\" - short-lived connection for testing addresses\n"
540+
" \"addr\" - address fetch; short-lived connection for requesting addresses\n"
541+
" net Network the peer connected through (\"ipv4\", \"ipv6\", \"onion\", \"i2p\", or \"cjdns\")\n"
542+
" mping Minimum observed ping time, in milliseconds (ms)\n"
543+
" ping Last observed ping time, in milliseconds (ms)\n"
544+
" send Time since last message sent to the peer, in seconds\n"
545+
" recv Time since last message received from the peer, in seconds\n"
546+
" txn Time since last novel transaction received from the peer and accepted into our mempool, in minutes\n"
547+
" blk Time since last novel block passing initial validity checks received from the peer, in minutes\n"
548+
" hb High-bandwidth BIP152 compact block relay\n"
549+
" \".\" (to) - we selected the peer as a high-bandwidth peer\n"
550+
" \"*\" (from) - the peer selected us as a high-bandwidth peer\n"
551+
" age Duration of connection to the peer, in minutes\n"
552+
" asmap Mapped AS (Autonomous System) number in the BGP route to the peer, used for diversifying\n"
553+
" peer selection (only displayed if the -asmap config option is set)\n"
554+
" id Peer index, in increasing order of peer connections since node startup\n"
555+
" address IP address and port of the peer\n"
556+
" version Peer version and subversion concatenated, e.g. \"70016/Satoshi:21.0.0/\"\n\n"
557+
"* The connection counts table displays the number of peers by direction, network, and the totals\n"
558+
" for each, as well as two special outbound columns for block relay peers and manual peers.\n\n"
559+
"* The local addresses table lists each local address broadcast by the node, the port, and the score.\n\n"
560+
"Examples:\n\n"
561+
"Connection counts and local addresses only\n"
562+
"> bitcoin-cli -netinfo\n\n"
563+
"Compact peers listing\n"
564+
"> bitcoin-cli -netinfo 1\n\n"
565+
"Full dashboard\n"
566+
"> bitcoin-cli -netinfo 4\n\n"
567+
"Full live dashboard, adjust --interval or --no-title as needed (Linux)\n"
568+
"> watch --interval 1 --no-title bitcoin-cli -netinfo 4\n\n"
569+
"See this help\n"
570+
"> bitcoin-cli -netinfo help\n"};
579571
};
580572

581573
/** Process RPC generatetoaddress request. */
@@ -907,6 +899,10 @@ static int CommandLineRPC(int argc, char *argv[])
907899
if (gArgs.IsArgSet("-getinfo")) {
908900
rh.reset(new GetinfoRequestHandler());
909901
} else if (gArgs.GetBoolArg("-netinfo", false)) {
902+
if (!args.empty() && args.at(0) == "help") {
903+
tfm::format(std::cout, "%s\n", NetinfoRequestHandler().m_help_doc);
904+
return 0;
905+
}
910906
rh.reset(new NetinfoRequestHandler());
911907
} else if (gArgs.GetBoolArg("-generate", false)) {
912908
const UniValue getnewaddress{GetNewAddress()};

0 commit comments

Comments
 (0)