1#include <xrpl/basics/Slice.h>
2#include <xrpl/basics/base_uint.h>
3#include <xrpl/basics/contract.h>
4#include <xrpl/beast/utility/instrumentation.h>
5#include <xrpl/protocol/Feature.h>
6#include <xrpl/protocol/digest.h>
8#include <boost/container_hash/hash.hpp>
9#include <boost/multi_index/hashed_index.hpp>
10#include <boost/multi_index/indexed_by.hpp>
11#include <boost/multi_index/member.hpp>
12#include <boost/multi_index/random_access_index.hpp>
13#include <boost/multi_index/tag.hpp>
14#include <boost/multi_index_container.hpp>
28 using namespace boost;
29 for (
auto const& n : feature)
30 hash_combine(seed, n);
65class FeatureCollections
74 : name(name_), feature(feature_)
93 template <
class tag,
typename Type, Type Feature::*PtrToMember>
94 using feature_hashed_unique = boost::multi_index::hashed_unique<
95 boost::multi_index::tag<tag>,
96 boost::multi_index::member<Feature, Type, PtrToMember>>;
99 using feature_indexing = boost::multi_index::indexed_by<
100 boost::multi_index::random_access<
101 boost::multi_index::tag<Feature::byIndex>>,
102 feature_hashed_unique<Feature::byFeature, uint256, &Feature::feature>,
103 feature_hashed_unique<Feature::byName, std::string, &Feature::name>>;
107 boost::multi_index::multi_index_container<Feature, feature_indexing>
119 getByIndex(
size_t i)
const
121 if (i >= features.size())
123 auto const& sequence = features.get<Feature::byIndex>();
127 getIndex(Feature
const& feature)
const
129 auto const& sequence = features.get<Feature::byIndex>();
130 auto const it_to = sequence.iterator_to(feature);
131 return it_to - sequence.begin();
134 getByFeature(
uint256 const& feature)
const
136 auto const& feature_index = features.get<Feature::byFeature>();
137 auto const feature_it = feature_index.find(feature);
138 return feature_it == feature_index.end() ? nullptr : &*feature_it;
143 auto const& name_index = features.get<Feature::byName>();
144 auto const name_it = name_index.find(name);
145 return name_it == name_index.end() ? nullptr : &*name_it;
149 FeatureCollections();
206FeatureCollections::FeatureCollections()
212FeatureCollections::getRegisteredFeature(
std::string const& name)
const
216 "ripple::FeatureCollections::getRegisteredFeature : startup completed");
217 Feature
const* feature = getByName(name);
219 return feature->feature;
224check(
bool condition,
char const* logicErrorMessage)
231FeatureCollections::registerFeature(
236 check(!readOnly,
"Attempting to register a feature after startup.");
239 "Invalid feature parameters. Must be supported to be up-voted.");
240 Feature
const* i = getByName(name);
245 "More features defined than allocated.");
249 features.emplace_back(name, f);
251 auto const getAmendmentSupport = [=]() {
257 all.emplace(name, getAmendmentSupport());
259 if (support == Supported::yes)
261 supported.emplace(name, vote);
269 upVotes + downVotes == supported.size(),
270 "Feature counting logic broke");
272 supported.size() <= features.size(),
273 "More supported features than defined features");
275 features.size() ==
all.size(),
276 "The 'all' features list is populated incorrectly");
286FeatureCollections::registrationIsDone()
293FeatureCollections::featureToBitsetIndex(
uint256 const& f)
const
297 "ripple::FeatureCollections::featureToBitsetIndex : startup completed");
299 Feature
const* feature = getByFeature(f);
303 return getIndex(*feature);
307FeatureCollections::bitsetIndexToFeature(
size_t i)
const
311 "ripple::FeatureCollections::bitsetIndexToFeature : startup completed");
312 Feature
const& feature = getByIndex(i);
313 return feature.feature;
317FeatureCollections::featureToName(
uint256 const& f)
const
321 "ripple::FeatureCollections::featureToName : startup completed");
322 Feature
const* feature = getByFeature(f);
323 return feature ? feature->name :
to_string(f);
326static FeatureCollections featureCollections;
334 return featureCollections.allAmendments();
343 return featureCollections.supportedAmendments();
350 return featureCollections.numDownVotedAmendments();
357 return featureCollections.numUpVotedAmendments();
365 return featureCollections.getRegisteredFeature(name);
371 return featureCollections.registerFeature(name, support, vote);
386 return featureCollections.registrationIsDone();
392 return featureCollections.featureToBitsetIndex(f);
398 return featureCollections.bitsetIndexToFeature(i);
404 return featureCollections.featureToName(f);
410#pragma push_macro("XRPL_FEATURE")
412#pragma push_macro("XRPL_FIX")
414#pragma push_macro("XRPL_RETIRE_FEATURE")
415#undef XRPL_RETIRE_FEATURE
416#pragma push_macro("XRPL_RETIRE_FIX")
417#undef XRPL_RETIRE_FIX
419#define XRPL_FEATURE(name, supported, vote) \
420 uint256 const feature##name = registerFeature(#name, supported, vote);
421#define XRPL_FIX(name, supported, vote) \
422 uint256 const fix##name = registerFeature("fix" #name, supported, vote);
425#define XRPL_RETIRE_FEATURE(name) \
426 [[deprecated("The referenced feature amendment has been retired")]] \
428 uint256 const retiredFeature##name = retireFeature(#name);
430#define XRPL_RETIRE_FIX(name) \
431 [[deprecated("The referenced fix amendment has been retired")]] \
433 uint256 const retiredFix##name = retireFeature("fix" #name);
436#include <xrpl/protocol/detail/features.macro>
438#undef XRPL_RETIRE_FEATURE
439#pragma pop_macro("XRPL_RETIRE_FEATURE")
440#undef XRPL_RETIRE_FIX
441#pragma pop_macro("XRPL_RETIRE_FIX")
443#pragma pop_macro("XRPL_FIX")
445#pragma pop_macro("XRPL_FEATURE")
453 featureCollections.registrationIsDone();
void check(bool condition, std::string const &message)
static constexpr std::size_t numFeatures
std::size_t numUpVotedAmendments()
Amendments that this server will vote for by default.
std::size_t numDownVotedAmendments()
Amendments that this server won't vote for by default.
std::map< std::string, VoteBehavior > const & supportedAmendments()
Amendments that this server supports and the default voting behavior.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
uint256 bitsetIndexToFeature(size_t i)
uint256 registerFeature(std::string const &name, Supported support, VoteBehavior vote)
size_t featureToBitsetIndex(uint256 const &f)
std::map< std::string, AmendmentSupport > const & allAmendments()
All amendments libxrpl knows about.
std::size_t hash_value(ripple::uint256 const &feature)
uint256 retireFeature(std::string const &name)
std::string featureToName(uint256 const &f)
std::optional< uint256 > getRegisteredFeature(std::string const &name)
std::string to_string(base_uint< Bits, Tag > const &a)
bool registrationIsDone()
Tell FeatureCollections when registration is complete.
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
static bool const readOnlySet
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.