Skip to content

Commit

Permalink
Add RPC::apiVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
Bronek committed Dec 7, 2023
1 parent 841a931 commit 4db09b7
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/ripple/app/misc/NetworkOPs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2211,7 +2211,7 @@ NetworkOPsImp::pubValidation(std::shared_ptr<STValidation> const& val)
// for consumers supporting different API versions
MultiApiJson multiObj{jvObj};
multiObj.visit(
std::integral_constant<unsigned int, 1>{}, //
RPC::apiVersion<1>, //
[](Json::Value& jvTx) {
// Type conversion for older API versions to string
if (jvTx.isMember(jss::ledger_index))
Expand Down
18 changes: 11 additions & 7 deletions src/ripple/protocol/ApiVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,17 @@ namespace ripple {

namespace RPC {

constexpr unsigned int apiInvalidVersion = 0;
constexpr unsigned int apiVersionIfUnspecified = 1;
constexpr unsigned int apiMinimumSupportedVersion = 1;
constexpr unsigned int apiMaximumSupportedVersion = 2;
constexpr unsigned int apiCommandLineVersion = 1; // TODO Bump to 2 later
constexpr unsigned int apiBetaVersion = 3;
constexpr unsigned int apiMaximumValidVersion = apiBetaVersion;
template <unsigned int Version>
constexpr static std::integral_constant<unsigned, Version> apiVersion = {};

constexpr static auto apiInvalidVersion = apiVersion<0>;
constexpr static auto apiVersionIfUnspecified = apiVersion<1>;
constexpr static auto apiMinimumSupportedVersion = apiVersion<1>;
constexpr static auto apiMaximumSupportedVersion = apiVersion<2>;
constexpr static auto apiCommandLineVersion =
apiVersion<1>; // TODO Bump to 2 later
constexpr static auto apiBetaVersion = apiVersion<3>;
constexpr static auto apiMaximumValidVersion = apiBetaVersion;

static_assert(apiMinimumSupportedVersion >= apiVersionIfUnspecified);
static_assert(apiMaximumSupportedVersion >= apiMinimumSupportedVersion);
Expand Down
2 changes: 1 addition & 1 deletion src/ripple/rpc/impl/RPCHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ setVersion(Object& parent, unsigned int apiVersion, bool betaEnabled)
}
else
{
object[jss::first] = apiMinimumSupportedVersion;
object[jss::first] = apiMinimumSupportedVersion.value;
object[jss::last] =
betaEnabled ? apiBetaVersion : apiMaximumSupportedVersion;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ripple/rpc/impl/ServerHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ ServerHandler::processRequest(
continue;
}

auto apiVersion = RPC::apiVersionIfUnspecified;
unsigned apiVersion = RPC::apiVersionIfUnspecified;
if (jsonRPC.isMember(jss::params) && jsonRPC[jss::params].isArray() &&
jsonRPC[jss::params].size() > 0 &&
jsonRPC[jss::params][0u].isObject())
Expand Down
7 changes: 2 additions & 5 deletions src/test/app/PayChan_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1088,10 +1088,7 @@ struct PayChan_test : public beast::unit_test::suite
args[jss::amount] = 51110000;

// test for all api versions
for (auto apiVersion = RPC::apiMinimumSupportedVersion;
apiVersion <= RPC::apiBetaVersion;
++apiVersion)
{
forAllApiVersions([&, this](unsigned apiVersion) {
testcase(
"PayChan Channel_Auth RPC Api " + std::to_string(apiVersion));
args[jss::api_version] = apiVersion;
Expand All @@ -1101,7 +1098,7 @@ struct PayChan_test : public beast::unit_test::suite
args.toStyledString())[jss::result];
auto const error = apiVersion < 2u ? "invalidParams" : "badKeyType";
BEAST_EXPECT(rs[jss::error] == error);
}
});
}

void
Expand Down
7 changes: 4 additions & 3 deletions src/test/protocol/ApiVersion_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct ApiVersion_test : beast::unit_test::suite
RPC::apiMinimumSupportedVersion);

int productAllVersions = 1;
for (auto i = RPC::apiMinimumSupportedVersion;
for (unsigned i = RPC::apiMinimumSupportedVersion;
i <= RPC::apiMaximumValidVersion;
++i)
{
Expand All @@ -102,8 +102,9 @@ struct ApiVersion_test : beast::unit_test::suite
}
BEAST_EXPECT(!s1.valid(0));
BEAST_EXPECT(!s1.valid(RPC::apiMaximumValidVersion + 1));
BEAST_EXPECT(!s1.valid(std::numeric_limits<decltype(
RPC::apiMaximumValidVersion)>::max()));
BEAST_EXPECT(
!s1.valid(std::numeric_limits<decltype(
RPC::apiMaximumValidVersion.value)>::max()));

int result = 1;
static_assert(
Expand Down
7 changes: 2 additions & 5 deletions src/test/rpc/GatewayBalances_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,7 @@ class GatewayBalances_test : public beast::unit_test::suite
qry2[jss::account] = alice.human();
qry2[jss::hotwallet] = "asdf";

for (auto apiVersion = RPC::apiMinimumSupportedVersion;
apiVersion <= RPC::apiBetaVersion;
++apiVersion)
{
forAllApiVersions([&, this](unsigned apiVersion) {
qry2[jss::api_version] = apiVersion;
auto jv = wsc->invoke("gateway_balances", qry2);
expect(jv[jss::status] == "error");
Expand All @@ -184,7 +181,7 @@ class GatewayBalances_test : public beast::unit_test::suite
auto const error =
apiVersion < 2u ? "invalidHotWallet" : "invalidParams";
BEAST_EXPECT(response[jss::error] == error);
}
});
}

void
Expand Down
7 changes: 2 additions & 5 deletions src/test/rpc/NoRipple_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,9 @@ class NoRipple_test : public beast::unit_test::suite
testSetAndClear();

auto withFeatsTests = [this](FeatureBitset features) {
for (auto testVersion = RPC::apiMinimumSupportedVersion;
testVersion <= RPC::apiBetaVersion;
++testVersion)
{
forAllApiVersions([&, this](unsigned testVersion) {
testDefaultRipple(features, testVersion);
}
});
testNegativeBalance(features);
testPairwise(features);
};
Expand Down
28 changes: 15 additions & 13 deletions src/test/rpc/Version_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ class Version_test : public beast::unit_test::suite
"{\"api_version\": " +
std::to_string(
std::max(
RPC::apiMaximumSupportedVersion, RPC::apiBetaVersion) +
RPC::apiMaximumSupportedVersion.value,
RPC::apiBetaVersion.value) +
1) +
"}");
BEAST_EXPECT(badVersion(re));
Expand Down Expand Up @@ -112,15 +113,15 @@ class Version_test : public beast::unit_test::suite
Json::Value j_object = Json::Value(Json::objectValue);
BEAST_EXPECT(
RPC::getAPIVersionNumber(j_object, false) == versionIfUnspecified);
j_object[jss::api_version] = RPC::apiVersionIfUnspecified;
j_object[jss::api_version] = RPC::apiVersionIfUnspecified.value;
BEAST_EXPECT(
RPC::getAPIVersionNumber(j_object, false) == versionIfUnspecified);

