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/ledger/Ledger.h>
21#include <xrpld/app/tx/detail/CreateTicket.h>
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#include <xrpl/protocol/st.h>
27
28namespace ripple {
29
30TxConsequences
32{
33 // Create TxConsequences identifying the number of sequences consumed.
34 return TxConsequences{ctx.tx, ctx.tx[sfTicketCount]};
35}
36
39{
40 if (!ctx.rules.enabled(featureTicketBatch))
41 return temDISABLED;
42
43 if (ctx.tx.getFlags() & tfUniversalMask)
44 return temINVALID_FLAG;
45
46 if (std::uint32_t const count = ctx.tx[sfTicketCount];
47 count < minValidCount || count > maxValidCount)
48 return temINVALID_COUNT;
49
50 if (NotTEC const ret{preflight1(ctx)}; !isTesSuccess(ret))
51 return ret;
52
53 return preflight2(ctx);
54}
55
56TER
58{
59 auto const id = ctx.tx[sfAccount];
60 auto const sleAccountRoot = ctx.view.read(keylet::account(id));
61 if (!sleAccountRoot)
62 return terNO_ACCOUNT;
63
64 // Make sure the TicketCreate would not cause the account to own
65 // too many tickets.
66 std::uint32_t const curTicketCount =
67 (*sleAccountRoot)[~sfTicketCount].value_or(0u);
68 std::uint32_t const addedTickets = ctx.tx[sfTicketCount];
69 std::uint32_t const consumedTickets =
70 ctx.tx.getSeqProxy().isTicket() ? 1u : 0u;
71
72 // Note that unsigned integer underflow can't currently happen because
73 // o curTicketCount >= 0
74 // o addedTickets >= 1
75 // o consumedTickets <= 1
76 // So in the worst case addedTickets == consumedTickets and the
77 // computation yields curTicketCount.
78 if (curTicketCount + addedTickets - consumedTickets > maxTicketThreshold)
79 return tecDIR_FULL;
80
81 return tesSUCCESS;
82}
83
84TER
86{
87 SLE::pointer const sleAccountRoot = view().peek(keylet::account(account_));
88 if (!sleAccountRoot)
89 return tefINTERNAL;
90
91 // Each ticket counts against the reserve of the issuing account, but we
92 // check the starting balance because we want to allow dipping into the
93 // reserve to pay fees.
94 std::uint32_t const ticketCount = ctx_.tx[sfTicketCount];
95 {
96 XRPAmount const reserve = view().fees().accountReserve(
97 sleAccountRoot->getFieldU32(sfOwnerCount) + ticketCount);
98
99 if (mPriorBalance < reserve)
101 }
102
103 beast::Journal viewJ{ctx_.app.journal("View")};
104
105 // The starting ticket sequence is the same as the current account
106 // root sequence. Before we got here to doApply(), the transaction
107 // machinery already incremented the account root sequence if that
108 // was appropriate.
109 std::uint32_t const firstTicketSeq = (*sleAccountRoot)[sfSequence];
110
111 // Sanity check that the transaction machinery really did already
112 // increment the account root Sequence.
113 if (std::uint32_t const txSeq = ctx_.tx[sfSequence];
114 txSeq != 0 && txSeq != (firstTicketSeq - 1))
115 return tefINTERNAL;
116
117 for (std::uint32_t i = 0; i < ticketCount; ++i)
118 {
119 std::uint32_t const curTicketSeq = firstTicketSeq + i;
120 Keylet const ticketKeylet = keylet::ticket(account_, curTicketSeq);
121 SLE::pointer sleTicket = std::make_shared<SLE>(ticketKeylet);
122
123 sleTicket->setAccountID(sfAccount, account_);
124 sleTicket->setFieldU32(sfTicketSequence, curTicketSeq);
125 view().insert(sleTicket);
126
127 auto const page = view().dirInsert(
129 ticketKeylet,
131
132 JLOG(j_.trace()) << "Creating ticket " << to_string(ticketKeylet.key)
133 << ": " << (page ? "success" : "failure");
134
135 if (!page)
136 return tecDIR_FULL;
137
138 sleTicket->setFieldU64(sfOwnerNode, *page);
139 }
140
141 // Update the record of the number of Tickets this account owns.
142 std::uint32_t const oldTicketCount =
143 (*(sleAccountRoot))[~sfTicketCount].value_or(0u);
144
145 sleAccountRoot->setFieldU32(sfTicketCount, oldTicketCount + ticketCount);
146
147 // Every added Ticket counts against the creator's reserve.
148 adjustOwnerCount(view(), sleAccountRoot, ticketCount, viewJ);
149
150 // TicketCreate is the only transaction that can cause an account root's
151 // Sequence field to increase by more than one. October 2018.
152 sleAccountRoot->setFieldU32(sfSequence, firstTicketSeq + ticketCount);
153
154 return tesSUCCESS;
155}
156
157} // namespace ripple
A generic endpoint for log messages.
Definition: Journal.h:59
Stream trace() const
Severity stream access functions.
Definition: Journal.h:311
virtual beast::Journal journal(std::string const &name)=0
Application & app
Definition: ApplyContext.h:47
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:310
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
Definition: CreateTicket.h:57
static TER preclaim(PreclaimContext const &ctx)
Enforce constraints beyond those of the Transactor base class.
static constexpr std::uint32_t maxTicketThreshold
Definition: CreateTicket.h:65
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:122
std::uint32_t getFlags() const
Definition: STObject.cpp:507
SeqProxy getSeqProxy() const
Definition: STTx.cpp:186
constexpr bool isTicket() const
Definition: SeqProxy.h:94
AccountID const account_
Definition: Transactor.h:91
ApplyView & view()
Definition: Transactor.h:107
beast::Journal const j_
Definition: Transactor.h:89
XRPAmount mPriorBalance
Definition: Transactor.h:92
ApplyContext & ctx_
Definition: Transactor.h:88
Class describing the consequences to the account of applying a transaction if the transaction consume...
Definition: applySteps.h:46
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:160
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition: Indexes.cpp:350
static ticket_t const ticket
Definition: Indexes.h:170
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
bool isTesSuccess(TER x)
Definition: TER.h:656
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition: View.cpp:848
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
Definition: Transactor.cpp:82
@ tefINTERNAL
Definition: TER.h:173
static bool adjustOwnerCount(ApplyContext &ctx, int count)
Definition: SetOracle.cpp:186
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:134
@ tecDIR_FULL
Definition: TER.h:274
@ tecINSUFFICIENT_RESERVE
Definition: TER.h:294
@ tesSUCCESS
Definition: TER.h:242
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:629
constexpr std::uint32_t tfUniversalMask
Definition: TxFlags.h:62
@ 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.
Definition: protocol/Fees.h:49
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:53
ReadView const & view
Definition: Transactor.h:56
State information when preflighting a tx.
Definition: Transactor.h:32