Add server_info. Make forwardToRippled return optional

This commit is contained in:
CJ Cobb
2021-09-21 12:15:10 -04:00
parent 3d580784d1
commit 6b3091a456
5 changed files with 48 additions and 28 deletions

View File

@@ -647,29 +647,21 @@ ETLLoadBalancer::getRippledForwardingStub() const
return nullptr; return nullptr;
} }
boost::json::object std::optional<boost::json::object>
ETLLoadBalancer::forwardToRippled(boost::json::object const& request) const ETLLoadBalancer::forwardToRippled(boost::json::object const& request) const
{ {
boost::json::object res;
if (sources_.size() == 0)
return res;
srand((unsigned)time(0)); srand((unsigned)time(0));
auto sourceIdx = rand() % sources_.size(); auto sourceIdx = rand() % sources_.size();
auto numAttempts = 0; auto numAttempts = 0;
while (numAttempts < sources_.size()) while (numAttempts < sources_.size())
{ {
res = sources_[sourceIdx]->forwardToRippled(request); if (auto res = sources_[sourceIdx]->forwardToRippled(request))
return res;
if (!res.contains("forwarded") || res.at("forwarded") != true)
{
sourceIdx = (sourceIdx + 1) % sources_.size(); sourceIdx = (sourceIdx + 1) % sources_.size();
++numAttempts; ++numAttempts;
continue;
} }
return res; return {};
}
res["error"] = "Failed to forward";
return res;
} }
std::unique_ptr<org::xrpl::rpc::v1::XRPLedgerAPIService::Stub> std::unique_ptr<org::xrpl::rpc::v1::XRPLedgerAPIService::Stub>
@@ -693,7 +685,7 @@ ETLSource::getRippledForwardingStub() const
} }
} }
boost::json::object std::optional<boost::json::object>
ETLSource::forwardToRippled(boost::json::object const& request) const ETLSource::forwardToRippled(boost::json::object const& request) const
{ {
BOOST_LOG_TRIVIAL(debug) << "Attempting to forward request to tx. " BOOST_LOG_TRIVIAL(debug) << "Attempting to forward request to tx. "
@@ -704,7 +696,7 @@ ETLSource::forwardToRippled(boost::json::object const& request) const
{ {
BOOST_LOG_TRIVIAL(error) BOOST_LOG_TRIVIAL(error)
<< "Attempted to proxy but failed to connect to tx"; << "Attempted to proxy but failed to connect to tx";
return response; return {};
} }
namespace beast = boost::beast; // from <boost/beast.hpp> namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp> namespace http = beast::http; // from <boost/beast/http.hpp>
@@ -764,9 +756,9 @@ ETLSource::forwardToRippled(boost::json::object const& request) const
if (!parsed.is_object()) if (!parsed.is_object())
{ {
BOOST_LOG_TRIVIAL(error) << "Error parsing response"; BOOST_LOG_TRIVIAL(error)
response["error"] = "Error parsing response from tx"; << "Error parsing response: " << std::string{begin, end};
return response; return {};
} }
BOOST_LOG_TRIVIAL(debug) << "Successfully forward request"; BOOST_LOG_TRIVIAL(debug) << "Successfully forward request";
@@ -778,7 +770,7 @@ ETLSource::forwardToRippled(boost::json::object const& request) const
catch (std::exception const& e) catch (std::exception const& e)
{ {
BOOST_LOG_TRIVIAL(error) << "Encountered exception : " << e.what(); BOOST_LOG_TRIVIAL(error) << "Encountered exception : " << e.what();
return response; return {};
} }
} }

View File

@@ -29,8 +29,8 @@
#include <webserver/SubscriptionManager.h> #include <webserver/SubscriptionManager.h>
#include "org/xrpl/rpc/v1/xrp_ledger.grpc.pb.h" #include "org/xrpl/rpc/v1/xrp_ledger.grpc.pb.h"
#include <grpcpp/grpcpp.h>
#include <etl/ETLHelpers.h> #include <etl/ETLHelpers.h>
#include <grpcpp/grpcpp.h>
class ETLLoadBalancer; class ETLLoadBalancer;
class SubscriptionManager; class SubscriptionManager;
@@ -321,7 +321,7 @@ public:
std::unique_ptr<org::xrpl::rpc::v1::XRPLedgerAPIService::Stub> std::unique_ptr<org::xrpl::rpc::v1::XRPLedgerAPIService::Stub>
getRippledForwardingStub() const; getRippledForwardingStub() const;
boost::json::object std::optional<boost::json::object>
forwardToRippled(boost::json::object const& request) const; forwardToRippled(boost::json::object const& request) const;
}; };
/// This class is used to manage connections to transaction processing processes /// This class is used to manage connections to transaction processing processes
@@ -428,7 +428,7 @@ public:
/// Forward a JSON RPC request to a randomly selected rippled node /// Forward a JSON RPC request to a randomly selected rippled node
/// @param request JSON-RPC request /// @param request JSON-RPC request
/// @return response received from rippled node /// @return response received from rippled node
boost::json::object std::optional<boost::json::object>
forwardToRippled(boost::json::object const& request) const; forwardToRippled(boost::json::object const& request) const;
private: private:

