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  if ((sleDst->getFlags() & lsfRequireDestTag) &&
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  {
150  using duration = NetClock::duration;
151  using timepoint = NetClock::time_point;
152  auto const optExpiry = ctx.tx[~sfExpiration];
153 
154  // Expiration is defined in terms of the close time of the parent
155  // ledger, because we definitively know the time that it closed but
156  // we do not know the closing time of the ledger that is under
157  // construction.
158  if (optExpiry &&
159  (ctx.view.parentCloseTime() >= timepoint{duration{*optExpiry}}))
160  {
161  JLOG(ctx.j.warn()) << "Creating a check that has already expired.";
162  return tecEXPIRED;
163  }
164  }
165  return tesSUCCESS;
166 }
167 
168 TER
169 CreateCheck::doApply()
170 {
171  auto const sle = view().peek(keylet::account(account_));
172  if (!sle)
173  return tefINTERNAL;
174 
175  // A check counts against the reserve of the issuing account, but we
176  // check the starting balance because we want to allow dipping into the
177  // reserve to pay fees.
178  {
179  STAmount const reserve{
180  view().fees().accountReserve(sle->getFieldU32(sfOwnerCount) + 1)};
181 
182  if (mPriorBalance < reserve)
184  }
185 
186  // Note that we we use the value from the sequence or ticket as the
187  // Check sequence. For more explanation see comments in SeqProxy.h.
188  std::uint32_t const seq = ctx_.tx.getSeqProxy().value();
189  auto sleCheck = std::make_shared<SLE>(keylet::check(account_, seq));
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 = dirAdd(
213  view(),
214  keylet::ownerDir(dstAccountId),
215  sleCheck->key(),
216  false,
217  describeOwnerDir(dstAccountId),
218  viewJ);
219 
220  JLOG(j_.trace()) << "Adding Check to destination directory "
221  << to_string(sleCheck->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 = dirAdd(
232  view(),
233  keylet::ownerDir(account_),
234  sleCheck->key(),
235  false,
236  describeOwnerDir(account_),
237  viewJ);
238 
239  JLOG(j_.trace()) << "Adding Check to owner directory "
240  << to_string(sleCheck->key()) << ": "
241  << (page ? "success" : "failure");
242 
243  if (!page)
244  return tecDIR_FULL;
245 
246  sleCheck->setFieldU64(sfOwnerNode, *page);
247  }
248  // If we succeeded, the new entry counts against the creator's reserve.
249  adjustOwnerCount(view(), sle, 1, viewJ);
250  return tesSUCCESS;
251 }
252 
253 } // 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:138
ripple::sfOwnerCount
const SF_UINT32 sfOwnerCount
ripple::tecFROZEN
@ tecFROZEN
Definition: TER.h:264
ripple::preflight2
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:108
ripple::sfSourceTag
const SF_UINT32 sfSourceTag
ripple::tefINTERNAL
@ tefINTERNAL
Definition: TER.h:150
ripple::sfSendMax
const SF_AMOUNT sfSendMax
ripple::PreclaimContext::view
ReadView const & view
Definition: Transactor.h:57
ripple::sfOwnerNode
const SF_UINT64 sfOwnerNode
ripple::temBAD_CURRENCY
@ temBAD_CURRENCY
Definition: TER.h:85
ripple::PreclaimContext::j
const beast::Journal j
Definition: Transactor.h:61
beast::Journal::trace
Stream trace() const
Severity stream access functions.
Definition: Journal.h:309
ripple::sfDestination
const SF_ACCOUNT sfDestination
ripple::describeOwnerDir
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition: View.cpp:713
ripple::isTesSuccess
bool isTesSuccess(TER x)
Definition: TER.h:580
ripple::sfSequence
const SF_UINT32 sfSequence
ripple::tecDST_TAG_NEEDED
@ tecDST_TAG_NEEDED
Definition: TER.h:270
beast::Journal::warn
Stream warn() const
Definition: Journal.h:327
ripple::isLegalNet
bool isLegalNet(STAmount const &value)
Definition: STAmount.h:385
ripple::ReadView::parentCloseTime
NetClock::time_point parentCloseTime() const
Returns the close time of the previous ledger.
Definition: ReadView.h:253
ripple::PreflightContext::j
const beast::Journal j
Definition: Transactor.h:39
ripple::preflight1
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
Definition: Transactor.cpp:56
ripple::sfExpiration
const SF_UINT32 sfExpiration
ripple::base_uint< 160, detail::AccountIDTag >
ripple::temINVALID_FLAG
@ temINVALID_FLAG
Definition: TER.h:106
ripple::isGlobalFrozen
bool isGlobalFrozen(ReadView const &view, AccountID const &issuer)
Definition: View.cpp:57
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:642
ripple::keylet::account
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:134
ripple::STObject::getAccountID
AccountID getAccountID(SField const &field) const
Definition: STObject.cpp:562
ripple::TERSubset
Definition: TER.h:326
ripple::STAmount
Definition: STAmount.h:42
ripple::sfDestinationNode
const SF_UINT64 sfDestinationNode
ripple::STObject::getFlags
std::uint32_t getFlags() const
Definition: STObject.cpp:454
ripple::temBAD_AMOUNT
@ temBAD_AMOUNT
Definition: TER.h:84
std::uint32_t
ripple::Rules::enabled
bool enabled(uint256 const &id) const
Returns true if a feature is enabled.
Definition: ReadView.cpp:103
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:194
ripple::featureChecks
const uint256 featureChecks
Definition: Feature.cpp:170
ripple::temREDUNDANT
@ temREDUNDANT
Definition: TER.h:107
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:59
ripple::lsfRequireDestTag
@ lsfRequireDestTag
Definition: LedgerFormats.h:104
ripple::tecDIR_FULL
@ tecDIR_FULL
Definition: TER.h:248
ripple::dirAdd
boost::optional< std::uint64_t > dirAdd(ApplyView &view, Keylet const &dir, uint256 const &uLedgerIndex, bool strictOrder, std::function< void(SLE::ref)> fDescriber, beast::Journal j)
Definition: View.cpp:721
ripple::PreclaimContext
State information when determining if a tx is likely to claim a fee.
Definition: Transactor.h:53
ripple::lsfHighFreeze
@ lsfHighFreeze
Definition: LedgerFormats.h:128
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::tecEXPIRED
@ tecEXPIRED
Definition: TER.h:275
ripple::temDISABLED
@ temDISABLED
Definition: TER.h:109
ripple::STObject::isFieldPresent
bool isFieldPresent(SField const &field) const
Definition: STObject.cpp:401
ripple::sfInvoiceID
const SF_HASH256 sfInvoiceID
ripple::sfDestinationTag
const SF_UINT32 sfDestinationTag
ripple::tecINSUFFICIENT_RESERVE
@ tecINSUFFICIENT_RESERVE
Definition: TER.h:268
ripple::sfAccount
const SF_ACCOUNT sfAccount
ripple::lsfLowFreeze
@ lsfLowFreeze
Definition: LedgerFormats.h:127
ripple::NetClock::duration
std::chrono::duration< rep, period > duration
Definition: chrono.h:53
ripple::tfUniversalMask
const std::uint32_t tfUniversalMask
Definition: TxFlags.h:50
ripple::PreflightContext::tx
STTx const & tx
Definition: Transactor.h:36
ripple::PreflightContext
State information when preflighting a tx.
Definition: Transactor.h:32
ripple::temBAD_EXPIRATION
@ temBAD_EXPIRATION
Definition: TER.h:86
ripple::CreateCheck::preflight
static NotTEC preflight(PreflightContext const &ctx)
Definition: CreateCheck.cpp:32
ripple::PreflightContext::rules
const Rules rules
Definition: Transactor.h:37
ripple::NetClock::time_point
std::chrono::time_point< NetClock > time_point
Definition: chrono.h:54
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:216
ripple::STObject::getFieldAmount
STAmount const & getFieldAmount(SField const &field) const
Definition: STObject.cpp:576
ripple::tecNO_DST
@ tecNO_DST
Definition: TER.h:251
ripple::NotTEC
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:511