rippled
Loading...
Searching...
No Matches
RPCHandler.cpp
1#include <xrpld/app/ledger/InboundLedgers.h>
2#include <xrpld/app/ledger/LedgerMaster.h>
3#include <xrpld/app/ledger/LedgerToJson.h>
4#include <xrpld/app/main/Application.h>
5#include <xrpld/core/Config.h>
6#include <xrpld/rpc/Context.h>
7#include <xrpld/rpc/RPCHandler.h>
8#include <xrpld/rpc/Role.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/JobQueue.h>
14#include <xrpl/core/PerfLog.h>
15#include <xrpl/json/to_string.h>
16#include <xrpl/protocol/ErrorCodes.h>
17#include <xrpl/protocol/jss.h>
18#include <xrpl/resource/Fees.h>
19#include <xrpl/server/InfoSub.h>
20#include <xrpl/server/NetworkOPs.h>
21
22#include <atomic>
23#include <chrono>
24
25namespace xrpl {
26namespace RPC {
27
28namespace {
29
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::maxJobQueueClients)
118 {
119 JLOG(context.j.debug()) << "Too busy for command: " << jobCount;
120 return rpcTOO_BUSY;
121 }
122 }
123
124 if (!context.params.isMember(jss::command) && !context.params.isMember(jss::method))
125 return rpcCOMMAND_MISSING;
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 rpcUNKNOWN_COMMAND;
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().BETA_RPC_API, strCommand);
139
140 if (handler == nullptr)
141 return rpcUNKNOWN_COMMAND;
142
143 if (handler->role_ == Role::ADMIN && context.role != Role::ADMIN)
144 return rpcNO_PERMISSION;
145
146 error_code_i 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> requestId{0};
161 auto& perfLog = context.app.getPerfLog();
162 std::uint64_t const curId = ++requestId;
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::feeReferenceRPC)
183 context.loadType = Resource::feeExceptionRPC;
184
185 inject_error(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 inject_error(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 rpcUNKNOWN_COMMAND;
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 RPC
238} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
Stream debug() const
Definition Journal.h:301
T count(T... args)
T empty(T... args)
T end(T... args)
Status
Return codes from Backend operations.
static int constexpr maxJobQueueClients
Role roleRequired(unsigned int version, bool betaEnabled, std::string const &method)
Handler const * getHandler(unsigned version, bool betaEnabled, std::string const &name)
Definition Handler.cpp:254
Status doCommand(RPC::JsonContext &context, Json::Value &result)
Execute an RPC command and store the results in a Json::Value.
error_code_i conditionMet(Condition condition_required, T &context)
Definition Handler.h:59
void inject_error(error_code_i code, Json::Value &json)
Add or update the json update to reflect the error code.
Charge const feeReferenceRPC
Charge const feeExceptionRPC
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Role
Indicates the level of administrative permission to grant.
Definition Role.h:24
@ jtCLIENT
Definition Job.h:24
@ jtGENERIC
Definition Job.h:66
bool isUnlimited(Role const &role)
ADMIN and IDENTIFIED roles shall have unlimited resources.
Definition Role.cpp:98
error_code_i
Definition ErrorCodes.h:20
@ rpcCOMMAND_MISSING
Definition ErrorCodes.h:82
@ rpcTOO_BUSY
Definition ErrorCodes.h:36
@ rpcNO_PERMISSION
Definition ErrorCodes.h:33
@ rpcINTERNAL
Definition ErrorCodes.h:110
@ rpcUNKNOWN_COMMAND
Definition ErrorCodes.h:65
@ rpcSUCCESS
Definition ErrorCodes.h:24
beast::Journal const j
Definition Context.h:20
char const * name_
Definition Handler.h:31
Method< Json::Value > valueMethod_
Definition Handler.h:32
std::string_view forwardedFor
Definition Context.h:40
T what(T... args)