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} ||
96 amount <= beast::zero)
112 [&]<
typename T>(T
const&) {
113 return escrowCreatePreflightHelper<T>(ctx);
115 amount.asset().value());
121 if (amount <= beast::zero)
126 if (!ctx.
tx[~sfCancelAfter] && !ctx.
tx[~sfFinishAfter])
131 if (ctx.
tx[~sfCancelAfter] && ctx.
tx[~sfFinishAfter] &&
132 ctx.
tx[sfCancelAfter] <= ctx.
tx[sfFinishAfter])
139 if (!ctx.
tx[~sfFinishAfter] && !ctx.
tx[~sfCondition])
142 if (
auto const cb = ctx.
tx[~sfCondition])
148 auto condition = Condition::deserialize(*cb, ec);
152 <<
"Malformed condition during escrow creation: "
159 if (condition->type != Type::preimageSha256 &&
167template <Val
idIssueType T>
185 if (issuer == account)
196 auto const sleRippleState =
201 STAmount const balance = (*sleRippleState)[sfBalance];
204 if (balance > beast::zero && issuer < account)
208 if (balance < beast::zero && issuer > account)
238 if (spendableAmount <= beast::zero)
243 if (spendableAmount < amount)
247 if (!
canAdd(spendableAmount, amount))
263 if (issuer == account)
267 auto const issuanceKey =
269 auto const sleIssuance = ctx.
view.
read(issuanceKey);
279 if (sleIssuance->getAccountID(sfIssuer) != issuer)
323 if (spendableAmount <= beast::zero)
328 if (spendableAmount < amount)
358 [&]<
typename T>(T
const&) {
359 return escrowCreatePreclaimHelper<T>(
360 ctx, account, dest, amount);
362 amount.asset().value());
369template <Val
idIssueType T>
388 if (issuer == sender)
413 if (issuer == sender)
460 !
ctx_.
tx[~sfDestinationTag])
468 (*slep)[sfAmount] = amount;
470 (*slep)[~sfCondition] =
ctx_.
tx[~sfCondition];
471 (*slep)[~sfSourceTag] =
ctx_.
tx[~sfSourceTag];
472 (*slep)[sfDestination] =
ctx_.
tx[sfDestination];
473 (*slep)[~sfCancelAfter] =
ctx_.
tx[~sfCancelAfter];
474 (*slep)[~sfFinishAfter] =
ctx_.
tx[~sfFinishAfter];
475 (*slep)[~sfDestinationTag] =
ctx_.
tx[~sfDestinationTag];
486 (*slep)[sfTransferRate] = xferRate.value;
499 (*slep)[sfOwnerNode] = *page;
510 (*slep)[sfDestinationNode] = *page;
516 AccountID const issuer = amount.getIssuer();
517 if (!
isXRP(amount) && issuer !=
account_ && issuer != dest &&
524 (*slep)[sfIssuerNode] = *page;
529 (*sle)[sfBalance] = (*sle)[sfBalance] - amount;
533 [&]<
typename T>(T
const&) {
534 return escrowLockApplyHelper<T>(
537 amount.asset().value());
559 auto condition = Condition::deserialize(c, ec);
563 auto fulfillment = Fulfillment::deserialize(f, ec);
567 return validate(*fulfillment, *condition);
580 auto const cb = ctx.
tx[~sfCondition];
581 auto const fb = ctx.
tx[~sfFulfillment];
585 if (
static_cast<bool>(cb) !=
static_cast<bool>(fb))
594 auto const cb = ctx.
tx[~sfCondition];
595 auto const fb = ctx.
tx[~sfFulfillment];
602 auto const flags = router.getFlags(
id);
628 if (
auto const fb = tx[~sfFulfillment])
630 extraFee +=
view.
fees().
base * (32 + (fb->size() / 16));
636template <Val
idIssueType T>
680 auto const issuanceKey =
682 auto const sleIssuance = ctx.
view.
read(issuanceKey);
719 AccountID const dest = (*slep)[sfDestination];
720 STAmount const amount = (*slep)[sfAmount];
725 [&]<
typename T>(T
const&) {
726 return escrowFinishPreclaimHelper<T>(ctx, dest, amount);
736template <Val
idIssueType T>
765 bool const recvLow = issuer > receiver;
766 bool const senderIssuer = issuer == sender;
767 bool const receiverIssuer = issuer == receiver;
768 bool const issuerHigh = issuer > receiver;
776 if (!view.
exists(trustLineKey) && createAsset && !receiverIssuer)
779 if (
std::uint32_t const ownerCount = {sleDest->at(sfOwnerCount)};
780 xrpBalance < view.
fees().accountReserve(ownerCount + 1))
782 JLOG(journal.
trace()) <<
"Trust line does not exist. "
783 "Insufficent reserve to create line.";
805 Issue(currency, receiver),
818 if (!view.
exists(trustLineKey) && !receiverIssuer)
823 if (xferRate < lockedRate)
824 lockedRate = xferRate;
835 auto finalAmt = amount;
836 if ((!senderIssuer && !receiverIssuer) && lockedRate !=
parityRate)
839 auto const xferFee = amount.
value() -
842 finalAmt = amount.
value() - xferFee;
849 auto const sleRippleState = view.
peek(trustLineKey);
855 STAmount const lineLimit = sleRippleState->getFieldAmount(
856 issuerHigh ? sfLowLimit : sfHighLimit);
858 STAmount lineBalance = sleRippleState->getFieldAmount(sfBalance);
865 lineBalance += finalAmt;
868 if (lineLimit < lineBalance)
876 rippleCredit(view, issuer, receiver, finalAmt,
true, journal);
897 bool const senderIssuer = issuer == sender;
898 bool const receiverIssuer = issuer == receiver;
900 auto const mptID = amount.
get<
MPTIssue>().getMptID();
903 createAsset && !receiverIssuer)
905 if (
std::uint32_t const ownerCount = {sleDest->at(sfOwnerCount)};
906 xrpBalance < view.
fees().accountReserve(ownerCount + 1))
928 if (xferRate < lockedRate)
929 lockedRate = xferRate;
940 auto finalAmt = amount;
941 if ((!senderIssuer && !receiverIssuer) && lockedRate !=
parityRate)
944 auto const xferFee = amount.
value() -
947 finalAmt = amount.
value() - xferFee;
954 view.
rules().
enabled(fixTokenEscrowV1) ? amount : finalAmt,
976 if ((*slep)[~sfFinishAfter] && !
after(now, (*slep)[sfFinishAfter]))
980 if ((*slep)[~sfCancelAfter] &&
after(now, (*slep)[sfCancelAfter]))
988 auto const cb =
ctx_.
tx[~sfCondition];
996 auto const fb =
ctx_.
tx[~sfFulfillment];
1016 auto const cond = (*slep)[~sfCondition];
1028 if (cond && (cond != cb))
1033 AccountID const destID = (*slep)[sfDestination];
1043 AccountID const account = (*slep)[sfAccount];
1047 auto const page = (*slep)[sfOwnerNode];
1052 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from owner.";
1059 if (
auto const optPage = (*slep)[~sfDestinationNode])
1065 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from recipient.";
1071 STAmount const amount = slep->getFieldAmount(sfAmount);
1074 (*sled)[sfBalance] = (*sled)[sfBalance] + amount;
1080 Rate lockedRate = slep->isFieldPresent(sfTransferRate)
1084 bool const createAsset = destID ==
account_;
1086 [&]<
typename T>(T
const&) {
1087 return escrowUnlockApplyHelper<T>(
1104 if (
auto const optPage = (*slep)[~sfIssuerNode]; optPage)
1110 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from recipient.";
1137template <Val
idIssueType T>
1153 if (issuer == account)
1173 if (issuer == account)
1177 auto const issuanceKey =
1179 auto const sleIssuance = ctx.
view.
read(issuanceKey);
1186 if (
auto const ter =
1200 auto const slep = ctx.
view.
read(k);
1204 AccountID const account = (*slep)[sfAccount];
1205 STAmount const amount = (*slep)[sfAmount];
1210 [&]<
typename T>(T
const&) {
1211 return escrowCancelPreclaimHelper<T>(
1212 ctx, account, amount);
1238 if (!(*slep)[~sfCancelAfter])
1242 if (!
after(now, (*slep)[sfCancelAfter]))
1245 AccountID const account = (*slep)[sfAccount];
1249 auto const page = (*slep)[sfOwnerNode];
1254 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from owner.";
1261 if (
auto const optPage = (*slep)[~sfDestinationNode]; optPage)
1270 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from recipient.";
1277 STAmount const amount = slep->getFieldAmount(sfAmount);
1281 (*sle)[sfBalance] = (*sle)[sfBalance] + amount;
1288 bool const createAsset = account ==
account_;
1290 [&]<
typename T>(T
const&) {
1291 return escrowUnlockApplyHelper<T>(
1308 if (
auto const optPage = (*slep)[~sfIssuerNode]; optPage)
1314 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 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.
virtual void erase(std::shared_ptr< SLE > const &sle)=0
Remove a peeked SLE.
constexpr value_type const & value() const
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
static TxConsequences makeTxConsequences(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
static NotTEC preflightSigValidated(PreflightContext const &ctx)
static bool checkExtraFeatures(PreflightContext const &ctx)
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
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 std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
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 LedgerInfo const & info() const =0
Returns information about the ledger.
virtual Rules const & rules() const =0
Returns the tx processing rules.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
constexpr bool holds() const noexcept
Asset const & asset() const
constexpr TIss const & get() const
void setIssuer(AccountID const &uIssuer)
Currency const & getCurrency() const
STAmount const & value() const noexcept
AccountID const & getIssuer() const
Issue const & issue() const
bool native() const noexcept
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 mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Keylet line(AccountID const &id0, AccountID const &id1, Currency const ¤cy) noexcept
The index of a trust line for a given currency.
Keylet mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Keylet account(AccountID const &id) noexcept
AccountID root.
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.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
AccountID const & noAccount()
A placeholder for empty accounts.
TER escrowFinishPreclaimHelper< Issue >(PreclaimContext const &ctx, AccountID const &dest, STAmount const &amount)
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
TER escrowCreatePreclaimHelper< MPTIssue >(PreclaimContext const &ctx, AccountID const &account, AccountID const &dest, STAmount const &amount)
bool isXRP(AccountID const &c)
TER rippleLockEscrowMPT(ApplyView &view, AccountID const &uGrantorID, STAmount const &saAmount, beast::Journal j)
static TER escrowFinishPreclaimHelper(PreclaimContext const &ctx, 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.
TER escrowLockApplyHelper< Issue >(ApplyView &view, AccountID const &issuer, AccountID const &sender, STAmount const &amount, beast::Journal journal)
bool isDeepFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
@ lsfAllowTrustLineLocking
TER escrowCancelPreclaimHelper< MPTIssue >(PreclaimContext const &ctx, AccountID const &account, STAmount const &amount)
void adjustOwnerCount(ApplyView &view, std::shared_ptr< SLE > const &sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
static TER escrowLockApplyHelper(ApplyView &view, AccountID const &issuer, AccountID const &sender, STAmount const &amount, beast::Journal journal)
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
STAmount divideRound(STAmount const &amount, Rate const &rate, bool roundUp)
TER verifyDepositPreauth(STTx const &tx, ApplyView &view, AccountID const &src, AccountID const &dst, std::shared_ptr< SLE > const &sleDst, beast::Journal j)
bool isFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
TER rippleUnlockEscrowMPT(ApplyView &view, AccountID const &uGrantorID, AccountID const &uGranteeID, STAmount const &netAmount, STAmount const &grossAmount, beast::Journal j)
TER requireAuth(ReadView const &view, Issue const &issue, AccountID const &account, AuthType authType=AuthType::Legacy)
Check if the account lacks required authorization.
constexpr HashRouterFlags SF_CF_VALID
TER escrowCreatePreclaimHelper< Issue >(PreclaimContext const &ctx, AccountID const &account, AccountID const &dest, STAmount const &amount)
TER canTransfer(ReadView const &view, MPTIssue const &mptIssue, AccountID const &from, AccountID const &to)
Check if the destination account is allowed to receive MPT.
static bool checkCondition(Slice f, Slice c)
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)
static NotTEC escrowCreatePreflightHelper(PreflightContext const &ctx)
Rate transferRate(ReadView const &view, AccountID const &issuer)
Returns IOU issuer transfer fee as Rate.
static TER escrowCancelPreclaimHelper(PreclaimContext const &ctx, AccountID const &account, STAmount const &amount)
@ tecCRYPTOCONDITION_ERROR
@ tecNO_LINE_INSUF_RESERVE
@ tecINSUFFICIENT_RESERVE
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.
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
constexpr HashRouterFlags SF_CF_INVALID
NotTEC escrowCreatePreflightHelper< MPTIssue >(PreflightContext const &ctx)
bool isTesSuccess(TER x) noexcept
TER escrowFinishPreclaimHelper< MPTIssue >(PreclaimContext const &ctx, AccountID const &dest, STAmount const &amount)
NotTEC escrowCreatePreflightHelper< Issue >(PreflightContext const &ctx)
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
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.
TER escrowCancelPreclaimHelper< Issue >(PreclaimContext const &ctx, AccountID const &account, STAmount const &amount)
TER escrowLockApplyHelper< MPTIssue >(ApplyView &view, AccountID const &issuer, AccountID const &sender, STAmount const &amount, beast::Journal journal)
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)
bool isPseudoAccount(std::shared_ptr< SLE const > sleAcct)
TERSubset< CanCvtToNotTEC > NotTEC
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)
Rate const parityRate
A transfer rate signifying a 1:1 exchange.
static TER escrowCreatePreclaimHelper(PreclaimContext const &ctx, AccountID const &account, AccountID const &dest, STAmount const &amount)
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.