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

APIv2(ledger_entry): return "invalidParams" when fields missing #4552

Merged
merged 16 commits into from
Jul 6, 2023
Merged
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
7 changes: 6 additions & 1 deletion src/ripple/rpc/handlers/LedgerEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,12 @@ doLedgerEntry(RPC::JsonContext& context)
}
}
else
jvResult[jss::error] = "unknownOption";
{
if (context.apiVersion < 2u)
jvResult[jss::error] = "unknownOption";
else
jvResult[jss::error] = "invalidParams";
}
}

if (uNodeIndex.isNonZero())
Expand Down
23 changes: 19 additions & 4 deletions src/test/rpc/LedgerRPC_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <ripple/beast/unit_test.h>
#include <ripple/protocol/ErrorCodes.h>
#include <ripple/protocol/jss.h>
#include <ripple/rpc/impl/RPCHelpers.h>
#include <test/jtx.h>

namespace ripple {
Expand Down Expand Up @@ -1212,21 +1213,28 @@ class LedgerRPC_test : public beast::unit_test::suite
}

void
testLedgerEntryUnknownOption()
testLedgerEntryInvalidParams(unsigned int apiVersion)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure what the convention is in rippled but i'd use uint32_t or perhaps uint16_t. Fine as it is if others are happy though

{
testcase("ledger_entry Request Unknown Option");
testcase(
"ledger_entry Request With Invalid Parameters v" +
std::to_string(apiVersion));
using namespace test::jtx;
Env env{*this};

std::string const ledgerHash{to_string(env.closed()->info().hash)};

// "features" is not an option supported by ledger_entry.
Json::Value jvParams;
jvParams[jss::api_version] = apiVersion;
jvParams[jss::features] = ledgerHash;
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr =
env.rpc("json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "unknownOption", "");

if (apiVersion < 2u)
checkErrorValue(jrr, "unknownOption", "");
else
checkErrorValue(jrr, "invalidParams", "");
}

/// @brief ledger RPC requests as a way to drive
Expand Down Expand Up @@ -1724,11 +1732,18 @@ class LedgerRPC_test : public beast::unit_test::suite
testLedgerEntryPayChan();
testLedgerEntryRippleState();
testLedgerEntryTicket();
testLedgerEntryUnknownOption();
testLookupLedger();
testNoQueue();
testQueue();
testLedgerAccountsOption();

// version specific tests
for (auto testVersion = RPC::apiMinimumSupportedVersion;
Copy link
Collaborator

Choose a reason for hiding this comment

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

It could be beneficial to extract this into some sort of helper function (maybe in the next pr) as i imagine this will be copy-pasted all over the place very soon. Better do it sooner than later. I'd go with a variadic template accepting callables (functions expecting api version as their input).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

you are right, it will get copied. I'll try this out in the next PR. Thank you for pointing it out.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

any idea what would be the most appropriate file to put this helper in ? RPCHelpers ? (cc: @ximinez )

Copy link
Collaborator

Choose a reason for hiding this comment

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

Since it's only going to be used in tests, it should be under src/test. Env.h already has a lot of stuff, including the functions to make RPC calls, so maybe a static member or standalone function in there... Alternately, just create a new RPCUtils.h under src/test/jtx.

Another alternative if you feel like rewriting this test again is to create a member function of Env, so you can loop over each test case individually without the overhead of creating a new Env every time.

I don't know for sure, though.

testVersion <= RPC::apiBetaVersion;
arihantkothari marked this conversation as resolved.
Show resolved Hide resolved
++testVersion)
{
testLedgerEntryInvalidParams(testVersion);
}
intelliot marked this conversation as resolved.
Show resolved Hide resolved
}
};

Expand Down