Skip to content

Commit 17f8f03

Browse files
committed
netinfo: allow setting an outbound-only peer list
by passing an additional argument of "outonly" or "o". This has been requested in order to keep the output within screen limits when running -netinfo as a live dashboard, i.e. with `watch`. Also allow passing "h" in addition to "help" to see the help documentation.
1 parent 681ebcc commit 17f8f03

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/bitcoin-cli.cpp

+25-9
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static void SetupCliArgs(ArgsManager& argsman)
9191
ArgsManager::ALLOW_ANY, OptionsCategory::CLI_COMMANDS);
9292
argsman.AddArg("-addrinfo", "Get the number of addresses known to the node, per network and total, after filtering for quality and recency. The total number of addresses known to the node may be higher.", ArgsManager::ALLOW_ANY, OptionsCategory::CLI_COMMANDS);
9393
argsman.AddArg("-getinfo", "Get general information from the remote server. Note that unlike server-side RPC calls, the output of -getinfo is the result of multiple non-atomic requests. Some entries in the output 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::CLI_COMMANDS);
94-
argsman.AddArg("-netinfo", strprintf("Get network peer connection information from the remote server. An optional integer argument from 0 to %d can be passed for different peers listings (default: 0). Pass \"help\" for detailed help documentation.", NETINFO_MAX_LEVEL), ArgsManager::ALLOW_ANY, OptionsCategory::CLI_COMMANDS);
94+
argsman.AddArg("-netinfo", strprintf("Get network peer connection information from the remote server. An optional argument from 0 to %d can be passed for different peers listings (default: 0). If a non-zero value is passed, an additional \"outonly\" (or \"o\") argument can be passed to see outbound peers only. Pass \"help\" (or \"h\") for detailed help documentation.", NETINFO_MAX_LEVEL), ArgsManager::ALLOW_ANY, OptionsCategory::CLI_COMMANDS);
9595

9696
SetupChainParamsBaseOptions(argsman);
9797
argsman.AddArg("-color=<when>", strprintf("Color setting for CLI output (default: %s). Valid values: always, auto (add color codes when standard output is connected to a terminal and OS is not WIN32), never.", DEFAULT_COLOR_SETTING), ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_NEGATION, OptionsCategory::OPTIONS);
@@ -394,6 +394,7 @@ class NetinfoRequestHandler : public BaseRequestHandler
394394
bool DetailsRequested() const { return m_details_level > 0 && m_details_level < 5; }
395395
bool IsAddressSelected() const { return m_details_level == 2 || m_details_level == 4; }
396396
bool IsVersionSelected() const { return m_details_level == 3 || m_details_level == 4; }
397+
bool m_outbound_only_selected{false};
397398
bool m_is_asmap_on{false};
398399
size_t m_max_addr_length{0};
399400
size_t m_max_addr_processed_length{5};
@@ -479,7 +480,16 @@ class NetinfoRequestHandler : public BaseRequestHandler
479480
if (ParseUInt8(args.at(0), &n)) {
480481
m_details_level = std::min(n, NETINFO_MAX_LEVEL);
481482
} else {
482-
throw std::runtime_error(strprintf("invalid -netinfo argument: %s\nFor more information, run: bitcoin-cli -netinfo help", args.at(0)));
483+
throw std::runtime_error(strprintf("invalid -netinfo level argument: %s\nFor more information, run: bitcoin-cli -netinfo help", args.at(0)));
484+
}
485+
if (args.size() > 1) {
486+
if (std::string_view s{args.at(1)}; n && (s == "o" || s == "outonly")) {
487+
m_outbound_only_selected = true;
488+
} else if (n) {
489+
throw std::runtime_error(strprintf("invalid -netinfo outonly argument: %s\nFor more information, run: bitcoin-cli -netinfo help", args.at(1)));
490+
} else {
491+
throw std::runtime_error(strprintf("invalid -netinfo outonly argument: %s\nThe outonly argument is only valid for details levels higher than 0. For more information, run: bitcoin-cli -netinfo help", args.at(1)));
492+
}
483493
}
484494
}
485495
UniValue result(UniValue::VARR);
@@ -514,6 +524,7 @@ class NetinfoRequestHandler : public BaseRequestHandler
514524
++m_counts.at(2).at(NETWORKS.size()); // total overall
515525
if (conn_type == "block-relay-only") ++m_block_relay_peers_count;
516526
if (conn_type == "manual") ++m_manual_peers_count;
527+
if (m_outbound_only_selected && !is_outbound) continue;
517528
if (DetailsRequested()) {
518529
// Push data for this peer to the peers vector.
519530
const int peer_id{peer["id"].getInt<int>()};
@@ -648,14 +659,15 @@ class NetinfoRequestHandler : public BaseRequestHandler
648659
}
649660

