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  FeeUnit32 units{0}; // Reference fee units
53  XRPAmount reserve{0}; // Reserve base (drops)
54  XRPAmount increment{0}; // Reserve increment (drops)
55 
56  explicit Fees() = default;
57  Fees(Fees const&) = default;
58  Fees&
59  operator=(Fees const&) = default;
60 
66  XRPAmount
67  accountReserve(std::size_t ownerCount) const
68  {
69  return reserve + ownerCount * increment;
70  }
71 
72  XRPAmount
73  toDrops(FeeUnit64 const& fee) const
74  {
75  if (auto const resultPair = mulDiv(base, fee, units); resultPair.first)
76  return resultPair.second;
77 
79  }
80 };
81 
82 //------------------------------------------------------------------------------
83 
85 struct LedgerInfo
86 {
87  explicit LedgerInfo() = default;
88 
89  //
90  // For all ledgers
91  //
92 
95 
96  //
97  // For closed ledgers
98  //
99 
100  // Closed means "tx set already determined"
101  uint256 hash = beast::zero;
102  uint256 txHash = beast::zero;
103  uint256 accountHash = beast::zero;
104  uint256 parentHash = beast::zero;
105 
106  XRPAmount drops = beast::zero;
107 
108  // If validated is false, it means "not yet validated."
109  // Once validated is true, it will never be set false at a later time.
110  // VFALCO TODO Make this not mutable
111  bool mutable validated = false;
112  bool accepted = false;
113 
114  // flags indicating how this ledger close took place
115  int closeFlags = 0;
116 
117  // the resolution for this ledger close time (2-120 seconds)
119 
120  // For closed ledgers, the time the ledger
121  // closed. For open ledgers, the time the ledger
122  // will close if there's no transactions.
123  //
125 };
126 
127 //------------------------------------------------------------------------------
128 
135 class ReadView
136 {
137 public:
138  using tx_type =
140 
141  using key_type = uint256;
142 
144 
145  struct sles_type : detail::ReadViewFwdRange<std::shared_ptr<SLE const>>
146  {
147  explicit sles_type(ReadView const& view);
148  iterator
149  begin() const;
150  iterator
151  end() const;
152  iterator
153  upper_bound(key_type const& key) const;
154  };
155 
157  {
158  explicit txs_type(ReadView const& view);
159  bool
160  empty() const;
161  iterator
162  begin() const;
163  iterator
164  end() const;
165  };
166 
167  virtual ~ReadView() = default;
168 
169  ReadView&
170  operator=(ReadView&& other) = delete;
171  ReadView&
172  operator=(ReadView const& other) = delete;
173 
174  ReadView() : sles(*this), txs(*this)
175  {
176  }
177 
178  ReadView(ReadView const& other) : sles(*this), txs(*this)
179  {
180  }
181 
182  ReadView(ReadView&& other) : sles(*this), txs(*this)
183  {
184  }
185 
187  virtual LedgerInfo const&
188  info() const = 0;
189 
191  virtual bool
192  open() const = 0;
193 
197  {
198  return info().parentCloseTime;
199  }
200 
203  seq() const
204  {
205  return info().seq;
206  }
207 
209  virtual Fees const&
210  fees() const = 0;
211 
213  virtual Rules const&
214  rules() const = 0;
215 
223  virtual bool
224  exists(Keylet const& k) const = 0;
225 
237  succ(
238  key_type const& key,
239  std::optional<key_type> const& last = std::nullopt) const = 0;
240 
255  read(Keylet const& k) const = 0;
256 
257  // Accounts in a payment are not allowed to use assets acquired during that
258  // payment. The PaymentSandbox tracks the debits, credits, and owner count
259  // changes that accounts make during a payment. `balanceHook` adjusts
260  // balances so newly acquired assets are not counted toward the balance.
261  // This is required to support PaymentSandbox.
262  virtual STAmount
264  AccountID const& account,
265  AccountID const& issuer,
266  STAmount const& amount) const
267  {
268  return amount;
269  }
270 
271  // Accounts in a payment are not allowed to use assets acquired during that
272  // payment. The PaymentSandbox tracks the debits, credits, and owner count
273  // changes that accounts make during a payment. `ownerCountHook` adjusts the
274  // ownerCount so it returns the max value of the ownerCount so far.
275  // This is required to support PaymentSandbox.
276  virtual std::uint32_t
277  ownerCountHook(AccountID const& account, std::uint32_t count) const
278  {
279  return count;
280  }
281 
282  // used by the implementation
284  slesBegin() const = 0;
285 
286  // used by the implementation
288  slesEnd() const = 0;
289 
290  // used by the implementation
292  slesUpperBound(key_type const& key) const = 0;
293 
294  // used by the implementation
296  txsBegin() const = 0;
297 
298  // used by the implementation
300  txsEnd() const = 0;
301 
307  virtual bool
308  txExists(key_type const& key) const = 0;
309 
318  virtual tx_type
319  txRead(key_type const& key) const = 0;
320 
321  //
322  // Memberspaces
323  //
324 
331 
332  // The range of transactions
334 };
335 
336 //------------------------------------------------------------------------------
337 
340 {
341 public:
343 
344  DigestAwareReadView() = default;
345  DigestAwareReadView(const DigestAwareReadView&) = default;
346 
352  digest(key_type const& key) const = 0;
353 };
354 
355 //------------------------------------------------------------------------------
356 
357 // ledger close flags
359 
360 inline bool
362 {
363  return (info.closeFlags & sLCF_NoConsensusTime) == 0;
364 }
365 
366 void
367 addRaw(LedgerInfo const&, Serializer&, bool includeHash = false);
368 
369 Rules
371  DigestAwareReadView const& ledger,
372  std::unordered_set<uint256, beast::uhash<>> const& presets);
373 
374 } // namespace ripple
375 
376 #include <ripple/ledger/detail/ReadViewFwdRange.ipp>
377 
378 #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:104
ripple::ReadView::txs_type
Definition: ReadView.h:156
unordered_set
std::pair
ripple::LedgerInfo::hash
uint256 hash
Definition: ReadView.h:101
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:141
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::makeRulesGivenLedger
Rules makeRulesGivenLedger(DigestAwareReadView const &ledger, std::unordered_set< uint256, beast::uhash<>> const &presets)
Definition: ReadView.cpp:69
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:263
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:93
ripple::ReadView::slesBegin
virtual std::unique_ptr< sles_type::iter_base > slesBegin() const =0
ripple::Fees::reserve
XRPAmount reserve
Definition: ReadView.h:53
ripple::ReadView::parentCloseTime
NetClock::time_point parentCloseTime() const
Returns the close time of the previous ledger.
Definition: ReadView.h:196
ripple::LedgerInfo::txHash
uint256 txHash
Definition: ReadView.h:102
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:543
ripple::Fees::increment
XRPAmount increment
Definition: ReadView.h:54
ripple::ReadView::txsBegin
virtual std::unique_ptr< txs_type::iter_base > txsBegin() const =0
ripple::LedgerInfo::closeTime
NetClock::time_point closeTime
Definition: ReadView.h:124
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:174
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:339
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:115
ripple::STAmount
Definition: STAmount.h:44
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:330
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::feeunit::TaggedFee
Definition: FeeUnits.h:70
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:106
ripple::ReadView::tx_type
std::pair< std::shared_ptr< STTx const >, std::shared_ptr< STObject const > > tx_type
Definition: ReadView.h:139
ripple::Fees::toDrops
XRPAmount toDrops(FeeUnit64 const &fee) const
Definition: ReadView.h:73
ripple::getCloseAgree
bool getCloseAgree(LedgerInfo const &info)
Definition: ReadView.h:361
ripple::ReadView::ReadView
ReadView(ReadView &&other)
Definition: ReadView.h:182
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:135
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:112
ripple::ReadView::sles_type
Definition: ReadView.h:145
ripple::ReadView::ReadView
ReadView(ReadView const &other)
Definition: ReadView.h:178
ripple::Fees::accountReserve
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
Definition: ReadView.h:67
ripple::ReadView::seq
LedgerIndex seq() const
Returns the sequence number of the base ledger.
Definition: ReadView.h:203
ripple::ReadView::rules
virtual Rules const & rules() const =0
Returns the tx processing rules.
ripple::Fees::units
FeeUnit32 units
Definition: ReadView.h:52
ripple::LedgerInfo::closeTimeResolution
NetClock::duration closeTimeResolution
Definition: ReadView.h:118
cassert
ripple::ReadView::ownerCountHook
virtual std::uint32_t ownerCountHook(AccountID const &account, std::uint32_t count) const
Definition: ReadView.h:277
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:85
ripple::mulDiv
std::pair< bool, Dest > mulDiv(Source1 value, Dest mul, Source2 div)
Definition: FeeUnits.h:473
beast::uhash<>
ripple::STAmount::cMaxNativeN
static const std::uint64_t cMaxNativeN
Definition: STAmount.h:70
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:111
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:103
ripple::Fees::base
XRPAmount base
Definition: ReadView.h:51
ripple::ReadView::txs
txs_type txs
Definition: ReadView.h:333
ripple::sLCF_NoConsensusTime
static const std::uint32_t sLCF_NoConsensusTime
Definition: ReadView.h:358
ripple::XRPAmount
Definition: XRPAmount.h:46
ripple::LedgerInfo::parentCloseTime
NetClock::time_point parentCloseTime
Definition: ReadView.h:94
ripple::ReadView::txs_type::txs_type
txs_type(ReadView const &view)
Definition: ReadView.cpp:46