rippled
BuildInfo.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/basics/contract.h>
21 #include <ripple/beast/core/LexicalCast.h>
22 #include <ripple/beast/core/SemanticVersion.h>
23 #include <ripple/protocol/BuildInfo.h>
24 #include <boost/preprocessor/stringize.hpp>
25 #include <algorithm>
26 
27 namespace ripple {
28 
29 namespace BuildInfo {
30 
31 //--------------------------------------------------------------------------
32 // The build version number. You must edit this for each release
33 // and follow the format described at http://semver.org/
34 //------------------------------------------------------------------------------
35 // clang-format off
36 char const* const versionString = "1.11.0-rc2"
37 // clang-format on
38 
39 #if defined(DEBUG) || defined(SANITIZER)
40  "+"
41 #ifdef GIT_COMMIT_HASH
42  GIT_COMMIT_HASH
43  "."
44 #endif
45 #ifdef DEBUG
46  "DEBUG"
47 #ifdef SANITIZER
48  "."
49 #endif
50 #endif
51 
52 #ifdef SANITIZER
53  BOOST_PP_STRINGIZE(SANITIZER)
54 #endif
55 #endif
56 
57  //--------------------------------------------------------------------------
58  ;
59 
60 //
61 // Don't touch anything below this line
62 //
63 
64 std::string const&
66 {
67  static std::string const value = [] {
68  std::string const s = versionString;
70  if (!v.parse(s) || v.print() != s)
71  LogicError(s + ": Bad server version string");
72  return s;
73  }();
74  return value;
75 }
76 
77 std::string const&
79 {
80  static std::string const value = "rippled-" + getVersionString();
81  return value;
82 }
83 
85  0x183B'0000'0000'0000LLU;
86 static constexpr std::uint64_t implementationVersionIdentifierMask =
87  0xFFFF'0000'0000'0000LLU;
88 
90 encodeSoftwareVersion(char const* const versionStr)
91 {
93 
95 
96  if (v.parse(std::string(versionStr)))
97  {
98  if (v.majorVersion >= 0 && v.majorVersion <= 255)
99  c |= static_cast<std::uint64_t>(v.majorVersion) << 40;
100 
101  if (v.minorVersion >= 0 && v.minorVersion <= 255)
102  c |= static_cast<std::uint64_t>(v.minorVersion) << 32;
103 
104  if (v.patchVersion >= 0 && v.patchVersion <= 255)
105  c |= static_cast<std::uint64_t>(v.patchVersion) << 24;
106 
107  if (!v.isPreRelease())
108  c |= static_cast<std::uint64_t>(0xC00000);
109 
110  if (v.isPreRelease())
111  {
112  std::uint8_t x = 0;
113 
114  for (auto id : v.preReleaseIdentifiers)
115  {
116  auto parsePreRelease = [](std::string_view identifier,
117  std::string_view prefix,
118  std::uint8_t key,
119  std::uint8_t lok,
120  std::uint8_t hik) -> std::uint8_t {
121  std::uint8_t ret = 0;
122 
123  if (prefix != identifier.substr(0, prefix.length()))
124  return 0;
125 
127  ret,
128  std::string(identifier.substr(prefix.length()))))
129  return 0;
130 
131  if (std::clamp(ret, lok, hik) != ret)
132  return 0;
133 
134  return ret + key;
135  };
136 
137  x = parsePreRelease(id, "rc", 0x80, 0, 63);
138 
139  if (x == 0)
140  x = parsePreRelease(id, "b", 0x40, 0, 63);
141 
142  if (x & 0xC0)
143  {
144  c |= static_cast<std::uint64_t>(x) << 16;
145  break;
146  }
147  }
148  }
149  }
150 
151  return c;
152 }
153 
156 {
157  static std::uint64_t const cookie = {encodeSoftwareVersion(versionString)};
158  return cookie;
159 }
160 
161 bool
163 {
164  return (version & implementationVersionIdentifierMask) ==
166 }
167 
168 bool
170 {
171  if (isRippledVersion(version))
172  return version > getEncodedVersion();
173  return false;
174 }
175 
176 } // namespace BuildInfo
177 
178 } // namespace ripple
std::string
STL class.
std::string_view
STL class.
ripple::BuildInfo::getEncodedVersion
std::uint64_t getEncodedVersion()
Returns this server's version packed in a 64-bit integer.
Definition: BuildInfo.cpp:155
beast::SemanticVersion::print
std::string print() const
Produce a string from semantic version components.
Definition: SemanticVersion.cpp:236
ripple::BuildInfo::encodeSoftwareVersion
std::uint64_t encodeSoftwareVersion(char const *const versionStr)
Encode an arbitrary server software version in a 64-bit integer.
Definition: BuildInfo.cpp:90
beast::SemanticVersion::isPreRelease
bool isPreRelease() const noexcept
Definition: SemanticVersion.h:68
beast::SemanticVersion
A Semantic Version number.
Definition: SemanticVersion.h:35
ripple::BuildInfo::implementationVersionIdentifier
static constexpr std::uint64_t implementationVersionIdentifier
Definition: BuildInfo.cpp:84
algorithm
ripple::BuildInfo::isRippledVersion
bool isRippledVersion(std::uint64_t version)
Check if the encoded software version is a rippled software version.
Definition: BuildInfo.cpp:162
beast::SemanticVersion::minorVersion
int minorVersion
Definition: SemanticVersion.h:41
beast::SemanticVersion::preReleaseIdentifiers
identifier_list preReleaseIdentifiers
Definition: SemanticVersion.h:44
ripple::BuildInfo::getVersionString
std::string const & getVersionString()
Server version.
Definition: BuildInfo.cpp:65
std::uint64_t
beast::SemanticVersion::majorVersion
int majorVersion
Definition: SemanticVersion.h:40
std::string_view::substr
T substr(T... args)
ripple::BuildInfo::isNewerVersion
bool isNewerVersion(std::uint64_t version)
Check if the version is newer than the local node's rippled software version.
Definition: BuildInfo.cpp:169
ripple::BuildInfo::getFullVersionString
std::string const & getFullVersionString()
Full server version string.
Definition: BuildInfo.cpp:78
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
beast::SemanticVersion::parse
bool parse(std::string const &input)
Parse a semantic version string.
Definition: SemanticVersion.cpp:168
std::clamp
T clamp(T... args)
ripple::BuildInfo::versionString
char const *const versionString
Definition: BuildInfo.cpp:36
beast::lexicalCastChecked
bool lexicalCastChecked(Out &out, In in)
Intelligently convert from one type to another.
Definition: LexicalCast.h:266
ripple::LogicError
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
Definition: contract.cpp:48
beast::SemanticVersion::patchVersion
int patchVersion
Definition: SemanticVersion.h:42
ripple::BuildInfo::implementationVersionIdentifierMask
static constexpr std::uint64_t implementationVersionIdentifierMask
Definition: BuildInfo.cpp:86