rippled
Loading...
Searching...
No Matches
ApiVersion.h
1#ifndef XRPL_PROTOCOL_APIVERSION_H_INCLUDED
2#define XRPL_PROTOCOL_APIVERSION_H_INCLUDED
3
4#include <xrpl/beast/core/SemanticVersion.h>
5#include <xrpl/beast/utility/instrumentation.h>
6#include <xrpl/json/json_value.h>
7#include <xrpl/protocol/jss.h>
8
9#include <type_traits>
10#include <utility>
11
12namespace ripple {
13
36namespace RPC {
37
38template <unsigned int Version>
40
41constexpr static auto apiInvalidVersion = apiVersion<0>;
42constexpr static auto apiMinimumSupportedVersion = apiVersion<1>;
43constexpr static auto apiMaximumSupportedVersion = apiVersion<2>;
44constexpr static auto apiVersionIfUnspecified = apiVersion<1>;
45constexpr static auto apiCommandLineVersion =
46 apiVersion<1>; // TODO Bump to 2 later
47constexpr static auto apiBetaVersion = apiVersion<3>;
48constexpr static auto apiMaximumValidVersion = apiBetaVersion;
49
51static_assert(
54static_assert(
60
61template <class JsonObject>
62void
63setVersion(JsonObject& parent, unsigned int apiVersion, bool betaEnabled)
64{
65 XRPL_ASSERT(
67 "ripple::RPC::setVersion : input is valid");
68 auto& retObj = addObject(parent, jss::version);
69
71 {
72 // API version numbers used in API version 1
73 static beast::SemanticVersion const firstVersion{"1.0.0"};
74 static beast::SemanticVersion const goodVersion{"1.0.0"};
75 static beast::SemanticVersion const lastVersion{"1.0.0"};
76
77 retObj[jss::first] = firstVersion.print();
78 retObj[jss::good] = goodVersion.print();
79 retObj[jss::last] = lastVersion.print();
80 }
81 else
82 {
83 retObj[jss::first] = apiMinimumSupportedVersion.value;
84 retObj[jss::last] =
86 }
87}
88
103inline unsigned int
104getAPIVersionNumber(Json::Value const& jv, bool betaEnabled)
105{
106 static Json::Value const minVersion(RPC::apiMinimumSupportedVersion);
107 Json::Value const maxVersion(
109
110 if (jv.isObject())
111 {
112 if (jv.isMember(jss::api_version))
113 {
114 auto const specifiedVersion = jv[jss::api_version];
115 if (!specifiedVersion.isInt() && !specifiedVersion.isUInt())
116 {
118 }
119 auto const specifiedVersionInt = specifiedVersion.asInt();
120 if (specifiedVersionInt < minVersion ||
121 specifiedVersionInt > maxVersion)
122 {
124 }
125 return specifiedVersionInt;
126 }
127 }
128
130}
131
132} // namespace RPC
133
134template <unsigned minVer, unsigned maxVer, typename Fn, typename... Args>
135void
136forApiVersions(Fn const& fn, Args&&... args)
137 requires //
138 (maxVer >= minVer) && //
139 (minVer >= RPC::apiMinimumSupportedVersion) && //
140 (RPC::apiMaximumValidVersion >= maxVer) && requires {
142 std::forward<Args>(args)...);
144 std::forward<Args>(args)...);
145 }
146{
147 constexpr auto size = maxVer + 1 - minVer;
148 [&]<std::size_t... offset>(std::index_sequence<offset...>) {
149 (((void)fn(
151 std::forward<Args>(args)...)),
152 ...);
154}
155
156template <typename Fn, typename... Args>
157void
158forAllApiVersions(Fn const& fn, Args&&... args)
159 requires requires {
163 }
164{
168}
169
170} // namespace ripple
171
172#endif
Represents a JSON value.
Definition json_value.h:131
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
void setVersion(JsonObject &parent, unsigned int apiVersion, bool betaEnabled)
Definition ApiVersion.h:63
static constexpr auto apiMaximumSupportedVersion
Definition ApiVersion.h:43
static constexpr std::integral_constant< unsigned, Version > apiVersion
Definition ApiVersion.h:39
static constexpr auto apiMaximumValidVersion
Definition ApiVersion.h:48
static constexpr auto apiCommandLineVersion
Definition ApiVersion.h:45
static constexpr auto apiBetaVersion
Definition ApiVersion.h:47
static constexpr auto apiVersionIfUnspecified
Definition ApiVersion.h:44
static constexpr auto apiInvalidVersion
Definition ApiVersion.h:41
unsigned int getAPIVersionNumber(Json::Value const &jv, bool betaEnabled)
Retrieve the api version number from the json value.
Definition ApiVersion.h:104
static constexpr auto apiMinimumSupportedVersion
Definition ApiVersion.h:42
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
void forAllApiVersions(Fn const &fn, Args &&... args)
Definition ApiVersion.h:158
void forApiVersions(Fn const &fn, Args &&... args)
Definition ApiVersion.h:136