rippled
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 <ripple/protocol/Feature.h>
21 #include <ripple/protocol/Rules.h>
22 
23 namespace ripple {
24 
26 {
27 private:
31 
32 public:
33  explicit Impl(std::unordered_set<uint256, beast::uhash<>> const& presets)
34  : presets_(presets)
35  {
36  }
37 
39  std::unordered_set<uint256, beast::uhash<>> const& presets,
41  STVector256 const& amendments)
42  : digest_(digest), presets_(presets)
43  {
44  set_.reserve(amendments.size());
45  set_.insert(amendments.begin(), amendments.end());
46  }
47 
48  bool
49  enabled(uint256 const& feature) const
50  {
51  if (presets_.count(feature) > 0)
52  return true;
53  return set_.count(feature) > 0;
54  }
55 
56  bool
57  operator==(Impl const& other) const
58  {
59  if (!digest_ && !other.digest_)
60  return true;
61  if (!digest_ || !other.digest_)
62  return false;
63  return *digest_ == *other.digest_;
64  }
65 };
66 
68  : impl_(std::make_shared<Impl>(presets))
69 {
70 }
71 
73  std::unordered_set<uint256, beast::uhash<>> const& presets,
75  STVector256 const& amendments)
76  : impl_(std::make_shared<Impl>(presets, digest, amendments))
77 {
78 }
79 
80 bool
81 Rules::enabled(uint256 const& feature) const
82 {
83  assert(impl_);
84 
85  // The functionality of the "NonFungibleTokensV1_1" amendment is
86  // precisely the functionality of the following three amendments
87  // so if their status is ever queried individually, we inject an
88  // extra check here to simplify the checking elsewhere.
89  if (feature == featureNonFungibleTokensV1 ||
90  feature == fixNFTokenNegOffer || feature == fixNFTokenDirV1)
91  {
92  if (impl_->enabled(featureNonFungibleTokensV1_1))
93  return true;
94  }
95 
96  return impl_->enabled(feature);
97 }
98 
99 bool
100 Rules::operator==(Rules const& other) const
101 {
102  assert(impl_ && other.impl_);
103  if (impl_.get() == other.impl_.get())
104  return true;
105  return *impl_ == *other.impl_;
106 }
107 
108 bool
109 Rules::operator!=(Rules const& other) const
110 {
111  return !(*this == other);
112 }
113 } // namespace ripple
ripple::fixNFTokenNegOffer
const uint256 fixNFTokenNegOffer
ripple::Rules::enabled
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition: Rules.cpp:81
ripple::Rules::Rules
Rules()=delete
ripple::Rules::impl_
std::shared_ptr< Impl const > impl_
Definition: Rules.h:36
std::unordered_set
STL class.
ripple::Rules::Impl::Impl
Impl(std::unordered_set< uint256, beast::uhash<>> const &presets)
Definition: Rules.cpp:33
ripple::Rules::Impl::presets_
std::unordered_set< uint256, beast::uhash<> > const & presets_
Definition: Rules.cpp:30
ripple::Rules::Impl::Impl
Impl(std::unordered_set< uint256, beast::uhash<>> const &presets, std::optional< uint256 > const &digest, STVector256 const &amendments)
Definition: Rules.cpp:38
ripple::Rules::operator!=
bool operator!=(Rules const &other) const
Definition: Rules.cpp:109
ripple::digest
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition: tokens.cpp:47
ripple::base_uint< 256 >
ripple::Rules::Impl
Definition: Rules.cpp:25
ripple::fixNFTokenDirV1
const uint256 fixNFTokenDirV1
ripple::Rules::operator==
bool operator==(Rules const &) const
Returns true if two rule sets are identical.
Definition: Rules.cpp:100
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::featureNonFungibleTokensV1
const uint256 featureNonFungibleTokensV1
ripple::Rules::Impl::enabled
bool enabled(uint256 const &feature) const
Definition: Rules.cpp:49
ripple::Rules::Impl::set_
std::unordered_set< uint256, hardened_hash<> > set_
Definition: Rules.cpp:28
std
STL namespace.
ripple::featureNonFungibleTokensV1_1
const uint256 featureNonFungibleTokensV1_1
ripple::STVector256
Definition: STVector256.h:29
ripple::Rules
Rules controlling protocol behavior.
Definition: Rules.h:33
std::optional
beast::uhash<>
ripple::Rules::Impl::digest_
std::optional< uint256 > digest_
Definition: Rules.cpp:29
ripple::Rules::Impl::operator==
bool operator==(Impl const &other) const
Definition: Rules.cpp:57