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/Rules.h>
21 
22 #include <ripple/protocol/Indexes.h>
23 
24 namespace ripple {
25 
27 {
28 private:
32 
33 public:
34  explicit Impl(std::unordered_set<uint256, beast::uhash<>> const& presets)
35  : presets_(presets)
36  {
37  }
38 
40  std::unordered_set<uint256, beast::uhash<>> const& presets,
42  STVector256 const& amendments)
43  : presets_(presets)
44  {
45  digest_ = digest;
46  set_.reserve(amendments.size());
47  set_.insert(amendments.begin(), amendments.end());
48  }
49 
50  bool
51  enabled(uint256 const& feature) const
52  {
53  if (presets_.count(feature) > 0)
54  return true;
55  return set_.count(feature) > 0;
56  }
57 
58  bool
59  operator==(Impl const& other) const
60  {
61  if (!digest_ && !other.digest_)
62  return true;
63  if (!digest_ || !other.digest_)
64  return false;
65  return *digest_ == *other.digest_;
66  }
67 };
68 
70  : impl_(std::make_shared<Impl>(presets))
71 {
72 }
73 
75  std::unordered_set<uint256, beast::uhash<>> const& presets,
77  STVector256 const& amendments)
78  : impl_(std::make_shared<Impl>(presets, digest, amendments))
79 {
80 }
81 
82 bool
83 Rules::enabled(uint256 const& feature) const
84 {
85  assert(impl_);
86  return impl_->enabled(feature);
87 }
88 
89 bool
90 Rules::operator==(Rules const& other) const
91 {
92  assert(impl_ && other.impl_);
93  if (impl_.get() == other.impl_.get())
94  return true;
95  return *impl_ == *other.impl_;
96 }
97 
98 bool
99 Rules::operator!=(Rules const& other) const
100 {
101  return !(*this == other);
102 }
103 } // namespace ripple
ripple::Rules::enabled
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition: Rules.cpp:83
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:34
ripple::Rules::Impl::presets_
std::unordered_set< uint256, beast::uhash<> > const & presets_
Definition: Rules.cpp:31
ripple::Rules::Impl::Impl
Impl(std::unordered_set< uint256, beast::uhash<>> const &presets, std::optional< uint256 > const &digest, STVector256 const &amendments)
Definition: Rules.cpp:39
ripple::Rules::operator!=
bool operator!=(Rules const &other) const
Definition: Rules.cpp:99
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:26
ripple::Rules::operator==
bool operator==(Rules const &) const
Returns true if two rule sets are identical.
Definition: Rules.cpp:90
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Rules::Impl::enabled
bool enabled(uint256 const &feature) const
Definition: Rules.cpp:51
ripple::Rules::Impl::set_
std::unordered_set< uint256, hardened_hash<> > set_
Definition: Rules.cpp:29
std
STL namespace.
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:30
ripple::Rules::Impl::operator==
bool operator==(Impl const &other) const
Definition: Rules.cpp:59