Skip to content

Commit faaeb2b

Browse files
author
MarcoFalke
committed
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.
1 parent fa8ec00 commit faaeb2b

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

src/rpc/server.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <amount.h>
1010
#include <rpc/request.h>
11+
#include <rpc/util.h>
1112

1213
#include <functional>
1314
#include <map>
@@ -85,6 +86,7 @@ void RPCUnsetTimerInterface(RPCTimerInterface *iface);
8586
void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nSeconds);
8687

8788
typedef UniValue(*rpcfn_type)(const JSONRPCRequest& jsonRequest);
89+
typedef RPCHelpMan (*RpcMethodFnType)();
8890

8991
class CRPCCommand
9092
{
@@ -101,6 +103,17 @@ class CRPCCommand
101103
{
102104
}
103105

106+
//! Simplified constructor taking plain RpcMethodFnType function pointer.
107+
CRPCCommand(std::string category, std::string name_in, RpcMethodFnType fn, std::vector<std::string> args_in)
108+
: CRPCCommand(
109+
category,
110+
fn().m_name,
111+
[fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn().HandleRequest(request); return true; },
112+
fn().GetArgNames(),
113+
intptr_t(fn))
114+
{
115+
}
116+
104117
//! Simplified constructor taking plain rpcfn_type function pointer.
105118
CRPCCommand(const char* category, const char* name, rpcfn_type fn, std::initializer_list<const char*> args)
106119
: CRPCCommand(category, name,
@@ -117,7 +130,7 @@ class CRPCCommand
117130
};
118131

119132
/**
120-
* Bitcoin RPC command dispatcher.
133+
* RPC command dispatcher.
121134
*/
122135
class CRPCTable
123136
{

src/rpc/util.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,11 @@ struct Sections {
418418
};
419419

420420
RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples)
421+
: RPCHelpMan{std::move(name), std::move(description), std::move(args), std::move(results), std::move(examples), nullptr} {}
422+
423+
RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples, RPCMethodImpl fun)
421424
: m_name{std::move(name)},
425+
m_fun{std::move(fun)},
422426
m_description{std::move(description)},
423427
m_args{std::move(args)},
424428
m_results{std::move(results)},
@@ -467,6 +471,16 @@ bool RPCHelpMan::IsValidNumArgs(size_t num_args) const
467471
}
468472
return num_required_args <= num_args && num_args <= m_args.size();
469473
}
474+
475+
std::vector<std::string> RPCHelpMan::GetArgNames() const
476+
{
477+
std::vector<std::string> ret;
478+
for (const auto& arg : m_args) {
479+
ret.emplace_back(arg.m_names);
480+
}
481+
return ret;
482+
}
483+
470484
std::string RPCHelpMan::ToString() const
471485
{
472486
std::string ret;

src/rpc/util.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,14 @@ class RPCHelpMan
326326
{
327327
public:
328328
RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples);
329+
using RPCMethodImpl = std::function<UniValue(const RPCHelpMan&, const JSONRPCRequest&)>;
330+
RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples, RPCMethodImpl fun);
329331

330332
std::string ToString() const;
333+
UniValue HandleRequest(const JSONRPCRequest& request)
334+
{
335+
return m_fun(*this, request);
336+
}
331337
/** If the supplied number of args is neither too small nor too high */
332338
bool IsValidNumArgs(size_t num_args) const;
333339
/**
@@ -340,8 +346,12 @@ class RPCHelpMan
340346
}
341347
}
342348

343-
private:
349+
std::vector<std::string> GetArgNames() const;
350+
344351
const std::string m_name;
352+
353+
private:
354+
const RPCMethodImpl m_fun;
345355
const std::string m_description;
346356
const std::vector<RPCArg> m_args;
347357
const RPCResults m_results;

0 commit comments

Comments
 (0)