rippled
ReadView.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/ledger/ReadView.h>
21 
22 namespace ripple {
23 
25 {
26 private:
30 
31 public:
32  explicit Impl(std::unordered_set<uint256, beast::uhash<>> const& presets)
33  : presets_(presets)
34  {
35  }
36 
37  explicit Impl(
38  DigestAwareReadView const& ledger,
39  std::unordered_set<uint256, beast::uhash<>> const& presets)
40  : presets_(presets)
41  {
42  auto const k = keylet::amendments();
43  digest_ = ledger.digest(k.key);
44  if (!digest_)
45  return;
46  auto const sle = ledger.read(k);
47  if (!sle)
48  {
49  // LogicError() ?
50  return;
51  }
52 
53  for (auto const& item : sle->getFieldV256(sfAmendments))
54  set_.insert(item);
55  }
56 
57  bool
58  enabled(uint256 const& feature) const
59  {
60  if (presets_.count(feature) > 0)
61  return true;
62  return set_.count(feature) > 0;
63  }
64 
65  bool
66  changed(DigestAwareReadView const& ledger) const
67  {
68  auto const digest = ledger.digest(keylet::amendments().key);
69  if (!digest && !digest_)
70  return false;
71  if (!digest || !digest_)
72  return true;
73  return *digest != *digest_;
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  return *digest_ == *other.digest_;
84  }
85 };
86 
87 //------------------------------------------------------------------------------
88 
90  DigestAwareReadView const& ledger,
91  std::unordered_set<uint256, beast::uhash<>> const& presets)
92  : impl_(std::make_shared<Impl>(ledger, presets))
93 {
94 }
95 
97  : impl_(std::make_shared<Impl>(presets))
98 {
99 }
100 
101 bool
102 Rules::enabled(uint256 const& id) const
103 {
104  assert(impl_);
105  return impl_->enabled(id);
106 }
107 
108 bool
110 {
111  assert(impl_);
112  return impl_->changed(ledger);
113 }
114 
115 bool
116 Rules::operator==(Rules const& other) const
117 {
118  assert(impl_ && other.impl_);
119  if (impl_.get() == other.impl_.get())
120  return true;
121  return *impl_ == *other.impl_;
122 }
123 
124 //------------------------------------------------------------------------------
125 
126 ReadView::sles_type::sles_type(ReadView const& view) : ReadViewFwdRange(view)
127 {
128 }
129 
130 auto
131 ReadView::sles_type::begin() const -> iterator
132 {
133  return iterator(view_, view_->slesBegin());
134 }
135 
136 auto
137 ReadView::sles_type::end() const -> iterator
138 {
139  return iterator(view_, view_->slesEnd());
140 }
141 
142 auto
143 ReadView::sles_type::upper_bound(key_type const& key) const -> iterator
144 {
145  return iterator(view_, view_->slesUpperBound(key));
146 }
147 
148 ReadView::txs_type::txs_type(ReadView const& view) : ReadViewFwdRange(view)
149 {
150 }
151 
152 bool
154 {
155  return begin() == end();
156 }
157 
158 auto
159 ReadView::txs_type::begin() const -> iterator
160 {
161  return iterator(view_, view_->txsBegin());
162 }
163 
164 auto
165 ReadView::txs_type::end() const -> iterator
166 {
167  return iterator(view_, view_->txsEnd());
168 }
169 
170 } // namespace ripple
ripple::Rules::Impl::Impl
Impl(DigestAwareReadView const &ledger, std::unordered_set< uint256, beast::uhash<>> const &presets)
Definition: ReadView.cpp:37
ripple::keylet::amendments
Keylet const & amendments() noexcept
The index of the amendment table.
Definition: Indexes.cpp:160
ripple::Rules::Rules
Rules()=delete
ripple::Rules::impl_
std::shared_ptr< Impl const > impl_
Definition: ReadView.h:134
std::unordered_set
STL class.
ripple::ReadView::sles_type::sles_type
sles_type(ReadView const &view)
Definition: ReadView.cpp:126
ripple::ReadView::txs_type::empty
bool empty() const
Definition: ReadView.cpp:153
ripple::Rules::Impl::Impl
Impl(std::unordered_set< uint256, beast::uhash<>> const &presets)
Definition: ReadView.cpp:32
ripple::Rules::Impl::presets_
std::unordered_set< uint256, beast::uhash<> > const & presets_
Definition: ReadView.cpp:29
ripple::ReadView::sles_type::upper_bound
iterator upper_bound(key_type const &key) const
Definition: ReadView.cpp:143
ripple::ReadView::sles_type::begin
iterator begin() const
Definition: ReadView.cpp:131
ripple::Rules::Impl::changed
bool changed(DigestAwareReadView const &ledger) const
Definition: ReadView.cpp:66
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: ReadView.cpp:24
ripple::ReadView::txs_type::begin
iterator begin() const
Definition: ReadView.cpp:159
ripple::DigestAwareReadView
ReadView that associates keys with digests.
Definition: ReadView.h:396
ripple::ReadView::sles_type::end
iterator end() const
Definition: ReadView.cpp:137
ripple::Rules::changed
bool changed(DigestAwareReadView const &ledger) const
Returns true if these rules don't match the ledger.
Definition: ReadView.cpp:109
ripple::Rules::enabled
bool enabled(uint256 const &id) const
Returns true if a feature is enabled.
Definition: ReadView.cpp:102
ripple::ReadView::txs_type::end
iterator end() const
Definition: ReadView.cpp:165
ripple::ReadView::read
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
ripple::Rules::operator==
bool operator==(Rules const &) const
Returns true if two rule sets are identical.
Definition: ReadView.cpp:116
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:192
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: ReadView.cpp:58
ripple::Rules::Impl::set_
std::unordered_set< uint256, hardened_hash<> > set_
Definition: ReadView.cpp:27
std
STL namespace.
ripple::Rules
Rules controlling protocol behavior.
Definition: ReadView.h:131
std::optional
beast::uhash<>
ripple::DigestAwareReadView::digest
virtual std::optional< digest_type > digest(key_type const &key) const =0
Return the digest associated with the key.
ripple::Rules::Impl::digest_
std::optional< uint256 > digest_
Definition: ReadView.cpp:28
ripple::Rules::Impl::operator==
bool operator==(Impl const &other) const
Definition: ReadView.cpp:77
ripple::sfAmendments
const SF_VECTOR256 sfAmendments
ripple::ReadView::txs_type::txs_type
txs_type(ReadView const &view)
Definition: ReadView.cpp:148