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/tx/impl/CreateCheck.h>
21 #include <ripple/app/ledger/Ledger.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 {view().fees().accountReserve (
180  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 (view(), keylet::ownerDir (dstAccountId),
212  sleCheck->key(), false, describeOwnerDir (dstAccountId), viewJ);
213 
214  JLOG(j_.trace())
215  << "Adding Check to destination directory "
216  << to_string (sleCheck->key())
217  << ": " << (page ? "success" : "failure");
218 
219  if (! page)
220  return tecDIR_FULL;
221 
222  sleCheck->setFieldU64 (sfDestinationNode, *page);
223  }
224 
225  {
226  auto const page = dirAdd (view(), keylet::ownerDir (account_),
227  sleCheck->key(), false, describeOwnerDir (account_), viewJ);
228 
229  JLOG(j_.trace())
230  << "Adding Check to owner directory "
231  << to_string (sleCheck->key())
232  << ": " << (page ? "success" : "failure");
233 
234  if (! page)
235  return tecDIR_FULL;
236 
237  sleCheck->setFieldU64 (sfOwnerNode, *page);
238  }
239  // If we succeeded, the new entry counts against the creator's reserve.
240  adjustOwnerCount (view(), sle, 1, viewJ);
241  return tesSUCCESS;
242 }
243 
244 } // 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:123
ripple::tecFROZEN
@ tecFROZEN
Definition: TER.h:268
ripple::preflight2
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:90
ripple::tefINTERNAL
@ tefINTERNAL
Definition: TER.h:153
ripple::PreclaimContext::view
ReadView const & view
Definition: Transactor.h:53
ripple::temBAD_CURRENCY
@ temBAD_CURRENCY
Definition: TER.h:88
ripple::PreclaimContext::j
const beast::Journal j
Definition: Transactor.h:57
beast::Journal::trace
Stream trace() const
Severity stream access functions.
Definition: Journal.h:287
ripple::describeOwnerDir
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition: View.cpp:700
ripple::isTesSuccess
bool isTesSuccess(TER x)
Definition: TER.h:501
ripple::sfSequence
const SF_U32 sfSequence(access, STI_UINT32, 4, "Sequence")
Definition: SField.h:340
ripple::sfAccount
const SF_Account sfAccount(access, STI_ACCOUNT, 1, "Account")
Definition: SField.h:460
ripple::tecDST_TAG_NEEDED
@ tecDST_TAG_NEEDED
Definition: TER.h:274
beast::Journal::warn
Stream warn() const
Definition: Journal.h:302
ripple::isLegalNet
bool isLegalNet(STAmount const &value)
Definition: STAmount.h:325
ripple::sfOwnerNode
const SF_U64 sfOwnerNode(access, STI_UINT64, 4, "OwnerNode")
Definition: SField.h:382
ripple::ReadView::parentCloseTime
NetClock::time_point parentCloseTime() const
Returns the close time of the previous ledger.
Definition: ReadView.h:251
ripple::sfOwnerCount
const SF_U32 sfOwnerCount(access, STI_UINT32, 13, "OwnerCount")
Definition: SField.h:349
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:86
ripple::sfDestinationTag
const SF_U32 sfDestinationTag(access, STI_UINT32, 14, "DestinationTag")
Definition: SField.h:350
ripple::base_uint< 160, detail::AccountIDTag >
ripple::getCheckIndex
uint256 getCheckIndex(AccountID const &account, std::uint32_t uSequence)
Definition: Indexes.cpp:186
ripple::temINVALID_FLAG
@ temINVALID_FLAG
Definition: TER.h:109
ripple::isGlobalFrozen
bool isGlobalFrozen(ReadView const &view, AccountID const &issuer)
Definition: View.cpp:56
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:629
ripple::keylet::line
static const line_t line
Definition: Indexes.h:176
ripple::keylet::account
static const account_t account
Definition: Indexes.h:116
ripple::sfSendMax
const SF_Amount sfSendMax(access, STI_AMOUNT, 9, "SendMax")
Definition: SField.h:431
ripple::STObject::getAccountID
AccountID getAccountID(SField const &field) const
Definition: STObject.cpp:537
ripple::TERSubset
Definition: TER.h:311
ripple::sfDestinationNode
const SF_U64 sfDestinationNode(access, STI_UINT64, 9, "DestinationNode")
Definition: SField.h:387
ripple::STAmount
Definition: STAmount.h:42
ripple::STObject::getFlags
std::uint32_t getFlags() const
Definition: STObject.cpp:439
ripple::temBAD_AMOUNT
@ temBAD_AMOUNT
Definition: TER.h:87
std::uint32_t
ripple::Rules::enabled
bool enabled(uint256 const &id) const
Returns true if a feature is enabled.
Definition: ReadView.cpp:107
ripple::featureChecks
const uint256 featureChecks
Definition: Feature.cpp:165
ripple::sfSourceTag
const SF_U32 sfSourceTag(access, STI_UINT32, 3, "SourceTag")
Definition: SField.h:339
ripple::temREDUNDANT
@ temREDUNDANT
Definition: TER.h:110
ripple::sfExpiration
const SF_U32 sfExpiration(access, STI_UINT32, 10, "Expiration")
Definition: SField.h:346
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:55
ripple::lsfRequireDestTag
@ lsfRequireDestTag
Definition: LedgerFormats.h:133
ripple::tecDIR_FULL
@ tecDIR_FULL
Definition: TER.h:252
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:709
ripple::PreclaimContext
State information when determining if a tx is likely to claim a fee.
Definition: Transactor.h:49
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:279
ripple::temDISABLED
@ temDISABLED
Definition: TER.h:112
ripple::STObject::isFieldPresent
bool isFieldPresent(SField const &field) const
Definition: STObject.cpp:392
ripple::tecINSUFFICIENT_RESERVE
@ tecINSUFFICIENT_RESERVE
Definition: TER.h:272
ripple::sfDestination
const SF_Account sfDestination(access, STI_ACCOUNT, 3, "Destination")
Definition: SField.h:462
ripple::lsfLowFreeze
@ lsfLowFreeze
Definition: LedgerFormats.h:153
ripple::NetClock::duration
std::chrono::duration< rep, period > duration
Definition: chrono.h:52
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:89
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:53
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:219
ripple::STObject::getFieldAmount
STAmount const & getFieldAmount(SField const &field) const
Definition: STObject.cpp:549
ripple::tecNO_DST
@ tecNO_DST
Definition: TER.h:255
ripple::sfInvoiceID
const SF_U256 sfInvoiceID(access, STI_HASH256, 17, "InvoiceID")
Definition: SField.h:413
ripple::NotTEC
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:461