rippled
Loading...
Searching...
No Matches
CreateCheck.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2017 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/CreateCheck.h>
21
22#include <xrpl/basics/Log.h>
23#include <xrpl/ledger/View.h>
24#include <xrpl/protocol/Feature.h>
25#include <xrpl/protocol/Indexes.h>
26#include <xrpl/protocol/TER.h>
27#include <xrpl/protocol/TxFlags.h>
28
29namespace ripple {
30
33{
34 if (!ctx.rules.enabled(featureChecks))
35 return temDISABLED;
36
37 NotTEC const ret{preflight1(ctx)};
38 if (!isTesSuccess(ret))
39 return ret;
40
41 if (ctx.tx.getFlags() & tfUniversalMask)
42 {
43 // There are no flags (other than universal) for CreateCheck yet.
44 JLOG(ctx.j.warn()) << "Malformed transaction: Invalid flags set.";
45 return temINVALID_FLAG;
46 }
47 if (ctx.tx[sfAccount] == ctx.tx[sfDestination])
48 {
49 // They wrote a check to themselves.
50 JLOG(ctx.j.warn()) << "Malformed transaction: Check to self.";
51 return temREDUNDANT;
52 }
53
54 {
55 STAmount const sendMax{ctx.tx.getFieldAmount(sfSendMax)};
56 if (!isLegalNet(sendMax) || sendMax.signum() <= 0)
57 {
58 JLOG(ctx.j.warn()) << "Malformed transaction: bad sendMax amount: "
59 << sendMax.getFullText();
60 return temBAD_AMOUNT;
61 }
62
63 if (badCurrency() == sendMax.getCurrency())
64 {
65 JLOG(ctx.j.warn()) << "Malformed transaction: Bad currency.";
66 return temBAD_CURRENCY;
67 }
68 }
69
70 if (auto const optExpiry = ctx.tx[~sfExpiration])
71 {
72 if (*optExpiry == 0)
73 {
74 JLOG(ctx.j.warn()) << "Malformed transaction: bad expiration";
75 return temBAD_EXPIRATION;
76 }
77 }
78
79 return preflight2(ctx);
80}
81
82TER
84{
85 AccountID const dstId{ctx.tx[sfDestination]};
86 auto const sleDst = ctx.view.read(keylet::account(dstId));
87 if (!sleDst)
88 {
89 JLOG(ctx.j.warn()) << "Destination account does not exist.";
90 return tecNO_DST;
91 }
92
93 auto const flags = sleDst->getFlags();
94
95 // Check if the destination has disallowed incoming checks
96 if (ctx.view.rules().enabled(featureDisallowIncoming) &&
98 return tecNO_PERMISSION;
99
100 // Pseudo-accounts cannot cash checks. Note, this is not amendment-gated
101 // because all writes to pseudo-account discriminator fields **are**
102 // amendment gated, hence the behaviour of this check will always match the
103 // currently active amendments.
104 if (isPseudoAccount(sleDst))
105 return tecNO_PERMISSION;
106
107 if ((flags & lsfRequireDestTag) && !ctx.tx.isFieldPresent(sfDestinationTag))
108 {
109 // The tag is basically account-specific information we don't
110 // understand, but we can require someone to fill it in.
111 JLOG(ctx.j.warn()) << "Malformed transaction: DestinationTag required.";
112 return tecDST_TAG_NEEDED;
113 }
114
115 {
116 STAmount const sendMax{ctx.tx[sfSendMax]};
117 if (!sendMax.native())
118 {
119 // The currency may not be globally frozen
120 AccountID const& issuerId{sendMax.getIssuer()};
121 if (isGlobalFrozen(ctx.view, issuerId))
122 {
123 JLOG(ctx.j.warn()) << "Creating a check for frozen asset";
124 return tecFROZEN;
125 }
126 // If this account has a trustline for the currency, that
127 // trustline may not be frozen.
128 //
129 // Note that we DO allow create check for a currency that the
130 // account does not yet have a trustline to.
131 AccountID const srcId{ctx.tx.getAccountID(sfAccount)};
132 if (issuerId != srcId)
133 {
134 // Check if the issuer froze the line
135 auto const sleTrust = ctx.view.read(
136 keylet::line(srcId, issuerId, sendMax.getCurrency()));
137 if (sleTrust &&
138 sleTrust->isFlag(
139 (issuerId > srcId) ? lsfHighFreeze : lsfLowFreeze))
140 {
141 JLOG(ctx.j.warn())
142 << "Creating a check for frozen trustline.";
143 return tecFROZEN;
144 }
145 }
146 if (issuerId != dstId)
147 {
148 // Check if dst froze the line.
149 auto const sleTrust = ctx.view.read(
150 keylet::line(issuerId, dstId, sendMax.getCurrency()));
151 if (sleTrust &&
152 sleTrust->isFlag(
153 (dstId > issuerId) ? lsfHighFreeze : lsfLowFreeze))
154 {
155 JLOG(ctx.j.warn())
156 << "Creating a check for destination frozen trustline.";
157 return tecFROZEN;
158 }
159 }
160 }
161 }
162 if (hasExpired(ctx.view, ctx.tx[~sfExpiration]))
163 {
164 JLOG(ctx.j.warn()) << "Creating a check that has already expired.";
165 return tecEXPIRED;
166 }
167 return tesSUCCESS;
168}
169
170TER
172{
173 auto const sle = view().peek(keylet::account(account_));
174 if (!sle)
175 return tefINTERNAL;
176
177 // A check counts against the reserve of the issuing account, but we
178 // check the starting balance because we want to allow dipping into the
179 // reserve to pay fees.
180 {
181 STAmount const reserve{
182 view().fees().accountReserve(sle->getFieldU32(sfOwnerCount) + 1)};
183
184 if (mPriorBalance < reserve)
186 }
187
188 // Note that we use the value from the sequence or ticket as the
189 // Check sequence. For more explanation see comments in SeqProxy.h.
190 std::uint32_t const seq = ctx_.tx.getSeqValue();
191 Keylet const checkKeylet = keylet::check(account_, seq);
192 auto sleCheck = std::make_shared<SLE>(checkKeylet);
193
194 sleCheck->setAccountID(sfAccount, account_);
195 AccountID const dstAccountId = ctx_.tx[sfDestination];
196 sleCheck->setAccountID(sfDestination, dstAccountId);
197 sleCheck->setFieldU32(sfSequence, seq);
198 sleCheck->setFieldAmount(sfSendMax, ctx_.tx[sfSendMax]);
199 if (auto const srcTag = ctx_.tx[~sfSourceTag])
200 sleCheck->setFieldU32(sfSourceTag, *srcTag);
201 if (auto const dstTag = ctx_.tx[~sfDestinationTag])
202 sleCheck->setFieldU32(sfDestinationTag, *dstTag);
203 if (auto const invoiceId = ctx_.tx[~sfInvoiceID])
204 sleCheck->setFieldH256(sfInvoiceID, *invoiceId);
205 if (auto const expiry = ctx_.tx[~sfExpiration])
206 sleCheck->setFieldU32(sfExpiration, *expiry);
207
208 view().insert(sleCheck);
209
210 auto viewJ = ctx_.app.journal("View");
211 // If it's not a self-send (and it shouldn't be), add Check to the
212 // destination's owner directory.
213 if (dstAccountId != account_)
214 {
215 auto const page = view().dirInsert(
216 keylet::ownerDir(dstAccountId),
217 checkKeylet,
218 describeOwnerDir(dstAccountId));
219
220 JLOG(j_.trace()) << "Adding Check to destination directory "
221 << to_string(checkKeylet.key) << ": "
222 << (page ? "success" : "failure");
223
224 if (!page)
225 return tecDIR_FULL;
226
227 sleCheck->setFieldU64(sfDestinationNode, *page);
228 }
229
230 {
231 auto const page = view().dirInsert(
233 checkKeylet,
235
236 JLOG(j_.trace()) << "Adding Check to owner directory "
237 << to_string(checkKeylet.key) << ": "
238 << (page ? "success" : "failure");
239
240 if (!page)
241 return tecDIR_FULL;
242
243 sleCheck->setFieldU64(sfOwnerNode, *page);
244 }
245 // If we succeeded, the new entry counts against the creator's reserve.
246 adjustOwnerCount(view(), sle, 1, viewJ);
247 return tesSUCCESS;
248}
249
250} // namespace ripple
Stream trace() const
Severity stream access functions.
Definition Journal.h:322
Stream warn() const
Definition Journal.h:340
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.
TER doApply() override
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
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.
virtual Rules const & rules() const =0
Returns the tx processing rules.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:130
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:651
STAmount const & getFieldAmount(SField const &field) const
Definition STObject.cpp:665
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:484
void setAccountID(SField const &field, AccountID const &)
Definition STObject.cpp:771
std::uint32_t getFlags() const
Definition STObject.cpp:537
std::uint32_t getSeqValue() const
Returns the first non-zero value of (Sequence, TicketSequence).
Definition STTx.cpp:231
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
T is_same_v
Keylet line(AccountID const &id0, AccountID const &id1, Currency const &currency) noexcept
The index of a trust line for a given currency.
Definition Indexes.cpp:244
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
Keylet check(AccountID const &id, std::uint32_t seq) noexcept
A Check.
Definition Indexes.cpp:336
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
bool isLegalNet(STAmount const &value)
Definition STAmount.h:600
@ lsfRequireDestTag
@ lsfDisallowIncomingCheck
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.
bool hasExpired(ReadView const &view, std::optional< std::uint32_t > const &exp)
Determines whether the given expiration time has passed.
Definition View.cpp:173
@ tecNO_DST
Definition TER.h:290
@ tecDIR_FULL
Definition TER.h:287
@ tecFROZEN
Definition TER.h:303
@ tecNO_PERMISSION
Definition TER.h:305
@ tecDST_TAG_NEEDED
Definition TER.h:309
@ tecINSUFFICIENT_RESERVE
Definition TER.h:307
@ tecEXPIRED
Definition TER.h:314
@ 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
bool isPseudoAccount(std::shared_ptr< SLE const > sleAcct)
Definition View.cpp:1115
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:605
bool isGlobalFrozen(ReadView const &view, AccountID const &issuer)
Definition View.cpp:182
@ temBAD_AMOUNT
Definition TER.h:89
@ temREDUNDANT
Definition TER.h:112
@ temBAD_CURRENCY
Definition TER.h:90
@ temBAD_EXPIRATION
Definition TER.h:91
@ 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
beast::Journal const j
Definition Transactor.h:88
State information when preflighting a tx.
Definition Transactor.h:35
beast::Journal const j
Definition Transactor.h:42