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/protocol/Feature.h>
22#include <xrpl/protocol/Rules.h>
23
24#include <optional>
25
26namespace ripple {
27
28namespace {
29// Use a static inside a function to help prevent order-of-initialization issues
30LocalValue<std::optional<Rules>>&
31getCurrentTransactionRulesRef()
32{
33 static LocalValue<std::optional<Rules>> r;
34 return r;
35}
36} // namespace
37
40{
41 return *getCurrentTransactionRulesRef();
42}
43
44void
46{
47 *getCurrentTransactionRulesRef() = std::move(r);
48}
49
51{
52private:
56
57public:
60 {
61 }
62
66 STVector256 const& amendments)
68 {
69 set_.reserve(amendments.size());
70 set_.insert(amendments.begin(), amendments.end());
71 }
72
74 presets() const
75 {
76 return presets_;
77 }
78
79 bool
80 enabled(uint256 const& feature) const
81 {
82 if (presets_.count(feature) > 0)
83 return true;
84 return set_.count(feature) > 0;
85 }
86
87 bool
88 operator==(Impl const& other) const
89 {
90 if (!digest_ && !other.digest_)
91 return true;
92 if (!digest_ || !other.digest_)
93 return false;
94 XRPL_ASSERT(
95 presets_ == other.presets_,
96 "ripple::Rules::Impl::operator==(Impl) const : input presets do "
97 "match");
98 return *digest_ == *other.digest_;
99 }
100};
101
103 : impl_(std::make_shared<Impl>(presets))
104{
105}
106
110 STVector256 const& amendments)
111 : impl_(std::make_shared<Impl>(presets, digest, amendments))
112{
113}
114
117{
118 return impl_->presets();
119}
120
121bool
122Rules::enabled(uint256 const& feature) const
123{
124 XRPL_ASSERT(impl_, "ripple::Rules::enabled : initialized");
125
126 // The functionality of the "NonFungibleTokensV1_1" amendment is
127 // precisely the functionality of the following three amendments
128 // so if their status is ever queried individually, we inject an
129 // extra check here to simplify the checking elsewhere.
130 if (feature == featureNonFungibleTokensV1 ||
131 feature == fixNFTokenNegOffer || feature == fixNFTokenDirV1)
132 {
133 if (impl_->enabled(featureNonFungibleTokensV1_1))
134 return true;
135 }
136
137 return impl_->enabled(feature);
138}
139
140bool
141Rules::operator==(Rules const& other) const
142{
143 XRPL_ASSERT(
144 impl_ && other.impl_,
145 "ripple::Rules::operator==(Rules) const : both initialized");
146 if (impl_.get() == other.impl_.get())
147 return true;
148 return *impl_ == *other.impl_;
149}
150
151bool
152Rules::operator!=(Rules const& other) const
153{
154 return !(*this == other);
155}
156} // namespace ripple
std::unordered_set< uint256, hardened_hash<> > set_
Definition: Rules.cpp:53
Impl(std::unordered_set< uint256, beast::uhash<> > const &presets)
Definition: Rules.cpp:58
std::unordered_set< uint256, beast::uhash<> > const & presets() const
Definition: Rules.cpp:74
std::optional< uint256 > digest_
Definition: Rules.cpp:54
std::unordered_set< uint256, beast::uhash<> > const & presets_
Definition: Rules.cpp:55
bool enabled(uint256 const &feature) const
Definition: Rules.cpp:80
bool operator==(Impl const &other) const
Definition: Rules.cpp:88
Impl(std::unordered_set< uint256, beast::uhash<> > const &presets, std::optional< uint256 > const &digest, STVector256 const &amendments)
Definition: Rules.cpp:63
Rules controlling protocol behavior.
Definition: Rules.h:35
std::shared_ptr< Impl const > impl_
Definition: Rules.h:41
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition: Rules.cpp:122
bool operator!=(Rules const &other) const
Definition: Rules.cpp:152
bool operator==(Rules const &) const
Returns true if two rule sets are identical.
Definition: Rules.cpp:141
std::unordered_set< uint256, beast::uhash<> > const & presets() const
Definition: Rules.cpp:116
Rules()=delete
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
void setCurrentTransactionRules(std::optional< Rules > r)
Definition: Rules.cpp:45
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition: tokens.cpp:152
std::optional< Rules > const & getCurrentTransactionRules()
Definition: Rules.cpp:39
STL namespace.