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 came from a privileged source
41  tapUNLIMITED = 0x400,
42 };
43 
44 constexpr ApplyFlags
45 operator|(ApplyFlags const& lhs, ApplyFlags const& rhs)
46 {
47  return safe_cast<ApplyFlags>(
50 }
51 
52 static_assert(
53  (tapFAIL_HARD | tapRETRY) == safe_cast<ApplyFlags>(0x30u),
54  "ApplyFlags operator |");
55 static_assert(
56  (tapRETRY | tapFAIL_HARD) == safe_cast<ApplyFlags>(0x30u),
57  "ApplyFlags operator |");
58 
59 constexpr ApplyFlags
60 operator&(ApplyFlags const& lhs, ApplyFlags const& rhs)
61 {
62  return safe_cast<ApplyFlags>(
65 }
66 
67 static_assert((tapFAIL_HARD & tapRETRY) == tapNONE, "ApplyFlags operator &");
68 static_assert((tapRETRY & tapFAIL_HARD) == tapNONE, "ApplyFlags operator &");
69 
70 constexpr ApplyFlags
71 operator~(ApplyFlags const& flags)
72 {
73  return safe_cast<ApplyFlags>(
75 }
76 
77 static_assert(
78  ~tapRETRY == safe_cast<ApplyFlags>(0xFFFFFFDFu),
79  "ApplyFlags operator ~");
80 
81 inline ApplyFlags
82 operator|=(ApplyFlags& lhs, ApplyFlags const& rhs)
83 {
84  lhs = lhs | rhs;
85  return lhs;
86 }
87 
88 inline ApplyFlags
89 operator&=(ApplyFlags& lhs, ApplyFlags const& rhs)
90 {
91  lhs = lhs & rhs;
92  return lhs;
93 }
94 
95 //------------------------------------------------------------------------------
96 
134 class ApplyView : public ReadView
135 {
136 private:
139  dirAdd(
140  bool preserveOrder,
141  Keylet const& directory,
142  uint256 const& key,
143  std::function<void(std::shared_ptr<SLE> const&)> const& describe);
144 
145 public:
146  ApplyView() = default;
147 
156  virtual ApplyFlags
157  flags() const = 0;
158 
173  virtual std::shared_ptr<SLE>
174  peek(Keylet const& k) = 0;
175 
187  virtual void
188  erase(std::shared_ptr<SLE> const& sle) = 0;
189 
208  virtual void
209  insert(std::shared_ptr<SLE> const& sle) = 0;
210 
227  virtual void
228  update(std::shared_ptr<SLE> const& sle) = 0;
229 
230  //--------------------------------------------------------------------------
231 
232  // Called when a credit is made to an account
233  // This is required to support PaymentSandbox
234  virtual void
236  AccountID const& from,
237  AccountID const& to,
238  STAmount const& amount,
239  STAmount const& preCreditBalance)
240  {
241  }
242 
243  // Called when the owner count changes
244  // This is required to support PaymentSandbox
245  virtual void
247  AccountID const& account,
248  std::uint32_t cur,
249  std::uint32_t next)
250  {
251  }
252 
273  Keylet const& directory,
274  Keylet const& key,
275  std::function<void(std::shared_ptr<SLE> const&)> const& describe)
276  {
277  if (key.type != ltOFFER)
278  {
279  assert(!"Only Offers are appended to book directories. "
280  "Call dirInsert() instead.");
281  return std::nullopt;
282  }
283  return dirAdd(true, directory, key.key, describe);
284  }
307  Keylet const& directory,
308  uint256 const& key,
309  std::function<void(std::shared_ptr<SLE> const&)> const& describe)
310  {
311  return dirAdd(false, directory, key, describe);
312  }
313 
316  Keylet const& directory,
317  Keylet const& key,
318  std::function<void(std::shared_ptr<SLE> const&)> const& describe)
319  {
320  return dirAdd(false, directory, key.key, describe);
321  }
340  bool
341  dirRemove(
342  Keylet const& directory,
343  std::uint64_t page,
344  uint256 const& key,
345  bool keepRoot);
346 
347  bool
349  Keylet const& directory,
350  std::uint64_t page,
351  Keylet const& key,
352  bool keepRoot)
353  {
354  return dirRemove(directory, page, key.key, keepRoot);
355  }
359  bool
360  dirDelete(
361  Keylet const& directory,
362  std::function<void(uint256 const&)> const&);
363 
373  bool
374  emptyDirDelete(Keylet const& directory);
375 };
376 
377 } // namespace ripple
378 
379 #endif
ripple::operator&=
ApplyFlags operator&=(ApplyFlags &lhs, ApplyFlags const &rhs)
Definition: ApplyView.h:89
ripple::Keylet
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:38
ripple::operator|
constexpr base_uint< Bits, Tag > operator|(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition: base_uint.h:606
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:315
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:235
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:134
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:82
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:272
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:532
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:45
ripple::ApplyView::dirDelete
bool dirDelete(Keylet const &directory, std::function< void(uint256 const &)> const &)
Remove the specified directory, invoking the callback for every node.
Definition: ApplyView.cpp:338
std::uint32_t
ripple::ApplyView::adjustOwnerCountHook
virtual void adjustOwnerCountHook(AccountID const &account, std::uint32_t cur, std::uint32_t next)
Definition: ApplyView.h:246
ripple::ApplyView::dirRemove
bool dirRemove(Keylet const &directory, std::uint64_t page, Keylet const &key, bool keepRoot)
Definition: ApplyView.h:348
ripple::tapRETRY
@ tapRETRY
Definition: ApplyView.h:38
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:125
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::operator&
constexpr base_uint< Bits, Tag > operator&(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition: base_uint.h:599
ripple::ApplyView::ApplyView
ApplyView()=default
ripple::tapUNLIMITED
@ tapUNLIMITED
Definition: ApplyView.h:41
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:306
ripple::operator~
constexpr ApplyFlags operator~(ApplyFlags const &flags)
Definition: ApplyView.h:71