xrpld
Loading...
Searching...
No Matches
EntryTestHelpers.h
1#pragma once
2
3#include <xrpl/basics/base_uint.h>
4#include <xrpl/ledger/ApplyView.h>
5#include <xrpl/ledger/ApplyViewImpl.h>
6#include <xrpl/ledger/ReadView.h>
7#include <xrpl/protocol/Keylet.h>
8#include <xrpl/protocol/LedgerHeader.h>
9
10#include <gtest/gtest.h>
11#include <helpers/Account.h>
12#include <helpers/TxTest.h>
13
14#include <string>
15
16namespace xrpl::test {
17
28{
29public:
31 Account const alice{"alice"};
32 Account const bob{"bob"};
33 Account const carol{"carol"};
34
38
44 [[nodiscard]] ReadView const&
45 read() const
46 {
47 return env.getClosedLedger();
48 }
49
50 [[nodiscard]] ApplyView&
52 {
53 return av_;
54 }
55
61 [[nodiscard]] UInt256
62 someID() const
63 {
64 return read().header().parentHash;
65 }
66
67private:
68 // Runs from the av_ member initializer, so it may only touch env and the
69 // accounts -- everything declared above av_.
70 ReadView const&
72 {
73 env.createAccount(alice, XRP(10'000));
74 env.createAccount(bob, XRP(10'000));
75 env.createAccount(carol, XRP(10'000));
76 env.close();
77 return env.getClosedLedger();
78 }
79
81};
82
94template <template <typename> class Entry, typename... Args>
95void
96expectKeylet(EntryTestEnv& e, Keylet const& expected, std::string const& what, Args const&... args)
97{
98 bool const present = e.read().read(expected) != nullptr;
99
100 // The writable entry retains its keylet, so it can be inspected whether
101 // or not the entry exists.
102 Entry<ApplyView> const w(args..., e.apply());
103 EXPECT_EQ(w.keylet().key, expected.key) << what << ": writable key";
104 EXPECT_EQ(w.keylet().type, expected.type) << what << ": writable type";
105 EXPECT_EQ(w.exists(), present) << what << ": writable exists";
106
107 // The read-only entry has no keylet of its own -- it derives one from the
108 // SLE, and only when the SLE exists. Its agreement with a direct read
109 // of the expected keylet is what shows it resolved the same key.
110 Entry<ReadView> const r(args..., e.read());
111 EXPECT_EQ(r.exists(), present) << what << ": read-only exists";
112 if (present)
113 {
114 EXPECT_EQ(r.key(), expected.key) << what << ": read-only key";
115 }
116
117 // Not r.type(): for a typed entry that returns kEntryType, so checking it
118 // would just be this same assertion spelled twice.
119 static_assert(Entry<ReadView>::kEntryType == Entry<ApplyView>::kEntryType);
120 EXPECT_EQ(Entry<ReadView>::kEntryType, expected.type) << what << ": kEntryType";
121}
122
123} // namespace xrpl::test
Editable, discardable view that can build metadata for one tx.
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:141
A view into a ledger.
Definition ReadView.h:41
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
virtual LedgerHeader const & header() const =0
Returns information about the ledger.
Scaffolding shared by the per-entry-type suites.
ReadView const & fundAndClose()
UInt256 someID() const
An arbitrary but stable UInt256, for the entry constructors that take an object ID directly.
ReadView const & read() const
The closed ledger apply() was built over.
A lightweight transaction testing harness.
Definition TxTest.h:185
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
void expectKeylet(EntryTestEnv &e, Keylet const &expected, std::string const &what, Args const &... args)
Assert that both flavors of Entry built from args resolve the ledger object that expected names.
BaseUInt< 256 > UInt256
Definition base_uint.h:580
@ TapNone
Definition ApplyView.h:28
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
UInt256 key
Definition Keylet.h:21
LedgerEntryType type
Definition Keylet.h:22