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