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 };
74 class FeatureCollections
83 : name(name_), feature(feature_)
102 template <
class tag,
typename Type, Type Feature::*PtrToMember>
103 using feature_hashed_unique = boost::multi_index::hashed_unique<
104 boost::multi_index::tag<tag>,
105 boost::multi_index::member<Feature, Type, PtrToMember>>;
108 using feature_indexing = boost::multi_index::indexed_by<
109 boost::multi_index::random_access<
110 boost::multi_index::tag<Feature::byIndex>>,
111 feature_hashed_unique<Feature::byFeature, uint256, &Feature::feature>,
112 feature_hashed_unique<Feature::byName, std::string, &Feature::name>>;
116 boost::multi_index::multi_index_container<Feature, feature_indexing>
127 getByIndex(
size_t i)
const
129 if (i >= features.size())
131 const auto& sequence = features.get<Feature::byIndex>();
135 getIndex(Feature
const& feature)
const
137 const auto& sequence = features.get<Feature::byIndex>();
138 auto const it_to = sequence.iterator_to(feature);
139 return it_to - sequence.begin();
142 getByFeature(
uint256 const& feature)
const
144 const auto& feature_index = features.get<Feature::byFeature>();
145 auto const feature_it = feature_index.find(feature);
146 return feature_it == feature_index.end() ? nullptr : &*feature_it;
151 const auto& name_index = features.get<Feature::byName>();
152 auto const name_it = name_index.find(name);
153 return name_it == name_index.end() ? nullptr : &*name_it;
157 FeatureCollections();
207 FeatureCollections::FeatureCollections()
213 FeatureCollections::getRegisteredFeature(
std::string const& name)
const
216 Feature
const* feature = getByName(name);
218 return feature->feature;
223 check(
bool condition,
const char* logicErrorMessage)
230 FeatureCollections::registerFeature(
235 check(!readOnly,
"Attempting to register a feature after startup.");
238 "Invalid feature parameters. Must be supported to be up-voted.");
239 Feature
const* i = getByName(name);
246 "More features defined than allocated. Adjust numFeatures in "
251 features.emplace_back(name, f);
253 if (support == Supported::yes)
255 supported.emplace(name, vote);
263 upVotes + downVotes == supported.size(),
264 "Feature counting logic broke");
266 supported.size() <= features.size(),
267 "More supported features than defined features");
277 FeatureCollections::registrationIsDone()
284 FeatureCollections::featureToBitsetIndex(
uint256 const& f)
const
288 Feature
const* feature = getByFeature(f);
292 return getIndex(*feature);
296 FeatureCollections::bitsetIndexToFeature(
size_t i)
const
299 Feature
const& feature = getByIndex(i);
300 return feature.feature;
304 FeatureCollections::featureToName(
uint256 const& f)
const
307 Feature
const* feature = getByFeature(f);
308 return feature ? feature->name :
to_string(f);
311 static FeatureCollections featureCollections;
321 return featureCollections.supportedAmendments();
328 return featureCollections.numDownVotedAmendments();
335 return featureCollections.numUpVotedAmendments();
343 return featureCollections.getRegisteredFeature(name);
349 return featureCollections.registerFeature(name, support, vote);
364 return featureCollections.registrationIsDone();
370 return featureCollections.featureToBitsetIndex(f);
376 return featureCollections.bitsetIndexToFeature(i);
382 return featureCollections.featureToName(f);
385 #pragma push_macro("REGISTER_FEATURE")
386 #undef REGISTER_FEATURE
393 #define REGISTER_FEATURE(fName, supported, defaultvote) \
394 uint256 const feature##fName = \
395 registerFeature(#fName, supported, defaultvote)
397 #pragma push_macro("REGISTER_FIX")
405 #define REGISTER_FIX(fName, supported, defaultvote) \
406 uint256 const fName = registerFeature(#fName, supported, defaultvote)
462 [[deprecated(
"The referenced amendment has been retired"), maybe_unused]]
483 #pragma pop_macro("REGISTER_FIX")
485 #undef REGISTER_FEATURE
486 #pragma pop_macro("REGISTER_FEATURE")
494 featureCollections.registrationIsDone();