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

Fix account_objects command for AMM account root #4678

Closed
wants to merge 1 commit into from
Closed
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
34 changes: 34 additions & 0 deletions src/ripple/rpc/impl/RPCHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,40 @@ getAccountObjects(
entryIndex = beast::zero;
}

// AMM root account requires special handling to fetch AMM object and
// trustlines
bool const filterAMM =
typeFilter.has_value() && typeMatchesFilter(typeFilter.value(), ltAMM);
// skip amm if marker (dirIndex) is provided
if (dirIndex == uint256{} && (!typeFilter.has_value() || filterAMM))
Copy link
Collaborator

Choose a reason for hiding this comment

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

I like declaring variables in if statements for simple cases where it saves polluting the parent scope with extra variables. Here I think the code is clearer if the variables are declared in the parent scope (which is already small). I'd also remove the == using256{} construct. Something like:

    if (!dirIndex.signum() && (!typeFilter.has_value() || filterAMM))
    {
        auto const sle = ledger.read(keylet::account(account));
        if (!sle)
            return false;
        bool const isAMM = sle->isFieldPresent(sfAMMID);
        if (filterAMM && !isAMM)
            return false;
        if (isAMM)
        {
            auto const sleAMM =
                ledger.read(keylet::amm(sle->getFieldH256(sfAMMID)));
            if (!sleAMM)
                return false;

            jvObjects.append(sleAMM->getJson(JsonOptions::none));

            if (filterAMM)
                return true;
            if (limit == 1)
            {
                jvResult[jss::limit] = limit;
                jvResult[jss::marker] = "AMM";
                return true;
            }
            if (limit > 1)
                --mlimit;
        }

{
if (auto const sle = ledger.read(keylet::account(account)); !sle)
return false;
else if (bool const isAMM = sle->isFieldPresent(sfAMMID);
filterAMM && !isAMM)
return false;
else if (isAMM)
{
auto const sleAMM =
ledger.read(keylet::amm(sle->getFieldH256(sfAMMID)));
if (!sleAMM)
return false;

jvObjects.append(sleAMM->getJson(JsonOptions::none));

if (filterAMM)
return true;
if (limit == 1)
{
jvResult[jss::limit] = limit;
jvResult[jss::marker] = "AMM";
return true;
}
if (limit > 1)
--mlimit;
}
}

auto const root = keylet::ownerDir(account);
auto found = false;

Expand Down
69 changes: 66 additions & 3 deletions src/test/rpc/AccountObjects_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <ripple/json/to_string.h>
#include <ripple/protocol/jss.h>
#include <test/jtx.h>
#include <test/jtx/AMM.h>

#include <boost/utility/string_ref.hpp>

Expand Down Expand Up @@ -552,10 +553,19 @@ class AccountObjects_test : public beast::unit_test::suite
Env env(*this);

// Make a lambda we can use to get "account_objects" easily.
auto acct_objs = [&env](Account const& acct, char const* type) {
auto acct_objs = [&env](
AccountID const& acct,
std::optional<Json::StaticString> const& type,
std::optional<std::uint16_t> limit = std::nullopt,
std::optional<std::string> marker = std::nullopt) {
Json::Value params;
params[jss::account] = acct.human();
params[jss::type] = type;
params[jss::account] = to_string(acct);
if (type)
params[jss::type] = *type;
if (limit)
params[jss::limit] = *limit;
if (marker)
params[jss::marker] = *marker;
params[jss::ledger_index] = "validated";
return env.rpc("json", "account_objects", to_string(params));
};
Expand Down Expand Up @@ -585,6 +595,7 @@ class AccountObjects_test : public beast::unit_test::suite
BEAST_EXPECT(acct_objs_is_size(acct_objs(gw, jss::signer_list), 0));
BEAST_EXPECT(acct_objs_is_size(acct_objs(gw, jss::state), 0));
BEAST_EXPECT(acct_objs_is_size(acct_objs(gw, jss::ticket), 0));
BEAST_EXPECT(acct_objs_is_size(acct_objs(gw, jss::amm), 0));

// gw mints an NFT so we can find it.
uint256 const nftID{token::getNextID(env, gw, 0u, tfTransferable)};
Expand Down Expand Up @@ -782,6 +793,58 @@ class AccountObjects_test : public beast::unit_test::suite
BEAST_EXPECT(aobjs[0u]["LedgerEntryType"] == jss::Escrow);
}
}
{
// Make a lambda we can use to check the number of fetched
// account objects and their ledger type
auto expectObjects =
[&](Json::Value const& resp,
std::vector<Json::StaticString> const& types) -> bool {
if (!acct_objs_is_size(resp, types.size()))
return false;
auto const objs = resp[jss::result][jss::account_objects];
for (std::uint32_t i = 0; i < types.size(); ++i)
if (objs[i][sfLedgerEntryType.fieldName].asString() !=
types[i])
return false;
return true;
};
// Find AMM objects
AMM amm(env, gw, XRP(1'000), USD(1'000));
amm.deposit(alice, USD(1));
// AMM account has 4 objects: AMM object and 3 trustlines
auto const lines = getAccountLines(env, amm.ammAccount());
BEAST_EXPECT(lines[jss::lines].size() == 3);
// request AMM only, doesn't depend on the limit
BEAST_EXPECT(
acct_objs_is_size(acct_objs(amm.ammAccount(), jss::amm), 1));
BEAST_EXPECT(
acct_objs_is_size(acct_objs(amm.ammAccount(), jss::amm, 1), 1));
BEAST_EXPECT(
acct_objs_is_size(acct_objs(amm.ammAccount(), jss::amm, 2), 1));
// no filter and limit 1 returns AMM only
auto resp = acct_objs(amm.ammAccount(), std::nullopt, 1);
BEAST_EXPECT(expectObjects(resp, {jss::AMM}));
// request first two: first two objects are AMM and trustline
resp = acct_objs(amm.ammAccount(), std::nullopt, 2);
BEAST_EXPECT(expectObjects(resp, {jss::AMM, jss::RippleState}));
// request next: next two objects are trustlines
resp = acct_objs(
amm.ammAccount(),
std::nullopt,
10,
resp[jss::result][jss::marker].asString());
BEAST_EXPECT(
expectObjects(resp, {jss::RippleState, jss::RippleState}));
// filter by state: there are three trustlines
resp = acct_objs(amm.ammAccount(), jss::state, 10);
BEAST_EXPECT(expectObjects(
resp, {jss::RippleState, jss::RippleState, jss::RippleState}));
// AMM account doesn't own offers
BEAST_EXPECT(
acct_objs_is_size(acct_objs(amm.ammAccount(), jss::offer), 0));
// gw account doesn't own AMM object
BEAST_EXPECT(acct_objs_is_size(acct_objs(gw, jss::amm), 0));
}

// Run up the number of directory entries so gw has two
// directory nodes.
Expand Down