20#include <xrpld/app/misc/CredentialHelpers.h>
21#include <xrpld/app/misc/HashRouter.h>
22#include <xrpld/app/tx/detail/Escrow.h>
23#include <xrpld/conditions/Condition.h>
24#include <xrpld/conditions/Fulfillment.h>
25#include <xrpld/ledger/ApplyView.h>
26#include <xrpld/ledger/View.h>
28#include <xrpl/basics/Log.h>
29#include <xrpl/basics/chrono.h>
30#include <xrpl/protocol/Feature.h>
31#include <xrpl/protocol/Indexes.h>
32#include <xrpl/protocol/TxFlags.h>
33#include <xrpl/protocol/XRPAmount.h>
38#define SF_CF_INVALID SF_PRIVATE5
39#define SF_CF_VALID SF_PRIVATE6
97 if (ctx.
tx[sfAmount] <= beast::zero)
101 if (!ctx.
tx[~sfCancelAfter] && !ctx.
tx[~sfFinishAfter])
106 if (ctx.
tx[~sfCancelAfter] && ctx.
tx[~sfFinishAfter] &&
107 ctx.
tx[sfCancelAfter] <= ctx.
tx[sfFinishAfter])
116 if (!ctx.
tx[~sfFinishAfter] && !ctx.
tx[~sfCondition])
120 if (
auto const cb = ctx.
tx[~sfCondition])
126 auto condition = Condition::deserialize(*cb, ec);
130 <<
"Malformed condition during escrow creation: "
137 if (condition->type != Type::preimageSha256 &&
182 if (
ctx_.
tx[~sfCancelAfter])
184 auto const cancelAfter =
ctx_.
tx[sfCancelAfter];
186 if (closeTime.time_since_epoch().count() >= cancelAfter)
190 if (
ctx_.
tx[~sfFinishAfter])
192 auto const finishAfter =
ctx_.
tx[sfFinishAfter];
194 if (closeTime.time_since_epoch().count() >= finishAfter)
199 auto const account =
ctx_.
tx[sfAccount];
206 auto const balance =
STAmount((*sle)[sfBalance]).
xrp();
210 if (balance < reserve)
224 !
ctx_.
tx[~sfDestinationTag])
237 auto const slep = std::make_shared<SLE>(escrowKeylet);
238 (*slep)[sfAmount] =
ctx_.
tx[sfAmount];
239 (*slep)[sfAccount] = account;
240 (*slep)[~sfCondition] =
ctx_.
tx[~sfCondition];
241 (*slep)[~sfSourceTag] =
ctx_.
tx[~sfSourceTag];
242 (*slep)[sfDestination] =
ctx_.
tx[sfDestination];
243 (*slep)[~sfCancelAfter] =
ctx_.
tx[~sfCancelAfter];
244 (*slep)[~sfFinishAfter] =
ctx_.
tx[~sfFinishAfter];
245 (*slep)[~sfDestinationTag] =
ctx_.
tx[~sfDestinationTag];
255 (*slep)[sfOwnerNode] = *page;
259 if (
auto const dest =
ctx_.
tx[sfDestination]; dest !=
ctx_.
tx[sfAccount])
265 (*slep)[sfDestinationNode] = *page;
269 (*sle)[sfBalance] = (*sle)[sfBalance] -
ctx_.
tx[sfAmount];
285 auto condition = Condition::deserialize(c, ec);
289 auto fulfillment = Fulfillment::deserialize(f, ec);
293 return validate(*fulfillment, *condition);
309 auto const cb = ctx.
tx[~sfCondition];
310 auto const fb = ctx.
tx[~sfFulfillment];
314 if (
static_cast<bool>(cb) !=
static_cast<bool>(fb))
330 auto const flags = router.getFlags(
id);
335 if (!(flags & (SF_CF_INVALID | SF_CF_VALID)))
338 router.setFlags(
id, SF_CF_VALID);
340 router.setFlags(
id, SF_CF_INVALID);
355 if (
auto const fb = tx[~sfFulfillment])
357 extraFee +=
view.
fees().
base * (32 + (fb->size() / 16));
392 if ((*slep)[~sfFinishAfter] && !
after(now, (*slep)[sfFinishAfter]))
396 if ((*slep)[~sfCancelAfter] &&
after(now, (*slep)[sfCancelAfter]))
402 if ((*slep)[~sfFinishAfter] &&
404 (*slep)[sfFinishAfter])
408 if ((*slep)[~sfCancelAfter] &&
410 (*slep)[sfCancelAfter])
419 auto const cb =
ctx_.
tx[~sfCondition];
424 if (cb && !(flags & (SF_CF_INVALID | SF_CF_VALID)))
426 auto const fb =
ctx_.
tx[~sfFulfillment];
434 flags = SF_CF_INVALID;
441 if (flags & SF_CF_INVALID)
445 auto const cond = (*slep)[~sfCondition];
457 if (cond && (cond != cb))
462 AccountID const destID = (*slep)[sfDestination];
474 AccountID const account = (*slep)[sfAccount];
478 auto const page = (*slep)[sfOwnerNode];
482 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from owner.";
488 if (
auto const optPage = (*slep)[~sfDestinationNode])
493 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from recipient.";
499 (*sled)[sfBalance] = (*sled)[sfBalance] + (*slep)[sfAmount];
540 if (!(*slep)[~sfCancelAfter])
544 if (!
after(now, (*slep)[sfCancelAfter]))
550 if (!(*slep)[~sfCancelAfter] ||
552 (*slep)[sfCancelAfter])
556 AccountID const account = (*slep)[sfAccount];
560 auto const page = (*slep)[sfOwnerNode];
564 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from owner.";
570 if (
auto const optPage = (*slep)[~sfDestinationNode]; optPage)
578 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from recipient.";
585 (*sle)[sfBalance] = (*sle)[sfBalance] + (*slep)[sfAmount];
virtual HashRouter & getHashRouter()=0
beast::Journal const journal
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.
static NotTEC preflight(PreflightContext 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 XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
int getFlags(uint256 const &key)
bool setFlags(uint256 const &key, int flags)
Set the flags on a hash.
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 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.
bool isFieldPresent(SField const &field) const
std::uint32_t getFlags() 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)
static TER preclaim(PreclaimContext const &ctx)
Class describing the consequences to the account of applying a transaction if the transaction consume...
NotTEC checkFields(PreflightContext const &ctx)
TER valid(PreclaimContext const &ctx, AccountID const &src)
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.
bool isXRP(AccountID const &c)
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
static bool adjustOwnerCount(ApplyContext &ctx, int count)
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
static bool checkCondition(Slice f, Slice c)
@ tecCRYPTOCONDITION_ERROR
@ tecINSUFFICIENT_RESERVE
TER verifyDepositPreauth(ApplyContext &ctx, AccountID const &src, AccountID const &dst, std::shared_ptr< SLE > const &sleDst)
bool isTesSuccess(TER x) noexcept
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
constexpr std::uint32_t tfUniversalMask
bool isPseudoAccount(std::shared_ptr< SLE const > sleAcct)
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.
T time_since_epoch(T... args)