Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support canonical ledger entry names #5271

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>>({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: For readability, consider a small struct instead of the tuple.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured that if we're going to deprecate the current way of referencing ledger entries, then the next release we can return to using a std::pair - which would be preferred over a dedicated struct.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although the RPC name-based deprecation is no longer going to happen, I looked at this again and think that adding a new struct just for this function is overkill, however small that struct may be.

#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
Loading