Skip to content
76 changes: 76 additions & 0 deletions include/xrpl/protocol/ApiVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
#ifndef RIPPLE_PROTOCOL_APIVERSION_H_INCLUDED
#define RIPPLE_PROTOCOL_APIVERSION_H_INCLUDED

#include <xrpl/beast/core/SemanticVersion.h>
#include <xrpl/beast/utility/instrumentation.h>
#include <xrpl/json/json_value.h>
#include <xrpl/protocol/jss.h>

#include <type_traits>
#include <utility>

Expand Down Expand Up @@ -72,6 +77,77 @@ static_assert(apiMaximumSupportedVersion >= apiMinimumSupportedVersion);
static_assert(apiBetaVersion >= apiMaximumSupportedVersion);
static_assert(apiMaximumValidVersion >= apiMaximumSupportedVersion);

template <class JsonObject>
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 <unsigned minVer, unsigned maxVer, typename Fn, typename... Args>
Expand Down
1 change: 1 addition & 0 deletions src/test/app/Batch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <test/jtx/utility.h>

#include <xrpld/app/misc/HashRouter.h>
#include <xrpld/app/misc/NetworkOPs.h>
#include <xrpld/app/misc/Transaction.h>
#include <xrpld/app/tx/apply.h>
#include <xrpld/app/tx/detail/Batch.h>
Expand Down
2 changes: 1 addition & 1 deletion src/test/app/Path_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@

#include <xrpld/core/JobQueue.h>
#include <xrpld/rpc/RPCHandler.h>
#include <xrpld/rpc/detail/RPCHelpers.h>
#include <xrpld/rpc/detail/Tuning.h>

#include <xrpl/beast/unit_test.h>
#include <xrpl/json/json_reader.h>
#include <xrpl/protocol/ApiVersion.h>
#include <xrpl/protocol/STParsedJSON.h>
#include <xrpl/protocol/TxFlags.h>
#include <xrpl/protocol/jss.h>
Expand Down
2 changes: 0 additions & 2 deletions src/test/app/PayChan_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

#include <test/jtx.h>

#include <xrpld/rpc/detail/RPCHelpers.h>

#include <xrpl/basics/chrono.h>
#include <xrpl/ledger/Dir.h>
#include <xrpl/protocol/Feature.h>
Expand Down
2 changes: 1 addition & 1 deletion src/test/jtx/Env.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
#include <xrpld/app/main/Application.h>
#include <xrpld/app/paths/Pathfinder.h>
#include <xrpld/core/Config.h>
#include <xrpld/rpc/detail/RPCHelpers.h>

#include <xrpl/basics/Log.h>
#include <xrpl/basics/chrono.h>
#include <xrpl/beast/utility/Journal.h>
#include <xrpl/json/json_value.h>
#include <xrpl/json/to_string.h>
#include <xrpl/protocol/ApiVersion.h>
#include <xrpl/protocol/Feature.h>
#include <xrpl/protocol/Indexes.h>
#include <xrpl/protocol/Issue.h>
Expand Down
2 changes: 2 additions & 0 deletions src/test/jtx/TestHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include <test/jtx/Env.h>

#include <xrpld/app/misc/TxQ.h>

#include <xrpl/basics/base_uint.h>
#include <xrpl/beast/unit_test/suite.h>
#include <xrpl/json/json_value.h>
Expand Down
2 changes: 1 addition & 1 deletion src/test/jtx/impl/AMM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

#include <xrpld/app/misc/AMMHelpers.h>
#include <xrpld/app/misc/AMMUtils.h>
#include <xrpld/rpc/detail/RPCHelpers.h>

#include <xrpl/protocol/AMMCore.h>
#include <xrpl/protocol/AmountConversions.h>
#include <xrpl/protocol/ApiVersion.h>
#include <xrpl/protocol/jss.h>

