#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 "util/JsonUtils.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace rpc { FeatureHandler::Result FeatureHandler::process(FeatureHandler::Input const& input, Context const& ctx) const { namespace vs = std::views; namespace rg = std::ranges; auto const range = sharedPtrBackend_->fetchLedgerRange(); ASSERT(range.has_value(), "Feature's ledger range must be available"); auto const expectedLgrInfo = getLedgerHeaderFromHashOrSeq( *sharedPtrBackend_, ctx.yield, input.ledgerHash, input.ledgerIndex, range->maxSequence ); if (not expectedLgrInfo.has_value()) return Error{expectedLgrInfo.error()}; auto const& lgrInfo = expectedLgrInfo.value(); 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 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 and feature.isSupportedByXRPL, }; } ); if (filtered.empty()) return Error{Status{RippledError::rpcBAD_FEATURE}}; std::vector names; rg::transform(filtered, std::back_inserter(names), [](auto const& feature) { return feature.name; }); std::map 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, .inlineResult = input.feature.has_value() }; } RpcSpecConstRef FeatureHandler::spec([[maybe_unused]] uint32_t apiVersion) { static RpcSpec const kRPC_SPEC = { {JS(feature), validation::Type{}}, {JS(vetoed), meta::WithCustomError{ 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 kRPC_SPEC; } void tag_invoke( boost::json::value_from_tag, boost::json::value& jv, FeatureHandler::Output const& output ) { using boost::json::value_from; if (output.inlineResult) { jv = value_from(output.features); } else { jv = { {JS(features), value_from(output.features)}, }; } auto& obj = jv.as_object(); obj[JS(ledger_hash)] = output.ledgerHash; obj[JS(ledger_index)] = output.ledgerIndex; obj[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, boost::json::value const& jv) { auto input = FeatureHandler::Input{}; auto const jsonObject = jv.as_object(); 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(jv.at(JS(ledger_hash))); if (jsonObject.contains(JS(ledger_index))) { auto const expectedLedgerIndex = util::getLedgerIndex(jv.at(JS(ledger_index))); if (expectedLedgerIndex.has_value()) input.ledgerIndex = *expectedLedgerIndex; } return input; } } // namespace rpc