xrpld
Loading...
Searching...
No Matches
SLEBase.cpp
1#include <xrpl/ledger/entries/SLEBase.h>
2
3#include <xrpl/beast/utility/Journal.h>
4#include <xrpl/ledger/ApplyView.h>
5#include <xrpl/ledger/ApplyViewImpl.h>
6#include <xrpl/ledger/OpenView.h>
7#include <xrpl/ledger/ReadView.h>
8#include <xrpl/ledger/entries/AMMEntry.h> // IWYU pragma: keep
9#include <xrpl/ledger/entries/AccountRootEntry.h>
10#include <xrpl/ledger/entries/AmendmentsEntry.h> // IWYU pragma: keep
11#include <xrpl/ledger/entries/CheckEntry.h> // IWYU pragma: keep
12#include <xrpl/ledger/entries/CredentialEntry.h> // IWYU pragma: keep
13#include <xrpl/ledger/entries/DIDEntry.h> // IWYU pragma: keep
14#include <xrpl/ledger/entries/DelegateEntry.h> // IWYU pragma: keep
15#include <xrpl/ledger/entries/DepositPreauthEntry.h> // IWYU pragma: keep
16#include <xrpl/ledger/entries/DirectoryNodeEntry.h> // IWYU pragma: keep
17#include <xrpl/ledger/entries/EscrowEntry.h> // IWYU pragma: keep
18#include <xrpl/ledger/entries/FeeSettingsEntry.h> // IWYU pragma: keep
19#include <xrpl/ledger/entries/LedgerHashesEntry.h> // IWYU pragma: keep
20#include <xrpl/ledger/entries/LoanBrokerEntry.h> // IWYU pragma: keep
21#include <xrpl/ledger/entries/LoanEntry.h> // IWYU pragma: keep
22#include <xrpl/ledger/entries/MPTokenEntry.h> // IWYU pragma: keep
23#include <xrpl/ledger/entries/MPTokenIssuanceEntry.h> // IWYU pragma: keep
24#include <xrpl/ledger/entries/NFTokenOfferEntry.h> // IWYU pragma: keep
25#include <xrpl/ledger/entries/NFTokenPageEntry.h> // IWYU pragma: keep
26#include <xrpl/ledger/entries/NegativeUNLEntry.h> // IWYU pragma: keep
27#include <xrpl/ledger/entries/OfferEntry.h>
28#include <xrpl/ledger/entries/OracleEntry.h> // IWYU pragma: keep
29#include <xrpl/ledger/entries/PayChannelEntry.h> // IWYU pragma: keep
30#include <xrpl/ledger/entries/PermissionedDomainEntry.h> // IWYU pragma: keep
31#include <xrpl/ledger/entries/RippleStateEntry.h> // IWYU pragma: keep
32#include <xrpl/ledger/entries/SignerListEntry.h> // IWYU pragma: keep
33#include <xrpl/ledger/entries/SponsorshipEntry.h> // IWYU pragma: keep
34#include <xrpl/ledger/entries/TicketEntry.h>
35#include <xrpl/ledger/entries/TransactionProposalEntry.h> // IWYU pragma: keep
36#include <xrpl/ledger/entries/VaultEntry.h> // IWYU pragma: keep
37#include <xrpl/protocol/Indexes.h>
38#include <xrpl/protocol/Keylet.h>
39#include <xrpl/protocol/LedgerFormats.h>
40#include <xrpl/protocol/SField.h>
41#include <xrpl/protocol/STLedgerEntry.h>
42#include <xrpl/protocol/SeqProxy.h>
43#include <xrpl/protocol/XRPAmount.h>
44#include <xrpl/protocol_autogen/transactions/AccountSet.h>
45
46#include <gtest/gtest.h>
47#include <helpers/Account.h>
48#include <helpers/TxTest.h>
49
50#include <stdexcept>
51#include <tuple>
52#include <type_traits>
53
54namespace xrpl {
55
56// An un-instantiated class template is barely type-checked. Name every entry
57// class in a static_assert below so its class definition is instantiated and
58// type-checked; the per-type suites check the member bodies. Keep this block
59// even once real call sites exist: it is what catches a new ledger entry type
60// being added without its wrapper class.
61//
62// Driving this off ledger_entries.macro keeps it exhaustive by construction:
63// adding a ledger entry type without adding its entry class stops compiling
64// here, and the static_assert pins each one to the right LedgerEntryType.
65//
66// Keep this loop in one file rather than splitting it across the per-entry
67// *Entry_test.cpp suites. Those are hand-written, so a new ledger entry type
68// would simply have no file there and nothing would complain; this is the only
69// thing making the coverage exhaustive rather than merely extensive.
70
71template class SLEBase<ReadView>;
72template class SLEBase<ApplyView>;
73
74#pragma push_macro("LEDGER_ENTRY")
75#undef LEDGER_ENTRY
76
77// No explicit instantiation here, neither a definition nor a declaration.
78// A definition would instantiate an entry a second time once its member
79// bodies live in its own .cpp (MSVC rejects that with C4661 under /WX), and a
80// declaration (`extern template`) stops clang from emitting the inline
81// constructors of the header-only entries at -O0, which leaves them undefined
82// at link time in the debug builds. Naming the class in a static_assert is
83// enough to instantiate its definition, which is all this exhaustiveness
84// check needs; the per-type suites exercise the members.
85#define LEDGER_ENTRY(tag, value, name, ...) \
86 static_assert( \
87 name##Entry<ReadView>::kEntryType == tag && name##Entry<ApplyView>::kEntryType == tag, \
88 #name "Entry must be bound to " #tag); \
89 static_assert( \
90 std::is_constructible_v<name##Entry<ReadView>, Keylet const&, ReadView const&> && \
91 std::is_constructible_v<name##Entry<ApplyView>, Keylet const&, ApplyView&>, \
92 #name "Entry must be constructible from a Keylet and a view");
93
94#include <xrpl/protocol/detail/ledger_entries.macro>
95
96#undef LEDGER_ENTRY
97#pragma pop_macro("LEDGER_ENTRY")
98
99// --- Entry-type safety, checked at compile time. ---
100//
101// The writable -> read-only converting constructor is inherited into every
102// per-type entry, so without the entry-type constraint it will bind any
103// writable entry that slices to SLEBase. These assertions pin down which
104// conversions are legal.
105
106// An entry class for one entry type must never be constructible from another.
107static_assert(
109 "cross-entry-type conversion must not compile");
110static_assert(
112 "cross-entry-type construction must not compile, even explicitly");
113static_assert(
115 "read-only cross-entry-type conversion must not compile");
116
117// Nor from a type-erased writable entry, which carries no static type.
118static_assert(
120 "generic -> typed conversion must not compile");
121
122// The intended conversions must keep working: same type writable -> read-only,
123// and typed -> generic widening.
124static_assert(
126 "same-type writable -> read-only conversion must keep working");
127static_assert(
129 "typed -> generic widening must keep working");
130
131// Detection idioms for the writable interface. These have to go through a
132// template parameter: a requires-expression over a concrete type is checked
133// eagerly, so spelling the calls out inline would be a hard error rather than
134// the `false` the assertions below want.
135template <typename T>
136concept HasMutableRawSle = requires(T& t) { t.mutableRawSle(); };
137
138template <typename T>
139concept HasApplyView = requires(T& t) { t.applyView(); };
140
141namespace test {
142
148class SLEBaseTests : public ::testing::Test
149{
150protected:
152 Account const alice_{"alice"};
153 Account const bob_{"bob"};
154
156 {
157 env_.createAccount(alice_, XRP(10'000));
158 }
159};
160
162{
163 AccountRootEntryR const absent(bob_.id(), env_.getClosedLedger());
164 EXPECT_FALSE(absent.exists());
165 EXPECT_FALSE(static_cast<bool>(absent));
166 // A typed entry knows its entry type even with nothing to read.
167 EXPECT_EQ(absent.type(), ltACCOUNT_ROOT);
168
169 AccountRootEntryR const present(alice_.id(), env_.getClosedLedger());
170 EXPECT_TRUE(present.exists());
171 EXPECT_TRUE(static_cast<bool>(present));
172 EXPECT_EQ(present.key(), keylet::account(alice_.id()).key);
173 EXPECT_EQ(present.type(), ltACCOUNT_ROOT);
174 EXPECT_EQ(present.keylet().type, ltACCOUNT_ROOT);
175 EXPECT_EQ(present->getType(), ltACCOUNT_ROOT);
176 EXPECT_EQ((*present).getType(), ltACCOUNT_ROOT);
177 EXPECT_EQ(&present.readView(), &env_.getClosedLedger());
178}
179
181{
182 auto const sle = env_.getClosedLedger().read(keylet::account(alice_.id()));
183 ASSERT_NE(sle, nullptr);
184
185 AccountRootEntryR const adopted(sle, env_.getClosedLedger());
186 EXPECT_TRUE(adopted.exists());
187 EXPECT_EQ(adopted.rawSle(), sle);
188 EXPECT_EQ(adopted.key(), keylet::account(alice_.id()).key);
189 EXPECT_EQ(adopted.type(), ltACCOUNT_ROOT);
190 // keylet() reports the SLE's own type, not the entry's static binding, so
191 // it stays truthful in a Release build where the constructor's
192 // entry-type assert is compiled out.
193 EXPECT_EQ(adopted.keylet().type, ltACCOUNT_ROOT);
194
195 // Adopting a null SLE is allowed: the assert only fires on a
196 // type mismatch, and a null pointer has no type to mismatch.
197 AccountRootEntryR const empty(SLE::const_pointer{}, env_.getClosedLedger());
198 EXPECT_FALSE(empty.exists());
199 EXPECT_EQ(empty.type(), ltACCOUNT_ROOT);
200
201 // A generic entry adopting the same SLE has to read the type back.
202 ReadOnlySLE const generic(sle, env_.getClosedLedger());
203 EXPECT_TRUE(generic.exists());
204 EXPECT_EQ(generic.type(), ltACCOUNT_ROOT);
205 EXPECT_EQ(generic.keylet().type, ltACCOUNT_ROOT);
206
207 // There is deliberately no writable equivalent.
208 static_assert(
210 "writable entries must not be constructible from a bare SLE");
211}
212
213TEST_F(SLEBaseTests, writable_accessors)
214{
215 ApplyViewImpl av(&env_.getClosedLedger(), TapNone);
217
218 AccountRootEntryW account(alice_.id(), av, j);
219 EXPECT_TRUE(account.exists());
220 EXPECT_EQ(account.mutableRawSle(), account.rawSle());
221 EXPECT_EQ(&account.applyView(), &av);
222 EXPECT_EQ(&account.readView(), static_cast<ReadView const*>(&av));
223 EXPECT_EQ(&account.journal().sink(), &j.sink());
224
225 // The mutable dereference operators reach the same entry.
226 EXPECT_EQ(account.operator->(), account.rawSle().get());
227 EXPECT_EQ(&*account, account.rawSle().get());
228
229 // Everything handing out mutable access is non-const, so a const
230 // writable entry is as inert as a read-only one.
232 static_assert(HasApplyView<AccountRootEntryW>);
233 static_assert(
235 "mutableRawSle() must not be callable on a const writable entry");
236 static_assert(
238 "applyView() must not be callable on a const writable entry");
239
240 // Read-only entries do not have the writable interface at all.
241 static_assert(
243 "mutableRawSle() must not exist on a read-only entry");
244 static_assert(
245 !HasApplyView<AccountRootEntryR>, "applyView() must not exist on a read-only entry");
246}
247
248TEST_F(SLEBaseTests, apply_view_context_ctor)
249{
250 ApplyViewImpl av(&env_.getClosedLedger(), TapNone);
252
253 transactions::AccountSetBuilder builder{alice_.id()};
254 builder.setSequence(env_.getAccountRoot(alice_.id()).getSequence());
255 builder.setFee(XRPAmount(10));
256 auto const tx = builder.build(alice_.pk(), alice_.sk()).getSTTx();
257 ASSERT_NE(tx, nullptr);
258 ApplyViewContext const ctx{.view = av, .tx = *tx};
259
260 // Delegates to the (Keylet, ApplyView&) constructor; ctx.tx is not
261 // retained, so this must be indistinguishable from building from
262 // ctx.view directly.
263 AccountRootEntryW fromCtx(keylet::account(alice_.id()), ctx, j);
264 EXPECT_TRUE(fromCtx.exists());
265 EXPECT_EQ(&fromCtx.applyView(), &av);
266 EXPECT_EQ(fromCtx.key(), keylet::account(alice_.id()).key);
267
268 AccountRootEntryW const fromView(keylet::account(alice_.id()), av, j);
269 EXPECT_EQ(fromCtx.rawSle(), fromView.rawSle());
270}
271
272TEST_F(SLEBaseTests, writable_lifecycle)
273{
274 // A view we never apply, so nothing here reaches the ledger.
275 ApplyViewImpl av(&env_.getClosedLedger(), TapNone);
276
277 // Entry that does not exist yet: newSLE() -> insert().
278 {
280 EXPECT_FALSE(ticket.exists());
281 EXPECT_EQ(ticket.key(), keylet::ticket(alice_.id(), SeqProxy::rawTicket(1)).key);
282 EXPECT_EQ(ticket.type(), ltTICKET);
283 EXPECT_EQ(ticket.keylet().type, ltTICKET);
284
285 ticket.newSLE();
286 EXPECT_TRUE(ticket.exists());
287 ticket.insert();
288 ticket.update();
289
290 // Erasing an entry inserted in this same view drops it outright.
291 ticket.erase();
292 EXPECT_FALSE(ticket.exists());
293 }
294
295 // Entry that already exists: update() is what promotes it from a bare
296 // peek to a real change. ApplyViewImpl::size() counts Insert, Modify and
297 // Erase but not Cache, so it shows the difference: building the entry
298 // only peeks, and the write is invisible to the view until update().
299 {
300 ApplyViewImpl fresh(&env_.getClosedLedger(), TapNone);
301
302 AccountRootEntryW account(alice_.id(), fresh);
303 EXPECT_TRUE(account.exists());
304 EXPECT_EQ(fresh.size(), 0);
305
306 account->setFieldU32(sfSequence, account->getFieldU32(sfSequence) + 1);
307 EXPECT_EQ(fresh.size(), 0);
308
309 account.update();
310 EXPECT_EQ(fresh.size(), 1);
311
312 // update() is idempotent: the entry is already a Modify.
313 account.update();
314 EXPECT_EQ(fresh.size(), 1);
315 }
316
317 // Entry that already exists. ApplyStateTable::erase() keeps holding
318 // this exact SLE and builds the DeletedNode's FinalFields from it, so
319 // the entry must drop its pointer or a later write would silently
320 // land in transaction metadata.
321 {
322 AccountRootEntryW account(alice_.id(), av);
323 EXPECT_TRUE(account.exists());
324
325 account.erase();
326 EXPECT_FALSE(account.exists());
327 }
328}
329
331{
332 ApplyViewImpl av(&env_.getClosedLedger(), TapNone);
333
334 AccountRootEntryW const writable(alice_.id(), av);
335 EXPECT_TRUE(writable.exists());
336
337 AccountRootEntryR const readOnly = writable;
338 EXPECT_TRUE(readOnly.exists());
339 EXPECT_EQ(readOnly.rawSle(), writable.rawSle());
340
341 ReadOnlySLE const generic = writable;
342 EXPECT_TRUE(generic.exists());
343 EXPECT_EQ(generic.rawSle(), writable.rawSle());
344 // A generic entry has to read the type back out of the SLE.
345 EXPECT_EQ(generic.type(), ltACCOUNT_ROOT);
346}
347
348TEST_F(SLEBaseTests, resolve_entry_peeks)
349{
350 // getOpenLedger() is an OpenView, which derives from ReadView but not
351 // from ApplyView, so resolveEntry's dynamic_cast fails and this takes
352 // the plain ReadView::read() path.
353 OpenView const& ledger = env_.getOpenLedger();
354 AccountRootEntryR const overLedger(alice_.id(), ledger);
355 EXPECT_TRUE(overLedger.exists());
356
357 ApplyViewImpl av(&ledger, TapNone);
358
359 // ReadView const& binds an ApplyViewImpl just as happily, and there the
360 // dynamic_cast succeeds, so this one resolves through ApplyView::peek().
361 AccountRootEntryR const readOnly(alice_.id(), av);
362 EXPECT_TRUE(readOnly.exists());
363
364 AccountRootEntryW writable(alice_.id(), av);
365 EXPECT_TRUE(writable.exists());
366
367 // The invariant resolveEntry() exists to hold: one SLE per key per
368 // view. read() would have handed back the base ledger's entry instead,
369 // which is a different object.
370 EXPECT_EQ(readOnly.rawSle(), writable.rawSle());
371 EXPECT_NE(readOnly.rawSle(), overLedger.rawSle());
372
373 // Which is what keeps a read-only entry from going stale: a write
374 // through any other entry over the same view is visible through it.
375 auto const bumped = writable->getFieldU32(sfSequence) + 1;
376 writable->setFieldU32(sfSequence, bumped);
377 EXPECT_EQ(readOnly->getFieldU32(sfSequence), bumped);
378}
379
380TEST_F(SLEBaseTests, throws_on_missing_entry)
381{
382 // A generic read-only entry has no static type to fall back on, so
383 // type() must read it off the (absent) SLE and throw.
384 ReadOnlySLE const absent(keylet::account(bob_.id()), env_.getClosedLedger());
385 EXPECT_FALSE(absent.exists());
386 EXPECT_THROW(std::ignore = absent.type(), std::logic_error);
387
388 // A per-type read-only entry always knows its type, but keylet() and
389 // key() still have to derive the ledger key from the SLE.
390 AccountRootEntryR const missing(bob_.id(), env_.getClosedLedger());
391 EXPECT_FALSE(missing.exists());
392 EXPECT_THROW(std::ignore = missing.key(), std::logic_error);
393 EXPECT_THROW(std::ignore = missing.keylet(), std::logic_error);
394
395 // Dereferencing an absent entry throws rather than handing back a null
396 // pointer for the caller to walk into.
397 EXPECT_THROW(std::ignore = missing.operator->(), std::logic_error);
398 EXPECT_THROW(std::ignore = (*missing).getType(), std::logic_error);
399}
400
401TEST_F(SLEBaseTests, throws_on_missing_writable_entry)
402{
403 // A view we never apply, so nothing here reaches the ledger.
404 ApplyViewImpl av(&env_.getClosedLedger(), TapNone);
405
406 // bob is unfunded, so this resolves to nothing and every operation that
407 // needs an SLE has to throw instead of dereferencing null. These are the
408 // cases a Release build used to walk straight past, back when they were
409 // XRPL_ASSERTs.
410 AccountRootEntryW missing(bob_.id(), av);
411 EXPECT_FALSE(missing.exists());
412
413 EXPECT_THROW(std::ignore = missing.operator->(), std::logic_error);
414 EXPECT_THROW(std::ignore = (*missing).getType(), std::logic_error);
415 EXPECT_THROW(missing.insert(), std::logic_error);
416 EXPECT_THROW(missing.update(), std::logic_error);
417 EXPECT_THROW(missing.erase(), std::logic_error);
418
419 // keylet() and key() are the exception: a writable entry keeps the keylet
420 // it was built from, so they stay valid before newSLE().
421 EXPECT_EQ(missing.key(), keylet::account(bob_.id()).key);
422
423 // newSLE() is the inverse -- it throws when the entry *does* exist,
424 // rather than silently dropping the SLE already held.
425 missing.newSLE();
426 EXPECT_TRUE(missing.exists());
427 EXPECT_THROW(missing.newSLE(), std::logic_error);
428
429 // And once erased, the entry is empty again and throws as before.
430 missing.insert();
431 missing.erase();
432 EXPECT_FALSE(missing.exists());
433 EXPECT_THROW(missing.update(), std::logic_error);
434}
435
436} // namespace test
437} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Sink & sink() const
Returns the Sink associated with this Journal.
Definition Journal.h:308
static Sink & getNullSink()
Returns a Sink which does nothing.
Editable, discardable view that can build metadata for one tx.
std::size_t size()
Get the number of modified entries.
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:59
A view into a ledger.
Definition ReadView.h:41
View-parameterized base class for all ledger entries.
Definition SLEBase.h:92
LedgerEntryType type() const
Definition SLEBase.h:264
ReadView const & readView() const
Definition SLEBase.h:322
SLE::const_pointer rawSle() const
Definition SLEBase.h:248
LedgerEntryType getType() const
std::shared_ptr< STLedgerEntry const > const_pointer
std::uint32_t getFieldU32(SField const &field) const
Definition STObject.cpp:601
void setFieldU32(SField const &field, std::uint32_t)
Definition STObject.cpp:743
static constexpr SeqProxy rawTicket(std::uint32_t v)
Factory function to return a ticket-based SeqProxy.
Definition SeqProxy.h:74
Scaffolding shared by the test cases below: a funded alice, an unfunded bob (for the entries that nee...
Definition SLEBase.cpp:149
A lightweight transaction testing harness.
Definition TxTest.h:184
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
AccountSet build(PublicKey const &publicKey, SecretKey const &secretKey)
Build and return the AccountSet wrapper.
Derived & setFee(STAmount const &value)
Set the transaction fee.
Derived & setSequence(std::uint32_t const &value)
Set the sequence number.
T is_constructible_v
T is_convertible_v
Keylet ticket(AccountID const &id, SeqProxy const &ticketSeq)
A ticket belonging to an account.
Definition Indexes.cpp:344
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
Ticket operations.
Definition ticket.cpp:13
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
constexpr XRPAmount
Convert XRP to drops (integral types).
Definition TxTest.h:53
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
AccountRootEntry< ReadView > AccountRootEntryR
TEST_F(HardenedHashTest, user_types)
TicketEntry< ApplyView > TicketEntryW
Definition TicketEntry.h:44
@ TapNone
Definition ApplyView.h:28
AccountRootEntry< ApplyView > AccountRootEntryW
SLEBase< ReadView > ReadOnlySLE
Generic (any-entry-type) SLE entries.
Definition SLEBase.h:496
Bundles the mutable ledger view and the transaction being applied.
Definition ApplyView.h:451
LedgerEntryType type
Definition Keylet.h:22