rippled
Loading...
Searching...
No Matches
directory.cpp
1#include <test/jtx/directory.h>
2
3#include <xrpl/ledger/Sandbox.h>
4
5namespace ripple::test::jtx {
6
8namespace directory {
9
10auto
12 Env& env,
13 std::uint64_t newLastPage,
14 Keylet directory,
17{
19 env.app().openLedger().modify(
20 [&](OpenView& view, beast::Journal j) -> bool {
21 Sandbox sb(&view, tapNONE);
22
23 // Find the root page
24 auto sleRoot = sb.peek(directory);
25 if (!sleRoot)
26 {
28 return false;
29 }
30
31 // Find last page
32 auto const lastIndex = sleRoot->getFieldU64(sfIndexPrevious);
33 if (lastIndex == 0)
34 {
36 return false;
37 }
38
39 if (sb.exists(keylet::page(directory, newLastPage)))
40 {
42 return false;
43 }
44
45 if (lastIndex >= newLastPage)
46 {
48 return false;
49 }
50
51 auto slePage = sb.peek(keylet::page(directory, lastIndex));
52 if (!slePage)
53 {
55 return false;
56 }
57
58 // Copy its data and delete the page
59 auto indexes = slePage->getFieldV256(sfIndexes);
60 auto prevIndex = slePage->at(~sfIndexPrevious);
61 auto owner = slePage->at(~sfOwner);
62 sb.erase(slePage);
63
64 // Create new page to replace slePage
65 auto sleNew =
66 std::make_shared<SLE>(keylet::page(directory, newLastPage));
67 sleNew->setFieldH256(sfRootIndex, directory.key);
68 sleNew->setFieldV256(sfIndexes, indexes);
69 if (owner)
70 sleNew->setAccountID(sfOwner, *owner);
71 if (prevIndex)
72 sleNew->setFieldU64(sfIndexPrevious, *prevIndex);
73 sb.insert(sleNew);
74
75 // Adjust root previous and previous node's next
76 sleRoot->setFieldU64(sfIndexPrevious, newLastPage);
77 if (prevIndex.value_or(0) == 0)
78 sleRoot->setFieldU64(sfIndexNext, newLastPage);
79 else
80 {
81 auto slePrev = sb.peek(keylet::page(directory, *prevIndex));
82 if (!slePrev)
83 {
85 return false;
86 }
87 slePrev->setFieldU64(sfIndexNext, newLastPage);
88 sb.update(slePrev);
89 }
90 sb.update(sleRoot);
91
92 // Fixup page numbers in the objects referred by indexes
93 if (adjust)
94 for (auto const key : indexes)
95 {
96 if (!adjust(sb, key, newLastPage))
97 {
99 return false;
100 }
101 }
102
103 sb.apply(view);
104 return true;
105 });
106
107 return res;
108}
109
110bool
112{
113 auto sle = view.peek({ltANY, key});
114 if (sle && sle->isFieldPresent(sfOwnerNode))
115 {
116 sle->setFieldU64(sfOwnerNode, page);
117 view.update(sle);
118 return true;
119 }
120
121 return false;
122}
123
124} // namespace directory
125
126} // namespace ripple::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:124
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 erase(std::shared_ptr< SLE > const &sle) override
Remove a peeked SLE.
void update(std::shared_ptr< SLE > const &sle) override
Indicate changes to a peeked SLE.
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.
std::shared_ptr< SLE > peek(Keylet const &k) override
Prepare to modify the SLE associated with key.
A transaction testing environment.
Definition Env.h:102
Application & app()
Definition Env.h:242
T is_same_v
Keylet page(uint256 const &root, std::uint64_t index=0) noexcept
A page in a directory.
Definition Indexes.cpp:361
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.
@ ltANY
A special type, matching any ledger entry type.
@ tapNONE
Definition ApplyView.h:12
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
uint256 key
Definition Keylet.h:21