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 
27 namespace ripple {
28 
30  tapNONE = 0x00,
31 
32  // This is a local transaction with the
33  // fail_hard flag set.
34  tapFAIL_HARD = 0x10,
35 
36  // This is not the transaction's last pass
37  // Transaction can be retried, soft failures allowed
38  tapRETRY = 0x20,
39 
40  // Transaction must pay more than both the open ledger
41  // fee and all transactions in the queue to get into the
42  // open ledger
44 
45  // Transaction came from a privileged source
46  tapUNLIMITED = 0x400,
47 };
48 
49 constexpr ApplyFlags
50 operator|(ApplyFlags const& lhs, ApplyFlags const& rhs)
51 {
52  return safe_cast<ApplyFlags>(
55 }
56 
57 static_assert(
58  (tapPREFER_QUEUE | tapRETRY) == safe_cast<ApplyFlags>(0x60u),
59  "ApplyFlags operator |");
60 static_assert(
61  (tapRETRY | tapPREFER_QUEUE) == safe_cast<ApplyFlags>(0x60u),
62  "ApplyFlags operator |");
63 
64 constexpr ApplyFlags
65 operator&(ApplyFlags const& lhs, ApplyFlags const& rhs)
66 {
67  return safe_cast<ApplyFlags>(
70 }
71 
72 static_assert((tapPREFER_QUEUE & tapRETRY) == tapNONE, "ApplyFlags operator &");
73 static_assert((tapRETRY & tapPREFER_QUEUE) == tapNONE, "ApplyFlags operator &");
74 
75 constexpr ApplyFlags
76 operator~(ApplyFlags const& flags)
77 {
78  return safe_cast<ApplyFlags>(
80 }
81 
82 static_assert(
83  ~tapRETRY == safe_cast<ApplyFlags>(0xFFFFFFDFu),
84  "ApplyFlags operator ~");
85 
86 inline ApplyFlags
87 operator|=(ApplyFlags& lhs, ApplyFlags const& rhs)
88 {
89  lhs = lhs | rhs;
90  return lhs;
91 }
92 
93 inline ApplyFlags
94 operator&=(ApplyFlags& lhs, ApplyFlags const& rhs)
95 {
96  lhs = lhs & rhs;
97  return lhs;
98 }
99 
100 //------------------------------------------------------------------------------
101 
139 class ApplyView : public ReadView
140 {
141 private:
144  dirAdd(
145  bool preserveOrder,
146  Keylet const& directory,
147  uint256 const& key,
148  std::function<void(std::shared_ptr<SLE> const&)> const& describe);
149 
150 public:
151  ApplyView() = default;
152 
161  virtual ApplyFlags
162  flags() const = 0;
163 
178  virtual std::shared_ptr<SLE>
179  peek(Keylet const& k) = 0;
180 
192  virtual void
193  erase(std::shared_ptr<SLE> const& sle) = 0;
194 
213  virtual void
214  insert(std::shared_ptr<SLE> const& sle) = 0;
215 
232  virtual void
233  update(std::shared_ptr<SLE> const& sle) = 0;
234 
235  //--------------------------------------------------------------------------
236 
237  // Called when a credit is made to an account
238  // This is required to support PaymentSandbox
239  virtual void
241  AccountID const& from,
242  AccountID const& to,
243  STAmount const& amount,
244  STAmount const& preCreditBalance)
245  {
246  }
247 
248  // Called when the owner count changes
249  // This is required to support PaymentSandbox
250  virtual void
252  AccountID const& account,
253  std::uint32_t cur,
254  std::uint32_t next)
255  {
256  }
257 
278  Keylet const& directory,
279  Keylet const& key,
280  std::function<void(std::shared_ptr<SLE> const&)> const& describe)
281  {
282  if (key.type != ltOFFER)
283  {
284  assert(!"Only Offers are appended to book directories. "
285  "Call dirInsert() instead.");
286  return std::nullopt;
287  }
288  return dirAdd(true, directory, key.key, describe);
289  }
312  Keylet const& directory,
313  uint256 const& key,
314  std::function<void(std::shared_ptr<SLE> const&)> const& describe)
315  {
316  return dirAdd(false, directory, key, describe);
317  }
318 
321  Keylet const& directory,
322  Keylet const& key,
323  std::function<void(std::shared_ptr<SLE> const&)> const& describe)
324  {
325  return dirAdd(false, directory, key.key, describe);
326  }
345  bool
346  dirRemove(
347  Keylet const& directory,
348  std::uint64_t page,
349  uint256 const& key,
350  bool keepRoot);
351 
352  bool
354  Keylet const& directory,
355  std::uint64_t page,
356  Keylet const& key,
357  bool keepRoot)
358  {
359  return dirRemove(directory, page, key.key, keepRoot);
360  }
372  bool
373  emptyDirDelete(Keylet const& directory);
374 };
375 
376 } // namespace ripple
377 
378 #endif
ripple::operator&=
ApplyFlags operator&=(ApplyFlags &lhs, ApplyFlags const &rhs)
Definition: ApplyView.h:94
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:622
std::shared_ptr
STL class.
ripple::ApplyView::dirInsert
std::optional< std::uint64_t > dirInsert(Keylet const &directory, Keylet const &key, std::function< void(std::shared_ptr< SLE > const &)> const &describe)
Definition: ApplyView.h:320
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::ApplyFlags
ApplyFlags
Definition: ApplyView.h:29
ripple::ApplyView::creditHook
virtual void creditHook(AccountID const &from, AccountID const &to, STAmount const &amount, STAmount const &preCreditBalance)
Definition: ApplyView.h:240
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:30
ripple::ApplyView
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:139
std::underlying_type_t
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:40
ripple::base_uint< 256 >
ripple::operator|=
ApplyFlags operator|=(ApplyFlags &lhs, ApplyFlags const &rhs)
Definition: ApplyView.h:87
ripple::ltOFFER
@ ltOFFER
A ledger object which describes an offer on the DEX.
Definition: LedgerFormats.h:92
ripple::ApplyView::dirAppend
std::optional< std::uint64_t > dirAppend(Keylet const &directory, Keylet const &key, std::function< void(std::shared_ptr< SLE > const &)> const &describe)
Append an entry to a directory.
Definition: ApplyView.h:277
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:537
ripple::ApplyView::dirAdd
std::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::Keylet::type
LedgerEntryType type
Definition: Keylet.h:41
ripple::STAmount
Definition: STAmount.h:43
std::uint32_t
ripple::tapPREFER_QUEUE
@ tapPREFER_QUEUE
Definition: ApplyView.h:43
ripple::ApplyView::adjustOwnerCountHook
virtual void adjustOwnerCountHook(AccountID const &account, std::uint32_t cur, std::uint32_t next)
Definition: ApplyView.h:251
ripple::operator&
const base_uint< Bits, Tag > operator&(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition: base_uint.h:615
ripple::ApplyView::dirRemove
bool dirRemove(Keylet const &directory, std::uint64_t page, Keylet const &key, bool keepRoot)
Definition: ApplyView.h:353
ripple::tapRETRY
@ tapRETRY
Definition: ApplyView.h:38
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:192
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:46
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:34
std::optional
ripple::ApplyView::flags
virtual ApplyFlags flags() const =0
Returns the tx apply flags.
ripple::ApplyView::dirInsert
std::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:311
ripple::operator~
constexpr ApplyFlags operator~(ApplyFlags const &flags)
Definition: ApplyView.h:76