xrpld
Loading...
Searching...
No Matches
tests/libxrpl/tx/ApplyContext.cpp
1#include <xrpl/tx/ApplyContext.h>
2
3#include <xrpl/ledger/ApplyView.h>
4#include <xrpl/ledger/Ledger.h>
5#include <xrpl/ledger/OpenView.h>
6#include <xrpl/protocol/Indexes.h>
7#include <xrpl/protocol/SField.h>
8#include <xrpl/protocol/STAmount.h>
9#include <xrpl/protocol/STLedgerEntry.h>
10#include <xrpl/protocol/STTx.h>
11#include <xrpl/protocol/SeqProxy.h>
12#include <xrpl/protocol/TER.h>
13#include <xrpl/protocol/TxMeta.h>
14#include <xrpl/protocol/XRPAmount.h>
15#include <xrpl/protocol_autogen/transactions/AccountSet.h>
16
17#include <gtest/gtest.h>
18#include <helpers/Account.h>
19#include <helpers/TxTest.h>
20
21#include <cstdint>
22#include <memory>
23
24namespace xrpl::test {
25
30class ApplyContextTests : public ::testing::Test
31{
32protected:
34 Account const alice_{"alice"};
35 XRPAmount const fee_{10};
37
39 {
40 env_.createAccount(alice_, XRP(10'000));
42 builder.setSequence(env_.getAccountRoot(alice_.id()).getSequence());
43 builder.setFee(fee_);
44 tx_ = builder.build(alice_.pk(), alice_.sk()).getSTTx();
45 }
46
55 {
57 sle->setAccountID(sfAccount, alice_.id());
58 sle->setFieldU32(sfTicketSequence, seq);
59 sle->setFieldU64(sfOwnerNode, 0);
60 return sle;
61 }
62};
63
64TEST_F(ApplyContextTests, discard_drops_pending_state_and_keeps_the_context_usable)
65{
66 ASSERT_NE(tx_, nullptr);
67 // Drops burned and amount delivered before the discard, and drops burned
68 // after it. Only the last may reach the ledger.
69 XRPAmount const discardedBurn{10};
70 STAmount const discardedDelivery{XRPAmount{5}};
71 XRPAmount const keptBurn{3};
72
73 // A closed base makes apply() produce metadata, which an open ledger
74 // does not.
75 OpenView open(&env_.getClosedLedger());
76 ApplyContext ctx(env_.getServiceRegistry(), open, *tx_, tesSUCCESS, fee_, TapRetry);
77
78 // Buffer an inserted entry, a modified entry, destroyed drops and a
79 // delivered amount through the context's own surface.
80 auto const stale = makeTicket(1);
81 ctx.view().insert(stale);
82 auto const account = ctx.view().peek(keylet::account(alice_.id()));
83 ASSERT_NE(account, nullptr);
84 auto const baseSeq = account->getFieldU32(sfSequence);
85 account->setFieldU32(sfSequence, baseSeq + 1);
86 ctx.view().update(account);
87 ctx.destroyXRP(discardedBurn);
88 ctx.deliver(discardedDelivery);
89 ASSERT_EQ(ctx.size(), 2);
90
91 ctx.discard();
92
93 // Reads fall through to the open ledger again and the flags survive.
94 EXPECT_EQ(ctx.size(), 0);
95 EXPECT_FALSE(ctx.view().exists(keylet::unchecked(stale->key())));
96 auto const reread = ctx.view().read(keylet::account(alice_.id()));
97 ASSERT_NE(reread, nullptr);
98 EXPECT_EQ(reread->getFieldU32(sfSequence), baseSeq);
99 EXPECT_EQ(ctx.view().flags(), TapRetry);
100
101 // Only changes made after the discard reach the target view.
102 auto const fresh = makeTicket(2);
103 ctx.view().insert(fresh);
104 ctx.destroyXRP(keptBurn);
105 auto const meta = ctx.apply(tesSUCCESS);
106 ASSERT_TRUE(meta.has_value());
107 EXPECT_FALSE(meta->getDeliveredAmount().has_value());
108 EXPECT_FALSE(open.exists(keylet::unchecked(stale->key())));
109 EXPECT_TRUE(open.exists(keylet::unchecked(fresh->key())));
110 auto const applied = open.read(keylet::account(alice_.id()));
111 ASSERT_NE(applied, nullptr);
112 EXPECT_EQ(applied->getFieldU32(sfSequence), baseSeq);
113
114 // OpenView buffers the burn without touching its header, so flush into a
115 // real Ledger to see it: only the post-discard burn is gone, not the
116 // discarded one as well.
117 Ledger const& prev = env_.getClosedLedger();
118 Ledger next(prev, prev.header().closeTime + prev.header().closeTimeResolution);
119 open.apply(next);
120 EXPECT_EQ(next.header().drops, prev.header().drops - keptBurn);
121}
122
123} // namespace xrpl::test
State information when applying a tx.
std::size_t size()
Get the number of unapplied changes.
void destroyXRP(XRPAmount const &fee)
void discard()
Discard changes and start fresh.
void deliver(STAmount const &amount)
Sets the DeliveredAmount field in the metadata.
std::optional< TxMeta > apply(TER)
Apply the transaction result to the base.
ApplyView & view()
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void insert(SLE::Ref sle)=0
Insert a new state SLE.
virtual ApplyFlags flags() const =0
Returns the tx apply flags.
virtual void update(SLE::Ref sle)=0
Indicate changes to a peeked SLE.
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:59
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
std::shared_ptr< STLedgerEntry > pointer
static constexpr SeqProxy rawTicket(std::uint32_t v)
Factory function to return a ticket-based SeqProxy.
Definition SeqProxy.h:74
A funded alice over an open ledger, plus a signed transaction to build an ApplyContext around.
SLE::pointer makeTicket(std::uint32_t seq) const
Build a ticket entry that is not in the ledger.
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.
std::shared_ptr< STTx const > getSTTx() const
Get the underlying STTx object.
Derived & setFee(STAmount const &value)
Set the transaction fee.
Derived & setSequence(std::uint32_t const &value)
Set the sequence number.
T make_shared(T... args)
Keylet unchecked(UInt256 const &key) noexcept
Any ledger entry.
Definition Indexes.cpp:409
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
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
TEST_F(BaseUintDeathTest, from_raw_size_mismatch)
Definition base_uint.cpp:73
constexpr XRPAmount
Convert XRP to drops (integral types).
Definition TxTest.h:53
void open(soci::session &s, BasicConfig const &config, std::string const &dbName)
Open a soci session.
Definition SociDB.cpp:91
@ TapRetry
Definition ApplyView.h:36
@ tesSUCCESS
Definition TER.h:253