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
73 explicit Feature(
std::string const& name_,
uint256 const& feature_) : name(name_), feature(feature_)
92 template <
class tag,
typename Type, Type Feature::*PtrToMember>
93 using feature_hashed_unique = boost::multi_index::
94 hashed_unique<boost::multi_index::tag<tag>, boost::multi_index::member<Feature, Type, PtrToMember>>;
97 using feature_indexing = boost::multi_index::indexed_by<
98 boost::multi_index::random_access<boost::multi_index::tag<Feature::byIndex>>,
99 feature_hashed_unique<Feature::byFeature, uint256, &Feature::feature>,
100 feature_hashed_unique<Feature::byName, std::string, &Feature::name>>;
104 boost::multi_index::multi_index_container<Feature, feature_indexing> features;
115 getByIndex(
size_t i)
const
117 if (i >= features.size())
119 auto const& sequence = features.get<Feature::byIndex>();
123 getIndex(Feature
const& feature)
const
125 auto const& sequence = features.get<Feature::byIndex>();
126 auto const it_to = sequence.iterator_to(feature);
127 return it_to - sequence.begin();
130 getByFeature(
uint256 const& feature)
const
132 auto const& feature_index = features.get<Feature::byFeature>();
133 auto const feature_it = feature_index.find(feature);
134 return feature_it == feature_index.end() ? nullptr : &*feature_it;
139 auto const& name_index = features.get<Feature::byName>();
140 auto const name_it = name_index.find(name);
141 return name_it == name_index.end() ? nullptr : &*name_it;
145 FeatureCollections();
199FeatureCollections::FeatureCollections()
205FeatureCollections::getRegisteredFeature(
std::string const& name)
const
207 XRPL_ASSERT(readOnly.
load(),
"xrpl::FeatureCollections::getRegisteredFeature : startup completed");
208 Feature
const* feature = getByName(name);
210 return feature->feature;
215check(
bool condition,
char const* logicErrorMessage)
224 check(!readOnly,
"Attempting to register a feature after startup.");
227 "Invalid feature parameters. Must be supported to be up-voted.");
228 Feature
const* i = getByName(name);
235 features.emplace_back(name, f);
237 auto const getAmendmentSupport = [=]() {
242 all.emplace(name, getAmendmentSupport());
244 if (support == Supported::yes)
246 supported.emplace(name, vote);
253 check(upVotes + downVotes == supported.size(),
"Feature counting logic broke");
254 check(supported.size() <= features.size(),
"More supported features than defined features");
255 check(features.size() ==
all.size(),
"The 'all' features list is populated incorrectly");
265FeatureCollections::registrationIsDone()
272FeatureCollections::featureToBitsetIndex(
uint256 const& f)
const
274 XRPL_ASSERT(readOnly.
load(),
"xrpl::FeatureCollections::featureToBitsetIndex : startup completed");
276 Feature
const* feature = getByFeature(f);
280 return getIndex(*feature);
284FeatureCollections::bitsetIndexToFeature(
size_t i)
const
286 XRPL_ASSERT(readOnly.
load(),
"xrpl::FeatureCollections::bitsetIndexToFeature : startup completed");
287 Feature
const& feature = getByIndex(i);
288 return feature.feature;
292FeatureCollections::featureToName(
uint256 const& f)
const
294 XRPL_ASSERT(readOnly.
load(),
"xrpl::FeatureCollections::featureToName : startup completed");
295 Feature
const* feature = getByFeature(f);
296 return feature ? feature->name :
to_string(f);
299static FeatureCollections featureCollections;
307 return featureCollections.allAmendments();
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);
383#pragma push_macro("XRPL_FEATURE")
385#pragma push_macro("XRPL_FIX")
387#pragma push_macro("XRPL_RETIRE_FEATURE")
388#undef XRPL_RETIRE_FEATURE
389#pragma push_macro("XRPL_RETIRE_FIX")
390#undef XRPL_RETIRE_FIX
392#define XRPL_FEATURE(name, supported, vote) uint256 const feature##name = registerFeature(#name, supported, vote);
393#define XRPL_FIX(name, supported, vote) uint256 const fix##name = registerFeature("fix" #name, supported, vote);
396#define XRPL_RETIRE_FEATURE(name) \
397 [[deprecated("The referenced feature amendment has been retired")]] \
399 uint256 const retiredFeature##name = retireFeature(#name);
401#define XRPL_RETIRE_FIX(name) \
402 [[deprecated("The referenced fix amendment has been retired")]] \
404 uint256 const retiredFix##name = retireFeature("fix" #name);
407#include <xrpl/protocol/detail/features.macro>
409#undef XRPL_RETIRE_FEATURE
410#pragma pop_macro("XRPL_RETIRE_FEATURE")
411#undef XRPL_RETIRE_FIX
412#pragma pop_macro("XRPL_RETIRE_FIX")
414#pragma pop_macro("XRPL_FIX")
416#pragma pop_macro("XRPL_FEATURE")
423[[maybe_unused]]
static bool const readOnlySet = featureCollections.registrationIsDone();
void check(bool condition, std::string const &message)
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.
std::size_t numUpVotedAmendments()
Amendments that this server will vote for by default.
static constexpr std::size_t numFeatures
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
uint256 registerFeature(std::string const &name, Supported support, VoteBehavior vote)
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
uint256 retireFeature(std::string const &name)
std::string to_string(base_uint< Bits, Tag > const &a)
static bool const readOnlySet
size_t featureToBitsetIndex(uint256 const &f)
bool registrationIsDone()
Tell FeatureCollections when registration is complete.
std::map< std::string, AmendmentSupport > const & allAmendments()
All amendments libxrpl knows about.
std::string featureToName(uint256 const &f)
uint256 bitsetIndexToFeature(size_t i)
std::optional< uint256 > getRegisteredFeature(std::string const &name)
std::size_t hash_value(xrpl::uint256 const &feature)