rippled
Loading...
Searching...
No Matches
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 <xrpl/basics/safe_cast.h>
24#include <xrpl/beast/utility/instrumentation.h>
25#include <xrpl/ledger/RawView.h>
26#include <xrpl/ledger/ReadView.h>
27
28namespace ripple {
29
31 tapNONE = 0x00,
32
33 // This is a local transaction with the
34 // fail_hard flag set.
36
37 // This is not the transaction's last pass
38 // Transaction can be retried, soft failures allowed
39 tapRETRY = 0x20,
40
41 // Transaction came from a privileged source
42 tapUNLIMITED = 0x400,
43
44 // Transaction is executing as part of a batch
45 tapBATCH = 0x800,
46
47 // Transaction shouldn't be applied
48 // Signatures shouldn't be checked
49 tapDRY_RUN = 0x1000
50};
51
52constexpr ApplyFlags
53operator|(ApplyFlags const& lhs, ApplyFlags const& rhs)
54{
55 return safe_cast<ApplyFlags>(
58}
59
60static_assert(
61 (tapFAIL_HARD | tapRETRY) == safe_cast<ApplyFlags>(0x30u),
62 "ApplyFlags operator |");
63static_assert(
64 (tapRETRY | tapFAIL_HARD) == safe_cast<ApplyFlags>(0x30u),
65 "ApplyFlags operator |");
66
67constexpr ApplyFlags
68operator&(ApplyFlags const& lhs, ApplyFlags const& rhs)
69{
70 return safe_cast<ApplyFlags>(
73}
74
75static_assert((tapFAIL_HARD & tapRETRY) == tapNONE, "ApplyFlags operator &");
76static_assert((tapRETRY & tapFAIL_HARD) == tapNONE, "ApplyFlags operator &");
77
78constexpr ApplyFlags
79operator~(ApplyFlags const& flags)
80{
81 return safe_cast<ApplyFlags>(
83}
84
85static_assert(
86 ~tapRETRY == safe_cast<ApplyFlags>(0xFFFFFFDFu),
87 "ApplyFlags operator ~");
88
89inline ApplyFlags
91{
92 lhs = lhs | rhs;
93 return lhs;
94}
95
96inline ApplyFlags
98{
99 lhs = lhs & rhs;
100 return lhs;
101}
102
103//------------------------------------------------------------------------------
104
142class ApplyView : public ReadView
143{
144private:
147 dirAdd(
148 bool preserveOrder,
149 Keylet const& directory,
150 uint256 const& key,
151 std::function<void(std::shared_ptr<SLE> const&)> const& describe);
152
153public:
154 ApplyView() = default;
155
164 virtual ApplyFlags
165 flags() const = 0;
166
182 peek(Keylet const& k) = 0;
183
195 virtual void
197
216 virtual void
218
235 virtual void
237
238 //--------------------------------------------------------------------------
239
240 // Called when a credit is made to an account
241 // This is required to support PaymentSandbox
242 virtual void
244 AccountID const& from,
245 AccountID const& to,
246 STAmount const& amount,
247 STAmount const& preCreditBalance)
248 {
249 }
250
251 // Called when the owner count changes
252 // This is required to support PaymentSandbox
253 virtual void
255 AccountID const& account,
256 std::uint32_t cur,
257 std::uint32_t next)
258 {
259 }
260
281 Keylet const& directory,
282 Keylet const& key,
283 std::function<void(std::shared_ptr<SLE> const&)> const& describe)
284 {
285 if (key.type != ltOFFER)
286 {
287 // LCOV_EXCL_START
288 UNREACHABLE(
289 "ripple::ApplyView::dirAppend : only Offers are appended to "
290 "book directories");
291 // Only Offers are appended to book directories. Call dirInsert()
292 // instead
293 return std::nullopt;
294 // LCOV_EXCL_STOP
295 }
296 return dirAdd(true, directory, key.key, describe);
297 }
320 Keylet const& directory,
321 uint256 const& key,
322 std::function<void(std::shared_ptr<SLE> const&)> const& describe)
323 {
324 return dirAdd(false, directory, key, describe);
325 }
326
329 Keylet const& directory,
330 Keylet const& key,
331 std::function<void(std::shared_ptr<SLE> const&)> const& describe)
332 {
333 return dirAdd(false, directory, key.key, describe);
334 }
353 bool
354 dirRemove(
355 Keylet const& directory,
356 std::uint64_t page,
357 uint256 const& key,
358 bool keepRoot);
359
360 bool
362 Keylet const& directory,
363 std::uint64_t page,
364 Keylet const& key,
365 bool keepRoot)
366 {
367 return dirRemove(directory, page, key.key, keepRoot);
368 }
372 bool
373 dirDelete(
374 Keylet const& directory,
375 std::function<void(uint256 const&)> const&);
376
386 bool
387 emptyDirDelete(Keylet const& directory);
388};
389
390} // namespace ripple
391
392#endif
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:143
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:328
virtual void creditHook(AccountID const &from, AccountID const &to, STAmount const &amount, STAmount const &preCreditBalance)
Definition ApplyView.h:243
ApplyView()=default
bool dirDelete(Keylet const &directory, std::function< void(uint256 const &)> const &)
Remove the specified directory, invoking the callback for every node.
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
bool dirRemove(Keylet const &directory, std::uint64_t page, uint256 const &key, bool keepRoot)
Remove an entry from a directory.
bool emptyDirDelete(Keylet const &directory)
Remove the specified directory, if it is empty.
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:280
virtual void adjustOwnerCountHook(AccountID const &account, std::uint32_t cur, std::uint32_t next)
Definition ApplyView.h:254
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:31
virtual void insert(std::shared_ptr< SLE > const &sle)=0
Insert a new state SLE.
virtual ApplyFlags flags() const =0
Returns the tx apply flags.
bool dirRemove(Keylet const &directory, std::uint64_t page, Keylet const &key, bool keepRoot)
Definition ApplyView.h:361
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:319
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void erase(std::shared_ptr< SLE > const &sle)=0
Remove a peeked SLE.
A view into a ledger.
Definition ReadView.h:51
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
ApplyFlags operator|=(ApplyFlags &lhs, ApplyFlags const &rhs)
Definition ApplyView.h:90
constexpr base_uint< Bits, Tag > operator|(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition base_uint.h:615
constexpr std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > safe_cast(Src s) noexcept
Definition safe_cast.h:41
ApplyFlags operator&=(ApplyFlags &lhs, ApplyFlags const &rhs)
Definition ApplyView.h:97
constexpr ApplyFlags operator~(ApplyFlags const &flags)
Definition ApplyView.h:79
constexpr base_uint< Bits, Tag > operator&(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition base_uint.h:608
@ tapFAIL_HARD
Definition ApplyView.h:35
@ tapUNLIMITED
Definition ApplyView.h:42
@ tapRETRY
Definition ApplyView.h:39
@ tapDRY_RUN
Definition ApplyView.h:49
@ tapBATCH
Definition ApplyView.h:45
@ tapNONE
Definition ApplyView.h:31
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:39
LedgerEntryType type
Definition Keylet.h:41
uint256 key
Definition Keylet.h:40