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 || (r->enabled(featureSingleAssetVault) || r->enabled(featureLendingProtocol));
44
45 *getCurrentTransactionRulesRef() = std::move(r);
46}
47
49{
50private:
54
55public:
59
63 STVector256 const& amendments)
65 {
66 set_.reserve(amendments.size());
67 set_.insert(amendments.begin(), amendments.end());
68 }
69
71 presets() const
72 {
73 return presets_;
74 }
75
76 bool
77 enabled(uint256 const& feature) const
78 {
79 if (presets_.count(feature) > 0)
80 return true;
81 return set_.count(feature) > 0;
82 }
83
84 bool
85 operator==(Impl const& other) const
86 {
87 if (!digest_ && !other.digest_)
88 return true;
89 if (!digest_ || !other.digest_)
90 return false;
91 XRPL_ASSERT(
92 presets_ == other.presets_,
93 "xrpl::Rules::Impl::operator==(Impl) const : input presets do "
94 "match");
95 return *digest_ == *other.digest_;
96 }
97};
98
99Rules::Rules(std::unordered_set<uint256, beast::uhash<>> const& presets) : impl_(std::make_shared<Impl>(presets))
100{
101}
102
106 STVector256 const& amendments)
107 : impl_(std::make_shared<Impl>(presets, digest, amendments))
108{
109}
110
113{
114 return impl_->presets();
115}
116
117bool
118Rules::enabled(uint256 const& feature) const
119{
120 XRPL_ASSERT(impl_, "xrpl::Rules::enabled : initialized");
121
122 return impl_->enabled(feature);
123}
124
125bool
126Rules::operator==(Rules const& other) const
127{
128 XRPL_ASSERT(impl_ && other.impl_, "xrpl::Rules::operator==(Rules) const : both initialized");
129 if (impl_.get() == other.impl_.get())
130 return true;
131 return *impl_ == *other.impl_;
132}
133
134bool
135Rules::operator!=(Rules const& other) const
136{
137 return !(*this == other);
138}
139
140bool
142{
143 auto const& rules = getCurrentTransactionRules();
144 return rules && rules->enabled(feature);
145}
146
147} // 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:51
bool operator==(Impl const &other) const
Definition Rules.cpp:85
std::unordered_set< uint256, beast::uhash<> > const & presets() const
Definition Rules.cpp:71
std::unordered_set< uint256, beast::uhash<> > const & presets_
Definition Rules.cpp:53
Impl(std::unordered_set< uint256, beast::uhash<> > const &presets)
Definition Rules.cpp:56
bool enabled(uint256 const &feature) const
Definition Rules.cpp:77
Impl(std::unordered_set< uint256, beast::uhash<> > const &presets, std::optional< uint256 > const &digest, STVector256 const &amendments)
Definition Rules.cpp:60
std::optional< uint256 > digest_
Definition Rules.cpp:52
Rules controlling protocol behavior.
Definition Rules.h:19
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:118
std::unordered_set< uint256, beast::uhash<> > const & presets() const
Definition Rules.cpp:112
Rules()=delete
std::shared_ptr< Impl const > impl_
Definition Rules.h:25
bool operator!=(Rules const &other) const
Definition Rules.cpp:135
bool operator==(Rules const &) const
Returns true if two rule sets are identical.
Definition Rules.cpp:126
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:137
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:141