Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
SourceImpl.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, the clio developers.
5
6 Permission to use, copy, modify, and 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#pragma once
21
22#include "etl/Source.hpp"
23#include "etl/impl/ForwardingSource.hpp"
24#include "etl/impl/GrpcSource.hpp"
25#include "etl/impl/SubscriptionSource.hpp"
26#include "rpc/Errors.hpp"
27
28#include <boost/asio/spawn.hpp>
29#include <boost/json/object.hpp>
30#include <grpcpp/support/status.h>
31#include <org/xrpl/rpc/v1/get_ledger.pb.h>
32
33#include <chrono>
34#include <cstdint>
35#include <expected>
36#include <memory>
37#include <optional>
38#include <string>
39#include <string_view>
40#include <utility>
41#include <vector>
42
43namespace etl::impl {
44
52template <
53 typename GrpcSourceType = GrpcSource,
54 typename SubscriptionSourceTypePtr = std::unique_ptr<SubscriptionSource>,
55 typename ForwardingSourceType = ForwardingSource>
56class SourceImpl : public SourceBase {
57 std::string ip_;
58 std::string wsPort_;
59 std::string grpcPort_;
60
61 GrpcSourceType grpcSource_;
62 SubscriptionSourceTypePtr subscriptionSource_;
63 ForwardingSourceType forwardingSource_;
64
65public:
76 template <typename SomeGrpcSourceType, typename SomeForwardingSourceType>
77 requires std::is_same_v<GrpcSourceType, SomeGrpcSourceType> and
78 std::is_same_v<ForwardingSourceType, SomeForwardingSourceType>
80 std::string ip,
81 std::string wsPort,
82 std::string grpcPort,
83 SomeGrpcSourceType&& grpcSource,
84 SubscriptionSourceTypePtr subscriptionSource,
85 SomeForwardingSourceType&& forwardingSource
86 )
87 : ip_(std::move(ip))
88 , wsPort_(std::move(wsPort))
89 , grpcPort_(std::move(grpcPort))
90 , grpcSource_(std::forward<SomeGrpcSourceType>(grpcSource))
91 , subscriptionSource_(std::move(subscriptionSource))
92 , forwardingSource_(std::forward<SomeForwardingSourceType>(forwardingSource))
93 {
94 }
95
99 void
100 run() final
101 {
102 subscriptionSource_->run();
103 }
104
105 void
106 stop(boost::asio::yield_context yield) final
107 {
108 subscriptionSource_->stop(yield);
109 }
110
116 bool
117 isConnected() const final
118 {
119 return subscriptionSource_->isConnected();
120 }
121
127 void
128 setForwarding(bool isForwarding) final
129 {
130 subscriptionSource_->setForwarding(isForwarding);
131 }
132
138 boost::json::object
139 toJson() const final
140 {
141 boost::json::object res;
142
143 res["validated_range"] = subscriptionSource_->validatedRange();
144 res["is_connected"] = std::to_string(static_cast<int>(subscriptionSource_->isConnected()));
145 res["ip"] = ip_;
146 res["ws_port"] = wsPort_;
147 res["grpc_port"] = grpcPort_;
148
149 auto last = subscriptionSource_->lastMessageTime();
150 if (last.time_since_epoch().count() != 0) {
151 res["last_msg_age_seconds"] = std::to_string(
152 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - last).count()
153 );
154 }
155
156 return res;
157 }
158
160 std::string
161 toString() const final
162 {
163 return "{validated range: " + subscriptionSource_->validatedRange() + ", ip: " + ip_ +
164 ", web socket port: " + wsPort_ + ", grpc port: " + grpcPort_ + "}";
165 }
166
173 bool
174 hasLedger(uint32_t sequence) const final
175 {
176 return subscriptionSource_->hasLedger(sequence);
177 }
178
190 std::pair<grpc::Status, org::xrpl::rpc::v1::GetLedgerResponse>
191 fetchLedger(uint32_t sequence, bool getObjects = true, bool getObjectNeighbors = false) final
192 {
193 return grpcSource_.fetchLedger(sequence, getObjects, getObjectNeighbors);
194 }
195
204 std::pair<std::vector<std::string>, bool>
205 loadInitialLedger(uint32_t sequence, std::uint32_t numMarkers, bool cacheOnly = false) final
206 {
207 return grpcSource_.loadInitialLedger(sequence, numMarkers, cacheOnly);
208 }
209
219 std::expected<boost::json::object, rpc::ClioError>
221 boost::json::object const& request,
222 std::optional<std::string> const& forwardToRippledClientIp,
223 std::string_view xUserValue,
224 boost::asio::yield_context yield
225 ) const final
226 {
227 return forwardingSource_.forwardToRippled(request, forwardToRippledClientIp, xUserValue, yield);
228 }
229};
230
231} // namespace etl::impl
Provides an implementation of a ETL source.
Definition Source.hpp:54
Provides an implementation of a ETL source.
Definition SourceImpl.hpp:56
std::string toString() const final
Definition SourceImpl.hpp:161
boost::json::object toJson() const final
Represent the source as a JSON object.
Definition SourceImpl.hpp:139
void run() final
Run subscriptions loop of the source.
Definition SourceImpl.hpp:100
std::expected< boost::json::object, rpc::ClioError > forwardToRippled(boost::json::object const &request, std::optional< std::string > const &forwardToRippledClientIp, std::string_view xUserValue, boost::asio::yield_context yield) const final
Forward a request to rippled.
Definition SourceImpl.hpp:220
bool isConnected() const final
Check if source is connected.
Definition SourceImpl.hpp:117
std::pair< grpc::Status, org::xrpl::rpc::v1::GetLedgerResponse > fetchLedger(uint32_t sequence, bool getObjects=true, bool getObjectNeighbors=false) final
Fetch data for a specific ledger.
Definition SourceImpl.hpp:191
SourceImpl(std::string ip, std::string wsPort, std::string grpcPort, SomeGrpcSourceType &&grpcSource, SubscriptionSourceTypePtr subscriptionSource, SomeForwardingSourceType &&forwardingSource)
Construct a new SourceImpl object.
Definition SourceImpl.hpp:79
std::pair< std::vector< std::string >, bool > loadInitialLedger(uint32_t sequence, std::uint32_t numMarkers, bool cacheOnly=false) final
Download a ledger in full.
Definition SourceImpl.hpp:205
bool hasLedger(uint32_t sequence) const final
Check if ledger is known by this source.
Definition SourceImpl.hpp:174
void setForwarding(bool isForwarding) final
Set the forwarding state of the source.
Definition SourceImpl.hpp:128
void stop(boost::asio::yield_context yield) final
Stop Source.
Definition SourceImpl.hpp:106