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 <type_traits>
9#include <utility>
10
11namespace xrpl {
12
34
35namespace RPC {
36
37template <unsigned int Version>
39
40static constexpr auto kApiInvalidVersion = kApiVersion<0>;
44static constexpr auto kApiCommandLineVersion = kApiVersion<1>; // TODO Bump to 2 later
45static constexpr auto kApiBetaVersion = kApiVersion<3>;
47
49static_assert(
52static_assert(
58
59inline void
60setVersion(json::Value& parent, unsigned int apiVersion, bool betaEnabled)
61{
62 XRPL_ASSERT(apiVersion != kApiInvalidVersion, "xrpl::RPC::setVersion : input is valid");
63
64 auto& retObj = parent[jss::version] = json::ValueType::Object;
65
66 if (apiVersion == kApiVersionIfUnspecified)
67 {
68 // API version numbers used in API version 1
69 static beast::SemanticVersion const kFirstVersion{"1.0.0"};
70 static beast::SemanticVersion const kGoodVersion{"1.0.0"};
71 static beast::SemanticVersion const kLastVersion{"1.0.0"};
72
73 retObj[jss::first] = kFirstVersion.print();
74 retObj[jss::good] = kGoodVersion.print();
75 retObj[jss::last] = kLastVersion.print();
76 }
77 else
78 {
79 retObj[jss::first] = kApiMinimumSupportedVersion.value;
80 retObj[jss::last] = betaEnabled ? kApiBetaVersion : kApiMaximumSupportedVersion;
81 }
82}
83
98inline unsigned int
99getAPIVersionNumber(json::Value const& jv, bool betaEnabled)
100{
101 static json::Value const kMinVersion(RPC::kApiMinimumSupportedVersion);
102 json::Value const maxVersion(
104
105 if (!jv.isObject() || !jv.isMember(jss::api_version))
107
108 try
109 {
110 auto const& rawVersion = jv[jss::api_version];
111 switch (rawVersion.type())
112 {
114 if (rawVersion.asInt() < 0)
116 [[fallthrough]];
118 auto const apiVersion = rawVersion.asUInt();
119 if (apiVersion < kMinVersion || apiVersion > maxVersion)
121 return apiVersion;
122 }
123 default:
125 }
126 }
127 catch (...)
128 {
130 }
131}
132
133} // namespace RPC
134
135template <unsigned MinVer, unsigned MaxVer, typename Fn, typename... Args>
136void
137forApiVersions(Fn const& fn, Args&&... args)
138 requires //
139 (MaxVer >= MinVer) && //
140 (MinVer >= RPC::kApiMinimumSupportedVersion) && //
141 (RPC::kApiMaximumValidVersion >= MaxVer) && requires {
144 }
145{
146 static constexpr auto kSize = MaxVer + 1 - MinVer;
147 [&]<std::size_t... Offset>(std::index_sequence<Offset...>) {
148 // NOLINTBEGIN(bugprone-use-after-move)
149 (((void)fn(
151 ...);
152 // NOLINTEND(bugprone-use-after-move)
153 }(std::make_index_sequence<kSize>{});
154}
155
156template <typename Fn, typename... Args>
157void
167
168} // 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:130
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:21
@ Int
signed integer value
Definition json_value.h:20
@ Object
object value (collection of name/value pairs).
Definition json_value.h:26
API version numbers used in later API versions.
Definition ApiVersion.h:35
static constexpr auto kApiInvalidVersion
Definition ApiVersion.h:40
void setVersion(json::Value &parent, unsigned int apiVersion, bool betaEnabled)
Definition ApiVersion.h:60
static constexpr auto kApiMinimumSupportedVersion
Definition ApiVersion.h:41
static constexpr std::integral_constant< unsigned, Version > kApiVersion
Definition ApiVersion.h:38
static constexpr auto kApiCommandLineVersion
Definition ApiVersion.h:44
static constexpr auto kApiMaximumValidVersion
Definition ApiVersion.h:46
static constexpr auto kApiVersionIfUnspecified
Definition ApiVersion.h:43
static constexpr auto kApiBetaVersion
Definition ApiVersion.h:45
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 kApiMaximumSupportedVersion
Definition ApiVersion.h:42
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
void forApiVersions(Fn const &fn, Args &&... args)
Definition ApiVersion.h:137
void forAllApiVersions(Fn const &fn, Args &&... args)
Definition ApiVersion.h:158