rippled
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 <ripple/app/ledger/Ledger.h>
21 #include <ripple/app/tx/impl/CreateCheck.h>
22 #include <ripple/basics/Log.h>
23 #include <ripple/protocol/Feature.h>
24 #include <ripple/protocol/Indexes.h>
25 #include <ripple/protocol/STAccount.h>
26 #include <ripple/protocol/TER.h>
27 #include <ripple/protocol/TxFlags.h>
28 
29 namespace ripple {
30 
31 NotTEC
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 
82 TER
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
97  (flags & lsfDisallowIncomingCheck))
98  return tecNO_PERMISSION;
99 
100  // AMM can not cash the check
101  if (sleDst->isFieldPresent(sfAMMID))
102  return tecNO_PERMISSION;
103 
104  if ((flags & lsfRequireDestTag) && !ctx.tx.isFieldPresent(sfDestinationTag))
105  {
106  // The tag is basically account-specific information we don't
107  // understand, but we can require someone to fill it in.
108  JLOG(ctx.j.warn()) << "Malformed transaction: DestinationTag required.";
109  return tecDST_TAG_NEEDED;
110  }
111 
112  {
113  STAmount const sendMax{ctx.tx[sfSendMax]};
114  if (!sendMax.native())
115  {
116  // The currency may not be globally frozen
117  AccountID const& issuerId{sendMax.getIssuer()};
118  if (isGlobalFrozen(ctx.view, issuerId))
119  {
120  JLOG(ctx.j.warn()) << "Creating a check for frozen asset";
121  return tecFROZEN;
122  }
123  // If this account has a trustline for the currency, that
124  // trustline may not be frozen.
125  //
126  // Note that we DO allow create check for a currency that the
127  // account does not yet have a trustline to.
128  AccountID const srcId{ctx.tx.getAccountID(sfAccount)};
129  if (issuerId != srcId)
130  {
131  // Check if the issuer froze the line
132  auto const sleTrust = ctx.view.read(
133  keylet::line(srcId, issuerId, sendMax.getCurrency()));
134  if (sleTrust &&
135  sleTrust->isFlag(
136  (issuerId > srcId) ? lsfHighFreeze : lsfLowFreeze))
137  {
138  JLOG(ctx.j.warn())
139  << "Creating a check for frozen trustline.";
140  return tecFROZEN;
141  }
142  }
143  if (issuerId != dstId)
144  {
145  // Check if dst froze the line.
146  auto const sleTrust = ctx.view.read(
147  keylet::line(issuerId, dstId, sendMax.getCurrency()));
148  if (sleTrust &&
149  sleTrust->isFlag(
150  (dstId > issuerId) ? lsfHighFreeze : lsfLowFreeze))
151  {
152  JLOG(ctx.j.warn())
153  << "Creating a check for destination frozen trustline.";
154  return tecFROZEN;
155  }
156  }
157  }
158  }
159  if (hasExpired(ctx.view, ctx.tx[~sfExpiration]))
160  {
161  JLOG(ctx.j.warn()) << "Creating a check that has already expired.";
162  return tecEXPIRED;
163  }
164  return tesSUCCESS;
165 }
166 
167 TER
169 {
170  auto const sle = view().peek(keylet::account(account_));
171  if (!sle)
172  return tefINTERNAL;
173 
174  // A check counts against the reserve of the issuing account, but we
175  // check the starting balance because we want to allow dipping into the
176  // reserve to pay fees.
177  {
178  STAmount const reserve{
179  view().fees().accountReserve(sle->getFieldU32(sfOwnerCount) + 1)};
180 
181  if (mPriorBalance < reserve)
183  }
184 
185  // Note that we use the value from the sequence or ticket as the
186  // Check sequence. For more explanation see comments in SeqProxy.h.
187  std::uint32_t const seq = ctx_.tx.getSeqProxy().value();
188  Keylet const checkKeylet = keylet::check(account_, seq);
189  auto sleCheck = std::make_shared<SLE>(checkKeylet);
190 
191  sleCheck->setAccountID(sfAccount, account_);
192  AccountID const dstAccountId = ctx_.tx[sfDestination];
193  sleCheck->setAccountID(sfDestination, dstAccountId);
194  sleCheck->setFieldU32(sfSequence, seq);
195  sleCheck->setFieldAmount(sfSendMax, ctx_.tx[sfSendMax]);
196  if (auto const srcTag = ctx_.tx[~sfSourceTag])
197  sleCheck->setFieldU32(sfSourceTag, *srcTag);
198  if (auto const dstTag = ctx_.tx[~sfDestinationTag])
199  sleCheck->setFieldU32(sfDestinationTag, *dstTag);
200  if (auto const invoiceId = ctx_.tx[~sfInvoiceID])
201  sleCheck->setFieldH256(sfInvoiceID, *invoiceId);
202  if (auto const expiry = ctx_.tx[~sfExpiration])
203  sleCheck->setFieldU32(sfExpiration, *expiry);
204 
205  view().insert(sleCheck);
206 
207  auto viewJ = ctx_.app.journal("View");
208  // If it's not a self-send (and it shouldn't be), add Check to the
209  // destination's owner directory.
210  if (dstAccountId != account_)
211  {
212  auto const page = view().dirInsert(
213  keylet::ownerDir(dstAccountId),
214  checkKeylet,
215  describeOwnerDir(dstAccountId));
216 
217  JLOG(j_.trace()) << "Adding Check to destination directory "
218  << to_string(checkKeylet.key) << ": "
219  << (page ? "success" : "failure");
220 
221  if (!page)
222  return tecDIR_FULL;
223 
224  sleCheck->setFieldU64(sfDestinationNode, *page);
225  }
226 
227  {
228  auto const page = view().dirInsert(
230  checkKeylet,
232 
233  JLOG(j_.trace()) << "Adding Check to owner directory "
234  << to_string(checkKeylet.key) << ": "
235  << (page ? "success" : "failure");
236 
237  if (!page)
238  return tecDIR_FULL;
239 
240  sleCheck->setFieldU64(sfOwnerNode, *page);
241  }
242  // If we succeeded, the new entry counts against the creator's reserve.
243  adjustOwnerCount(view(), sle, 1, viewJ);
244  return tesSUCCESS;
245 }
246 
247 } // namespace ripple
ripple::badCurrency
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
Definition: UintTypes.cpp:129
ripple::keylet::ownerDir
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition: Indexes.cpp:312
ripple::sfOwnerCount
const SF_UINT32 sfOwnerCount
ripple::STObject::setAccountID
void setAccountID(SField const &field, AccountID const &)
Definition: STObject.cpp:689
ripple::tecFROZEN
@ tecFROZEN
Definition: TER.h:283
ripple::preflight2
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:133
ripple::sfSourceTag
const SF_UINT32 sfSourceTag
ripple::Keylet
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:38
ripple::tefINTERNAL
@ tefINTERNAL
Definition: TER.h:167
ripple::sfSendMax
const SF_AMOUNT sfSendMax
ripple::Rules::enabled
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition: Rules.cpp:94
ripple::PreclaimContext::view
ReadView const & view
Definition: Transactor.h:56
ripple::sfOwnerNode
const SF_UINT64 sfOwnerNode
ripple::temBAD_CURRENCY
@ temBAD_CURRENCY
Definition: TER.h:89
ripple::PreclaimContext::j
const beast::Journal j
Definition: Transactor.h:60
beast::Journal::trace
Stream trace() const
Severity stream access functions.
Definition: Journal.h:308
ripple::ApplyView::peek
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
ripple::sfDestination
const SF_ACCOUNT sfDestination
ripple::describeOwnerDir
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition: View.cpp:748
ripple::Transactor::j_
const beast::Journal j_
Definition: Transactor.h:89
ripple::isTesSuccess
bool isTesSuccess(TER x)
Definition: TER.h:636
ripple::sfSequence
const SF_UINT32 sfSequence
ripple::ReadView::fees
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
ripple::tecDST_TAG_NEEDED
@ tecDST_TAG_NEEDED
Definition: TER.h:289
beast::Journal::warn
Stream warn() const
Definition: Journal.h:326
ripple::hasExpired
bool hasExpired(ReadView const &view, std::optional< std::uint32_t > const &exp)
Determines whether the given expiration time has passed.
Definition: View.cpp:162
ripple::STTx::getSeqProxy
SeqProxy getSeqProxy() const
Definition: STTx.cpp:184
ripple::isLegalNet
bool isLegalNet(STAmount const &value)
Definition: STAmount.h:449
ripple::PreflightContext::j
const beast::Journal j
Definition: Transactor.h:38
ripple::preflight1
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
Definition: Transactor.cpp:81
ripple::ApplyContext::app
Application & app
Definition: ApplyContext.h:47
ripple::sfExpiration
const SF_UINT32 sfExpiration
ripple::Keylet::key
uint256 key
Definition: Keylet.h:40
ripple::base_uint< 160, detail::AccountIDTag >
ripple::temINVALID_FLAG
@ temINVALID_FLAG
Definition: TER.h:110
ripple::isGlobalFrozen
bool isGlobalFrozen(ReadView const &view, AccountID const &issuer)
Definition: View.cpp:171
ripple::CreateCheck::preclaim
static TER preclaim(PreclaimContext const &ctx)
Definition: CreateCheck.cpp:83
ripple::adjustOwnerCount
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:730
ripple::keylet::account
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:142
ripple::featureDisallowIncoming
const uint256 featureDisallowIncoming
ripple::STObject::getAccountID
AccountID getAccountID(SField const &field) const
Definition: STObject.cpp:589
ripple::TERSubset
Definition: TER.h:379
ripple::STAmount
Definition: STAmount.h:46
ripple::sfDestinationNode
const SF_UINT64 sfDestinationNode
ripple::STObject::getFlags
std::uint32_t getFlags() const
Definition: STObject.cpp:481
ripple::temBAD_AMOUNT
@ temBAD_AMOUNT
Definition: TER.h:88
std::uint32_t
ripple::keylet::line
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:202
ripple::featureChecks
const uint256 featureChecks
ripple::temREDUNDANT
@ temREDUNDANT
Definition: TER.h:111
ripple::sfAMMID
const SF_UINT256 sfAMMID
ripple::ReadView::read
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
ripple::PreclaimContext::tx
STTx const & tx
Definition: Transactor.h:58
ripple::lsfRequireDestTag
@ lsfRequireDestTag
Definition: LedgerFormats.h:255
ripple::tecDIR_FULL
@ tecDIR_FULL
Definition: TER.h:267
ripple::PreclaimContext
State information when determining if a tx is likely to claim a fee.
Definition: Transactor.h:52
ripple::lsfHighFreeze
@ lsfHighFreeze
Definition: LedgerFormats.h:293
ripple::SeqProxy::value
constexpr std::uint32_t value() const
Definition: SeqProxy.h:82
ripple::ApplyView::insert
virtual void insert(std::shared_ptr< SLE > const &sle)=0
Insert a new state SLE.
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Application::journal
virtual beast::Journal journal(std::string const &name)=0
ripple::Transactor::view
ApplyView & view()
Definition: Transactor.h:107
ripple::tecEXPIRED
@ tecEXPIRED
Definition: TER.h:294
ripple::temDISABLED
@ temDISABLED
Definition: TER.h:113
ripple::Fees::accountReserve
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
Definition: protocol/Fees.h:49
ripple::sfInvoiceID
const SF_UINT256 sfInvoiceID
ripple::ReadView::rules
virtual Rules const & rules() const =0
Returns the tx processing rules.
ripple::STObject::isFieldPresent
bool isFieldPresent(SField const &field) const
Definition: STObject.cpp:428
ripple::sfDestinationTag
const SF_UINT32 sfDestinationTag
ripple::Transactor::mPriorBalance
XRPAmount mPriorBalance
Definition: Transactor.h:92
ripple::tecNO_PERMISSION
@ tecNO_PERMISSION
Definition: TER.h:285
ripple::lsfDisallowIncomingCheck
@ lsfDisallowIncomingCheck
Definition: LedgerFormats.h:271
ripple::CreateCheck::doApply
TER doApply() override
Definition: CreateCheck.cpp:168
ripple::tecINSUFFICIENT_RESERVE
@ tecINSUFFICIENT_RESERVE
Definition: TER.h:287
ripple::Transactor::ctx_
ApplyContext & ctx_
Definition: Transactor.h:88
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::sfAccount
const SF_ACCOUNT sfAccount
ripple::lsfLowFreeze
@ lsfLowFreeze
Definition: LedgerFormats.h:292
ripple::PreflightContext::tx
STTx const & tx
Definition: Transactor.h:35
ripple::PreflightContext
State information when preflighting a tx.
Definition: Transactor.h:31
ripple::temBAD_EXPIRATION
@ temBAD_EXPIRATION
Definition: TER.h:90
ripple::ApplyView::dirInsert
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:306
ripple::keylet::check
Keylet check(AccountID const &id, std::uint32_t seq) noexcept
A Check.
Definition: Indexes.cpp:290
ripple::CreateCheck::preflight
static NotTEC preflight(PreflightContext const &ctx)
Definition: CreateCheck.cpp:32
ripple::PreflightContext::rules
const Rules rules
Definition: Transactor.h:36
ripple::tfUniversalMask
constexpr std::uint32_t tfUniversalMask
Definition: TxFlags.h:60
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:235
ripple::Transactor::account_
const AccountID account_
Definition: Transactor.h:91
ripple::STObject::getFieldAmount
STAmount const & getFieldAmount(SField const &field) const
Definition: STObject.cpp:603
ripple::ApplyContext::tx
STTx const & tx
Definition: ApplyContext.h:48
ripple::tecNO_DST
@ tecNO_DST
Definition: TER.h:270
ripple::NotTEC
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:567