From be82139b2a1e5bcd7c47391672d38fa5aa05bc0b Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 16 Feb 2025 09:24:12 -0600 Subject: [PATCH 1/4] cli, refactor: simplify DetailsRequested() The bounds check is no longer needed after the merge of PR 21192. --- src/bitcoin-cli.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index 75911d00874..44198b9a9be 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -404,7 +404,7 @@ private: return UNKNOWN_NETWORK; } uint8_t m_details_level{0}; //!< Optional user-supplied arg to set dashboard details level - bool DetailsRequested() const { return m_details_level > 0 && m_details_level < 5; } + bool DetailsRequested() const { return m_details_level; } bool IsAddressSelected() const { return m_details_level == 2 || m_details_level == 4; } bool IsVersionSelected() const { return m_details_level == 3 || m_details_level == 4; } bool m_outbound_only_selected{false}; From fdbfd250fbf27137f23b1064ae2fcd9bf3c59937 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 16 Feb 2025 09:38:15 -0600 Subject: [PATCH 2/4] cli, refactor: deduplicate NetworkStringToId() --- src/bitcoin-cli.cpp | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index 44198b9a9be..a6d84747cdb 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -259,6 +259,14 @@ static void http_error_cb(enum evhttp_request_error err, void *ctx) reply->error = err; } +static int8_t NetworkStringToId(const std::string& str) +{ + for (size_t i = 0; i < NETWORKS.size(); ++i) { + if (str == NETWORKS[i]) return i; + } + return UNKNOWN_NETWORK; +} + /** Class that handles the conversion from a command-line to a JSON-RPC request, * as well as converting back to a JSON object that can be shown as result. */ @@ -273,15 +281,6 @@ public: /** Process addrinfo requests */ class AddrinfoRequestHandler : public BaseRequestHandler { -private: - int8_t NetworkStringToId(const std::string& str) const - { - for (size_t i = 0; i < NETWORKS.size(); ++i) { - if (str == NETWORKS[i]) return i; - } - return UNKNOWN_NETWORK; - } - public: UniValue PrepareRequest(const std::string& method, const std::vector& args) override { @@ -396,13 +395,6 @@ private: std::array, 3> m_counts{{{}}}; //!< Peer counts by (in/out/total, networks/total) uint8_t m_block_relay_peers_count{0}; uint8_t m_manual_peers_count{0}; - int8_t NetworkStringToId(const std::string& str) const - { - for (size_t i = 0; i < NETWORKS.size(); ++i) { - if (str == NETWORKS[i]) return i; - } - return UNKNOWN_NETWORK; - } uint8_t m_details_level{0}; //!< Optional user-supplied arg to set dashboard details level bool DetailsRequested() const { return m_details_level; } bool IsAddressSelected() const { return m_details_level == 2 || m_details_level == 4; } From e99e41b30704f0067e064f92e7f582361f49676c Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 16 Feb 2025 09:41:32 -0600 Subject: [PATCH 3/4] cli, refactor: simplify public-only classes with structs and run clang-format on it --- src/bitcoin-cli.cpp | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index a6d84747cdb..7aa9dc1ea1a 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -130,14 +130,10 @@ static void libevent_log_cb(int severity, const char *msg) // Exception thrown on connection error. This error is used to determine // when to wait if -rpcwait is given. // -class CConnectionFailed : public std::runtime_error -{ -public: - +struct CConnectionFailed : std::runtime_error { explicit inline CConnectionFailed(const std::string& msg) : std::runtime_error(msg) {} - }; // @@ -267,21 +263,17 @@ static int8_t NetworkStringToId(const std::string& str) return UNKNOWN_NETWORK; } -/** Class that handles the conversion from a command-line to a JSON-RPC request, +/** Handle the conversion from a command-line to a JSON-RPC request, * as well as converting back to a JSON object that can be shown as result. */ -class BaseRequestHandler -{ -public: +struct BaseRequestHandler { virtual ~BaseRequestHandler() = default; virtual UniValue PrepareRequest(const std::string& method, const std::vector& args) = 0; virtual UniValue ProcessReply(const UniValue &batch_in) = 0; }; /** Process addrinfo requests */ -class AddrinfoRequestHandler : public BaseRequestHandler -{ -public: +struct AddrinfoRequestHandler : BaseRequestHandler { UniValue PrepareRequest(const std::string& method, const std::vector& args) override { if (!args.empty()) { @@ -320,9 +312,7 @@ public: }; /** Process getinfo requests */ -class GetinfoRequestHandler: public BaseRequestHandler -{ -public: +struct GetinfoRequestHandler : BaseRequestHandler { const int ID_NETWORKINFO = 0; const int ID_BLOCKCHAININFO = 1; const int ID_WALLETINFO = 2; @@ -767,8 +757,7 @@ protected: }; /** Process default single requests */ -class DefaultRequestHandler: public BaseRequestHandler { -public: +struct DefaultRequestHandler : BaseRequestHandler { UniValue PrepareRequest(const std::string& method, const std::vector& args) override { UniValue params; From d423fd9ec83ae323ac29bdc1b677ed8260bd59c4 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 22 Feb 2025 13:58:30 -0600 Subject: [PATCH 4/4] cli, bugfix: for -getinfo, replace IsArgSet() with GetBoolArg() for consistency with the other CLI commands (-netinfo, -addrinfo, -generate). This can be considered a bugfix because IsArgSet() returns whether an arg has been set even if it has been negated. After this change, we no longer treat -nogetinfo and -getinfo=0 the same as -getinfo and -getinfo=1, and instead as if -getinfo was not specified. Co-authored-by: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Co-authored-by: Ryan Ofsky --- src/bitcoin-cli.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index 7aa9dc1ea1a..863b495fb25 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -1248,7 +1248,7 @@ static int CommandLineRPC(int argc, char *argv[]) gArgs.CheckMultipleCLIArgs(); std::unique_ptr rh; std::string method; - if (gArgs.IsArgSet("-getinfo")) { + if (gArgs.GetBoolArg("-getinfo", false)) { rh.reset(new GetinfoRequestHandler()); } else if (gArgs.GetBoolArg("-netinfo", false)) { if (!args.empty() && (args.at(0) == "h" || args.at(0) == "help")) {