20#include <xrpld/app/misc/DelegateUtils.h>
21#include <xrpld/app/tx/detail/SetTrust.h>
22#include <xrpld/ledger/View.h>
24#include <xrpl/basics/Log.h>
25#include <xrpl/protocol/AMMCore.h>
26#include <xrpl/protocol/Feature.h>
27#include <xrpl/protocol/Indexes.h>
28#include <xrpl/protocol/Quality.h>
29#include <xrpl/protocol/SField.h>
30#include <xrpl/protocol/TER.h>
42 bool bClearDeepFreeze)
44 if (bSetFreeze && !bClearFreeze && !bNoFreeze)
48 else if (bClearFreeze && !bSetFreeze)
52 if (bSetDeepFreeze && !bClearDeepFreeze && !bNoFreeze)
57 else if (bClearDeepFreeze && !bSetDeepFreeze)
83 JLOG(j.trace()) <<
"Malformed transaction: Invalid flags set.";
97 STAmount const saLimitAmount(tx.getFieldAmount(sfLimitAmount));
102 if (saLimitAmount.
native())
104 JLOG(j.trace()) <<
"Malformed transaction: specifies native limit "
111 JLOG(j.trace()) <<
"Malformed transaction: specifies XRP as IOU";
115 if (saLimitAmount < beast::zero)
117 JLOG(j.trace()) <<
"Malformed transaction: Negative credit limit.";
122 auto const& issuer = saLimitAmount.
getIssuer();
126 JLOG(j.trace()) <<
"Malformed transaction: no destination account.";
136 auto const delegate = tx[~sfDelegate];
141 auto const sle =
view.
read(delegateKey);
162 tx[sfAccount], saLimitAmount.getIssuer(), saLimitAmount.getCurrency()));
173 !granularPermissions.
contains(TrustlineAuthorize))
178 !granularPermissions.
contains(TrustlineUnfreeze))
183 auto const curLimit = tx[sfAccount] > saLimitAmount.getIssuer()
185 : sleRippleState->getFieldAmount(sfLowLimit);
187 STAmount saLimitAllow = saLimitAmount;
190 if (curLimit != saLimitAllow)
199 auto const id = ctx.
tx[sfAccount];
207 bool const bSetAuth = (uTxFlags &
tfSetfAuth);
211 JLOG(ctx.
j.
trace()) <<
"Retry: Auth not required.";
215 auto const saLimitAmount = ctx.
tx[sfLimitAmount];
217 auto const currency = saLimitAmount.getCurrency();
218 auto const uDstAccountID = saLimitAmount.getIssuer();
222 if (
id == uDstAccountID)
227 if (
id == uDstAccountID)
232 auto const sleDelete =
238 <<
"Malformed transaction: Can not extend credit to self.";
281 if (sleDst->isFieldPresent(sfAMMID))
289 ctx.
view.
read({ltAMM, sleDst->getFieldH256(sfAMMID)}))
291 if (
auto const lpTokens =
292 ammSle->getFieldAmount(sfLPTokenBalance);
293 lpTokens == beast::zero)
295 else if (lpTokens.getCurrency() != saLimitAmount.getCurrency())
301 else if (sleDst->isFieldPresent(sfVaultID))
318 if (bNoFreeze && (bSetFreeze || bSetDeepFreeze))
326 if ((bSetFreeze || bSetDeepFreeze) &&
327 (bClearFreeze || bClearDeepFreeze))
334 bool const bHigh =
id > uDstAccountID;
336 auto const sleRippleState =
339 sleRippleState ? sleRippleState->getFieldU32(sfFlags) : 0u;
341 uFlags = computeFreezeFlags(
351 auto const deepFrozen =
357 if (deepFrozen && !frozen)
379 bool const bHigh =
account_ > uDstAccountID;
385 std::uint32_t const uOwnerCount = sle->getFieldU32(sfOwnerCount);
406 (uOwnerCount < 2) ?
XRPAmount(beast::zero)
407 :
view().fees().accountReserve(uOwnerCount + 1));
413 if (bQualityOut && QUALITY_ONE == uQualityOut)
418 bool const bSetAuth = (uTxFlags &
tfSetfAuth);
448 <<
"Delay transaction: Destination account does not exist.";
452 STAmount saLimitAllow = saLimitAmount;
468 auto const& uLowAccountID = !bHigh ?
account_ : uDstAccountID;
469 auto const& uHighAccountID = bHigh ?
account_ : uDstAccountID;
470 SLE::ref sleLowAccount = !bHigh ? sle : sleDst;
471 SLE::ref sleHighAccount = bHigh ? sle : sleDst;
477 saLowBalance = sleRippleState->getFieldAmount(sfBalance);
478 saHighBalance = -saLowBalance;
484 sleRippleState->setFieldAmount(
485 !bHigh ? sfLowLimit : sfHighLimit, saLimitAllow);
488 !bHigh ? saLimitAllow : sleRippleState->getFieldAmount(sfLowLimit);
490 bHigh ? saLimitAllow : sleRippleState->getFieldAmount(sfHighLimit);
500 uLowQualityIn = sleRippleState->getFieldU32(sfLowQualityIn);
501 uHighQualityIn = sleRippleState->getFieldU32(sfHighQualityIn);
507 sleRippleState->setFieldU32(
508 !bHigh ? sfLowQualityIn : sfHighQualityIn, uQualityIn);
510 uLowQualityIn = !bHigh
512 : sleRippleState->getFieldU32(sfLowQualityIn);
513 uHighQualityIn = bHigh
515 : sleRippleState->getFieldU32(sfHighQualityIn);
521 sleRippleState->makeFieldAbsent(
522 !bHigh ? sfLowQualityIn : sfHighQualityIn);
525 !bHigh ? 0 : sleRippleState->getFieldU32(sfLowQualityIn);
527 bHigh ? 0 : sleRippleState->getFieldU32(sfHighQualityIn);
530 if (QUALITY_ONE == uLowQualityIn)
533 if (QUALITY_ONE == uHighQualityIn)
544 uLowQualityOut = sleRippleState->getFieldU32(sfLowQualityOut);
545 uHighQualityOut = sleRippleState->getFieldU32(sfHighQualityOut);
547 else if (uQualityOut)
551 sleRippleState->setFieldU32(
552 !bHigh ? sfLowQualityOut : sfHighQualityOut, uQualityOut);
554 uLowQualityOut = !bHigh
556 : sleRippleState->getFieldU32(sfLowQualityOut);
557 uHighQualityOut = bHigh
559 : sleRippleState->getFieldU32(sfHighQualityOut);
565 sleRippleState->makeFieldAbsent(
566 !bHigh ? sfLowQualityOut : sfHighQualityOut);
569 !bHigh ? 0 : sleRippleState->getFieldU32(sfLowQualityOut);
571 bHigh ? 0 : sleRippleState->getFieldU32(sfHighQualityOut);
574 std::uint32_t const uFlagsIn(sleRippleState->getFieldU32(sfFlags));
577 if (bSetNoRipple && !bClearNoRipple)
579 if ((bHigh ? saHighBalance : saLowBalance) >= beast::zero)
586 else if (bClearNoRipple && !bSetNoRipple)
593 uFlagsOut = computeFreezeFlags(
602 if (QUALITY_ONE == uLowQualityOut)
605 if (QUALITY_ONE == uHighQualityOut)
609 bool const bHighDefRipple =
612 bool const bLowReserveSet = uLowQualityIn || uLowQualityOut ||
615 saLowBalance > beast::zero;
616 bool const bLowReserveClear = !bLowReserveSet;
618 bool const bHighReserveSet = uHighQualityIn || uHighQualityOut ||
621 saHighBalance > beast::zero;
622 bool const bHighReserveClear = !bHighReserveSet;
624 bool const bDefault = bLowReserveClear && bHighReserveClear;
629 bool bReserveIncrease =
false;
636 if (bLowReserveSet && !bLowReserved)
643 bReserveIncrease =
true;
646 if (bLowReserveClear && bLowReserved)
650 uFlagsOut &= ~lsfLowReserve;
653 if (bHighReserveSet && !bHighReserved)
660 bReserveIncrease =
true;
663 if (bHighReserveClear && bHighReserved)
667 uFlagsOut &= ~lsfHighReserve;
670 if (uFlagsIn != uFlagsOut)
671 sleRippleState->setFieldU32(sfFlags, uFlagsOut);
678 view(), sleRippleState, uLowAccountID, uHighAccountID, viewJ);
683 JLOG(
j_.
trace()) <<
"Delay transaction: Insufficent reserve to "
694 JLOG(
j_.
trace()) <<
"Modify ripple line";
700 (!bQualityIn || !uQualityIn) &&
702 (!bQualityOut || !uQualityOut) &&
707 <<
"Redundant: Setting non-existent ripple line to defaults.";
713 JLOG(
j_.
trace()) <<
"Delay transaction: Line does not exist. "
714 "Insufficent reserve to create line.";
727 JLOG(
j_.
trace()) <<
"doTrustSet: Creating ripple line: "
739 bSetNoRipple && !bClearNoRipple,
740 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)
static TER checkPermission(ReadView const &view, STTx const &tx)
Keylet delegate(AccountID const &account, AccountID const &authorizedAccount) noexcept
A keylet for Delegate object.
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...
constexpr std::uint32_t tfSetDeepFreeze
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.
constexpr std::uint32_t tfTrustSetPermissionMask
constexpr std::uint32_t tfClearNoRipple
static bool adjustOwnerCount(ApplyContext &ctx, int count)
void loadGranularPermission(std::shared_ptr< SLE const > const &delegate, TxType const &type, std::unordered_set< GranularPermissionType > &granularPermissions)
Load the granular permissions granted to the delegate account for the specified transaction type.
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
constexpr std::uint32_t tfSetfAuth
constexpr std::uint32_t tfClearFreeze
TER trustCreate(ApplyView &view, bool const bSrcHigh, AccountID const &uSrcAccountID, AccountID const &uDstAccountID, uint256 const &uIndex, SLE::ref sleAccount, bool const bAuth, bool const bNoRipple, bool const bFreeze, bool bDeepFreeze, STAmount const &saBalance, STAmount const &saLimit, std::uint32_t uQualityIn, std::uint32_t uQualityOut, beast::Journal j)
Create a trust line.
TER checkTxPermission(std::shared_ptr< SLE const > const &delegate, STTx const &tx)
Check if the delegate account has permission to execute the transaction.
TER trustDelete(ApplyView &view, std::shared_ptr< SLE > const &sleRippleState, AccountID const &uLowAccountID, AccountID const &uHighAccountID, beast::Journal j)
@ tecNO_DELEGATE_PERMISSION
@ tecNO_LINE_INSUF_RESERVE
constexpr std::uint32_t tfClearDeepFreeze
bool isTesSuccess(TER x) noexcept
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
bool isPseudoAccount(std::shared_ptr< SLE const > sleAcct)
TERSubset< CanCvtToNotTEC > NotTEC
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.