From b18dece1459e33497b7a4b1a1a1f74ea2feba01b Mon Sep 17 00:00:00 2001 From: Mayukha Vadari Date: Mon, 3 Nov 2025 14:09:14 -0500 Subject: [PATCH] refactor: move API functions from `RPCHelpers.h` to `ApiVersion.h` (#5889) This change moves two functions, `setVersion` and `getAPIVersionNumber`, from `RPCHelpers.h` to `ApiVersion.h`. --- include/xrpl/protocol/ApiVersion.h | 76 ++++++++++++++++++++++++ src/test/app/Batch_test.cpp | 1 + src/test/app/Path_test.cpp | 2 +- src/test/app/PayChan_test.cpp | 2 - src/test/jtx/Env.h | 2 +- src/test/jtx/TestHelpers.h | 2 + src/test/jtx/impl/AMM.cpp | 2 +- src/test/jtx/impl/AMMTest.cpp | 2 +- src/test/jtx/utility.h | 1 - src/test/rpc/GatewayBalances_test.cpp | 2 - src/test/rpc/LedgerRequestRPC_test.cpp | 1 - src/test/rpc/NoRipple_test.cpp | 2 - src/test/rpc/RPCCall_test.cpp | 2 +- src/test/rpc/TransactionEntry_test.cpp | 2 - src/test/rpc/Version_test.cpp | 3 +- src/xrpld/app/main/Application.cpp | 2 +- src/xrpld/app/main/GRPCServer.h | 1 - src/xrpld/rpc/detail/Handler.cpp | 1 - src/xrpld/rpc/detail/Handler.h | 3 +- src/xrpld/rpc/detail/RPCHelpers.cpp | 25 -------- src/xrpld/rpc/detail/RPCHelpers.h | 46 -------------- src/xrpld/rpc/detail/ServerHandler.cpp | 3 +- src/xrpld/rpc/handlers/LedgerHandler.cpp | 1 + src/xrpld/rpc/handlers/LedgerHandler.h | 2 +- src/xrpld/rpc/handlers/Tx.cpp | 1 - src/xrpld/rpc/handlers/Version.h | 2 +- 26 files changed, 93 insertions(+), 96 deletions(-) diff --git a/include/xrpl/protocol/ApiVersion.h b/include/xrpl/protocol/ApiVersion.h index deafafa513..c836fbb5b4 100644 --- a/include/xrpl/protocol/ApiVersion.h +++ b/include/xrpl/protocol/ApiVersion.h @@ -20,6 +20,11 @@ #ifndef RIPPLE_PROTOCOL_APIVERSION_H_INCLUDED #define RIPPLE_PROTOCOL_APIVERSION_H_INCLUDED +#include +#include +#include +#include + #include #include @@ -72,6 +77,77 @@ static_assert(apiMaximumSupportedVersion >= apiMinimumSupportedVersion); static_assert(apiBetaVersion >= apiMaximumSupportedVersion); static_assert(apiMaximumValidVersion >= apiMaximumSupportedVersion); +template +void +setVersion(JsonObject& parent, unsigned int apiVersion, bool betaEnabled) +{ + XRPL_ASSERT( + apiVersion != apiInvalidVersion, + "ripple::RPC::setVersion : input is valid"); + auto& retObj = addObject(parent, jss::version); + + if (apiVersion == apiVersionIfUnspecified) + { + // API version numbers used in API version 1 + static beast::SemanticVersion const firstVersion{"1.0.0"}; + static beast::SemanticVersion const goodVersion{"1.0.0"}; + static beast::SemanticVersion const lastVersion{"1.0.0"}; + + retObj[jss::first] = firstVersion.print(); + retObj[jss::good] = goodVersion.print(); + retObj[jss::last] = lastVersion.print(); + } + else + { + retObj[jss::first] = apiMinimumSupportedVersion.value; + retObj[jss::last] = + betaEnabled ? apiBetaVersion : apiMaximumSupportedVersion; + } +} + +/** + * Retrieve the api version number from the json value + * + * Note that APIInvalidVersion will be returned if + * 1) the version number field has a wrong format + * 2) the version number retrieved is out of the supported range + * 3) the version number is unspecified and + * APIVersionIfUnspecified is out of the supported range + * + * @param jv a Json value that may or may not specifies + * the api version number + * @param betaEnabled if the beta API version is enabled + * @return the api version number + */ +inline unsigned int +getAPIVersionNumber(Json::Value const& jv, bool betaEnabled) +{ + static Json::Value const minVersion(RPC::apiMinimumSupportedVersion); + Json::Value const maxVersion( + betaEnabled ? RPC::apiBetaVersion : RPC::apiMaximumSupportedVersion); + + if (jv.isObject()) + { + if (jv.isMember(jss::api_version)) + { + auto const specifiedVersion = jv[jss::api_version]; + if (!specifiedVersion.isInt() && !specifiedVersion.isUInt()) + { + return RPC::apiInvalidVersion; + } + auto const specifiedVersionInt = specifiedVersion.asInt(); + if (specifiedVersionInt < minVersion || + specifiedVersionInt > maxVersion) + { + return RPC::apiInvalidVersion; + } + return specifiedVersionInt; + } + } + + return RPC::apiVersionIfUnspecified; +} + } // namespace RPC template diff --git a/src/test/app/Batch_test.cpp b/src/test/app/Batch_test.cpp index c87d4b46d2..7b5c9dee60 100644 --- a/src/test/app/Batch_test.cpp +++ b/src/test/app/Batch_test.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include #include diff --git a/src/test/app/Path_test.cpp b/src/test/app/Path_test.cpp index 6ff22a5dc7..26b3dab898 100644 --- a/src/test/app/Path_test.cpp +++ b/src/test/app/Path_test.cpp @@ -25,11 +25,11 @@ #include #include -#include #include #include #include +#include #include #include #include diff --git a/src/test/app/PayChan_test.cpp b/src/test/app/PayChan_test.cpp index 595a12aed9..9cbe4817b0 100644 --- a/src/test/app/PayChan_test.cpp +++ b/src/test/app/PayChan_test.cpp @@ -19,8 +19,6 @@ #include -#include - #include #include #include diff --git a/src/test/jtx/Env.h b/src/test/jtx/Env.h index 68d8d3e53f..7181ec2a3e 100644 --- a/src/test/jtx/Env.h +++ b/src/test/jtx/Env.h @@ -36,13 +36,13 @@ #include #include #include -#include #include #include #include #include #include +#include #include #include #include diff --git a/src/test/jtx/TestHelpers.h b/src/test/jtx/TestHelpers.h index 5d919add47..81d2f9c5ec 100644 --- a/src/test/jtx/TestHelpers.h +++ b/src/test/jtx/TestHelpers.h @@ -22,6 +22,8 @@ #include +#include + #include #include #include diff --git a/src/test/jtx/impl/AMM.cpp b/src/test/jtx/impl/AMM.cpp index ca96401bc4..999fe73e3f 100644 --- a/src/test/jtx/impl/AMM.cpp +++ b/src/test/jtx/impl/AMM.cpp @@ -22,10 +22,10 @@ #include #include -#include #include #include +#include #include namespace ripple { diff --git a/src/test/jtx/impl/AMMTest.cpp b/src/test/jtx/impl/AMMTest.cpp index 5bb8f14cbf..7238cac7eb 100644 --- a/src/test/jtx/impl/AMMTest.cpp +++ b/src/test/jtx/impl/AMMTest.cpp @@ -24,8 +24,8 @@ #include #include -#include +#include #include #include diff --git a/src/test/jtx/utility.h b/src/test/jtx/utility.h index 9e89c3bb93..555adc21de 100644 --- a/src/test/jtx/utility.h +++ b/src/test/jtx/utility.h @@ -23,7 +23,6 @@ #include #include -#include #include #include diff --git a/src/test/rpc/GatewayBalances_test.cpp b/src/test/rpc/GatewayBalances_test.cpp index a13e5bc20c..30f0701e9e 100644 --- a/src/test/rpc/GatewayBalances_test.cpp +++ b/src/test/rpc/GatewayBalances_test.cpp @@ -18,8 +18,6 @@ #include #include -#include - #include #include #include diff --git a/src/test/rpc/LedgerRequestRPC_test.cpp b/src/test/rpc/LedgerRequestRPC_test.cpp index 03be9fb29b..7b9326963b 100644 --- a/src/test/rpc/LedgerRequestRPC_test.cpp +++ b/src/test/rpc/LedgerRequestRPC_test.cpp @@ -20,7 +20,6 @@ #include #include -#include #include #include diff --git a/src/test/rpc/NoRipple_test.cpp b/src/test/rpc/NoRipple_test.cpp index 0f2b4748b6..626a70ce00 100644 --- a/src/test/rpc/NoRipple_test.cpp +++ b/src/test/rpc/NoRipple_test.cpp @@ -19,8 +19,6 @@ #include -#include - #include #include diff --git a/src/test/rpc/RPCCall_test.cpp b/src/test/rpc/RPCCall_test.cpp index 26a6a536ef..cc84a375f6 100644 --- a/src/test/rpc/RPCCall_test.cpp +++ b/src/test/rpc/RPCCall_test.cpp @@ -19,10 +19,10 @@ #include #include -#include #include #include +#include #include #include diff --git a/src/test/rpc/TransactionEntry_test.cpp b/src/test/rpc/TransactionEntry_test.cpp index d8f685f568..27e1f659b5 100644 --- a/src/test/rpc/TransactionEntry_test.cpp +++ b/src/test/rpc/TransactionEntry_test.cpp @@ -20,8 +20,6 @@ #include #include -#include - #include #include #include diff --git a/src/test/rpc/Version_test.cpp b/src/test/rpc/Version_test.cpp index c0bc5d450b..4c310b9ab9 100644 --- a/src/test/rpc/Version_test.cpp +++ b/src/test/rpc/Version_test.cpp @@ -19,8 +19,7 @@ #include -#include - +#include #include namespace ripple { diff --git a/src/xrpld/app/main/Application.cpp b/src/xrpld/app/main/Application.cpp index 12a5b3d409..3725fcea6d 100644 --- a/src/xrpld/app/main/Application.cpp +++ b/src/xrpld/app/main/Application.cpp @@ -53,7 +53,6 @@ #include #include #include -#include #include #include @@ -64,6 +63,7 @@ #include #include #include +#include #include #include #include diff --git a/src/xrpld/app/main/GRPCServer.h b/src/xrpld/app/main/GRPCServer.h index c48138cd92..c8beb86749 100644 --- a/src/xrpld/app/main/GRPCServer.h +++ b/src/xrpld/app/main/GRPCServer.h @@ -27,7 +27,6 @@ #include #include #include -#include #include #include diff --git a/src/xrpld/rpc/detail/Handler.cpp b/src/xrpld/rpc/detail/Handler.cpp index d15c5aaed0..9f8028f9fd 100644 --- a/src/xrpld/rpc/detail/Handler.cpp +++ b/src/xrpld/rpc/detail/Handler.cpp @@ -18,7 +18,6 @@ //============================================================================== #include -#include #include #include diff --git a/src/xrpld/rpc/detail/Handler.h b/src/xrpld/rpc/detail/Handler.h index 8c263a90ab..85744b7fd2 100644 --- a/src/xrpld/rpc/detail/Handler.h +++ b/src/xrpld/rpc/detail/Handler.h @@ -24,9 +24,10 @@ #include #include #include -#include #include +#include + namespace Json { class Object; } diff --git a/src/xrpld/rpc/detail/RPCHelpers.cpp b/src/xrpld/rpc/detail/RPCHelpers.cpp index 9c49751bcd..a1eb602d57 100644 --- a/src/xrpld/rpc/detail/RPCHelpers.cpp +++ b/src/xrpld/rpc/detail/RPCHelpers.cpp @@ -1007,31 +1007,6 @@ isAccountObjectsValidType(LedgerEntryType const& type) } } -beast::SemanticVersion const firstVersion("1.0.0"); -beast::SemanticVersion const goodVersion("1.0.0"); -beast::SemanticVersion const lastVersion("1.0.0"); - -unsigned int -getAPIVersionNumber(Json::Value const& jv, bool betaEnabled) -{ - static Json::Value const minVersion(RPC::apiMinimumSupportedVersion); - static Json::Value const invalidVersion(RPC::apiInvalidVersion); - - Json::Value const maxVersion( - betaEnabled ? RPC::apiBetaVersion : RPC::apiMaximumSupportedVersion); - Json::Value requestedVersion(RPC::apiVersionIfUnspecified); - if (jv.isObject()) - { - requestedVersion = jv.get(jss::api_version, requestedVersion); - } - if (!(requestedVersion.isInt() || requestedVersion.isUInt()) || - requestedVersion < minVersion || requestedVersion > maxVersion) - { - requestedVersion = invalidVersion; - } - return requestedVersion.asUInt(); -} - std::variant, Json::Value> getLedgerByContext(RPC::JsonContext& context) { diff --git a/src/xrpld/rpc/detail/RPCHelpers.h b/src/xrpld/rpc/detail/RPCHelpers.h index 1d33d69459..836cef64ce 100644 --- a/src/xrpld/rpc/detail/RPCHelpers.h +++ b/src/xrpld/rpc/detail/RPCHelpers.h @@ -201,35 +201,6 @@ getSeedFromRPC(Json::Value const& params, Json::Value& error); std::optional parseRippleLibSeed(Json::Value const& params); -/** - * API version numbers used in API version 1 - */ -extern beast::SemanticVersion const firstVersion; -extern beast::SemanticVersion const goodVersion; -extern beast::SemanticVersion const lastVersion; - -template -void -setVersion(Object& parent, unsigned int apiVersion, bool betaEnabled) -{ - XRPL_ASSERT( - apiVersion != apiInvalidVersion, - "ripple::RPC::setVersion : input is valid"); - auto&& object = addObject(parent, jss::version); - if (apiVersion == apiVersionIfUnspecified) - { - object[jss::first] = firstVersion.print(); - object[jss::good] = goodVersion.print(); - object[jss::last] = lastVersion.print(); - } - else - { - object[jss::first] = apiMinimumSupportedVersion.value; - object[jss::last] = - betaEnabled ? apiBetaVersion : apiMaximumSupportedVersion; - } -} - std::pair chooseLedgerEntryType(Json::Value const& params); @@ -242,23 +213,6 @@ chooseLedgerEntryType(Json::Value const& params); bool isAccountObjectsValidType(LedgerEntryType const& type); -/** - * Retrieve the api version number from the json value - * - * Note that APIInvalidVersion will be returned if - * 1) the version number field has a wrong format - * 2) the version number retrieved is out of the supported range - * 3) the version number is unspecified and - * APIVersionIfUnspecified is out of the supported range - * - * @param value a Json value that may or may not specifies - * the api version number - * @param betaEnabled if the beta API version is enabled - * @return the api version number - */ -unsigned int -getAPIVersionNumber(Json::Value const& value, bool betaEnabled); - /** Return a ledger based on ledger_hash or ledger_index, or an RPC error */ std::variant, Json::Value> diff --git a/src/xrpld/rpc/detail/ServerHandler.cpp b/src/xrpld/rpc/detail/ServerHandler.cpp index 66f4efffc6..dd0dd0332c 100644 --- a/src/xrpld/rpc/detail/ServerHandler.cpp +++ b/src/xrpld/rpc/detail/ServerHandler.cpp @@ -18,13 +18,13 @@ //============================================================================== #include +#include #include #include #include #include #include #include -#include #include #include @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include diff --git a/src/xrpld/rpc/handlers/LedgerHandler.cpp b/src/xrpld/rpc/handlers/LedgerHandler.cpp index 8987f2d07e..0dd4307dca 100644 --- a/src/xrpld/rpc/handlers/LedgerHandler.cpp +++ b/src/xrpld/rpc/handlers/LedgerHandler.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include diff --git a/src/xrpld/rpc/handlers/LedgerHandler.h b/src/xrpld/rpc/handlers/LedgerHandler.h index 4cbf984505..072a918f0f 100644 --- a/src/xrpld/rpc/handlers/LedgerHandler.h +++ b/src/xrpld/rpc/handlers/LedgerHandler.h @@ -27,10 +27,10 @@ #include #include #include -#include #include #include +#include #include namespace Json { diff --git a/src/xrpld/rpc/handlers/Tx.cpp b/src/xrpld/rpc/handlers/Tx.cpp index d43a699ab3..32b9775558 100644 --- a/src/xrpld/rpc/handlers/Tx.cpp +++ b/src/xrpld/rpc/handlers/Tx.cpp @@ -28,7 +28,6 @@ #include #include #include -#include #include #include diff --git a/src/xrpld/rpc/handlers/Version.h b/src/xrpld/rpc/handlers/Version.h index 4efeed2484..89baf70eb4 100644 --- a/src/xrpld/rpc/handlers/Version.h +++ b/src/xrpld/rpc/handlers/Version.h @@ -20,7 +20,7 @@ #ifndef RIPPLED_RIPPLE_RPC_HANDLERS_VERSION_H #define RIPPLED_RIPPLE_RPC_HANDLERS_VERSION_H -#include +#include namespace ripple { namespace RPC {