rippled
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
11 tapNONE = 0x00,
12
13 // This is a local transaction with the
14 // fail_hard flag set.
16
17 // This is not the transaction's last pass
18 // Transaction can be retried, soft failures allowed
19 tapRETRY = 0x20,
20
21 // Transaction came from a privileged source
22 tapUNLIMITED = 0x400,
23
24 // Transaction is executing as part of a batch
25 tapBATCH = 0x800,
26
27 // Transaction shouldn't be applied
28 // Signatures shouldn't be checked
29 tapDRY_RUN = 0x1000
30};
31
32constexpr ApplyFlags
33operator|(ApplyFlags const& lhs, ApplyFlags const& rhs)
34{
35 return safe_cast<ApplyFlags>(
37}
38
39static_assert((tapFAIL_HARD | tapRETRY) == safe_cast<ApplyFlags>(0x30u), "ApplyFlags operator |");
40static_assert((tapRETRY | tapFAIL_HARD) == safe_cast<ApplyFlags>(0x30u), "ApplyFlags operator |");
41
42constexpr ApplyFlags
43operator&(ApplyFlags const& lhs, ApplyFlags const& rhs)
44{
45 return safe_cast<ApplyFlags>(
47}
48
49static_assert((tapFAIL_HARD & tapRETRY) == tapNONE, "ApplyFlags operator &");
50static_assert((tapRETRY & tapFAIL_HARD) == tapNONE, "ApplyFlags operator &");
51
52constexpr ApplyFlags
53operator~(ApplyFlags const& flags)
54{
55 return safe_cast<ApplyFlags>(~safe_cast<std::underlying_type_t<ApplyFlags>>(flags));
56}
57
58static_assert(~tapRETRY == safe_cast<ApplyFlags>(0xFFFFFFDFu), "ApplyFlags operator ~");
59
60inline ApplyFlags
62{
63 lhs = lhs | rhs;
64 return lhs;
65}
66
67inline ApplyFlags
69{
70 lhs = lhs & rhs;
71 return lhs;
72}
73
74//------------------------------------------------------------------------------
75
113class ApplyView : public ReadView
114{
115private:
118 dirAdd(
119 bool preserveOrder,
120 Keylet const& directory,
121 uint256 const& key,
122 std::function<void(std::shared_ptr<SLE> const&)> const& describe);
123
124public:
125 ApplyView() = default;
126
135 virtual ApplyFlags
136 flags() const = 0;
137
153 peek(Keylet const& k) = 0;
154
166 virtual void
168
187 virtual void
189
206 virtual void
208
209 //--------------------------------------------------------------------------
210
211 // Called when a credit is made to an account
212 // This is required to support PaymentSandbox
213 virtual void
214 creditHook(AccountID const& from, AccountID const& to, STAmount const& amount, STAmount const& preCreditBalance)
215 {
216 }
217
218 // Called when the owner count changes
219 // This is required to support PaymentSandbox
220 virtual void
222 {
223 }
224
245 Keylet const& directory,
246 Keylet const& key,
247 std::function<void(std::shared_ptr<SLE> const&)> const& describe)
248 {
249 if (key.type != ltOFFER)
250 {
251 // LCOV_EXCL_START
252 UNREACHABLE(
253 "xrpl::ApplyView::dirAppend : only Offers are appended to "
254 "book directories");
255 // Only Offers are appended to book directories. Call dirInsert()
256 // instead
257 return std::nullopt;
258 // LCOV_EXCL_STOP
259 }
260 return dirAdd(true, directory, key.key, describe);
261 }
284 Keylet const& directory,
285 uint256 const& key,
286 std::function<void(std::shared_ptr<SLE> const&)> const& describe)
287 {
288 return dirAdd(false, directory, key, describe);
289 }
290
293 Keylet const& directory,
294 Keylet const& key,
295 std::function<void(std::shared_ptr<SLE> const&)> const& describe)
296 {
297 return dirAdd(false, directory, key.key, describe);
298 }
317 bool
318 dirRemove(Keylet const& directory, std::uint64_t page, uint256 const& key, bool keepRoot);
319
320 bool
321 dirRemove(Keylet const& directory, std::uint64_t page, Keylet const& key, bool keepRoot)
322 {
323 return dirRemove(directory, page, key.key, keepRoot);
324 }
328 bool
329 dirDelete(Keylet const& directory, std::function<void(uint256 const&)> const&);
330
340 bool
341 emptyDirDelete(Keylet const& directory);
342};
343
344namespace directory {
354 ApplyView& view,
355 Keylet const& directory,
356 uint256 const& key,
357 std::function<void(std::shared_ptr<SLE> const&)> const& describe);
358
359auto
360findPreviousPage(ApplyView& view, Keylet const& directory, SLE::ref start);
361
364 ApplyView& view,
365 SLE::ref node,
366 std::uint64_t page,
367 bool preserveOrder,
368 STVector256& indexes,
369 uint256 const& key);
370
373 ApplyView& view,
374 std::uint64_t page,
375 SLE::pointer node,
376 std::uint64_t nextPage,
377 SLE::ref next,
378 uint256 const& key,
379 Keylet const& directory,
380 std::function<void(std::shared_ptr<SLE> const&)> const& describe);
381
382} // namespace directory
383} // namespace xrpl
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:114
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.
ApplyView()=default
virtual void erase(std::shared_ptr< SLE > const &sle)=0
Remove a peeked SLE.
bool dirRemove(Keylet const &directory, std::uint64_t page, Keylet const &key, bool keepRoot)
Definition ApplyView.h:321
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.
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:292
virtual void insert(std::shared_ptr< SLE > const &sle)=0
Insert a new state SLE.
bool emptyDirDelete(Keylet const &directory)
Remove the specified directory, if it is empty.
virtual void creditHook(AccountID const &from, AccountID const &to, STAmount const &amount, STAmount const &preCreditBalance)
Definition ApplyView.h:214
virtual void adjustOwnerCountHook(AccountID const &account, std::uint32_t cur, std::uint32_t next)
Definition ApplyView.h:221
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.
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:283
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
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:244
A view into a ledger.
Definition ReadView.h:31
T is_same_v
auto findPreviousPage(ApplyView &view, Keylet const &directory, SLE::ref start)
Definition ApplyView.cpp:33
std::uint64_t insertKey(ApplyView &view, SLE::ref node, std::uint64_t page, bool preserveOrder, STVector256 &indexes, uint256 const &key)
Definition ApplyView.cpp:54
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(std::shared_ptr< SLE > const &)> const &describe)
Definition ApplyView.cpp:90
std::uint64_t createRoot(ApplyView &view, Keylet const &directory, uint256 const &key, std::function< void(std::shared_ptr< SLE > const &)> const &describe)
Helper functions for managing low-level directory operations.
Definition ApplyView.cpp:14
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr ApplyFlags operator~(ApplyFlags const &flags)
Definition ApplyView.h:53
constexpr HashRouterFlags & operator|=(HashRouterFlags &lhs, HashRouterFlags rhs)
Definition HashRouter.h:41
constexpr base_uint< Bits, Tag > operator|(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition base_uint.h:582
constexpr HashRouterFlags & operator&=(HashRouterFlags &lhs, HashRouterFlags rhs)
Definition HashRouter.h:56
constexpr base_uint< Bits, Tag > operator&(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition base_uint.h:575
ApplyFlags
Definition ApplyView.h:10
@ tapDRY_RUN
Definition ApplyView.h:29
@ tapNONE
Definition ApplyView.h:11
@ tapRETRY
Definition ApplyView.h:19
@ tapFAIL_HARD
Definition ApplyView.h:15
@ tapUNLIMITED
Definition ApplyView.h:22
@ tapBATCH
Definition ApplyView.h:25
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:19
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:19
uint256 key
Definition Keylet.h:20
LedgerEntryType type
Definition Keylet.h:21