xrpld
Loading...
Searching...
No Matches
ApplyView.h
1#pragma once
2
3#include <xrpl/basics/base_uint.h>
4#include <xrpl/basics/safe_cast.h>
5#include <xrpl/beast/utility/instrumentation.h>
6#include <xrpl/ledger/OwnerCounts.h>
7#include <xrpl/ledger/ReadView.h>
8#include <xrpl/protocol/AccountID.h>
9#include <xrpl/protocol/Issue.h> // IWYU pragma: keep
10#include <xrpl/protocol/Keylet.h>
11#include <xrpl/protocol/LedgerFormats.h>
12#include <xrpl/protocol/MPTIssue.h>
13#include <xrpl/protocol/STAmount.h>
14#include <xrpl/protocol/STLedgerEntry.h>
15#include <xrpl/protocol/STTx.h>
16#include <xrpl/protocol/STVector256.h>
17
18#include <cstdint>
19#include <functional>
20#include <optional>
21#include <type_traits>
22
23namespace xrpl {
24
25// Bitwise flag enum with existing operator overloads
26// NOLINTNEXTLINE(cppcoreguidelines-use-enum-class)
28 TapNone = 0x00,
29
30 // This is a local transaction with the
31 // fail_hard flag set.
33
34 // This is not the transaction's last pass
35 // Transaction can be retried, soft failures allowed
36 TapRetry = 0x20,
37
38 // Transaction came from a privileged source
39 TapUnlimited = 0x400,
40
41 // Transaction is executing as part of a batch
42 TapBatch = 0x800,
43
44 // Transaction shouldn't be applied
45 // Signatures shouldn't be checked
46 TapDryRun = 0x1000
47};
48
49constexpr ApplyFlags
56
57static_assert((TapFailHard | TapRetry) == safeCast<ApplyFlags>(0x30u), "ApplyFlags operator |");
58static_assert((TapRetry | TapFailHard) == safeCast<ApplyFlags>(0x30u), "ApplyFlags operator |");
59
60constexpr ApplyFlags
67
68static_assert((TapFailHard & TapRetry) == TapNone, "ApplyFlags operator &");
69static_assert((TapRetry & TapFailHard) == TapNone, "ApplyFlags operator &");
70
71constexpr ApplyFlags
76
77static_assert(~TapRetry == safeCast<ApplyFlags>(0xFFFFFFDFu), "ApplyFlags operator ~");
78
79inline ApplyFlags
81{
82 lhs = lhs | rhs;
83 return lhs;
84}
85
86inline ApplyFlags
88{
89 lhs = lhs & rhs;
90 return lhs;
91}
92
93//------------------------------------------------------------------------------
94
133class ApplyView : public ReadView
134{
135private:
140 dirAdd(
141 bool preserveOrder,
142 Keylet const& directory,
143 uint256 const& key,
144 std::function<void(SLE::ref)> const& describe);
145
146public:
147 ApplyView() = default;
148
158 [[nodiscard]] virtual ApplyFlags
159 flags() const = 0;
160
176 virtual SLE::pointer
177 peek(Keylet const& k) = 0;
178
191 virtual void
192 erase(SLE::ref sle) = 0;
193
213 virtual void
214 insert(SLE::ref sle) = 0;
215
233 virtual void
234 update(SLE::ref 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 XRPL_ASSERT(amount.holds<Issue>(), "creditHookIOU: amount is for Issue");
248 }
249
250 virtual void
252 AccountID const& from,
253 AccountID const& to,
254 STAmount const& amount,
255 std::uint64_t preCreditBalanceHolder,
256 std::int64_t preCreditBalanceIssuer)
257 {
258 XRPL_ASSERT(amount.holds<MPTIssue>(), "creditHookMPT: amount is for MPTIssue");
259 }
260
294 virtual void
296 {
297 }
298
299 // Called when the owner count changes
300 // This is required to support PaymentSandbox
301 virtual void
302 adjustOwnerCountHook(AccountID const& account, OwnerCounts const& cur, OwnerCounts const& next)
303 {
304 }
305
327 Keylet const& directory,
328 Keylet const& key,
329 std::function<void(SLE::ref)> const& describe)
330 {
331 if (key.type != ltOFFER)
332 {
333 // LCOV_EXCL_START
334 UNREACHABLE(
335 "xrpl::ApplyView::dirAppend : only Offers are appended to "
336 "book directories");
337 // Only Offers are appended to book directories. Call dirInsert()
338 // instead
339 return std::nullopt;
340 // LCOV_EXCL_STOP
341 }
342 return dirAdd(true, directory, key.key, describe);
343 }
344
345
367 Keylet const& directory,
368 uint256 const& key,
369 std::function<void(SLE::ref)> const& describe)
370 {
371 return dirAdd(false, directory, key, describe);
372 }
373
376 Keylet const& directory,
377 Keylet const& key,
378 std::function<void(SLE::ref)> const& describe)
379 {
380 return dirAdd(false, directory, key.key, describe);
381 }
382
383
401 bool
402 dirRemove(Keylet const& directory, std::uint64_t page, uint256 const& key, bool keepRoot);
403
404 bool
405 dirRemove(Keylet const& directory, std::uint64_t page, Keylet const& key, bool keepRoot)
406 {
407 return dirRemove(directory, page, key.key, keepRoot);
408 }
409
410
414 bool
415 dirDelete(Keylet const& directory, std::function<void(uint256 const&)> const&);
416
427 bool
429};
430
444{
446 STTx const& tx;
447};
448
449namespace directory {
457
460 ApplyView& view,
461 Keylet const& directory,
462 uint256 const& key,
463 std::function<void(SLE::ref)> const& describe);
464
465auto
467
470 ApplyView& view,
471 SLE::ref node,
472 std::uint64_t page,
473 bool preserveOrder,
474 STVector256& indexes,
475 uint256 const& key);
476
479 ApplyView& view,
480 std::uint64_t page,
481 SLE::pointer node,
482 std::uint64_t nextPage,
483 SLE::ref next,
484 uint256 const& key,
485 Keylet const& directory,
486 std::function<void(SLE::ref)> const& describe);
487
488} // namespace directory
489} // namespace xrpl
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:134
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:241
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:405
virtual void creditHookMPT(AccountID const &from, AccountID const &to, STAmount const &amount, std::uint64_t preCreditBalanceHolder, std::int64_t preCreditBalanceIssuer)
Definition ApplyView.h:251
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:295
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:326
virtual void adjustOwnerCountHook(AccountID const &account, OwnerCounts const &cur, OwnerCounts const &next)
Definition ApplyView.h:302
std::optional< std::uint64_t > dirInsert(Keylet const &directory, Keylet const &key, std::function< void(SLE::ref)> const &describe)
Definition ApplyView.h:375
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:366
virtual void update(SLE::ref sle)=0
Indicate changes to a peeked SLE.
A currency issued by an account.
Definition Issue.h:18
constexpr bool holds() const noexcept
Definition STAmount.h:478
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:629
constexpr ApplyFlags operator~(ApplyFlags const &flags)
Definition ApplyView.h:72
constexpr HashRouterFlags & operator|=(HashRouterFlags &lhs, HashRouterFlags rhs)
Definition HashRouter.h:49
constexpr BaseUInt< Bits, Tag > operator|(BaseUInt< Bits, Tag > const &a, BaseUInt< Bits, Tag > const &b)
Definition base_uint.h:636
constexpr HashRouterFlags & operator&=(HashRouterFlags &lhs, HashRouterFlags rhs)
Definition HashRouter.h:64
constexpr Dest safeCast(Src s) noexcept
Definition safe_cast.h:21
ApplyFlags
Definition ApplyView.h:27
@ TapDryRun
Definition ApplyView.h:46
@ TapUnlimited
Definition ApplyView.h:39
@ TapFailHard
Definition ApplyView.h:32
@ TapNone
Definition ApplyView.h:28
@ TapRetry
Definition ApplyView.h:36
@ TapBatch
Definition ApplyView.h:42
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
BaseUInt< 256 > uint256
Definition base_uint.h:580
Bundles the mutable ledger view and the transaction being applied.
Definition ApplyView.h:444
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
uint256 key
Definition Keylet.h:21
LedgerEntryType type
Definition Keylet.h:22