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/ledger/detail/ReadViewFwdRange.h>
24 #include <ripple/basics/chrono.h>
25 #include <ripple/basics/FeeUnits.h>
26 #include <ripple/basics/IOUAmount.h>
27 #include <ripple/basics/XRPAmount.h>
28 #include <ripple/protocol/Indexes.h>
29 #include <ripple/protocol/Protocol.h>
30 #include <ripple/protocol/STLedgerEntry.h>
31 #include <ripple/protocol/STTx.h>
32 #include <ripple/beast/hash/uhash.h>
33 #include <ripple/beast/utility/Journal.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& operator= (Fees const&) = default;
57 
63  XRPAmount
64  accountReserve (std::size_t ownerCount) const
65  {
66  return reserve + ownerCount * increment;
67  }
68 
70  toDrops(FeeUnit64 const& fee) const
71  {
72  return mulDiv(base, fee, units);
73  }
74 };
75 
76 //------------------------------------------------------------------------------
77 
79 struct LedgerInfo
80 {
81  explicit LedgerInfo() = default;
82 
83  //
84  // For all ledgers
85  //
86 
89 
90  //
91  // For closed ledgers
92  //
93 
94  // Closed means "tx set already determined"
95  uint256 hash = beast::zero;
96  uint256 txHash = beast::zero;
97  uint256 accountHash = beast::zero;
98  uint256 parentHash = beast::zero;
99 
100  XRPAmount drops = beast::zero;
101 
102  // If validated is false, it means "not yet validated."
103  // Once validated is true, it will never be set false at a later time.
104  // VFALCO TODO Make this not mutable
105  bool mutable validated = false;
106  bool accepted = false;
107 
108  // flags indicating how this ledger close took place
109  int closeFlags = 0;
110 
111  // the resolution for this ledger close time (2-120 seconds)
113 
114  // For closed ledgers, the time the ledger
115  // closed. For open ledgers, the time the ledger
116  // will close if there's no transactions.
117  //
119 };
120 
121 //------------------------------------------------------------------------------
122 
123 class DigestAwareReadView;
124 
126 class Rules
127 {
128 private:
129  class Impl;
130 
132 
133 public:
134  Rules (Rules const&) = default;
135  Rules& operator= (Rules const&) = default;
136 
137  Rules() = delete;
138 
144  explicit Rules(std::unordered_set<uint256, beast::uhash<>> const& presets);
145 
151  explicit Rules(
152  DigestAwareReadView const& ledger,
153  std::unordered_set<uint256, beast::uhash<>> const& presets);
154 
156  bool
157  enabled (uint256 const& id) const;
158 
160  bool
161  changed (DigestAwareReadView const& ledger) const;
162 
168  bool
169  operator== (Rules const&) const;
170 
171  bool
172  operator!= (Rules const& other) const
173  {
174  return ! (*this == other);
175  }
176 };
177 
178 //------------------------------------------------------------------------------
179 
186 class ReadView
187 {
188 public:
189  using tx_type =
192 
193  using key_type = uint256;
194 
195  using mapped_type =
197 
199  std::shared_ptr<SLE const>>
200  {
201  explicit sles_type (ReadView const& view);
202  iterator begin() const;
203  iterator const& end() const;
204  iterator upper_bound(key_type const& key) const;
205  };
206 
207  struct txs_type
208  : detail::ReadViewFwdRange<tx_type>
209  {
210  explicit txs_type (ReadView const& view);
211  bool empty() const;
212  iterator begin() const;
213  iterator const& end() const;
214  };
215 
216  virtual ~ReadView() = default;
217 
218  ReadView& operator= (ReadView&& other) = delete;
219  ReadView& operator= (ReadView const& other) = delete;
220 
222  : sles(*this)
223  , txs(*this)
224  {
225  }
226 
227  ReadView (ReadView const& other)
228  : sles(*this)
229  , txs(*this)
230  {
231  }
232 
233  ReadView (ReadView&& other)
234  : sles(*this)
235  , txs(*this)
236  {
237  }
238 
240  virtual
241  LedgerInfo const&
242  info() const = 0;
243 
245  virtual
246  bool
247  open() const = 0;
248 
252  {
253  return info().parentCloseTime;
254  }
255 
258  seq() const
259  {
260  return info().seq;
261  }
262 
264  virtual
265  Fees const&
266  fees() const = 0;
267 
269  virtual
270  Rules const&
271  rules() const = 0;
272 
280  virtual
281  bool
282  exists (Keylet const& k) const = 0;
283 
294  virtual
295  boost::optional<key_type>
296  succ (key_type const& key, boost::optional<
297  key_type> const& last = boost::none) const = 0;
298 
312  virtual
314  read (Keylet const& k) const = 0;
315 
316  // Accounts in a payment are not allowed to use assets acquired during that
317  // payment. The PaymentSandbox tracks the debits, credits, and owner count
318  // changes that accounts make during a payment. `balanceHook` adjusts balances
319  // so newly acquired assets are not counted toward the balance.
320  // This is required to support PaymentSandbox.
321  virtual
322  STAmount
323  balanceHook (AccountID const& account,
324  AccountID const& issuer,
325  STAmount const& amount) const
326  {
327  return amount;
328  }
329 
330  // Accounts in a payment are not allowed to use assets acquired during that
331  // payment. The PaymentSandbox tracks the debits, credits, and owner count
332  // changes that accounts make during a payment. `ownerCountHook` adjusts the
333  // ownerCount so it returns the max value of the ownerCount so far.
334  // This is required to support PaymentSandbox.
335  virtual
337  ownerCountHook (AccountID const& account,
338  std::uint32_t count) const
339  {
340  return count;
341  }
342 
343  // used by the implementation
344  virtual
346  slesBegin() const = 0;
347 
348  // used by the implementation
349  virtual
351  slesEnd() const = 0;
352 
353  // used by the implementation
354  virtual
356  slesUpperBound(key_type const& key) const = 0;
357 
358  // used by the implementation
359  virtual
361  txsBegin() const = 0;
362 
363  // used by the implementation
364  virtual
366  txsEnd() const = 0;
367 
373  virtual
374  bool
375  txExists (key_type const& key) const = 0;
376 
385  virtual
386  tx_type
387  txRead (key_type const& key) const = 0;
388 
389  //
390  // Memberspaces
391  //
392 
399 
400  // The range of transactions
402 };
403 
404 //------------------------------------------------------------------------------
405 
408  : public ReadView
409 {
410 public:
412 
413  DigestAwareReadView () = default;
414  DigestAwareReadView (const DigestAwareReadView&) = default;
415 
420  virtual
421  boost::optional<digest_type>
422  digest (key_type const& key) const = 0;
423 };
424 
425 //------------------------------------------------------------------------------
426 
427 // ledger close flags
428 static
430 
431 inline
432 bool getCloseAgree (LedgerInfo const& info)
433 {
434  return (info.closeFlags & sLCF_NoConsensusTime) == 0;
435 }
436 
437 void addRaw (LedgerInfo const&, Serializer&);
438 
439 } // ripple
440 
441 #include <ripple/ledger/detail/ReadViewFwdRange.ipp>
442 
443 #endif
ripple::ReadView::info
virtual LedgerInfo const & info() const =0
Returns information about the ledger.
ripple::detail::ReadViewFwdRange
Definition: ReadViewFwdRange.h:71
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:98
ripple::Rules::Rules
Rules()=delete
ripple::Rules::impl_
std::shared_ptr< Impl const > impl_
Definition: ReadView.h:129
ripple::ReadView::txs_type
Definition: ReadView.h:207
unordered_set
std::pair
ripple::LedgerInfo::hash
uint256 hash
Definition: ReadView.h:95
ripple::ReadView::sles_type::sles_type
sles_type(ReadView const &view)
Definition: ReadView.cpp:131
ripple::ReadView::key_type
uint256 key_type
Definition: ReadView.h:193
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:167
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:323
ripple::addRaw
void addRaw(LedgerInfo const &info, Serializer &s)
Definition: View.cpp:42
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:154
ripple::LedgerInfo::seq
LedgerIndex seq
Definition: ReadView.h:87
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:251
ripple::LedgerInfo::txHash
uint256 txHash
Definition: ReadView.h:96
ripple::ReadView::sles_type::begin
iterator begin() const
Definition: ReadView.cpp:138
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:172
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:436
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:118
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:221
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:173
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:407
ripple::ReadView::sles_type::end
iterator const & end() const
Definition: ReadView.cpp:145
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:109
ripple::STAmount
Definition: STAmount.h:42
ripple::Fees::toDrops
std::pair< bool, XRPAmount > toDrops(FeeUnit64 const &fee) const
Definition: ReadView.h:70
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:398
cstdint
ripple::Rules::changed
bool changed(DigestAwareReadView const &ledger) const
Returns true if these rules don't match the ledger.
Definition: ReadView.cpp:114
std::uint32_t
ripple::Rules::enabled
bool enabled(uint256 const &id) const
Returns true if a feature is enabled.
Definition: ReadView.cpp:107
ripple::feeunit::TaggedFee
Definition: FeeUnits.h:72
ripple::ReadView::txs_type::end
iterator const & end() const
Definition: ReadView.cpp:180
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:121
ripple::LedgerInfo::drops
XRPAmount drops
Definition: ReadView.h:100
ripple::ReadView::tx_type
std::pair< std::shared_ptr< STTx const >, std::shared_ptr< STObject const > > tx_type
Definition: ReadView.h:191
ripple::getCloseAgree
bool getCloseAgree(LedgerInfo const &info)
Definition: ReadView.h:432
ripple::ReadView::ReadView
ReadView(ReadView &&other)
Definition: ReadView.h:233
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:186
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:106
ripple::ReadView::sles_type
Definition: ReadView.h:198
ripple::ReadView::ReadView
ReadView(ReadView const &other)
Definition: ReadView.h:227
ripple::Fees::accountReserve
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
Definition: ReadView.h:64
ripple::ReadView::seq
LedgerIndex seq() const
Returns the sequence number of the base ledger.
Definition: ReadView.h:258
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:112
cassert
ripple::ReadView::ownerCountHook
virtual std::uint32_t ownerCountHook(AccountID const &account, std::uint32_t count) const
Definition: ReadView.h:337
ripple::Rules
Rules controlling protocol behavior.
Definition: ReadView.h:126
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:79
ripple::mulDiv
std::pair< bool, Dest > mulDiv(Source1 value, Dest mul, Source2 div)
Definition: FeeUnits.h:487
beast::uhash<>
ripple::LedgerInfo::validated
bool validated
Definition: ReadView.h:105
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:97
ripple::Fees::base
XRPAmount base
Definition: ReadView.h:49
ripple::ReadView::txs
txs_type txs
Definition: ReadView.h:401
ripple::sLCF_NoConsensusTime
static const std::uint32_t sLCF_NoConsensusTime
Definition: ReadView.h:429
ripple::XRPAmount
Definition: XRPAmount.h:46
ripple::LedgerInfo::parentCloseTime
NetClock::time_point parentCloseTime
Definition: ReadView.h:88
ripple::ReadView::txs_type::txs_type
txs_type(ReadView const &view)
Definition: ReadView.cpp:160