xrpld
Loading...
Searching...
No Matches
ApplyView.h
1#pragma once
2
3#include <xrpl/basics/safe_cast.h>
4#include <xrpl/beast/utility/instrumentation.h>
5#include <xrpl/ledger/RawView.h>
6#include <xrpl/ledger/ReadView.h>
7
8namespace xrpl {
9
10// Bitwise flag enum with existing operator overloads
11// NOLINTNEXTLINE(cppcoreguidelines-use-enum-class)
13 TapNone = 0x00,
14
15 // This is a local transaction with the
16 // fail_hard flag set.
18
19 // This is not the transaction's last pass
20 // Transaction can be retried, soft failures allowed
21 TapRetry = 0x20,
22
23 // Transaction came from a privileged source
24 TapUnlimited = 0x400,
25
26 // Transaction is executing as part of a batch
27 TapBatch = 0x800,
28
29 // Transaction shouldn't be applied
30 // Signatures shouldn't be checked
31 TapDryRun = 0x1000
32};
33
34constexpr ApplyFlags
41
42static_assert((TapFailHard | TapRetry) == safeCast<ApplyFlags>(0x30u), "ApplyFlags operator |");
43static_assert((TapRetry | TapFailHard) == safeCast<ApplyFlags>(0x30u), "ApplyFlags operator |");
44
45constexpr ApplyFlags
52
53static_assert((TapFailHard & TapRetry) == TapNone, "ApplyFlags operator &");
54static_assert((TapRetry & TapFailHard) == TapNone, "ApplyFlags operator &");
55
56constexpr ApplyFlags
61
62static_assert(~TapRetry == safeCast<ApplyFlags>(0xFFFFFFDFu), "ApplyFlags operator ~");
63
64inline ApplyFlags
66{
67 lhs = lhs | rhs;
68 return lhs;
69}
70
71inline ApplyFlags
73{
74 lhs = lhs & rhs;
75 return lhs;
76}
77
78//------------------------------------------------------------------------------
79
117class ApplyView : public ReadView
118{
119private:
122 dirAdd(
123 bool preserveOrder,
124 Keylet const& directory,
125 uint256 const& key,
126 std::function<void(SLE::ref)> const& describe);
127
128public:
129 ApplyView() = default;
130
139 [[nodiscard]] virtual ApplyFlags
140 flags() const = 0;
141
156 virtual SLE::pointer
157 peek(Keylet const& k) = 0;
158
170 virtual void
171 erase(SLE::ref sle) = 0;
172
191 virtual void
192 insert(SLE::ref sle) = 0;
193
210 virtual void
211 update(SLE::ref sle) = 0;
212
213 //--------------------------------------------------------------------------
214
215 // Called when a credit is made to an account
216 // This is required to support PaymentSandbox
217 virtual void
219 AccountID const& from,
220 AccountID const& to,
221 STAmount const& amount,
222 STAmount const& preCreditBalance)
223 {
224 XRPL_ASSERT(amount.holds<Issue>(), "creditHookIOU: amount is for Issue");
225 }
226
227 virtual void
229 AccountID const& from,
230 AccountID const& to,
231 STAmount const& amount,
232 std::uint64_t preCreditBalanceHolder,
233 std::int64_t preCreditBalanceIssuer)
234 {
235 XRPL_ASSERT(amount.holds<MPTIssue>(), "creditHookMPT: amount is for MPTIssue");
236 }
237
270 virtual void
272 {
273 }
274
275 // Called when the owner count changes
276 // This is required to support PaymentSandbox
277 virtual void
279 {
280 }
281
302 Keylet const& directory,
303 Keylet const& key,
304 std::function<void(SLE::ref)> const& describe)
305 {
306 if (key.type != ltOFFER)
307 {
308 // LCOV_EXCL_START
309 UNREACHABLE(
310 "xrpl::ApplyView::dirAppend : only Offers are appended to "
311 "book directories");
312 // Only Offers are appended to book directories. Call dirInsert()
313 // instead
314 return std::nullopt;
315 // LCOV_EXCL_STOP
316 }
317 return dirAdd(true, directory, key.key, describe);
318 }
319
320
341 Keylet const& directory,
342 uint256 const& key,
343 std::function<void(SLE::ref)> const& describe)
344 {
345 return dirAdd(false, directory, key, describe);
346 }
347
350 Keylet const& directory,
351 Keylet const& key,
352 std::function<void(SLE::ref)> const& describe)
353 {
354 return dirAdd(false, directory, key.key, describe);
355 }
356
357
374 bool
375 dirRemove(Keylet const& directory, std::uint64_t page, uint256 const& key, bool keepRoot);
376
377 bool
378 dirRemove(Keylet const& directory, std::uint64_t page, Keylet const& key, bool keepRoot)
379 {
380 return dirRemove(directory, page, key.key, keepRoot);
381 }
382
383
385 bool
386 dirDelete(Keylet const& directory, std::function<void(uint256 const&)> const&);
387
397 bool
399};
400
401namespace directory {
408
411 ApplyView& view,
412 Keylet const& directory,
413 uint256 const& key,
414 std::function<void(SLE::ref)> const& describe);
415
416auto
418
421 ApplyView& view,
422 SLE::ref node,
423 std::uint64_t page,
424 bool preserveOrder,
425 STVector256& indexes,
426 uint256 const& key);
427
430 ApplyView& view,
431 std::uint64_t page,
432 SLE::pointer node,
433 std::uint64_t nextPage,
434 SLE::ref next,
435 uint256 const& key,
436 Keylet const& directory,
437 std::function<void(SLE::ref)> const& describe);
438
439} // namespace directory
440} // namespace xrpl
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:118
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void insert(SLE::ref sle)=0
Insert a new state SLE.
std::optional< std::uint64_t > dirAdd(bool preserveOrder, Keylet const &directory, uint256 const &key, std::function< void(SLE::ref)> const &describe)
Add an entry to a directory using the specified insert strategy.
bool dirRemove(Keylet const &directory, std::uint64_t page, uint256 const &key, bool keepRoot)
Remove an entry from a directory.
virtual void creditHookIOU(AccountID const &from, AccountID const &to, STAmount const &amount, STAmount const &preCreditBalance)
Definition ApplyView.h:218
virtual void erase(SLE::ref sle)=0
Remove a peeked SLE.
ApplyView()=default
bool dirRemove(Keylet const &directory, std::uint64_t page, Keylet const &key, bool keepRoot)
Definition ApplyView.h:378
virtual void creditHookMPT(AccountID const &from, AccountID const &to, STAmount const &amount, std::uint64_t preCreditBalanceHolder, std::int64_t preCreditBalanceIssuer)
Definition ApplyView.h:228
bool dirDelete(Keylet const &directory, std::function< void(uint256 const &)> const &)
Remove the specified directory, invoking the callback for every node.
virtual ApplyFlags flags() const =0
Returns the tx apply flags.
virtual void issuerSelfDebitHookMPT(MPTIssue const &issue, std::uint64_t amount, std::int64_t origBalance)
Facilitate tracking of MPT sold by an issuer owning MPT sell offer.
Definition ApplyView.h:271
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(SLE::ref)> const &describe)
Append an entry to a directory.
Definition ApplyView.h:301
virtual void adjustOwnerCountHook(AccountID const &account, std::uint32_t cur, std::uint32_t next)
Definition ApplyView.h:278
std::optional< std::uint64_t > dirInsert(Keylet const &directory, Keylet const &key, std::function< void(SLE::ref)> const &describe)
Definition ApplyView.h:349
std::optional< std::uint64_t > dirInsert(Keylet const &directory, uint256 const &key, std::function< void(SLE::ref)> const &describe)
Insert an entry to a directory.
Definition ApplyView.h:340
virtual void update(SLE::ref sle)=0
Indicate changes to a peeked SLE.
A currency issued by an account.
Definition Issue.h:13
constexpr bool holds() const noexcept
Definition STAmount.h:460
std::shared_ptr< STLedgerEntry > const & ref
std::shared_ptr< STLedgerEntry > pointer
auto findPreviousPage(ApplyView &view, Keylet const &directory, SLE::ref start)
Definition ApplyView.cpp:49
std::uint64_t insertKey(ApplyView &view, SLE::ref node, std::uint64_t page, bool preserveOrder, STVector256 &indexes, uint256 const &key)
Definition ApplyView.cpp:70
std::optional< std::uint64_t > insertPage(ApplyView &view, std::uint64_t page, SLE::pointer node, std::uint64_t nextPage, SLE::ref next, uint256 const &key, Keylet const &directory, std::function< void(SLE::ref)> const &describe)
std::uint64_t createRoot(ApplyView &view, Keylet const &directory, uint256 const &key, std::function< void(SLE::ref)> const &describe)
Helper functions for managing low-level directory operations.
Definition ApplyView.cpp:30
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr BaseUInt< Bits, Tag > operator&(BaseUInt< Bits, Tag > const &a, BaseUInt< Bits, Tag > const &b)
Definition base_uint.h:611
constexpr ApplyFlags operator~(ApplyFlags const &flags)
Definition ApplyView.h:57
constexpr HashRouterFlags & operator|=(HashRouterFlags &lhs, HashRouterFlags rhs)
Definition HashRouter.h:41
constexpr BaseUInt< Bits, Tag > operator|(BaseUInt< Bits, Tag > const &a, BaseUInt< Bits, Tag > const &b)
Definition base_uint.h:618
constexpr std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > safeCast(Src s) noexcept
Definition safe_cast.h:21
constexpr HashRouterFlags & operator&=(HashRouterFlags &lhs, HashRouterFlags rhs)
Definition HashRouter.h:56
ApplyFlags
Definition ApplyView.h:12
@ TapDryRun
Definition ApplyView.h:31
@ TapUnlimited
Definition ApplyView.h:24
@ TapFailHard
Definition ApplyView.h:17
@ TapNone
Definition ApplyView.h:13
@ TapRetry
Definition ApplyView.h:21
@ TapBatch
Definition ApplyView.h:27
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
BaseUInt< 256 > uint256
Definition base_uint.h:562
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:19
uint256 key
Definition Keylet.h:20
LedgerEntryType type
Definition Keylet.h:21