Skip to content

Commit 1f89bfd

Browse files
committed
merge bitcoin#21832: Implement human readable -getinfo
1 parent 2200b78 commit 1f89bfd

File tree

3 files changed

+220
-49
lines changed

3 files changed

+220
-49
lines changed

doc/release-notes-21832.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Tools and Utilities
2+
-------------------
3+
4+
- Update `-getinfo` to return data in a user-friendly format that also reduces vertical space.

src/bitcoin-cli.cpp

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <chainparamsbase.h>
1212
#include <clientversion.h>
1313
#include <compat.h>
14+
#include <policy/feerate.h>
1415
#include <rpc/client.h>
1516
#include <rpc/mining.h>
1617
#include <rpc/protocol.h>
@@ -31,6 +32,10 @@
3132
#include <string>
3233
#include <tuple>
3334

35+
#ifndef WIN32
36+
#include <unistd.h>
37+
#endif
38+
3439
#include <event2/buffer.h>
3540
#include <event2/keyvalq_struct.h>
3641
#include <support/events.h>
@@ -51,6 +56,9 @@ static constexpr int8_t UNKNOWN_NETWORK{-1};
5156
/** Default number of blocks to generate for RPC generatetoaddress. */
5257
static const std::string DEFAULT_NBLOCKS = "1";
5358

