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/Indexes.h>
31 #include <ripple/protocol/Protocol.h>
32 #include <ripple/protocol/Rules.h>
33 #include <ripple/protocol/STAmount.h>
34 #include <ripple/protocol/STLedgerEntry.h>
35 #include <ripple/protocol/STTx.h>
36 #include <cassert>
37 #include <cstdint>
38 #include <memory>
39 #include <optional>
40 #include <unordered_set>
41 
42 namespace ripple {
43 
49 struct Fees
50 {
51  XRPAmount base{0}; // Reference tx cost (drops)
52  XRPAmount reserve{0}; // Reserve base (drops)
53  XRPAmount increment{0}; // Reserve increment (drops)
54 
55  explicit Fees() = default;
56  Fees(Fees const&) = default;
57  Fees&
58  operator=(Fees const&) = default;
59 
65  XRPAmount
66  accountReserve(std::size_t ownerCount) const
67  {
68  return reserve + ownerCount * increment;
69  }
70 };
71 
72 //------------------------------------------------------------------------------
73 
75 struct LedgerInfo
76 {
77  explicit LedgerInfo() = default;
78 
79  //
80  // For all ledgers
81  //
82 
85 
86  //
87  // For closed ledgers
88  //
89 
90  // Closed means "tx set already determined"
91  uint256 hash = beast::zero;
92  uint256 txHash = beast::zero;
93  uint256 accountHash = beast::zero;
94  uint256 parentHash = beast::zero;
95 
96  XRPAmount drops = beast::zero;
97 
98  // If validated is false, it means "not yet validated."
99  // Once validated is true, it will never be set false at a later time.
100  // VFALCO TODO Make this not mutable
101  bool mutable validated = false;
102  bool accepted = false;
103 
104  // flags indicating how this ledger close took place
105  int closeFlags = 0;
106 
107  // the resolution for this ledger close time (2-120 seconds)
109 
110  // For closed ledgers, the time the ledger
111  // closed. For open ledgers, the time the ledger
112  // will close if there's no transactions.
113  //
115 };
116 
117 //------------------------------------------------------------------------------
118 
125 class ReadView
126 {
127 public:
128  using tx_type =
130 
131  using key_type = uint256;
132 
134 
135  struct sles_type : detail::ReadViewFwdRange<std::shared_ptr<SLE const>>
136  {
137  explicit sles_type(ReadView const& view);
138  iterator
139  begin() const;
140  iterator
141  end() const;
142  iterator
143  upper_bound(key_type const& key) const;
144  };
145 
147  {
148  explicit txs_type(ReadView const& view);
149  bool
150  empty() const;
151  iterator
152  begin() const;
153  iterator
154  end() const;
155  };
156 
157  virtual ~ReadView() = default;
158 
159  ReadView&
160  operator=(ReadView&& other) = delete;
161  ReadView&
162  operator=(ReadView const& other) = delete;
163 
164  ReadView() : sles(*this), txs(*this)
165  {
166  }
167 
168  ReadView(ReadView const& other) : sles(*this), txs(*this)
169  {
170  }
171 
172  ReadView(ReadView&& other) : sles(*this), txs(*this)
173  {
174  }
175 
177  virtual LedgerInfo const&
178  info() const = 0;
179 
181  virtual bool
182  open() const = 0;
183 
187  {
188  return info().parentCloseTime;
189  }
190 
193  seq() const
194  {
195  return info().seq;
196  }
197 
199  virtual Fees const&
200  fees() const = 0;
201 
203  virtual Rules const&
204  rules() const = 0;
205 
213  virtual bool
214  exists(Keylet const& k) const = 0;
215 
227  succ(
228  key_type const& key,
229  std::optional<key_type> const& last = std::nullopt) const = 0;
230 
245  read(Keylet const& k) const = 0;
246 
247  // Accounts in a payment are not allowed to use assets acquired during that
248  // payment. The PaymentSandbox tracks the debits, credits, and owner count
249  // changes that accounts make during a payment. `balanceHook` adjusts
250  // balances so newly acquired assets are not counted toward the balance.
251  // This is required to support PaymentSandbox.
252  virtual STAmount
254  AccountID const& account,
255  AccountID const& issuer,
256  STAmount const& amount) const
257  {
258  return amount;
259  }
260 
261  // Accounts in a payment are not allowed to use assets acquired during that
262  // payment. The PaymentSandbox tracks the debits, credits, and owner count
263  // changes that accounts make during a payment. `ownerCountHook` adjusts the
264  // ownerCount so it returns the max value of the ownerCount so far.
265  // This is required to support PaymentSandbox.
266  virtual std::uint32_t
267  ownerCountHook(AccountID const& account, std::uint32_t count) const
268  {
269  return count;
270  }
271 
272  // used by the implementation
274  slesBegin() const = 0;
275 
276  // used by the implementation
278  slesEnd() const = 0;
279 
280  // used by the implementation
282  slesUpperBound(key_type const& key) const = 0;
283 
284  // used by the implementation
286  txsBegin() const = 0;
287 
288  // used by the implementation
290  txsEnd() const = 0;
291 
297  virtual bool
298  txExists(key_type const& key) const = 0;
299 
308  virtual tx_type
309  txRead(key_type const& key) const = 0;
310 
311  //
312  // Memberspaces
313  //
314 
321 
322  // The range of transactions
324 };
325 
326 //------------------------------------------------------------------------------
327 
330 {
331 public:
333 
334  DigestAwareReadView() = default;
335  DigestAwareReadView(const DigestAwareReadView&) = default;
336 
342  digest(key_type const& key) const = 0;
343 };
344 
345 //------------------------------------------------------------------------------
346 
347 // ledger close flags
349 
350 inline bool
352 {
353  return (info.closeFlags & sLCF_NoConsensusTime) == 0;
354 }
355 
356 void
357 addRaw(LedgerInfo const&, Serializer&, bool includeHash = false);
358 
359 Rules
360 makeRulesGivenLedger(DigestAwareReadView const& ledger, Rules const& current);
361 
362 Rules
364  DigestAwareReadView const& ledger,
365  std::unordered_set<uint256, beast::uhash<>> const& presets);
366 
367 } // namespace ripple
368 
369 #include <ripple/ledger/detail/ReadViewFwdRange.ipp>
370 
371 #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::LedgerInfo::parentHash
uint256 parentHash
Definition: ReadView.h:94
ripple::ReadView::txs_type
Definition: ReadView.h:146
unordered_set
std::pair
ripple::LedgerInfo::hash
uint256 hash
Definition: ReadView.h:91
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:131
ripple::addRaw
void addRaw(LedgerInfo const &info, Serializer &s, bool includeHash)
Definition: View.cpp:162
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
std::chrono::duration
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:253
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::LedgerInfo::seq
LedgerIndex seq
Definition: ReadView.h:83
ripple::ReadView::slesBegin
virtual std::unique_ptr< sles_type::iter_base > slesBegin() const =0
ripple::Fees::reserve
XRPAmount reserve
Definition: ReadView.h:52
ripple::ReadView::parentCloseTime
NetClock::time_point parentCloseTime() const
Returns the close time of the previous ledger.
Definition: ReadView.h:186
ripple::LedgerInfo::txHash
uint256 txHash
Definition: ReadView.h:92
ripple::ReadView::sles_type::begin
iterator begin() const
Definition: ReadView.cpp:29
ripple::Fees::Fees
Fees()=default
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:550
ripple::Fees::increment
XRPAmount increment
Definition: ReadView.h:53
ripple::ReadView::txsBegin
virtual std::unique_ptr< txs_type::iter_base > txsBegin() const =0
ripple::LedgerInfo::closeTime
NetClock::time_point closeTime
Definition: ReadView.h:114
ripple::base_uint< 256 >
ripple::Fees
Reflects the fee settings for a particular ledger.
Definition: ReadView.h:49
ripple::ReadView::ReadView
ReadView()
Definition: ReadView.h:164
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:329
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::LedgerInfo::closeFlags
int closeFlags
Definition: ReadView.h:105
ripple::STAmount
Definition: STAmount.h:45
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:320
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::LedgerInfo::drops
XRPAmount drops
Definition: ReadView.h:96
ripple::ReadView::tx_type
std::pair< std::shared_ptr< STTx const >, std::shared_ptr< STObject const > > tx_type
Definition: ReadView.h:129
ripple::getCloseAgree
bool getCloseAgree(LedgerInfo const &info)
Definition: ReadView.h:351
ripple::ReadView::ReadView
ReadView(ReadView &&other)
Definition: ReadView.h:172
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:125
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::LedgerInfo::accepted
bool accepted
Definition: ReadView.h:102
ripple::ReadView::sles_type
Definition: ReadView.h:135
ripple::ReadView::ReadView
ReadView(ReadView const &other)
Definition: ReadView.h:168
ripple::Fees::accountReserve
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
Definition: ReadView.h:66
ripple::ReadView::seq
LedgerIndex seq() const
Returns the sequence number of the base ledger.
Definition: ReadView.h:193
ripple::ReadView::rules
virtual Rules const & rules() const =0
Returns the tx processing rules.
ripple::LedgerInfo::closeTimeResolution
NetClock::duration closeTimeResolution
Definition: ReadView.h:108
cassert
ripple::ReadView::ownerCountHook
virtual std::uint32_t ownerCountHook(AccountID const &account, std::uint32_t count) const
Definition: ReadView.h:267
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::Fees::operator=
Fees & operator=(Fees const &)=default
ripple::ReadView::~ReadView
virtual ~ReadView()=default
optional
std::size_t
ripple::LedgerInfo
Information about the notional ledger backing the view.
Definition: ReadView.h:75
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::LedgerInfo::validated
bool validated
Definition: ReadView.h:101
std::unique_ptr
STL class.
ripple::ReadView::open
virtual bool open() const =0
Returns true if this reflects an open ledger.
ripple::LedgerInfo::LedgerInfo
LedgerInfo()=default
ripple::LedgerInfo::accountHash
uint256 accountHash
Definition: ReadView.h:93
ripple::Fees::base
XRPAmount base
Definition: ReadView.h:51
ripple::ReadView::txs
txs_type txs
Definition: ReadView.h:323
ripple::sLCF_NoConsensusTime
static const std::uint32_t sLCF_NoConsensusTime
Definition: ReadView.h:348
ripple::XRPAmount
Definition: XRPAmount.h:46
ripple::LedgerInfo::parentCloseTime
NetClock::time_point parentCloseTime
Definition: ReadView.h:84
ripple::ReadView::txs_type::txs_type
txs_type(ReadView const &view)
Definition: ReadView.cpp:46