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/detail/Handler.h>
9#include <xrpld/rpc/detail/Tuning.h>
10
11#include <xrpl/basics/Log.h>
12#include <xrpl/core/Job.h>
13#include <xrpl/core/JobQueue.h>
14#include <xrpl/core/PerfLog.h>
15#include <xrpl/json/to_string.h> // IWYU pragma: keep
16#include <xrpl/protocol/ErrorCodes.h>
17#include <xrpl/protocol/jss.h>
18#include <xrpl/resource/Fees.h>
19
20#include <rpcspec/Errors.hpp>
21
22#include <atomic>
23#include <chrono>
24#include <cstdint>
25#include <exception>
26#include <string>
27#include <string_view>
28
29namespace xrpl::rpc {
30
31namespace {
32
111
113fillHandler(JsonContext& context, Handler const*& result)
114{
115 if (!isUnlimited(context.role))
116 {
117 // Count all jobs at jtCLIENT priority or higher.
118 int const jobCount = context.app.getJobQueue().getJobCountGE(JtClient);
119 if (jobCount > tuning::kMaxJobQueueClients)
120 {
121 JLOG(context.j.debug()) << "Too busy for command: " << jobCount;
122 return RpcTooBusy;
123 }
124 }
125
126 if (!context.params.isMember(jss::command) && !context.params.isMember(jss::method))
127 return RpcCommandMissing;
128 if (context.params.isMember(jss::command) && context.params.isMember(jss::method))
129 {
130 if (context.params[jss::command].asString() != context.params[jss::method].asString())
131 return RpcUnknownCommand;
132 }
133
134 std::string const strCommand = context.params.isMember(jss::command)
135 ? context.params[jss::command].asString()
136 : context.params[jss::method].asString();
137
138 JLOG(context.j.trace()) << "COMMAND:" << strCommand;
139 JLOG(context.j.trace()) << "REQUEST:" << context.params;
140 auto handler = getHandler(context.apiVersion, context.app.config().betaRpcApi, strCommand);
141
142 if (handler == nullptr)
143 return RpcUnknownCommand;
144
145 if (handler->role == Role::ADMIN && context.role != Role::ADMIN)
146 return RpcNoPermission;
147
148 ErrorCodeI const res = conditionMet(handler->condition, context);
149 if (res != RpcSuccess)
150 {
151 return res;
152 }
153
154 result = handler;
155 return RpcSuccess;
156}
157
158::rpc::Status
159callMethod(JsonContext& context, Handler::Method method, std::string_view name, json::Value& result)
160{
161 static std::atomic<std::uint64_t> kRequestId{0};
162 auto& perfLog = context.app.getPerfLog();
163 std::uint64_t const curId = ++kRequestId;
164 try
165 {
166 perfLog.rpcStart(name, curId);
167 auto const v =
168 context.app.getJobQueue().makeLoadEvent(JtGeneric, std::string{"cmd:"}.append(name));
169
170 auto const start = std::chrono::system_clock::now();
171 auto ret = method(context, result);
172 auto const end = std::chrono::system_clock::now();
173
174 JLOG(context.j.debug()) << "RPC call " << name << " completed in "
175 << ((end - start).count() / 1000000000.0) << "seconds";
176 perfLog.rpcFinish(name, curId);
177 return ret;
178 }
179 catch (std::exception& e)
180 {
181 perfLog.rpcError(name, curId);
182 JLOG(context.j.info()) << "Caught throw: " << e.what();
183
184 if (context.loadType == resource::kFeeReferenceRpc)
185 context.loadType = resource::kFeeExceptionRpc;
186
187 injectError(RpcInternal, result);
188 return RpcInternal;
189 }
190}
191
192} // namespace
193
194::rpc::Status
196{
197 Handler const* handler = nullptr;
198 if (auto const error = fillHandler(context, handler))
199 {
200 injectError(error, result);
201 return error;
202 }
203
204 // No null check on the method: Handler::Method has no default constructor, so
205 // every entry in the dispatch table names one.
206 if (!context.headers.user.empty() || !context.headers.forwardedFor.empty())
207 {
208 JLOG(context.j.debug()) << "start command: " << handler->name
209 << ", user: " << context.headers.user
210 << ", forwarded for: " << context.headers.forwardedFor;
211
212 auto const ret = callMethod(context, handler->valueMethod, handler->name, result);
213
214 JLOG(context.j.debug()) << "finish command: " << handler->name
215 << ", user: " << context.headers.user
216 << ", forwarded for: " << context.headers.forwardedFor;
217
218 return ret;
219 }
220
221 return callMethod(context, handler->valueMethod, handler->name, result);
222}
223
224Role
225roleRequired(unsigned int version, bool betaEnabled, std::string_view method)
226{
227 auto handler = rpc::getHandler(version, betaEnabled, method);
228
229 if (handler == nullptr)
230 return Role::FORBID;
231
232 return handler->role;
233}
234
235} // namespace xrpl::rpc
Stream debug() const
Definition Journal.h:344
Represents a JSON value.
Definition json_value.h:121
The function a handler dispatches to.
Definition Handler.h:49
T empty(T... args)
T end(T... args)
Charge const kFeeReferenceRpc
Charge const kFeeExceptionRpc
static constexpr int kMaxJobQueueClients
The api_version numbers this server serves.
Definition ApiVersion.h:26
ErrorCodeI conditionMet(Condition conditionRequired, T &context)
Definition Handler.h:140
Role roleRequired(unsigned int version, bool betaEnabled, std::string_view method)
Handler const * getHandler(unsigned version, bool betaEnabled, std::string_view name)
Definition Handler.cpp:638
void injectError(ErrorCodeI code, json::Value &json)
Add or update the json update to reflect the error code.
::rpc::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
Method valueMethod
Definition Handler.h:88
std::string_view name
Definition Handler.h:87
std::string_view forwardedFor
Definition Context.h:48
T what(T... args)