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 
31  tapNONE = 0x00,
32 
33  // This is a local transaction with the
34  // fail_hard flag set.
35  tapFAIL_HARD = 0x10,
36 
37  // This is not the transaction's last pass
38  // Transaction can be retried, soft failures allowed
39  tapRETRY = 0x20,
40 
41  // Transaction must pay more than both the open ledger
42  // fee and all transactions in the queue to get into the
43  // open ledger
45 
46  // Transaction came from a privileged source
47  tapUNLIMITED = 0x400,
48 };
49 
50 constexpr ApplyFlags
51 operator|(ApplyFlags const& lhs, ApplyFlags const& rhs)
52 {
53  return safe_cast<ApplyFlags>(
56 }
57 
58 static_assert(
59  (tapPREFER_QUEUE | tapRETRY) == safe_cast<ApplyFlags>(0x60u),
60  "ApplyFlags operator |");
61 static_assert(
62  (tapRETRY | tapPREFER_QUEUE) == safe_cast<ApplyFlags>(0x60u),
63  "ApplyFlags operator |");
64 
65 constexpr ApplyFlags
66 operator&(ApplyFlags const& lhs, ApplyFlags const& rhs)
67 {
68  return safe_cast<ApplyFlags>(
71 }
72 
73 static_assert((tapPREFER_QUEUE & tapRETRY) == tapNONE, "ApplyFlags operator &");
74 static_assert((tapRETRY & tapPREFER_QUEUE) == tapNONE, "ApplyFlags operator &");
75 
76 constexpr ApplyFlags
77 operator~(ApplyFlags const& flags)
78 {
79  return safe_cast<ApplyFlags>(
81 }
82 
83 static_assert(
84  ~tapRETRY == safe_cast<ApplyFlags>(0xFFFFFFDFu),
85  "ApplyFlags operator ~");
86 
87 inline ApplyFlags
88 operator|=(ApplyFlags& lhs, ApplyFlags const& rhs)
89 {
90  lhs = lhs | rhs;
91  return lhs;
92 }
93 
94 inline ApplyFlags
95 operator&=(ApplyFlags& lhs, ApplyFlags const& rhs)
96 {
97  lhs = lhs & rhs;
98  return lhs;
99 }
100 
101 //------------------------------------------------------------------------------
102 
140 class ApplyView : public ReadView
141 {
142 private:
144  boost::optional<std::uint64_t>
145  dirAdd(
146  bool preserveOrder,
147  Keylet const& directory,
148  uint256 const& key,
149  std::function<void(std::shared_ptr<SLE> const&)> const& describe);
150 
151 public:
152  ApplyView() = default;
153 
162  virtual ApplyFlags
163  flags() const = 0;
164 
179  virtual std::shared_ptr<SLE>
180  peek(Keylet const& k) = 0;
181 
193  virtual void
194  erase(std::shared_ptr<SLE> const& sle) = 0;
195 
214  virtual void
215  insert(std::shared_ptr<SLE> const& sle) = 0;
216 
233  virtual void
234  update(std::shared_ptr<SLE> const& sle) = 0;
235 
236  //--------------------------------------------------------------------------
237 
238  // Called when a credit is made to an account
239  // This is required to support PaymentSandbox
240  virtual void
242  AccountID const& from,
243  AccountID const& to,
244  STAmount const& amount,
245  STAmount const& preCreditBalance)
246  {
247  }
248 
249  // Called when the owner count changes
250  // This is required to support PaymentSandbox
251  virtual void
253  AccountID const& account,
254  std::uint32_t cur,
255  std::uint32_t next)
256  {
257  }
258 
277  boost::optional<std::uint64_t>
279  Keylet const& directory,
280  uint256 const& key,
281  std::function<void(std::shared_ptr<SLE> const&)> const& describe)
282  {
283  return dirAdd(true, directory, key, describe);
284  }
285 
286  boost::optional<std::uint64_t>
288  Keylet const& directory,
289  Keylet const& key,
290  std::function<void(std::shared_ptr<SLE> const&)> const& describe)
291  {
292  return dirAppend(directory, key.key, describe);
293  }
314  boost::optional<std::uint64_t>
316  Keylet const& directory,
317  uint256 const& key,
318  std::function<void(std::shared_ptr<SLE> const&)> const& describe)
319  {
320  return dirAdd(false, directory, key, describe);
321  }
322 
323  boost::optional<std::uint64_t>
325  Keylet const& directory,
326  Keylet const& key,
327  std::function<void(std::shared_ptr<SLE> const&)> const& describe)
328  {
329  return dirInsert(directory, key.key, describe);
330  }
349  bool
350  dirRemove(
351  Keylet const& directory,
352  std::uint64_t page,
353  uint256 const& key,
354  bool keepRoot);
355 
356  bool
358  Keylet const& directory,
359  std::uint64_t page,
360  Keylet const& key,
361  bool keepRoot)
362  {
363  return dirRemove(directory, page, key.key, keepRoot);
364  }
376  bool
377  emptyDirDelete(Keylet const& directory);
378 };
379 
380 } // namespace ripple
381 
382 #endif
ripple::operator&=
ApplyFlags operator&=(ApplyFlags &lhs, ApplyFlags const &rhs)
Definition: ApplyView.h:95
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:550
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:315
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:324
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:241
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:278
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:31
ripple::ApplyView
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:140
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:88
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:287
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::STAmount
Definition: STAmount.h:42
std::uint32_t
ripple::tapPREFER_QUEUE
@ tapPREFER_QUEUE
Definition: ApplyView.h:44
ripple::ApplyView::adjustOwnerCountHook
virtual void adjustOwnerCountHook(AccountID const &account, std::uint32_t cur, std::uint32_t next)
Definition: ApplyView.h:252
ripple::operator&
const base_uint< Bits, Tag > operator&(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition: base_uint.h:543
ripple::ApplyView::dirRemove
bool dirRemove(Keylet const &directory, std::uint64_t page, Keylet const &key, bool keepRoot)
Definition: ApplyView.h:357
ripple::tapRETRY
@ tapRETRY
Definition: ApplyView.h:39
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:47
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:35
ripple::ApplyView::flags
virtual ApplyFlags flags() const =0
Returns the tx apply flags.
ripple::operator~
constexpr ApplyFlags operator~(ApplyFlags const &flags)
Definition: ApplyView.h:77