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 =
43 !r || (r->enabled(featureSingleAssetVault) || r->enabled(featureLendingProtocol));
45
46 *getCurrentTransactionRulesRef() = std::move(r);
47}
48
50{
51private:
55
56public:
60
64 STVector256 const& amendments)
66 {
67 set_.reserve(amendments.size());
68 set_.insert(amendments.begin(), amendments.end());
69 }
70
72 presets() const
73 {
74 return presets_;
75 }
76
77 bool
78 enabled(uint256 const& feature) const
79 {
80 if (presets_.count(feature) > 0)
81 return true;
82 return set_.count(feature) > 0;
83 }
84
85 bool
86 operator==(Impl const& other) const
87 {
88 if (!digest_ && !other.digest_)
89 return true;
90 if (!digest_ || !other.digest_)
91 return false;
92 XRPL_ASSERT(
93 presets_ == other.presets_,
94 "xrpl::Rules::Impl::operator==(Impl) const : input presets do "
95 "match");
96 return *digest_ == *other.digest_;
97 }
98};
99
101 : impl_(std::make_shared<Impl>(presets))
102{
103}
104
108 STVector256 const& amendments)
109 : impl_(std::make_shared<Impl>(presets, digest, amendments))
110{
111}
112
115{
116 return impl_->presets();
117}
118
119bool
120Rules::enabled(uint256 const& feature) const
121{
122 XRPL_ASSERT(impl_, "xrpl::Rules::enabled : initialized");
123
124 return impl_->enabled(feature);
125}
126
127bool
128Rules::operator==(Rules const& other) const
129{
130 XRPL_ASSERT(impl_ && other.impl_, "xrpl::Rules::operator==(Rules) const : both initialized");
131 if (impl_.get() == other.impl_.get())
132 return true;
133 return *impl_ == *other.impl_;
134}
135
136bool
137Rules::operator!=(Rules const& other) const
138{
139 return !(*this == other);
140}
141
142bool
144{
145 auto const& rules = getCurrentTransactionRules();
146 return rules && rules->enabled(feature);
147}
148
149} // namespace xrpl
static void setMantissaScale(MantissaRange::mantissa_scale scale)
Changes which mantissa scale is used for normalization.
Definition Number.cpp:51
std::unordered_set< uint256, hardened_hash<> > set_
Definition Rules.cpp:52
bool operator==(Impl const &other) const
Definition Rules.cpp:86
std::unordered_set< uint256, beast::uhash<> > const & presets() const
Definition Rules.cpp:72
std::unordered_set< uint256, beast::uhash<> > const & presets_
Definition Rules.cpp:54
Impl(std::unordered_set< uint256, beast::uhash<> > const &presets)
Definition Rules.cpp:57
bool enabled(uint256 const &feature) const
Definition Rules.cpp:78
Impl(std::unordered_set< uint256, beast::uhash<> > const &presets, std::optional< uint256 > const &digest, STVector256 const &amendments)
Definition Rules.cpp:61
std::optional< uint256 > digest_
Definition Rules.cpp:53
Rules controlling protocol behavior.
Definition Rules.h:18
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:120
std::unordered_set< uint256, beast::uhash<> > const & presets() const
Definition Rules.cpp:114
Rules()=delete
std::shared_ptr< Impl const > impl_
Definition Rules.h:24
bool operator!=(Rules const &other) const
Definition Rules.cpp:137
bool operator==(Rules const &) const
Returns true if two rule sets are identical.
Definition Rules.cpp:128
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
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:143