xrpld
Loading...
Searching...
No Matches
RPCHandler.cpp
1#include <xrpld/rpc/RPCHandler.h>
2
3#include <xrpld/app/ledger/LedgerMaster.h> // IWYU pragma: keep
4#include <xrpld/app/main/Application.h>
5#include <xrpld/core/Config.h>
6#include <xrpld/rpc/Context.h>
7#include <xrpld/rpc/Role.h>
8#include <xrpld/rpc/Status.h>
9#include <xrpld/rpc/detail/Handler.h>
10#include <xrpld/rpc/detail/Tuning.h>
11
12#include <xrpl/basics/Log.h>
13#include <xrpl/core/Job.h>
14#include <xrpl/core/JobQueue.h>
15#include <xrpl/core/PerfLog.h>
16#include <xrpl/json/to_string.h> // IWYU pragma: keep
17#include <xrpl/protocol/ErrorCodes.h>
18#include <xrpl/protocol/jss.h>
19#include <xrpl/resource/Fees.h>
20
21#include <atomic>
22#include <chrono>
23#include <cstdint>
24#include <exception>
25#include <string>
26
27namespace xrpl::rpc {
28
29namespace {
30
109
111fillHandler(JsonContext& context, Handler const*& result)
112{
113 if (!isUnlimited(context.role))
114 {
115 // Count all jobs at jtCLIENT priority or higher.
116 int const jobCount = context.app.getJobQueue().getJobCountGE(JtClient);
117 if (jobCount > tuning::kMaxJobQueueClients)
118 {
119 JLOG(context.j.debug()) << "Too busy for command: " << jobCount;
120 return RpcTooBusy;
121 }
122 }
123
124 if (!context.params.isMember(jss::command) && !context.params.isMember(jss::method))
125 return RpcCommandMissing;
126 if (context.params.isMember(jss::command) && context.params.isMember(jss::method))
127 {
128 if (context.params[jss::command].asString() != context.params[jss::method].asString())
129 return RpcUnknownCommand;
130 }
131
132 std::string const strCommand = context.params.isMember(jss::command)
133 ? context.params[jss::command].asString()
134 : context.params[jss::method].asString();
135
136 JLOG(context.j.trace()) << "COMMAND:" << strCommand;
137 JLOG(context.j.trace()) << "REQUEST:" << context.params;
138 auto handler = getHandler(context.apiVersion, context.app.config().betaRpcApi, strCommand);
139
140 if (handler == nullptr)
141 return RpcUnknownCommand;
142
143 if (handler->role == Role::ADMIN && context.role != Role::ADMIN)
144 return RpcNoPermission;
145
146 ErrorCodeI const res = conditionMet(handler->condition, context);
147 if (res != RpcSuccess)
148 {
149 return res;
150 }
151
152 result = handler;
153 return RpcSuccess;
154}
155
156template <class Object, class Method>
157Status
158callMethod(JsonContext& context, Method method, std::string const& name, Object& result)
159{
160 static std::atomic<std::uint64_t> kRequestId{0};
161 auto& perfLog = context.app.getPerfLog();
162 std::uint64_t const curId = ++kRequestId;
163 try
164 {
165 perfLog.rpcStart(name, curId);
166 auto v = context.app.getJobQueue().makeLoadEvent(JtGeneric, "cmd:" + name);
167
168 auto start = std::chrono::system_clock::now();
169 auto ret = method(context, result);
171
172 JLOG(context.j.debug()) << "RPC call " << name << " completed in "
173 << ((end - start).count() / 1000000000.0) << "seconds";
174 perfLog.rpcFinish(name, curId);
175 return ret;
176 }
177 catch (std::exception& e)
178 {
179 perfLog.rpcError(name, curId);
180 JLOG(context.j.info()) << "Caught throw: " << e.what();
181
182 if (context.loadType == resource::kFeeReferenceRpc)
183 context.loadType = resource::kFeeExceptionRpc;
184
185 injectError(RpcInternal, result);
186 return RpcInternal;
187 }
188}
189
190} // namespace
191
192Status
194{
195 Handler const* handler = nullptr;
196 if (auto error = fillHandler(context, handler))
197 {
198 injectError(error, result);
199 return error;
200 }
201
202 if (auto method = handler->valueMethod)
203 {
204 if (!context.headers.user.empty() || !context.headers.forwardedFor.empty())
205 {
206 JLOG(context.j.debug())
207 << "start command: " << handler->name << ", user: " << context.headers.user
208 << ", forwarded for: " << context.headers.forwardedFor;
209
210 auto ret = callMethod(context, method, handler->name, result);
211
212 JLOG(context.j.debug())
213 << "finish command: " << handler->name << ", user: " << context.headers.user
214 << ", forwarded for: " << context.headers.forwardedFor;
215
216 return ret;
217 }
218
219 auto ret = callMethod(context, method, handler->name, result);
220 return ret;
221 }
222
223 return RpcUnknownCommand;
224}
225
226Role
227roleRequired(unsigned int version, bool betaEnabled, std::string const& method)
228{
229 auto handler = rpc::getHandler(version, betaEnabled, method);
230
231 if (handler == nullptr)
232 return Role::FORBID;
233
234 return handler->role;
235}
236
237} // namespace xrpl::rpc
Stream debug() const
Definition Journal.h:344
Represents a JSON value.
Definition json_value.h:117
T empty(T... args)
T end(T... args)
Charge const kFeeReferenceRpc
Charge const kFeeExceptionRpc
static constexpr int kMaxJobQueueClients
API version numbers used in later API versions.
Definition ApiVersion.h:36
ErrorCodeI conditionMet(Condition conditionRequired, T &context)
Definition Handler.h:70
void injectError(ErrorCodeI code, json::Value &json)
Add or update the json update to reflect the error code.
Handler const * getHandler(unsigned version, bool betaEnabled, std::string const &name)
Definition Handler.cpp:480
Role roleRequired(unsigned int version, bool betaEnabled, std::string const &method)
Status doCommand(rpc::JsonContext &context, json::Value &result)
Execute an RPC command and store the results in a json::Value.
ErrorCodeI
Definition ErrorCodes.h:23
@ RpcInternal
Definition ErrorCodes.h:113
@ RpcSuccess
Definition ErrorCodes.h:27
@ RpcCommandMissing
Definition ErrorCodes.h:85
@ RpcTooBusy
Definition ErrorCodes.h:39
@ RpcNoPermission
Definition ErrorCodes.h:36
@ RpcUnknownCommand
Definition ErrorCodes.h:68
Role
Indicates the level of administrative permission to grant.
Definition Role.h:27
@ ADMIN
Definition Role.h:27
@ FORBID
Definition Role.h:27
@ JtGeneric
Definition Job.h:73
@ JtClient
Definition Job.h:31
bool isUnlimited(Role const &role)
ADMIN and IDENTIFIED roles shall have unlimited resources.
Definition Role.cpp:115
beast::Journal const j
Definition Context.h:28
char const * name
Definition Handler.h:38
Method< json::Value > valueMethod
Definition Handler.h:39
std::string_view forwardedFor
Definition Context.h:48
Status represents the results of an operation that might fail.
Definition Status.h:27
T what(T... args)