59+
/** Default -color setting. */
60+
static const std::string DEFAULT_COLOR_SETTING{"auto"};
61+
5462
static void SetupCliArgs(ArgsManager& argsman)
5563
{
5664
SetupHelpOptions(argsman);
@@ -66,6 +74,8 @@ static void SetupCliArgs(ArgsManager& argsman)
6674
argsman.AddArg("-addrinfo", "Get the number of addresses known to the node, per network and total.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
6775
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);
6876
argsman.AddArg("-netinfo", "Get network peer connection information from the remote server. An optional integer argument from 0 to 4 can be passed for different peers listings (default: 0). Pass \"help\" for detailed help documentation.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
77+
78+
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_STRING, OptionsCategory::OPTIONS);
6979
argsman.AddArg("-named", strprintf("Pass named instead of positional arguments (default: %s)", DEFAULT_NAMED), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
7080
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);
7181
argsman.AddArg("-rpcconnect=<ip>", strprintf("Send commands to node running on <ip> (default: %s)", DEFAULT_RPCCONNECT), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
@@ -355,7 +365,9 @@ class GetinfoRequestHandler: public BaseRequestHandler
355365
result.pushKV("chain", UniValue(batch[ID_BLOCKCHAININFO]["result"]["chain"]));
356366
if (!batch[ID_WALLETINFO]["result"].isNull()) {
357367
result.pushKV("coinjoin_balance", batch[ID_WALLETINFO]["result"]["coinjoin_balance"]);
368+
result.pushKV("has_wallet", true);
358369
result.pushKV("keypoolsize", batch[ID_WALLETINFO]["result"]["keypoolsize"]);
370+
result.pushKV("walletname", batch[ID_WALLETINFO]["result"]["walletname"]);
359371
if (!batch[ID_WALLETINFO]["result"]["unlocked_until"].isNull()) {
360372
result.pushKV("unlocked_until", batch[ID_WALLETINFO]["result"]["unlocked_until"]);
361373
}
@@ -882,6 +894,105 @@ static void GetWalletBalances(UniValue& result)
882894
result.pushKV("balances", balances);
883895
}
884896

897+
/**
898+
* ParseGetInfoResult takes in -getinfo result in UniValue object and parses it
899+
* into a user friendly UniValue string to be printed on the console.
900+
* @param[out] result Reference to UniValue result containing the -getinfo output.
901+
*/
902+
static void ParseGetInfoResult(UniValue& result)
903+
{
904+
if (!find_value(result, "error").isNull()) return;
905+
906+
std::string RESET, GREEN, BLUE, YELLOW, MAGENTA, CYAN;
907+
bool should_colorize = false;
908+
909+
#ifndef WIN32
910+
if (isatty(fileno(stdout))) {
911+
// By default, only print colored text if OS is not WIN32 and stdout is connected to a terminal.
912+
should_colorize = true;
913+
}
914+
#endif
915+
916+
if (gArgs.IsArgSet("-color")) {
917+
const std::string color{gArgs.GetArg("-color", DEFAULT_COLOR_SETTING)};
918+
if (color == "always") {
919+
should_colorize = true;
920+
} else if (color == "never") {
921+
should_colorize = false;
922+
} else if (color != "auto") {
923+
throw std::runtime_error("Invalid value for -color option. Valid values: always, auto, never.");
924+
}
925+
}
926+
927+
if (should_colorize) {
928+
RESET = "\x1B[0m";
929+
GREEN = "\x1B[32m";
930+
BLUE = "\x1B[34m";
931+
YELLOW = "\x1B[33m";
932+
MAGENTA = "\x1B[35m";
933+
CYAN = "\x1B[36m";
934+
}
935+
936+
std::string result_string = strprintf("%sChain: %s%s\n", BLUE, result["chain"].getValStr(), RESET);
937+
result_string += strprintf("Blocks: %s\n", result["blocks"].getValStr());
938+
result_string += strprintf("Headers: %s\n", result["headers"].getValStr());
939+
result_string += strprintf("Verification progress: %.4f%%\n", result["verificationprogress"].get_real() * 100);
940+
result_string += strprintf("Difficulty: %s\n\n", result["difficulty"].getValStr());
941+
942+
result_string += strprintf(
943+
"%sNetwork: in %s, out %s, total %s, mn_in %s, mn_out %s, mn_total %s%s\n",
944+
GREEN,
945+
result["connections"]["in"].getValStr(),
946+
result["connections"]["out"].getValStr(),
947+
result["connections"]["total"].getValStr(),
948+
result["connections"]["mn_in"].getValStr(),
949+
result["connections"]["mn_out"].getValStr(),
950+
result["connections"]["mn_total"].getValStr(),
951+
RESET);
952+
result_string += strprintf("Version: %s\n", result["version"].getValStr());
953+
result_string += strprintf("Time offset (s): %s\n", result["timeoffset"].getValStr());
954+
const std::string proxy = result["proxy"].getValStr();
955+
result_string += strprintf("Proxy: %s\n", proxy.empty() ? "N/A" : proxy);
956+
result_string += strprintf("Min tx relay fee rate (%s/kB): %s\n\n", CURRENCY_UNIT, result["relayfee"].getValStr());
957+
958+
if (!result["has_wallet"].isNull()) {
959+
const std::string walletname = result["walletname"].getValStr();
960+
result_string += strprintf("%sWallet: %s%s\n", MAGENTA, walletname.empty() ? "\"\"" : walletname, RESET);
961+
962+
result_string += strprintf("%sCoinJoin balance:%s %s\n", CYAN, RESET, result["coinjoin_balance"].getValStr());
963+
964+
result_string += strprintf("Keypool size: %s\n", result["keypoolsize"].getValStr());
965+
if (!result["unlocked_until"].isNull()) {
966+
result_string += strprintf("Unlocked until: %s\n", result["unlocked_until"].getValStr());
967+
}
968+
result_string += strprintf("Transaction fee rate (-paytxfee) (%s/kB): %s\n\n", CURRENCY_UNIT, result["paytxfee"].getValStr());
969+
}
970+
if (!result["balance"].isNull()) {
971+
result_string += strprintf("%sBalance:%s %s\n\n", CYAN, RESET, result["balance"].getValStr());
972+
}
973+
974+
if (!result["balances"].isNull()) {
975+
result_string += strprintf("%sBalances%s\n", CYAN, RESET);
976+
977+
size_t max_balance_length{10};
978+
979+
for (const std::string& wallet : result["balances"].getKeys()) {
980+
max_balance_length = std::max(result["balances"][wallet].getValStr().length(), max_balance_length);
981+
}
982+
983+
for (const std::string& wallet : result["balances"].getKeys()) {
984+
result_string += strprintf("%*s %s\n",
985+
max_balance_length,
986+
result["balances"][wallet].getValStr(),
987+
wallet.empty() ? "\"\"" : wallet);
988+
}
989+
result_string += "\n";
990+
}
991+
992+
result_string += strprintf("%sWarnings:%s %s", YELLOW, RESET, result["warnings"].getValStr());
993+
result.setStr(result_string);
994+
}
995+
885996
/**
886997
* Call RPC getnewaddress.
887998
* @returns getnewaddress response as a UniValue object.
@@ -1003,9 +1114,13 @@ static int CommandLineRPC(int argc, char *argv[])
10031114
UniValue result = find_value(reply, "result");
10041115
const UniValue& error = find_value(reply, "error");
10051116
if (error.isNull()) {
1006-
if (gArgs.IsArgSet("-getinfo") && !gArgs.IsArgSet("-rpcwallet")) {
1007-
GetWalletBalances(result); // fetch multiwallet balances and append to result
1117+
if (gArgs.GetBoolArg("-getinfo", false)) {
1118+
if (!gArgs.IsArgSet("-rpcwallet")) {
1119+
GetWalletBalances(result); // fetch multiwallet balances and append to result
1120+
}
1121+
ParseGetInfoResult(result);
10081122
}
1123+
10091124
ParseResult(result, strPrint);
10101125
} else {
10111126
ParseError(error, strPrint, nRet);

0 commit comments

Comments
 (0)