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  AccountID const dstAccountId{ctx_.tx[sfDestination]};
187  std::uint32_t const seq{ctx_.tx.getSequence()};
188  auto sleCheck = std::make_shared<SLE>(keylet::check(account_, seq));
189 
190  sleCheck->setAccountID(sfAccount, account_);
191  sleCheck->setAccountID(sfDestination, dstAccountId);
192  sleCheck->setFieldU32(sfSequence, seq);
193  sleCheck->setFieldAmount(sfSendMax, ctx_.tx[sfSendMax]);
194  if (auto const srcTag = ctx_.tx[~sfSourceTag])
195  sleCheck->setFieldU32(sfSourceTag, *srcTag);
196  if (auto const dstTag = ctx_.tx[~sfDestinationTag])
197  sleCheck->setFieldU32(sfDestinationTag, *dstTag);
198  if (auto const invoiceId = ctx_.tx[~sfInvoiceID])
199  sleCheck->setFieldH256(sfInvoiceID, *invoiceId);
200  if (auto const expiry = ctx_.tx[~sfExpiration])
201  sleCheck->setFieldU32(sfExpiration, *expiry);
202 
203  view().insert(sleCheck);
204 
205  auto viewJ = ctx_.app.journal("View");
206  // If it's not a self-send (and it shouldn't be), add Check to the
207  // destination's owner directory.
208  if (dstAccountId != account_)
209  {
210  auto const page = dirAdd(
211  view(),
212  keylet::ownerDir(dstAccountId),
213  sleCheck->key(),
214  false,
215  describeOwnerDir(dstAccountId),
216  viewJ);
217 
218  JLOG(j_.trace()) << "Adding Check to destination directory "
219  << to_string(sleCheck->key()) << ": "
220  << (page ? "success" : "failure");
221 
222  if (!page)
223  return tecDIR_FULL;
224 
225  sleCheck->setFieldU64(sfDestinationNode, *page);
226  }
227 
228  {
229  auto const page = dirAdd(
230  view(),
231  keylet::ownerDir(account_),
232  sleCheck->key(),
233  false,
234  describeOwnerDir(account_),
235  viewJ);
236 
237  JLOG(j_.trace()) << "Adding Check to owner directory "
238  << to_string(sleCheck->key()) << ": "
239  << (page ? "success" : "failure");
240 
241  if (!page)
242  return tecDIR_FULL;
243 
244  sleCheck->setFieldU64(sfOwnerNode, *page);
245  }
246  // If we succeeded, the new entry counts against the creator's reserve.
247  adjustOwnerCount(view(), sle, 1, viewJ);
248  return tesSUCCESS;
249 }
250 
251 } // 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:135
ripple::tecFROZEN
@ tecFROZEN
Definition: TER.h:261
ripple::preflight2
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:90
ripple::tefINTERNAL
@ tefINTERNAL
Definition: TER.h:149
ripple::PreclaimContext::view
ReadView const & view
Definition: Transactor.h:57
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::describeOwnerDir
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition: View.cpp:713
ripple::isTesSuccess
bool isTesSuccess(TER x)
Definition: TER.h:576
ripple::sfSequence
const SF_U32 sfSequence(access, STI_UINT32, 4, "Sequence")
Definition: SField.h:355
ripple::sfAccount
const SF_Account sfAccount(access, STI_ACCOUNT, 1, "Account")
Definition: SField.h:476
ripple::tecDST_TAG_NEEDED
@ tecDST_TAG_NEEDED
Definition: TER.h:267
beast::Journal::warn
Stream warn() const
Definition: Journal.h:327
ripple::isLegalNet
bool isLegalNet(STAmount const &value)
Definition: STAmount.h:385
ripple::sfOwnerNode
const SF_U64 sfOwnerNode(access, STI_UINT64, 4, "OwnerNode")
Definition: SField.h:397
ripple::ReadView::parentCloseTime
NetClock::time_point parentCloseTime() const
Returns the close time of the previous ledger.
Definition: ReadView.h:249
ripple::sfOwnerCount
const SF_U32 sfOwnerCount(access, STI_UINT32, 13, "OwnerCount")
Definition: SField.h:364
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::sfDestinationTag
const SF_U32 sfDestinationTag(access, STI_UINT32, 14, "DestinationTag")
Definition: SField.h:365
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:120
ripple::sfSendMax
const SF_Amount sfSendMax(access, STI_AMOUNT, 9, "SendMax")
Definition: SField.h:447
ripple::STObject::getAccountID
AccountID getAccountID(SField const &field) const
Definition: STObject.cpp:562
ripple::TERSubset
Definition: TER.h:322
ripple::sfDestinationNode
const SF_U64 sfDestinationNode(access, STI_UINT64, 9, "DestinationNode")
Definition: SField.h:402
ripple::STAmount
Definition: STAmount.h:42
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:172
ripple::featureChecks
const uint256 featureChecks
Definition: Feature.cpp:168
ripple::sfSourceTag
const SF_U32 sfSourceTag(access, STI_UINT32, 3, "SourceTag")
Definition: SField.h:354
ripple::temREDUNDANT
@ temREDUNDANT
Definition: TER.h:107
ripple::sfExpiration
const SF_U32 sfExpiration(access, STI_UINT32, 10, "Expiration")
Definition: SField.h:361
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:102
ripple::tecDIR_FULL
@ tecDIR_FULL
Definition: TER.h:245
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:126
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:272
ripple::temDISABLED
@ temDISABLED
Definition: TER.h:109
ripple::STObject::isFieldPresent
bool isFieldPresent(SField const &field) const
Definition: STObject.cpp:401
ripple::tecINSUFFICIENT_RESERVE
@ tecINSUFFICIENT_RESERVE
Definition: TER.h:265
ripple::sfDestination
const SF_Account sfDestination(access, STI_ACCOUNT, 3, "Destination")
Definition: SField.h:478
ripple::lsfLowFreeze
@ lsfLowFreeze
Definition: LedgerFormats.h:125
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:213
ripple::STObject::getFieldAmount
STAmount const & getFieldAmount(SField const &field) const
Definition: STObject.cpp:576
ripple::tecNO_DST
@ tecNO_DST
Definition: TER.h:248
ripple::sfInvoiceID
const SF_U256 sfInvoiceID(access, STI_HASH256, 17, "InvoiceID")
Definition: SField.h:428
ripple::NotTEC
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:507