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  if (hasExpired(ctx.view, ctx.tx[~sfExpiration]))
150  {
151  JLOG(ctx.j.warn()) << "Creating a check that has already expired.";
152  return tecEXPIRED;
153  }
154  return tesSUCCESS;
155 }
156 
157 TER
159 {
160  auto const sle = view().peek(keylet::account(account_));
161  if (!sle)
162  return tefINTERNAL;
163 
164  // A check counts against the reserve of the issuing account, but we
165  // check the starting balance because we want to allow dipping into the
166  // reserve to pay fees.
167  {
168  STAmount const reserve{
169  view().fees().accountReserve(sle->getFieldU32(sfOwnerCount) + 1)};
170 
171  if (mPriorBalance < reserve)
173  }
174 
175  // Note that we use the value from the sequence or ticket as the
176  // Check sequence. For more explanation see comments in SeqProxy.h.
177  std::uint32_t const seq = ctx_.tx.getSeqProxy().value();
178  Keylet const checkKeylet = keylet::check(account_, seq);
179  auto sleCheck = std::make_shared<SLE>(checkKeylet);
180 
181  sleCheck->setAccountID(sfAccount, account_);
182  AccountID const dstAccountId = ctx_.tx[sfDestination];
183  sleCheck->setAccountID(sfDestination, dstAccountId);
184  sleCheck->setFieldU32(sfSequence, seq);
185  sleCheck->setFieldAmount(sfSendMax, ctx_.tx[sfSendMax]);
186  if (auto const srcTag = ctx_.tx[~sfSourceTag])
187  sleCheck->setFieldU32(sfSourceTag, *srcTag);
188  if (auto const dstTag = ctx_.tx[~sfDestinationTag])
189  sleCheck->setFieldU32(sfDestinationTag, *dstTag);
190  if (auto const invoiceId = ctx_.tx[~sfInvoiceID])
191  sleCheck->setFieldH256(sfInvoiceID, *invoiceId);
192  if (auto const expiry = ctx_.tx[~sfExpiration])
193  sleCheck->setFieldU32(sfExpiration, *expiry);
194 
195  view().insert(sleCheck);
196 
197  auto viewJ = ctx_.app.journal("View");
198  // If it's not a self-send (and it shouldn't be), add Check to the
199  // destination's owner directory.
200  if (dstAccountId != account_)
201  {
202  auto const page = view().dirInsert(
203  keylet::ownerDir(dstAccountId),
204  checkKeylet,
205  describeOwnerDir(dstAccountId));
206 
207  JLOG(j_.trace()) << "Adding Check to destination directory "
208  << to_string(checkKeylet.key) << ": "
209  << (page ? "success" : "failure");
210 
211  if (!page)
212  return tecDIR_FULL;
213 
214  sleCheck->setFieldU64(sfDestinationNode, *page);
215  }
216 
217  {
218  auto const page = view().dirInsert(
220  checkKeylet,
222 
223  JLOG(j_.trace()) << "Adding Check to owner directory "
224  << to_string(checkKeylet.key) << ": "
225  << (page ? "success" : "failure");
226 
227  if (!page)
228  return tecDIR_FULL;
229 
230  sleCheck->setFieldU64(sfOwnerNode, *page);
231  }
232  // If we succeeded, the new entry counts against the creator's reserve.
233  adjustOwnerCount(view(), sle, 1, viewJ);
234  return tesSUCCESS;
235 }
236 
237 } // 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:267
ripple::preflight2
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:109
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:152
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:83
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::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:594
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:273
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:431
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:57
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:106
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::STObject::getAccountID
AccountID getAccountID(SField const &field) const
Definition: STObject.cpp:589
ripple::TERSubset
Definition: TER.h:337
ripple::STAmount
Definition: STAmount.h:44
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:84
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: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:224
ripple::tecDIR_FULL
@ tecDIR_FULL
Definition: TER.h:251
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:248
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:278
ripple::temDISABLED
@ temDISABLED
Definition: TER.h:109
ripple::Fees::accountReserve
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
Definition: ReadView.h:67
ripple::sfInvoiceID
const SF_UINT256 sfInvoiceID
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::CreateCheck::doApply
TER doApply() override
Definition: CreateCheck.cpp:158
ripple::tecINSUFFICIENT_RESERVE
@ tecINSUFFICIENT_RESERVE
Definition: TER.h:271
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:247
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::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:58
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:219
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:254
ripple::NotTEC
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:525