namespace ripple {
Expand Down
2 changes: 1 addition & 1 deletion src/test/jtx/impl/AMMTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#include <test/jtx/pay.h>

#include <xrpld/rpc/RPCHandler.h>
#include <xrpld/rpc/detail/RPCHelpers.h>

#include <xrpl/protocol/ApiVersion.h>
#include <xrpl/protocol/STParsedJSON.h>
#include <xrpl/resource/Fees.h>

Expand Down
1 change: 0 additions & 1 deletion src/test/jtx/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <test/jtx/Account.h>

#include <xrpld/app/ledger/Ledger.h>
#include <xrpld/rpc/detail/RPCHelpers.h>

#include <xrpl/json/json_value.h>
#include <xrpl/protocol/STObject.h>
Expand Down
2 changes: 0 additions & 2 deletions src/test/rpc/GatewayBalances_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#include <test/jtx.h>
#include <test/jtx/WSClient.h>

#include <xrpld/rpc/detail/RPCHelpers.h>

#include <xrpl/beast/unit_test.h>
#include <xrpl/protocol/Feature.h>
#include <xrpl/protocol/jss.h>
Expand Down
1 change: 0 additions & 1 deletion src/test/rpc/LedgerRequestRPC_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <test/jtx.h>

#include <xrpld/app/ledger/LedgerMaster.h>
#include <xrpld/rpc/detail/RPCHelpers.h>

#include <xrpl/beast/unit_test.h>
#include <xrpl/protocol/ErrorCodes.h>
Expand Down
2 changes: 0 additions & 2 deletions src/test/rpc/NoRipple_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

#include <test/jtx.h>

#include <xrpld/rpc/detail/RPCHelpers.h>

#include <xrpl/protocol/Feature.h>
#include <xrpl/protocol/jss.h>

Expand Down
2 changes: 1 addition & 1 deletion src/test/rpc/RPCCall_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
#include <test/jtx/utility.h>

#include <xrpld/rpc/RPCCall.h>
#include <xrpld/rpc/detail/RPCHelpers.h>

#include <xrpl/beast/unit_test.h>
#include <xrpl/json/json_reader.h>
#include <xrpl/protocol/ApiVersion.h>
#include <xrpl/protocol/ErrorCodes.h>

#include <boost/algorithm/string.hpp>
Expand Down
2 changes: 0 additions & 2 deletions src/test/rpc/TransactionEntry_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
#include <test/jtx.h>
#include <test/jtx/Env.h>

#include <xrpld/rpc/detail/RPCHelpers.h>

#include <xrpl/json/json_reader.h>
#include <xrpl/json/json_value.h>
#include <xrpl/protocol/jss.h>
Expand Down
3 changes: 1 addition & 2 deletions src/test/rpc/Version_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

#include <test/jtx.h>

#include <xrpld/rpc/detail/RPCHelpers.h>

#include <xrpl/protocol/ApiVersion.h>
#include <xrpl/protocol/jss.h>

namespace ripple {
Expand Down
2 changes: 1 addition & 1 deletion src/xrpld/app/main/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
#include <xrpld/overlay/PeerSet.h>
#include <xrpld/overlay/make_Overlay.h>
#include <xrpld/perflog/PerfLog.h>
#include <xrpld/rpc/detail/RPCHelpers.h>
#include <xrpld/shamap/NodeFamily.h>

#include <xrpl/basics/ByteUtilities.h>
Expand All @@ -64,6 +63,7 @@
#include <xrpl/beast/core/LexicalCast.h>
#include <xrpl/crypto/csprng.h>
#include <xrpl/json/json_reader.h>
#include <xrpl/protocol/ApiVersion.h>
#include <xrpl/protocol/BuildInfo.h>
#include <xrpl/protocol/Feature.h>
#include <xrpl/protocol/Protocol.h>
Expand Down
1 change: 0 additions & 1 deletion src/xrpld/app/main/GRPCServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <xrpld/rpc/InfoSub.h>
#include <xrpld/rpc/Role.h>
#include <xrpld/rpc/detail/Handler.h>
#include <xrpld/rpc/detail/RPCHelpers.h>

#include <xrpl/proto/org/xrpl/rpc/v1/xrp_ledger.grpc.pb.h>
#include <xrpl/resource/Charge.h>
Expand Down
1 change: 0 additions & 1 deletion src/xrpld/rpc/detail/Handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
//==============================================================================

#include <xrpld/rpc/detail/Handler.h>
#include <xrpld/rpc/detail/RPCHelpers.h>
#include <xrpld/rpc/handlers/Handlers.h>
#include <xrpld/rpc/handlers/Version.h>

Expand Down
3 changes: 2 additions & 1 deletion src/xrpld/rpc/detail/Handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
#include <xrpld/app/misc/NetworkOPs.h>
#include <xrpld/rpc/RPCHandler.h>
#include <xrpld/rpc/Status.h>
#include <xrpld/rpc/detail/RPCHelpers.h>
#include <xrpld/rpc/detail/Tuning.h>

#include <xrpl/protocol/ApiVersion.h>

namespace Json {
class Object;
}
Expand Down
25 changes: 0 additions & 25 deletions src/xrpld/rpc/detail/RPCHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::shared_ptr<Ledger const>, Json::Value>
getLedgerByContext(RPC::JsonContext& context)
{
Expand Down
46 changes: 0 additions & 46 deletions src/xrpld/rpc/detail/RPCHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,35 +201,6 @@ getSeedFromRPC(Json::Value const& params, Json::Value& error);
std::optional<Seed>
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 <class Object>
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<RPC::Status, LedgerEntryType>
chooseLedgerEntryType(Json::Value const& params);

Expand All @@ -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<std::shared_ptr<Ledger const>, Json::Value>
Expand Down
3 changes: 2 additions & 1 deletion src/xrpld/rpc/detail/ServerHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
//==============================================================================

#include <xrpld/app/main/Application.h>
#include <xrpld/app/misc/NetworkOPs.h>
#include <xrpld/core/ConfigSections.h>
#include <xrpld/core/JobQueue.h>
#include <xrpld/overlay/Overlay.h>
#include <xrpld/rpc/RPCHandler.h>
#include <xrpld/rpc/Role.h>
#include <xrpld/rpc/ServerHandler.h>
#include <xrpld/rpc/detail/RPCHelpers.h>
#include <xrpld/rpc/detail/Tuning.h>
#include <xrpld/rpc/json_body.h>

Expand All @@ -36,6 +36,7 @@
#include <xrpl/beast/rfc2616.h>
#include <xrpl/json/json_reader.h>
#include <xrpl/json/to_string.h>
#include <xrpl/protocol/ApiVersion.h>
#include <xrpl/protocol/ErrorCodes.h>
#include <xrpl/protocol/RPCErr.h>
#include <xrpl/resource/Fees.h>
Expand Down
1 change: 1 addition & 0 deletions src/xrpld/rpc/handlers/LedgerHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <xrpld/app/misc/LoadFeeTrack.h>
#include <xrpld/rpc/GRPCHandlers.h>
#include <xrpld/rpc/Role.h>
#include <xrpld/rpc/detail/RPCHelpers.h>
#include <xrpld/rpc/handlers/LedgerHandler.h>

#include <xrpl/protocol/ErrorCodes.h>
Expand Down
Loading