rippled
Loading...
Searching...
No Matches
Rules.cpp
1#include <xrpl/protocol/Rules.h>
2// Do not remove. Forces Rules.h to stay first, to verify it can compile
3// without any hidden dependencies
4#include <xrpl/basics/LocalValue.h>
5#include <xrpl/basics/Number.h>
6#include <xrpl/basics/base_uint.h>
7#include <xrpl/basics/hardened_hash.h>
8#include <xrpl/beast/hash/uhash.h>
9#include <xrpl/beast/utility/instrumentation.h>
10#include <xrpl/protocol/Feature.h>
11#include <xrpl/protocol/STVector256.h>
12
13#include <memory>
14#include <optional>
15#include <unordered_set>
16#include <utility>
17
18namespace xrpl {
19
20namespace {
21// Use a static inside a function to help prevent order-of-initialization issues
22LocalValue<std::optional<Rules>>&
23getCurrentTransactionRulesRef()
24{
25 static LocalValue<std::optional<Rules>> r;
26 return r;
27}
28} // namespace
29
32{
33 return *getCurrentTransactionRulesRef();
34}
35
36void
38{
39 // Make global changes associated with the rules before the value is moved.
40 // Push the appropriate setting, instead of having the class pull every time
41 // the value is needed. That could get expensive fast.
42 bool enableLargeNumbers = !r ||
43 (r->enabled(featureSingleAssetVault) ||
44 r->enabled(featureLendingProtocol));
46 enableLargeNumbers ? MantissaRange::large : MantissaRange::small);
47
48 *getCurrentTransactionRulesRef() = std::move(r);
49}
50
52{
53private:
57
58public:
63
67 STVector256 const& amendments)
69 {
70 set_.reserve(amendments.size());
71 set_.insert(amendments.begin(), amendments.end());
72 }
73
75 presets() const
76 {
77 return presets_;
78 }
79
80 bool
81 enabled(uint256 const& feature) const
82 {
83 if (presets_.count(feature) > 0)
84 return true;
85 return set_.count(feature) > 0;
86 }
87
88 bool
89 operator==(Impl const& other) const
90 {
91 if (!digest_ && !other.digest_)
92 return true;
93 if (!digest_ || !other.digest_)
94 return false;
95 XRPL_ASSERT(
96 presets_ == other.presets_,
97 "xrpl::Rules::Impl::operator==(Impl) const : input presets do "
98 "match");
99 return *digest_ == *other.digest_;
100 }
101};
102
104 : impl_(std::make_shared<Impl>(presets))
105{
106}
107
111 STVector256 const& amendments)
112 : impl_(std::make_shared<Impl>(presets, digest, amendments))
113{
114}
115
118{
119 return impl_->presets();
120}
121
122bool
123Rules::enabled(uint256 const& feature) const
124{
125 XRPL_ASSERT(impl_, "xrpl::Rules::enabled : initialized");
126
127 return impl_->enabled(feature);
128}
129
130bool
131Rules::operator==(Rules const& other) const
132{
133 XRPL_ASSERT(
134 impl_ && other.impl_,
135 "xrpl::Rules::operator==(Rules) const : both initialized");
136 if (impl_.get() == other.impl_.get())
137 return true;
138 return *impl_ == *other.impl_;
139}
140
141bool
142Rules::operator!=(Rules const& other) const
143{
144 return !(*this == other);
145}
146
147bool
149{
150 auto const& rules = getCurrentTransactionRules();
151 return rules && rules->enabled(feature);
152}
153
154} // namespace xrpl
static void setMantissaScale(MantissaRange::mantissa_scale scale)
Changes which mantissa scale is used for normalization.
Definition Number.cpp:52
std::unordered_set< uint256, hardened_hash<> > set_
Definition Rules.cpp:54
bool operator==(Impl const &other) const
Definition Rules.cpp:89
std::unordered_set< uint256, beast::uhash<> > const & presets() const
Definition Rules.cpp:75
std::unordered_set< uint256, beast::uhash<> > const & presets_
Definition Rules.cpp:56
Impl(std::unordered_set< uint256, beast::uhash<> > const &presets)
Definition Rules.cpp:59
bool enabled(uint256 const &feature) const
Definition Rules.cpp:81
Impl(std::unordered_set< uint256, beast::uhash<> > const &presets, std::optional< uint256 > const &digest, STVector256 const &amendments)
Definition Rules.cpp:64
std::optional< uint256 > digest_
Definition Rules.cpp:55
Rules controlling protocol behavior.
Definition Rules.h:19
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:123
std::unordered_set< uint256, beast::uhash<> > const & presets() const
Definition Rules.cpp:117
Rules()=delete
std::shared_ptr< Impl const > impl_
Definition Rules.h:25
bool operator!=(Rules const &other) const
Definition Rules.cpp:142
bool operator==(Rules const &) const
Returns true if two rule sets are identical.
Definition Rules.cpp:131
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition tokens.cpp:138
std::optional< Rules > const & getCurrentTransactionRules()
Definition Rules.cpp:31
void setCurrentTransactionRules(std::optional< Rules > r)
Definition Rules.cpp:37
bool isFeatureEnabled(uint256 const &feature)
Definition Rules.cpp:148