xrpld
Loading...
Searching...
No Matches
TicketCreate.cpp
1#include <xrpl/tx/transactors/system/TicketCreate.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/beast/utility/Journal.h>
6#include <xrpl/core/ServiceRegistry.h>
7#include <xrpl/ledger/helpers/AccountRootHelpers.h>
8#include <xrpl/ledger/helpers/DirectoryHelpers.h>
9#include <xrpl/protocol/Indexes.h>
10#include <xrpl/protocol/Keylet.h>
11#include <xrpl/protocol/SField.h>
12#include <xrpl/protocol/STLedgerEntry.h>
13#include <xrpl/protocol/STTx.h>
14#include <xrpl/protocol/TER.h>
15#include <xrpl/protocol/XRPAmount.h>
16#include <xrpl/tx/Transactor.h>
17#include <xrpl/tx/applySteps.h>
18
19#include <cstdint>
20#include <memory>
21
22namespace xrpl {
23
26{
27 // Create TxConsequences identifying the number of sequences consumed.
28 return TxConsequences{ctx.tx, ctx.tx[sfTicketCount]};
29}
30
33{
34 if (std::uint32_t const count = ctx.tx[sfTicketCount];
35 count < kMinValidCount || count > kMaxValidCount)
36 return temINVALID_COUNT;
37
38 return tesSUCCESS;
39}
40
41TER
43{
44 auto const id = ctx.tx[sfAccount];
45 auto const sleAccountRoot = ctx.view.read(keylet::account(id));
46 if (!sleAccountRoot)
47 return terNO_ACCOUNT;
48
49 // Make sure the TicketCreate would not cause the account to own
50 // too many tickets.
51 std::uint32_t const curTicketCount = (*sleAccountRoot)[~sfTicketCount].value_or(0u);
52 std::uint32_t const addedTickets = ctx.tx[sfTicketCount];
53 std::uint32_t const consumedTickets = ctx.tx.getSeqProxy().isTicket() ? 1u : 0u;
54
55 // Note that unsigned integer underflow can't currently happen because
56 // o curTicketCount >= 0
57 // o addedTickets >= 1
58 // o consumedTickets <= 1
59 // So in the worst case addedTickets == consumedTickets and the
60 // computation yields curTicketCount.
61 if (curTicketCount + addedTickets - consumedTickets > kMaxTicketThreshold)
62 return tecDIR_FULL;
63
64 return tesSUCCESS;
65}
66
67TER
69{
70 SLE::pointer const sleAccountRoot = view().peek(keylet::account(accountID_));
71 if (!sleAccountRoot)
72 return tefINTERNAL; // LCOV_EXCL_LINE
73
74 // Each ticket counts against the reserve of the issuing account, but we
75 // check the starting balance because we want to allow dipping into the
76 // reserve to pay fees.
77 std::uint32_t const ticketCount = ctx_.tx[sfTicketCount];
78 {
79 XRPAmount const reserve =
80 view().fees().accountReserve(sleAccountRoot->getFieldU32(sfOwnerCount) + ticketCount);
81
82 if (preFeeBalance_ < reserve)
84 }
85
86 beast::Journal const viewJ{ctx_.registry.get().getJournal("View")};
87
88 // The starting ticket sequence is the same as the current account
89 // root sequence. Before we got here to doApply(), the transaction
90 // machinery already incremented the account root sequence if that
91 // was appropriate.
92 std::uint32_t const firstTicketSeq = (*sleAccountRoot)[sfSequence];
93
94 // Sanity check that the transaction machinery really did already
95 // increment the account root Sequence.
96 if (std::uint32_t const txSeq = ctx_.tx[sfSequence];
97 txSeq != 0 && txSeq != (firstTicketSeq - 1))
98 return tefINTERNAL; // LCOV_EXCL_LINE
99
100 for (std::uint32_t i = 0; i < ticketCount; ++i)
101 {
102 std::uint32_t const curTicketSeq = firstTicketSeq + i;
103 Keylet const ticketKeylet = keylet::ticket(accountID_, curTicketSeq);
104 SLE::pointer const sleTicket = std::make_shared<SLE>(ticketKeylet);
105
106 sleTicket->setAccountID(sfAccount, accountID_);
107 sleTicket->setFieldU32(sfTicketSequence, curTicketSeq);
108 view().insert(sleTicket);
109
110 auto const page = view().dirInsert(
112
113 JLOG(j_.trace()) << "Creating ticket " << to_string(ticketKeylet.key) << ": "
114 << (page ? "success" : "failure");
115
116 if (!page)
117 return tecDIR_FULL; // LCOV_EXCL_LINE
118
119 sleTicket->setFieldU64(sfOwnerNode, *page);
120 }
121
122 // Update the record of the number of Tickets this account owns.
123 std::uint32_t const oldTicketCount = (*sleAccountRoot)[~sfTicketCount].valueOr(0u);
124
125 sleAccountRoot->setFieldU32(sfTicketCount, oldTicketCount + ticketCount);
126
127 // Every added Ticket counts against the creator's reserve.
128 adjustOwnerCount(view(), sleAccountRoot, ticketCount, viewJ);
129
130 // TicketCreate is the only transaction that can cause an account root's
131 // Sequence field to increase by more than one. October 2018.
132 sleAccountRoot->setFieldU32(sfSequence, firstTicketSeq + ticketCount);
133
134 return tesSUCCESS;
135}
136
137void
139{
140 // No transaction-specific invariants yet (future work).
141}
142
143bool
145 STTx const&,
146 TER,
147 XRPAmount,
148 ReadView const&,
149 beast::Journal const&)
150{
151 // No transaction-specific invariants yet (future work).
152 return true;
153}
154
155} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
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.
std::optional< std::uint64_t > dirInsert(Keylet const &directory, uint256 const &key, std::function< void(SLE::ref)> const &describe)
Insert an entry to a directory.
Definition ApplyView.h:340
A view into a ledger.
Definition ReadView.h:31
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
std::shared_ptr< STLedgerEntry > pointer
std::shared_ptr< STLedgerEntry const > const & const_ref
SeqProxy getSeqProxy() const
Definition STTx.cpp:193
constexpr bool isTicket() const
Definition SeqProxy.h:74
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
static NotTEC preflight(PreflightContext const &ctx)
Enforce constraints beyond those of the Transactor base class.
static constexpr std::uint32_t kMaxTicketThreshold
TER doApply() override
Precondition: fee collection is likely.
static TER preclaim(PreclaimContext const &ctx)
Enforce constraints beyond those of the Transactor base class.
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
static TxConsequences makeTxConsequences(PreflightContext const &ctx)
static constexpr std::uint32_t kMaxValidCount
beast::Journal const j_
Definition Transactor.h:118
ApplyView & view()
Definition Transactor.h:136
AccountID const accountID_
Definition Transactor.h:120
XRPAmount preFeeBalance_
Definition Transactor.h:121
ApplyContext & ctx_
Definition Transactor.h:116
Class describing the consequences to the account of applying a transaction if the transaction consume...
Definition applySteps.h:38
T make_shared(T... args)
Keylet ticket(AccountID const &id, std::uint32_t ticketSeq)
A ticket belonging to an account.
Definition Indexes.cpp:295
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:357
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:186
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ terNO_ACCOUNT
Definition TER.h:209
@ tefINTERNAL
Definition TER.h:163
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:594
void adjustOwnerCount(ApplyView &view, SLE::ref sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Returns a function that sets the owner on a directory SLE.
@ temINVALID_COUNT
Definition TER.h:107
TERSubset< CanCvtToTER > TER
Definition TER.h:634
@ tecDIR_FULL
Definition TER.h:285
@ tecINSUFFICIENT_RESERVE
Definition TER.h:305
@ tesSUCCESS
Definition TER.h:240
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:19
uint256 key
Definition Keylet.h:20
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:61
ReadView const & view
Definition Transactor.h:64
State information when preflighting a tx.
Definition Transactor.h:18