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/STLedgerEntry.h>
33 #include <ripple/protocol/STTx.h>
34 #include <boost/optional.hpp>
35 #include <cassert>
36 #include <cstdint>
37 #include <memory>
38 #include <unordered_set>
39 
40 namespace ripple {
41 
47 struct Fees
48 {
49  XRPAmount base{0}; // Reference tx cost (drops)
50  FeeUnit32 units{0}; // Reference fee units
51  XRPAmount reserve{0}; // Reserve base (drops)
52  XRPAmount increment{0}; // Reserve increment (drops)
53 
54  explicit Fees() = default;
55  Fees(Fees const&) = default;
56  Fees&
57  operator=(Fees const&) = default;
58 
64  XRPAmount
65  accountReserve(std::size_t ownerCount) const
66  {
67  return reserve + ownerCount * increment;
68  }
69 
71  toDrops(FeeUnit64 const& fee) const
72  {
73  return mulDiv(base, fee, units);
74  }
75 };
76 
77 //------------------------------------------------------------------------------
78 
80 struct LedgerInfo
81 {
82  explicit LedgerInfo() = default;
83 
84  //
85  // For all ledgers
86  //
87 
90 
91  //
92  // For closed ledgers
93  //
94 
95  // Closed means "tx set already determined"
96  uint256 hash = beast::zero;
97  uint256 txHash = beast::zero;
98  uint256 accountHash = beast::zero;
99  uint256 parentHash = beast::zero;
100 
101  XRPAmount drops = beast::zero;
102 
103  // If validated is false, it means "not yet validated."
104  // Once validated is true, it will never be set false at a later time.
105  // VFALCO TODO Make this not mutable
106  bool mutable validated = false;
107  bool accepted = false;
108 
109  // flags indicating how this ledger close took place
110  int closeFlags = 0;
111 
112  // the resolution for this ledger close time (2-120 seconds)
114 
115  // For closed ledgers, the time the ledger
116  // closed. For open ledgers, the time the ledger
117  // will close if there's no transactions.
118  //
120 };
121 
122 //------------------------------------------------------------------------------
123 
124 class DigestAwareReadView;
125 
127 class Rules
128 {
129 private:
130  class Impl;
131 
133 
134 public:
135  Rules(Rules const&) = default;
136  Rules&
137  operator=(Rules const&) = default;
138 
139  Rules() = delete;
140 
146  explicit Rules(std::unordered_set<uint256, beast::uhash<>> const& presets);
147 
153  explicit Rules(
154  DigestAwareReadView const& ledger,
155  std::unordered_set<uint256, beast::uhash<>> const& presets);
156 
158  bool
159  enabled(uint256 const& id) const;
160 
162  bool
163  changed(DigestAwareReadView const& ledger) const;
164 
170  bool
171  operator==(Rules const&) const;
172 
173  bool
174  operator!=(Rules const& other) const
175  {
176  return !(*this == other);
177  }
178 };
179 
180 //------------------------------------------------------------------------------
181 
188 class ReadView
189 {
190 public:
191  using tx_type =
193 
194  using key_type = uint256;
195 
197 
198  struct sles_type : detail::ReadViewFwdRange<std::shared_ptr<SLE const>>
199  {
200  explicit sles_type(ReadView const& view);
201  iterator
202  begin() const;
203  iterator const&
204  end() const;
205  iterator
206  upper_bound(key_type const& key) const;
207  };
208 
210  {
211  explicit txs_type(ReadView const& view);
212  bool
213  empty() const;
214  iterator
215  begin() const;
216  iterator const&
217  end() const;
218  };
219 
220  virtual ~ReadView() = default;
221 
222  ReadView&
223  operator=(ReadView&& other) = delete;
224  ReadView&
225  operator=(ReadView const& other) = delete;
226 
227  ReadView() : sles(*this), txs(*this)
228  {
229  }
230 
231  ReadView(ReadView const& other) : sles(*this), txs(*this)
232  {
233  }
234 
235  ReadView(ReadView&& other) : sles(*this), txs(*this)
236  {
237  }
238 
240  virtual LedgerInfo const&
241  info() const = 0;
242 
244  virtual bool
245  open() const = 0;
246 
250  {
251  return info().parentCloseTime;
252  }
253 
256  seq() const
257  {
258  return info().seq;
259  }
260 
262  virtual Fees const&
263  fees() const = 0;
264 
266  virtual Rules const&
267  rules() const = 0;
268 
276  virtual bool
277  exists(Keylet const& k) const = 0;
278 
289  virtual boost::optional<key_type>
290  succ(
291  key_type const& key,
292  boost::optional<key_type> const& last = boost::none) const = 0;
293 
308  read(Keylet const& k) const = 0;
309 
310  // Accounts in a payment are not allowed to use assets acquired during that
311  // payment. The PaymentSandbox tracks the debits, credits, and owner count
312  // changes that accounts make during a payment. `balanceHook` adjusts
313  // balances so newly acquired assets are not counted toward the balance.
314  // This is required to support PaymentSandbox.
315  virtual STAmount
317  AccountID const& account,
318  AccountID const& issuer,
319  STAmount const& amount) const
320  {
321  return amount;
322  }
323 
324  // Accounts in a payment are not allowed to use assets acquired during that
325  // payment. The PaymentSandbox tracks the debits, credits, and owner count
326  // changes that accounts make during a payment. `ownerCountHook` adjusts the
327  // ownerCount so it returns the max value of the ownerCount so far.
328  // This is required to support PaymentSandbox.
329  virtual std::uint32_t
330  ownerCountHook(AccountID const& account, std::uint32_t count) const
331  {
332  return count;
333  }
334 
335  // used by the implementation
337  slesBegin() const = 0;
338 
339  // used by the implementation
341  slesEnd() const = 0;
342 
343  // used by the implementation
345  slesUpperBound(key_type const& key) const = 0;
346 
347  // used by the implementation
349  txsBegin() const = 0;
350 
351  // used by the implementation
353  txsEnd() const = 0;
354 
360  virtual bool
361  txExists(key_type const& key) const = 0;
362 
371  virtual tx_type
372  txRead(key_type const& key) const = 0;
373 
374  //
375  // Memberspaces
376  //
377 
384 
385  // The range of transactions
387 };
388 
389 //------------------------------------------------------------------------------
390 
393 {
394 public:
396 
397  DigestAwareReadView() = default;
398  DigestAwareReadView(const DigestAwareReadView&) = default;
399 
404  virtual boost::optional<digest_type>
405  digest(key_type const& key) const = 0;
406 };
407 
408 //------------------------------------------------------------------------------
409 
410 // ledger close flags
412 
413 inline bool
415 {
416  return (info.closeFlags & sLCF_NoConsensusTime) == 0;
417 }
418 
419 void
420 addRaw(LedgerInfo const&, Serializer&);
421 
422 } // namespace ripple
423 
424 #include <ripple/ledger/detail/ReadViewFwdRange.ipp>
425 
426 #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:99
ripple::Rules::Rules
Rules()=delete
ripple::Rules::impl_
std::shared_ptr< Impl const > impl_
Definition: ReadView.h:130
ripple::ReadView::txs_type
Definition: ReadView.h:209
unordered_set
std::pair
ripple::LedgerInfo::hash
uint256 hash
Definition: ReadView.h:96
ripple::ReadView::sles_type::sles_type
sles_type(ReadView const &view)
Definition: ReadView.cpp:127
ripple::ReadView::key_type
uint256 key_type
Definition: ReadView.h:194
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:156
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:316
ripple::addRaw
void addRaw(LedgerInfo const &info, Serializer &s)
Definition: View.cpp:43
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:146
ripple::LedgerInfo::seq
LedgerIndex seq
Definition: ReadView.h:88
ripple::ReadView::slesBegin
virtual std::unique_ptr< sles_type::iter_base > slesBegin() const =0
ripple::Fees::reserve
XRPAmount reserve
Definition: ReadView.h:51
ripple::ReadView::parentCloseTime
NetClock::time_point parentCloseTime() const
Returns the close time of the previous ledger.
Definition: ReadView.h:249
ripple::LedgerInfo::txHash
uint256 txHash
Definition: ReadView.h:97
ripple::ReadView::sles_type::begin
iterator begin() const
Definition: ReadView.cpp:132
ripple::Fees::Fees
Fees()=default
ripple::Rules::operator=
Rules & operator=(Rules const &)=default
ripple::Rules::operator!=
bool operator!=(Rules const &other) const
Definition: ReadView.h:174
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:493
ripple::Fees::increment
XRPAmount increment
Definition: ReadView.h:52
ripple::ReadView::txsBegin
virtual std::unique_ptr< txs_type::iter_base > txsBegin() const =0
ripple::LedgerInfo::closeTime
NetClock::time_point closeTime
Definition: ReadView.h:119
ripple::base_uint< 256 >
ripple::Rules::Impl
Definition: ReadView.cpp:25
ripple::DigestAwareReadView::digest
virtual boost::optional< digest_type > digest(key_type const &key) const =0
Return the digest associated with the key.
ripple::Fees
Reflects the fee settings for a particular ledger.
Definition: ReadView.h:47
ripple::ReadView::ReadView
ReadView()
Definition: ReadView.h:227
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:162
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:392
ripple::ReadView::sles_type::end
iterator const & end() const
Definition: ReadView.cpp:138
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:110
ripple::STAmount
Definition: STAmount.h:42
ripple::Fees::toDrops
std::pair< bool, XRPAmount > toDrops(FeeUnit64 const &fee) const
Definition: ReadView.h:71
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:383
cstdint
ripple::Rules::changed
bool changed(DigestAwareReadView const &ledger) const
Returns true if these rules don't match the ledger.
Definition: ReadView.cpp:110
std::uint32_t
ripple::Rules::enabled
bool enabled(uint256 const &id) const
Returns true if a feature is enabled.
Definition: ReadView.cpp:103
ripple::feeunit::TaggedFee
Definition: FeeUnits.h:70
ripple::ReadView::txs_type::end
iterator const & end() const
Definition: ReadView.cpp:168
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::Rules::operator==
bool operator==(Rules const &) const
Returns true if two rule sets are identical.
Definition: ReadView.cpp:117
ripple::LedgerInfo::drops
XRPAmount drops
Definition: ReadView.h:101
ripple::ReadView::tx_type
std::pair< std::shared_ptr< STTx const >, std::shared_ptr< STObject const > > tx_type
Definition: ReadView.h:192
ripple::getCloseAgree
bool getCloseAgree(LedgerInfo const &info)
Definition: ReadView.h:414
ripple::ReadView::ReadView
ReadView(ReadView &&other)
Definition: ReadView.h:235
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:188
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:107
ripple::ReadView::sles_type
Definition: ReadView.h:198
ripple::ReadView::ReadView
ReadView(ReadView const &other)
Definition: ReadView.h:231
ripple::Fees::accountReserve
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
Definition: ReadView.h:65
ripple::ReadView::seq
LedgerIndex seq() const
Returns the sequence number of the base ledger.
Definition: ReadView.h:256
ripple::ReadView::rules
virtual Rules const & rules() const =0
Returns the tx processing rules.
ripple::Fees::units
FeeUnit32 units
Definition: ReadView.h:50
ripple::LedgerInfo::closeTimeResolution
NetClock::duration closeTimeResolution
Definition: ReadView.h:113
cassert
ripple::ReadView::ownerCountHook
virtual std::uint32_t ownerCountHook(AccountID const &account, std::uint32_t count) const
Definition: ReadView.h:330
ripple::Rules
Rules controlling protocol behavior.
Definition: ReadView.h:127
ripple::Fees::operator=
Fees & operator=(Fees const &)=default
ripple::ReadView::~ReadView
virtual ~ReadView()=default
std::size_t
ripple::LedgerInfo
Information about the notional ledger backing the view.
Definition: ReadView.h:80
ripple::mulDiv
std::pair< bool, Dest > mulDiv(Source1 value, Dest mul, Source2 div)
Definition: FeeUnits.h:473
beast::uhash<>
ripple::LedgerInfo::validated
bool validated
Definition: ReadView.h:106
std::unique_ptr
STL class.
ripple::ReadView::open
virtual bool open() const =0
Returns true if this reflects an open ledger.
ripple::ReadView::succ
virtual boost::optional< key_type > succ(key_type const &key, boost::optional< key_type > const &last=boost::none) const =0
Return the key of the next state item.
ripple::LedgerInfo::LedgerInfo
LedgerInfo()=default
ripple::LedgerInfo::accountHash
uint256 accountHash
Definition: ReadView.h:98
ripple::Fees::base
XRPAmount base
Definition: ReadView.h:49
ripple::ReadView::txs
txs_type txs
Definition: ReadView.h:386
ripple::sLCF_NoConsensusTime
static const std::uint32_t sLCF_NoConsensusTime
Definition: ReadView.h:411
ripple::XRPAmount
Definition: XRPAmount.h:46
ripple::LedgerInfo::parentCloseTime
NetClock::time_point parentCloseTime
Definition: ReadView.h:89
ripple::ReadView::txs_type::txs_type
txs_type(ReadView const &view)
Definition: ReadView.cpp:151