xrpld
Loading...
Searching...
No Matches
Rules.cpp
1#include <xrpl/protocol/Rules.h>
2
3#include <xrpl/basics/LocalValue.h>
4#include <xrpl/basics/Number.h>
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/basics/hardened_hash.h>
7#include <xrpl/beast/hash/uhash.h>
8#include <xrpl/beast/utility/instrumentation.h>
9#include <xrpl/protocol/Feature.h>
10#include <xrpl/protocol/STVector256.h>
11
12#include <memory>
13#include <optional>
14#include <unordered_set>
15#include <utility>
16
17namespace xrpl {
18
19namespace {
20// Use a static inside a function to help prevent order-of-initialization issues
22getCurrentTransactionRulesRef()
23{
25 return kR;
26}
27} // namespace
28
29std::optional<Rules> const&
31{
32 return *getCurrentTransactionRulesRef();
33}
34
35void
37{
38 // Make global changes associated with the rules before the value is moved.
39 // Push the appropriate setting, instead of having the class pull every time
40 // the value is needed. That could get expensive fast.
41
42 // If any new conditions with new amendments are added, those amendments must also be added to
43 // useRulesGuards.
44 bool const enableVaultNumbers =
45 !r || (r->enabled(featureSingleAssetVault) || r->enabled(featureLendingProtocol));
46 bool const enableCuspRoundingFix = !r || r->enabled(fixCleanup3_2_0);
47 XRPL_ASSERT(
48 !r || useRulesGuards(*r) == (enableCuspRoundingFix || enableVaultNumbers),
49 "setCurrentTransactionRules : rule decisions match");
50
51 // Declare the range this way to keep clang-tidy from complaining
52 auto const range = [enableCuspRoundingFix, enableVaultNumbers]() {
53 if (enableVaultNumbers)
54 {
55 if (enableCuspRoundingFix)
56 {
58 }
60 }
62 }();
64
65 *getCurrentTransactionRulesRef() = std::move(r);
66}
67
68bool
69useRulesGuards(Rules const& rules)
70{
71 // The list of amendments used here - to decide whether to create a RulesGuard - must be a
72 // superset of the list used to figure out which mantissa scale to use in
73 // setCurrentTransactionRules. Additional amendments can be added if desired.
74 //
75 // As soon as any one of these amendments is retired, this whole function can be removed, along
76 // with createGuards, and any other callers, and the first set of guards can be created directly
77 // at the call site, without using optional.
78 return rules.enabled(fixCleanup3_2_0) || rules.enabled(featureSingleAssetVault) ||
79 rules.enabled(featureLendingProtocol);
80}
81
82void
84 Rules const& rules,
87{
88 if (useRulesGuards(rules))
89 {
90 // raii classes for the current ledger rules.
91 rulesGuard.emplace(rules);
92 }
93 else
94 {
95 // Without those features enabled, always use the old number rules.
97 }
98}
99
101{
102private:
106
107public:
111
115 STVector256 const& amendments)
117 {
118 set_.reserve(amendments.size());
119 set_.insert(amendments.begin(), amendments.end());
120 }
121
123 presets() const
124 {
125 return presets_;
126 }
127
128 [[nodiscard]] bool
129 enabled(uint256 const& feature) const
130 {
131 if (presets_.contains(feature))
132 return true;
133 return set_.contains(feature);
134 }
135
136 bool
137 operator==(Impl const& other) const
138 {
139 if (!digest_ && !other.digest_)
140 return true;
141 if (!digest_ || !other.digest_)
142 return false;
143 XRPL_ASSERT(
144 presets_ == other.presets_,
145 "xrpl::Rules::Impl::operator==(Impl) const : input presets do "
146 "match");
147 return *digest_ == *other.digest_;
148 }
149};
150
155
159 STVector256 const& amendments)
160 : impl_(std::make_shared<Impl>(presets, digest, amendments))
161{
162}
163
166{
167 return impl_->presets();
168}
169
170bool
171Rules::enabled(uint256 const& feature) const
172{
173 XRPL_ASSERT(impl_, "xrpl::Rules::enabled : initialized");
174
175 return impl_->enabled(feature);
176}
177
178bool
179Rules::operator==(Rules const& other) const
180{
181 XRPL_ASSERT(impl_ && other.impl_, "xrpl::Rules::operator==(Rules) const : both initialized");
182 if (impl_.get() == other.impl_.get())
183 return true;
184 return *impl_ == *other.impl_;
185}
186
187bool
188Rules::operator!=(Rules const& other) const
189{
190 return !(*this == other);
191}
192
193bool
194isFeatureEnabled(uint256 const& feature, bool resultIfNoRules)
195{
196 auto const& rules = getCurrentTransactionRules();
197 if (!rules)
198 return resultIfNoRules;
199 return rules->enabled(feature);
200}
201
202bool
204{
205 return isFeatureEnabled(feature, false);
206}
207
208} // namespace xrpl
static void setMantissaScale(MantissaRange::MantissaScale scale)
Changes which mantissa scale is used for normalization.
Definition Number.cpp:123
bool operator==(Impl const &other) const
Definition Rules.cpp:137
Impl(std::unordered_set< uint256, beast::Uhash<> > const &presets)
Definition Rules.cpp:108
bool enabled(uint256 const &feature) const
Definition Rules.cpp:129
std::unordered_set< uint256, beast::Uhash<> > const & presets() const
Definition Rules.cpp:123
std::unordered_set< uint256, beast::Uhash<> > const & presets_
Definition Rules.cpp:105
Impl(std::unordered_set< uint256, beast::Uhash<> > const &presets, std::optional< uint256 > const &digest, STVector256 const &amendments)
Definition Rules.cpp:112
std::unordered_set< uint256, HardenedHash<> > set_
Definition Rules.cpp:103
std::optional< uint256 > digest_
Definition Rules.cpp:104
Rules controlling protocol behavior.
Definition Rules.h:33
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:171
std::unordered_set< uint256, beast::Uhash<> > const & presets() const
Definition Rules.cpp:165
Rules(Rules const &)=default
Rules()=delete
std::shared_ptr< Impl const > impl_
Definition Rules.h:39
bool operator!=(Rules const &other) const
Definition Rules.cpp:188
bool operator==(Rules const &) const
Returns true if two rule sets are identical.
Definition Rules.cpp:179
T emplace(T... args)
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool isFeatureEnabled(uint256 const &feature, bool resultIfNoRules)
Check whether a feature is enabled in the current ledger rules.
Definition Rules.cpp:194
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition tokens.cpp:139
bool useRulesGuards(Rules const &rules)
Definition Rules.cpp:69
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition RangeSet.h:34
void createGuards(Rules const &rules, std::optional< CurrentTransactionRulesGuard > &rulesGuard, std::optional< NumberMantissaScaleGuard > &mantissaScaleGuard)
Definition Rules.cpp:83
std::optional< Rules > const & getCurrentTransactionRules()
Definition Rules.cpp:30
void setCurrentTransactionRules(std::optional< Rules > r)
Definition Rules.cpp:36
BaseUInt< 256 > uint256
Definition base_uint.h:562