rippled
Loading...
Searching...
No Matches
Feature.h
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#ifndef RIPPLE_PROTOCOL_FEATURE_H_INCLUDED
21#define RIPPLE_PROTOCOL_FEATURE_H_INCLUDED
22
23#include <xrpl/basics/base_uint.h>
24#include <boost/container/flat_map.hpp>
25#include <array>
26#include <bitset>
27#include <map>
28#include <optional>
29#include <string>
30
68namespace ripple {
69
70enum class VoteBehavior : int { Obsolete = -1, DefaultNo = 0, DefaultYes };
71enum class AmendmentSupport : int { Retired = -1, Supported = 0, Unsupported };
72
76
77namespace detail {
78
79// This value SHOULD be equal to the number of amendments registered in
80// Feature.cpp. Because it's only used to reserve storage, and determine how
81// large to make the FeatureBitset, it MAY be larger. It MUST NOT be less than
82// the actual number of amendments. A LogicError on startup will verify this.
83static constexpr std::size_t numFeatures = 87;
84
90
97
104
105} // namespace detail
106
109
110size_t
112
114bitsetIndexToFeature(size_t i);
115
117featureToName(uint256 const& f);
118
119class FeatureBitset : private std::bitset<detail::numFeatures>
120{
122
123 template <class... Fs>
124 void
125 initFromFeatures(uint256 const& f, Fs&&... fs)
126 {
127 set(f);
128 if constexpr (sizeof...(fs) > 0)
129 initFromFeatures(std::forward<Fs>(fs)...);
130 }
131
132public:
133 using base::bitset;
134 using base::operator==;
135
136 using base::all;
137 using base::any;
138 using base::count;
139 using base::flip;
140 using base::none;
141 using base::reset;
142 using base::set;
143 using base::size;
144 using base::test;
145 using base::operator[];
146 using base::to_string;
147 using base::to_ullong;
148 using base::to_ulong;
149
150 FeatureBitset() = default;
151
152 explicit FeatureBitset(base const& b) : base(b)
153 {
154 XRPL_ASSERT(
155 b.count() == count(),
156 "ripple::FeatureBitset::FeatureBitset(base) : count match");
157 }
158
159 template <class... Fs>
160 explicit FeatureBitset(uint256 const& f, Fs&&... fs)
161 {
162 initFromFeatures(f, std::forward<Fs>(fs)...);
163 XRPL_ASSERT(
164 count() == (sizeof...(fs) + 1),
165 "ripple::FeatureBitset::FeatureBitset(uint256) : count and "
166 "sizeof... do match");
167 }
168
169 template <class Col>
170 explicit FeatureBitset(Col const& fs)
171 {
172 for (auto const& f : fs)
174 XRPL_ASSERT(
175 fs.size() == count(),
176 "ripple::FeatureBitset::FeatureBitset(Container auto) : count and "
177 "size do match");
178 }
179
180 auto
182 {
184 }
185
186 auto
187 operator[](uint256 const& f) const
188 {
190 }
191
193 set(uint256 const& f, bool value = true)
194 {
196 return *this;
197 }
198
200 reset(uint256 const& f)
201 {
203 return *this;
204 }
205
207 flip(uint256 const& f)
208 {
210 return *this;
211 }
212
215 {
216 base::operator&=(rhs);
217 return *this;
218 }
219
222 {
223 base::operator|=(rhs);
224 return *this;
225 }
226
228 operator~() const
229 {
231 }
232
233 friend FeatureBitset
234 operator&(FeatureBitset const& lhs, FeatureBitset const& rhs)
235 {
236 return FeatureBitset{
237 static_cast<base const&>(lhs) & static_cast<base const&>(rhs)};
238 }
239
240 friend FeatureBitset
241 operator&(FeatureBitset const& lhs, uint256 const& rhs)
242 {
243 return lhs & FeatureBitset{rhs};
244 }
245
246 friend FeatureBitset
247 operator&(uint256 const& lhs, FeatureBitset const& rhs)
248 {
249 return FeatureBitset{lhs} & rhs;
250 }
251
252 friend FeatureBitset
253 operator|(FeatureBitset const& lhs, FeatureBitset const& rhs)
254 {
255 return FeatureBitset{
256 static_cast<base const&>(lhs) | static_cast<base const&>(rhs)};
257 }
258
259 friend FeatureBitset
260 operator|(FeatureBitset const& lhs, uint256 const& rhs)
261 {
262 return lhs | FeatureBitset{rhs};
263 }
264
265 friend FeatureBitset
266 operator|(uint256 const& lhs, FeatureBitset const& rhs)
267 {
268 return FeatureBitset{lhs} | rhs;
269 }
270
271 friend FeatureBitset
272 operator^(FeatureBitset const& lhs, FeatureBitset const& rhs)
273 {
274 return FeatureBitset{
275 static_cast<base const&>(lhs) ^ static_cast<base const&>(rhs)};
276 }
277
278 friend FeatureBitset
279 operator^(FeatureBitset const& lhs, uint256 const& rhs)
280 {
281 return lhs ^ FeatureBitset { rhs };
282 }
283
284 friend FeatureBitset
285 operator^(uint256 const& lhs, FeatureBitset const& rhs)
286 {
287 return FeatureBitset{lhs} ^ rhs;
288 }
289
290 // set difference
291 friend FeatureBitset
292 operator-(FeatureBitset const& lhs, FeatureBitset const& rhs)
293 {
294 return lhs & ~rhs;
295 }
296
297 friend FeatureBitset
298 operator-(FeatureBitset const& lhs, uint256 const& rhs)
299 {
300 return lhs - FeatureBitset{rhs};
301 }
302
303 friend FeatureBitset
304 operator-(uint256 const& lhs, FeatureBitset const& rhs)
305 {
306 return FeatureBitset{lhs} - rhs;
307 }
308};
309
310template <class F>
311void
313{
314 for (size_t i = 0; i < bs.size(); ++i)
315 if (bs[i])
317}
318
319#pragma push_macro("XRPL_FEATURE")
320#undef XRPL_FEATURE
321#pragma push_macro("XRPL_FIX")
322#undef XRPL_FIX
323
324#define XRPL_FEATURE(name, supported, vote) extern uint256 const feature##name;
325#define XRPL_FIX(name, supported, vote) extern uint256 const fix##name;
326
327#include <xrpl/protocol/detail/features.macro>
328
329#undef XRPL_FIX
330#pragma pop_macro("XRPL_FIX")
331#undef XRPL_FEATURE
332#pragma pop_macro("XRPL_FEATURE")
333
334} // namespace ripple
335
336#endif
friend FeatureBitset operator|(FeatureBitset const &lhs, FeatureBitset const &rhs)
Definition: Feature.h:253
friend FeatureBitset operator&(FeatureBitset const &lhs, uint256 const &rhs)
Definition: Feature.h:241
friend FeatureBitset operator-(FeatureBitset const &lhs, uint256 const &rhs)
Definition: Feature.h:298
friend FeatureBitset operator|(uint256 const &lhs, FeatureBitset const &rhs)
Definition: Feature.h:266
FeatureBitset & operator|=(FeatureBitset const &rhs)
Definition: Feature.h:221
friend FeatureBitset operator^(FeatureBitset const &lhs, uint256 const &rhs)
Definition: Feature.h:279
auto operator[](uint256 const &f)
Definition: Feature.h:181
void initFromFeatures(uint256 const &f, Fs &&... fs)
Definition: Feature.h:125
friend FeatureBitset operator|(FeatureBitset const &lhs, uint256 const &rhs)
Definition: Feature.h:260
friend FeatureBitset operator&(uint256 const &lhs, FeatureBitset const &rhs)
Definition: Feature.h:247
friend FeatureBitset operator^(FeatureBitset const &lhs, FeatureBitset const &rhs)
Definition: Feature.h:272
FeatureBitset(uint256 const &f, Fs &&... fs)
Definition: Feature.h:160
FeatureBitset operator~() const
Definition: Feature.h:228
friend FeatureBitset operator^(uint256 const &lhs, FeatureBitset const &rhs)
Definition: Feature.h:285
friend FeatureBitset operator&(FeatureBitset const &lhs, FeatureBitset const &rhs)
Definition: Feature.h:234
friend FeatureBitset operator-(uint256 const &lhs, FeatureBitset const &rhs)
Definition: Feature.h:304
FeatureBitset & flip(uint256 const &f)
Definition: Feature.h:207
FeatureBitset(Col const &fs)
Definition: Feature.h:170
FeatureBitset & set(uint256 const &f, bool value=true)
Definition: Feature.h:193
friend FeatureBitset operator-(FeatureBitset const &lhs, FeatureBitset const &rhs)
Definition: Feature.h:292
FeatureBitset(base const &b)
Definition: Feature.h:152
FeatureBitset & operator&=(FeatureBitset const &rhs)
Definition: Feature.h:214
FeatureBitset & reset(uint256 const &f)
Definition: Feature.h:200
auto operator[](uint256 const &f) const
Definition: Feature.h:187
static constexpr std::size_t numFeatures
Definition: Feature.h:83
std::size_t numUpVotedAmendments()
Amendments that this server will vote for by default.
Definition: Feature.cpp:368
std::size_t numDownVotedAmendments()
Amendments that this server won't vote for by default.
Definition: Feature.cpp:361
std::map< std::string, VoteBehavior > const & supportedAmendments()
Amendments that this server supports and the default voting behavior.
Definition: Feature.cpp:354
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
uint256 bitsetIndexToFeature(size_t i)
Definition: Feature.cpp:409
AmendmentSupport
Definition: Feature.h:71
size_t featureToBitsetIndex(uint256 const &f)
Definition: Feature.cpp:403
std::map< std::string, AmendmentSupport > const & allAmendments()
All amendments libxrpl knows about.
Definition: Feature.cpp:345
std::string featureToName(uint256 const &f)
Definition: Feature.cpp:415
std::optional< uint256 > getRegisteredFeature(std::string const &name)
Definition: Feature.cpp:376
VoteBehavior
Definition: Feature.h:70
void foreachFeature(FeatureBitset bs, F &&f)
Definition: Feature.h:312