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];
79 view(),
80 sleAccountRoot,
81 j_,
82 {.ownerCountDelta = static_cast<std::int32_t>(ticketCount)}))
84
85 beast::Journal const viewJ{ctx_.registry.get().getJournal("View")};
86
87 // The starting ticket sequence is the same as the current account
88 // root sequence. Before we got here to doApply(), the transaction
89 // machinery already incremented the account root sequence if that
90 // was appropriate.
91 std::uint32_t const firstTicketSeq = (*sleAccountRoot)[sfSequence];
92
93 // Sanity check that the transaction machinery really did already
94 // increment the account root Sequence.
95 if (std::uint32_t const txSeq = ctx_.tx[sfSequence];
96 txSeq != 0 && txSeq != (firstTicketSeq - 1))
97 return tefINTERNAL; // LCOV_EXCL_LINE
98
99 for (std::uint32_t i = 0; i < ticketCount; ++i)
100 {
101 std::uint32_t const curTicketSeq = firstTicketSeq + i;
102 Keylet const ticketKeylet = keylet::ticket(accountID_, SeqProxy::rawTicket(curTicketSeq));
103 SLE::pointer const sleTicket = std::make_shared<SLE>(ticketKeylet);
104
105 sleTicket->setAccountID(sfAccount, accountID_);
106 sleTicket->setFieldU32(sfTicketSequence, curTicketSeq);
107
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 increaseOwnerCount(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:44
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:366
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.
std::shared_ptr< STLedgerEntry > pointer
std::shared_ptr< STLedgerEntry const > const & const_ref
SeqProxy getSeqProxy() const
Definition STTx.cpp:199
constexpr bool isTicket() const
Definition SeqProxy.h:92
static constexpr SeqProxy rawTicket(std::uint32_t v)
Factory function to return a ticket-based SeqProxy.
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:155
ApplyView & view()
Definition Transactor.h:175
AccountID const accountID_
Definition Transactor.h:157
XRPAmount preFeeBalance_
Definition Transactor.h:158
ApplyContext & ctx_
Definition Transactor.h:153
Class describing the consequences to the account of applying a transaction if the transaction consume...
Definition applySteps.h:52
T make_shared(T... args)
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:373
Keylet ticket(AccountID const &id, SeqProxy const &ticketSeq)
A ticket belonging to an account.
Definition Indexes.cpp:310
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:198
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ terNO_ACCOUNT
Definition TER.h:213
void increaseOwnerCount(ApplyView &view, SLE::ref accountSle, SLE::ref sponsorSle, std::uint32_t count, beast::Journal j)
Increase owner-count fields when the caller supplies the sponsor.
@ tefINTERNAL
Definition TER.h:165
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:651
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:607
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:109
TERSubset< CanCvtToTER > TER
Definition TER.h:647
@ tecDIR_FULL
Definition TER.h:290
@ tecINSUFFICIENT_RESERVE
Definition TER.h:310
XRPAmount accountReserve(ReadView const &view, SLE::const_ref sle, beast::Journal j, Adjustment adj={})
Returns the account reserve, in drops.
@ tesSUCCESS
Definition TER.h:245
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
uint256 key
Definition Keylet.h:21
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:83
ReadView const & view
Definition Transactor.h:86
State information when preflighting a tx.
Definition Transactor.h:38