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  auto const flags = sleDst->getFlags();
94 
95  // Check if the destination has disallowed incoming checks
97  (flags & lsfDisallowIncomingCheck))
98  return tecNO_PERMISSION;
99 
100  if ((flags & lsfRequireDestTag) && !ctx.tx.isFieldPresent(sfDestinationTag))
101  {
102  // The tag is basically account-specific information we don't
103  // understand, but we can require someone to fill it in.
104  JLOG(ctx.j.warn()) << "Malformed transaction: DestinationTag required.";
105  return tecDST_TAG_NEEDED;
106  }
107 
108  {
109  STAmount const sendMax{ctx.tx[sfSendMax]};
110  if (!sendMax.native())
111  {
112  // The currency may not be globally frozen
113  AccountID const& issuerId{sendMax.getIssuer()};
114  if (isGlobalFrozen(ctx.view, issuerId))
115  {
116  JLOG(ctx.j.warn()) << "Creating a check for frozen asset";
117  return tecFROZEN;
118  }
119  // If this account has a trustline for the currency, that
120  // trustline may not be frozen.
121  //
122  // Note that we DO allow create check for a currency that the
123  // account does not yet have a trustline to.
124  AccountID const srcId{ctx.tx.getAccountID(sfAccount)};
125  if (issuerId != srcId)
126  {
127  // Check if the issuer froze the line
128  auto const sleTrust = ctx.view.read(
129  keylet::line(srcId, issuerId, sendMax.getCurrency()));
130  if (sleTrust &&
131  sleTrust->isFlag(
132  (issuerId > srcId) ? lsfHighFreeze : lsfLowFreeze))
133  {
134  JLOG(ctx.j.warn())
135  << "Creating a check for frozen trustline.";
136  return tecFROZEN;
137  }
138  }
139  if (issuerId != dstId)
140  {
141  // Check if dst froze the line.
142  auto const sleTrust = ctx.view.read(
143  keylet::line(issuerId, dstId, sendMax.getCurrency()));
144  if (sleTrust &&
145  sleTrust->isFlag(
146  (dstId > issuerId) ? lsfHighFreeze : lsfLowFreeze))
147  {
148  JLOG(ctx.j.warn())
149  << "Creating a check for destination frozen trustline.";
150  return tecFROZEN;
151  }
152  }
153  }
154  }
155  if (hasExpired(ctx.view, ctx.tx[~sfExpiration]))
156  {
157  JLOG(ctx.j.warn()) << "Creating a check that has already expired.";
158  return tecEXPIRED;
159  }
160  return tesSUCCESS;
161 }
162 
163 TER
165 {
166  auto const sle = view().peek(keylet::account(account_));
167  if (!sle)
168  return tefINTERNAL;
169 
170  // A check counts against the reserve of the issuing account, but we
171  // check the starting balance because we want to allow dipping into the
172  // reserve to pay fees.
173  {
174  STAmount const reserve{
175  view().fees().accountReserve(sle->getFieldU32(sfOwnerCount) + 1)};
176 
177  if (mPriorBalance < reserve)
179  }
180 
181  // Note that we use the value from the sequence or ticket as the
182  // Check sequence. For more explanation see comments in SeqProxy.h.
183  std::uint32_t const seq = ctx_.tx.getSeqProxy().value();
184  Keylet const checkKeylet = keylet::check(account_, seq);
185  auto sleCheck = std::make_shared<SLE>(checkKeylet);
186 
187  sleCheck->setAccountID(sfAccount, account_);
188  AccountID const dstAccountId = ctx_.tx[sfDestination];
189  sleCheck->setAccountID(sfDestination, dstAccountId);
190  sleCheck->setFieldU32(sfSequence, seq);
191  sleCheck->setFieldAmount(sfSendMax, ctx_.tx[sfSendMax]);
192  if (auto const srcTag = ctx_.tx[~sfSourceTag])
193  sleCheck->setFieldU32(sfSourceTag, *srcTag);
194  if (auto const dstTag = ctx_.tx[~sfDestinationTag])
195  sleCheck->setFieldU32(sfDestinationTag, *dstTag);
196  if (auto const invoiceId = ctx_.tx[~sfInvoiceID])
197  sleCheck->setFieldH256(sfInvoiceID, *invoiceId);
198  if (auto const expiry = ctx_.tx[~sfExpiration])
199  sleCheck->setFieldU32(sfExpiration, *expiry);
200 
201  view().insert(sleCheck);
202 
203  auto viewJ = ctx_.app.journal("View");
204  // If it's not a self-send (and it shouldn't be), add Check to the
205  // destination's owner directory.
206  if (dstAccountId != account_)
207  {
208  auto const page = view().dirInsert(
209  keylet::ownerDir(dstAccountId),
210  checkKeylet,
211  describeOwnerDir(dstAccountId));
212 
213  JLOG(j_.trace()) << "Adding Check to destination directory "
214  << to_string(checkKeylet.key) << ": "
215  << (page ? "success" : "failure");
216 
217  if (!page)
218  return tecDIR_FULL;
219 
220  sleCheck->setFieldU64(sfDestinationNode, *page);
221  }
222 
223  {
224  auto const page = view().dirInsert(
226  checkKeylet,
228 
229  JLOG(j_.trace()) << "Adding Check to owner directory "
230  << to_string(checkKeylet.key) << ": "
231  << (page ? "success" : "failure");
232 
233  if (!page)
234  return tecDIR_FULL;
235 
236  sleCheck->setFieldU64(sfOwnerNode, *page);
237  }
238  // If we succeeded, the new entry counts against the creator's reserve.
239  adjustOwnerCount(view(), sle, 1, viewJ);
240  return tesSUCCESS;
241 }
242 
243 } // 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::keylet::ownerDir
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition: Indexes.cpp:303
ripple::sfOwnerCount
const SF_UINT32 sfOwnerCount
ripple::STObject::setAccountID
void setAccountID(SField const &field, AccountID const &)
Definition: STObject.cpp:689
ripple::tecFROZEN
@ tecFROZEN
Definition: TER.h:270
ripple::preflight2
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:130
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:155
ripple::sfSendMax
const SF_AMOUNT sfSendMax
ripple::Rules::enabled
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition: Rules.cpp:94
ripple::PreclaimContext::view
ReadView const & view
Definition: Transactor.h:56
ripple::sfOwnerNode
const SF_UINT64 sfOwnerNode
ripple::temBAD_CURRENCY
@ temBAD_CURRENCY
Definition: TER.h:88
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::ApplyView::peek
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
ripple::sfDestination
const SF_ACCOUNT sfDestination
ripple::describeOwnerDir
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition: View.cpp:731
ripple::Transactor::j_
const beast::Journal j_
Definition: Transactor.h:89
ripple::isTesSuccess
bool isTesSuccess(TER x)
Definition: TER.h:597
ripple::sfSequence
const SF_UINT32 sfSequence
ripple::ReadView::fees
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
ripple::tecDST_TAG_NEEDED
@ tecDST_TAG_NEEDED
Definition: TER.h:276
beast::Journal::warn
Stream warn() const
Definition: Journal.h:327
ripple::hasExpired
bool hasExpired(ReadView const &view, std::optional< std::uint32_t > const &exp)
Determines whether the given expiration time has passed.
Definition: View.cpp:179
ripple::STTx::getSeqProxy
SeqProxy getSeqProxy() const
Definition: STTx.cpp:183
ripple::isLegalNet
bool isLegalNet(STAmount const &value)
Definition: STAmount.h:446
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:78
ripple::ApplyContext::app
Application & app
Definition: ApplyContext.h:47
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:109
ripple::isGlobalFrozen
bool isGlobalFrozen(ReadView const &view, AccountID const &issuer)
Definition: View.cpp:188
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:133
ripple::featureDisallowIncoming
const uint256 featureDisallowIncoming
ripple::STObject::getAccountID
AccountID getAccountID(SField const &field) const
Definition: STObject.cpp:589
ripple::TERSubset
Definition: TER.h:340
ripple::STAmount
Definition: STAmount.h:45
ripple::sfDestinationNode
const SF_UINT64 sfDestinationNode
ripple::STObject::getFlags
std::uint32_t getFlags() const
Definition: STObject.cpp:481
ripple::temBAD_AMOUNT
@ temBAD_AMOUNT
Definition: TER.h:87
std::uint32_t
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:193
ripple::featureChecks
const uint256 featureChecks
ripple::temREDUNDANT
@ temREDUNDANT
Definition: TER.h:110
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:224
ripple::tecDIR_FULL
@ tecDIR_FULL
Definition: TER.h:254
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:259
ripple::SeqProxy::value
constexpr std::uint32_t value() const
Definition: SeqProxy.h:82
ripple::ApplyView::insert
virtual void insert(std::shared_ptr< SLE > const &sle)=0
Insert a new state SLE.
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Application::journal
virtual beast::Journal journal(std::string const &name)=0
ripple::Transactor::view
ApplyView & view()
Definition: Transactor.h:107
ripple::tecEXPIRED
@ tecEXPIRED
Definition: TER.h:281
ripple::temDISABLED
@ temDISABLED
Definition: TER.h:112
ripple::Fees::accountReserve
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
Definition: ReadView.h:66
ripple::sfInvoiceID
const SF_UINT256 sfInvoiceID
ripple::ReadView::rules
virtual Rules const & rules() const =0
Returns the tx processing rules.
ripple::STObject::isFieldPresent
bool isFieldPresent(SField const &field) const
Definition: STObject.cpp:428
ripple::sfDestinationTag
const SF_UINT32 sfDestinationTag
ripple::Transactor::mPriorBalance
XRPAmount mPriorBalance
Definition: Transactor.h:92
ripple::tecNO_PERMISSION
@ tecNO_PERMISSION
Definition: TER.h:272
ripple::lsfDisallowIncomingCheck
@ lsfDisallowIncomingCheck
Definition: LedgerFormats.h:240
ripple::CreateCheck::doApply
TER doApply() override
Definition: CreateCheck.cpp:164
ripple::tecINSUFFICIENT_RESERVE
@ tecINSUFFICIENT_RESERVE
Definition: TER.h:274
ripple::Transactor::ctx_
ApplyContext & ctx_
Definition: Transactor.h:88
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::sfAccount
const SF_ACCOUNT sfAccount
ripple::lsfLowFreeze
@ lsfLowFreeze
Definition: LedgerFormats.h:258
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:89
ripple::ApplyView::dirInsert
std::optional< std::uint64_t > dirInsert(Keylet const &directory, uint256 const &key, std::function< void(std::shared_ptr< SLE > const &)> const &describe)
Insert an entry to a directory.
Definition: ApplyView.h:306
ripple::keylet::check
Keylet check(AccountID const &id, std::uint32_t seq) noexcept
A Check.
Definition: Indexes.cpp:281
ripple::CreateCheck::preflight
static NotTEC preflight(PreflightContext const &ctx)
Definition: CreateCheck.cpp:32
ripple::PreflightContext::rules
const Rules rules
Definition: Transactor.h:36
ripple::tfUniversalMask
constexpr std::uint32_t tfUniversalMask
Definition: TxFlags.h:60
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:222
ripple::Transactor::account_
const AccountID account_
Definition: Transactor.h:91
ripple::STObject::getFieldAmount
STAmount const & getFieldAmount(SField const &field) const
Definition: STObject.cpp:603
ripple::ApplyContext::tx
STTx const & tx
Definition: ApplyContext.h:48
ripple::tecNO_DST
@ tecNO_DST
Definition: TER.h:257
ripple::NotTEC
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:528