Kagome
Polkadot Runtime Engine in C++17
version.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CORE_PRIMITIVES_VERSION_HPP
7 #define KAGOME_CORE_PRIMITIVES_VERSION_HPP
8 
9 #include <array>
10 #include <optional>
11 #include <string>
12 #include <vector>
13 
14 #include "common/blob.hpp"
15 
16 namespace kagome::primitives {
27 
31  using Api = std::pair<ApiId, uint32_t>;
32 
36  using ApisVec = std::vector<Api>;
37 
46  struct Version {
51  std::string spec_name;
52 
60  std::string impl_name;
61 
63  uint32_t authoring_version = 0u;
64 
73  uint32_t spec_version = 0u;
74 
75  uint32_t impl_version = 0u;
76 
79 
80  uint32_t transaction_version = 1u;
81 
83  uint8_t state_version = 0u;
84 
85  bool operator==(const Version &rhs) const {
86  return spec_name == rhs.spec_name and impl_name == rhs.impl_name
87  and authoring_version == rhs.authoring_version
88  and impl_version == rhs.impl_version and apis == rhs.apis
89  and spec_version == rhs.spec_version
90  and transaction_version == rhs.transaction_version
91  and state_version == rhs.state_version;
92  }
93 
94  bool operator!=(const Version &rhs) const {
95  return !operator==(rhs);
96  }
97  };
98 
99  namespace detail {
105  std::optional<uint32_t> coreVersionFromApis(const ApisVec &apis);
106  } // namespace detail
107 
115  template <class Stream,
116  typename = std::enable_if_t<Stream::is_encoder_stream>>
117  Stream &operator<<(Stream &s, const Version &v) {
118  return s << v.spec_name << v.impl_name << v.authoring_version
119  << v.spec_version << v.impl_version << v.apis
121  }
122 
130  template <class Stream,
131  typename = std::enable_if_t<Stream::is_decoder_stream>>
133  s >> v.spec_name >> v.impl_name >> v.authoring_version >> v.spec_version
134  >> v.impl_version >> v.apis;
135 
136  auto core_version = detail::coreVersionFromApis(v.apis);
137  // old Kusama runtimes do not contain transaction_version and state_version
138  // https://github.com/paritytech/substrate/blob/1b3ddae9dec6e7653b5d6ef0179df1af831f46f0/primitives/version/src/lib.rs#L238
139  if (core_version.has_value() and core_version.value() >= 3
140  and s.hasMore(sizeof(v.transaction_version))) {
141  s >> v.transaction_version;
142  } else {
143  v.transaction_version = 1;
144  }
145  if (core_version.has_value() and core_version.value() >= 4
146  and s.hasMore(sizeof(v.state_version))) {
147  s >> v.state_version;
148  } else {
149  v.state_version = 0;
150  }
151  return s;
152  }
153 } // namespace kagome::primitives
154 
155 #endif // KAGOME_CORE_PRIMITIVES_VERSION_HPP
Stream & operator>>(Stream &s, ArithmeticError &v)
ApisVec apis
List of supported API "features" along with their versions.
Definition: version.hpp:78
uint8_t state_version
Version of the state implementation used by this runtime.
Definition: version.hpp:83
bool operator!=(const Version &rhs) const
Definition: version.hpp:94
std::vector< Api > ApisVec
A vector of pairs of ApiId and a u32 for version.
Definition: version.hpp:36
bool operator==(const Version &rhs) const
Definition: version.hpp:85
std::pair< ApiId, uint32_t > Api
single Api item
Definition: version.hpp:31
uint32_t authoring_version
authoring_version is the version of the authorship interface
Definition: version.hpp:63
Stream & operator<<(Stream &s, const ArithmeticError &v)
libp2p::connection::Stream Stream
std::optional< uint32_t > coreVersionFromApis(const ApisVec &apis)
Definition: version.cpp:22