xrpld
Loading...
Searching...
No Matches
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
20namespace xrpl::build_info {
21
22namespace {
23
24// Set by cmake/XrplVersion.cmake, see http://semver.org/
25constexpr char kVersionString[] = XRPLD_VERSION;
26
27std::string
28buildVersionString()
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
62std::string const&
64{
65 static std::string const kValue = [] {
66 std::string const s = buildVersionString();
67
69 if (!v.parse(s) || v.print() != s)
70 logicError(s + ": Bad server version string");
71 return s;
72 }();
73 return kValue;
74}
75
76std::string const&
78{
79 static std::string const kValue = systemName() + "-" + getVersionString();
80 return kValue;
81}
82
83static constexpr std::uint64_t kImplementationVersionIdentifier = 0x183B'0000'0000'0000LLU;
84static constexpr std::uint64_t kImplementationVersionIdentifierMask = 0xFFFF'0000'0000'0000LLU;
85
88{
90
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
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
152{
153 static std::uint64_t const kCookie = {encodeSoftwareVersion(getVersionString())};
154 return kCookie;
155}
156
157bool
162
163bool
165{
166 if (isXrpldVersion(version))
167 return version > getEncodedVersion();
168 return false;
169}
170
171} // namespace xrpl::build_info
T clamp(T... args)
A Semantic Version number.
IdentifierList preReleaseIdentifiers
bool isPreRelease() const noexcept
bool parse(std::string_view input)
Parse a semantic version string.
std::string print() const
Produce a string from semantic version components.
T empty(T... args)
constexpr bool lexicalCastChecked(Out &out, In in)
Intelligently convert from one type to another.
Versioning information for this build.
Definition BuildInfo.h:11
std::string const & getFullVersionString()
Full server version string.
Definition BuildInfo.cpp:77
static constexpr std::uint64_t kImplementationVersionIdentifierMask
Definition BuildInfo.cpp:84
std::string const & getVersionString()
Server version.
Definition BuildInfo.cpp:63
bool isXrpldVersion(std::uint64_t version)
Check if the encoded software version is an xrpld software version.
std::uint64_t encodeSoftwareVersion(std::string_view versionStr)
Encode an arbitrary server software version in a 64-bit integer.
Definition BuildInfo.cpp:87
std::uint64_t getEncodedVersion()
Returns this server's version packed in a 64-bit integer.
bool isNewerVersion(std::uint64_t version)
Check if the version is newer than the local node's xrpld software version.
static constexpr std::uint64_t kImplementationVersionIdentifier
Definition BuildInfo.cpp:83
std::string const & getCommitHash()
Definition Git.cpp:18
void logicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
static std::string const & systemName()
T starts_with(T... args)
T substr(T... args)