1#include <xrpl/tx/transactors/escrow/EscrowFinish.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Slice.h>
5#include <xrpl/basics/chrono.h>
6#include <xrpl/conditions/Condition.h>
7#include <xrpl/conditions/Fulfillment.h>
8#include <xrpl/core/HashRouter.h>
9#include <xrpl/ledger/ApplyView.h>
10#include <xrpl/ledger/ReadView.h>
11#include <xrpl/ledger/View.h>
12#include <xrpl/ledger/entries/EscrowEntry.h>
13#include <xrpl/ledger/helpers/AccountRootHelpers.h>
14#include <xrpl/ledger/helpers/CredentialHelpers.h>
15#include <xrpl/ledger/helpers/EscrowHelpers.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/Rate.h>
22#include <xrpl/protocol/SField.h>
23#include <xrpl/protocol/STAmount.h>
24#include <xrpl/protocol/STLedgerEntry.h>
25#include <xrpl/protocol/STTx.h>
26#include <xrpl/protocol/SeqProxy.h>
27#include <xrpl/protocol/TER.h>
28#include <xrpl/protocol/XRPAmount.h>
29#include <xrpl/tx/Transactor.h>
61 return validate(*fulfillment, *condition);
79 auto const cb = ctx.
tx[~sfCondition];
80 auto const fb = ctx.
tx[~sfFulfillment];
84 if (
static_cast<bool>(cb) !=
static_cast<bool>(fb))
86 JLOG(ctx.
j.
debug()) <<
"Condition != Fulfillment";
90 if (
auto const allowance = ctx.
tx[~sfGas]; allowance)
99 JLOG(ctx.
j.
debug()) <<
"Gas too large: " << *allowance;
110 auto const cb = ctx.
tx[~sfCondition];
111 auto const fb = ctx.
tx[~sfFulfillment];
115 auto& router = ctx.
registry.get().getHashRouter();
118 auto flags = router.getFlags(
id);
150 JLOG(ctx.
j.
debug()) <<
"Malformed condition during escrow finish: " << ec.
message();
167 if (
auto const fb = tx[~sfFulfillment])
169 extraFee +=
view.fees().base * (32 + (fb->size() / 16));
171 if (
auto const allowance = tx[~sfGas]; allowance)
178 extraFee += allowanceFee;
204 if (escrow->isFieldPresent(sfBytecode))
208 JLOG(ctx.
j.
debug()) <<
"Bytecode requires Gas";
218 JLOG(ctx.
j.
debug()) <<
"WASM runtime deactivated by fee voting";
222 if (ctx.
tx[sfGas] > fees.gasLimit)
224 JLOG(ctx.
j.
debug()) <<
"Gas over voted limit: " << ctx.
tx[sfGas];
232 JLOG(ctx.
j.
debug()) <<
"Bytecode not present, "
240 AccountID const dest = (*escrow)[sfDestination];
241 STAmount const amount = (*escrow)[sfAmount];
246 [&]<
typename T>(T
const&) {
249 amount.
asset().value());
265 if (
ctx_.view().rules().enabled(featureTokenEscrow) ||
266 ctx_.view().rules().enabled(featureSmartEscrow))
274 auto const now =
ctx_.view().header().parentCloseTime;
277 if ((*escrow)[~sfFinishAfter] && !
after(now, (*escrow)[sfFinishAfter]))
281 if ((*escrow)[~sfCancelAfter] &&
after(now, (*escrow)[sfCancelAfter]))
284 AccountID const destID = (*escrow)[sfDestination];
286 if (
ctx_.view().rules().enabled(featureSmartEscrow))
300 auto const id =
ctx_.tx.getTransactionID();
301 auto flags =
ctx_.registry.get().getHashRouter().getFlags(
id);
303 auto const cb =
ctx_.tx[~sfCondition];
311 auto const fb =
ctx_.tx[~sfFulfillment];
325 ctx_.registry.get().getHashRouter().setFlags(
id, flags);
335 auto const cond = (*escrow)[~sfCondition];
347 if (cond && (cond != cb))
351 if (!
ctx_.view().rules().enabled(featureSmartEscrow))
364 if ((*escrow)[~sfBytecode])
375 JLOG(
j_.debug()) <<
"EscrowFinish: no WASM engine to run the escrow's bytecode";
379 AccountID const account = (*escrow)[sfAccount];
383 auto const page = (*escrow)[sfOwnerNode];
387 JLOG(
j_.fatal()) <<
"Unable to delete Escrow from owner.";
394 if (
auto const optPage = (*escrow)[~sfDestinationNode])
399 JLOG(
j_.fatal()) <<
"Unable to delete Escrow from recipient.";
410 bool const recycleReserve =
411 ctx_.view().rules().enabled(featureSponsor) ||
ctx_.view().rules().enabled(fixCleanup3_4_0);
415 ctx_.view(), account, escrow.mutableRawSle(), reserveToSubtract,
ctx_.journal);
418 STAmount const amount = escrow->getFieldAmount(sfAmount);
422 (*sled)[sfBalance] = (*sled)[sfBalance] + amount;
426 if (!
ctx_.view().rules().enabled(featureTokenEscrow))
429 Rate lockedRate = escrow->isFieldPresent(sfTransferRate)
430 ?
xrpl::Rate(escrow->getFieldU32(sfTransferRate))
433 bool const createAsset = destID ==
accountID_;
435 [&]<
typename T>(T
const&) {
437 ctx_.getApplyViewContext(),
448 amount.
asset().value());
453 if (
auto const optPage = (*escrow)[~sfIssuerNode]; optPage)
458 JLOG(
j_.fatal()) <<
"Unable to delete Escrow from recipient.";
465 ctx_.view().update(sled);
470 ctx_.view(), account, escrow.mutableRawSle(), reserveToSubtract,
ctx_.journal);
A generic endpoint for log messages.
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
static bool checkExtraFeatures(PreflightContext 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 TER preclaim(PreclaimContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::ConstRef before, SLE::ConstRef after) override
Inspect a single ledger entry modified by this transaction.
static NotTEC preflight(PreflightContext const &ctx)
static NotTEC preflightSigValidated(PreflightContext const &ctx)
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
bool enabled(UInt256 const &feature) const
Returns true if a feature is enabled.
Asset const & asset() const
AccountID const & getIssuer() const
std::shared_ptr< STLedgerEntry const > const & ConstRef
bool isFieldPresent(SField const &field) const
UInt256 getTransactionID() const
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
An immutable linear range of bytes.
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
AccountID const accountID_
static std::unique_ptr< Condition > deserialize(Slice s, std::error_code &ec)
Load a condition from its binary form.
NotTEC checkFields(STTx const &tx, Rules const &rules, beast::Journal j)
TER valid(STTx const &tx, ReadView const &view, AccountID const &src, beast::Journal j)
bool validate(Fulfillment const &f, Condition const &c, Slice m)
Verify if the given message satisfies the fulfillment.
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.
constexpr std::uint32_t microDropsPerDrop
bool isXRP(AccountID const &c)
int32_t calculateAdditionalReserve(T const &finishFunction)
Owner count an escrow costs.
@ tefBYTECODE_NOT_INCLUDED
TER escrowUnlockApplyHelper(ApplyViewContext ctx, Rate lockedRate, SLE::Ref sleDest, XRPAmount xrpBalance, STAmount const &amount, AccountID const &issuer, AccountID const &sender, AccountID const &receiver, bool createAsset, beast::Journal journal)
bool isBytecodeExecutionDisabled(Fees const &fees)
constexpr HashRouterFlags kSfCfInvalid
EscrowEntry< ReadView > EscrowEntryR
constexpr HashRouterFlags kSfCfValid
TERSubset< CanCvtToNotTEC > NotTEC
void decreaseOwnerCountForObject(ApplyView &view, SLE::Ref accountSle, SLE::Ref objectSle, std::uint32_t count, beast::Journal j)
Decrease owner-count fields for an existing ledger object.
TER escrowUnlockPreclaimHelper(ReadView const &view, AccountID const &account, STAmount const &amount, bool checkFreeze=true)
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
static bool checkCondition(Slice f, Slice c)
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
bool isTesSuccess(TER x) noexcept
TERSubset< CanCvtToTER > TER
@ tecCRYPTOCONDITION_ERROR
constexpr std::uint32_t kMaxGasLimit
Hard protocol bounds on the Feature Extension fee settings.
TER verifyDepositPreauth(STTx const &tx, ApplyView &view, AccountID const &src, AccountID const &dst, SLE::ConstRef sleDst, beast::Journal j)
constexpr bool any(HashRouterFlags flags)
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.
std::reference_wrapper< ServiceRegistry > registry
Represents a transfer rate.
static std::unique_ptr< Fulfillment > deserialize(Slice s, std::error_code &ec)
Load a fulfillment from its binary form.