20 #include <ripple/protocol/Feature.h>
22 #include <ripple/basics/Slice.h>
23 #include <ripple/basics/contract.h>
24 #include <ripple/protocol/digest.h>
25 #include <boost/container_hash/hash.hpp>
26 #include <boost/multi_index/hashed_index.hpp>
27 #include <boost/multi_index/key_extractors.hpp>
28 #include <boost/multi_index/random_access_index.hpp>
29 #include <boost/multi_index_container.hpp>
38 using namespace boost;
39 for (
auto const& n : feature)
40 hash_combine(seed, n);
46 enum class Supported : bool { no =
false, yes };
69 class FeatureCollections
78 : name(name_), feature(feature_)
97 template <
class tag,
typename Type, Type Feature::*PtrToMember>
98 using feature_hashed_unique = boost::multi_index::hashed_unique<
99 boost::multi_index::tag<tag>,
100 boost::multi_index::member<Feature, Type, PtrToMember>>;
103 using feature_indexing = boost::multi_index::indexed_by<
104 boost::multi_index::random_access<
105 boost::multi_index::tag<Feature::byIndex>>,
106 feature_hashed_unique<Feature::byFeature, uint256, &Feature::feature>,
107 feature_hashed_unique<Feature::byName, std::string, &Feature::name>>;
111 boost::multi_index::multi_index_container<Feature, feature_indexing>
122 getByIndex(
size_t i)
const
124 if (i >= features.size())
126 const auto& sequence = features.get<Feature::byIndex>();
130 getIndex(Feature
const& feature)
const
132 const auto& sequence = features.get<Feature::byIndex>();
133 auto const it_to = sequence.iterator_to(feature);
134 return it_to - sequence.begin();
137 getByFeature(
uint256 const& feature)
const
139 const auto& feature_index = features.get<Feature::byFeature>();
140 auto const feature_it = feature_index.find(feature);
141 return feature_it == feature_index.end() ? nullptr : &*feature_it;
146 const auto& name_index = features.get<Feature::byName>();
147 auto const name_it = name_index.find(name);
148 return name_it == name_index.end() ? nullptr : &*name_it;
152 FeatureCollections();
202 FeatureCollections::FeatureCollections()
208 FeatureCollections::getRegisteredFeature(
std::string const& name)
const
211 Feature
const* feature = getByName(name);
213 return feature->feature;
218 check(
bool condition,
const char* logicErrorMessage)
225 FeatureCollections::registerFeature(
230 check(!readOnly,
"Attempting to register a feature after startup.");
233 "Invalid feature parameters. Must be supported to be up-voted.");
234 Feature
const* i = getByName(name);
241 "More features defined than allocated. Adjust numFeatures in "
246 features.emplace_back(name, f);
248 if (support == Supported::yes)
250 supported.emplace(name, vote);
258 upVotes + downVotes == supported.size(),
259 "Feature counting logic broke");
261 supported.size() <= features.size(),
262 "More supported features than defined features");
272 FeatureCollections::registrationIsDone()
279 FeatureCollections::featureToBitsetIndex(
uint256 const& f)
const
283 Feature
const* feature = getByFeature(f);
287 return getIndex(*feature);
291 FeatureCollections::bitsetIndexToFeature(
size_t i)
const
294 Feature
const& feature = getByIndex(i);
295 return feature.feature;
299 FeatureCollections::featureToName(
uint256 const& f)
const
302 Feature
const* feature = getByFeature(f);
303 return feature ? feature->name :
to_string(f);
306 static FeatureCollections featureCollections;
316 return featureCollections.supportedAmendments();
323 return featureCollections.numDownVotedAmendments();
330 return featureCollections.numUpVotedAmendments();
338 return featureCollections.getRegisteredFeature(name);
344 return featureCollections.registerFeature(name, support, vote);
359 return featureCollections.registrationIsDone();
365 return featureCollections.featureToBitsetIndex(f);
371 return featureCollections.bitsetIndexToFeature(i);
377 return featureCollections.featureToName(f);
380 #pragma push_macro("REGISTER_FEATURE")
381 #undef REGISTER_FEATURE
388 #define REGISTER_FEATURE(fName, supported, defaultvote) \
389 uint256 const feature##fName = \
390 registerFeature(#fName, supported, defaultvote)
392 #pragma push_macro("REGISTER_FIX")
400 #define REGISTER_FIX(fName, supported, defaultvote) \
401 uint256 const fName = registerFeature(#fName, supported, defaultvote)
448 [[deprecated(
"The referenced amendment has been retired"), maybe_unused]]
469 #pragma pop_macro("REGISTER_FIX")
471 #undef REGISTER_FEATURE
472 #pragma pop_macro("REGISTER_FEATURE")
480 featureCollections.registrationIsDone();