Skip to content

Commit 770d39a

Browse files
committed
Merge bitcoin/bitcoin#31887: CLI cleanups
d423fd9 cli, bugfix: for -getinfo, replace IsArgSet() with GetBoolArg() (Jon Atack) e99e41b cli, refactor: simplify public-only classes with structs (Jon Atack) fdbfd25 cli, refactor: deduplicate NetworkStringToId() (Jon Atack) be82139 cli, refactor: simplify DetailsRequested() (Jon Atack) Pull request description: These have been accumulating over the past few years. Each is described in its commit message. ACKs for top commit: pablomartin4btc: re-ACK d423fd9 hodlinator: Code review ACK d423fd9 l0rinc: ACK d423fd9 ryanofsky: Code review ACK d423fd9, just running clang-format and updating commit messages since last review Tree-SHA512: a8e5f7827cef308186d5a7c3a627d2cd8f57437f4465d181986e5d3274ff0e2b9faac252dd55d9257d66a7aa99fca62b3000cdc0988d23385df20ff1f870a046
2 parents a203928 + d423fd9 commit 770d39a

File tree

1 file changed

+16
-35
lines changed

1 file changed

+16
-35
lines changed

src/bitcoin-cli.cpp

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,10 @@ static void libevent_log_cb(int severity, const char *msg)
130130
// Exception thrown on connection error. This error is used to determine
131131
// when to wait if -rpcwait is given.
132132
//
133-
class CConnectionFailed : public std::runtime_error
134-
{
135-
public:
136-
133+
struct CConnectionFailed : std::runtime_error {
137134
explicit inline CConnectionFailed(const std::string& msg) :
138135
std::runtime_error(msg)
139136
{}
140-
141137
};
142138

143139
//
@@ -259,30 +255,25 @@ static void http_error_cb(enum evhttp_request_error err, void *ctx)
259255
reply->error = err;
260256
}
261257

262-
/** Class that handles the conversion from a command-line to a JSON-RPC request,
258+
static int8_t NetworkStringToId(const std::string& str)
259+
{
260+
for (size_t i = 0; i < NETWORKS.size(); ++i) {
261+
if (str == NETWORKS[i]) return i;
262+
}
263+
return UNKNOWN_NETWORK;
264+
}
265+
266+
/** Handle the conversion from a command-line to a JSON-RPC request,
263267
* as well as converting back to a JSON object that can be shown as result.
264268
*/
265-
class BaseRequestHandler
266-
{
267-
public:
269+
struct BaseRequestHandler {
268270
virtual ~BaseRequestHandler() = default;
269271
virtual UniValue PrepareRequest(const std::string& method, const std::vector<std::string>& args) = 0;
270272
virtual UniValue ProcessReply(const UniValue &batch_in) = 0;
271273
};
272274

273275
/** Process addrinfo requests */
274-
class AddrinfoRequestHandler : public BaseRequestHandler
275-
{
276-
private:
277-
int8_t NetworkStringToId(const std::string& str) const
278-
{
279-
for (size_t i = 0; i < NETWORKS.size(); ++i) {
280-
if (str == NETWORKS[i]) return i;
281-
}
282-
return UNKNOWN_NETWORK;
283-
}
284-
285-
public:
276+
struct AddrinfoRequestHandler : BaseRequestHandler {
286277
UniValue PrepareRequest(const std::string& method, const std::vector<std::string>& args) override
287278
{
288279
if (!args.empty()) {
@@ -321,9 +312,7 @@ class AddrinfoRequestHandler : public BaseRequestHandler
321312
};
322313

323314
/** Process getinfo requests */
324-
class GetinfoRequestHandler: public BaseRequestHandler
325-
{
326-
public:
315+
struct GetinfoRequestHandler : BaseRequestHandler {
327316
const int ID_NETWORKINFO = 0;
328317
const int ID_BLOCKCHAININFO = 1;
329318
const int ID_WALLETINFO = 2;
@@ -396,15 +385,8 @@ class NetinfoRequestHandler : public BaseRequestHandler
396385
std::array<std::array<uint16_t, NETWORKS.size() + 1>, 3> m_counts{{{}}}; //!< Peer counts by (in/out/total, networks/total)
397386
uint8_t m_block_relay_peers_count{0};
398387
uint8_t m_manual_peers_count{0};
399-
int8_t NetworkStringToId(const std::string& str) const
400-
{
401-
for (size_t i = 0; i < NETWORKS.size(); ++i) {
402-
if (str == NETWORKS[i]) return i;
403-
}
404-
return UNKNOWN_NETWORK;
405-
}
406388
uint8_t m_details_level{0}; //!< Optional user-supplied arg to set dashboard details level
407-
bool DetailsRequested() const { return m_details_level > 0 && m_details_level < 5; }
389+
bool DetailsRequested() const { return m_details_level; }
408390
bool IsAddressSelected() const { return m_details_level == 2 || m_details_level == 4; }
409391
bool IsVersionSelected() const { return m_details_level == 3 || m_details_level == 4; }
410392
bool m_outbound_only_selected{false};
@@ -775,8 +757,7 @@ class GenerateToAddressRequestHandler : public BaseRequestHandler
775757
};
776758

777759
/** Process default single requests */
778-
class DefaultRequestHandler: public BaseRequestHandler {
779-
public:
760+
struct DefaultRequestHandler : BaseRequestHandler {
780761
UniValue PrepareRequest(const std::string& method, const std::vector<std::string>& args) override
781762
{
782763
UniValue params;
@@ -1267,7 +1248,7 @@ static int CommandLineRPC(int argc, char *argv[])
12671248
gArgs.CheckMultipleCLIArgs();
12681249
std::unique_ptr<BaseRequestHandler> rh;
12691250
std::string method;
1270-
if (gArgs.IsArgSet("-getinfo")) {
1251+
if (gArgs.GetBoolArg("-getinfo", false)) {
12711252
rh.reset(new GetinfoRequestHandler());
12721253
} else if (gArgs.GetBoolArg("-netinfo", false)) {
12731254
if (!args.empty() && (args.at(0) == "h" || args.at(0) == "help")) {

0 commit comments

Comments
 (0)