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/STAmount.h>
33 #include <ripple/protocol/STLedgerEntry.h>
34 #include <ripple/protocol/STTx.h>
35 #include <boost/optional.hpp>
36 #include <cassert>
37 #include <cstdint>
38 #include <memory>
39 #include <unordered_set>
40 
41 namespace ripple {
42 
48 struct Fees
49 {
50  XRPAmount base{0}; // Reference tx cost (drops)
51  FeeUnit32 units{0}; // Reference fee units
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  XRPAmount
72  toDrops(FeeUnit64 const& fee) const
73  {
74  if (auto const resultPair = mulDiv(base, fee, units); resultPair.first)
75  return resultPair.second;
76 
78  }
79 };
80 
81 //------------------------------------------------------------------------------
82 
84 struct LedgerInfo
85 {
86  explicit LedgerInfo() = default;
87 
88  //
89  // For all ledgers
90  //
91 
94 
95  //
96  // For closed ledgers
97  //
98 
99  // Closed means "tx set already determined"
100  uint256 hash = beast::zero;
101  uint256 txHash = beast::zero;
102  uint256 accountHash = beast::zero;
103  uint256 parentHash = beast::zero;
104 
105  XRPAmount drops = beast::zero;
106 
107  // If validated is false, it means "not yet validated."
108  // Once validated is true, it will never be set false at a later time.
109  // VFALCO TODO Make this not mutable
110  bool mutable validated = false;
111  bool accepted = false;
112 
113  // flags indicating how this ledger close took place
114  int closeFlags = 0;
115 
116  // the resolution for this ledger close time (2-120 seconds)
118 
119  // For closed ledgers, the time the ledger
120  // closed. For open ledgers, the time the ledger
121  // will close if there's no transactions.
122  //
124 };
125 
126 //------------------------------------------------------------------------------
127 
128 class DigestAwareReadView;
129 
131 class Rules
132 {
133 private:
134  class Impl;
135 
137 
138 public:
139  Rules(Rules const&) = default;
140  Rules&
141  operator=(Rules const&) = default;
142 
143  Rules() = delete;
144 
150  explicit Rules(std::unordered_set<uint256, beast::uhash<>> const& presets);
151 
157  explicit Rules(
158  DigestAwareReadView const& ledger,
159  std::unordered_set<uint256, beast::uhash<>> const& presets);
160 
162  bool
163  enabled(uint256 const& id) const;
164 
166  bool
167  changed(DigestAwareReadView const& ledger) const;
168 
174  bool
175  operator==(Rules const&) const;
176 
177  bool
178  operator!=(Rules const& other) const
179  {
180  return !(*this == other);
181  }
182 };
183 
184 //------------------------------------------------------------------------------
185 
192 class ReadView
193 {
194 public:
195  using tx_type =
197 
198  using key_type = uint256;
199 
201 
202  struct sles_type : detail::ReadViewFwdRange<std::shared_ptr<SLE const>>
203  {
204  explicit sles_type(ReadView const& view);
205  iterator
206  begin() const;
207  iterator
208  end() const;
209  iterator
210  upper_bound(key_type const& key) const;
211  };
212 
214  {
215  explicit txs_type(ReadView const& view);
216  bool
217  empty() const;
218  iterator
219  begin() const;
220  iterator
221  end() const;
222  };
223 
224  virtual ~ReadView() = default;
225 
226  ReadView&
227  operator=(ReadView&& other) = delete;
228  ReadView&
229  operator=(ReadView const& other) = delete;
230 
231  ReadView() : sles(*this), txs(*this)
232  {
233  }
234 
235  ReadView(ReadView const& other) : sles(*this), txs(*this)
236  {
237  }
238 
239  ReadView(ReadView&& other) : sles(*this), txs(*this)
240  {
241  }
242 
244  virtual LedgerInfo const&
245  info() const = 0;
246 
248  virtual bool
249  open() const = 0;
250 
254  {
255  return info().parentCloseTime;
256  }
257 
260  seq() const
261  {
262  return info().seq;
263  }
264 
266  virtual Fees const&
267  fees() const = 0;
268 
270  virtual Rules const&
271  rules() const = 0;
272 
280  virtual bool
281  exists(Keylet const& k) const = 0;
282 
293  virtual boost::optional<key_type>
294  succ(
295  key_type const& key,
296  boost::optional<key_type> const& last = boost::none) const = 0;
297 
312  read(Keylet const& k) const = 0;
313 
314  // Accounts in a payment are not allowed to use assets acquired during that
315  // payment. The PaymentSandbox tracks the debits, credits, and owner count
316  // changes that accounts make during a payment. `balanceHook` adjusts
317  // balances so newly acquired assets are not counted toward the balance.
318  // This is required to support PaymentSandbox.
319  virtual STAmount
321  AccountID const& account,
322  AccountID const& issuer,
323  STAmount const& amount) const
324  {
325  return amount;
326  }
327 
328  // Accounts in a payment are not allowed to use assets acquired during that
329  // payment. The PaymentSandbox tracks the debits, credits, and owner count
330  // changes that accounts make during a payment. `ownerCountHook` adjusts the
331  // ownerCount so it returns the max value of the ownerCount so far.
332  // This is required to support PaymentSandbox.
333  virtual std::uint32_t
334  ownerCountHook(AccountID const& account, std::uint32_t count) const
335  {
336  return count;
337  }
338 
339  // used by the implementation
341  slesBegin() const = 0;
342 
343  // used by the implementation
345  slesEnd() const = 0;
346 
347  // used by the implementation
349  slesUpperBound(key_type const& key) const = 0;
350 
351  // used by the implementation
353  txsBegin() const = 0;
354 
355  // used by the implementation
357  txsEnd() const = 0;
358 
364  virtual bool
365  txExists(key_type const& key) const = 0;
366 
375  virtual tx_type
376  txRead(key_type const& key) const = 0;
377 
378  //
379  // Memberspaces
380  //
381 
388 
389  // The range of transactions
391 };
392 
393 //------------------------------------------------------------------------------
394 
397 {
398 public:
400 
401  DigestAwareReadView() = default;
402  DigestAwareReadView(const DigestAwareReadView&) = default;
403 
408  virtual boost::optional<digest_type>
409  digest(key_type const& key) const = 0;
410 };
411 
412 //------------------------------------------------------------------------------
413 
414 // ledger close flags
416 
417 inline bool
419 {
420  return (info.closeFlags & sLCF_NoConsensusTime) == 0;
421 }
422 
423 void
424 addRaw(LedgerInfo const&, Serializer&);
425 
426 } // namespace ripple
427 
428 #include <ripple/ledger/detail/ReadViewFwdRange.ipp>
429 
430 #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:103
ripple::Rules::Rules
Rules()=delete
ripple::Rules::impl_
std::shared_ptr< Impl const > impl_
Definition: ReadView.h:134
ripple::ReadView::txs_type
Definition: ReadView.h:213
unordered_set
std::pair
ripple::LedgerInfo::hash
uint256 hash
Definition: ReadView.h:100
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:198
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:154
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:320
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:144
ripple::LedgerInfo::seq
LedgerIndex seq
Definition: ReadView.h:92
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:253
ripple::LedgerInfo::txHash
uint256 txHash
Definition: ReadView.h:101
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:178
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:457
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:123
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:48
ripple::ReadView::ReadView
ReadView()
Definition: ReadView.h:231
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:160
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:396
ripple::ReadView::sles_type::end
iterator 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:114
ripple::STAmount
Definition: STAmount.h:42
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:387
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 end() const
Definition: ReadView.cpp:166
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:105
ripple::ReadView::tx_type
std::pair< std::shared_ptr< STTx const >, std::shared_ptr< STObject const > > tx_type
Definition: ReadView.h:196
ripple::Fees::toDrops
XRPAmount toDrops(FeeUnit64 const &fee) const
Definition: ReadView.h:72
ripple::getCloseAgree
bool getCloseAgree(LedgerInfo const &info)
Definition: ReadView.h:418
ripple::ReadView::ReadView
ReadView(ReadView &&other)
Definition: ReadView.h:239
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:192
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:111
ripple::ReadView::sles_type
Definition: ReadView.h:202
ripple::ReadView::ReadView
ReadView(ReadView const &other)
Definition: ReadView.h:235
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:260
ripple::ReadView::rules
virtual Rules const & rules() const =0
Returns the tx processing rules.
ripple::Fees::units
FeeUnit32 units
Definition: ReadView.h:51
ripple::LedgerInfo::closeTimeResolution
NetClock::duration closeTimeResolution
Definition: ReadView.h:117
cassert
ripple::ReadView::ownerCountHook
virtual std::uint32_t ownerCountHook(AccountID const &account, std::uint32_t count) const
Definition: ReadView.h:334
ripple::Rules
Rules controlling protocol behavior.
Definition: ReadView.h:131
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:84
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:68
ripple::LedgerInfo::validated
bool validated
Definition: ReadView.h:110
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:102
ripple::Fees::base
XRPAmount base
Definition: ReadView.h:50
ripple::ReadView::txs
txs_type txs
Definition: ReadView.h:390
ripple::sLCF_NoConsensusTime
static const std::uint32_t sLCF_NoConsensusTime
Definition: ReadView.h:415
ripple::XRPAmount
Definition: XRPAmount.h:46
ripple::LedgerInfo::parentCloseTime
NetClock::time_point parentCloseTime
Definition: ReadView.h:93
ripple::ReadView::txs_type::txs_type
txs_type(ReadView const &view)
Definition: ReadView.cpp:149