rpc: Add CRPCCommand constructor which takes RPCHelpMan

This allows the constructor to ask the rpc manager for the name of the
rpc method or the rpc argument names instead of having it manually
passed in.
This commit is contained in:
MarcoFalke 2020-06-26 12:16:22 -04:00
parent fa8ec00061
commit faaeb2b0b3
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
3 changed files with 39 additions and 2 deletions

View file

@ -8,6 +8,7 @@
#include <amount.h> #include <amount.h>
#include <rpc/request.h> #include <rpc/request.h>
#include <rpc/util.h>
#include <functional> #include <functional>
#include <map> #include <map>
@ -85,6 +86,7 @@ void RPCUnsetTimerInterface(RPCTimerInterface *iface);
void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nSeconds); void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nSeconds);
typedef UniValue(*rpcfn_type)(const JSONRPCRequest& jsonRequest); typedef UniValue(*rpcfn_type)(const JSONRPCRequest& jsonRequest);
typedef RPCHelpMan (*RpcMethodFnType)();
class CRPCCommand class CRPCCommand
{ {
@ -101,6 +103,17 @@ public:
{ {
} }
//! Simplified constructor taking plain RpcMethodFnType function pointer.
CRPCCommand(std::string category, std::string name_in, RpcMethodFnType fn, std::vector<std::string> args_in)
: CRPCCommand(
category,
fn().m_name,
[fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn().HandleRequest(request); return true; },
fn().GetArgNames(),
intptr_t(fn))
{
}
//! Simplified constructor taking plain rpcfn_type function pointer. //! Simplified constructor taking plain rpcfn_type function pointer.
CRPCCommand(const char* category, const char* name, rpcfn_type fn, std::initializer_list<const char*> args) CRPCCommand(const char* category, const char* name, rpcfn_type fn, std::initializer_list<const char*> args)
: CRPCCommand(category, name, : CRPCCommand(category, name,
@ -117,7 +130,7 @@ public:
}; };
/** /**
* Bitcoin RPC command dispatcher. * RPC command dispatcher.
*/ */
class CRPCTable class CRPCTable
{ {

View file

@ -418,7 +418,11 @@ struct Sections {
}; };
RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples) RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples)
: RPCHelpMan{std::move(name), std::move(description), std::move(args), std::move(results), std::move(examples), nullptr} {}
RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples, RPCMethodImpl fun)
: m_name{std::move(name)}, : m_name{std::move(name)},
m_fun{std::move(fun)},
m_description{std::move(description)}, m_description{std::move(description)},
m_args{std::move(args)}, m_args{std::move(args)},
m_results{std::move(results)}, m_results{std::move(results)},
@ -467,6 +471,16 @@ bool RPCHelpMan::IsValidNumArgs(size_t num_args) const
} }
return num_required_args <= num_args && num_args <= m_args.size(); return num_required_args <= num_args && num_args <= m_args.size();
} }
std::vector<std::string> RPCHelpMan::GetArgNames() const
{
std::vector<std::string> ret;
for (const auto& arg : m_args) {
ret.emplace_back(arg.m_names);
}
return ret;
}
std::string RPCHelpMan::ToString() const std::string RPCHelpMan::ToString() const
{ {
std::string ret; std::string ret;

View file

@ -326,8 +326,14 @@ class RPCHelpMan
{ {
public: public:
RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples); RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples);
using RPCMethodImpl = std::function<UniValue(const RPCHelpMan&, const JSONRPCRequest&)>;
RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples, RPCMethodImpl fun);
std::string ToString() const; std::string ToString() const;
UniValue HandleRequest(const JSONRPCRequest& request)
{
return m_fun(*this, request);
}
/** If the supplied number of args is neither too small nor too high */ /** If the supplied number of args is neither too small nor too high */
bool IsValidNumArgs(size_t num_args) const; bool IsValidNumArgs(size_t num_args) const;
/** /**
@ -340,8 +346,12 @@ public:
} }
} }
private: std::vector<std::string> GetArgNames() const;
const std::string m_name; const std::string m_name;
private:
const RPCMethodImpl m_fun;
const std::string m_description; const std::string m_description;
const std::vector<RPCArg> m_args; const std::vector<RPCArg> m_args;
const RPCResults m_results; const RPCResults m_results;