rippled
Loading...
Searching...
No Matches
CreateTicket.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2014 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/app/tx/detail/CreateTicket.h>
21
22#include <xrpl/basics/Log.h>
23#include <xrpl/protocol/Feature.h>
24#include <xrpl/protocol/Indexes.h>
25#include <xrpl/protocol/TxFlags.h>
26
27namespace ripple {
28
29TxConsequences
31{
32 // Create TxConsequences identifying the number of sequences consumed.
33 return TxConsequences{ctx.tx, ctx.tx[sfTicketCount]};
34}
35
38{
39 if (std::uint32_t const count = ctx.tx[sfTicketCount];
40 count < minValidCount || count > maxValidCount)
41 return temINVALID_COUNT;
42
43 return tesSUCCESS;
44}
45
46TER
48{
49 auto const id = ctx.tx[sfAccount];
50 auto const sleAccountRoot = ctx.view.read(keylet::account(id));
51 if (!sleAccountRoot)
52 return terNO_ACCOUNT;
53
54 // Make sure the TicketCreate would not cause the account to own
55 // too many tickets.
56 std::uint32_t const curTicketCount =
57 (*sleAccountRoot)[~sfTicketCount].value_or(0u);
58 std::uint32_t const addedTickets = ctx.tx[sfTicketCount];
59 std::uint32_t const consumedTickets =
60 ctx.tx.getSeqProxy().isTicket() ? 1u : 0u;
61
62 // Note that unsigned integer underflow can't currently happen because
63 // o curTicketCount >= 0
64 // o addedTickets >= 1
65 // o consumedTickets <= 1
66 // So in the worst case addedTickets == consumedTickets and the
67 // computation yields curTicketCount.
68 if (curTicketCount + addedTickets - consumedTickets > maxTicketThreshold)
69 return tecDIR_FULL;
70
71 return tesSUCCESS;
72}
73
74TER
76{
77 SLE::pointer const sleAccountRoot = view().peek(keylet::account(account_));
78 if (!sleAccountRoot)
79 return tefINTERNAL;
80
81 // Each ticket counts against the reserve of the issuing account, but we
82 // check the starting balance because we want to allow dipping into the
83 // reserve to pay fees.
84 std::uint32_t const ticketCount = ctx_.tx[sfTicketCount];
85 {
86 XRPAmount const reserve = view().fees().accountReserve(
87 sleAccountRoot->getFieldU32(sfOwnerCount) + ticketCount);
88
89 if (mPriorBalance < reserve)
91 }
92
93 beast::Journal viewJ{ctx_.app.journal("View")};
94
95 // The starting ticket sequence is the same as the current account
96 // root sequence. Before we got here to doApply(), the transaction
97 // machinery already incremented the account root sequence if that
98 // was appropriate.
99 std::uint32_t const firstTicketSeq = (*sleAccountRoot)[sfSequence];
100
101 // Sanity check that the transaction machinery really did already
102 // increment the account root Sequence.
103 if (std::uint32_t const txSeq = ctx_.tx[sfSequence];
104 txSeq != 0 && txSeq != (firstTicketSeq - 1))
105 return tefINTERNAL;
106
107 for (std::uint32_t i = 0; i < ticketCount; ++i)
108 {
109 std::uint32_t const curTicketSeq = firstTicketSeq + i;
110 Keylet const ticketKeylet = keylet::ticket(account_, curTicketSeq);
111 SLE::pointer sleTicket = std::make_shared<SLE>(ticketKeylet);
112
113 sleTicket->setAccountID(sfAccount, account_);
114 sleTicket->setFieldU32(sfTicketSequence, curTicketSeq);
115 view().insert(sleTicket);
116
117 auto const page = view().dirInsert(
119 ticketKeylet,
121
122 JLOG(j_.trace()) << "Creating ticket " << to_string(ticketKeylet.key)
123 << ": " << (page ? "success" : "failure");
124
125 if (!page)
126 return tecDIR_FULL;
127
128 sleTicket->setFieldU64(sfOwnerNode, *page);
129 }
130
131 // Update the record of the number of Tickets this account owns.
132 std::uint32_t const oldTicketCount =
133 (*(sleAccountRoot))[~sfTicketCount].value_or(0u);
134
135 sleAccountRoot->setFieldU32(sfTicketCount, oldTicketCount + ticketCount);
136
137 // Every added Ticket counts against the creator's reserve.
138 adjustOwnerCount(view(), sleAccountRoot, ticketCount, viewJ);
139
140 // TicketCreate is the only transaction that can cause an account root's
141 // Sequence field to increase by more than one. October 2018.
142 sleAccountRoot->setFieldU32(sfSequence, firstTicketSeq + ticketCount);
143
144 return tesSUCCESS;
145}
146
147} // namespace ripple
A generic endpoint for log messages.
Definition Journal.h:60
Stream trace() const
Severity stream access functions.
Definition Journal.h:322
virtual beast::Journal journal(std::string const &name)=0
Application & app
virtual void insert(std::shared_ptr< SLE > const &sle)=0
Insert a new state SLE.
std::optional< std::uint64_t > dirInsert(Keylet const &directory, uint256 const &key, std::function< void(std::shared_ptr< SLE > const &)> const &describe)
Insert an entry to a directory.
Definition ApplyView.h:317
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
static TxConsequences makeTxConsequences(PreflightContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
Enforce constraints beyond those of the Transactor base class.
static constexpr std::uint32_t maxValidCount
static TER preclaim(PreclaimContext const &ctx)
Enforce constraints beyond those of the Transactor base class.
static constexpr std::uint32_t maxTicketThreshold
TER doApply() override
Precondition: fee collection is likely.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
SeqProxy getSeqProxy() const
Definition STTx.cpp:216
constexpr bool isTicket() const
Definition SeqProxy.h:94
AccountID const account_
Definition Transactor.h:147
ApplyView & view()
Definition Transactor.h:163
beast::Journal const j_
Definition Transactor.h:145
XRPAmount mPriorBalance
Definition Transactor.h:148
ApplyContext & ctx_
Definition Transactor.h:143
Class describing the consequences to the account of applying a transaction if the transaction consume...
Definition applySteps.h:58
T is_same_v
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:184
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:374
static ticket_t const ticket
Definition Indexes.h:171
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
void adjustOwnerCount(ApplyView &view, std::shared_ptr< SLE > const &sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
Definition View.cpp:1029
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition View.cpp:1047
@ tefINTERNAL
Definition TER.h:173
@ tecDIR_FULL
Definition TER.h:287
@ tecINSUFFICIENT_RESERVE
Definition TER.h:307
@ tesSUCCESS
Definition TER.h:244
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:630
@ terNO_ACCOUNT
Definition TER.h:217
@ temINVALID_COUNT
Definition TER.h:121
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:39
uint256 key
Definition Keylet.h:40
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:80
ReadView const & view
Definition Transactor.h:83
State information when preflighting a tx.
Definition Transactor.h:35