xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
protocol
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
12
namespace
xrpl
{
13
35
36
namespace
rpc
{
37
38
template
<
unsigned
int
Version>
39
static
constexpr
std::integral_constant<unsigned, Version>
kApiVersion
= {};
40
41
static
constexpr
auto
kApiInvalidVersion
=
kApiVersion<0>
;
42
static
constexpr
auto
kApiMinimumSupportedVersion
=
kApiVersion<1>
;
43
static
constexpr
auto
kApiMaximumSupportedVersion
=
kApiVersion<2>
;
44
static
constexpr
auto
kApiVersionIfUnspecified
=
kApiVersion<1>
;
45
static
constexpr
auto
kApiCommandLineVersion
=
kApiVersion<1>
;
// TODO Bump to 2 later
46
static
constexpr
auto
kApiBetaVersion
=
kApiVersion<3>
;
47
static
constexpr
auto
kApiMaximumValidVersion
=
kApiBetaVersion
;
48
49
static_assert
(
kApiInvalidVersion
<
kApiMinimumSupportedVersion
);
50
static_assert
(
51
kApiVersionIfUnspecified
>=
kApiMinimumSupportedVersion
&&
52
kApiVersionIfUnspecified
<=
kApiMaximumSupportedVersion
);
53
static_assert
(
54
kApiCommandLineVersion
>=
kApiMinimumSupportedVersion
&&
55
kApiCommandLineVersion
<=
kApiMaximumSupportedVersion
);
56
static_assert
(
kApiMaximumSupportedVersion
>=
kApiMinimumSupportedVersion
);
57
static_assert
(
kApiBetaVersion
>=
kApiMaximumSupportedVersion
);
58
static_assert
(
kApiMaximumValidVersion
>=
kApiMaximumSupportedVersion
);
59
60
inline
void
61
setVersion
(
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
99
inline
unsigned
int
100
getAPIVersionNumber
(
json::Value
const
& jv,
bool
betaEnabled)
101
{
102
static
json::Value
const
kMinVersion(
rpc::kApiMinimumSupportedVersion
);
103
json::Value
const
maxVersion(
104
betaEnabled ?
rpc::kApiBetaVersion
:
rpc::kApiMaximumSupportedVersion
);
105
106
if
(!jv.
isObject
() || !jv.
isMember
(jss::api_version))
107
return
rpc::kApiVersionIfUnspecified
;
108
109
try
110
{
111
auto
const
& rawVersion = jv[jss::api_version];
112
switch
(rawVersion.type())
113
{
114
case
json::ValueType::Int
:
115
if
(rawVersion.asInt() < 0)
116
return
rpc::kApiInvalidVersion
;
117
[[fallthrough]];
118
case
json::ValueType::UInt
: {
119
auto
const
apiVersion = rawVersion.asUInt();
120
if
(apiVersion < kMinVersion || apiVersion > maxVersion)
121
return
rpc::kApiInvalidVersion
;
122
return
apiVersion;
123
}
124
default
:
125
return
rpc::kApiInvalidVersion
;
126
}
127
}
128
catch
(...)
129
{
130
return
rpc::kApiInvalidVersion
;
131
}
132
}
133
134
}
// namespace rpc
135
136
template
<
unsigned
MinVer,
unsigned
MaxVer,
typename
Fn,
typename
... Args>
137
void
138
forApiVersions
(Fn
const
& fn, Args&&... args)
139
requires
//
140
(MaxVer >= MinVer) &&
//
141
(MinVer >=
rpc::kApiMinimumSupportedVersion
) &&
//
142
(
rpc::kApiMaximumValidVersion
>= MaxVer) &&
requires
{
143
fn(
std::integral_constant<unsigned int, MinVer>
{},
std::forward<Args>
(args)...);
144
fn(
std::integral_constant<unsigned int, MaxVer>
{},
std::forward<Args>
(args)...);
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(
151
std::integral_constant<unsigned int, MinVer + Offset>
{},
std::forward<Args>
(args)...)),
152
...);
153
// NOLINTEND(bugprone-use-after-move)
154
}(std::make_index_sequence<kSize>{});
155
}
156
157
template
<
typename
Fn,
typename
... Args>
158
void
159
forAllApiVersions
(Fn
const
& fn, Args&&... args)
160
requires
requires
{
161
forApiVersions<rpc::kApiMinimumSupportedVersion, rpc::kApiMaximumValidVersion>
(
162
fn,
std::forward<Args>
(args)...);
163
}
164
{
165
forApiVersions<rpc::kApiMinimumSupportedVersion, rpc::kApiMaximumValidVersion>
(
166
fn,
std::forward<Args>
(args)...);
167
}
168
169
}
// namespace xrpl
beast::SemanticVersion
A Semantic Version number.
Definition
SemanticVersion.h:18
beast::SemanticVersion::print
std::string print() const
Produce a string from semantic version components.
Definition
libxrpl/beast/core/SemanticVersion.cpp:213
json::Value
Represents a JSON value.
Definition
json_value.h:117
json::Value::isObject
bool isObject() const
Definition
json_value.cpp:1152
json::Value::isMember
bool isMember(char const *key) const
Return true if the object has a member named key.
Definition
json_value.cpp:1049
cstddef
std::forward
T forward(T... args)
std::index_sequence
std::integral_constant
json::ValueType::UInt
@ UInt
unsigned integer value
Definition
json_value.h:24
json::ValueType::Int
@ Int
signed integer value
Definition
json_value.h:23
json::ValueType::Object
@ Object
object value (collection of name/value pairs).
Definition
json_value.h:29
xrpl::rpc
API version numbers used in later API versions.
Definition
ApiVersion.h:36
xrpl::rpc::kApiMaximumSupportedVersion
static constexpr auto kApiMaximumSupportedVersion
Definition
ApiVersion.h:43
xrpl::rpc::setVersion
void setVersion(json::Value &parent, unsigned int apiVersion, bool betaEnabled)
Definition
ApiVersion.h:61
xrpl::rpc::kApiVersion
static constexpr std::integral_constant< unsigned, Version > kApiVersion
Definition
ApiVersion.h:39
xrpl::rpc::kApiMaximumValidVersion
static constexpr auto kApiMaximumValidVersion
Definition
ApiVersion.h:47
xrpl::rpc::kApiVersionIfUnspecified
static constexpr auto kApiVersionIfUnspecified
Definition
ApiVersion.h:44
xrpl::rpc::kApiMinimumSupportedVersion
static constexpr auto kApiMinimumSupportedVersion
Definition
ApiVersion.h:42
xrpl::rpc::getAPIVersionNumber
unsigned int getAPIVersionNumber(json::Value const &jv, bool betaEnabled)
Retrieve the api version number from the json value.
Definition
ApiVersion.h:100
xrpl::rpc::kApiBetaVersion
static constexpr auto kApiBetaVersion
Definition
ApiVersion.h:46
xrpl::rpc::kApiCommandLineVersion
static constexpr auto kApiCommandLineVersion
Definition
ApiVersion.h:45
xrpl::rpc::kApiInvalidVersion
static constexpr auto kApiInvalidVersion
Definition
ApiVersion.h:41
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::forAllApiVersions
void forAllApiVersions(Fn const &fn, Args &&... args)
Definition
ApiVersion.h:159
xrpl::forApiVersions
void forApiVersions(Fn const &fn, Args &&... args)
Definition
ApiVersion.h:138
std::size_t
type_traits
utility
Generated by
1.17.0