rippled
Loading...
Searching...
No Matches
directory.cpp
1#include <test/jtx/directory.h>
2
3#include <xrpl/ledger/Sandbox.h>
4
5namespace xrpl::test::jtx {
6
8namespace directory {
9
10auto
12 Env& env,
13 std::uint64_t newLastPage,
14 Keylet directory,
16{
18 env.app().openLedger().modify([&](OpenView& view, beast::Journal j) -> bool {
19 Sandbox sb(&view, tapNONE);
20
21 // Find the root page
22 auto sleRoot = sb.peek(directory);
23 if (!sleRoot)
24 {
26 return false;
27 }
28
29 // Find last page
30 auto const lastIndex = sleRoot->getFieldU64(sfIndexPrevious);
31 if (lastIndex == 0)
32 {
34 return false;
35 }
36
37 if (sb.exists(keylet::page(directory, newLastPage)))
38 {
40 return false;
41 }
42
43 if (lastIndex >= newLastPage)
44 {
46 return false;
47 }
48
49 auto slePage = sb.peek(keylet::page(directory, lastIndex));
50 if (!slePage)
51 {
53 return false;
54 }
55
56 // Copy its data and delete the page
57 auto indexes = slePage->getFieldV256(sfIndexes);
58 auto prevIndex = slePage->at(~sfIndexPrevious);
59 auto owner = slePage->at(~sfOwner);
60 sb.erase(slePage);
61
62 // Create new page to replace slePage
63 auto sleNew = std::make_shared<SLE>(keylet::page(directory, newLastPage));
64 sleNew->setFieldH256(sfRootIndex, directory.key);
65 sleNew->setFieldV256(sfIndexes, indexes);
66 if (owner)
67 sleNew->setAccountID(sfOwner, *owner);
68 if (prevIndex)
69 sleNew->setFieldU64(sfIndexPrevious, *prevIndex);
70 sb.insert(sleNew);
71
72 // Adjust root previous and previous node's next
73 sleRoot->setFieldU64(sfIndexPrevious, newLastPage);
74 if (prevIndex.value_or(0) == 0)
75 sleRoot->setFieldU64(sfIndexNext, newLastPage);
76 else
77 {
78 auto slePrev = sb.peek(keylet::page(directory, *prevIndex));
79 if (!slePrev)
80 {
82 return false;
83 }
84 slePrev->setFieldU64(sfIndexNext, newLastPage);
85 sb.update(slePrev);
86 }
87 sb.update(sleRoot);
88
89 // Fixup page numbers in the objects referred by indexes
90 if (adjust)
91 for (auto const key : indexes)
92 {
93 if (!adjust(sb, key, newLastPage))
94 {
96 return false;
97 }
98 }
99
100 sb.apply(view);
101 return true;
102 });
103
104 return res;
105}
106
107bool
109{
110 auto sle = view.peek({ltANY, key});
111 if (sle && sle->isFieldPresent(sfOwnerNode))
112 {
113 sle->setFieldU64(sfOwnerNode, page);
114 view.update(sle);
115 return true;
116 }
117
118 return false;
119}
120
121} // namespace directory
122
123} // namespace xrpl::test::jtx
A generic endpoint for log messages.
Definition Journal.h:41
virtual OpenLedger & openLedger()=0
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:115
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
bool modify(modify_type const &f)
Modify the open ledger.
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:46
Discardable, editable view to a ledger.
Definition Sandbox.h:16
void apply(RawView &to)
Definition Sandbox.h:36
void update(std::shared_ptr< SLE > const &sle) override
Indicate changes to a peeked SLE.
void erase(std::shared_ptr< SLE > const &sle) override
Remove a peeked SLE.
std::shared_ptr< SLE > peek(Keylet const &k) override
Prepare to modify the SLE associated with key.
void insert(std::shared_ptr< SLE > const &sle) override
Insert a new state SLE.
bool exists(Keylet const &k) const override
Determine if a state item exists.
A transaction testing environment.
Definition Env.h:98
Application & app()
Definition Env.h:230
T is_same_v
Keylet page(uint256 const &root, std::uint64_t index=0) noexcept
A page in a directory.
Definition Indexes.cpp:331
auto bumpLastPage(Env &env, std::uint64_t newLastPage, Keylet directory, std::function< bool(ApplyView &, uint256, std::uint64_t)> adjust) -> Expected< void, Error >
Move the position of the last page in the user's directory on open ledger to newLastPage.
Definition directory.cpp:11
bool adjustOwnerNode(ApplyView &view, uint256 key, std::uint64_t page)
Implementation of adjust for the most common ledger entry, i.e.
@ tapNONE
Definition ApplyView.h:12
@ ltANY
A special type, matching any ledger entry type.
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
uint256 key
Definition Keylet.h:21