rippled
Loading...
Searching...
No Matches
ApiVersion.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2023 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_PROTOCOL_APIVERSION_H_INCLUDED
21#define RIPPLE_PROTOCOL_APIVERSION_H_INCLUDED
22
23#include <xrpl/beast/core/SemanticVersion.h>
24#include <xrpl/beast/utility/instrumentation.h>
25#include <xrpl/json/json_value.h>
26#include <xrpl/protocol/jss.h>
27
28#include <type_traits>
29#include <utility>
30
31namespace ripple {
32
55namespace RPC {
56
57template <unsigned int Version>
59
60constexpr static auto apiInvalidVersion = apiVersion<0>;
61constexpr static auto apiMinimumSupportedVersion = apiVersion<1>;
62constexpr static auto apiMaximumSupportedVersion = apiVersion<2>;
63constexpr static auto apiVersionIfUnspecified = apiVersion<1>;
64constexpr static auto apiCommandLineVersion =
65 apiVersion<1>; // TODO Bump to 2 later
66constexpr static auto apiBetaVersion = apiVersion<3>;
67constexpr static auto apiMaximumValidVersion = apiBetaVersion;
68
70static_assert(
73static_assert(
79
80template <class JsonObject>
81void
82setVersion(JsonObject& parent, unsigned int apiVersion, bool betaEnabled)
83{
84 XRPL_ASSERT(
86 "ripple::RPC::setVersion : input is valid");
87 auto& retObj = addObject(parent, jss::version);
88
90 {
91 // API version numbers used in API version 1
92 static beast::SemanticVersion const firstVersion{"1.0.0"};
93 static beast::SemanticVersion const goodVersion{"1.0.0"};
94 static beast::SemanticVersion const lastVersion{"1.0.0"};
95
96 retObj[jss::first] = firstVersion.print();
97 retObj[jss::good] = goodVersion.print();
98 retObj[jss::last] = lastVersion.print();
99 }
100 else
101 {
102 retObj[jss::first] = apiMinimumSupportedVersion.value;
103 retObj[jss::last] =
105 }
106}
107
122inline unsigned int
123getAPIVersionNumber(Json::Value const& jv, bool betaEnabled)
124{
125 static Json::Value const minVersion(RPC::apiMinimumSupportedVersion);
126 Json::Value const maxVersion(
128
129 if (jv.isObject())
130 {
131 if (jv.isMember(jss::api_version))
132 {
133 auto const specifiedVersion = jv[jss::api_version];
134 if (!specifiedVersion.isInt() && !specifiedVersion.isUInt())
135 {
137 }
138 auto const specifiedVersionInt = specifiedVersion.asInt();
139 if (specifiedVersionInt < minVersion ||
140 specifiedVersionInt > maxVersion)
141 {
143 }
144 return specifiedVersionInt;
145 }
146 }
147
149}
150
151} // namespace RPC
152
153template <unsigned minVer, unsigned maxVer, typename Fn, typename... Args>
154void
155forApiVersions(Fn const& fn, Args&&... args)
156 requires //
157 (maxVer >= minVer) && //
158 (minVer >= RPC::apiMinimumSupportedVersion) && //
159 (RPC::apiMaximumValidVersion >= maxVer) && requires {
161 std::forward<Args>(args)...);
163 std::forward<Args>(args)...);
164 }
165{
166 constexpr auto size = maxVer + 1 - minVer;
167 [&]<std::size_t... offset>(std::index_sequence<offset...>) {
168 (((void)fn(
170 std::forward<Args>(args)...)),
171 ...);
173}
174
175template <typename Fn, typename... Args>
176void
177forAllApiVersions(Fn const& fn, Args&&... args)
178 requires requires {
182 }
183{
187}
188
189} // namespace ripple
190
191#endif
Represents a JSON value.
Definition json_value.h:149
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:82
static constexpr auto apiMaximumSupportedVersion
Definition ApiVersion.h:62
static constexpr std::integral_constant< unsigned, Version > apiVersion
Definition ApiVersion.h:58
static constexpr auto apiMaximumValidVersion
Definition ApiVersion.h:67
static constexpr auto apiCommandLineVersion
Definition ApiVersion.h:64
static constexpr auto apiBetaVersion
Definition ApiVersion.h:66
static constexpr auto apiVersionIfUnspecified
Definition ApiVersion.h:63
static constexpr auto apiInvalidVersion
Definition ApiVersion.h:60
unsigned int getAPIVersionNumber(Json::Value const &jv, bool betaEnabled)
Retrieve the api version number from the json value.
Definition ApiVersion.h:123
static constexpr auto apiMinimumSupportedVersion
Definition ApiVersion.h:61
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
void forAllApiVersions(Fn const &fn, Args &&... args)
Definition ApiVersion.h:177
void forApiVersions(Fn const &fn, Args &&... args)
Definition ApiVersion.h:155