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

@@ -30,6 +30,7 @@
#include <xrpl/protocol/Indexes.h>
#include <xrpl/protocol/SField.h>
#include <xrpl/protocol/STLedgerEntry.h>
#include <xrpl/protocol/STVector256.h>
#include <xrpl/protocol/Serializer.h>
#include <xrpl/protocol/digest.h>
@@ -38,7 +39,9 @@
#include <iterator>
#include <map>
#include <memory>
#include <optional>
#include <ranges>
#include <stdexcept>
#include <string>
#include <string_view>
#include <unordered_set>
@@ -54,6 +57,15 @@ SUPPORTED_AMENDMENTS()
return amendments;
}
bool
lookupAmendment(auto const& allAmendments, std::vector<ripple::uint256> const& ledgerAmendments, std::string_view name)
{
namespace rg = std::ranges;
if (auto const am = rg::find(allAmendments, name, &data::Amendment::name); am != rg::end(allAmendments))
return rg::find(ledgerAmendments, am->feature) != rg::end(ledgerAmendments);
return false;
}
} // namespace
namespace data {
@@ -72,6 +84,11 @@ AmendmentKey::operator std::string const&() const
return name;
}
AmendmentKey::operator std::string_view() const
{
return name;
}
AmendmentKey::operator ripple::uint256() const
{
return Amendment::GetAmendmentId(name);
@@ -127,28 +144,29 @@ AmendmentCenter::isEnabled(AmendmentKey const& key, uint32_t seq) const
bool
AmendmentCenter::isEnabled(boost::asio::yield_context yield, AmendmentKey const& key, uint32_t seq) const
{
namespace rg = std::ranges;
// the amendments should always be present on the ledger
auto const& amendments = backend_->fetchLedgerObject(ripple::keylet::amendments().key, seq, yield);
ASSERT(amendments.has_value(), "Amendments ledger object must be present in the database");
ripple::SLE const amendmentsSLE{
ripple::SerialIter{amendments->data(), amendments->size()}, ripple::keylet::amendments().key
};
if (not amendmentsSLE.isFieldPresent(ripple::sfAmendments))
return false;
auto const listAmendments = amendmentsSLE.getFieldV256(ripple::sfAmendments);
if (auto am = rg::find(all_, key.name, [](auto const& am) { return am.name; }); am != rg::end(all_)) {
return rg::find(listAmendments, am->feature) != rg::end(listAmendments);
}
if (auto const listAmendments = fetchAmendmentsList(yield, seq); listAmendments)
return lookupAmendment(all_, *listAmendments, key);
return false;
}
std::vector<bool>
AmendmentCenter::isEnabled(boost::asio::yield_context yield, std::vector<AmendmentKey> const& keys, uint32_t seq) const
{
namespace rg = std::ranges;
if (auto const listAmendments = fetchAmendmentsList(yield, seq); listAmendments) {
std::vector<bool> out;
rg::transform(keys, std::back_inserter(out), [this, &listAmendments](auto const& key) {
return lookupAmendment(all_, *listAmendments, key);
});
return out;
}
return std::vector<bool>(keys.size(), false);
}
Amendment const&
AmendmentCenter::getAmendment(AmendmentKey const& key) const
{
@@ -168,4 +186,19 @@ Amendment::GetAmendmentId(std::string_view name)
return ripple::sha512Half(ripple::Slice(name.data(), name.size()));
}
std::optional<std::vector<ripple::uint256>>
AmendmentCenter::fetchAmendmentsList(boost::asio::yield_context yield, uint32_t seq) const
{
// the amendments should always be present on the ledger
auto const amendments = backend_->fetchLedgerObject(ripple::keylet::amendments().key, seq, yield);
if (not amendments.has_value())
throw std::runtime_error("Amendments ledger object must be present in the database");
ripple::SLE const amendmentsSLE{
ripple::SerialIter{amendments->data(), amendments->size()}, ripple::keylet::amendments().key
};
return amendmentsSLE[~ripple::sfAmendments];
}
} // namespace data

View File

