rippled
ReadView.h
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 #ifndef RIPPLE_LEDGER_READVIEW_H_INCLUDED
21 #define RIPPLE_LEDGER_READVIEW_H_INCLUDED
22 
23 #include <ripple/basics/FeeUnits.h>
24 #include <ripple/basics/IOUAmount.h>
25 #include <ripple/basics/XRPAmount.h>
26 #include <ripple/basics/chrono.h>
27 #include <ripple/beast/hash/uhash.h>
28 #include <ripple/beast/utility/Journal.h>
29 #include <ripple/ledger/detail/ReadViewFwdRange.h>
30 #include <ripple/protocol/Fees.h>
31 #include <ripple/protocol/Indexes.h>
32 #include <ripple/protocol/LedgerHeader.h>
33 #include <ripple/protocol/Protocol.h>
34 #include <ripple/protocol/Rules.h>
35 #include <ripple/protocol/STAmount.h>
36 #include <ripple/protocol/STLedgerEntry.h>
37 #include <ripple/protocol/STTx.h>
38 #include <cassert>
39 #include <cstdint>
40 #include <memory>
41 #include <optional>
42 #include <unordered_set>
43 
44 namespace ripple {
45 
46 //------------------------------------------------------------------------------
47 
54 class ReadView
55 {
56 public:
57  using tx_type =
59 
60  using key_type = uint256;
61 
63 
64  struct sles_type : detail::ReadViewFwdRange<std::shared_ptr<SLE const>>
65  {
66  explicit sles_type(ReadView const& view);
67  iterator
68  begin() const;
69  iterator
70  end() const;
71  iterator
72  upper_bound(key_type const& key) const;
73  };
74 
76  {
77  explicit txs_type(ReadView const& view);
78  bool
79  empty() const;
80  iterator
81  begin() const;
82  iterator
83  end() const;
84  };
85 
86  virtual ~ReadView() = default;
87 
88  ReadView&
89  operator=(ReadView&& other) = delete;
90  ReadView&
91  operator=(ReadView const& other) = delete;
92 
93  ReadView() : sles(*this), txs(*this)
94  {
95  }
96 
97  ReadView(ReadView const& other) : sles(*this), txs(*this)
98  {
99  }
100 
101  ReadView(ReadView&& other) : sles(*this), txs(*this)
102  {
103  }
104 
106  virtual LedgerInfo const&
107  info() const = 0;
108 
110  virtual bool
111  open() const = 0;
112 
116  {
117  return info().parentCloseTime;
118  }
119 
122  seq() const
123  {
124  return info().seq;
125  }
126 
128  virtual Fees const&
129  fees() const = 0;
130 
132  virtual Rules const&
133  rules() const = 0;
134 
142  virtual bool
143  exists(Keylet const& k) const = 0;
144 
156  succ(
157  key_type const& key,
158  std::optional<key_type> const& last = std::nullopt) const = 0;
159 
174  read(Keylet const& k) const = 0;
175 
176  // Accounts in a payment are not allowed to use assets acquired during that
177  // payment. The PaymentSandbox tracks the debits, credits, and owner count
178  // changes that accounts make during a payment. `balanceHook` adjusts
179  // balances so newly acquired assets are not counted toward the balance.
180  // This is required to support PaymentSandbox.
181  virtual STAmount
183  AccountID const& account,
184  AccountID const& issuer,
185  STAmount const& amount) const
186  {
187  return amount;
188  }
189 
190  // Accounts in a payment are not allowed to use assets acquired during that
191  // payment. The PaymentSandbox tracks the debits, credits, and owner count
192  // changes that accounts make during a payment. `ownerCountHook` adjusts the
193  // ownerCount so it returns the max value of the ownerCount so far.
194  // This is required to support PaymentSandbox.
195  virtual std::uint32_t
196  ownerCountHook(AccountID const& account, std::uint32_t count) const
197  {
198  return count;
199  }
200 
201  // used by the implementation
203  slesBegin() const = 0;
204 
205  // used by the implementation
207  slesEnd() const = 0;
208 
209  // used by the implementation
211  slesUpperBound(key_type const& key) const = 0;
212 
213  // used by the implementation
215  txsBegin() const = 0;
216 
217  // used by the implementation
219  txsEnd() const = 0;
220 
226  virtual bool
227  txExists(key_type const& key) const = 0;
228 
237  virtual tx_type
238  txRead(key_type const& key) const = 0;
239 
240  //
241  // Memberspaces
242  //
243 
250 
251  // The range of transactions
253 };
254 
255 //------------------------------------------------------------------------------
256 
259 {
260 public:
262 
263  DigestAwareReadView() = default;
264  DigestAwareReadView(const DigestAwareReadView&) = default;
265 
271  digest(key_type const& key) const = 0;
272 };
273 
274 //------------------------------------------------------------------------------
275 
276 Rules
277 makeRulesGivenLedger(DigestAwareReadView const& ledger, Rules const& current);
278 
279 Rules
281  DigestAwareReadView const& ledger,
282  std::unordered_set<uint256, beast::uhash<>> const& presets);
283 
284 } // namespace ripple
285 
286 #include <ripple/ledger/detail/ReadViewFwdRange.ipp>
287 
288 #endif
ripple::ReadView::info
virtual LedgerInfo const & info() const =0
Returns information about the ledger.
ripple::detail::ReadViewFwdRange
Definition: ReadViewFwdRange.h:67
ripple::Keylet
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:38
ripple::ReadView::operator=
ReadView & operator=(ReadView &&other)=delete
std::shared_ptr
STL class.
ripple::ReadView::txs_type
Definition: ReadView.h:75
unordered_set
std::pair
ripple::ReadView::sles_type::sles_type
sles_type(ReadView const &view)
Definition: ReadView.cpp:24
ripple::ReadView::key_type
uint256 key_type
Definition: ReadView.h:60
ripple::ReadView::fees
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
ripple::ReadView::txs_type::empty
bool empty() const
Definition: ReadView.cpp:51
ripple::LedgerHeader::seq
LedgerIndex seq
Definition: LedgerHeader.h:41
ripple::ReadView::txsEnd
virtual std::unique_ptr< txs_type::iter_base > txsEnd() const =0
ripple::ReadView::balanceHook
virtual STAmount balanceHook(AccountID const &account, AccountID const &issuer, STAmount const &amount) const
Definition: ReadView.h:182
ripple::ReadView::txExists
virtual bool txExists(key_type const &key) const =0
Returns true if a tx exists in the tx map.
ripple::ReadView::sles_type::upper_bound
iterator upper_bound(key_type const &key) const
Definition: ReadView.cpp:41
ripple::ReadView::slesBegin
virtual std::unique_ptr< sles_type::iter_base > slesBegin() const =0
ripple::ReadView::parentCloseTime
NetClock::time_point parentCloseTime() const
Returns the close time of the previous ledger.
Definition: ReadView.h:115
ripple::ReadView::sles_type::begin
iterator begin() const
Definition: ReadView.cpp:29
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:550
ripple::ReadView::txsBegin
virtual std::unique_ptr< txs_type::iter_base > txsBegin() const =0
ripple::base_uint< 256 >
ripple::LedgerHeader::parentCloseTime
NetClock::time_point parentCloseTime
Definition: LedgerHeader.h:42
ripple::Fees
Reflects the fee settings for a particular ledger.
Definition: protocol/Fees.h:32
ripple::ReadView::ReadView
ReadView()
Definition: ReadView.h:93
ripple::ReadView::slesUpperBound
virtual std::unique_ptr< sles_type::iter_base > slesUpperBound(key_type const &key) const =0
ripple::DigestAwareReadView::DigestAwareReadView
DigestAwareReadView()=default
ripple::ReadView::txs_type::begin
iterator begin() const
Definition: ReadView.cpp:57
ripple::ReadView::slesEnd
virtual std::unique_ptr< sles_type::iter_base > slesEnd() const =0
ripple::DigestAwareReadView
ReadView that associates keys with digests.
Definition: ReadView.h:258
ripple::ReadView::sles_type::end
iterator end() const
Definition: ReadView.cpp:35
ripple::ReadView::txRead
virtual tx_type txRead(key_type const &key) const =0
Read a transaction from the tx map.
ripple::STAmount
Definition: STAmount.h:46
ripple::ReadView::exists
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
std::chrono::time_point
ripple::ReadView::sles
sles_type sles
Iterable range of ledger state items.
Definition: ReadView.h:249
cstdint
std::uint32_t
ripple::ReadView::succ
virtual std::optional< key_type > succ(key_type const &key, std::optional< key_type > const &last=std::nullopt) const =0
Return the key of the next state item.
ripple::ReadView::txs_type::end
iterator end() const
Definition: ReadView.cpp:63
ripple::ReadView::read
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
memory
ripple::LedgerHeader
Information about the notional ledger backing the view.
Definition: LedgerHeader.h:33
ripple::ReadView::tx_type
std::pair< std::shared_ptr< STTx const >, std::shared_ptr< STObject const > > tx_type
Definition: ReadView.h:58
ripple::ReadView::ReadView
ReadView(ReadView &&other)
Definition: ReadView.h:101
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:54
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::ReadView::sles_type
Definition: ReadView.h:64
ripple::ReadView::ReadView
ReadView(ReadView const &other)
Definition: ReadView.h:97
ripple::ReadView::seq
LedgerIndex seq() const
Returns the sequence number of the base ledger.
Definition: ReadView.h:122
ripple::ReadView::rules
virtual Rules const & rules() const =0
Returns the tx processing rules.
cassert
ripple::ReadView::ownerCountHook
virtual std::uint32_t ownerCountHook(AccountID const &account, std::uint32_t count) const
Definition: ReadView.h:196
ripple::makeRulesGivenLedger
Rules makeRulesGivenLedger(DigestAwareReadView const &ledger, Rules const &current)
Definition: ReadView.cpp:69
ripple::Rules
Rules controlling protocol behavior.
Definition: Rules.h:33
ripple::ReadView::~ReadView
virtual ~ReadView()=default
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.
std::unique_ptr
STL class.
ripple::ReadView::open
virtual bool open() const =0
Returns true if this reflects an open ledger.
ripple::ReadView::txs
txs_type txs
Definition: ReadView.h:252
ripple::ReadView::txs_type::txs_type
txs_type(ReadView const &view)
Definition: ReadView.cpp:46