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 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  Keylet const checkKeylet = keylet::check(account_, seq);
190  auto sleCheck = std::make_shared<SLE>(checkKeylet);
191 
192  sleCheck->setAccountID(sfAccount, account_);
193  AccountID const dstAccountId = ctx_.tx[sfDestination];
194  sleCheck->setAccountID(sfDestination, dstAccountId);
195  sleCheck->setFieldU32(sfSequence, seq);
196  sleCheck->setFieldAmount(sfSendMax, ctx_.tx[sfSendMax]);
197  if (auto const srcTag = ctx_.tx[~sfSourceTag])
198  sleCheck->setFieldU32(sfSourceTag, *srcTag);
199  if (auto const dstTag = ctx_.tx[~sfDestinationTag])
200  sleCheck->setFieldU32(sfDestinationTag, *dstTag);
201  if (auto const invoiceId = ctx_.tx[~sfInvoiceID])
202  sleCheck->setFieldH256(sfInvoiceID, *invoiceId);
203  if (auto const expiry = ctx_.tx[~sfExpiration])
204  sleCheck->setFieldU32(sfExpiration, *expiry);
205 
206  view().insert(sleCheck);
207 
208  auto viewJ = ctx_.app.journal("View");
209  // If it's not a self-send (and it shouldn't be), add Check to the
210  // destination's owner directory.
211  if (dstAccountId != account_)
212  {
213  auto const page = view().dirInsert(
214  keylet::ownerDir(dstAccountId),
215  checkKeylet,
216  describeOwnerDir(dstAccountId));
217 
218  JLOG(j_.trace()) << "Adding Check to destination directory "
219  << to_string(checkKeylet.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 = view().dirInsert(
230  keylet::ownerDir(account_),
231  checkKeylet,
232  describeOwnerDir(account_));
233 
234  JLOG(j_.trace()) << "Adding Check to owner directory "
235  << to_string(checkKeylet.key) << ": "
236  << (page ? "success" : "failure");
237 
238  if (!page)
239  return tecDIR_FULL;
240 
241  sleCheck->setFieldU64(sfOwnerNode, *page);
242  }
243  // If we succeeded, the new entry counts against the creator's reserve.
244  adjustOwnerCount(view(), sle, 1, viewJ);
245  return tesSUCCESS;
246 }
247 
248 } // 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::sfOwnerCount
const SF_UINT32 sfOwnerCount
ripple::tecFROZEN
@ tecFROZEN
Definition: TER.h:265
ripple::preflight2
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:108
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:151
ripple::sfSendMax
const SF_AMOUNT sfSendMax
ripple::PreclaimContext::view
ReadView const & view
Definition: Transactor.h:56
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:60
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:731
ripple::isTesSuccess
bool isTesSuccess(TER x)
Definition: TER.h:584
ripple::sfSequence
const SF_UINT32 sfSequence
ripple::tecDST_TAG_NEEDED
@ tecDST_TAG_NEEDED
Definition: TER.h:271
beast::Journal::warn
Stream warn() const
Definition: Journal.h:327
ripple::isLegalNet
bool isLegalNet(STAmount const &value)
Definition: STAmount.h:386
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:38
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::Keylet::key
uint256 key
Definition: Keylet.h:40
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:181
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:713
ripple::keylet::account
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:130
ripple::STObject::getAccountID
AccountID getAccountID(SField const &field) const
Definition: STObject.cpp:562
ripple::TERSubset
Definition: TER.h:327
ripple::STAmount
Definition: STAmount.h:43
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:102
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:190
ripple::featureChecks
const uint256 featureChecks
Definition: Feature.cpp:173
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:58
ripple::lsfRequireDestTag
@ lsfRequireDestTag
Definition: LedgerFormats.h:212
ripple::tecDIR_FULL
@ tecDIR_FULL
Definition: TER.h:249
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:236
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:276
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:269
ripple::sfAccount
const SF_ACCOUNT sfAccount
ripple::lsfLowFreeze
@ lsfLowFreeze
Definition: LedgerFormats.h:235
ripple::NetClock::duration
std::chrono::duration< rep, period > duration
Definition: chrono.h:55
ripple::tfUniversalMask
const std::uint32_t tfUniversalMask
Definition: TxFlags.h:50
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:86
ripple::CreateCheck::preflight
static NotTEC preflight(PreflightContext const &ctx)
Definition: CreateCheck.cpp:32
ripple::PreflightContext::rules
const Rules rules
Definition: Transactor.h:36
ripple::NetClock::time_point
std::chrono::time_point< NetClock > time_point
Definition: chrono.h:56
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:217
ripple::STObject::getFieldAmount
STAmount const & getFieldAmount(SField const &field) const
Definition: STObject.cpp:576
ripple::tecNO_DST
@ tecNO_DST
Definition: TER.h:252
ripple::NotTEC
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:515