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