1#include <xrpl/tx/transactors/escrow/EscrowCreate.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/chrono.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/conditions/Condition.h>
7#include <xrpl/ledger/ApplyView.h>
8#include <xrpl/ledger/ReadView.h>
9#include <xrpl/ledger/View.h>
10#include <xrpl/ledger/entries/EscrowEntry.h>
11#include <xrpl/ledger/helpers/AccountRootHelpers.h>
12#include <xrpl/ledger/helpers/DirectoryHelpers.h>
13#include <xrpl/ledger/helpers/EscrowHelpers.h>
14#include <xrpl/ledger/helpers/SponsorHelpers.h>
15#include <xrpl/ledger/helpers/TokenHelpers.h>
16#include <xrpl/protocol/AccountID.h>
17#include <xrpl/protocol/Concepts.h>
18#include <xrpl/protocol/Feature.h>
19#include <xrpl/protocol/Fees.h>
20#include <xrpl/protocol/Indexes.h>
21#include <xrpl/protocol/Issue.h>
22#include <xrpl/protocol/Keylet.h>
23#include <xrpl/protocol/LedgerFormats.h>
24#include <xrpl/protocol/MPTAmount.h>
25#include <xrpl/protocol/MPTIssue.h>
26#include <xrpl/protocol/Protocol.h>
27#include <xrpl/protocol/Rate.h>
28#include <xrpl/protocol/SField.h>
29#include <xrpl/protocol/STAmount.h>
30#include <xrpl/protocol/STLedgerEntry.h>
31#include <xrpl/protocol/STTx.h>
32#include <xrpl/protocol/TER.h>
33#include <xrpl/protocol/UintTypes.h>
34#include <xrpl/protocol/XRPAmount.h>
35#include <xrpl/tx/Transactor.h>
36#include <xrpl/tx/applySteps.h>
83 auto const amount = ctx.
tx[sfAmount];
102template <Val
idIssueType T>
127 auto const amount = ctx.
tx[sfAmount];
142 txnFees += 9 *
view.fees().base + 5 * tx[sfBytecode].size();
158 amount.
asset().value());
169 if (!ctx.
tx[~sfCancelAfter] && !ctx.
tx[~sfFinishAfter])
174 if (ctx.
tx[~sfCancelAfter] && ctx.
tx[~sfFinishAfter] &&
175 ctx.
tx[sfCancelAfter] <= ctx.
tx[sfFinishAfter])
185 if (!ctx.
tx[~sfFinishAfter] && !ctx.
tx[~sfCondition] && !ctx.
tx[~sfBytecode])
187 JLOG(ctx.
j.
debug()) <<
"Must have at least one of FinishAfter, "
188 "Condition, or Bytecode.";
192 if (
auto const cb = ctx.
tx[~sfCondition])
201 JLOG(ctx.
j.
debug()) <<
"Malformed condition during escrow creation: " << ec.
message();
210 JLOG(ctx.
j.
debug()) <<
"EscrowCreate with Data requires Bytecode";
216 JLOG(ctx.
j.
debug()) <<
"EscrowCreate.Data bad size " << data.size();
228 JLOG(ctx.
j.
debug()) <<
"EscrowCreate.Bytecode bad size " << code.size();
260 [&]<
typename T>(T
const&) {
263 amount.
asset().value());
275 JLOG(ctx.
j.
debug()) <<
"WASM runtime deactivated by fee voting";
280 if (code.size() > fees.bytecodeSizeLimit)
282 JLOG(ctx.
j.
debug()) <<
"EscrowCreate.Bytecode over voted limit " << code.size();
297 auto const closeTime =
ctx_.view().header().parentCloseTime;
299 if (
ctx_.tx[~sfCancelAfter] &&
after(closeTime,
ctx_.tx[sfCancelAfter]))
302 if (
ctx_.tx[~sfFinishAfter] &&
after(closeTime,
ctx_.tx[sfFinishAfter]))
313 auto const balance = sle->getFieldAmount(sfBalance).xrp();
320 ctx_.getApplyViewContext(), sle, balance, {.ownerCountDelta = reserveToAdd},
j_);
342 {.ownerCountDelta = getTxReserveSponsorID(ctx_.tx) ? 0 : reserveToAdd});
343 if (balance -
STAmount(amount).xrp() < sourceReserve)
352 if (sled->isFlag(lsfRequireDestTag) && !
ctx_.tx[~sfDestinationTag])
359 Keylet const escrowKeylet = escrow.keylet();
361 (*escrow)[sfAmount] = amount;
363 (*escrow)[~sfCondition] =
ctx_.tx[~sfCondition];
364 (*escrow)[~sfSourceTag] =
ctx_.tx[~sfSourceTag];
365 (*escrow)[sfDestination] =
ctx_.tx[sfDestination];
366 (*escrow)[~sfCancelAfter] =
ctx_.tx[~sfCancelAfter];
367 (*escrow)[~sfFinishAfter] =
ctx_.tx[~sfFinishAfter];
368 (*escrow)[~sfDestinationTag] =
ctx_.tx[~sfDestinationTag];
369 (*escrow)[~sfBytecode] =
ctx_.tx[~sfBytecode];
370 (*escrow)[~sfData] =
ctx_.tx[~sfData];
372 if (
ctx_.view().rules().enabled(fixIncludeKeyletFields))
374 (*escrow)[sfSequence] =
ctx_.tx.getSeqProxy().value();
377 if (
ctx_.view().rules().enabled(featureTokenEscrow) && !
isXRP(amount))
381 (*escrow)[sfTransferRate] = xferRate.value;
388 auto page =
ctx_.view().dirInsert(
392 (*escrow)[sfOwnerNode] = *page;
403 (*escrow)[sfDestinationNode] = *page;
416 (*escrow)[sfIssuerNode] = *page;
422 (*sle)[sfBalance] = (*sle)[sfBalance] - amount;
427 [&]<
typename T>(T
const&) {
430 amount.
asset().value());
440 ctx_.view().update(sle);
A generic endpoint for log messages.
static bool checkExtraFeatures(PreflightContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
void visitInvariantEntry(bool isDelete, SLE::ConstRef before, SLE::ConstRef after) override
Inspect a single ledger entry modified by this transaction.
static TER preclaim(PreclaimContext const &ctx)
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
static TxConsequences makeTxConsequences(PreflightContext const &ctx)
A currency issued by an account.
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 SLE::const_pointer 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.
constexpr bool holds() const noexcept
constexpr TIss const & get() const
bool native() const noexcept
Asset const & asset() const
AccountID const & getIssuer() const
std::shared_ptr< STLedgerEntry const > const & ConstRef
Blob getFieldVL(SField const &field) const
bool isFieldPresent(SField const &field) const
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
AccountID const accountID_
Class describing the consequences to the account of applying a transaction if the transaction consume...
static std::unique_ptr< Condition > deserialize(Slice s, std::error_code &ec)
Load a condition from its binary form.
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Keylet account(AccountID const &id) noexcept
AccountID root.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
bool isXRP(AccountID const &c)
int32_t calculateAdditionalReserve(T const &finishFunction)
Owner count an escrow costs.
NotTEC escrowCreatePreflightHelper< Issue >(PreflightContext const &ctx)
constexpr std::size_t kMaxWasmDataLength
Maximum length of a Data field in Escrow object that can be updated by WASM code.
TER escrowLockApplyHelper(ApplyView &view, AccountID const &issuer, AccountID const &sender, STAmount const &amount, beast::Journal journal)
TER checkReserve(ApplyViewContext ctx, SLE::ConstRef accSle, XRPAmount accBalance, SLE::ConstRef sponsorSle, Adjustment adj, beast::Journal j, TER insufReserveCode=tecINSUFFICIENT_RESERVE)
Check if an account has sufficient reserve.
void increaseOwnerCount(ApplyView &view, SLE::Ref accountSle, SLE::Ref sponsorSle, std::uint32_t count, beast::Journal j)
Increase owner-count fields when the caller supplies the sponsor.
void addSponsorToLedgerEntry(SLE::Ref sle, SLE::ConstRef sponsorSle, SF_ACCOUNT const &field=sfSponsor)
Stamp a reserve sponsor onto a ledger entry using an explicit sponsor SLE.
TERSubset< CanCvtToNotTEC > NotTEC
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?
Rate const kParityRate
A transfer rate signifying a 1:1 exchange.
EscrowEntry< ApplyView > EscrowEntryW
bool isPseudoAccount(SLE::const_pointer sleAcct)
Returns true if and only if sleAcct is a pseudo-account of any kind (i.e.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
static NotTEC escrowCreatePreflightHelper(PreflightContext const &ctx)
TER escrowLockPreclaimHelper(ReadView const &view, AccountID const &account, AccountID const &dest, STAmount const &amount, beast::Journal j)
Validate that account may lock amount of a token for later delivery to dest.
bool isTesSuccess(TER x) noexcept
TERSubset< CanCvtToTER > TER
XRPAmount accountReserve(ReadView const &view, SLE::ConstRef sle, beast::Journal j, Adjustment adj={})
Returns the account reserve, in drops.
NotTEC escrowCreatePreflightHelper< MPTIssue >(PreflightContext const &ctx)
constexpr std::uint32_t kMaxBytecodeSizeLimit
bool isBytecodeUploadDisabled(Fees const &fees)
Smart escrow kill switches, driven by the voted FeeSettings.
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
std::function< void(SLE::Ref)> describeOwnerDir(AccountID const &account)
Returns a function that sets the owner on a directory SLE.
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.