j_object[jss::api_version] = RPC::apiMinimumSupportedVersion;
j_object[jss::api_version] = RPC::apiMinimumSupportedVersion.value;
BEAST_EXPECT(
RPC::getAPIVersionNumber(j_object, false) ==
RPC::apiMinimumSupportedVersion);
j_object[jss::api_version] = RPC::apiMaximumSupportedVersion;
j_object[jss::api_version] = RPC::apiMaximumSupportedVersion.value;
BEAST_EXPECT(
RPC::getAPIVersionNumber(j_object, false) ==
RPC::apiMaximumSupportedVersion);
Expand All @@ -133,14 +134,14 @@ class Version_test : public beast::unit_test::suite
BEAST_EXPECT(
RPC::getAPIVersionNumber(j_object, false) ==
RPC::apiInvalidVersion);
j_object[jss::api_version] = RPC::apiBetaVersion;
j_object[jss::api_version] = RPC::apiBetaVersion.value;
BEAST_EXPECT(
RPC::getAPIVersionNumber(j_object, true) == RPC::apiBetaVersion);
j_object[jss::api_version] = RPC::apiBetaVersion + 1;
BEAST_EXPECT(
RPC::getAPIVersionNumber(j_object, true) == RPC::apiInvalidVersion);

j_object[jss::api_version] = RPC::apiInvalidVersion;
j_object[jss::api_version] = RPC::apiInvalidVersion.value;
BEAST_EXPECT(
RPC::getAPIVersionNumber(j_object, false) ==
RPC::apiInvalidVersion);
Expand Down Expand Up @@ -202,17 +203,17 @@ class Version_test : public beast::unit_test::suite
"\"id\": 5, "
"\"method\": \"version\", "
"\"params\": {}}";
auto const with_wrong_api_verion =
std::string("{ ") +
auto const with_wrong_api_verion = std::string("{ ") +
"\"jsonrpc\": \"2.0\", "
"\"ripplerpc\": \"2.0\", "
"\"id\": 6, "
"\"method\": \"version\", "
"\"params\": { "
"\"api_version\": " +
std::to_string(
std::max(RPC::apiMaximumSupportedVersion, RPC::apiBetaVersion) +
1) +
std::to_string(std::max(
RPC::apiMaximumSupportedVersion.value,
RPC::apiBetaVersion.value) +
1) +
"}}";
auto re = env.rpc(
"json2",
Expand Down Expand Up @@ -275,8 +276,9 @@ class Version_test : public beast::unit_test::suite
jrr[jss::version].isMember(jss::last))
return;
BEAST_EXPECT(
jrr[jss::version][jss::first] == RPC::apiMinimumSupportedVersion);
BEAST_EXPECT(jrr[jss::version][jss::last] == RPC::apiBetaVersion);
jrr[jss::version][jss::first] ==
RPC::apiMinimumSupportedVersion.value);
BEAST_EXPECT(jrr[jss::version][jss::last] == RPC::apiBetaVersion.value);
}

public:
Expand Down

0 comments on commit 4db09b7

Please sign in to comment.