View File

@@ -152,9 +152,9 @@ buildResponse(Context const& ctx)
if (shouldForwardToRippled(ctx)) if (shouldForwardToRippled(ctx))
{ {
auto res = ctx.balancer->forwardToRippled(ctx.params); auto res = ctx.balancer->forwardToRippled(ctx.params);
if (res.size() == 0) if (!res)
return Status{Error::rpcFAILED_TO_FORWARD}; return Status{Error::rpcFAILED_TO_FORWARD};
return res; return *res;
} }
if (ctx.method == "ping") if (ctx.method == "ping")
return boost::json::object{}; return boost::json::object{};

View File

@@ -1,5 +1,6 @@
#include <backend/BackendInterface.h> #include <backend/BackendInterface.h>
#include <etl/ETLSource.h>
#include <rpc/RPCHelpers.h> #include <rpc/RPCHelpers.h>
namespace RPC { namespace RPC {
@@ -12,7 +13,10 @@ doServerInfo(Context const& context)
auto range = context.backend->fetchLedgerRange(); auto range = context.backend->fetchLedgerRange();
if (!range) if (!range)
{ {
return Status{Error::rpcNOT_READY, "rangeNotFound"}; return Status{
Error::rpcNOT_READY,
"emptyDatabase",
"The server has no data in the database"};
} }
else else
{ {
@@ -21,6 +25,31 @@ doServerInfo(Context const& context)
std::to_string(range->minSequence) + " - " + std::to_string(range->minSequence) + " - " +
std::to_string(range->maxSequence); std::to_string(range->maxSequence);
} }
auto serverInfoRippled = context.balancer->forwardToRippled(context.params);
if (serverInfoRippled && !serverInfoRippled->contains("error"))
response["info"].as_object()["load_factor"] = 1;
auto lgrInfo = context.backend->fetchLedgerBySequence(range->maxSequence);
assert(lgrInfo.has_value());
auto age = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch())
.count() -
lgrInfo->closeTime.time_since_epoch().count() - 946684800;
auto& validatedLgr =
(response["validated_ledger"] = boost::json::object{}).as_object();
validatedLgr["age"] = age;
validatedLgr["hash"] = ripple::strHex(lgrInfo->hash);
validatedLgr["seq"] = lgrInfo->seq;
auto fees = context.backend->fetchFees(lgrInfo->seq);
assert(fees.has_value());
validatedLgr["base_fee_xrp"] = fees->base.decimalXRP();
validatedLgr["reserve_base_xrp"] = fees->reserve.decimalXRP();
validatedLgr["reserve_inc_xrp"] = fees->increment.decimalXRP();
response["note"] =
"This is a clio server. If you want to talk to rippled, include "
"\"ledger_index\":\"current\" in your request";
return response; return response;
} }
} // namespace RPC } // namespace RPC

View File

@@ -18,8 +18,7 @@ getLedgerPubMessage(
pubMsg["fee_ref"] = RPC::toBoostJson(fees.units.jsonClipped()); pubMsg["fee_ref"] = RPC::toBoostJson(fees.units.jsonClipped());
pubMsg["fee_base"] = RPC::toBoostJson(fees.base.jsonClipped()); pubMsg["fee_base"] = RPC::toBoostJson(fees.base.jsonClipped());
pubMsg["reserve_base"] = pubMsg["reserve_base"] = RPC::toBoostJson(fees.reserve.jsonClipped());
RPC::toBoostJson(fees.accountReserve(0).jsonClipped());
pubMsg["reserve_inc"] = RPC::toBoostJson(fees.increment.jsonClipped()); pubMsg["reserve_inc"] = RPC::toBoostJson(fees.increment.jsonClipped());
pubMsg["validated_ledgers"] = ledgerRange; pubMsg["validated_ledgers"] = ledgerRange;