xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
test
jtx
impl
directory.cpp
1
#include <test/jtx/directory.h>
2
3
#include <test/jtx/Env.h>
4
5
#include <xrpl/basics/base_uint.h>
6
#include <xrpl/beast/utility/Journal.h>
7
#include <xrpl/core/ServiceRegistry.h>
8
#include <xrpl/ledger/ApplyView.h>
9
#include <xrpl/ledger/OpenView.h>
10
#include <xrpl/ledger/Sandbox.h>
11
#include <xrpl/protocol/Indexes.h>
12
#include <xrpl/protocol/Keylet.h>
13
#include <xrpl/protocol/LedgerFormats.h>
14
#include <xrpl/protocol/SField.h>
15
16
#include <
cstdint
>
17
#include <expected>
18
#include <
functional
>
19
#include <
memory
>
20
24
namespace
xrpl::test::jtx::directory
{
25
26
auto
27
bumpLastPage
(
28
Env
& env,
29
std::uint64_t
newLastPage,
30
Keylet
directory
,
31
std::function
<
bool
(
ApplyView
&,
UInt256
,
std::uint64_t
)> adjust) -> std::expected<void, Error>
32
{
33
std::expected<void, Error> res{};
34
env.
app
().
getOpenLedger
().
modify
([&](
OpenView
& view,
beast::Journal
j) ->
bool
{
35
Sandbox
sb(&view,
TapNone
);
36
37
// Find the root page
38
auto
sleRoot = sb.
peek
(
directory
);
39
if
(!sleRoot)
40
{
41
res =
std::unexpected<Error>
(
Error::DirectoryRootNotFound
);
42
return
false
;
43
}
44
45
// Find last page
46
auto
const
lastIndex = sleRoot->getFieldU64(sfIndexPrevious);
47
if
(lastIndex == 0)
48
{
49
res =
std::unexpected<Error>
(
Error::DirectoryTooSmall
);
50
return
false
;
51
}
52
53
if
(sb.
exists
(
keylet::page
(
directory
, newLastPage)))
54
{
55
res =
std::unexpected<Error>
(
Error::DirectoryPageDuplicate
);
56
return
false
;
57
}
58
59
if
(lastIndex >= newLastPage)
60
{
61
res =
std::unexpected<Error>
(
Error::InvalidLastPage
);
62
return
false
;
63
}
64
65
auto
slePage = sb.
peek
(
keylet::page
(
directory
, lastIndex));
66
if
(!slePage)
67
{
68
res =
std::unexpected<Error>
(
Error::DirectoryPageNotFound
);
69
return
false
;
70
}
71
72
// Copy its data and delete the page
73
auto
indexes = slePage->getFieldV256(sfIndexes);
74
auto
prevIndex = slePage->at(~sfIndexPrevious);
75
auto
owner = slePage->at(~sfOwner);
76
sb.
erase
(slePage);
77
78
// Create new page to replace slePage
79
auto
sleNew =
std::make_shared<SLE>
(
keylet::page
(
directory
, newLastPage));
80
sleNew->setFieldH256(sfRootIndex,
directory
.key);
81
sleNew->setFieldV256(sfIndexes, indexes);
82
if
(owner)
83
sleNew->setAccountID(sfOwner, *owner);
84
if
(prevIndex)
85
sleNew->setFieldU64(sfIndexPrevious, *prevIndex);
86
sb.
insert
(sleNew);
87
88
// Adjust root previous and previous node's next
89
sleRoot->setFieldU64(sfIndexPrevious, newLastPage);
90
if
(prevIndex.valueOr(0) == 0)
91
{
92
sleRoot->setFieldU64(sfIndexNext, newLastPage);
93
}
94
else
95
{
96
auto
slePrev = sb.
peek
(
keylet::page
(
directory
, *prevIndex));
97
if
(!slePrev)
98
{
99
res =
std::unexpected<Error>
(
Error::DirectoryPageNotFound
);
100
return
false
;
101
}
102
slePrev->setFieldU64(sfIndexNext, newLastPage);
103
sb.
update
(slePrev);
104
}
105
sb.
update
(sleRoot);
106
107
// Fixup page numbers in the objects referred by indexes
108
if
(adjust)
109
{
110
for
(
auto
const
key : indexes)
111
{
112
if
(!adjust(sb, key, newLastPage))
113
{
114
res =
std::unexpected<Error>
(
Error::AdjustmentError
);
115
return
false
;
116
}
117
}
118
}
119
120
sb.
apply
(view);
121
return
true
;
122
});
123
124
return
res;
125
}
126
127
bool
128
adjustOwnerNode
(
ApplyView
& view,
UInt256
key,
std::uint64_t
page)
129
{
130
auto
sle = view.
peek
({
ltANY
, key});
131
if
(sle && sle->isFieldPresent(sfOwnerNode))
132
{
133
sle->setFieldU64(sfOwnerNode, page);
134
view.
update
(sle);
135
return
true
;
136
}
137
138
return
false
;
139
}
140
141
}
// namespace xrpl::test::jtx::directory
beast::Journal
A generic endpoint for log messages.
Definition
Journal.h:44
xrpl::ApplyView
Writeable view to a ledger, for applying a transaction.
Definition
ApplyView.h:141
xrpl::ApplyView::peek
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
xrpl::ApplyView::update
virtual void update(SLE::Ref sle)=0
Indicate changes to a peeked SLE.
xrpl::OpenLedger::modify
bool modify(ModifyType const &f)
Modify the open ledger.
Definition
OpenLedger.cpp:64
xrpl::OpenView
Writable ledger view that accumulates state and tx changes.
Definition
OpenView.h:59
xrpl::Sandbox
Discardable, editable view to a ledger.
Definition
Sandbox.h:18
xrpl::Sandbox::apply
void apply(RawView &to)
Definition
Sandbox.h:38
xrpl::ServiceRegistry::getOpenLedger
virtual OpenLedger & getOpenLedger()=0
xrpl::detail::ApplyViewBase::insert
void insert(SLE::Ref sle) override
Insert a new state SLE.
Definition
ApplyViewBase.cpp:130
xrpl::detail::ApplyViewBase::peek
SLE::pointer peek(Keylet const &k) override
Prepare to modify the SLE associated with key.
Definition
ApplyViewBase.cpp:118
xrpl::detail::ApplyViewBase::erase
void erase(SLE::Ref sle) override
Remove a peeked SLE.
Definition
ApplyViewBase.cpp:124
xrpl::detail::ApplyViewBase::update
void update(SLE::Ref sle) override
Indicate changes to a peeked SLE.
Definition
ApplyViewBase.cpp:136
xrpl::detail::ApplyViewBase::exists
bool exists(Keylet const &k) const override
Determine if a state item exists.
Definition
ApplyViewBase.cpp:49
xrpl::test::jtx::Env
A transaction testing environment.
Definition
Env.h:161
xrpl::test::jtx::Env::app
Application & app()
Definition
Env.h:300
cstdint
std::function
functional
std::uint64_t
std::make_shared
T make_shared(T... args)
memory
xrpl::keylet::page
Keylet page(UInt256 const &root, std::uint64_t const index=0) noexcept
A page in a directory.
Definition
Indexes.cpp:409
xrpl::test::jtx::directory
Directory operations.
Definition
directory.h:19
xrpl::test::jtx::directory::Error::InvalidLastPage
@ InvalidLastPage
Definition
directory.h:26
xrpl::test::jtx::directory::Error::DirectoryTooSmall
@ DirectoryTooSmall
Definition
directory.h:23
xrpl::test::jtx::directory::Error::DirectoryPageNotFound
@ DirectoryPageNotFound
Definition
directory.h:25
xrpl::test::jtx::directory::Error::DirectoryPageDuplicate
@ DirectoryPageDuplicate
Definition
directory.h:24
xrpl::test::jtx::directory::Error::DirectoryRootNotFound
@ DirectoryRootNotFound
Definition
directory.h:22
xrpl::test::jtx::directory::Error::AdjustmentError
@ AdjustmentError
Definition
directory.h:27
xrpl::test::jtx::directory::bumpLastPage
auto bumpLastPage(Env &env, std::uint64_t newLastPage, Keylet directory, std::function< bool(ApplyView &, UInt256, std::uint64_t)> adjust) -> std::expected< void, Error >
Move the position of the last page in the user's directory on open ledger to newLastPage.
Definition
directory.cpp:27
xrpl::test::jtx::directory::adjustOwnerNode
bool adjustOwnerNode(ApplyView &view, UInt256 key, std::uint64_t page)
Implementation of adjust for the most common ledger entry, i.e.
Definition
directory.cpp:128
xrpl::UInt256
BaseUInt< 256 > UInt256
Definition
base_uint.h:580
xrpl::TapNone
@ TapNone
Definition
ApplyView.h:28
xrpl::ltANY
@ ltANY
A special type, matching any ledger entry type.
Definition
LedgerFormats.h:63
xrpl::Keylet
A pair of SHAMap key and LedgerEntryType.
Definition
Keylet.h:20
std::unexpected
T unexpected(T... args)
Generated by
1.17.0