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.8.4"
37 // clang-format on
38 
39 #if defined(DEBUG) || defined(SANITIZER)
40 #ifdef GIT_COMMIT_HASH
41  "-" GIT_COMMIT_HASH
42 #endif
43  "+"
44 #ifdef DEBUG
45  "DEBUG"
46 #ifdef SANITIZER
47  "."
48 #endif
49 #endif
50 
51 #ifdef SANITIZER
52  BOOST_PP_STRINGIZE(SANITIZER)
53 #endif
54 #endif
55 
56  //--------------------------------------------------------------------------
57  ;
58 
59 //
60 // Don't touch anything below this line
61 //
62 
63 std::string const&
65 {
66  static std::string const value = [] {
67  std::string const s = versionString;
69  if (!v.parse(s) || v.print() != s)
70  LogicError(s + ": Bad server version string");
71  return s;
72  }();
73  return value;
74 }
75 
76 std::string const&
78 {
79  static std::string const value = "rippled-" + getVersionString();
80  return value;
81 }
82 
84  0x183B'0000'0000'0000LLU;
85 static constexpr std::uint64_t implementationVersionIdentifierMask =
86  0xFFFF'0000'0000'0000LLU;
87 
89 encodeSoftwareVersion(char const* const versionStr)
90 {
92 
94 
95  if (v.parse(std::string(versionStr)))
96  {
97  if (v.majorVersion >= 0 && v.majorVersion <= 255)
98  c |= static_cast<std::uint64_t>(v.majorVersion) << 40;
99 
100  if (v.minorVersion >= 0 && v.minorVersion <= 255)
101  c |= static_cast<std::uint64_t>(v.minorVersion) << 32;
102 
103  if (v.patchVersion >= 0 && v.patchVersion <= 255)
104  c |= static_cast<std::uint64_t>(v.patchVersion) << 24;
105 
106  if (!v.isPreRelease())
107  c |= static_cast<std::uint64_t>(0xC00000);
108 
109  if (v.isPreRelease())
110  {
111  std::uint8_t x = 0;
112 
113  for (auto id : v.preReleaseIdentifiers)
114  {
115  auto parsePreRelease = [](std::string_view identifier,
116  std::string_view prefix,
117  std::uint8_t key,
118  std::uint8_t lok,
119  std::uint8_t hik) -> std::uint8_t {
120  std::uint8_t ret = 0;
121 
122  if (prefix != identifier.substr(0, prefix.length()))
123  return 0;
124 
126  ret,
127  std::string(identifier.substr(prefix.length()))))
128  return 0;
129 
130  if (std::clamp(ret, lok, hik) != ret)
131  return 0;
132 
133  return ret + key;
134  };
135 
136  x = parsePreRelease(id, "rc", 0x80, 0, 63);
137 
138  if (x == 0)
139  x = parsePreRelease(id, "b", 0x40, 0, 63);
140 
141  if (x & 0xC0)
142  {
143  c |= static_cast<std::uint64_t>(x) << 16;
144  break;
145  }
146  }
147  }
148  }
149 
150  return c;
151 }
152 
155 {
156  static std::uint64_t const cookie = {encodeSoftwareVersion(versionString)};
157  return cookie;
158 }
159 
160 bool
162 {
163  return (version & implementationVersionIdentifierMask) ==
165 }
166 
167 bool
169 {
170  if (isRippledVersion(version))
171  return version > getEncodedVersion();
172  return false;
173 }
174 
175 } // namespace BuildInfo
176 
177 } // 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:154
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:89
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:83
algorithm
ripple::BuildInfo::isRippledVersion
bool isRippledVersion(std::uint64_t version)
Check if the encoded software version is a rippled software version.
Definition: BuildInfo.cpp:161
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:64
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:168
ripple::BuildInfo::getFullVersionString
std::string const & getFullVersionString()
Full server version string.
Definition: BuildInfo.cpp:77
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:85