feat: Native Feature RPC (#1526)

This commit is contained in:
Alex Kremer
2024-07-11 12:18:13 +01:00
committed by GitHub
parent 6e606cb7d8
commit f771478da0
15 changed files with 715 additions and 80 deletions

View File

@@ -29,6 +29,7 @@
#include <fmt/core.h>
#include <xrpl/protocol/ErrorCodes.h>
#include <concepts>
#include <cstdint>
#include <ctime>
#include <functional>

View File

@@ -60,10 +60,9 @@ public:
if (ctx.method == "subscribe" || ctx.method == "unsubscribe")
return false;
// TODO https://github.com/XRPLF/clio/issues/1131 - remove once clio-native feature is
// implemented fully. For now we disallow forwarding of the admin api, only user api is allowed.
if (ctx.method == "feature" and not request.contains("vetoed"))
return true;
// Disallow forwarding of the admin api, only user api is allowed for security reasons.
if (ctx.method == "feature" and request.contains("vetoed"))
return false;
if (handlerProvider_->isClioOnly(ctx.method))
return false;

View File

@@ -89,7 +89,7 @@ ProductionHandlerProvider::ProductionHandlerProvider(
{"book_changes", {BookChangesHandler{backend}}},
{"book_offers", {BookOffersHandler{backend}}},
{"deposit_authorized", {DepositAuthorizedHandler{backend}}},
{"feature", {FeatureHandler{}}},
{"feature", {FeatureHandler{backend, amendmentCenter}}},
{"gateway_balances", {GatewayBalancesHandler{backend}}},
{"get_aggregate_price", {GetAggregatePriceHandler{backend}}},
{"ledger", {LedgerHandler{backend}}},

View File

@@ -19,30 +19,88 @@
#include "rpc/handlers/Feature.hpp"
#include "data/Types.hpp"
#include "rpc/Errors.hpp"
#include "rpc/JS.hpp"
#include "rpc/RPCHelpers.hpp"
#include "rpc/common/MetaProcessors.hpp"
#include "rpc/common/Specs.hpp"
#include "rpc/common/Types.hpp"
#include "rpc/common/Validators.hpp"
#include "util/Assert.hpp"
#include <boost/json/conversion.hpp>
#include <boost/json/value.hpp>
#include <boost/json/value_to.hpp>
#include <xrpl/basics/base_uint.h>
#include <xrpl/basics/strHex.h>
#include <xrpl/protocol/LedgerHeader.h>
#include <xrpl/protocol/UintTypes.h>
#include <xrpl/protocol/jss.h>
#include <algorithm>
#include <cstdint>
#include <iterator>
#include <map>
#include <ranges>
#include <string>
#include <utility>
#include <variant>
#include <vector>
namespace rpc {
FeatureHandler::Result
FeatureHandler::process([[maybe_unused]] FeatureHandler::Input input, [[maybe_unused]] Context const& ctx)
FeatureHandler::process(FeatureHandler::Input input, Context const& ctx) const
{
// For now this handler only fires when "vetoed" is set in the request.
// This always leads to a `notSupported` error as we don't want anyone to be able to
ASSERT(false, "FeatureHandler::process is not implemented.");
return Output{};
namespace vs = std::views;
namespace rg = std::ranges;
auto const range = sharedPtrBackend_->fetchLedgerRange();
auto const lgrInfoOrStatus = getLedgerHeaderFromHashOrSeq(
*sharedPtrBackend_, ctx.yield, input.ledgerHash, input.ledgerIndex, range->maxSequence
);
if (auto const status = std::get_if<Status>(&lgrInfoOrStatus))
return Error{*status};
auto const lgrInfo = std::get<ripple::LedgerHeader>(lgrInfoOrStatus);
auto const& all = amendmentCenter_->getAll();
auto searchPredicate = [search = input.feature](auto const& feature) {
if (search)
return ripple::to_string(feature.feature) == search.value() or feature.name == search.value();
return true;
};
std::vector<Output::Feature> filtered;
rg::transform(all | vs::filter(searchPredicate), std::back_inserter(filtered), [&](auto const& feature) {
return Output::Feature{
.name = feature.name,
.key = ripple::to_string(feature.feature),
.supported = feature.isSupportedByClio,
};
});
if (filtered.empty())
return Error{Status{RippledError::rpcBAD_FEATURE}};
std::vector<data::AmendmentKey> names;
rg::transform(filtered, std::back_inserter(names), [](auto const& feature) { return feature.name; });
std::map<std::string, Output::Feature> features;
rg::transform(
filtered,
amendmentCenter_->isEnabled(ctx.yield, names, lgrInfo.seq),
std::inserter(features, std::end(features)),
[&](Output::Feature feature, bool isEnabled) {
feature.enabled = isEnabled;
return std::make_pair(feature.key, std::move(feature));
}
);
return Output{
.features = std::move(features), .ledgerHash = ripple::strHex(lgrInfo.hash), .ledgerIndex = lgrInfo.seq
};
}
RpcSpecConstRef
@@ -55,6 +113,8 @@ FeatureHandler::spec([[maybe_unused]] uint32_t apiVersion)
validation::NotSupported{},
Status(RippledError::rpcNO_PERMISSION, "The admin portion of feature API is not available through Clio.")
}},
{JS(ledger_hash), validation::CustomValidators::Uint256HexStringValidator},
{JS(ledger_index), validation::CustomValidators::LedgerIndexValidator},
};
return rpcSpec;
}
@@ -65,10 +125,25 @@ tag_invoke(boost::json::value_from_tag, boost::json::value& jv, FeatureHandler::
using boost::json::value_from;
jv = {
{JS(features), value_from(output.features)},
{JS(ledger_hash), output.ledgerHash},
{JS(ledger_index), output.ledgerIndex},
{JS(validated), output.validated},
};
}
void
tag_invoke(boost::json::value_from_tag, boost::json::value& jv, FeatureHandler::Output::Feature const& feature)
{
using boost::json::value_from;
jv = {
{JS(name), feature.name},
{JS(enabled), feature.enabled},
{JS(supported), feature.supported},
};
}
FeatureHandler::Input
tag_invoke(boost::json::value_to_tag<FeatureHandler::Input>, boost::json::value const& jv)
{
@@ -78,6 +153,16 @@ tag_invoke(boost::json::value_to_tag<FeatureHandler::Input>, boost::json::value
if (jsonObject.contains(JS(feature)))
input.feature = jv.at(JS(feature)).as_string();
if (jsonObject.contains(JS(ledger_hash)))
input.ledgerHash = boost::json::value_to<std::string>(jv.at(JS(ledger_hash)));
if (jsonObject.contains(JS(ledger_index))) {
if (!jsonObject.at(JS(ledger_index)).is_string()) {
input.ledgerIndex = jv.at(JS(ledger_index)).as_int64();
} else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") {
input.ledgerIndex = std::stoi(boost::json::value_to<std::string>(jv.at(JS(ledger_index))));
}
}
return input;
}

