rippled
Loading...
Searching...
No Matches
ApiVersion.h
1#pragma once
2
3#include <xrpl/beast/core/SemanticVersion.h>
4#include <xrpl/beast/utility/instrumentation.h>
5#include <xrpl/json/json_value.h>
6#include <xrpl/protocol/jss.h>
7
8#include <type_traits>
9#include <utility>
10
11namespace xrpl {
12
35namespace RPC {
36
37template <unsigned int Version>
39
40constexpr static auto apiInvalidVersion = apiVersion<0>;
41constexpr static auto apiMinimumSupportedVersion = apiVersion<1>;
42constexpr static auto apiMaximumSupportedVersion = apiVersion<2>;
43constexpr static auto apiVersionIfUnspecified = apiVersion<1>;
44constexpr static auto apiCommandLineVersion = apiVersion<1>; // TODO Bump to 2 later
45constexpr static auto apiBetaVersion = apiVersion<3>;
46constexpr static auto apiMaximumValidVersion = apiBetaVersion;
47
49static_assert(
51static_assert(
56
57inline void
58setVersion(Json::Value& parent, unsigned int apiVersion, bool betaEnabled)
59{
60 XRPL_ASSERT(apiVersion != apiInvalidVersion, "xrpl::RPC::setVersion : input is valid");
61
62 auto& retObj = parent[jss::version] = Json::objectValue;
63
65 {
66 // API version numbers used in API version 1
67 static beast::SemanticVersion const firstVersion{"1.0.0"};
68 static beast::SemanticVersion const goodVersion{"1.0.0"};
69 static beast::SemanticVersion const lastVersion{"1.0.0"};
70
71 retObj[jss::first] = firstVersion.print();
72 retObj[jss::good] = goodVersion.print();
73 retObj[jss::last] = lastVersion.print();
74 }
75 else
76 {
77 retObj[jss::first] = apiMinimumSupportedVersion.value;
78 retObj[jss::last] = betaEnabled ? apiBetaVersion : apiMaximumSupportedVersion;
79 }
80}
81
96inline unsigned int
97getAPIVersionNumber(Json::Value const& jv, bool betaEnabled)
98{
99 static Json::Value const minVersion(RPC::apiMinimumSupportedVersion);
100 Json::Value const maxVersion(betaEnabled ? RPC::apiBetaVersion : RPC::apiMaximumSupportedVersion);
101
102 if (jv.isObject())
103 {
104 if (jv.isMember(jss::api_version))
105 {
106 auto const specifiedVersion = jv[jss::api_version];
107 if (!specifiedVersion.isInt() && !specifiedVersion.isUInt())
108 {
110 }
111 auto const specifiedVersionInt = specifiedVersion.asInt();
112 if (specifiedVersionInt < minVersion || specifiedVersionInt > maxVersion)
113 {
115 }
116 return specifiedVersionInt;
117 }
118 }
119
121}
122
123} // namespace RPC
124
125template <unsigned minVer, unsigned maxVer, typename Fn, typename... Args>
126void
127forApiVersions(Fn const& fn, Args&&... args)
128 requires //
129 (maxVer >= minVer) && //
130 (minVer >= RPC::apiMinimumSupportedVersion) && //
131 (RPC::apiMaximumValidVersion >= maxVer) && requires {
134 }
135{
136 constexpr auto size = maxVer + 1 - minVer;
137 [&]<std::size_t... offset>(std::index_sequence<offset...>) {
140}
141
142template <typename Fn, typename... Args>
143void
144forAllApiVersions(Fn const& fn, Args&&... args)
145 requires requires {
146 forApiVersions<RPC::apiMinimumSupportedVersion, RPC::apiMaximumValidVersion>(fn, std::forward<Args>(args)...);
147 }
148{
149 forApiVersions<RPC::apiMinimumSupportedVersion, RPC::apiMaximumValidVersion>(fn, std::forward<Args>(args)...);
150}
151
152} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
Int asInt() const
bool isObject() const
bool isMember(char const *key) const
Return true if the object has a member named key.
A Semantic Version number.
std::string print() const
Produce a string from semantic version components.
T is_same_v
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:26
static constexpr auto apiBetaVersion
Definition ApiVersion.h:45
static constexpr auto apiInvalidVersion
Definition ApiVersion.h:40
static constexpr auto apiMinimumSupportedVersion
Definition ApiVersion.h:41
static constexpr auto apiMaximumValidVersion
Definition ApiVersion.h:46
static constexpr std::integral_constant< unsigned, Version > apiVersion
Definition ApiVersion.h:38
static constexpr auto apiMaximumSupportedVersion
Definition ApiVersion.h:42
void setVersion(Json::Value &parent, unsigned int apiVersion, bool betaEnabled)
Definition ApiVersion.h:58
static constexpr auto apiCommandLineVersion
Definition ApiVersion.h:44
unsigned int getAPIVersionNumber(Json::Value const &jv, bool betaEnabled)
Retrieve the api version number from the json value.
Definition ApiVersion.h:97
static constexpr auto apiVersionIfUnspecified
Definition ApiVersion.h:43
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
void forAllApiVersions(Fn const &fn, Args &&... args)
Definition ApiVersion.h:144
void forApiVersions(Fn const &fn, Args &&... args)
Definition ApiVersion.h:127