650661
const std::string m_help_doc{
651-
"-netinfo level|\"help\" \n\n"
662+
"-netinfo level|outonly|help\n\n"
652663
"Returns a network peer connections dashboard with information from the remote server.\n"
653664
"This human-readable interface will change regularly and is not intended to be a stable API.\n"
654665
"Under the hood, -netinfo fetches the data by calling getpeerinfo and getnetworkinfo.\n"
655-
+ strprintf("An optional integer argument from 0 to %d can be passed for different peers listings; values from %d to 255 are parsed as %d.\n", NETINFO_MAX_LEVEL, NETINFO_MAX_LEVEL, NETINFO_MAX_LEVEL) +
656-
"Pass \"help\" to see this detailed help documentation.\n"
657-
"If more than one argument is passed, only the first one is read and parsed.\n"
658-
"Suggestion: use with the Linux watch(1) command for a live dashboard; see example below.\n\n"
666+
+ strprintf("An optional argument from 0 to %d can be passed for different peers listings; values above %d up to 255 are parsed as %d.\n", NETINFO_MAX_LEVEL, NETINFO_MAX_LEVEL, NETINFO_MAX_LEVEL) +
667+
"If that argument is passed, an optional additional \"outonly\" argument may be passed to obtain the listing with outbound peers only.\n"
668+
"Pass \"help\" or \"h\" to see this detailed help documentation.\n"
669+
"If more than two arguments are passed, only the first two are read and parsed.\n"
670+
"Suggestion: use -netinfo with the Linux watch(1) command for a live dashboard; see example below.\n\n"
659671
"Arguments:\n"
660672
+ strprintf("1. level (integer 0-%d, optional) Specify the info level of the peers dashboard (default 0):\n", NETINFO_MAX_LEVEL) +
661673
" 0 - Peer counts for each reachable network as well as for block relay peers\n"
@@ -664,7 +676,9 @@ class NetinfoRequestHandler : public BaseRequestHandler
664676
" 2 - Like 1 but with an address column\n"
665677
" 3 - Like 1 but with a version column\n"
666678
" 4 - Like 1 but with both address and version columns\n"
667-
"2. help (string \"help\", optional) Print this help documentation instead of the dashboard.\n\n"
679+
"2. outonly (\"outonly\" or \"o\", optional) Return the peers listing with outbound peers only, i.e. to save screen space\n"
680+
" when a node has many inbound peers. Only valid if a level is passed.\n\n"
681+
"help (\"help\" or \"h\", optional) Print this help documentation instead of the dashboard.\n\n"
668682
"Result:\n\n"
669683
+ strprintf("* The peers listing in levels 1-%d displays all of the peers sorted by direction and minimum ping time:\n\n", NETINFO_MAX_LEVEL) +
670684
" Column Description\n"
@@ -717,6 +731,8 @@ class NetinfoRequestHandler : public BaseRequestHandler
717731
"> bitcoin-cli -netinfo 1\n\n"
718732
"Full dashboard\n"
719733
+ strprintf("> bitcoin-cli -netinfo %d\n\n", NETINFO_MAX_LEVEL) +
734+
"Full dashboard, but with outbound peers only\n"
735+
+ strprintf("> bitcoin-cli -netinfo %d outonly\n\n", NETINFO_MAX_LEVEL) +
720736
"Full live dashboard, adjust --interval or --no-title as needed (Linux)\n"
721737
+ strprintf("> watch --interval 1 --no-title bitcoin-cli -netinfo %d\n\n", NETINFO_MAX_LEVEL) +
722738
"See this help\n"
@@ -1242,7 +1258,7 @@ static int CommandLineRPC(int argc, char *argv[])
12421258
if (gArgs.IsArgSet("-getinfo")) {
12431259
rh.reset(new GetinfoRequestHandler());
12441260
} else if (gArgs.GetBoolArg("-netinfo", false)) {
1245-
if (!args.empty() && args.at(0) == "help") {
1261+
if (!args.empty() && (args.at(0) == "h" || args.at(0) == "help")) {
12461262
tfm::format(std::cout, "%s\n", NetinfoRequestHandler().m_help_doc);
12471263
return 0;
12481264
}

0 commit comments

Comments
 (0)