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

@@ -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();
}