@@ -40,16 +40,19 @@
#include <cstdint>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#define REGISTER(name) inline static impl::WritingAmendmentKey const name = std::string(BOOST_PP_STRINGIZE(name))
#define REGISTER(name) \
inline static impl::WritingAmendmentKey const name = \
impl::WritingAmendmentKey(std::string(BOOST_PP_STRINGIZE(name)))
namespace data {
namespace impl {
struct WritingAmendmentKey : AmendmentKey {
WritingAmendmentKey(std::string amendmentName);
explicit WritingAmendmentKey(std::string amendmentName);
};
} // namespace impl
@@ -172,7 +175,7 @@ public:
* @param key The key of the amendment to check
* @return true if supported; false otherwise
*/
bool
[[nodiscard]] bool
isSupported(AmendmentKey const& key) const final;
/**
@@ -180,7 +183,7 @@ public:
*
* @return The amendments supported by Clio
*/
std::map<std::string, Amendment> const&
[[nodiscard]] std::map<std::string, Amendment> const&
getSupported() const final;
/**
@@ -188,7 +191,7 @@ public:
*
* @return All known amendments as a vector
*/
std::vector<Amendment> const&
[[nodiscard]] std::vector<Amendment> const&
getAll() const final;
/**
@@ -198,7 +201,7 @@ public:
* @param seq The sequence to check for
* @return true if enabled; false otherwise
*/
bool
[[nodiscard]] bool
isEnabled(AmendmentKey const& key, uint32_t seq) const final;
/**
@@ -209,16 +212,27 @@ public:
* @param seq The sequence to check for
* @return true if enabled; false otherwise
*/
bool
[[nodiscard]] bool
isEnabled(boost::asio::yield_context yield, AmendmentKey const& key, uint32_t seq) const final;
/**
* @brief Check whether an amendment was/is enabled for a given sequence
*
* @param yield The coroutine context to use
* @param keys The keys of the amendments to check
* @param seq The sequence to check for
* @return A vector of bools representing enabled state for each of the given keys
*/
[[nodiscard]] std::vector<bool>
isEnabled(boost::asio::yield_context yield, std::vector<AmendmentKey> const& keys, uint32_t seq) const final;
/**
* @brief Get an amendment
*
* @param key The key of the amendment to get
* @return The amendment as a const ref; asserts if the amendment is unknown
*/
Amendment const&
[[nodiscard]] Amendment const&
getAmendment(AmendmentKey const& key) const final;
/**
@@ -227,8 +241,12 @@ public:
* @param key The amendment key from @see Amendments
* @return The amendment as a const ref; asserts if the amendment is unknown
*/
Amendment const&
[[nodiscard]] Amendment const&
operator[](AmendmentKey const& key) const final;
private:
[[nodiscard]] std::optional<std::vector<ripple::uint256>>
fetchAmendmentsList(boost::asio::yield_context yield, uint32_t seq) const;
};
} // namespace data

View File

@@ -43,7 +43,7 @@ public:
* @param key The key of the amendment to check
* @return true if supported; false otherwise
*/
virtual bool
[[nodiscard]] virtual bool
isSupported(AmendmentKey const& key) const = 0;
/**
@@ -51,7 +51,7 @@ public:
*
* @return The amendments supported by Clio
*/
virtual std::map<std::string, Amendment> const&
[[nodiscard]] virtual std::map<std::string, Amendment> const&
getSupported() const = 0;
/**
@@ -59,7 +59,7 @@ public:
*
* @return All known amendments as a vector
*/
virtual std::vector<Amendment> const&
[[nodiscard]] virtual std::vector<Amendment> const&
getAll() const = 0;
/**
@@ -69,7 +69,7 @@ public:
* @param seq The sequence to check for
* @return true if enabled; false otherwise
*/
virtual bool
[[nodiscard]] virtual bool
isEnabled(AmendmentKey const& key, uint32_t seq) const = 0;
/**
@@ -80,16 +80,27 @@ public:
* @param seq The sequence to check for
* @return true if enabled; false otherwise
*/
virtual bool
[[nodiscard]] virtual bool
isEnabled(boost::asio::yield_context yield, AmendmentKey const& key, uint32_t seq) const = 0;
/**
* @brief Check whether an amendment was/is enabled for a given sequence
*
* @param yield The coroutine context to use
* @param keys The keys of the amendments to check
* @param seq The sequence to check for
* @return A vector of bools representing enabled state for each of the given keys
*/
[[nodiscard]] virtual std::vector<bool>
isEnabled(boost::asio::yield_context yield, std::vector<AmendmentKey> const& keys, uint32_t seq) const = 0;
/**
* @brief Get an amendment
*
* @param key The key of the amendment to get
* @return The amendment as a const ref; asserts if the amendment is unknown
*/
virtual Amendment const&
[[nodiscard]] virtual Amendment const&
getAmendment(AmendmentKey const& key) const = 0;
/**
@@ -98,7 +109,7 @@ public:
* @param key The amendment key from @see Amendments
* @return The amendment as a const ref; asserts if the amendment is unknown
*/
virtual Amendment const&
[[nodiscard]] virtual Amendment const&
operator[](AmendmentKey const& key) const = 0;
};

