3#include "data/BackendInterface.hpp"
5#include "etl/ETLServiceInterface.hpp"
7#include "feed/SubscriptionManagerInterface.hpp"
9#include "rpc/common/SpecBackend.hpp"
10#include "rpc/common/Types.hpp"
11#include "util/Assert.hpp"
12#include "util/Concepts.hpp"
13#include "util/build/Build.hpp"
15#include <boost/json/conversion.hpp>
16#include <boost/json/object.hpp>
17#include <boost/json/value.hpp>
18#include <fmt/format.h>
19#include <rpcspec/Errors.hpp>
20#include <rpcspec/handlers/server_info/Types.hpp>
21#include <xrpl/basics/chrono.h>
22#include <xrpl/basics/strHex.h>
23#include <xrpl/protocol/BuildInfo.h>
24#include <xrpl/protocol/Fees.h>
25#include <xrpl/protocol/Protocol.h>
26#include <xrpl/protocol/jss.h>
49template <
typename CountersType, util::SomeSystemClock ClockType = std::chrono::system_clock>
51 static constexpr auto kBackendCountersKey =
"backend_counters";
53 std::shared_ptr<BackendInterface> backend_;
54 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions_;
55 std::shared_ptr<etl::LoadBalancerInterface> balancer_;
56 std::shared_ptr<etl::ETLServiceInterface const> etl_;
57 std::reference_wrapper<CountersType const> counters_;
67 boost::json::object
etl;
76 xrpl::LedgerIndex
seq = {};
77 std::optional<xrpl::Fees>
fees = std::nullopt;
102 std::chrono::time_point<std::chrono::system_clock>
time = ClockType::now();
147 std::shared_ptr<BackendInterface> backend,
148 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions,
149 std::shared_ptr<etl::LoadBalancerInterface> balancer,
150 std::shared_ptr<etl::ETLServiceInterface const>
etl,
151 CountersType
const& counters
153 : backend_(std::move(backend))
154 , subscriptions_(std::move(subscriptions))
155 , balancer_(std::move(balancer))
156 , etl_(std::move(
etl))
157 , counters_(std::cref(counters))
172 using namespace std::chrono;
174 auto const range = backend_->fetchLedgerRange();
175 ASSERT(range.has_value(),
"ServerInfo's ledger range must be available");
177 auto const lgrInfo = backend_->fetchLedgerBySequence(
182 if (not lgrInfo.has_value())
183 return Error{Status{XrpldError::RpcInternal}};
185 auto const fees = backend_->fetchFees(lgrInfo->seq, ctx.
yield);
186 if (not fees.has_value())
187 return Error{Status{XrpldError::RpcInternal}};
189 auto output = Output{};
190 auto const sinceEpoch = duration_cast<seconds>(output.info.time.time_since_epoch()).count();
191 auto const age =
static_cast<int32_t
>(sinceEpoch) -
192 static_cast<int32_t
>(lgrInfo->closeTime.time_since_epoch().count()) -
196 output.info.completeLedgers = fmt::format(
"{}-{}", range->minSequence, range->maxSequence);
200 output.info.adminSection = {
201 .counters = counters_.get().report(),
203 input.backendCounters ? std::make_optional(backend_->stats()) : std::nullopt,
204 .subscriptions = subscriptions_->report(),
205 .etl = etl_->getInfo(),
209 auto const serverInfoRippled = balancer_->forwardToRippled(
213 if (serverInfoRippled && !serverInfoRippled->contains(JS(error))) {
214 if (serverInfoRippled->contains(JS(result)) &&
215 serverInfoRippled->at(JS(result)).as_object().contains(JS(info))) {
216 output.info.rippledInfo =
217 serverInfoRippled->at(JS(result)).as_object().at(JS(info)).as_object();
221 output.info.validatedLedger.age = age < 0 ? 0 : age;
222 output.info.validatedLedger.hash = xrpl::strHex(lgrInfo->hash);
223 output.info.validatedLedger.seq = lgrInfo->seq;
224 output.info.validatedLedger.fees = fees;
225 output.info.cache.size = backend_->cache().size();
226 output.info.cache.isFull = backend_->cache().isFull();
227 output.info.cache.latestLedgerSeq = backend_->cache().latestLedgerSequence();
228 output.info.cache.objectHitRate = backend_->cache().getObjectHitRate();
229 output.info.cache.successorHitRate = backend_->cache().getSuccessorHitRate();
230 output.info.cache.isEnabled = not backend_->cache().isDisabled();
231 output.info.uptime = counters_.get().uptime();
232 output.info.isAmendmentBlocked = etl_->isAmendmentBlocked();
233 output.info.isCorruptionDetected = etl_->isCorruptionDetected();
240 tag_invoke(boost::json::value_from_tag, boost::json::value& jv, Output
const& output)
242 using boost::json::value_from;
245 {JS(info), value_from(output.info)},
246 {JS(validated), output.validated},
257 tag_invoke(boost::json::value_from_tag, boost::json::value& jv, InfoSection
const& info)
259 using boost::json::value_from;
261 {JS(complete_ledgers), info.completeLedgers},
262 {JS(load_factor), info.loadFactor},
263 {JS(time), xrpl::to_string(std::chrono::floor<std::chrono::microseconds>(info.time))},
264 {JS(uptime), info.uptime.count()},
265 {
"clio_version", info.clioVersion},
266 {
"libxrpl_version", info.xrplVersion},
267 {JS(validated_ledger), value_from(info.validatedLedger)},
268 {
"cache", value_from(info.cache)},
271 if (info.isAmendmentBlocked)
272 jv.as_object()[JS(amendment_blocked)] =
true;
274 if (info.isCorruptionDetected)
275 jv.as_object()[
"corruption_detected"] =
true;
277 if (info.rippledInfo) {
278 auto const& rippledInfo = *info.rippledInfo;
280 if (rippledInfo.contains(JS(load_factor)))
281 jv.as_object()[JS(load_factor)] = rippledInfo.at(JS(load_factor));
282 if (rippledInfo.contains(JS(validation_quorum)))
283 jv.as_object()[JS(validation_quorum)] = rippledInfo.at(JS(validation_quorum));
284 if (rippledInfo.contains(JS(build_version)))
285 jv.as_object()[
"rippled_version"] = rippledInfo.at(JS(build_version));
286 if (rippledInfo.contains(JS(network_id)))
287 jv.as_object()[JS(network_id)] = rippledInfo.at(JS(network_id));
290 if (info.adminSection) {
291 jv.as_object()[
"etl"] = info.adminSection->etl;
292 jv.as_object()[JS(counters)] = info.adminSection->counters;
293 jv.as_object()[JS(counters)].as_object()[
"subscriptions"] =
294 info.adminSection->subscriptions;
295 if (info.adminSection->backendCounters.has_value()) {
296 jv.as_object()[kBackendCountersKey] = *info.adminSection->backendCounters;
309 boost::json::value_from_tag,
310 boost::json::value& jv,
311 ValidatedLedgerSection
const& validated
316 {JS(age), validated.age},
317 {JS(hash), validated.hash},
318 {JS(seq), validated.seq},
319 {JS(base_fee_xrp), validated.fees->base.decimalXRP()},
320 {JS(reserve_base_xrp), validated.fees->reserve.decimalXRP()},
321 {JS(reserve_inc_xrp), validated.fees->increment.decimalXRP()},
333 tag_invoke(boost::json::value_from_tag, boost::json::value& jv, CacheSection
const& cache)
336 {
"size", cache.size},
337 {
"is_enabled", cache.isEnabled},
338 {
"is_full", cache.isFull},
339 {
"latest_ledger_seq", cache.latestLedgerSeq},
340 {
"object_hit_rate", cache.objectHitRate},
341 {
"successor_hit_rate", cache.successorHitRate},
static constexpr std::uint32_t kRippleEpochStart
The ripple epoch start timestamp. Midnight on 1st January 2000.
Definition DBHelpers.hpp:296
Contains common functionality for handling the server_info command.
Definition ServerInfo.hpp:50
Result process(Input const &input, Context const &ctx) const
Process the ServerInfo command.
Definition ServerInfo.hpp:169
HandlerReturnType< Output > Result
The result type of the command: either the Output or an error status.
Definition ServerInfo.hpp:135
friend void tag_invoke(boost::json::value_from_tag, boost::json::value &jv, ValidatedLedgerSection const &validated)
Convert the ValidatedLedgerSection to a JSON object.
Definition ServerInfo.hpp:308
friend void tag_invoke(boost::json::value_from_tag, boost::json::value &jv, InfoSection const &info)
Convert the InfoSection to a JSON object.
Definition ServerInfo.hpp:257
BaseServerInfoHandler(std::shared_ptr< BackendInterface > backend, std::shared_ptr< feed::SubscriptionManagerInterface > subscriptions, std::shared_ptr< etl::LoadBalancerInterface > balancer, std::shared_ptr< etl::ETLServiceInterface const > etl, CountersType const &counters)
Construct a new BaseServerInfoHandler object.
Definition ServerInfo.hpp:146
friend void tag_invoke(boost::json::value_from_tag, boost::json::value &jv, CacheSection const &cache)
Convert the CacheSection to a JSON object.
Definition ServerInfo.hpp:333
Holds information about successful, failed, forwarded, etc. RPC handler calls.
Definition Counters.hpp:22
This namespace contains everything to do with the ETL and ETL sources.
Definition AmendmentBlockHandlerInterface.hpp:6
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:17
std::expected< OutputType, Status > HandlerReturnType
Return type for each individual handler.
Definition Types.hpp:63
void tag_invoke(boost::json::value_from_tag, boost::json::value &jv, BookChange const &change)
Implementation of value_from for BookChange type.
Definition BookChangesHelper.hpp:233
spec::HandlerFor< InputT, boost::json::value > HandlerFor
The spec-driven base every typed handler inherits.
Definition SpecBackend.hpp:24
std::unexpected< Status > Error
The type that represents just the error part of MaybeError.
Definition Types.hpp:55
BaseServerInfoHandler< Counters > ServerInfoHandler
The server_info command asks the Clio server for a human-readable version of various information abou...
Definition ServerInfo.hpp:352
std::string const & getClioVersionString()
Get the Clio version.
Definition Build.cpp:26
A struct to hold the admin section of the output.
Definition ServerInfo.hpp:63
boost::json::object subscriptions
Report of the subscription manager.
Definition ServerInfo.hpp:66
boost::json::object counters
Report of the RPC counters.
Definition ServerInfo.hpp:64
std::optional< boost::json::object > backendCounters
Backend stats, if requested.
Definition ServerInfo.hpp:65
A struct to hold the cache section of the output.
Definition ServerInfo.hpp:83
bool isEnabled
Whether the cache is enabled.
Definition ServerInfo.hpp:85
std::size_t size
Number of objects in the cache.
Definition ServerInfo.hpp:84
float successorHitRate
Cache hit rate of successor lookups.
Definition ServerInfo.hpp:89
bool isFull
Whether the cache is fully loaded.
Definition ServerInfo.hpp:86
float objectHitRate
Cache hit rate of object lookups.
Definition ServerInfo.hpp:88
xrpl::LedgerIndex latestLedgerSeq
Latest ledger sequence in the cache.
Definition ServerInfo.hpp:87
A struct to hold the info section of the output.
Definition ServerInfo.hpp:95
CacheSection cache
State of the ledger cache.
Definition ServerInfo.hpp:117
std::string completeLedgers
Ledgers in the database, as min-max.
Definition ServerInfo.hpp:97
bool isCorruptionDetected
Whether ETL detected corruption.
Definition ServerInfo.hpp:119
std::chrono::time_point< std::chrono::system_clock > time
Current time of the server.
Definition ServerInfo.hpp:102
std::optional< boost::json::object > rippledInfo
info from rippled's server_info
Definition ServerInfo.hpp:115
ValidatedLedgerSection validatedLedger
Latest validated ledger.
Definition ServerInfo.hpp:116
uint32_t loadFactor
Load factor; rippled's takes precedence.
Definition ServerInfo.hpp:98
std::chrono::seconds uptime
Time since the server started.
Definition ServerInfo.hpp:103
bool isAmendmentBlocked
Whether ETL is amendment blocked.
Definition ServerInfo.hpp:118
std::string xrplVersion
Version of libxrpl.
Definition ServerInfo.hpp:111
std::string clioVersion
Version of Clio.
Definition ServerInfo.hpp:107
std::optional< AdminSection > adminSection
Admin-only data.
Definition ServerInfo.hpp:96
A struct to hold the output data of the command.
Definition ServerInfo.hpp:125
InfoSection info
Information about the server.
Definition ServerInfo.hpp:126
bool validated
Whether the ledger is validated.
Definition ServerInfo.hpp:129
A struct to hold the validated ledger section of the output.
Definition ServerInfo.hpp:73
xrpl::LedgerIndex seq
Sequence of the ledger.
Definition ServerInfo.hpp:76
std::optional< xrpl::Fees > fees
Fee settings of the ledger.
Definition ServerInfo.hpp:77
std::string hash
Hash of the ledger.
Definition ServerInfo.hpp:75
uint32_t age
Seconds since the ledger closed.
Definition ServerInfo.hpp:74
Context of an RPC call.
Definition Types.hpp:102
std::string clientIp
IP address of the client.
Definition Types.hpp:121
boost::asio::yield_context yield
The coroutine context the RPC call is executed in.
Definition Types.hpp:106
bool isAdmin
Whether the client has admin privileges.
Definition Types.hpp:116
Result type used to return responses or error statuses to the Webserver subsystem.
Definition Types.hpp:132