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/app/misc/NetworkOPs.h>
6#include <xrpld/core/Config.h>
7#include <xrpld/rpc/Context.h>
8#include <xrpld/rpc/InfoSub.h>
9#include <xrpld/rpc/RPCHandler.h>
10#include <xrpld/rpc/Role.h>
11#include <xrpld/rpc/detail/Handler.h>
12#include <xrpld/rpc/detail/Tuning.h>
13
14#include <xrpl/basics/Log.h>
15#include <xrpl/core/JobQueue.h>
16#include <xrpl/core/PerfLog.h>
17#include <xrpl/json/to_string.h>
18#include <xrpl/protocol/ErrorCodes.h>
19#include <xrpl/protocol/jss.h>
20#include <xrpl/resource/Fees.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 strCommand = context.params.isMember(jss::command) ? context.params[jss::command].asString()
133 : context.params[jss::method].asString();
134
135 JLOG(context.j.trace()) << "COMMAND:" << strCommand;
136 JLOG(context.j.trace()) << "REQUEST:" << context.params;
137 auto handler = getHandler(context.apiVersion, context.app.config().BETA_RPC_API, strCommand);
138
139 if (!handler)
140 return rpcUNKNOWN_COMMAND;
141
142 if (handler->role_ == Role::ADMIN && context.role != Role::ADMIN)
143 return rpcNO_PERMISSION;
144
145 error_code_i res = conditionMet(handler->condition_, context);
146 if (res != rpcSUCCESS)
147 {
148 return res;
149 }
150
151 result = handler;
152 return rpcSUCCESS;
153}
154
155template <class Object, class Method>
156Status
157callMethod(JsonContext& context, Method method, std::string const& name, Object& result)
158{
159 static std::atomic<std::uint64_t> requestId{0};
160 auto& perfLog = context.app.getPerfLog();
161 std::uint64_t const curId = ++requestId;
162 try
163 {
164 perfLog.rpcStart(name, curId);
165 auto v = context.app.getJobQueue().makeLoadEvent(jtGENERIC, "cmd:" + name);
166
167 auto start = std::chrono::system_clock::now();
168 auto ret = method(context, result);
170
171 JLOG(context.j.debug()) << "RPC call " << name << " completed in " << ((end - start).count() / 1000000000.0)
172 << "seconds";
173 perfLog.rpcFinish(name, curId);
174 return ret;
175 }
176 catch (std::exception& e)
177 {
178 perfLog.rpcError(name, curId);
179 JLOG(context.j.info()) << "Caught throw: " << e.what();
180
181 if (context.loadType == Resource::feeReferenceRPC)
182 context.loadType = Resource::feeExceptionRPC;
183
184 inject_error(rpcINTERNAL, result);
185 return rpcINTERNAL;
186 }
187}
188
189} // namespace
190
191Status
193{
194 Handler const* handler = nullptr;
195 if (auto error = fillHandler(context, handler))
196 {
197 inject_error(error, result);
198 return error;
199 }
200
201 if (auto method = handler->valueMethod_)
202 {
203 if (!context.headers.user.empty() || !context.headers.forwardedFor.empty())
204 {
205 JLOG(context.j.debug()) << "start command: " << handler->name_ << ", user: " << context.headers.user
206 << ", forwarded for: " << context.headers.forwardedFor;
207
208 auto ret = callMethod(context, method, handler->name_, result);
209
210 JLOG(context.j.debug()) << "finish command: " << handler->name_ << ", user: " << context.headers.user
211 << ", forwarded for: " << context.headers.forwardedFor;
212
213 return ret;
214 }
215 else
216 {
217 auto ret = callMethod(context, method, handler->name_, result);
218 return ret;
219 }
220 }
221
222 return rpcUNKNOWN_COMMAND;
223}
224
225Role
226roleRequired(unsigned int version, bool betaEnabled, std::string const& method)
227{
228 auto handler = RPC::getHandler(version, betaEnabled, method);
229
230 if (!handler)
231 return Role::FORBID;
232
233 return handler->role_;
234}
235
236} // namespace RPC
237} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
Stream debug() const
Definition Journal.h:300
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:236
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:96
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)