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(
52static_assert(
58
59inline void
60setVersion(Json::Value& parent, unsigned int apiVersion, bool betaEnabled)
61{
62 XRPL_ASSERT(apiVersion != apiInvalidVersion, "xrpl::RPC::setVersion : input is valid");
63
64 auto& retObj = parent[jss::version] = Json::objectValue;
65
67 {
68 // API version numbers used in API version 1
69 static beast::SemanticVersion const firstVersion{"1.0.0"};
70 static beast::SemanticVersion const goodVersion{"1.0.0"};
71 static beast::SemanticVersion const lastVersion{"1.0.0"};
72
73 retObj[jss::first] = firstVersion.print();
74 retObj[jss::good] = goodVersion.print();
75 retObj[jss::last] = lastVersion.print();
76 }
77 else
78 {
79 retObj[jss::first] = apiMinimumSupportedVersion.value;
80 retObj[jss::last] = betaEnabled ? apiBetaVersion : apiMaximumSupportedVersion;
81 }
82}
83
98inline unsigned int
99getAPIVersionNumber(Json::Value const& jv, bool betaEnabled)
100{
101 static Json::Value const minVersion(RPC::apiMinimumSupportedVersion);
102 Json::Value const maxVersion(
104
105 if (jv.isObject())
106 {
107 if (jv.isMember(jss::api_version))
108 {
109 auto const specifiedVersion = jv[jss::api_version];
110 if (!specifiedVersion.isInt() && !specifiedVersion.isUInt())
111 {
113 }
114 auto const specifiedVersionInt = specifiedVersion.asInt();
115 if (specifiedVersionInt < minVersion || specifiedVersionInt > maxVersion)
116 {
118 }
119 return specifiedVersionInt;
120 }
121 }
122
124}
125
126} // namespace RPC
127
128template <unsigned minVer, unsigned maxVer, typename Fn, typename... Args>
129void
130forApiVersions(Fn const& fn, Args&&... args)
131 requires //
132 (maxVer >= minVer) && //
133 (minVer >= RPC::apiMinimumSupportedVersion) && //
134 (RPC::apiMaximumValidVersion >= maxVer) && requires {
137 }
138{
139 constexpr auto size = maxVer + 1 - minVer;
140 [&]<std::size_t... offset>(std::index_sequence<offset...>) {
141 (((void)fn(
143 ...);
145}
146
147template <typename Fn, typename... Args>
148void
149forAllApiVersions(Fn const& fn, Args&&... args)
150 requires requires {
151 forApiVersions<RPC::apiMinimumSupportedVersion, RPC::apiMaximumValidVersion>(
152 fn, std::forward<Args>(args)...);
153 }
154{
155 forApiVersions<RPC::apiMinimumSupportedVersion, RPC::apiMaximumValidVersion>(
156 fn, std::forward<Args>(args)...);
157}
158
159} // 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:60
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:99
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:149
void forApiVersions(Fn const &fn, Args &&... args)
Definition ApiVersion.h:130