xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
protocol
BuildInfo.cpp
1
#include <xrpl/protocol/BuildInfo.h>
2
3
#include <xrpl/basics/contract.h>
4
#include <xrpl/beast/core/LexicalCast.h>
5
#include <xrpl/beast/core/SemanticVersion.h>
6
#include <xrpl/git/Git.h>
// IWYU pragma: keep
7
#include <xrpl/protocol/SystemParameters.h>
8
9
#include <boost/preprocessor/stringize.hpp>
// IWYU pragma: keep
10
11
#include <
algorithm
>
12
#include <
cstdint
>
13
#include <
string
>
14
#include <
string_view
>
15
16
#ifndef XRPLD_VERSION
17
#error "XRPLD_VERSION must be defined"
18
#endif
19
20
namespace
xrpl::build_info
{
21
22
namespace
{
23
24
// Set by cmake/XrplVersion.cmake, see http://semver.org/
25
constexpr
char
kVersionString[] = XRPLD_VERSION;
26
27
std::string
28
buildVersionString()
29
{
30
std::string version = kVersionString;
31
32
#if defined(DEBUG) || defined(SANITIZERS)
33
// A version derived from git already names its commit in the metadata.
34
bool
const
hasMetadata = version.contains(
'+'
);
35
std::string metadata;
36
37
std::string
const
& commitHash =
xrpl::git::getCommitHash
();
38
if
(!hasMetadata && !commitHash.
empty
())
39
metadata += commitHash +
"."
;
40
41
#ifdef DEBUG
42
metadata +=
"DEBUG"
;
43
#endif
44
45
#if defined(DEBUG) && defined(SANITIZERS)
46
metadata +=
"."
;
47
#endif
48
49
#ifdef SANITIZERS
50
metadata += BOOST_PP_STRINGIZE(SANITIZERS);
// cspell: disable-line
51
#endif
52
53
if
(!metadata.
empty
())
54
version += (hasMetadata ?
"."
:
"+"
) + metadata;
55
#endif
56
57
return
version;
58
}
59
60
}
// namespace
61
62
std::string
const
&
63
getVersionString
()
64
{
65
static
std::string
const
kValue = [] {
66
std::string
const
s = buildVersionString();
67
68
beast::SemanticVersion
v;
69
if
(!v.
parse
(s) || v.
print
() != s)
70
logicError
(s +
": Bad server version string"
);
71
return
s;
72
}();
73
return
kValue;
74
}
75
76
std::string
const
&
77
getFullVersionString
()
78
{
79
static
std::string
const
kValue =
systemName
() +
"-"
+
getVersionString
();
80
return
kValue;
81
}
82
83
static
constexpr
std::uint64_t
kImplementationVersionIdentifier
= 0x183B'0000'0000'0000LLU;
84
static
constexpr
std::uint64_t
kImplementationVersionIdentifierMask
= 0xFFFF'0000'0000'0000LLU;
85
86
std::uint64_t
87
encodeSoftwareVersion
(
std::string_view
versionStr)
88
{
89
std::uint64_t
c =
kImplementationVersionIdentifier
;
90
91
beast::SemanticVersion
v;
92
93
if
(v.
parse
(versionStr))
94
{
95
if
(v.
majorVersion
>= 0 && v.
majorVersion
<= 255)
96
c |=
static_cast<
std::uint64_t
>
(v.
majorVersion
) << 40;
97
98
if
(v.
minorVersion
>= 0 && v.
minorVersion
<= 255)
99
c |=
static_cast<
std::uint64_t
>
(v.
minorVersion
) << 32;
100
101
if
(v.
patchVersion
>= 0 && v.
patchVersion
<= 255)
102
c |=
static_cast<
std::uint64_t
>
(v.
patchVersion
) << 24;
103
104
if
(!v.
isPreRelease
())
105
c |=
static_cast<
std::uint64_t
>
(0xC00000);
106
107
if
(v.
isPreRelease
())
108
{
109
std::uint8_t
x = 0;
110
111
for
(
auto
const
&
id
: v.
preReleaseIdentifiers
)
112
{
113
auto
parsePreRelease = [](
std::string_view
identifier,
114
std::string_view
prefix,
115
std::uint8_t
key,
116
std::uint8_t
lok,
117
std::uint8_t
hik) ->
std::uint8_t
{
118
std::uint8_t
ret = 0;
119
120
if
(!identifier.
starts_with
(prefix))
121
return
0;
122
123
if
(!
beast::lexicalCastChecked
(
124
ret,
std::string
(identifier.
substr
(prefix.length()))))
125
return
0;
126
127
if
(
std::clamp
(ret, lok, hik) != ret)
128
return
0;
129
130
return
ret + key;
131
};
132
133
x = parsePreRelease(
id
,
"rc"
, 0x80, 0, 63);
134
135
if
(x == 0)
136
x = parsePreRelease(
id
,
"b"
, 0x40, 0, 63);
137
138
if
((x & 0xC0) != 0)
139
{
140
c |=
static_cast<
std::uint64_t
>
(x) << 16;
141
break
;
142
}
143
}
144
}
145
}
146
147
return
c;
148
}
149
150
std::uint64_t
151
getEncodedVersion
()
152
{
153
static
std::uint64_t
const
kCookie = {
encodeSoftwareVersion
(
getVersionString
())};
154
return
kCookie;
155
}
156
157
bool
158
isXrpldVersion
(
std::uint64_t
version)
159
{
160
return
(version &
kImplementationVersionIdentifierMask
) ==
kImplementationVersionIdentifier
;
161
}
162
163
bool
164
isNewerVersion
(
std::uint64_t
version)
165
{
166
if
(
isXrpldVersion
(version))
167
return
version >
getEncodedVersion
();
168
return
false
;
169
}
170
171
}
// namespace xrpl::build_info
algorithm
std::string
std::string_view
std::clamp
T clamp(T... args)
beast::SemanticVersion
A Semantic Version number.
Definition
SemanticVersion.h:18
beast::SemanticVersion::preReleaseIdentifiers
IdentifierList preReleaseIdentifiers
Definition
SemanticVersion.h:26
beast::SemanticVersion::majorVersion
int majorVersion
Definition
SemanticVersion.h:22
beast::SemanticVersion::minorVersion
int minorVersion
Definition
SemanticVersion.h:23
beast::SemanticVersion::patchVersion
int patchVersion
Definition
SemanticVersion.h:24
beast::SemanticVersion::isPreRelease
bool isPreRelease() const noexcept
Definition
SemanticVersion.h:53
beast::SemanticVersion::parse
bool parse(std::string_view input)
Parse a semantic version string.
Definition
libxrpl/beast/core/SemanticVersion.cpp:150
beast::SemanticVersion::print
std::string print() const
Produce a string from semantic version components.
Definition
libxrpl/beast/core/SemanticVersion.cpp:213
cstdint
std::string::empty
T empty(T... args)
std::uint64_t
beast::lexicalCastChecked
constexpr bool lexicalCastChecked(Out &out, In in)
Intelligently convert from one type to another.
Definition
LexicalCast.h:181
xrpl::build_info
Versioning information for this build.
Definition
BuildInfo.h:11
xrpl::build_info::getFullVersionString
std::string const & getFullVersionString()
Full server version string.
Definition
BuildInfo.cpp:77
xrpl::build_info::kImplementationVersionIdentifierMask
static constexpr std::uint64_t kImplementationVersionIdentifierMask
Definition
BuildInfo.cpp:84
xrpl::build_info::getVersionString
std::string const & getVersionString()
Server version.
Definition
BuildInfo.cpp:63
xrpl::build_info::isXrpldVersion
bool isXrpldVersion(std::uint64_t version)
Check if the encoded software version is an xrpld software version.
Definition
BuildInfo.cpp:158
xrpl::build_info::encodeSoftwareVersion
std::uint64_t encodeSoftwareVersion(std::string_view versionStr)
Encode an arbitrary server software version in a 64-bit integer.
Definition
BuildInfo.cpp:87
xrpl::build_info::getEncodedVersion
std::uint64_t getEncodedVersion()
Returns this server's version packed in a 64-bit integer.
Definition
BuildInfo.cpp:151
xrpl::build_info::isNewerVersion
bool isNewerVersion(std::uint64_t version)
Check if the version is newer than the local node's xrpld software version.
Definition
BuildInfo.cpp:164
xrpl::build_info::kImplementationVersionIdentifier
static constexpr std::uint64_t kImplementationVersionIdentifier
Definition
BuildInfo.cpp:83
xrpl::git::getCommitHash
std::string const & getCommitHash()
Definition
Git.cpp:18
xrpl::logicError
void logicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
Definition
libxrpl/basics/contract.cpp:19
xrpl::systemName
static std::string const & systemName()
Definition
SystemParameters.h:18
std::string_view::starts_with
T starts_with(T... args)
string
string_view
std::string_view::substr
T substr(T... args)
Generated by
1.17.0