20#include <xrpld/app/tx/detail/SetTrust.h>
21#include <xrpld/ledger/View.h>
22#include <xrpl/basics/Log.h>
23#include <xrpl/protocol/AMMCore.h>
24#include <xrpl/protocol/Feature.h>
25#include <xrpl/protocol/Indexes.h>
26#include <xrpl/protocol/Quality.h>
27#include <xrpl/protocol/st.h>
44 JLOG(j.trace()) <<
"Malformed transaction: Invalid flags set.";
48 STAmount const saLimitAmount(tx.getFieldAmount(sfLimitAmount));
53 if (saLimitAmount.
native())
55 JLOG(j.trace()) <<
"Malformed transaction: specifies native limit "
62 JLOG(j.trace()) <<
"Malformed transaction: specifies XRP as IOU";
66 if (saLimitAmount < beast::zero)
68 JLOG(j.trace()) <<
"Malformed transaction: Negative credit limit.";
73 auto const& issuer = saLimitAmount.
getIssuer();
77 JLOG(j.trace()) <<
"Malformed transaction: no destination account.";
87 auto const id = ctx.
tx[sfAccount];
99 JLOG(ctx.
j.
trace()) <<
"Retry: Auth not required.";
103 auto const saLimitAmount = ctx.
tx[sfLimitAmount];
105 auto const currency = saLimitAmount.getCurrency();
106 auto const uDstAccountID = saLimitAmount.getIssuer();
110 if (
id == uDstAccountID)
115 if (
id == uDstAccountID)
120 auto const sleDelete =
126 <<
"Malformed transaction: Can not extend credit to self.";
167 if (sleDst->isFieldPresent(sfAMMID) &&
170 if (
auto const ammSle =
171 ctx.
view.
read({ltAMM, sleDst->getFieldH256(sfAMMID)}))
173 if (
auto const lpTokens =
174 ammSle->getFieldAmount(sfLPTokenBalance);
175 lpTokens == beast::zero)
177 else if (lpTokens.getCurrency() != saLimitAmount.getCurrency())
201 bool const bHigh =
account_ > uDstAccountID;
207 std::uint32_t const uOwnerCount = sle->getFieldU32(sfOwnerCount);
228 (uOwnerCount < 2) ?
XRPAmount(beast::zero)
229 :
view().fees().accountReserve(uOwnerCount + 1));
235 if (bQualityOut && QUALITY_ONE == uQualityOut)
240 bool const bSetAuth = (uTxFlags &
tfSetfAuth);
268 <<
"Delay transaction: Destination account does not exist.";
272 STAmount saLimitAllow = saLimitAmount;
288 auto const& uLowAccountID = !bHigh ?
account_ : uDstAccountID;
289 auto const& uHighAccountID = bHigh ?
account_ : uDstAccountID;
290 SLE::ref sleLowAccount = !bHigh ? sle : sleDst;
291 SLE::ref sleHighAccount = bHigh ? sle : sleDst;
297 saLowBalance = sleRippleState->getFieldAmount(sfBalance);
298 saHighBalance = -saLowBalance;
304 sleRippleState->setFieldAmount(
305 !bHigh ? sfLowLimit : sfHighLimit, saLimitAllow);
308 !bHigh ? saLimitAllow : sleRippleState->getFieldAmount(sfLowLimit);
310 bHigh ? saLimitAllow : sleRippleState->getFieldAmount(sfHighLimit);
320 uLowQualityIn = sleRippleState->getFieldU32(sfLowQualityIn);
321 uHighQualityIn = sleRippleState->getFieldU32(sfHighQualityIn);
327 sleRippleState->setFieldU32(
328 !bHigh ? sfLowQualityIn : sfHighQualityIn, uQualityIn);
330 uLowQualityIn = !bHigh
332 : sleRippleState->getFieldU32(sfLowQualityIn);
333 uHighQualityIn = bHigh
335 : sleRippleState->getFieldU32(sfHighQualityIn);
341 sleRippleState->makeFieldAbsent(
342 !bHigh ? sfLowQualityIn : sfHighQualityIn);
345 !bHigh ? 0 : sleRippleState->getFieldU32(sfLowQualityIn);
347 bHigh ? 0 : sleRippleState->getFieldU32(sfHighQualityIn);
350 if (QUALITY_ONE == uLowQualityIn)
353 if (QUALITY_ONE == uHighQualityIn)
364 uLowQualityOut = sleRippleState->getFieldU32(sfLowQualityOut);
365 uHighQualityOut = sleRippleState->getFieldU32(sfHighQualityOut);
367 else if (uQualityOut)
371 sleRippleState->setFieldU32(
372 !bHigh ? sfLowQualityOut : sfHighQualityOut, uQualityOut);
374 uLowQualityOut = !bHigh
376 : sleRippleState->getFieldU32(sfLowQualityOut);
377 uHighQualityOut = bHigh
379 : sleRippleState->getFieldU32(sfHighQualityOut);
385 sleRippleState->makeFieldAbsent(
386 !bHigh ? sfLowQualityOut : sfHighQualityOut);
389 !bHigh ? 0 : sleRippleState->getFieldU32(sfLowQualityOut);
391 bHigh ? 0 : sleRippleState->getFieldU32(sfHighQualityOut);
394 std::uint32_t const uFlagsIn(sleRippleState->getFieldU32(sfFlags));
397 if (bSetNoRipple && !bClearNoRipple)
399 if ((bHigh ? saHighBalance : saLowBalance) >= beast::zero)
406 else if (bClearNoRipple && !bSetNoRipple)
411 if (bSetFreeze && !bClearFreeze && !sle->isFlag(
lsfNoFreeze))
415 else if (bClearFreeze && !bSetFreeze)
420 if (QUALITY_ONE == uLowQualityOut)
423 if (QUALITY_ONE == uHighQualityOut)
427 bool const bHighDefRipple =
430 bool const bLowReserveSet = uLowQualityIn || uLowQualityOut ||
433 saLowBalance > beast::zero;
434 bool const bLowReserveClear = !bLowReserveSet;
436 bool const bHighReserveSet = uHighQualityIn || uHighQualityOut ||
439 saHighBalance > beast::zero;
440 bool const bHighReserveClear = !bHighReserveSet;
442 bool const bDefault = bLowReserveClear && bHighReserveClear;
447 bool bReserveIncrease =
false;
454 if (bLowReserveSet && !bLowReserved)
461 bReserveIncrease =
true;
464 if (bLowReserveClear && bLowReserved)
468 uFlagsOut &= ~lsfLowReserve;
471 if (bHighReserveSet && !bHighReserved)
478 bReserveIncrease =
true;
481 if (bHighReserveClear && bHighReserved)
485 uFlagsOut &= ~lsfHighReserve;
488 if (uFlagsIn != uFlagsOut)
489 sleRippleState->setFieldU32(sfFlags, uFlagsOut);
496 view(), sleRippleState, uLowAccountID, uHighAccountID, viewJ);
502 <<
"Delay transaction: Insufficent reserve to add trust line.";
512 JLOG(
j_.
trace()) <<
"Modify ripple line";
518 (!bQualityIn || !uQualityIn) &&
520 (!bQualityOut || !uQualityOut) &&
525 <<
"Redundant: Setting non-existent ripple line to defaults.";
530 JLOG(
j_.
trace()) <<
"Delay transaction: Line does not exist. "
531 "Insufficent reserve to create line.";
544 JLOG(
j_.
trace()) <<
"doTrustSet: Creating ripple line: "
556 bSetNoRipple && !bClearNoRipple,
557 bSetFreeze && !bClearFreeze,
Stream trace() const
Severity stream access functions.
virtual beast::Journal journal(std::string const &name)=0
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
A currency issued by an account.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
virtual Rules const & rules() const =0
Returns the tx processing rules.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
void setIssuer(AccountID const &uIssuer)
Currency const & getCurrency() const
AccountID const & getIssuer() const
std::string getFullText() const override
bool native() const noexcept
std::uint32_t getFieldU32(SField const &field) const
STAmount const & getFieldAmount(SField const &field) const
bool isFieldPresent(SField const &field) const
std::uint32_t getFlags() const
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
Keylet line(AccountID const &id0, AccountID const &id1, Currency const ¤cy) noexcept
The index of a trust line for a given currency.
Keylet account(AccountID const &id) noexcept
AccountID root.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
AccountID const & noAccount()
A placeholder for empty accounts.
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
bool isLegalNet(STAmount const &value)
@ lsfDisallowIncomingTrustline
bool ammEnabled(Rules const &)
Return true if required AMM amendments are enabled.
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
TER trustCreate(ApplyView &view, const bool bSrcHigh, AccountID const &uSrcAccountID, AccountID const &uDstAccountID, uint256 const &uIndex, SLE::ref sleAccount, const bool bAuth, const bool bNoRipple, const bool bFreeze, STAmount const &saBalance, STAmount const &saLimit, std::uint32_t uQualityIn, std::uint32_t uQualityOut, beast::Journal j)
Create a trust line.
constexpr std::uint32_t tfClearNoRipple
static bool adjustOwnerCount(ApplyContext &ctx, int count)
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
constexpr std::uint32_t tfSetfAuth
constexpr std::uint32_t tfClearFreeze
TER trustDelete(ApplyView &view, std::shared_ptr< SLE > const &sleRippleState, AccountID const &uLowAccountID, AccountID const &uHighAccountID, beast::Journal j)
@ tecNO_LINE_INSUF_RESERVE
constexpr std::uint32_t tfTrustSetMask
std::string to_string(base_uint< Bits, Tag > const &a)
constexpr std::uint32_t tfSetFreeze
constexpr std::uint32_t tfSetNoRipple
TERSubset< CanCvtToNotTEC > NotTEC
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.