rippled
Loading...
Searching...
No Matches
GRPCServer.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_CORE_GRPCSERVER_H_INCLUDED
21#define RIPPLE_CORE_GRPCSERVER_H_INCLUDED
22
23#include <xrpld/app/main/Application.h>
24#include <xrpld/core/JobQueue.h>
25#include <xrpld/net/InfoSub.h>
26#include <xrpld/rpc/Context.h>
27#include <xrpld/rpc/GRPCHandlers.h>
28#include <xrpld/rpc/Role.h>
29#include <xrpld/rpc/detail/Handler.h>
30#include <xrpld/rpc/detail/RPCHelpers.h>
31#include <xrpld/rpc/detail/Tuning.h>
32#include <xrpl/protocol/ErrorCodes.h>
33#include <xrpl/resource/Charge.h>
34
35#include <xrpl/proto/org/xrpl/rpc/v1/xrp_ledger.grpc.pb.h>
36#include <grpcpp/grpcpp.h>
37
38namespace ripple {
39
40// Interface that CallData implements
42{
43public:
44 virtual ~Processor() = default;
45
46 Processor() = default;
47
48 Processor(const Processor&) = delete;
49
51 operator=(const Processor&) = delete;
52
53 // process a request that has arrived. Can only be called once per instance
54 virtual void
55 process() = 0;
56
57 // create a new instance of this CallData object, with the same type
58 //(same template parameters) as original. This is called when a CallData
59 // object starts processing a request. Creating a new instance allows the
60 // server to handle additional requests while the first is being processed
62 clone() = 0;
63
64 // true if this object has finished processing the request. Object will be
65 // deleted once this function returns true
66 virtual bool
68};
69
70class GRPCServerImpl final
71{
72private:
73 // CompletionQueue returns events that have occurred, or events that have
74 // been cancelled
76
78
79 // The gRPC service defined by the .proto files
80 org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService service_;
81
83
85
88
90
92
93 // typedef for function to bind a listener
94 // This is always of the form:
95 // org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService::Request[RPC NAME]
96 template <class Request, class Response>
98 org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService&,
99 grpc::ServerContext*,
100 Request*,
101 grpc::ServerAsyncResponseWriter<Response>*,
102 grpc::CompletionQueue*,
103 grpc::ServerCompletionQueue*,
104 void*)>;
105
106 // typedef for actual handler (that populates a response)
107 // handlers are defined in rpc/GRPCHandlers.h
108 template <class Request, class Response>
111 // This implementation is currently limited to v1 of the API
112 static unsigned constexpr apiVersion = 1;
113
114 template <class Request, class Response>
115 using Forward = std::function<grpc::Status(
116 org::xrpl::rpc::v1::XRPLedgerAPIService::Stub*,
117 grpc::ClientContext*,
118 Request,
119 Response*)>;
120
121public:
122 explicit GRPCServerImpl(Application& app);
123
125
127 operator=(const GRPCServerImpl&) = delete;
128
129 void
130 shutdown();
131
132 // setup the server and listeners
133 // returns true if server started successfully
134 bool
135 start();
136
137 // the main event loop
138 void
139 handleRpcs();
140
141 // Create a CallData object for each RPC. Return created objects in vector
144
145 // Obtaining actually binded endpoint (if port 0 was used for server setup).
146 boost::asio::ip::tcp::endpoint
147 getEndpoint() const;
148
149private:
150 // Class encompasing the state and logic needed to serve a request.
151 template <class Request, class Response>
153 : public Processor,
154 public std::enable_shared_from_this<CallData<Request, Response>>
155 {
156 private:
157 // The means of communication with the gRPC runtime for an asynchronous
158 // server.
159 org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService& service_;
160
161 // The producer-consumer queue for asynchronous server notifications.
162 grpc::ServerCompletionQueue& cq_;
163
164 // Context for the rpc, allowing to tweak aspects of it such as the use
165 // of compression, authentication, as well as to send metadata back to
166 // the client.
167 grpc::ServerContext ctx_;
168
169 // true if finished processing request
170 // Note, this variable does not need to be atomic, since it is
171 // currently only accessed from one thread. However, isFinished(),
172 // which returns the value of this variable, is public facing. In the
173 // interest of avoiding future concurrency bugs, we make it atomic.
175
177
178 // What we get from the client.
179 Request request_;
180
181 // The means to get back to the client.
182 grpc::ServerAsyncResponseWriter<Response> responder_;
183
184 // Function that creates a listener for specific request type
186
187 // Function that processes a request
189
190 // Function to call to forward to another server
192
193 // Condition required for this RPC
195
196 // Load type for this RPC
198
200
201 public:
202 virtual ~CallData() = default;
203
204 // Take in the "service" instance (in this case representing an
205 // asynchronous server) and the completion queue "cq" used for
206 // asynchronous communication with the gRPC runtime.
207 explicit CallData(
208 org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService& service,
209 grpc::ServerCompletionQueue& cq,
210 Application& app,
214 RPC::Condition requiredCondition,
215 Resource::Charge loadType,
216 std::vector<boost::asio::ip::address> const& secureGatewayIPs);
217
218 CallData(const CallData&) = delete;
219
220 CallData&
221 operator=(const CallData&) = delete;
222
223 virtual void
224 process() override;
225
226 virtual bool
227 isFinished() override;
228
230 clone() override;
231
232 private:
233 // process the request. Called inside the coroutine passed to JobQueue
234 void
236
237 // return load type of this RPC
239 getLoadType();
240
241 // return the Role used for this RPC
242 Role
243 getRole(bool isUnlimited);
244
245 // register endpoint with ResourceManager and return usage
247 getUsage();
248
249 // Returns the ip of the client
250 // Empty optional if there was an error decoding the client ip
253
254 // Returns the endpoint of the client.
255 // Empty optional if there was an error decoding the client
256 // endpoint
259
260 // If the request was proxied through
261 // another rippled node, returns the ip of the originating client.
262 // Empty optional if request was not proxied or there was an error
263 // decoding the client ip
266
267 // If the request was proxied through
268 // another rippled node, returns the endpoint of the originating client.
269 // Empty optional if request was not proxied or there was an error
270 // decoding the client endpoint
273
274 // Returns the user specified in the request. Empty optional if no user
275 // was specified
277 getUser();
278
279 // Sets is_unlimited in response to value of clientIsUnlimited
280 // Does nothing if is_unlimited is not a field of the response
281 void
282 setIsUnlimited(Response& response, bool isUnlimited);
283
284 // True if the client is exempt from resource controls
285 bool
287
288 // True if the request was proxied through another rippled node prior
289 // to arriving here
290 bool
292
293 // forward request to a p2p node
294 void
296
297 }; // CallData
298
299}; // GRPCServerImpl
300
302{
303public:
304 explicit GRPCServer(Application& app) : impl_(app)
305 {
306 }
307
308 GRPCServer(const GRPCServer&) = delete;
309
311 operator=(const GRPCServer&) = delete;
312
313 bool
314 start();
315
316 void
317 stop();
318
319 ~GRPCServer();
320
321 boost::asio::ip::tcp::endpoint
322 getEndpoint() const;
323
324private:
327 bool running_ = false;
328};
329} // namespace ripple
330#endif
A generic endpoint for log messages.
Definition: Journal.h:59
Forward< Request, Response > forward_
Definition: GRPCServer.h:191
Resource::Consumer getUsage()
Definition: GRPCServer.cpp:310
std::optional< std::string > getUser()
Definition: GRPCServer.cpp:243
grpc::ServerCompletionQueue & cq_
Definition: GRPCServer.h:162
grpc::ServerContext ctx_
Definition: GRPCServer.h:167
Handler< Request, Response > handler_
Definition: GRPCServer.h:188
Resource::Charge getLoadType()
Definition: GRPCServer.cpp:226
void setIsUnlimited(Response &response, bool isUnlimited)
Definition: GRPCServer.cpp:294
std::optional< boost::asio::ip::address > getClientIpAddress()
Definition: GRPCServer.cpp:259
std::optional< boost::asio::ip::tcp::endpoint > getProxiedClientEndpoint()
CallData & operator=(const CallData &)=delete
BindListener< Request, Response > bindListener_
Definition: GRPCServer.h:185
grpc::ServerAsyncResponseWriter< Response > responder_
Definition: GRPCServer.h:182
Role getRole(bool isUnlimited)
Definition: GRPCServer.cpp:233
std::vector< boost::asio::ip::address > const & secureGatewayIPs_
Definition: GRPCServer.h:199
std::shared_ptr< Processor > clone() override
Definition: GRPCServer.cpp:89
CallData(const CallData &)=delete
std::optional< boost::asio::ip::tcp::endpoint > getClientEndpoint()
Definition: GRPCServer.cpp:269
std::optional< boost::asio::ip::address > getProxiedClientIpAddress()
org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService & service_
Definition: GRPCServer.h:159
virtual void process() override
Definition: GRPCServer.cpp:105
virtual bool isFinished() override
Definition: GRPCServer.cpp:219
void forwardToP2p(RPC::GRPCContext< Request > &context)
std::string serverAddress_
Definition: GRPCServer.h:86
Application & app_
Definition: GRPCServer.h:84
std::vector< std::shared_ptr< Processor > > setupListeners()
Definition: GRPCServer.cpp:478
GRPCServerImpl & operator=(const GRPCServerImpl &)=delete
std::unique_ptr< grpc::Server > server_
Definition: GRPCServer.h:82
std::unique_ptr< grpc::ServerCompletionQueue > cq_
Definition: GRPCServer.h:75
org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService service_
Definition: GRPCServer.h:80
std::vector< std::shared_ptr< Processor > > requests_
Definition: GRPCServer.h:77
static unsigned constexpr apiVersion
Definition: GRPCServer.h:112
boost::asio::ip::tcp::endpoint getEndpoint() const
Definition: GRPCServer.cpp:587
std::vector< boost::asio::ip::address > secureGatewayIPs_
Definition: GRPCServer.h:89
std::uint16_t serverPort_
Definition: GRPCServer.h:87
GRPCServerImpl(const GRPCServerImpl &)=delete
beast::Journal journal_
Definition: GRPCServer.h:91
GRPCServer(Application &app)
Definition: GRPCServer.h:304
GRPCServer & operator=(const GRPCServer &)=delete
std::thread thread_
Definition: GRPCServer.h:326
boost::asio::ip::tcp::endpoint getEndpoint() const
Definition: GRPCServer.cpp:627
GRPCServer(const GRPCServer &)=delete
GRPCServerImpl impl_
Definition: GRPCServer.h:325
virtual void process()=0
Processor(const Processor &)=delete
virtual bool isFinished()=0
Processor & operator=(const Processor &)=delete
Processor()=default
virtual std::shared_ptr< Processor > clone()=0
virtual ~Processor()=default
A consumption charge.
Definition: Charge.h:31
An endpoint that consumes resources.
Definition: Consumer.h:35
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
bool isUnlimited(Role const &role)
ADMIN and IDENTIFIED roles shall have unlimited resources.
Definition: Role.cpp:124
Role
Indicates the level of administrative permission to grant.
Definition: Role.h:43