rippled
ApplyView.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_APPLYVIEW_H_INCLUDED
21 #define RIPPLE_LEDGER_APPLYVIEW_H_INCLUDED
22 
23 #include <ripple/basics/safe_cast.h>
24 #include <ripple/ledger/RawView.h>
25 #include <ripple/ledger/ReadView.h>
26 #include <boost/optional.hpp>
27 
28 namespace ripple {
29 
32 {
33  tapNONE = 0x00,
34 
35  // This is a local transaction with the
36  // fail_hard flag set.
37  tapFAIL_HARD = 0x10,
38 
39  // This is not the transaction's last pass
40  // Transaction can be retried, soft failures allowed
41  tapRETRY = 0x20,
42 
43  // Transaction must pay more than both the open ledger
44  // fee and all transactions in the queue to get into the
45  // open ledger
47 
48  // Transaction came from a privileged source
49  tapUNLIMITED = 0x400,
50 };
51 
52 constexpr
54 operator|(ApplyFlags const& lhs,
55  ApplyFlags const& rhs)
56 {
57  return safe_cast<ApplyFlags>(
60 }
61 
62 static_assert((tapPREFER_QUEUE | tapRETRY) == safe_cast<ApplyFlags>(0x60u),
63  "ApplyFlags operator |");
64 static_assert((tapRETRY | tapPREFER_QUEUE) == safe_cast<ApplyFlags>(0x60u),
65  "ApplyFlags operator |");
66 
67 constexpr
69 operator&(ApplyFlags const& lhs,
70  ApplyFlags const& rhs)
71 {
72  return safe_cast<ApplyFlags>(
75 }
76 
77 static_assert((tapPREFER_QUEUE & tapRETRY) == tapNONE,
78  "ApplyFlags operator &");
79 static_assert((tapRETRY & tapPREFER_QUEUE) == tapNONE,
80  "ApplyFlags operator &");
81 
82 constexpr
84 operator~(ApplyFlags const& flags)
85 {
86  return safe_cast<ApplyFlags>(
88 }
89 
90 static_assert(~tapRETRY == safe_cast<ApplyFlags>(0xFFFFFFDFu),
91  "ApplyFlags operator ~");
92 
93 inline
96  ApplyFlags const& rhs)
97 {
98  lhs = lhs | rhs;
99  return lhs;
100 }
101 
102 inline
105  ApplyFlags const& rhs)
106 {
107  lhs = lhs & rhs;
108  return lhs;
109 }
110 
111 //------------------------------------------------------------------------------
112 
151  : public ReadView
152 {
153 private:
155  boost::optional<std::uint64_t>
156  dirAdd (
157  bool preserveOrder,
158  Keylet const& directory,
159  uint256 const& key,
160  std::function<void(std::shared_ptr<SLE> const&)> const& describe);
161 
162 public:
163  ApplyView () = default;
164 
173  virtual
174  ApplyFlags
175  flags() const = 0;
176 
191  virtual
193  peek (Keylet const& k) = 0;
194 
206  virtual
207  void
208  erase (std::shared_ptr<SLE> const& sle) = 0;
209 
228  virtual
229  void
230  insert (std::shared_ptr<SLE> const& sle) = 0;
231 
248  virtual
249  void
250  update (std::shared_ptr<SLE> const& sle) = 0;
251 
252  //--------------------------------------------------------------------------
253 
254  // Called when a credit is made to an account
255  // This is required to support PaymentSandbox
256  virtual void
257  creditHook (AccountID const& from,
258  AccountID const& to,
259  STAmount const& amount,
260  STAmount const& preCreditBalance)
261  {
262  }
263 
264  // Called when the owner count changes
265  // This is required to support PaymentSandbox
266  virtual
267  void adjustOwnerCountHook (AccountID const& account,
268  std::uint32_t cur, std::uint32_t next)
269  {}
270 
289  boost::optional<std::uint64_t>
291  Keylet const& directory,
292  uint256 const& key,
293  std::function<void(std::shared_ptr<SLE> const&)> const& describe)
294  {
295  return dirAdd (true, directory, key, describe);
296  }
297 
298  boost::optional<std::uint64_t>
300  Keylet const& directory,
301  Keylet const& key,
302  std::function<void(std::shared_ptr<SLE> const&)> const& describe)
303  {
304  return dirAppend (directory, key.key, describe);
305  }
326  boost::optional<std::uint64_t>
328  Keylet const& directory,
329  uint256 const& key,
330  std::function<void(std::shared_ptr<SLE> const&)> const& describe)
331  {
332  return dirAdd (false, directory, key, describe);
333  }
334 
335  boost::optional<std::uint64_t>
337  Keylet const& directory,
338  Keylet const& key,
339  std::function<void(std::shared_ptr<SLE> const&)> const& describe)
340  {
341  return dirInsert (directory, key.key, describe);
342  }
361  bool
362  dirRemove (
363  Keylet const& directory,
364  std::uint64_t page,
365  uint256 const& key,
366  bool keepRoot);
367 
368  bool
370  Keylet const& directory,
371  std::uint64_t page,
372  Keylet const& key,
373  bool keepRoot)
374  {
375  return dirRemove (directory, page, key.key, keepRoot);
376  }
388  bool
389  emptyDirDelete(Keylet const& directory);
390 };
391 
392 } // ripple
393 
394 #endif
ripple::operator&=
ApplyFlags operator&=(ApplyFlags &lhs, ApplyFlags const &rhs)
Definition: ApplyView.h:104
ripple::Keylet
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:38
ripple::operator|
const base_uint< Bits, Tag > operator|(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition: base_uint.h:526
std::shared_ptr
STL class.
ripple::ApplyView::peek
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
ripple::ApplyView::erase
virtual void erase(std::shared_ptr< SLE > const &sle)=0
Remove a peeked SLE.
ripple::ApplyView::dirInsert
boost::optional< std::uint64_t > dirInsert(Keylet const &directory, uint256 const &key, std::function< void(std::shared_ptr< SLE > const &)> const &describe)
Insert an entry to a directory.
Definition: ApplyView.h:327
ripple::ApplyView::dirInsert
boost::optional< std::uint64_t > dirInsert(Keylet const &directory, Keylet const &key, std::function< void(std::shared_ptr< SLE > const &)> const &describe)
Definition: ApplyView.h:336
ripple::ApplyFlags
ApplyFlags
Definition: ApplyView.h:30
ripple::ApplyView::creditHook
virtual void creditHook(AccountID const &from, AccountID const &to, STAmount const &amount, STAmount const &preCreditBalance)
Definition: ApplyView.h:257
ripple::ApplyView::dirAppend
boost::optional< std::uint64_t > dirAppend(Keylet const &directory, uint256 const &key, std::function< void(std::shared_ptr< SLE > const &)> const &describe)
Append an entry to a directory.
Definition: ApplyView.h:290
ripple::ApplyView::update
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
std::function
ripple::tapNONE
@ tapNONE
Definition: ApplyView.h:33
ripple::ApplyView
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:150
std::underlying_type_t
ripple::ApplyView::dirAdd
boost::optional< std::uint64_t > dirAdd(bool preserveOrder, Keylet const &directory, uint256 const &key, std::function< void(std::shared_ptr< SLE > const &)> const &describe)
Add an entry to a directory using the specified insert strategy.
Definition: ApplyView.cpp:28
ripple::ApplyView::dirRemove
bool dirRemove(Keylet const &directory, std::uint64_t page, uint256 const &key, bool keepRoot)
Remove an entry from a directory.
Definition: ApplyView.cpp:189
ripple::Keylet::key
uint256 key
Definition: Keylet.h:41
ripple::base_uint< 256 >
ripple::operator|=
ApplyFlags operator|=(ApplyFlags &lhs, ApplyFlags const &rhs)
Definition: ApplyView.h:95
ripple::ApplyView::dirAppend
boost::optional< std::uint64_t > dirAppend(Keylet const &directory, Keylet const &key, std::function< void(std::shared_ptr< SLE > const &)> const &describe)
Definition: ApplyView.h:299
ripple::safe_cast
constexpr std::enable_if_t< std::is_same_v< typename Dest::unit_type, typename Src::unit_type > &&std::is_integral_v< typename Dest::value_type > &&std::is_integral_v< typename Src::value_type >, Dest > safe_cast(Src s) noexcept
Definition: FeeUnits.h:553
ripple::STAmount
Definition: STAmount.h:42
std::uint32_t
ripple::tapPREFER_QUEUE
@ tapPREFER_QUEUE
Definition: ApplyView.h:46
ripple::ApplyView::adjustOwnerCountHook
virtual void adjustOwnerCountHook(AccountID const &account, std::uint32_t cur, std::uint32_t next)
Definition: ApplyView.h:267
ripple::operator&
const base_uint< Bits, Tag > operator&(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition: base_uint.h:519
ripple::ApplyView::dirRemove
bool dirRemove(Keylet const &directory, std::uint64_t page, Keylet const &key, bool keepRoot)
Definition: ApplyView.h:369
ripple::tapRETRY
@ tapRETRY
Definition: ApplyView.h:41
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:186
ripple::ApplyView::insert
virtual void insert(std::shared_ptr< SLE > const &sle)=0
Insert a new state SLE.
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::ApplyView::ApplyView
ApplyView()=default
ripple::tapUNLIMITED
@ tapUNLIMITED
Definition: ApplyView.h:49
ripple::ApplyView::emptyDirDelete
bool emptyDirDelete(Keylet const &directory)
Remove the specified directory, if it is empty.
Definition: ApplyView.cpp:125
ripple::tapFAIL_HARD
@ tapFAIL_HARD
Definition: ApplyView.h:37
ripple::ApplyView::flags
virtual ApplyFlags flags() const =0
Returns the tx apply flags.
ripple::operator~
constexpr ApplyFlags operator~(ApplyFlags const &flags)
Definition: ApplyView.h:84