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 =
189  std::make_shared<SLE>(ltCHECK, getCheckIndex(account_, seq));
190 
191  sleCheck->setAccountID(sfAccount, account_);
192  sleCheck->setAccountID(sfDestination, dstAccountId);
193  sleCheck->setFieldU32(sfSequence, seq);
194  sleCheck->setFieldAmount(sfSendMax, ctx_.tx[sfSendMax]);
195  if (auto const srcTag = ctx_.tx[~sfSourceTag])
196  sleCheck->setFieldU32(sfSourceTag, *srcTag);
197  if (auto const dstTag = ctx_.tx[~sfDestinationTag])
198  sleCheck->setFieldU32(sfDestinationTag, *dstTag);
199  if (auto const invoiceId = ctx_.tx[~sfInvoiceID])
200  sleCheck->setFieldH256(sfInvoiceID, *invoiceId);
201  if (auto const expiry = ctx_.tx[~sfExpiration])
202  sleCheck->setFieldU32(sfExpiration, *expiry);
203 
204  view().insert(sleCheck);
205 
206  auto viewJ = ctx_.app.journal("View");
207  // If it's not a self-send (and it shouldn't be), add Check to the
208  // destination's owner directory.
209  if (dstAccountId != account_)
210  {
211  auto const page = dirAdd(
212  view(),
213  keylet::ownerDir(dstAccountId),
214  sleCheck->key(),
215  false,
216  describeOwnerDir(dstAccountId),
217  viewJ);
218 
219  JLOG(j_.trace()) << "Adding Check to destination directory "
220  << to_string(sleCheck->key()) << ": "
221  << (page ? "success" : "failure");
222 
223  if (!page)
224  return tecDIR_FULL;
225 
226  sleCheck->setFieldU64(sfDestinationNode, *page);
227  }
228 
229  {
230  auto const page = dirAdd(
231  view(),
232  keylet::ownerDir(account_),
233  sleCheck->key(),
234  false,
235  describeOwnerDir(account_),
236  viewJ);
237 
238  JLOG(j_.trace()) << "Adding Check to owner directory "
239  << to_string(sleCheck->key()) << ": "
240  << (page ? "success" : "failure");
241 
242  if (!page)
243  return tecDIR_FULL;
244 
245  sleCheck->setFieldU64(sfOwnerNode, *page);
246  }
247  // If we succeeded, the new entry counts against the creator's reserve.
248  adjustOwnerCount(view(), sle, 1, viewJ);
249  return tesSUCCESS;
250 }
251 
252 } // 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:126
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:474
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::ltCHECK
@ ltCHECK
Definition: LedgerFormats.h:85
ripple::sfDestinationTag
const SF_U32 sfDestinationTag(access, STI_UINT32, 14, "DestinationTag")
Definition: SField.h:365
ripple::base_uint< 160, detail::AccountIDTag >
ripple::getCheckIndex
uint256 getCheckIndex(AccountID const &account, std::uint32_t uSequence)
Definition: Indexes.cpp:168
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::line
static const line_t line
Definition: Indexes.h:190
ripple::keylet::account
static const account_t account
Definition: Indexes.h:120
ripple::sfSendMax
const SF_Amount sfSendMax(access, STI_AMOUNT, 9, "SendMax")
Definition: SField.h:445
ripple::STObject::getAccountID
AccountID getAccountID(SField const &field) const
Definition: STObject.cpp:573
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:465
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::featureChecks
const uint256 featureChecks
Definition: Feature.cpp:167
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:130
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:154
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:412
ripple::tecINSUFFICIENT_RESERVE
@ tecINSUFFICIENT_RESERVE
Definition: TER.h:265
ripple::sfDestination
const SF_Account sfDestination(access, STI_ACCOUNT, 3, "Destination")
Definition: SField.h:476
ripple::lsfLowFreeze
@ lsfLowFreeze
Definition: LedgerFormats.h:153
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:587
ripple::tecNO_DST
@ tecNO_DST
Definition: TER.h:248
ripple::sfInvoiceID
const SF_U256 sfInvoiceID(access, STI_HASH256, 17, "InvoiceID")
Definition: SField.h:427
ripple::NotTEC
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:507