xrpld
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 <cstddef>
9#include <type_traits>
10#include <utility>
11
12namespace xrpl {
13
35
36namespace rpc {
37
38template <unsigned int Version>
40
41static constexpr auto kApiInvalidVersion = kApiVersion<0>;
45static constexpr auto kApiCommandLineVersion = kApiVersion<1>; // TODO Bump to 2 later
46static constexpr auto kApiBetaVersion = kApiVersion<3>;
48
50static_assert(
53static_assert(
59
60inline void
61setVersion(json::Value& parent, unsigned int apiVersion, bool betaEnabled)
62{
63 XRPL_ASSERT(apiVersion != kApiInvalidVersion, "xrpl::rpc::setVersion : input is valid");
64
65 auto& retObj = parent[jss::version] = json::ValueType::Object;
66
67 if (apiVersion == kApiVersionIfUnspecified)
68 {
69 // API version numbers used in API version 1
70 static beast::SemanticVersion const kFirstVersion{"1.0.0"};
71 static beast::SemanticVersion const kGoodVersion{"1.0.0"};
72 static beast::SemanticVersion const kLastVersion{"1.0.0"};
73
74 retObj[jss::first] = kFirstVersion.print();
75 retObj[jss::good] = kGoodVersion.print();
76 retObj[jss::last] = kLastVersion.print();
77 }
78 else
79 {
80 retObj[jss::first] = kApiMinimumSupportedVersion.value;
81 retObj[jss::last] = betaEnabled ? kApiBetaVersion : kApiMaximumSupportedVersion;
82 }
83}
84
99inline unsigned int
100getAPIVersionNumber(json::Value const& jv, bool betaEnabled)
101{
102 static json::Value const kMinVersion(rpc::kApiMinimumSupportedVersion);
103 json::Value const maxVersion(
105
106 if (!jv.isObject() || !jv.isMember(jss::api_version))
108
109 try
110 {
111 auto const& rawVersion = jv[jss::api_version];
112 switch (rawVersion.type())
113 {
115 if (rawVersion.asInt() < 0)
117 [[fallthrough]];
119 auto const apiVersion = rawVersion.asUInt();
120 if (apiVersion < kMinVersion || apiVersion > maxVersion)
122 return apiVersion;
123 }
124 default:
126 }
127 }
128 catch (...)
129 {
131 }
132}
133
134} // namespace rpc
135
136template <unsigned MinVer, unsigned MaxVer, typename Fn, typename... Args>
137void
138forApiVersions(Fn const& fn, Args&&... args)
139 requires //
140 (MaxVer >= MinVer) && //
141 (MinVer >= rpc::kApiMinimumSupportedVersion) && //
142 (rpc::kApiMaximumValidVersion >= MaxVer) && requires {
145 }
146{
147 static constexpr auto kSize = MaxVer + 1 - MinVer;
148 [&]<std::size_t... Offset>(std::index_sequence<Offset...>) {
149 // NOLINTBEGIN(bugprone-use-after-move)
150 (((void)fn(
152 ...);
153 // NOLINTEND(bugprone-use-after-move)
154 }(std::make_index_sequence<kSize>{});
155}
156
157template <typename Fn, typename... Args>
158void
168
169} // namespace xrpl
A Semantic Version number.
std::string print() const
Produce a string from semantic version components.
Represents a JSON value.
Definition json_value.h:117
bool isObject() const
bool isMember(char const *key) const
Return true if the object has a member named key.
T forward(T... args)
@ UInt
unsigned integer value
Definition json_value.h:24
@ Int
signed integer value
Definition json_value.h:23
@ Object
object value (collection of name/value pairs).
Definition json_value.h:29
API version numbers used in later API versions.
Definition ApiVersion.h:36
static constexpr auto kApiMaximumSupportedVersion
Definition ApiVersion.h:43
void setVersion(json::Value &parent, unsigned int apiVersion, bool betaEnabled)
Definition ApiVersion.h:61
static constexpr std::integral_constant< unsigned, Version > kApiVersion
Definition ApiVersion.h:39
static constexpr auto kApiMaximumValidVersion
Definition ApiVersion.h:47
static constexpr auto kApiVersionIfUnspecified
Definition ApiVersion.h:44
static constexpr auto kApiMinimumSupportedVersion
Definition ApiVersion.h:42
unsigned int getAPIVersionNumber(json::Value const &jv, bool betaEnabled)
Retrieve the api version number from the json value.
Definition ApiVersion.h:100
static constexpr auto kApiBetaVersion
Definition ApiVersion.h:46
static constexpr auto kApiCommandLineVersion
Definition ApiVersion.h:45
static constexpr auto kApiInvalidVersion
Definition ApiVersion.h:41
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:159
void forApiVersions(Fn const &fn, Args &&... args)
Definition ApiVersion.h:138