Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
RPCServerHandler.hpp
1#pragma once
2
3#include "data/BackendInterface.hpp"
4#include "etl/ETLServiceInterface.hpp"
5#include "rpc/Errors.hpp"
6#include "rpc/Factories.hpp"
7#include "rpc/JS.hpp"
8#include "rpc/RPCHelpers.hpp"
9#include "rpc/common/impl/APIVersionParser.hpp"
10#include "util/Assert.hpp"
11#include "util/CoroutineGroup.hpp"
12#include "util/JsonUtils.hpp"
13#include "util/Profiler.hpp"
14#include "util/Taggable.hpp"
15#include "util/log/Logger.hpp"
16#include "web/SubscriptionContextInterface.hpp"
17#include "web/dosguard/DOSGuardInterface.hpp"
18#include "web/ng/Connection.hpp"
19#include "web/ng/Request.hpp"
20#include "web/ng/Response.hpp"
21#include "web/ng/impl/ErrorHandling.hpp"
22
23#include <boost/asio/spawn.hpp>
24#include <boost/asio/steady_timer.hpp>
25#include <boost/beast/core/error.hpp>
26#include <boost/beast/http/status.hpp>
27#include <boost/json/array.hpp>
28#include <boost/json/object.hpp>
29#include <boost/json/parse.hpp>
30#include <boost/json/serialize.hpp>
31#include <boost/system/system_error.hpp>
32#include <xrpl/protocol/jss.h>
33
34#include <chrono>
35#include <exception>
36#include <functional>
37#include <memory>
38#include <optional>
39#include <ratio>
40#include <string>
41#include <utility>
42
43namespace web::ng {
44
50template <typename RPCEngineType>
52 std::shared_ptr<BackendInterface const> const backend_;
53 std::shared_ptr<RPCEngineType> const rpcEngine_;
54 std::shared_ptr<etl::ETLServiceInterface const> const etl_;
55 std::reference_wrapper<dosguard::DOSGuardInterface> dosguard_;
56 util::TagDecoratorFactory const tagFactory_;
57 rpc::impl::ProductionAPIVersionParser apiVersionParser_; // can be injected if needed
58
59 util::Logger log_{"RPC"};
60 util::Logger perfLog_{"Performance"};
61
62public:
74 std::shared_ptr<BackendInterface const> const& backend,
75 std::shared_ptr<RPCEngineType> const& rpcEngine,
76 std::shared_ptr<etl::ETLServiceInterface const> const& etl,
78 )
79 : backend_(backend)
80 , rpcEngine_(rpcEngine)
81 , etl_(etl)
82 , dosguard_(dosguard)
83 , tagFactory_(config)
84 , apiVersionParser_(config.getObject("api_version"))
85 {
86 }
87
97 [[nodiscard]] Response
99 Request const& request,
100 ConnectionMetadata const& connectionMetadata,
101 SubscriptionContextPtr subscriptionContext,
102 boost::asio::yield_context yield
103 )
104 {
105 if (not dosguard_.get().isOk(connectionMetadata.ip())) {
106 return makeSlowDownResponse(request, std::nullopt);
107 }
108
109 std::optional<Response> response;
110 util::CoroutineGroup coroutineGroup{yield, 1};
111 auto const onTaskComplete = coroutineGroup.registerForeign(yield);
112 ASSERT(onTaskComplete.has_value(), "Coroutine group can't be full");
113
114 bool const postSuccessful = rpcEngine_->post(
115 [this,
116 &request,
117 &response,
118 &onTaskComplete = onTaskComplete.value(),
119 &connectionMetadata,
120 subscriptionContext =
121 std::move(subscriptionContext)](boost::asio::yield_context innerYield) mutable {
122 try {
123 boost::system::error_code ec;
124 auto parsedRequest = boost::json::parse(request.message(), ec);
125 if (ec.failed() or not parsedRequest.is_object()) {
126 rpcEngine_->notifyBadSyntax();
127 response = impl::ErrorHelper{request}.makeJsonParsingError();
128 if (ec.failed()) {
129 LOG(log_.warn()) << "Error parsing JSON: " << ec.message()
130 << ". For request: " << request.message();
131 } else {
132 LOG(log_.warn())
133 << "Received not a JSON object. For request: " << request.message();
134 }
135 } else {
136 auto parsedObject = std::move(parsedRequest).as_object();
137
138 if (not dosguard_.get().request(connectionMetadata.ip(), parsedObject)) {
139 response = makeSlowDownResponse(request, parsedObject);
140 } else {
141 LOG(perfLog_.debug())
142 << connectionMetadata.tag() << "Adding to work queue";
143
144 if (not connectionMetadata.wasUpgraded() and
145 shouldReplaceParams(parsedObject)) {
146 parsedObject[JS(params)] =
147 boost::json::array({boost::json::object{}});
148 }
149
150 response = handleRequest(
151 innerYield,
152 request,
153 std::move(parsedObject),
154 connectionMetadata,
155 std::move(subscriptionContext)
156 );
157 }
158 }
159 } catch (std::exception const& ex) {
160 LOG(perfLog_.error())
161 << connectionMetadata.tag() << "Caught exception: " << ex.what();
162 rpcEngine_->notifyInternalError();
163 response = impl::ErrorHelper{request}.makeInternalError();
164 }
165
166 // notify the coroutine group that the foreign task is done
167 onTaskComplete();
168 },
169 connectionMetadata.ip()
170 );
171
172 if (not postSuccessful) {
173 // onTaskComplete must be called to notify coroutineGroup that the foreign task is done
174 onTaskComplete->operator()();
175 rpcEngine_->notifyTooBusy();
176 return impl::ErrorHelper{request}.makeTooBusyError();
177 }
178
179 // Put the coroutine to sleep until the foreign task is done
180 coroutineGroup.asyncWait(yield);
181 ASSERT(response.has_value(), "Woke up coroutine without setting response");
182
183 if (not dosguard_.get().add(connectionMetadata.ip(), response->message().size())) {
184 response->setMessage(makeLoadWarning(*response));
185 }
186
187 return std::move(response).value();
188 }
189
190private:
192 handleRequest(
193 boost::asio::yield_context yield,
194 Request const& rawRequest,
195 boost::json::object&& request,
196 ConnectionMetadata const& connectionMetadata,
197 SubscriptionContextPtr subscriptionContext
198 )
199 {
200 LOG(log_.info()) << connectionMetadata.tag()
201 << (connectionMetadata.wasUpgraded() ? "ws" : "http")
202 << " received request from work queue: " << util::removeSecret(request)
203 << " ip = " << connectionMetadata.ip();
204
205 try {
206 auto const range = backend_->fetchLedgerRange();
207 if (!range) {
208 // for error that happened before the handler, we don't attach any warnings
209 rpcEngine_->notifyNotReady();
210 return impl::ErrorHelper{rawRequest, std::move(request)}.makeNotReadyError();
211 }
212
213 auto const context = [&] {
214 if (connectionMetadata.wasUpgraded()) {
215 ASSERT(
216 subscriptionContext != nullptr,
217 "Subscription context must exist for a WS connection"
218 );
219 return rpc::makeWsContext(
220 yield,
221 request,
222 std::move(subscriptionContext),
223 tagFactory_.with(connectionMetadata.tag()),
224 *range,
225 connectionMetadata.ip(),
226 std::cref(apiVersionParser_),
227 connectionMetadata.isAdmin()
228 );
229 }
231 yield,
232 request,
233 tagFactory_.with(connectionMetadata.tag()),
234 *range,
235 connectionMetadata.ip(),
236 std::cref(apiVersionParser_),
237 connectionMetadata.isAdmin()
238 );
239 }();
240
241 if (!context) {
242 auto const err = context.error();
243 LOG(perfLog_.warn())
244 << connectionMetadata.tag() << "Could not create Web context: " << err;
245 LOG(log_.warn()) << connectionMetadata.tag()
246 << "Could not create Web context: " << err;
247
248 // we count all those as BadSyntax - as the WS path would.
249 // Although over HTTP these will yield a 400 status with a plain text response (for
250 // most).
251 rpcEngine_->notifyBadSyntax();
252 return impl::ErrorHelper(rawRequest, std::move(request)).makeError(err);
253 }
254
255 auto [result, timeDiff] =
256 util::timed([&]() { return rpcEngine_->buildResponse(*context); });
257
258 auto us = std::chrono::duration<int, std::milli>(timeDiff);
259 rpc::logDuration(request, context->tag(), us);
260
261 boost::json::object response;
262
263 if (!result.response.has_value()) {
264 // note: error statuses are counted/notified in buildResponse itself
265 response =
266 impl::ErrorHelper(rawRequest, request).composeError(result.response.error());
267 auto const responseStr = boost::json::serialize(response);
268
269 LOG(perfLog_.debug()) << context->tag() << "Encountered error: " << responseStr;
270 LOG(log_.debug()) << context->tag() << "Encountered error: " << responseStr;
271 } else {
272 auto& json = result.response.value();
273 auto const isForwarded = json.contains("forwarded") &&
274 json.at("forwarded").is_bool() && json.at("forwarded").as_bool();
275
276 // This can still technically be an error. Clio counts forwarded requests
277 // as successful.
278 rpcEngine_->notifyComplete(*context, us, isForwarded);
279
280 if (isForwarded)
281 json.erase("forwarded");
282
283 // if the result is forwarded - just use it as is
284 // if forwarded request has error, for http, error should be in "result"; for ws,
285 // error should be at top
286 if (isForwarded &&
287 (json.contains(JS(result)) || connectionMetadata.wasUpgraded())) {
288 for (auto const& [k, v] : json)
289 response.insert_or_assign(k, v);
290 } else {
291 response[JS(result)] = json;
292 }
293
294 if (isForwarded)
295 response["forwarded"] = true;
296
297 // for ws there is an additional field "status" in the response,
298 // otherwise the "status" is in the "result" field
299 if (connectionMetadata.wasUpgraded()) {
300 auto const appendFieldIfExist = [&](auto const& field) {
301 if (request.contains(field) and not request.at(field).is_null())
302 response[field] = request.at(field);
303 };
304
305 appendFieldIfExist(JS(id));
306 appendFieldIfExist(JS(api_version));
307
308 if (!response.contains(JS(error)))
309 response[JS(status)] = JS(success);
310
311 response[JS(type)] = JS(response);
312 } else {
313 if (response.contains(JS(result)) &&
314 !response[JS(result)].as_object().contains(JS(error)))
315 response[JS(result)].as_object()[JS(status)] = JS(success);
316 }
317 }
318
319 boost::json::array warnings = std::move(result.warnings);
320 warnings.emplace_back(rpc::makeWarning(rpc::WarnRpcClio));
321
322 if (etl_->lastCloseAgeSeconds() >= 60)
323 warnings.emplace_back(rpc::makeWarning(rpc::WarnRpcOutdated));
324
325 response["warnings"] = warnings;
326 return Response{boost::beast::http::status::ok, response, rawRequest};
327 } catch (std::exception const& ex) {
328 // note: while we are catching this in buildResponse too, this is here to make sure
329 // that any other code that may throw is outside of buildResponse is also worked around.
330 LOG(perfLog_.error()) << connectionMetadata.tag() << "Caught exception: " << ex.what();
331 LOG(log_.error()) << connectionMetadata.tag() << "Caught exception: " << ex.what();
332
333 rpcEngine_->notifyInternalError();
334 return impl::ErrorHelper(rawRequest, std::move(request)).makeInternalError();
335 }
336 }
337
338 static Response
339 makeSlowDownResponse(Request const& request, std::optional<boost::json::value> requestJson)
340 {
341 auto error = rpc::makeError(rpc::RippledError::rpcSLOW_DOWN);
342
343 if (not request.isHttp()) {
344 try {
345 if (not requestJson.has_value()) {
346 requestJson = boost::json::parse(request.message());
347 }
348 if (requestJson->is_object() && requestJson->as_object().contains("id"))
349 error["id"] = requestJson->as_object().at("id");
350 error["request"] = request.message();
351 } catch (std::exception const&) {
352 error["request"] = request.message();
353 }
354 }
355 return web::ng::Response{boost::beast::http::status::service_unavailable, error, request};
356 }
357
358 static boost::json::object
359 makeLoadWarning(Response const& response)
360 {
361 auto jsonResponse = boost::json::parse(response.message()).as_object();
362 jsonResponse["warning"] = "load";
363 if (jsonResponse.contains("warnings") && jsonResponse["warnings"].is_array()) {
364 jsonResponse["warnings"].as_array().push_back(rpc::makeWarning(rpc::WarnRpcRateLimit));
365 } else {
366 jsonResponse["warnings"] = boost::json::array{rpc::makeWarning(rpc::WarnRpcRateLimit)};
367 }
368 return jsonResponse;
369 }
370
371 bool
372 shouldReplaceParams(boost::json::object const& req) const
373 {
374 auto const hasParams = req.contains(JS(params));
375 auto const paramsIsArray = hasParams and req.at(JS(params)).is_array();
376 auto const paramsIsEmptyString =
377 hasParams and req.at(JS(params)).is_string() and req.at(JS(params)).as_string().empty();
378 auto const paramsIsEmptyObject =
379 hasParams and req.at(JS(params)).is_object() and req.at(JS(params)).as_object().empty();
380 auto const paramsIsNull = hasParams and req.at(JS(params)).is_null();
381 auto const arrayIsEmpty = paramsIsArray and req.at(JS(params)).as_array().empty();
382 auto const arrayIsNotEmpty = paramsIsArray and not req.at(JS(params)).as_array().empty();
383 auto const firstArgIsNull =
384 arrayIsNotEmpty and req.at(JS(params)).as_array().at(0).is_null();
385 auto const firstArgIsEmptyString = arrayIsNotEmpty and
386 req.at(JS(params)).as_array().at(0).is_string() and
387 req.at(JS(params)).as_array().at(0).as_string().empty();
388
389 // Note: all this compatibility dance is to match `rippled` as close as possible
390 return not hasParams or paramsIsEmptyString or paramsIsNull or paramsIsEmptyObject or
391 arrayIsEmpty or firstArgIsEmptyString or firstArgIsNull;
392 }
393};
394
395} // namespace web::ng
Definition APIVersionParser.hpp:15
CoroutineGroup is a helper class to manage a group of coroutines. It allows to spawn multiple corouti...
Definition CoroutineGroup.hpp:18
std::optional< std::function< void()> > registerForeign(boost::asio::yield_context yield)
Register a foreign coroutine this group should wait for.
Definition CoroutineGroup.cpp:48
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:77
Pump warn(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::WRN severity.
Definition Logger.cpp:493
Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::ERR severity.
Definition Logger.cpp:498
Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::DBG severity.
Definition Logger.cpp:483
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:488
A factory for TagDecorator instantiation.
Definition Taggable.hpp:165
TagDecoratorFactory with(ParentType parent) const noexcept
Creates a new tag decorator factory with a bound parent tag decorator.
Definition Taggable.cpp:47
BaseTagDecorator const & tag() const
Getter for tag decorator.
Definition Taggable.hpp:264
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:31
The interface of a denial of service guard.
Definition DOSGuardInterface.hpp:27
An interface for a connection metadata class.
Definition Connection.hpp:25
std::string const & ip() const
Get the ip of the client.
Definition Connection.cpp:21
RPCServerHandler(util::config::ClioConfigDefinition const &config, std::shared_ptr< BackendInterface const > const &backend, std::shared_ptr< RPCEngineType > const &rpcEngine, std::shared_ptr< etl::ETLServiceInterface const > const &etl, dosguard::DOSGuardInterface &dosguard)
Create a new server handler.
Definition RPCServerHandler.hpp:72
Response operator()(Request const &request, ConnectionMetadata const &connectionMetadata, SubscriptionContextPtr subscriptionContext, boost::asio::yield_context yield)
The callback when server receives a request.
Definition RPCServerHandler.hpp:98
Represents an HTTP or WebSocket request.
Definition Request.hpp:18
Represents an HTTP or Websocket response.
Definition Response.hpp:21
A helper that attempts to match rippled reporting mode HTTP errors as close as possible.
Definition ErrorHandling.hpp:22
Response makeTooBusyError() const
Make a response for when the server is too busy.
Definition ErrorHandling.cpp:126
boost::json::object composeError(rpc::Status const &error) const
Compose an error into json object from a status.
Definition ErrorHandling.cpp:158
Response makeError(rpc::Status const &err) const
Make an error response from a status.
Definition ErrorHandling.cpp:62
Response makeNotReadyError() const
Make a response for when the server is not ready.
Definition ErrorHandling.cpp:120
Response makeInternalError() const
Make an internal error response.
Definition ErrorHandling.cpp:110
std::expected< web::Context, Status > makeWsContext(boost::asio::yield_context yc, boost::json::object const &request, web::SubscriptionContextPtr session, util::TagDecoratorFactory const &tagFactory, data::LedgerRange const &range, std::string const &clientIp, std::reference_wrapper< APIVersionParser const > apiVersionParser, bool isAdmin)
A factory function that creates a Websocket context.
Definition Factories.cpp:28
void logDuration(boost::json::object const &request, util::BaseTagDecorator const &tag, DurationType const &dur)
Log the duration of the request processing.
Definition RPCHelpers.hpp:756
boost::json::object makeError(RippledError err, std::optional< std::string_view > customError, std::optional< std::string_view > customMessage)
Generate JSON from a rpc::RippledError.
Definition Errors.cpp:160
boost::json::object makeWarning(WarningCode code)
Generate JSON from a rpc::WarningCode.
Definition Errors.cpp:84
std::expected< web::Context, Status > makeHttpContext(boost::asio::yield_context yc, boost::json::object const &request, util::TagDecoratorFactory const &tagFactory, data::LedgerRange const &range, std::string const &clientIp, std::reference_wrapper< APIVersionParser const > apiVersionParser, bool const isAdmin)
A factory function that creates a HTTP context.
Definition Factories.cpp:63
boost::json::object removeSecret(boost::json::object const &object)
Removes any detected secret information from a response JSON object.
Definition JsonUtils.hpp:55
auto timed(FnType &&func)
Profiler function to measure the time a function execution consumes.
Definition Profiler.hpp:21
std::shared_ptr< SubscriptionContextInterface > SubscriptionContextPtr
An alias for shared pointer to a SubscriptionContextInterface.
Definition SubscriptionContextInterface.hpp:64