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.tx[sfAccount] == ctx.tx[sfDestination])
35 {
36 // They wrote a check to themselves.
37 JLOG(ctx.j.warn()) << "Malformed transaction: Check to self.";
38 return temREDUNDANT;
39 }
40
41 {
42 STAmount const sendMax{ctx.tx.getFieldAmount(sfSendMax)};
43 if (!isLegalNet(sendMax) || sendMax.signum() <= 0)
44 {
45 JLOG(ctx.j.warn()) << "Malformed transaction: bad sendMax amount: "
46 << sendMax.getFullText();
47 return temBAD_AMOUNT;
48 }
49
50 if (badCurrency() == sendMax.getCurrency())
51 {
52 JLOG(ctx.j.warn()) << "Malformed transaction: Bad currency.";
53 return temBAD_CURRENCY;
54 }
55 }
56
57 if (auto const optExpiry = ctx.tx[~sfExpiration])
58 {
59 if (*optExpiry == 0)
60 {
61 JLOG(ctx.j.warn()) << "Malformed transaction: bad expiration";
62 return temBAD_EXPIRATION;
63 }
64 }
65
66 return tesSUCCESS;
67}
68
69TER
71{
72 AccountID const dstId{ctx.tx[sfDestination]};
73 auto const sleDst = ctx.view.read(keylet::account(dstId));
74 if (!sleDst)
75 {
76 JLOG(ctx.j.warn()) << "Destination account does not exist.";
77 return tecNO_DST;
78 }
79
80 auto const flags = sleDst->getFlags();
81
82 // Check if the destination has disallowed incoming checks
83 if (ctx.view.rules().enabled(featureDisallowIncoming) &&
85 return tecNO_PERMISSION;
86
87 // Pseudo-accounts cannot cash checks. Note, this is not amendment-gated
88 // because all writes to pseudo-account discriminator fields **are**
89 // amendment gated, hence the behaviour of this check will always match the
90 // currently active amendments.
91 if (isPseudoAccount(sleDst))
92 return tecNO_PERMISSION;
93
94 if ((flags & lsfRequireDestTag) && !ctx.tx.isFieldPresent(sfDestinationTag))
95 {
96 // The tag is basically account-specific information we don't
97 // understand, but we can require someone to fill it in.
98 JLOG(ctx.j.warn()) << "Malformed transaction: DestinationTag required.";
99 return tecDST_TAG_NEEDED;
100 }
101
102 {
103 STAmount const sendMax{ctx.tx[sfSendMax]};
104 if (!sendMax.native())
105 {
106 // The currency may not be globally frozen
107 AccountID const& issuerId{sendMax.getIssuer()};
108 if (isGlobalFrozen(ctx.view, issuerId))
109 {
110 JLOG(ctx.j.warn()) << "Creating a check for frozen asset";
111 return tecFROZEN;
112 }
113 // If this account has a trustline for the currency, that
114 // trustline may not be frozen.
115 //
116 // Note that we DO allow create check for a currency that the
117 // account does not yet have a trustline to.
118 AccountID const srcId{ctx.tx.getAccountID(sfAccount)};
119 if (issuerId != srcId)
120 {
121 // Check if the issuer froze the line
122 auto const sleTrust = ctx.view.read(
123 keylet::line(srcId, issuerId, sendMax.getCurrency()));
124 if (sleTrust &&
125 sleTrust->isFlag(
126 (issuerId > srcId) ? lsfHighFreeze : lsfLowFreeze))
127 {
128 JLOG(ctx.j.warn())
129 << "Creating a check for frozen trustline.";
130 return tecFROZEN;
131 }
132 }
133 if (issuerId != dstId)
134 {
135 // Check if dst froze the line.
136 auto const sleTrust = ctx.view.read(
137 keylet::line(issuerId, dstId, sendMax.getCurrency()));
138 if (sleTrust &&
139 sleTrust->isFlag(
140 (dstId > issuerId) ? lsfHighFreeze : lsfLowFreeze))
141 {
142 JLOG(ctx.j.warn())
143 << "Creating a check for destination frozen trustline.";
144 return tecFROZEN;
145 }
146 }
147 }
148 }
149 if (hasExpired(ctx.view, ctx.tx[~sfExpiration]))
150 {
151 JLOG(ctx.j.warn()) << "Creating a check that has already expired.";
152 return tecEXPIRED;
153 }
154 return tesSUCCESS;
155}
156
157TER
159{
160 auto const sle = view().peek(keylet::account(account_));
161 if (!sle)
162 return tefINTERNAL; // LCOV_EXCL_LINE
163
164 // A check counts against the reserve of the issuing account, but we
165 // check the starting balance because we want to allow dipping into the
166 // reserve to pay fees.
167 {
168 STAmount const reserve{
169 view().fees().accountReserve(sle->getFieldU32(sfOwnerCount) + 1)};
170
171 if (mPriorBalance < reserve)
173 }
174
175 // Note that we use the value from the sequence or ticket as the
176 // Check sequence. For more explanation see comments in SeqProxy.h.
177 std::uint32_t const seq = ctx_.tx.getSeqValue();
178 Keylet const checkKeylet = keylet::check(account_, seq);
179 auto sleCheck = std::make_shared<SLE>(checkKeylet);
180
181 sleCheck->setAccountID(sfAccount, account_);
182 AccountID const dstAccountId = ctx_.tx[sfDestination];
183 sleCheck->setAccountID(sfDestination, dstAccountId);
184 sleCheck->setFieldU32(sfSequence, seq);
185 sleCheck->setFieldAmount(sfSendMax, ctx_.tx[sfSendMax]);
186 if (auto const srcTag = ctx_.tx[~sfSourceTag])
187 sleCheck->setFieldU32(sfSourceTag, *srcTag);
188 if (auto const dstTag = ctx_.tx[~sfDestinationTag])
189 sleCheck->setFieldU32(sfDestinationTag, *dstTag);
190 if (auto const invoiceId = ctx_.tx[~sfInvoiceID])
191 sleCheck->setFieldH256(sfInvoiceID, *invoiceId);
192 if (auto const expiry = ctx_.tx[~sfExpiration])
193 sleCheck->setFieldU32(sfExpiration, *expiry);
194
195 view().insert(sleCheck);
196
197 auto viewJ = ctx_.app.journal("View");
198 // If it's not a self-send (and it shouldn't be), add Check to the
199 // destination's owner directory.
200 if (dstAccountId != account_)
201 {
202 auto const page = view().dirInsert(
203 keylet::ownerDir(dstAccountId),
204 checkKeylet,
205 describeOwnerDir(dstAccountId));
206
207 JLOG(j_.trace()) << "Adding Check to destination directory "
208 << to_string(checkKeylet.key) << ": "
209 << (page ? "success" : "failure");
210
211 if (!page)
212 return tecDIR_FULL; // LCOV_EXCL_LINE
213
214 sleCheck->setFieldU64(sfDestinationNode, *page);
215 }
216
217 {
218 auto const page = view().dirInsert(
220 checkKeylet,
222
223 JLOG(j_.trace()) << "Adding Check to owner directory "
224 << to_string(checkKeylet.key) << ": "
225 << (page ? "success" : "failure");
226
227 if (!page)
228 return tecDIR_FULL; // LCOV_EXCL_LINE
229
230 sleCheck->setFieldU64(sfOwnerNode, *page);
231 }
232 // If we succeeded, the new entry counts against the creator's reserve.
233 adjustOwnerCount(view(), sle, 1, viewJ);
234 return tesSUCCESS;
235}
236
237} // 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:319
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:657
STAmount const & getFieldAmount(SField const &field) const
Definition STObject.cpp:671
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:484
void setAccountID(SField const &field, AccountID const &)
Definition STObject.cpp:783
std::uint32_t getSeqValue() const
Returns the first non-zero value of (Sequence, TicketSequence).
Definition STTx.cpp:231
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
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:1032
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition View.cpp:1050
@ tefINTERNAL
Definition TER.h:173
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
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:630
bool isPseudoAccount(std::shared_ptr< SLE const > sleAcct)
Definition View.cpp:1118
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
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