View File

@@ -19,6 +19,8 @@
#pragma once
#include "data/AmendmentCenterInterface.hpp"
#include "data/BackendInterface.hpp"
#include "rpc/common/Specs.hpp"
#include "rpc/common/Types.hpp"
@@ -27,6 +29,9 @@
#include <xrpl/protocol/jss.h>
#include <cstdint>
#include <map>
#include <memory>
#include <optional>
#include <string>
namespace rpc {
@@ -35,24 +40,57 @@ namespace rpc {
* @brief Contains common functionality for handling the `server_info` command
*/
class FeatureHandler {
std::shared_ptr<BackendInterface> sharedPtrBackend_;
std::shared_ptr<data::AmendmentCenterInterface const> amendmentCenter_;
public:
/**
* @brief A struct to hold the input data for the command
*/
struct Input {
std::string feature;
std::optional<std::string> ledgerHash;
std::optional<uint32_t> ledgerIndex;
std::optional<std::string> feature;
};
/**
* @brief A struct to hold the output data of the command
*/
struct Output {
/**
* @brief Represents an amendment/feature
*/
struct Feature {
std::string name;
std::string key;
bool supported = false;
bool enabled = false;
};
std::map<std::string, Feature> features;
std::string ledgerHash;
uint32_t ledgerIndex{};
// validated should be sent via framework
bool validated = true;
};
using Result = HandlerReturnType<Output>;
/**
* @brief Construct a new FeatureHandler object
*
* @param backend The backend to use
* @param amendmentCenter The amendment center to use
*/
FeatureHandler(
std::shared_ptr<BackendInterface> const& backend,
std::shared_ptr<data::AmendmentCenterInterface const> const& amendmentCenter
)
: sharedPtrBackend_(backend), amendmentCenter_(amendmentCenter)
{
}
/**
* @brief Returns the API specification for the command
*
@@ -69,8 +107,8 @@ public:
* @param ctx The context of the request
* @return The result of the operation
*/
static Result
process(Input input, Context const& ctx); // NOLINT(readability-convert-member-functions-to-static)
Result
process(Input input, Context const& ctx) const; // NOLINT(readability-convert-member-functions-to-static)
private:
/**
@@ -82,6 +120,15 @@ private:
friend void
tag_invoke(boost::json::value_from_tag, boost::json::value& jv, Output const& output);
/**
* @brief Convert the Feature to a JSON object
*
* @param [out] jv The JSON object to convert to
* @param feature The feature to convert
*/
friend void
tag_invoke(boost::json::value_from_tag, boost::json::value& jv, Output::Feature const& feature);
/**
* @brief Convert a JSON object to Input type
*