rippled
Loading...
Searching...
No Matches
Rules.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/basics/LocalValue.h>
21#include <xrpl/basics/base_uint.h>
22#include <xrpl/basics/hardened_hash.h>
23#include <xrpl/beast/hash/uhash.h>
24#include <xrpl/beast/utility/instrumentation.h>
25#include <xrpl/protocol/Feature.h>
26#include <xrpl/protocol/Rules.h>
27#include <xrpl/protocol/STVector256.h>
28
29#include <memory>
30#include <optional>
31#include <unordered_set>
32#include <utility>
33
34namespace ripple {
35
36namespace {
37// Use a static inside a function to help prevent order-of-initialization issues
38LocalValue<std::optional<Rules>>&
39getCurrentTransactionRulesRef()
40{
41 static LocalValue<std::optional<Rules>> r;
42 return r;
43}
44} // namespace
45
48{
49 return *getCurrentTransactionRulesRef();
50}
51
52void
54{
55 *getCurrentTransactionRulesRef() = std::move(r);
56}
57
59{
60private:
64
65public:
70
74 STVector256 const& amendments)
76 {
77 set_.reserve(amendments.size());
78 set_.insert(amendments.begin(), amendments.end());
79 }
80
82 presets() const
83 {
84 return presets_;
85 }
86
87 bool
88 enabled(uint256 const& feature) const
89 {
90 if (presets_.count(feature) > 0)
91 return true;
92 return set_.count(feature) > 0;
93 }
94
95 bool
96 operator==(Impl const& other) const
97 {
98 if (!digest_ && !other.digest_)
99 return true;
100 if (!digest_ || !other.digest_)
101 return false;
102 XRPL_ASSERT(
103 presets_ == other.presets_,
104 "ripple::Rules::Impl::operator==(Impl) const : input presets do "
105 "match");
106 return *digest_ == *other.digest_;
107 }
108};
109
111 : impl_(std::make_shared<Impl>(presets))
112{
113}
114
118 STVector256 const& amendments)
119 : impl_(std::make_shared<Impl>(presets, digest, amendments))
120{
121}
122
125{
126 return impl_->presets();
127}
128
129bool
130Rules::enabled(uint256 const& feature) const
131{
132 XRPL_ASSERT(impl_, "ripple::Rules::enabled : initialized");
133
134 return impl_->enabled(feature);
135}
136
137bool
138Rules::operator==(Rules const& other) const
139{
140 XRPL_ASSERT(
141 impl_ && other.impl_,
142 "ripple::Rules::operator==(Rules) const : both initialized");
143 if (impl_.get() == other.impl_.get())
144 return true;
145 return *impl_ == *other.impl_;
146}
147
148bool
149Rules::operator!=(Rules const& other) const
150{
151 return !(*this == other);
152}
153
154bool
156{
157 auto const& rules = getCurrentTransactionRules();
158 return rules && rules->enabled(feature);
159}
160
161} // namespace ripple
std::unordered_set< uint256, hardened_hash<> > set_
Definition Rules.cpp:61
Impl(std::unordered_set< uint256, beast::uhash<> > const &presets)
Definition Rules.cpp:66
std::unordered_set< uint256, beast::uhash<> > const & presets() const
Definition Rules.cpp:82
std::optional< uint256 > digest_
Definition Rules.cpp:62
std::unordered_set< uint256, beast::uhash<> > const & presets_
Definition Rules.cpp:63
bool enabled(uint256 const &feature) const
Definition Rules.cpp:88
bool operator==(Impl const &other) const
Definition Rules.cpp:96
Impl(std::unordered_set< uint256, beast::uhash<> > const &presets, std::optional< uint256 > const &digest, STVector256 const &amendments)
Definition Rules.cpp:71
Rules controlling protocol behavior.
Definition Rules.h:38
std::shared_ptr< Impl const > impl_
Definition Rules.h:44
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:130
bool operator!=(Rules const &other) const
Definition Rules.cpp:149
bool operator==(Rules const &) const
Returns true if two rule sets are identical.
Definition Rules.cpp:138
std::unordered_set< uint256, beast::uhash<> > const & presets() const
Definition Rules.cpp:124
Rules()=delete
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
bool isFeatureEnabled(uint256 const &feature)
Definition Rules.cpp:155
void setCurrentTransactionRules(std::optional< Rules > r)
Definition Rules.cpp:53
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition tokens.cpp:156
std::optional< Rules > const & getCurrentTransactionRules()
Definition Rules.cpp:47
STL namespace.