Skip to content

Commit 6158a6d

Browse files
committed
refactor: Replace JSONRPCRequest fHelp field with mode field
No change in behavior
1 parent 80e16ca commit 6158a6d

File tree

6 files changed

+18
-21
lines changed

6 files changed

+18
-21
lines changed

src/rpc/request.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ class JSONRPCRequest
3434
UniValue id;
3535
std::string strMethod;
3636
UniValue params;
37-
bool fHelp;
37+
enum Mode { EXECUTE, GET_HELP } mode = EXECUTE;
3838
std::string URI;
3939
std::string authUser;
4040
std::string peerAddr;
4141
const util::Ref& context;
4242

43-
explicit JSONRPCRequest(const util::Ref& context) : id(NullUniValue), params(NullUniValue), fHelp(false), context(context) {}
43+
explicit JSONRPCRequest(const util::Ref& context) : id(NullUniValue), params(NullUniValue), context(context) {}
4444

4545
//! Initializes request information from another request object and the
4646
//! given context. The implementation should be updated if any members are
4747
//! added or removed above.
4848
JSONRPCRequest(const JSONRPCRequest& other, const util::Ref& context)
49-
: id(other.id), strMethod(other.strMethod), params(other.params), fHelp(other.fHelp), URI(other.URI),
49+
: id(other.id), strMethod(other.strMethod), params(other.params), mode(other.mode), URI(other.URI),
5050
authUser(other.authUser), peerAddr(other.peerAddr), context(context)
5151
{
5252
}

src/rpc/server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest&
8888
sort(vCommands.begin(), vCommands.end());
8989

9090
JSONRPCRequest jreq(helpreq);
91-
jreq.fHelp = true;
91+
jreq.mode = JSONRPCRequest::GET_HELP;
9292
jreq.params = UniValue();
9393

9494
for (const std::pair<std::string, const CRPCCommand*>& command : vCommands)

src/rpc/util.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,18 @@ std::string RPCExamples::ToDescriptionString() const
476476
return m_examples.empty() ? m_examples : "\nExamples:\n" + m_examples;
477477
}
478478

479+
UniValue RPCHelpMan::HandleRequest(const JSONRPCRequest& request)
480+
{
481+
/*
482+
* Check if the given request is valid according to this command or if
483+
* the user is asking for help information, and throw help when appropriate.
484+
*/
485+
if (request.mode == JSONRPCRequest::GET_HELP || !IsValidNumArgs(request.params.size())) {
486+
throw std::runtime_error(ToString());
487+
}
488+
return m_fun(*this, request);
489+
}
490+
479491
bool RPCHelpMan::IsValidNumArgs(size_t num_args) const
480492
{
481493
size_t num_required_args = 0;

src/rpc/util.h

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -335,26 +335,12 @@ class RPCHelpMan
335335
using RPCMethodImpl = std::function<UniValue(const RPCHelpMan&, const JSONRPCRequest&)>;
336336
RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples, RPCMethodImpl fun);
337337

338+
UniValue HandleRequest(const JSONRPCRequest& request);
338339
std::string ToString() const;
339340
/** Append the named args that need to be converted from string to another JSON type */
340341
void AppendArgMap(UniValue& arr) const;
341-
UniValue HandleRequest(const JSONRPCRequest& request)
342-
{
343-
Check(request);
344-
return m_fun(*this, request);
345-
}
346342
/** If the supplied number of args is neither too small nor too high */
347343
bool IsValidNumArgs(size_t num_args) const;
348-
/**
349-
* Check if the given request is valid according to this command or if
350-
* the user is asking for help information, and throw help when appropriate.
351-
*/
352-
inline void Check(const JSONRPCRequest& request) const {
353-
if (request.fHelp || !IsValidNumArgs(request.params.size())) {
354-
throw std::runtime_error(ToString());
355-
}
356-
}
357-
358344
std::vector<std::string> GetArgNames() const;
359345

360346
const std::string m_name;

src/test/rpc_tests.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ UniValue RPCTestingSetup::CallRPC(std::string args)
3636
JSONRPCRequest request(context);
3737
request.strMethod = strMethod;
3838
request.params = RPCConvertValues(strMethod, vArgs);
39-
request.fHelp = false;
4039
if (RPCIsInWarmup(nullptr)) SetRPCWarmupFinished();
4140
try {
4241
UniValue result = tableRPC.execute(request);

src/wallet/rpcwallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ bool GetWalletNameFromJSONRPCRequest(const JSONRPCRequest& request, std::string&
9696

9797
std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& request)
9898
{
99-
CHECK_NONFATAL(!request.fHelp);
99+
CHECK_NONFATAL(request.mode == JSONRPCRequest::EXECUTE);
100100
std::string wallet_name;
101101
if (GetWalletNameFromJSONRPCRequest(request, wallet_name)) {
102102
std::shared_ptr<CWallet> pwallet = GetWallet(wallet_name);

0 commit comments

Comments
 (0)