1#include <xrpld/app/misc/HashRouter.h>
2#include <xrpld/app/tx/detail/Escrow.h>
3#include <xrpld/app/tx/detail/MPTokenAuthorize.h>
4#include <xrpld/conditions/Condition.h>
5#include <xrpld/conditions/Fulfillment.h>
7#include <xrpl/basics/Log.h>
8#include <xrpl/basics/chrono.h>
9#include <xrpl/ledger/ApplyView.h>
10#include <xrpl/ledger/CredentialHelpers.h>
11#include <xrpl/ledger/View.h>
12#include <xrpl/protocol/Feature.h>
13#include <xrpl/protocol/Indexes.h>
14#include <xrpl/protocol/MPTAmount.h>
15#include <xrpl/protocol/TxFlags.h>
16#include <xrpl/protocol/XRPAmount.h>
65 auto const amount = ctx.
tx[sfAmount];
69template <Val
idIssueType T>
78 if (amount.native() || amount <= beast::zero)
94 auto const amount = ctx.
tx[sfAmount];
95 if (amount.native() || amount.mpt() >
MPTAmount{maxMPTokenAmount} || amount <= beast::zero)
111 [&]<
typename T>(T
const&) {
return escrowCreatePreflightHelper<T>(ctx); }, amount.asset().value());
117 if (amount <= beast::zero)
122 if (!ctx.
tx[~sfCancelAfter] && !ctx.
tx[~sfFinishAfter])
127 if (ctx.
tx[~sfCancelAfter] && ctx.
tx[~sfFinishAfter] && ctx.
tx[sfCancelAfter] <= ctx.
tx[sfFinishAfter])
134 if (!ctx.
tx[~sfFinishAfter] && !ctx.
tx[~sfCondition])
137 if (
auto const cb = ctx.
tx[~sfCondition])
143 auto condition = Condition::deserialize(*cb, ec);
146 JLOG(ctx.
j.
debug()) <<
"Malformed condition during escrow creation: " << ec.
message();
154template <Val
idIssueType T>
172 if (issuer == account)
187 STAmount const balance = (*sleRippleState)[sfBalance];
190 if (balance > beast::zero && issuer < account)
194 if (balance < beast::zero && issuer > account)
217 if (spendableAmount <= beast::zero)
222 if (spendableAmount < amount)
226 if (!
canAdd(spendableAmount, amount))
242 if (issuer == account)
247 auto const sleIssuance = ctx.
view.
read(issuanceKey);
257 if (sleIssuance->getAccountID(sfIssuer) != issuer)
266 auto const& mptIssue = amount.get<
MPTIssue>();
291 if (spendableAmount <= beast::zero)
296 if (spendableAmount < amount)
326 [&]<
typename T>(T
const&) {
return escrowCreatePreclaimHelper<T>(ctx, account, dest, amount); },
327 amount.asset().value());
334template <Val
idIssueType T>
353 if (issuer == sender)
356 auto const ter =
rippleCredit(view, sender, issuer, amount, amount.holds<
MPTIssue>() ?
false :
true, journal);
372 if (issuer == sender)
424 (*slep)[sfAmount] = amount;
426 (*slep)[~sfCondition] =
ctx_.
tx[~sfCondition];
427 (*slep)[~sfSourceTag] =
ctx_.
tx[~sfSourceTag];
428 (*slep)[sfDestination] =
ctx_.
tx[sfDestination];
429 (*slep)[~sfCancelAfter] =
ctx_.
tx[~sfCancelAfter];
430 (*slep)[~sfFinishAfter] =
ctx_.
tx[~sfFinishAfter];
431 (*slep)[~sfDestinationTag] =
ctx_.
tx[~sfDestinationTag];
442 (*slep)[sfTransferRate] = xferRate.value;
452 (*slep)[sfOwnerNode] = *page;
462 (*slep)[sfDestinationNode] = *page;
468 AccountID const issuer = amount.getIssuer();
474 (*slep)[sfIssuerNode] = *page;
479 (*sle)[sfBalance] = (*sle)[sfBalance] - amount;
483 [&]<
typename T>(T
const&) {
486 amount.asset().value());
508 auto condition = Condition::deserialize(c, ec);
512 auto fulfillment = Fulfillment::deserialize(f, ec);
516 return validate(*fulfillment, *condition);
528 auto const cb = ctx.
tx[~sfCondition];
529 auto const fb = ctx.
tx[~sfFulfillment];
533 if (
static_cast<bool>(cb) !=
static_cast<bool>(fb))
542 auto const cb = ctx.
tx[~sfCondition];
543 auto const fb = ctx.
tx[~sfFulfillment];
550 auto const flags = router.getFlags(
id);
575 if (
auto const fb = tx[~sfFulfillment])
577 extraFee +=
view.
fees().
base * (32 + (fb->size() / 16));
583template <Val
idIssueType T>
618 auto const sleIssuance = ctx.
view.
read(issuanceKey);
624 auto const& mptIssue = amount.get<
MPTIssue>();
651 AccountID const dest = (*slep)[sfDestination];
652 STAmount const amount = (*slep)[sfAmount];
657 [&]<
typename T>(T
const&) {
return escrowFinishPreclaimHelper<T>(ctx, dest, amount); },
658 amount.asset().value());
666template <Val
idIssueType T>
695 bool const recvLow = issuer > receiver;
696 bool const senderIssuer = issuer == sender;
697 bool const receiverIssuer = issuer == receiver;
698 bool const issuerHigh = issuer > receiver;
706 if (!view.
exists(trustLineKey) && createAsset && !receiverIssuer)
709 if (
std::uint32_t const ownerCount = {sleDest->at(sfOwnerCount)};
710 xrpBalance < view.
fees().accountReserve(ownerCount + 1))
712 JLOG(journal.
trace()) <<
"Trust line does not exist. "
713 "Insufficent reserve to create line.";
718 Currency const currency = amount.getCurrency();
719 STAmount initialBalance(amount.issue());
735 Issue(currency, receiver),
748 if (!view.
exists(trustLineKey) && !receiverIssuer)
753 if (xferRate < lockedRate)
754 lockedRate = xferRate;
765 auto finalAmt = amount;
766 if ((!senderIssuer && !receiverIssuer) && lockedRate !=
parityRate)
769 auto const xferFee = amount.value() -
divideRound(amount, lockedRate, amount.issue(),
true);
771 finalAmt = amount.value() - xferFee;
778 auto const sleRippleState = view.
peek(trustLineKey);
784 STAmount const lineLimit = sleRippleState->getFieldAmount(issuerHigh ? sfLowLimit : sfHighLimit);
786 STAmount lineBalance = sleRippleState->getFieldAmount(sfBalance);
793 lineBalance += finalAmt;
796 if (lineLimit < lineBalance)
803 auto const ter =
rippleCredit(view, issuer, receiver, finalAmt,
true, journal);
824 bool const senderIssuer = issuer == sender;
825 bool const receiverIssuer = issuer == receiver;
827 auto const mptID = amount.get<
MPTIssue>().getMptID();
831 if (
std::uint32_t const ownerCount = {sleDest->at(sfOwnerCount)};
832 xrpBalance < view.
fees().accountReserve(ownerCount + 1))
851 if (xferRate < lockedRate)
852 lockedRate = xferRate;
863 auto finalAmt = amount;
864 if ((!senderIssuer && !receiverIssuer) && lockedRate !=
parityRate)
867 auto const xferFee = amount.value() -
divideRound(amount, lockedRate, amount.asset(),
true);
869 finalAmt = amount.value() - xferFee;
872 view, sender, receiver, finalAmt, view.
rules().
enabled(fixTokenEscrowV1) ? amount : finalAmt, journal);
893 if ((*slep)[~sfFinishAfter] && !
after(now, (*slep)[sfFinishAfter]))
897 if ((*slep)[~sfCancelAfter] &&
after(now, (*slep)[sfCancelAfter]))
905 auto const cb =
ctx_.
tx[~sfCondition];
913 auto const fb =
ctx_.
tx[~sfFulfillment];
933 auto const cond = (*slep)[~sfCondition];
945 if (cond && (cond != cb))
950 AccountID const destID = (*slep)[sfDestination];
958 AccountID const account = (*slep)[sfAccount];
962 auto const page = (*slep)[sfOwnerNode];
966 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from owner.";
973 if (
auto const optPage = (*slep)[~sfDestinationNode])
978 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from recipient.";
984 STAmount const amount = slep->getFieldAmount(sfAmount);
987 (*sled)[sfBalance] = (*sled)[sfBalance] + amount;
994 slep->isFieldPresent(sfTransferRate) ?
xrpl::Rate(slep->getFieldU32(sfTransferRate)) :
parityRate;
995 auto const issuer = amount.getIssuer();
996 bool const createAsset = destID ==
account_;
998 [&]<
typename T>(T
const&) {
999 return escrowUnlockApplyHelper<T>(
1002 amount.asset().value());
1007 if (
auto const optPage = (*slep)[~sfIssuerNode]; optPage)
1012 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from recipient.";
1039template <Val
idIssueType T>
1049 if (issuer == account)
1065 if (issuer == account)
1070 auto const sleIssuance = ctx.
view.
read(issuanceKey);
1076 auto const& mptIssue = amount.get<
MPTIssue>();
1089 auto const slep = ctx.
view.
read(k);
1093 AccountID const account = (*slep)[sfAccount];
1094 STAmount const amount = (*slep)[sfAmount];
1099 [&]<
typename T>(T
const&) {
return escrowCancelPreclaimHelper<T>(ctx, account, amount); },
1100 amount.asset().value());
1124 if (!(*slep)[~sfCancelAfter])
1128 if (!
after(now, (*slep)[sfCancelAfter]))
1131 AccountID const account = (*slep)[sfAccount];
1135 auto const page = (*slep)[sfOwnerNode];
1139 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from owner.";
1146 if (
auto const optPage = (*slep)[~sfDestinationNode]; optPage)
1151 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from recipient.";
1158 STAmount const amount = slep->getFieldAmount(sfAmount);
1162 (*sle)[sfBalance] = (*sle)[sfBalance] + amount;
1168 auto const issuer = amount.getIssuer();
1169 bool const createAsset = account ==
account_;
1171 [&]<
typename T>(T
const&) {
1172 return escrowUnlockApplyHelper<T>(
1184 amount.asset().value());
1189 if (
auto const optPage = (*slep)[~sfIssuerNode]; optPage)
1194 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from recipient.";
A generic endpoint for log messages.
Stream trace() const
Severity stream access functions.
virtual HashRouter & getHashRouter()=0
beast::Journal const journal
Writeable view to a ledger, for applying a transaction.
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
bool dirRemove(Keylet const &directory, std::uint64_t page, uint256 const &key, bool keepRoot)
Remove an entry from a directory.
virtual void erase(std::shared_ptr< SLE > const &sle)=0
Remove a peeked SLE.
virtual void insert(std::shared_ptr< SLE > const &sle)=0
Insert a new state SLE.
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.
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
static TxConsequences makeTxConsequences(PreflightContext const &ctx)
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
static bool checkExtraFeatures(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
static NotTEC preflightSigValidated(PreflightContext const &ctx)
HashRouterFlags getFlags(uint256 const &key)
bool setFlags(uint256 const &key, HashRouterFlags flags)
Set the flags on a hash.
A currency issued by an account.
constexpr MPTID const & getMptID() const
static TER createMPToken(ApplyView &view, MPTID const &mptIssuanceID, AccountID const &account, std::uint32_t const flags)
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
virtual LedgerHeader const & header() const =0
Returns information about the ledger.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
void setIssuer(AccountID const &uIssuer)
bool isFieldPresent(SField const &field) const
std::uint32_t getSeqValue() const
Returns the first non-zero value of (Sequence, TicketSequence).
uint256 getTransactionID() const
An immutable linear range of bytes.
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
Class describing the consequences to the account of applying a transaction if the transaction consume...
NotTEC checkFields(STTx const &tx, beast::Journal j)
TER valid(STTx const &tx, ReadView const &view, AccountID const &src, beast::Journal j)
Keylet escrow(AccountID const &src, std::uint32_t seq) noexcept
An escrow entry.
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Keylet mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Keylet line(AccountID const &id0, AccountID const &id1, Currency const ¤cy) noexcept
The index of a trust line for a given currency.
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Keylet account(AccountID const &id) noexcept
AccountID root.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
constexpr HashRouterFlags SF_CF_VALID
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 uSrcQualityIn, std::uint32_t uSrcQualityOut, beast::Journal j)
Create a trust line.
bool isXRP(AccountID const &c)
static TER escrowFinishPreclaimHelper(PreclaimContext const &ctx, AccountID const &dest, STAmount const &amount)
TER escrowCancelPreclaimHelper< MPTIssue >(PreclaimContext const &ctx, AccountID const &account, STAmount const &amount)
TER rippleUnlockEscrowMPT(ApplyView &view, AccountID const &uGrantorID, AccountID const &uGranteeID, STAmount const &netAmount, STAmount const &grossAmount, beast::Journal j)
NotTEC escrowCreatePreflightHelper< Issue >(PreflightContext const &ctx)
static TER escrowUnlockApplyHelper(ApplyView &view, Rate lockedRate, std::shared_ptr< SLE > const &sleDest, STAmount const &xrpBalance, STAmount const &amount, AccountID const &issuer, AccountID const &sender, AccountID const &receiver, bool createAsset, beast::Journal journal)
TER escrowCancelPreclaimHelper< Issue >(PreclaimContext const &ctx, AccountID const &account, STAmount const &amount)
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=shSIMPLE_BALANCE)
bool isDeepFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
TER escrowFinishPreclaimHelper< Issue >(PreclaimContext const &ctx, AccountID const &dest, STAmount const &amount)
TER verifyDepositPreauth(STTx const &tx, ApplyView &view, AccountID const &src, AccountID const &dst, std::shared_ptr< SLE > const &sleDst, beast::Journal j)
bool isPseudoAccount(std::shared_ptr< SLE const > sleAcct, std::set< SField const * > const &pseudoFieldFilter={})
TER escrowUnlockApplyHelper< MPTIssue >(ApplyView &view, Rate lockedRate, std::shared_ptr< SLE > const &sleDest, STAmount const &xrpBalance, STAmount const &amount, AccountID const &issuer, AccountID const &sender, AccountID const &receiver, bool createAsset, beast::Journal journal)
TER escrowCreatePreclaimHelper< MPTIssue >(PreclaimContext const &ctx, AccountID const &account, AccountID const &dest, STAmount const &amount)
bool canAdd(STAmount const &amt1, STAmount const &amt2)
Safely checks if two STAmount values can be added without overflow, underflow, or precision loss.
void adjustOwnerCount(ApplyView &view, std::shared_ptr< SLE > const &sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
TER escrowFinishPreclaimHelper< MPTIssue >(PreclaimContext const &ctx, AccountID const &dest, STAmount const &amount)
TER escrowLockApplyHelper< MPTIssue >(ApplyView &view, AccountID const &issuer, AccountID const &sender, STAmount const &amount, beast::Journal journal)
Rate transferRate(ReadView const &view, AccountID const &issuer)
Returns IOU issuer transfer fee as Rate.
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
TER requireAuth(ReadView const &view, Issue const &issue, AccountID const &account, AuthType authType=AuthType::Legacy)
Check if the account lacks required authorization.
static TER escrowLockApplyHelper(ApplyView &view, AccountID const &issuer, AccountID const &sender, STAmount const &amount, beast::Journal journal)
TER canTransfer(ReadView const &view, MPTIssue const &mptIssue, AccountID const &from, AccountID const &to)
Check if the destination account is allowed to receive MPT.
bool isFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
static bool checkCondition(Slice f, Slice c)
TER escrowCreatePreclaimHelper< Issue >(PreclaimContext const &ctx, AccountID const &account, AccountID const &dest, STAmount const &amount)
static NotTEC escrowCreatePreflightHelper(PreflightContext const &ctx)
AccountID const & noAccount()
A placeholder for empty accounts.
static TER escrowCancelPreclaimHelper(PreclaimContext const &ctx, AccountID const &account, STAmount const &amount)
bool isTesSuccess(TER x) noexcept
TER escrowUnlockApplyHelper< Issue >(ApplyView &view, Rate lockedRate, std::shared_ptr< SLE > const &sleDest, STAmount const &xrpBalance, STAmount const &amount, AccountID const &issuer, AccountID const &sender, AccountID const &receiver, bool createAsset, beast::Journal journal)
static TER escrowCreatePreclaimHelper(PreclaimContext const &ctx, AccountID const &account, AccountID const &dest, STAmount const &amount)
TER rippleLockEscrowMPT(ApplyView &view, AccountID const &uGrantorID, STAmount const &saAmount, beast::Journal j)
@ tecNO_LINE_INSUF_RESERVE
@ tecCRYPTOCONDITION_ERROR
@ tecINSUFFICIENT_RESERVE
@ lsfAllowTrustLineLocking
TER escrowLockApplyHelper< Issue >(ApplyView &view, AccountID const &issuer, AccountID const &sender, STAmount const &amount, beast::Journal journal)
NotTEC escrowCreatePreflightHelper< MPTIssue >(PreflightContext const &ctx)
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
constexpr HashRouterFlags SF_CF_INVALID
STAmount divideRound(STAmount const &amount, Rate const &rate, bool roundUp)
TERSubset< CanCvtToNotTEC > NotTEC
Rate const parityRate
A transfer rate signifying a 1:1 exchange.
TER rippleCredit(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, bool bCheckIssuer, beast::Journal j)
Calls static rippleCreditIOU if saAmount represents Issue.
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
A pair of SHAMap key and LedgerEntryType.
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.
Represents a transfer rate.