20#include <xrpld/app/main/GRPCServer.h>
21#include <xrpld/core/ConfigSections.h>
23#include <xrpl/beast/core/CurrentThreadName.h>
24#include <xrpl/beast/net/IPAddressConversion.h>
25#include <xrpl/resource/Fees.h>
43 peerClean = peer.
substr(first + 1);
59template <
class Request,
class Response>
61 org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService& service,
62 grpc::ServerCompletionQueue& cq,
75 , bindListener_(
std::move(bindListener))
76 , handler_(
std::move(handler))
78 , requiredCondition_(
std::move(requiredCondition))
79 , loadType_(
std::move(loadType))
80 , secureGatewayIPs_(secureGatewayIPs)
87template <
class Request,
class Response>
103template <
class Request,
class Response>
108 BOOST_ASSERT(!finished_);
111 this->shared_from_this();
126 thisShared->process(coro);
133 grpc::StatusCode::INTERNAL,
"Job Queue is already stopped"};
134 responder_.FinishWithError(status,
this);
138template <
class Request,
class Response>
145 auto usage = getUsage();
150 grpc::StatusCode::RESOURCE_EXHAUSTED,
151 "usage balance exceeds threshold"};
152 responder_.FinishWithError(status,
this);
156 auto loadType = getLoadType();
157 usage.charge(loadType);
162 toLog <<
"role = " << (int)role;
164 toLog <<
" address = ";
165 if (
auto clientIp = getClientIpAddress())
166 toLog << clientIp.value();
169 if (
auto user = getUser())
170 toLog << user.value();
198 grpc::StatusCode::FAILED_PRECONDITION,
200 responder_.FinishWithError(status,
this);
206 responder_.Finish(result.
first, result.
second,
this);
212 grpc::Status status{grpc::StatusCode::INTERNAL, ex.
what()};
213 responder_.FinishWithError(status,
this);
217template <
class Request,
class Response>
224template <
class Request,
class Response>
231template <
class Request,
class Response>
241template <
class Request,
class Response>
245 if (
auto descriptor = Request::GetDescriptor()->FindFieldByName(
"user"))
248 Request::GetReflection()->GetString(request_, descriptor);
257template <
class Request,
class Response>
261 auto endpoint = getClientEndpoint();
263 return endpoint->address();
267template <
class Request,
class Response>
271 return ripple::getEndpoint(ctx_.peer());
274template <
class Request,
class Response>
280 auto clientIp = getClientIpAddress();
292template <
class Request,
class Response>
300 if (
auto descriptor =
301 Response::GetDescriptor()->FindFieldByName(
"is_unlimited"))
303 Response::GetReflection()->SetBool(&response, descriptor,
true);
308template <
class Request,
class Response>
312 auto endpoint = getClientEndpoint();
316 Throw<std::runtime_error>(
"Failed to get client endpoint");
327 auto const optIp = section.
get(
"ip");
331 auto const optPort = section.
get(
"port");
336 boost::asio::ip::tcp::endpoint endpoint(
337 boost::asio::ip::make_address(*optIp),
std::stoi(*optPort));
345 JLOG(
journal_.
error()) <<
"Error setting grpc server address";
346 Throw<std::runtime_error>(
"Error setting grpc server address");
349 auto const optSecureGateway = section.
get(
"secure_gateway");
350 if (optSecureGateway)
358 boost::algorithm::trim(ip);
359 auto const addr = boost::asio::ip::make_address(ip);
361 if (addr.is_unspecified())
364 <<
"Can't pass unspecified IP in "
365 <<
"secure_gateway section of port_grpc";
366 Throw<std::runtime_error>(
367 "Unspecified IP in secure_gateway section");
376 <<
"Error parsing secure gateway IPs for grpc server";
377 Throw<std::runtime_error>(
378 "Error parsing secure_gateway section");
403 JLOG(
journal_.
debug()) <<
"Completion Queue has been shutdown";
419 return sPtr.get() == ptr;
421 BOOST_ASSERT(it != requests.
end());
422 it->swap(requests.
back());
442 while (
cq_->Next(&tag, &ok))
446 <<
" ptr = " << ptr <<
" ok = " << ok;
451 <<
"Destroying object";
456 if (!ptr->isFinished())
458 JLOG(
journal_.
debug()) <<
"Received new request. Processing";
461 auto cloned = ptr->clone();
468 JLOG(
journal_.
debug()) <<
"Sent response. Destroying object";
482 auto addToRequests = [&requests](
auto callData) {
488 org::xrpl::rpc::v1::GetLedgerRequest,
489 org::xrpl::rpc::v1::GetLedgerResponse>;
495 &org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService::
498 &org::xrpl::rpc::v1::XRPLedgerAPIService::Stub::GetLedger,
505 org::xrpl::rpc::v1::GetLedgerDataRequest,
506 org::xrpl::rpc::v1::GetLedgerDataResponse>;
512 &org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService::
513 RequestGetLedgerData,
515 &org::xrpl::rpc::v1::XRPLedgerAPIService::Stub::GetLedgerData,
522 org::xrpl::rpc::v1::GetLedgerDiffRequest,
523 org::xrpl::rpc::v1::GetLedgerDiffResponse>;
529 &org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService::
530 RequestGetLedgerDiff,
532 &org::xrpl::rpc::v1::XRPLedgerAPIService::Stub::GetLedgerDiff,
539 org::xrpl::rpc::v1::GetLedgerEntryRequest,
540 org::xrpl::rpc::v1::GetLedgerEntryResponse>;
546 &org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService::
547 RequestGetLedgerEntry,
549 &org::xrpl::rpc::v1::XRPLedgerAPIService::Stub::GetLedgerEntry,
566 grpc::ServerBuilder builder;
571 builder.AddListeningPort(
578 cq_ = builder.AddCompletionQueue();
580 server_ = builder.BuildAndStart();
586boost::asio::ip::tcp::endpoint
591 return boost::asio::ip::tcp::endpoint(
623 XRPL_ASSERT(!
running_,
"ripple::GRPCServer::~GRPCServer : is not running");
626boost::asio::ip::tcp::endpoint
constexpr char const * c_str() const
static std::optional< Endpoint > from_string_checked(std::string const &s)
Create an Endpoint from a string.
Stream trace() const
Severity stream access functions.
virtual Config & config()=0
virtual beast::Journal journal(std::string const &name)=0
virtual JobQueue & getJobQueue()=0
virtual Resource::Manager & getResourceManager()=0
virtual NetworkOPs & getOPs()=0
virtual LedgerMaster & getLedgerMaster()=0
bool exists(std::string const &name) const
Returns true if a section with the given name exists.
Section & section(std::string const &name)
Returns the section with the given name.
Resource::Consumer getUsage()
std::optional< std::string > getUser()
grpc::ServerCompletionQueue & cq_
Resource::Charge getLoadType()
void setIsUnlimited(Response &response, bool isUnlimited)
std::optional< boost::asio::ip::address > getClientIpAddress()
BindListener< Request, Response > bindListener_
grpc::ServerAsyncResponseWriter< Response > responder_
Role getRole(bool isUnlimited)
std::shared_ptr< Processor > clone() override
CallData(org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService &service, grpc::ServerCompletionQueue &cq, Application &app, BindListener< Request, Response > bindListener, Handler< Request, Response > handler, Forward< Request, Response > forward, RPC::Condition requiredCondition, Resource::Charge loadType, std::vector< boost::asio::ip::address > const &secureGatewayIPs)
std::optional< boost::asio::ip::tcp::endpoint > getClientEndpoint()
org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService & service_
virtual void process() override
virtual bool isFinished() override
std::string serverAddress_
std::vector< std::shared_ptr< Processor > > setupListeners()
std::unique_ptr< grpc::Server > server_
std::unique_ptr< grpc::ServerCompletionQueue > cq_
org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService service_
GRPCServerImpl(Application &app)
static unsigned constexpr apiVersion
boost::asio::ip::tcp::endpoint getEndpoint() const
std::vector< boost::asio::ip::address > secureGatewayIPs_
std::uint16_t serverPort_
boost::asio::ip::tcp::endpoint getEndpoint() const
std::shared_ptr< InfoSub > pointer
std::shared_ptr< Coro > postCoro(JobType t, std::string const &name, F &&f)
Creates a coroutine and adds a job to the queue which will run it.
An endpoint that consumes resources.
virtual Consumer newInboundEndpoint(beast::IP::Endpoint const &address)=0
Create a new endpoint keyed by inbound IP address or the forwarded IP if proxied.
Holds a collection of configuration values.
std::optional< T > get(std::string const &name) const
T emplace_back(T... args)
T find_first_of(T... args)
T find_last_of(T... args)
boost::asio::ip::tcp::endpoint to_asio_endpoint(Endpoint const &endpoint)
Convert to asio::ip::tcp::endpoint.
Endpoint from_asio(boost::asio::ip::address const &address)
Convert to Endpoint.
void setCurrentThreadName(std::string_view newThreadName)
Changes the name of the caller thread.
error_code_i conditionMet(Condition condition_required, T &context)
ErrorInfo const & get_error_info(error_code_i code)
Returns an ErrorInfo that reflects the error code.
Charge const feeMediumBurdenRPC
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
bool isUnlimited(Role const &role)
ADMIN and IDENTIFIED roles shall have unlimited resources.
std::pair< org::xrpl::rpc::v1::GetLedgerResponse, grpc::Status > doLedgerGrpc(RPC::GRPCContext< org::xrpl::rpc::v1::GetLedgerRequest > &context)
std::pair< org::xrpl::rpc::v1::GetLedgerEntryResponse, grpc::Status > doLedgerEntryGrpc(RPC::GRPCContext< org::xrpl::rpc::v1::GetLedgerEntryRequest > &context)
void erase(STObject &st, TypedField< U > const &f)
Remove a field in an STObject.
Role
Indicates the level of administrative permission to grant.
std::pair< org::xrpl::rpc::v1::GetLedgerDiffResponse, grpc::Status > doLedgerDiffGrpc(RPC::GRPCContext< org::xrpl::rpc::v1::GetLedgerDiffRequest > &context)
std::pair< org::xrpl::rpc::v1::GetLedgerDataResponse, grpc::Status > doLedgerDataGrpc(RPC::GRPCContext< org::xrpl::rpc::v1::GetLedgerDataRequest > &context)
Maps an rpc error code to its token, default message, and HTTP status.
Json::StaticString message