View File

@@ -247,9 +247,9 @@ struct LedgerRange {
struct Amendment {
std::string name;
ripple::uint256 feature;
bool isSupportedByXRPL;
bool isSupportedByClio;
bool isRetired;
bool isSupportedByXRPL = false;
bool isSupportedByClio = false;
bool isRetired = false;
/**
* @brief Get the amendment Id from its name
@@ -289,6 +289,9 @@ struct AmendmentKey {
/** @brief Conversion to string */
operator std::string const&() const;
/** @brief Conversion to string_view */
operator std::string_view() const;
/** @brief Conversion to uint256 */
operator ripple::uint256() const;

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
*

View File

@@ -46,6 +46,13 @@ struct MockAmendmentCenter : public data::AmendmentCenterInterface {
MOCK_METHOD(bool, isEnabled, (boost::asio::yield_context, data::AmendmentKey const&, uint32_t), (const, override));
MOCK_METHOD(
std::vector<bool>,
isEnabled,
(boost::asio::yield_context, std::vector<data::AmendmentKey> const&, uint32_t),
(const, override)
);
MOCK_METHOD(data::Amendment const&, getAmendment, (data::AmendmentKey const&), (const, override));
MOCK_METHOD(data::Amendment const&, IndexOperator, (data::AmendmentKey const&), (const));

View File

@@ -902,6 +902,16 @@ CreateAmendmentsObject(std::vector<ripple::uint256> const& enabledAmendments)
return amendments;
}
ripple::STObject
CreateBrokenAmendmentsObject()
{
auto amendments = ripple::STObject(ripple::sfLedgerEntry);
amendments.setFieldU16(ripple::sfLedgerEntryType, ripple::ltAMENDMENTS);
amendments.setFieldU32(ripple::sfFlags, 0);
// Note: no sfAmendments present
return amendments;
}
ripple::STObject
CreateAMMObject(
std::string_view accountId,

View File

@@ -330,6 +330,9 @@ CreateCreateNFTOfferTxWithMetadata(
[[nodiscard]] ripple::STObject
CreateAmendmentsObject(std::vector<ripple::uint256> const& enabledAmendments);
[[nodiscard]] ripple::STObject
CreateBrokenAmendmentsObject();
[[nodiscard]] ripple::STObject
CreateAMMObject(
std::string_view accountId,

View File

@@ -19,6 +19,7 @@
#include "data/AmendmentCenter.hpp"
#include "data/Types.hpp"
#include "util/AsioContextTestFixture.hpp"
#include "util/MockBackendTestFixture.hpp"
#include "util/MockPrometheus.hpp"
#include "util/TestObject.hpp"
@@ -29,13 +30,17 @@
#include <xrpl/protocol/Feature.h>
#include <xrpl/protocol/Indexes.h>
#include <algorithm>
#include <optional>
#include <stdexcept>
#include <string>
#include <vector>
using namespace data;
constexpr auto SEQ = 30;
constexpr auto SEQ = 30u;
struct AmendmentCenterTest : util::prometheus::WithPrometheus, MockBackendTest {
struct AmendmentCenterTest : util::prometheus::WithPrometheus, MockBackendTest, SyncAsioContextTest {
AmendmentCenter amendmentCenter{backend};
};
@@ -81,6 +86,60 @@ TEST_F(AmendmentCenterTest, IsEnabled)
EXPECT_FALSE(amendmentCenter.isEnabled("ImmediateOfferKilled", SEQ));
}
TEST_F(AmendmentCenterTest, IsMultipleEnabled)
{
auto const amendments = CreateAmendmentsObject({Amendments::fixUniversalNumber});
EXPECT_CALL(*backend, doFetchLedgerObject(ripple::keylet::amendments().key, SEQ, testing::_))
.WillOnce(testing::Return(amendments.getSerializer().peekData()));
runSpawn([this](auto yield) {
std::vector<data::AmendmentKey> keys{"fixUniversalNumber", "unknown", "ImmediateOfferKilled"};
auto const result = amendmentCenter.isEnabled(yield, keys, SEQ);
EXPECT_EQ(result.size(), keys.size());
EXPECT_TRUE(result.at(0));
EXPECT_FALSE(result.at(1));
EXPECT_FALSE(result.at(2));
});
}
TEST_F(AmendmentCenterTest, IsEnabledThrowsWhenUnavailable)
{
EXPECT_CALL(*backend, doFetchLedgerObject(ripple::keylet::amendments().key, SEQ, testing::_))
.WillOnce(testing::Return(std::nullopt));
runSpawn([this](auto yield) {
EXPECT_THROW(
{ [[maybe_unused]] auto const result = amendmentCenter.isEnabled(yield, "irrelevant", SEQ); },
std::runtime_error
);
});
}
TEST_F(AmendmentCenterTest, IsEnabledReturnsFalseWhenNoAmendments)
{
auto const amendments = CreateBrokenAmendmentsObject();
EXPECT_CALL(*backend, doFetchLedgerObject(ripple::keylet::amendments().key, SEQ, testing::_))
.WillOnce(testing::Return(amendments.getSerializer().peekData()));
runSpawn([this](auto yield) { EXPECT_FALSE(amendmentCenter.isEnabled(yield, "irrelevant", SEQ)); });
}
TEST_F(AmendmentCenterTest, IsEnabledReturnsVectorOfFalseWhenNoAmendments)
{
auto const amendments = CreateBrokenAmendmentsObject();
EXPECT_CALL(*backend, doFetchLedgerObject(ripple::keylet::amendments().key, SEQ, testing::_))
.WillOnce(testing::Return(amendments.getSerializer().peekData()));
runSpawn([this](auto yield) {
std::vector<data::AmendmentKey> keys{"fixUniversalNumber", "ImmediateOfferKilled"};
auto const vec = amendmentCenter.isEnabled(yield, keys, SEQ);
EXPECT_EQ(vec.size(), keys.size());
EXPECT_TRUE(std::ranges::all_of(vec, [](bool val) { return val == false; }));
});
}
TEST(AmendmentTest, GenerateAmendmentId)
{
// https://xrpl.org/known-amendments.html#disallowincoming refer to the published id
@@ -94,8 +153,8 @@ struct AmendmentCenterDeathTest : AmendmentCenterTest {};
TEST_F(AmendmentCenterDeathTest, GetInvalidAmendmentAsserts)
{
EXPECT_DEATH({ amendmentCenter.getAmendment("invalidAmendmentKey"); }, ".*");
EXPECT_DEATH({ amendmentCenter["invalidAmendmentKey"]; }, ".*");
EXPECT_DEATH({ [[maybe_unused]] auto _ = amendmentCenter.getAmendment("invalidAmendmentKey"); }, ".*");
EXPECT_DEATH({ [[maybe_unused]] auto _ = amendmentCenter["invalidAmendmentKey"]; }, ".*");
}
struct AmendmentKeyTest : testing::Test {};

View File

@@ -259,22 +259,6 @@ TEST_F(RPCForwardingProxyTest, ShouldForwardReturnsFalseIfAPIVersionIsV2)
});
}
TEST_F(RPCForwardingProxyTest, ShouldForwardFeatureWithoutVetoedFlag)
{
auto const apiVersion = 1u;
auto const method = "feature";
auto const params = json::parse(R"({"feature": "foo"})");
runSpawn([&](auto yield) {
auto const range = backend->fetchLedgerRange();
auto const ctx =
web::Context(yield, method, apiVersion, params.as_object(), nullptr, tagFactory, *range, CLIENT_IP, true);
auto const res = proxy.shouldForward(ctx);
ASSERT_TRUE(res);
});
}
TEST_F(RPCForwardingProxyTest, ShouldNeverForwardFeatureWithVetoedFlag)
{
auto const apiVersion = 1u;

View File

@@ -17,24 +17,206 @@
*/
//==============================================================================
#include "data/AmendmentCenter.hpp"
#include "data/Types.hpp"
#include "rpc/Errors.hpp"
#include "rpc/common/AnyHandler.hpp"
#include "rpc/common/Types.hpp"
#include "rpc/handlers/Feature.hpp"
#include "util/HandlerBaseTestFixture.hpp"
#include "util/MockAmendmentCenter.hpp"
#include "util/NameGenerator.hpp"
#include "util/TestObject.hpp"
#include <boost/asio/io_context.hpp>
#include <boost/json/parse.hpp>
#include <fmt/core.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <xrpl/basics/base_uint.h>
#include <xrpl/protocol/Indexes.h>
#include <optional>
#include <string>
#include <vector>
using namespace rpc;
class RPCFeatureHandlerTest : public HandlerBaseTest {};
constexpr static auto RANGEMIN = 10;
constexpr static auto RANGEMAX = 30;
constexpr static auto SEQ = 30;
constexpr static auto LEDGERHASH = "4BC50C9B0D8515D3EAAE1E74B29A95804346C491EE1A95BF25E4AAB854A6A652";
class RPCFeatureHandlerTest : public HandlerBaseTest {
protected:
StrictMockAmendmentCenterSharedPtr mockAmendmentCenterPtr;
};
struct RPCFeatureHandlerParamTestCaseBundle {
std::string testName;
std::string testJson;
std::string expectedError;
std::string expectedErrorMessage;
};
// parameterized test cases for parameters check
struct RPCFeatureHandlerParamTest : public RPCFeatureHandlerTest,
public testing::WithParamInterface<RPCFeatureHandlerParamTestCaseBundle> {};
static auto
generateTestValuesForParametersTest()
{
return std::vector<RPCFeatureHandlerParamTestCaseBundle>{
// Note: on rippled this and below returns "badFeature"
RPCFeatureHandlerParamTestCaseBundle{
"InvalidTypeFeatureBool", R"({"feature": true})", "invalidParams", "Invalid parameters."
},
RPCFeatureHandlerParamTestCaseBundle{
"InvalidTypeFeatureInt", R"({"feature": 42})", "invalidParams", "Invalid parameters."
},
RPCFeatureHandlerParamTestCaseBundle{
"InvalidTypeFeatureDouble", R"({"feature": 4.2})", "invalidParams", "Invalid parameters."
},
RPCFeatureHandlerParamTestCaseBundle{
"InvalidTypeFeatureNull", R"({"feature": null})", "invalidParams", "Invalid parameters."
},
// Note: this and below internal errors on rippled
RPCFeatureHandlerParamTestCaseBundle{
"InvalidTypeFeatureObj", R"({"feature": {}})", "invalidParams", "Invalid parameters."
},
RPCFeatureHandlerParamTestCaseBundle{
"InvalidTypeFeatureArray", R"({"feature": []})", "invalidParams", "Invalid parameters."
},
// "vetoed" should always be blocked
RPCFeatureHandlerParamTestCaseBundle{
"VetoedPassed",
R"({"feature": "foo", "vetoed": true})",
"noPermission",
"The admin portion of feature API is not available through Clio."
},
RPCFeatureHandlerParamTestCaseBundle{
"InvalidTypeVetoedString",
R"({"feature": "foo", "vetoed": "test"})",
"noPermission",
"The admin portion of feature API is not available through Clio."
},
RPCFeatureHandlerParamTestCaseBundle{
"InvalidTypeVetoedInt",
R"({"feature": "foo", "vetoed": 42})",
"noPermission",
"The admin portion of feature API is not available through Clio."
},
RPCFeatureHandlerParamTestCaseBundle{
"InvalidTypeVetoedDouble",
R"({"feature": "foo", "vetoed": 4.2})",
"noPermission",
"The admin portion of feature API is not available through Clio."
},
RPCFeatureHandlerParamTestCaseBundle{
"InvalidTypeVetoedObject",
R"({"feature": "foo", "vetoed": {}})",
"noPermission",
"The admin portion of feature API is not available through Clio."
},
RPCFeatureHandlerParamTestCaseBundle{
"InvalidTypeVetoedArray",
R"({"feature": "foo", "vetoed": []})",
"noPermission",
"The admin portion of feature API is not available through Clio."
},
};
}
INSTANTIATE_TEST_CASE_P(
RPCFeatureGroup1,
RPCFeatureHandlerParamTest,
testing::ValuesIn(generateTestValuesForParametersTest()),
tests::util::NameGenerator
);
TEST_P(RPCFeatureHandlerParamTest, InvalidParams)
{
auto const testBundle = GetParam();
runSpawn([&, this](auto yield) {
auto const handler = AnyHandler{FeatureHandler{backend, mockAmendmentCenterPtr}};
auto const req = boost::json::parse(testBundle.testJson);
auto const output = handler.process(req, Context{.yield = yield, .apiVersion = 2});
ASSERT_FALSE(output);
auto const err = rpc::makeError(output.result.error());
EXPECT_EQ(err.at("error").as_string(), testBundle.expectedError);
EXPECT_EQ(err.at("error_message").as_string(), testBundle.expectedErrorMessage);
});
}
TEST_F(RPCFeatureHandlerTest, LedgerNotExistViaIntSequence)
{
backend->setRange(RANGEMIN, RANGEMAX);
EXPECT_CALL(*backend, fetchLedgerBySequence(RANGEMAX, testing::_)).WillOnce(testing::Return(std::nullopt));
runSpawn([&, this](auto yield) {
auto const handler = AnyHandler{FeatureHandler{backend, mockAmendmentCenterPtr}};
auto const req = boost::json::parse(fmt::format(
R"({{
"ledger_index": {}
}})",
RANGEMAX
));
auto const output = handler.process(req, Context{yield});
ASSERT_FALSE(output);
auto const err = rpc::makeError(output.result.error());
EXPECT_EQ(err.at("error").as_string(), "lgrNotFound");
EXPECT_EQ(err.at("error_message").as_string(), "ledgerNotFound");
});
}
TEST_F(RPCFeatureHandlerTest, LedgerNotExistViaStringSequence)
{
backend->setRange(RANGEMIN, RANGEMAX);
EXPECT_CALL(*backend, fetchLedgerBySequence(RANGEMAX, testing::_)).WillOnce(testing::Return(std::nullopt));
runSpawn([&, this](auto yield) {
auto const handler = AnyHandler{FeatureHandler{backend, mockAmendmentCenterPtr}};
auto const req = boost::json::parse(fmt::format(
R"({{
"ledger_index": "{}"
}})",
RANGEMAX
));
auto const output = handler.process(req, Context{yield});
ASSERT_FALSE(output);
auto const err = rpc::makeError(output.result.error());
EXPECT_EQ(err.at("error").as_string(), "lgrNotFound");
EXPECT_EQ(err.at("error_message").as_string(), "ledgerNotFound");
});
}
TEST_F(RPCFeatureHandlerTest, LedgerNotExistViaHash)
{
backend->setRange(RANGEMIN, RANGEMAX);
EXPECT_CALL(*backend, fetchLedgerByHash(ripple::uint256{LEDGERHASH}, testing::_))
.WillOnce(testing::Return(std::nullopt));
runSpawn([&, this](auto yield) {
auto const handler = AnyHandler{FeatureHandler{backend, mockAmendmentCenterPtr}};
auto const req = boost::json::parse(fmt::format(
R"({{
"ledger_hash": "{}"
}})",
LEDGERHASH
));
auto const output = handler.process(req, Context{yield});
ASSERT_FALSE(output);
auto const err = rpc::makeError(output.result.error());
EXPECT_EQ(err.at("error").as_string(), "lgrNotFound");
EXPECT_EQ(err.at("error_message").as_string(), "ledgerNotFound");
});
}
TEST_F(RPCFeatureHandlerTest, AlwaysNoPermissionForVetoed)
{
runSpawn([](auto yield) {
auto const handler = AnyHandler{FeatureHandler{}};
runSpawn([this](auto yield) {
auto const handler = AnyHandler{FeatureHandler{backend, mockAmendmentCenterPtr}};
auto const output =
handler.process(boost::json::parse(R"({"vetoed": true, "feature": "foo"})"), Context{yield});
@@ -48,12 +230,205 @@ TEST_F(RPCFeatureHandlerTest, AlwaysNoPermissionForVetoed)
});
}
TEST(RPCFeatureHandlerDeathTest, ProcessCausesDeath)
TEST_F(RPCFeatureHandlerTest, SuccessPathViaNameWithSingleSupportedAndEnabledResult)
{
FeatureHandler handler{};
boost::asio::io_context ioContext;
boost::asio::spawn(ioContext, [&](auto yield) {
EXPECT_DEATH(handler.process(FeatureHandler::Input{"foo"}, Context{yield}), "");
backend->setRange(10, 30);
auto const all = std::vector<data::Amendment>{
{
.name = Amendments::fixUniversalNumber,
.feature = data::Amendment::GetAmendmentId(Amendments::fixUniversalNumber),
.isSupportedByXRPL = true,
.isSupportedByClio = true,
},
{
.name = Amendments::fixRemoveNFTokenAutoTrustLine,
.feature = data::Amendment::GetAmendmentId(Amendments::fixRemoveNFTokenAutoTrustLine),
.isSupportedByXRPL = true,
.isSupportedByClio = true,
}
};
auto const keys = std::vector<data::AmendmentKey>{Amendments::fixUniversalNumber};
auto const enabled = std::vector<bool>{true};
EXPECT_CALL(*mockAmendmentCenterPtr, getAll).WillOnce(testing::ReturnRef(all));
EXPECT_CALL(*mockAmendmentCenterPtr, isEnabled(testing::_, keys, SEQ)).WillOnce(testing::Return(enabled));
auto const ledgerHeader = CreateLedgerHeader(LEDGERHASH, 30);
EXPECT_CALL(*backend, fetchLedgerBySequence).WillOnce(testing::Return(ledgerHeader));
auto const expectedOutput = fmt::format(
R"({{
"features": {{
"2E2FB9CF8A44EB80F4694D38AADAE9B8B7ADAFD2F092E10068E61C98C4F092B0":
{{
"name": "fixUniversalNumber",
"enabled": true,
"supported": true
}}
}},
"ledger_hash": "{}",
"ledger_index": {},
"validated": true
}})",
LEDGERHASH,
SEQ
);
runSpawn([this, &expectedOutput](auto yield) {
auto const handler = AnyHandler{FeatureHandler{backend, mockAmendmentCenterPtr}};
auto const output = handler.process(boost::json::parse(R"({"feature": "fixUniversalNumber"})"), Context{yield});
ASSERT_TRUE(output);
EXPECT_EQ(*output.result, boost::json::parse(expectedOutput));
});
}
TEST_F(RPCFeatureHandlerTest, SuccessPathViaHashWithSingleResult)
{
backend->setRange(10, 30);
auto const all = std::vector<data::Amendment>{
{
.name = Amendments::fixUniversalNumber,
.feature = data::Amendment::GetAmendmentId(Amendments::fixUniversalNumber),
.isSupportedByXRPL = true,
.isSupportedByClio = true,
},
{
.name = Amendments::fixRemoveNFTokenAutoTrustLine,
.feature = data::Amendment::GetAmendmentId(Amendments::fixRemoveNFTokenAutoTrustLine),
.isSupportedByXRPL = true,
.isSupportedByClio = true,
}
};
auto const keys = std::vector<data::AmendmentKey>{Amendments::fixUniversalNumber};
auto const enabled = std::vector<bool>{true};
EXPECT_CALL(*mockAmendmentCenterPtr, getAll).WillOnce(testing::ReturnRef(all));
EXPECT_CALL(*mockAmendmentCenterPtr, isEnabled(testing::_, keys, SEQ)).WillOnce(testing::Return(enabled));
auto const ledgerHeader = CreateLedgerHeader(LEDGERHASH, 30);
EXPECT_CALL(*backend, fetchLedgerBySequence).WillOnce(testing::Return(ledgerHeader));
auto const expectedOutput = fmt::format(
R"({{
"features": {{
"2E2FB9CF8A44EB80F4694D38AADAE9B8B7ADAFD2F092E10068E61C98C4F092B0":
{{
"name": "fixUniversalNumber",
"enabled": true,
"supported": true
}}
}},
"ledger_hash": "{}",
"ledger_index": {},
"validated": true
}})",
LEDGERHASH,
SEQ
);
runSpawn([this, &expectedOutput](auto yield) {
auto const handler = AnyHandler{FeatureHandler{backend, mockAmendmentCenterPtr}};
auto const output = handler.process(
boost::json::parse(R"({"feature": "2E2FB9CF8A44EB80F4694D38AADAE9B8B7ADAFD2F092E10068E61C98C4F092B0"})"),
Context{yield}
);
ASSERT_TRUE(output);
EXPECT_EQ(*output.result, boost::json::parse(expectedOutput));
});
}
TEST_F(RPCFeatureHandlerTest, BadFeaturePath)
{
backend->setRange(10, 30);
auto const all = std::vector<data::Amendment>{{
.name = Amendments::fixUniversalNumber,
.feature = data::Amendment::GetAmendmentId(Amendments::fixUniversalNumber),
.isSupportedByXRPL = true,
.isSupportedByClio = true,
}};
auto const keys = std::vector<data::AmendmentKey>{"nonexistent"};
EXPECT_CALL(*mockAmendmentCenterPtr, getAll).WillOnce(testing::ReturnRef(all));
auto const ledgerHeader = CreateLedgerHeader(LEDGERHASH, 30);
EXPECT_CALL(*backend, fetchLedgerBySequence).WillOnce(testing::Return(ledgerHeader));
runSpawn([this](auto yield) {
auto const handler = AnyHandler{FeatureHandler{backend, mockAmendmentCenterPtr}};
auto const output = handler.process(boost::json::parse(R"({"feature": "nonexistent"})"), Context{yield});
ASSERT_FALSE(output);
auto const err = rpc::makeError(output.result.error());
EXPECT_EQ(err.at("error").as_string(), "badFeature");
EXPECT_EQ(err.at("error_message").as_string(), "Feature unknown or invalid.");
});
}
TEST_F(RPCFeatureHandlerTest, SuccessPathWithMultipleResults)
{
backend->setRange(10, 30);
auto const all = std::vector<data::Amendment>{
{
.name = Amendments::fixUniversalNumber,
.feature = data::Amendment::GetAmendmentId(Amendments::fixUniversalNumber),
.isSupportedByXRPL = true,
.isSupportedByClio = true,
},
{
.name = Amendments::fixRemoveNFTokenAutoTrustLine,
.feature = data::Amendment::GetAmendmentId(Amendments::fixRemoveNFTokenAutoTrustLine),
.isSupportedByXRPL = true,
.isSupportedByClio = false,
}
};
auto const keys =
std::vector<data::AmendmentKey>{Amendments::fixUniversalNumber, Amendments::fixRemoveNFTokenAutoTrustLine};
auto const enabled = std::vector<bool>{true, false};
EXPECT_CALL(*mockAmendmentCenterPtr, getAll).WillOnce(testing::ReturnRef(all));
EXPECT_CALL(*mockAmendmentCenterPtr, isEnabled(testing::_, keys, SEQ)).WillOnce(testing::Return(enabled));
auto const ledgerHeader = CreateLedgerHeader(LEDGERHASH, 30);
EXPECT_CALL(*backend, fetchLedgerBySequence).WillOnce(testing::Return(ledgerHeader));
auto const amendments =
CreateAmendmentsObject({Amendments::fixUniversalNumber, Amendments::fixRemoveNFTokenAutoTrustLine});
auto const expectedOutput = fmt::format(
R"({{
"features": {{
"2E2FB9CF8A44EB80F4694D38AADAE9B8B7ADAFD2F092E10068E61C98C4F092B0":
{{
"name": "fixUniversalNumber",
"enabled": true,
"supported": true
}},
"DF8B4536989BDACE3F934F29423848B9F1D76D09BE6A1FCFE7E7F06AA26ABEAD":
{{
"name": "fixRemoveNFTokenAutoTrustLine",
"enabled": false,
"supported": false
}}
}},
"ledger_hash": "{}",
"ledger_index": {},
"validated": true
}})",
LEDGERHASH,
SEQ
);
runSpawn([this, &expectedOutput](auto yield) {
auto const handler = AnyHandler{FeatureHandler{backend, mockAmendmentCenterPtr}};
auto const output = handler.process(boost::json::parse(R"({})"), Context{yield});
ASSERT_TRUE(output);
EXPECT_EQ(*output.result, boost::json::parse(expectedOutput));
});
ioContext.run();
}