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 (!ctx.rules.enabled(featureTicketBatch))
40 return temDISABLED;
41
42 if (ctx.tx.getFlags() & tfUniversalMask)
43 return temINVALID_FLAG;
44
45 if (std::uint32_t const count = ctx.tx[sfTicketCount];
46 count < minValidCount || count > maxValidCount)
47 return temINVALID_COUNT;
48
49 if (NotTEC const ret{preflight1(ctx)}; !isTesSuccess(ret))
50 return ret;
51
52 return preflight2(ctx);
53}
54
55TER
57{
58 auto const id = ctx.tx[sfAccount];
59 auto const sleAccountRoot = ctx.view.read(keylet::account(id));
60 if (!sleAccountRoot)
61 return terNO_ACCOUNT;
62
63 // Make sure the TicketCreate would not cause the account to own
64 // too many tickets.
65 std::uint32_t const curTicketCount =
66 (*sleAccountRoot)[~sfTicketCount].value_or(0u);
67 std::uint32_t const addedTickets = ctx.tx[sfTicketCount];
68 std::uint32_t const consumedTickets =
69 ctx.tx.getSeqProxy().isTicket() ? 1u : 0u;
70
71 // Note that unsigned integer underflow can't currently happen because
72 // o curTicketCount >= 0
73 // o addedTickets >= 1
74 // o consumedTickets <= 1
75 // So in the worst case addedTickets == consumedTickets and the
76 // computation yields curTicketCount.
77 if (curTicketCount + addedTickets - consumedTickets > maxTicketThreshold)
78 return tecDIR_FULL;
79
80 return tesSUCCESS;
81}
82
83TER
85{
86 SLE::pointer const sleAccountRoot = view().peek(keylet::account(account_));
87 if (!sleAccountRoot)
88 return tefINTERNAL;
89
90 // Each ticket counts against the reserve of the issuing account, but we
91 // check the starting balance because we want to allow dipping into the
92 // reserve to pay fees.
93 std::uint32_t const ticketCount = ctx_.tx[sfTicketCount];
94 {
95 XRPAmount const reserve = view().fees().accountReserve(
96 sleAccountRoot->getFieldU32(sfOwnerCount) + ticketCount);
97
98 if (mPriorBalance < reserve)
100 }
101
102 beast::Journal viewJ{ctx_.app.journal("View")};
103
104 // The starting ticket sequence is the same as the current account
105 // root sequence. Before we got here to doApply(), the transaction
106 // machinery already incremented the account root sequence if that
107 // was appropriate.
108 std::uint32_t const firstTicketSeq = (*sleAccountRoot)[sfSequence];
109
110 // Sanity check that the transaction machinery really did already
111 // increment the account root Sequence.
112 if (std::uint32_t const txSeq = ctx_.tx[sfSequence];
113 txSeq != 0 && txSeq != (firstTicketSeq - 1))
114 return tefINTERNAL;
115
116 for (std::uint32_t i = 0; i < ticketCount; ++i)
117 {
118 std::uint32_t const curTicketSeq = firstTicketSeq + i;
119 Keylet const ticketKeylet = keylet::ticket(account_, curTicketSeq);
120 SLE::pointer sleTicket = std::make_shared<SLE>(ticketKeylet);
121
122 sleTicket->setAccountID(sfAccount, account_);
123 sleTicket->setFieldU32(sfTicketSequence, curTicketSeq);
124 view().insert(sleTicket);
125
126 auto const page = view().dirInsert(
128 ticketKeylet,
130
131 JLOG(j_.trace()) << "Creating ticket " << to_string(ticketKeylet.key)
132 << ": " << (page ? "success" : "failure");
133
134 if (!page)
135 return tecDIR_FULL;
136
137 sleTicket->setFieldU64(sfOwnerNode, *page);
138 }
139
140 // Update the record of the number of Tickets this account owns.
141 std::uint32_t const oldTicketCount =
142 (*(sleAccountRoot))[~sfTicketCount].value_or(0u);
143
144 sleAccountRoot->setFieldU32(sfTicketCount, oldTicketCount + ticketCount);
145
146 // Every added Ticket counts against the creator's reserve.
147 adjustOwnerCount(view(), sleAccountRoot, ticketCount, viewJ);
148
149 // TicketCreate is the only transaction that can cause an account root's
150 // Sequence field to increase by more than one. October 2018.
151 sleAccountRoot->setFieldU32(sfSequence, firstTicketSeq + ticketCount);
152
153 return tesSUCCESS;
154}
155
156} // 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.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:130
std::uint32_t getFlags() const
Definition STObject.cpp:537
SeqProxy getSeqProxy() const
Definition STTx.cpp:216
constexpr bool isTicket() const
Definition SeqProxy.h:94
AccountID const account_
Definition Transactor.h:145
ApplyView & view()
Definition Transactor.h:161
beast::Journal const j_
Definition Transactor.h:143
XRPAmount mPriorBalance
Definition Transactor.h:146
ApplyContext & ctx_
Definition Transactor.h:141
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
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
@ tefINTERNAL
Definition TER.h:173
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
@ tecDIR_FULL
Definition TER.h:287
@ tecINSUFFICIENT_RESERVE
Definition TER.h:307
@ tesSUCCESS
Definition TER.h:244
bool isTesSuccess(TER x) noexcept
Definition TER.h:674
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:630
constexpr std::uint32_t tfUniversalMask
Definition TxFlags.h:63
@ terNO_ACCOUNT
Definition TER.h:217
@ temINVALID_COUNT
Definition TER.h:121
@ temINVALID_FLAG
Definition TER.h:111
@ temDISABLED
Definition TER.h:114
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