xrpld
Loading...
Searching...
No Matches
GRPCServer.h
1#pragma once
2
3#include <xrpld/app/main/Application.h>
4#include <xrpld/rpc/Context.h>
5#include <xrpld/rpc/GRPCHandlers.h>
6#include <xrpld/rpc/Role.h>
7#include <xrpld/rpc/detail/Handler.h>
8
9#include <xrpl/core/JobQueue.h>
10#include <xrpl/proto/org/xrpl/rpc/v1/xrp_ledger.grpc.pb.h>
11#include <xrpl/resource/Charge.h>
12#include <xrpl/server/InfoSub.h>
13
14#include <grpcpp/grpcpp.h>
15
16namespace xrpl {
17
18// Interface that CallData implements
20{
21public:
22 virtual ~Processor() = default;
23
24 Processor() = default;
25
26 Processor(Processor const&) = delete;
27
29 operator=(Processor const&) = delete;
30
31 // process a request that has arrived. Can only be called once per instance
32 virtual void
33 process() = 0;
34
35 // create a new instance of this CallData object, with the same type
36 //(same template parameters) as original. This is called when a CallData
37 // object starts processing a request. Creating a new instance allows the
38 // server to handle additional requests while the first is being processed
40 clone() = 0;
41
42 // true if this object has finished processing the request. Object will be
43 // deleted once this function returns true
44 virtual bool
46};
47
48class GRPCServerImpl final
49{
50private:
51 // CompletionQueue returns events that have occurred, or events that have
52 // been cancelled
54
56
57 // The gRPC service defined by the .proto files
58 org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService service_;
59
61
63
66
68
69 // TLS certificate paths
72 std::optional<std::string> sslCertChainPath_; // Intermediate CA certs for server cert chain
74 sslClientCAPath_; // CA cert for client certificate verification (mTLS)
75
77
78 // typedef for function to bind a listener
79 // This is always of the form:
80 // org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService::Request[RPC NAME]
81 template <class Request, class Response>
83 org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService&,
84 grpc::ServerContext*,
85 Request*,
86 grpc::ServerAsyncResponseWriter<Response>*,
87 grpc::CompletionQueue*,
88 grpc::ServerCompletionQueue*,
89 void*)>;
90
91 // typedef for actual handler (that populates a response)
92 // handlers are defined in rpc/GRPCHandlers.h
93 template <class Request, class Response>
95 // This implementation is currently limited to v1 of the API
96 static constexpr unsigned kApiVersion = 1;
97
98 template <class Request, class Response>
99 using Forward = std::function<grpc::Status(
100 org::xrpl::rpc::v1::XRPLedgerAPIService::Stub*,
101 grpc::ClientContext*,
102 Request,
103 Response*)>;
104
105public:
106 explicit GRPCServerImpl(Application& app);
107
109
111 operator=(GRPCServerImpl const&) = delete;
112
113 void
114 shutdown();
115
116 // setup the server and listeners
117 // returns true if server started successfully
118 bool
119 start();
120
121 // the main event loop
122 void
123 handleRpcs();
124
125 // Create a CallData object for each RPC. Return created objects in vector
128
129 // Obtaining actually binded endpoint (if port 0 was used for server setup).
130 [[nodiscard]] boost::asio::ip::tcp::endpoint
131 getEndpoint() const;
132
133private:
134 // Create server credentials (TLS or insecure) based on configuration
137
138 // Class encompassing the state and logic needed to serve a request.
139 template <class Request, class Response>
140 class CallData : public Processor,
141 public std::enable_shared_from_this<CallData<Request, Response>>
142 {
143 private:
144 // The means of communication with the gRPC runtime for an asynchronous
145 // server.
146 org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService& service_;
147
148 // The producer-consumer queue for asynchronous server notifications.
149 grpc::ServerCompletionQueue& cq_;
150
151 // Context for the rpc, allowing to tweak aspects of it such as the use
152 // of compression, authentication, as well as to send metadata back to
153 // the client.
154 grpc::ServerContext ctx_;
155
156 // true if finished processing request
157 // Note, this variable does not need to be atomic, since it is
158 // currently only accessed from one thread. However, isFinished(),
159 // which returns the value of this variable, is public facing. In the
160 // interest of avoiding future concurrency bugs, we make it atomic.
162
164
165 // What we get from the client.
166 Request request_;
167
168 // The means to get back to the client.
169 grpc::ServerAsyncResponseWriter<Response> responder_;
170
171 // Function that creates a listener for specific request type
173
174 // Function that processes a request
176
177 // Function to call to forward to another server
179
180 // Condition required for this RPC
182
183 // Load type for this RPC
185
187
188 public:
189 ~CallData() override = default;
190
191 // Take in the "service" instance (in this case representing an
192 // asynchronous server) and the completion queue "cq" used for
193 // asynchronous communication with the gRPC runtime.
194 explicit CallData(
195 org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService& service,
196 grpc::ServerCompletionQueue& cq,
197 Application& app,
201 RPC::Condition requiredCondition,
202 Resource::Charge loadType,
203 std::vector<boost::asio::ip::address> const& secureGatewayIPs);
204
205 CallData(CallData const&) = delete;
206
207 CallData&
208 operator=(CallData const&) = delete;
209
210 void
211 process() override;
212
213 bool
214 isFinished() override;
215
217 clone() override;
218
219 private:
220 // process the request. Called inside the coroutine passed to JobQueue
221 void
223
224 // return load type of this RPC
226 getLoadType();
227
228 // return the Role used for this RPC
229 Role
230 getRole(bool isUnlimited);
231
232 // register endpoint with ResourceManager and return usage
234 getUsage();
235
236 // Returns the ip of the client
237 // Empty optional if there was an error decoding the client ip
240
241 // Returns the endpoint of the client.
242 // Empty optional if there was an error decoding the client
243 // endpoint
246
247 // If the request was proxied through
248 // another xrpld node, returns the ip of the originating client.
249 // Empty optional if request was not proxied or there was an error
250 // decoding the client ip
253
254 // If the request was proxied through
255 // another xrpld node, returns the endpoint of the originating client.
256 // Empty optional if request was not proxied or there was an error
257 // decoding the client endpoint
260
261 // Returns the user specified in the request. Empty optional if no user
262 // was specified
264 getUser();
265
266 // Sets is_unlimited in response to value of clientIsUnlimited
267 // Does nothing if is_unlimited is not a field of the response
268 void
269 setIsUnlimited(Response& response, bool isUnlimited);
270
271 // True if the client is exempt from resource controls
272 bool
274
275 // True if the request was proxied through another xrpld node prior
276 // to arriving here
277 bool
279
280 // forward request to a p2p node
281 void
283
284 }; // CallData
285
286}; // GRPCServerImpl
287
289{
290public:
291 explicit GRPCServer(Application& app) : impl_(app)
292 {
293 }
294
295 GRPCServer(GRPCServer const&) = delete;
296
298 operator=(GRPCServer const&) = delete;
299
300 bool
301 start();
302
303 void
304 stop();
305
306 ~GRPCServer();
307
308 [[nodiscard]] boost::asio::ip::tcp::endpoint
309 getEndpoint() const;
310
311private:
314 bool running_ = false;
315};
316} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
Forward< Request, Response > forward_
Definition GRPCServer.h:178
Resource::Consumer getUsage()
CallData(CallData const &)=delete
std::vector< boost::asio::ip::address > const & secureGatewayIPs_
Definition GRPCServer.h:186
std::optional< boost::asio::ip::address > getClientIpAddress()
org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService & service_
Definition GRPCServer.h:146
Handler< Request, Response > handler_
Definition GRPCServer.h:175
RPC::Condition requiredCondition_
Definition GRPCServer.h:181
std::optional< std::string > getUser()
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)
BindListener< Request, Response > bindListener_
Definition GRPCServer.h:172
Role getRole(bool isUnlimited)
CallData & operator=(CallData const &)=delete
Resource::Charge getLoadType()
~CallData() override=default
std::optional< boost::asio::ip::address > getProxiedClientIpAddress()
std::optional< boost::asio::ip::tcp::endpoint > getClientEndpoint()
grpc::ServerAsyncResponseWriter< Response > responder_
Definition GRPCServer.h:169
std::shared_ptr< Processor > clone() override
void setIsUnlimited(Response &response, bool isUnlimited)
grpc::ServerCompletionQueue & cq_
Definition GRPCServer.h:149
void forwardToP2p(RPC::GRPCContext< Request > &context)
std::optional< boost::asio::ip::tcp::endpoint > getProxiedClientEndpoint()
grpc::ServerContext ctx_
Definition GRPCServer.h:154
std::uint16_t serverPort_
Definition GRPCServer.h:65
GRPCServerImpl & operator=(GRPCServerImpl const &)=delete
Application & app_
Definition GRPCServer.h:62
GRPCServerImpl(GRPCServerImpl const &)=delete
std::optional< std::string > sslKeyPath_
Definition GRPCServer.h:71
std::optional< std::string > sslCertPath_
Definition GRPCServer.h:70
std::function< grpc::Status( org::xrpl::rpc::v1::XRPLedgerAPIService::Stub *, grpc::ClientContext *, Request, Response *)> Forward
Definition GRPCServer.h:99
std::function< std::pair< Response, grpc::Status >(RPC::GRPCContext< Request > &)> Handler
Definition GRPCServer.h:94
std::vector< boost::asio::ip::address > secureGatewayIPs_
Definition GRPCServer.h:67
std::optional< std::string > sslCertChainPath_
Definition GRPCServer.h:72
std::unique_ptr< grpc::ServerCompletionQueue > cq_
Definition GRPCServer.h:53
std::vector< std::shared_ptr< Processor > > setupListeners()
std::optional< std::string > sslClientCAPath_
Definition GRPCServer.h:74
GRPCServerImpl(Application &app)
beast::Journal journal_
Definition GRPCServer.h:76
std::unique_ptr< grpc::Server > server_
Definition GRPCServer.h:60
boost::asio::ip::tcp::endpoint getEndpoint() const
std::vector< std::shared_ptr< Processor > > requests_
Definition GRPCServer.h:55
std::string serverAddress_
Definition GRPCServer.h:64
org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService service_
Definition GRPCServer.h:58
std::function< void( org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService &, grpc::ServerContext *, Request *, grpc::ServerAsyncResponseWriter< Response > *, grpc::CompletionQueue *, grpc::ServerCompletionQueue *, void *)> BindListener
Definition GRPCServer.h:82
static constexpr unsigned kApiVersion
Definition GRPCServer.h:96
std::shared_ptr< grpc::ServerCredentials > createServerCredentials()
std::thread thread_
Definition GRPCServer.h:313
boost::asio::ip::tcp::endpoint getEndpoint() const
GRPCServerImpl impl_
Definition GRPCServer.h:312
GRPCServer & operator=(GRPCServer const &)=delete
GRPCServer(Application &app)
Definition GRPCServer.h:291
GRPCServer(GRPCServer const &)=delete
Processor(Processor const &)=delete
Processor()=default
virtual ~Processor()=default
virtual bool isFinished()=0
Processor & operator=(Processor const &)=delete
virtual void process()=0
virtual std::shared_ptr< Processor > clone()=0
A consumption charge.
Definition Charge.h:9
An endpoint that consumes resources.
Definition Consumer.h:15
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
bool isUnlimited(Role const &role)
ADMIN and IDENTIFIED roles shall have unlimited resources.
Definition Role.cpp:115