Skip to content

Commit

Permalink
Support canonical ledger entry names (#5271)
Browse files Browse the repository at this point in the history
This change enhances the filtering in the ledger, ledger_data, and account_objects methods by also supporting filtering by the canonical name of the LedgerEntryType using case-insensitive matching.
  • Loading branch information
bthomee authored and q73zhao committed Feb 24, 2025
1 parent f82b566 commit 6a3c6dd
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 10 deletions.
1 change: 1 addition & 0 deletions API-CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ As of 2025-01-28, version 2.4.0 is in development. You can use a pre-release ver
### Additions and bugfixes in 2.4.0

- `ledger_entry`: `state` is added an alias for `ripple_state`.
- `ledger_entry`: Enables case-insensitive filtering by canonical name in addition to case-sensitive filtering by RPC name.
- `validators`: Added new field `validator_list_threshold` in response.
- `simulate`: A new RPC that executes a [dry run of a transaction submission](https://github.com/XRPLF/XRPL-Standards/tree/master/XLS-0069d-simulate#2-rpc-simulate)
- Signing methods autofill fees better and properly handle transactions that don't have a base fee, and will also autofill the `NetworkID` field.
Expand Down
92 changes: 92 additions & 0 deletions src/test/rpc/RPCHelpers_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#include <xrpld/rpc/detail/RPCHelpers.h>
#include <xrpl/beast/unit_test.h>
#include <xrpl/protocol/jss.h>

namespace ripple {
namespace test {

class RPCHelpers_test : public beast::unit_test::suite
{
public:
void
testChooseLedgerEntryType()
{
testcase("ChooseLedgerEntryType");

// Test no type.
Json::Value tx = Json::objectValue;
auto result = RPC::chooseLedgerEntryType(tx);
BEAST_EXPECT(result.first == RPC::Status::OK);
BEAST_EXPECT(result.second == 0);

// Test empty type.
tx[jss::type] = "";
result = RPC::chooseLedgerEntryType(tx);
BEAST_EXPECT(result.first == RPC::Status{rpcINVALID_PARAMS});
BEAST_EXPECT(result.second == 0);

// Test type using canonical name in mixedcase.
tx[jss::type] = "MPTokenIssuance";
result = RPC::chooseLedgerEntryType(tx);
BEAST_EXPECT(result.first == RPC::Status::OK);
BEAST_EXPECT(result.second == ltMPTOKEN_ISSUANCE);

// Test type using canonical name in lowercase.
tx[jss::type] = "mptokenissuance";
result = RPC::chooseLedgerEntryType(tx);
BEAST_EXPECT(result.first == RPC::Status::OK);
BEAST_EXPECT(result.second == ltMPTOKEN_ISSUANCE);

// Test type using RPC name with exact match.
tx[jss::type] = "mpt_issuance";
result = RPC::chooseLedgerEntryType(tx);
BEAST_EXPECT(result.first == RPC::Status::OK);
BEAST_EXPECT(result.second == ltMPTOKEN_ISSUANCE);

// Test type using RPC name with inexact match.
tx[jss::type] = "MPT_Issuance";
result = RPC::chooseLedgerEntryType(tx);
BEAST_EXPECT(result.first == RPC::Status{rpcINVALID_PARAMS});
BEAST_EXPECT(result.second == 0);

// Test invalid type.
tx[jss::type] = 1234;
result = RPC::chooseLedgerEntryType(tx);
BEAST_EXPECT(result.first == RPC::Status{rpcINVALID_PARAMS});
BEAST_EXPECT(result.second == 0);

// Test unknown type.
tx[jss::type] = "unknown";
result = RPC::chooseLedgerEntryType(tx);
BEAST_EXPECT(result.first == RPC::Status{rpcINVALID_PARAMS});
BEAST_EXPECT(result.second == 0);
}

void
run() override
{
testChooseLedgerEntryType();
}
};

BEAST_DEFINE_TESTSUITE(RPCHelpers, app, ripple);

} // namespace test
} // namespace ripple
24 changes: 14 additions & 10 deletions src/xrpld/rpc/detail/RPCHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
#include <xrpl/resource/Fees.h>

#include <boost/algorithm/string/case_conv.hpp>

#include <regex>
#include <boost/algorithm/string/predicate.hpp>

namespace ripple {
namespace RPC {
Expand Down Expand Up @@ -934,18 +933,19 @@ chooseLedgerEntryType(Json::Value const& params)
std::pair<RPC::Status, LedgerEntryType> result{RPC::Status::OK, ltANY};
if (params.isMember(jss::type))
{
static constexpr auto types =
std::to_array<std::pair<char const*, LedgerEntryType>>({
static constexpr auto types = std::to_array<
std::tuple<char const*, char const*, LedgerEntryType>>({
#pragma push_macro("LEDGER_ENTRY")
#undef LEDGER_ENTRY

#define LEDGER_ENTRY(tag, value, name, rpcName, fields) {jss::rpcName, tag},
#define LEDGER_ENTRY(tag, value, name, rpcName, fields) \
{jss::name, jss::rpcName, tag},

#include <xrpl/protocol/detail/ledger_entries.macro>

#undef LEDGER_ENTRY
#pragma pop_macro("LEDGER_ENTRY")
});
});

auto const& p = params[jss::type];
if (!p.isString())
Expand All @@ -958,10 +958,14 @@ chooseLedgerEntryType(Json::Value const& params)
return result;
}

// Use the passed in parameter to find a ledger type based on matching
// against the canonical name (case-insensitive) or the RPC name
// (case-sensitive).
auto const filter = p.asString();
auto iter = std::find_if(
types.begin(), types.end(), [&filter](decltype(types.front())& t) {
return t.first == filter;
const auto iter =
std::ranges::find_if(types, [&filter](decltype(types.front())& t) {
return boost::iequals(std::get<0>(t), filter) ||
std::get<1>(t) == filter;
});
if (iter == types.end())
{
Expand All @@ -973,7 +977,7 @@ chooseLedgerEntryType(Json::Value const& params)
"type");
return result;
}
result.second = iter->second;
result.second = std::get<2>(*iter);
}
return result;
}
Expand Down

0 comments on commit 6a3c6dd

Please sign in to comment.