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 // Transaction is being preflighted as the payload of a
49 // TransactionProposalCreate. Its signatures are collected on-ledger
50 // afterward, so signature-presence checks (e.g. Batch signer matching)
51 // are skipped at proposal-creation time (On-Chain Cosigner spec
52 // ยง5.3.1.2).
53 TapProposal = 0x2000
54};
55
56constexpr ApplyFlags
63
64static_assert((TapFailHard | TapRetry) == safeCast<ApplyFlags>(0x30u), "ApplyFlags operator |");
65static_assert((TapRetry | TapFailHard) == safeCast<ApplyFlags>(0x30u), "ApplyFlags operator |");
66
67constexpr ApplyFlags
74
75static_assert((TapFailHard & TapRetry) == TapNone, "ApplyFlags operator &");
76static_assert((TapRetry & TapFailHard) == TapNone, "ApplyFlags operator &");
77
78constexpr ApplyFlags
83
84static_assert(~TapRetry == safeCast<ApplyFlags>(0xFFFFFFDFu), "ApplyFlags operator ~");
85
86inline ApplyFlags
88{
89 lhs = lhs | rhs;
90 return lhs;
91}
92
93inline ApplyFlags
95{
96 lhs = lhs & rhs;
97 return lhs;
98}
99
100//------------------------------------------------------------------------------
101
140class ApplyView : public ReadView
141{
142private:
147 dirAdd(
148 bool preserveOrder,
149 Keylet const& directory,
150 UInt256 const& key,
151 std::function<void(SLE::Ref)> const& describe);
152
153public:
154 ApplyView() = default;
155
165 [[nodiscard]] virtual ApplyFlags
166 flags() const = 0;
167
183 virtual SLE::pointer
184 peek(Keylet const& k) = 0;
185
198 virtual void
199 erase(SLE::Ref sle) = 0;
200
220 virtual void
221 insert(SLE::Ref sle) = 0;
222
240 virtual void
241 update(SLE::Ref sle) = 0;
242
243 //--------------------------------------------------------------------------
244
245 // Called when a credit is made to an account
246 // This is required to support PaymentSandbox
247 virtual void
249 AccountID const& from,
250 AccountID const& to,
251 STAmount const& amount,
252 STAmount const& preCreditBalance)
253 {
254 XRPL_ASSERT(amount.holds<Issue>(), "creditHookIOU: amount is for Issue");
255 }
256
257 virtual void
259 AccountID const& from,
260 AccountID const& to,
261 STAmount const& amount,
262 std::uint64_t preCreditBalanceHolder,
263 std::int64_t preCreditBalanceIssuer)
264 {
265 XRPL_ASSERT(amount.holds<MPTIssue>(), "creditHookMPT: amount is for MPTIssue");
266 }
267
301 virtual void
303 {
304 }
305
306 // Called when the owner count changes
307 // This is required to support PaymentSandbox
308 virtual void
309 adjustOwnerCountHook(AccountID const& account, OwnerCounts const& cur, OwnerCounts const& next)
310 {
311 }
312
334 Keylet const& directory,
335 Keylet const& key,
336 std::function<void(SLE::Ref)> const& describe)
337 {
338 if (key.type != ltOFFER)
339 {
340 // LCOV_EXCL_START
341 UNREACHABLE(
342 "xrpl::ApplyView::dirAppend : only Offers are appended to "
343 "book directories");
344 // Only Offers are appended to book directories. Call dirInsert()
345 // instead
346 return std::nullopt;
347 // LCOV_EXCL_STOP
348 }
349 return dirAdd(true, directory, key.key, describe);
350 }
351
352
374 Keylet const& directory,
375 UInt256 const& key,
376 std::function<void(SLE::Ref)> const& describe)
377 {
378 return dirAdd(false, directory, key, describe);
379 }
380
383 Keylet const& directory,
384 Keylet const& key,
385 std::function<void(SLE::Ref)> const& describe)
386 {
387 return dirAdd(false, directory, key.key, describe);
388 }
389
390
408 bool
409 dirRemove(Keylet const& directory, std::uint64_t page, UInt256 const& key, bool keepRoot);
410
411 bool
412 dirRemove(Keylet const& directory, std::uint64_t page, Keylet const& key, bool keepRoot)
413 {
414 return dirRemove(directory, page, key.key, keepRoot);
415 }
416
417
421 bool
422 dirDelete(Keylet const& directory, std::function<void(UInt256 const&)> const&);
423
434 bool
436};
437
451{
453 STTx const& tx;
454};
455
456namespace directory {
464
467 ApplyView& view,
468 Keylet const& directory,
469 UInt256 const& key,
470 std::function<void(SLE::Ref)> const& describe);
471
472auto
474
477 ApplyView& view,
478 SLE::Ref node,
479 std::uint64_t page,
480 bool preserveOrder,
481 STVector256& indexes,
482 UInt256 const& key);
483
486 ApplyView& view,
487 std::uint64_t page,
488 SLE::pointer node,
489 std::uint64_t nextPage,
490 SLE::Ref next,
491 UInt256 const& key,
492 Keylet const& directory,
493 std::function<void(SLE::Ref)> const& describe);
494
495} // namespace directory
496} // namespace xrpl
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:141
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.
virtual void creditHookIOU(AccountID const &from, AccountID const &to, STAmount const &amount, STAmount const &preCreditBalance)
Definition ApplyView.h:248
ApplyView()=default
bool dirRemove(Keylet const &directory, std::uint64_t page, Keylet const &key, bool keepRoot)
Definition ApplyView.h:412
virtual void creditHookMPT(AccountID const &from, AccountID const &to, STAmount const &amount, std::uint64_t preCreditBalanceHolder, std::int64_t preCreditBalanceIssuer)
Definition ApplyView.h:258
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:302
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:373
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:333
virtual void adjustOwnerCountHook(AccountID const &account, OwnerCounts const &cur, OwnerCounts const &next)
Definition ApplyView.h:309
bool dirRemove(Keylet const &directory, std::uint64_t page, UInt256 const &key, bool keepRoot)
Remove an entry from a directory.
virtual void erase(SLE::Ref sle)=0
Remove a peeked SLE.
virtual void update(SLE::Ref sle)=0
Indicate changes to a peeked SLE.
std::optional< std::uint64_t > dirInsert(Keylet const &directory, Keylet const &key, std::function< void(SLE::Ref)> const &describe)
Definition ApplyView.h:382
A currency issued by an account.
Definition Issue.h:18
constexpr bool holds() const noexcept
Definition STAmount.h:478
std::shared_ptr< STLedgerEntry > pointer
std::shared_ptr< STLedgerEntry > const & Ref
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)
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::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:635
constexpr ApplyFlags operator~(ApplyFlags const &flags)
Definition ApplyView.h:79
constexpr HashRouterFlags & operator|=(HashRouterFlags &lhs, HashRouterFlags rhs)
Definition HashRouter.h:52
constexpr BaseUInt< Bits, Tag > operator|(BaseUInt< Bits, Tag > const &a, BaseUInt< Bits, Tag > const &b)
Definition base_uint.h:642
constexpr HashRouterFlags & operator&=(HashRouterFlags &lhs, HashRouterFlags rhs)
Definition HashRouter.h:67
constexpr Dest safeCast(Src s) noexcept
Definition safe_cast.h:21
BaseUInt< 256 > UInt256
Definition base_uint.h:580
ApplyFlags
Definition ApplyView.h:27
@ TapDryRun
Definition ApplyView.h:46
@ TapProposal
Definition ApplyView.h:53
@ 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
Bundles the mutable ledger view and the transaction being applied.
Definition ApplyView.h:451
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
UInt256 key
Definition Keylet.h:21
LedgerEntryType type
Definition Keylet.h:22