rippled
Loading...
Searching...
No Matches
Rules.cpp
1#include <xrpl/basics/LocalValue.h>
2#include <xrpl/basics/base_uint.h>
3#include <xrpl/basics/hardened_hash.h>
4#include <xrpl/beast/hash/uhash.h>
5#include <xrpl/beast/utility/instrumentation.h>
6#include <xrpl/protocol/Feature.h>
7#include <xrpl/protocol/Rules.h>
8#include <xrpl/protocol/STVector256.h>
9
10#include <memory>
11#include <optional>
12#include <unordered_set>
13#include <utility>
14
15namespace ripple {
16
17namespace {
18// Use a static inside a function to help prevent order-of-initialization issues
19LocalValue<std::optional<Rules>>&
20getCurrentTransactionRulesRef()
21{
22 static LocalValue<std::optional<Rules>> r;
23 return r;
24}
25} // namespace
26
29{
30 return *getCurrentTransactionRulesRef();
31}
32
33void
35{
36 *getCurrentTransactionRulesRef() = std::move(r);
37}
38
40{
41private:
45
46public:
51
55 STVector256 const& amendments)
57 {
58 set_.reserve(amendments.size());
59 set_.insert(amendments.begin(), amendments.end());
60 }
61
63 presets() const
64 {
65 return presets_;
66 }
67
68 bool
69 enabled(uint256 const& feature) const
70 {
71 if (presets_.count(feature) > 0)
72 return true;
73 return set_.count(feature) > 0;
74 }
75
76 bool
77 operator==(Impl const& other) const
78 {
79 if (!digest_ && !other.digest_)
80 return true;
81 if (!digest_ || !other.digest_)
82 return false;
83 XRPL_ASSERT(
84 presets_ == other.presets_,
85 "ripple::Rules::Impl::operator==(Impl) const : input presets do "
86 "match");
87 return *digest_ == *other.digest_;
88 }
89};
90
92 : impl_(std::make_shared<Impl>(presets))
93{
94}
95
99 STVector256 const& amendments)
100 : impl_(std::make_shared<Impl>(presets, digest, amendments))
101{
102}
103
106{
107 return impl_->presets();
108}
109
110bool
111Rules::enabled(uint256 const& feature) const
112{
113 XRPL_ASSERT(impl_, "ripple::Rules::enabled : initialized");
114
115 return impl_->enabled(feature);
116}
117
118bool
119Rules::operator==(Rules const& other) const
120{
121 XRPL_ASSERT(
122 impl_ && other.impl_,
123 "ripple::Rules::operator==(Rules) const : both initialized");
124 if (impl_.get() == other.impl_.get())
125 return true;
126 return *impl_ == *other.impl_;
127}
128
129bool
130Rules::operator!=(Rules const& other) const
131{
132 return !(*this == other);
133}
134
135bool
137{
138 auto const& rules = getCurrentTransactionRules();
139 return rules && rules->enabled(feature);
140}
141
142} // namespace ripple
std::unordered_set< uint256, hardened_hash<> > set_
Definition Rules.cpp:42
Impl(std::unordered_set< uint256, beast::uhash<> > const &presets)
Definition Rules.cpp:47
std::unordered_set< uint256, beast::uhash<> > const & presets() const
Definition Rules.cpp:63
std::optional< uint256 > digest_
Definition Rules.cpp:43
std::unordered_set< uint256, beast::uhash<> > const & presets_
Definition Rules.cpp:44
bool enabled(uint256 const &feature) const
Definition Rules.cpp:69
bool operator==(Impl const &other) 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:52
Rules controlling protocol behavior.
Definition Rules.h:19
std::shared_ptr< Impl const > impl_
Definition Rules.h:25
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:111
bool operator!=(Rules const &other) const
Definition Rules.cpp:130
bool operator==(Rules const &) const
Returns true if two rule sets are identical.
Definition Rules.cpp:119
std::unordered_set< uint256, beast::uhash<> > const & presets() const
Definition Rules.cpp:105
Rules()=delete
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
bool isFeatureEnabled(uint256 const &feature)
Definition Rules.cpp:136
void setCurrentTransactionRules(std::optional< Rules > r)
Definition Rules.cpp:34
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:28
STL namespace.