From 97fb65c6d35a352adbe6f5064dc779e66741bcfb Mon Sep 17 00:00:00 2001 From: Arihant Kothari Date: Thu, 6 Jul 2023 14:58:53 -0400 Subject: [PATCH] APIv2(ledger_entry): return "invalidParams" when fields missing (#4552) Improve error handling for ledger_entry by returning an "invalidParams" error when one or more request fields are specified incorrectly, or one or more required fields are missing. For example, if none of of the following fields is provided, then the API should return an invalidParams error: * index, account_root, directory, offer, ripple_state, check, escrow, payment_channel, deposit_preauth, ticket Prior to this commit, the API returned an "unknownOption" error instead. Since the error was actually due to invalid parameters, rather than unknown options, this error was misleading. Since this is an API breaking change, the "invalidParams" error is only returned for requests using api_version: 2 and above. To maintain backward compatibility, the "unknownOption" error is still returned for api_version: 1. Related: #4573 Fix #4303 --- src/ripple/rpc/handlers/LedgerEntry.cpp | 7 ++++++- src/test/rpc/LedgerRPC_test.cpp | 23 +++++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/ripple/rpc/handlers/LedgerEntry.cpp b/src/ripple/rpc/handlers/LedgerEntry.cpp index aa5ffb02a..f53bf9c60 100644 --- a/src/ripple/rpc/handlers/LedgerEntry.cpp +++ b/src/ripple/rpc/handlers/LedgerEntry.cpp @@ -520,7 +520,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()) diff --git a/src/test/rpc/LedgerRPC_test.cpp b/src/test/rpc/LedgerRPC_test.cpp index 50188dd88..3022331a7 100644 --- a/src/test/rpc/LedgerRPC_test.cpp +++ b/src/test/rpc/LedgerRPC_test.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -1832,9 +1833,11 @@ public: } void - testLedgerEntryUnknownOption() + testLedgerEntryInvalidParams(unsigned int apiVersion) { - 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}; @@ -1842,11 +1845,16 @@ public: // "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 @@ -2356,11 +2364,18 @@ public: testLedgerEntryTicket(); testLedgerEntryURIToken(); testLedgerEntryImportVLSeq(); - testLedgerEntryUnknownOption(); testLookupLedger(); testNoQueue(); testQueue(); testLedgerAccountsOption(); + + // version specific tests + for (auto testVersion = RPC::apiMinimumSupportedVersion; + testVersion <= RPC::apiBetaVersion; + ++testVersion) + { + testLedgerEntryInvalidParams(testVersion); + } } };