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 &&
151 if (sled->isFieldPresent(sfAMMID))
177 if (
ctx_.
tx[~sfCancelAfter])
179 auto const cancelAfter =
ctx_.
tx[sfCancelAfter];
181 if (closeTime.time_since_epoch().count() >= cancelAfter)
185 if (
ctx_.
tx[~sfFinishAfter])
187 auto const finishAfter =
ctx_.
tx[sfFinishAfter];
189 if (closeTime.time_since_epoch().count() >= finishAfter)
194 auto const account =
ctx_.
tx[sfAccount];
201 auto const balance =
STAmount((*sle)[sfBalance]).
xrp();
205 if (balance < reserve)
219 !
ctx_.
tx[~sfDestinationTag])
231 Keylet const escrowKeylet =
233 auto const slep = std::make_shared<SLE>(escrowKeylet);
234 (*slep)[sfAmount] =
ctx_.
tx[sfAmount];
235 (*slep)[sfAccount] = account;
236 (*slep)[~sfCondition] =
ctx_.
tx[~sfCondition];
237 (*slep)[~sfSourceTag] =
ctx_.
tx[~sfSourceTag];
238 (*slep)[sfDestination] =
ctx_.
tx[sfDestination];
239 (*slep)[~sfCancelAfter] =
ctx_.
tx[~sfCancelAfter];
240 (*slep)[~sfFinishAfter] =
ctx_.
tx[~sfFinishAfter];
241 (*slep)[~sfDestinationTag] =
ctx_.
tx[~sfDestinationTag];
251 (*slep)[sfOwnerNode] = *page;
255 if (
auto const dest =
ctx_.
tx[sfDestination]; dest !=
ctx_.
tx[sfAccount])
261 (*slep)[sfDestinationNode] = *page;
265 (*sle)[sfBalance] = (*sle)[sfBalance] -
ctx_.
tx[sfAmount];
281 auto condition = Condition::deserialize(c, ec);
285 auto fulfillment = Fulfillment::deserialize(f, ec);
289 return validate(*fulfillment, *condition);
305 auto const cb = ctx.
tx[~sfCondition];
306 auto const fb = ctx.
tx[~sfFulfillment];
310 if (
static_cast<bool>(cb) !=
static_cast<bool>(fb))
326 auto const flags = router.getFlags(
id);
331 if (!(flags & (SF_CF_INVALID | SF_CF_VALID)))
334 router.setFlags(
id, SF_CF_VALID);
336 router.setFlags(
id, SF_CF_INVALID);
351 if (
auto const fb = tx[~sfFulfillment])
353 extraFee +=
view.
fees().
base * (32 + (fb->size() / 16));
388 if ((*slep)[~sfFinishAfter] && !
after(now, (*slep)[sfFinishAfter]))
392 if ((*slep)[~sfCancelAfter] &&
after(now, (*slep)[sfCancelAfter]))
398 if ((*slep)[~sfFinishAfter] &&
400 (*slep)[sfFinishAfter])
404 if ((*slep)[~sfCancelAfter] &&
406 (*slep)[sfCancelAfter])
415 auto const cb =
ctx_.
tx[~sfCondition];
420 if (cb && !(flags & (SF_CF_INVALID | SF_CF_VALID)))
422 auto const fb =
ctx_.
tx[~sfFulfillment];
430 flags = SF_CF_INVALID;
437 if (flags & SF_CF_INVALID)
441 auto const cond = (*slep)[~sfCondition];
453 if (cond && (cond != cb))
458 AccountID const destID = (*slep)[sfDestination];
470 AccountID const account = (*slep)[sfAccount];
474 auto const page = (*slep)[sfOwnerNode];
478 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from owner.";
484 if (
auto const optPage = (*slep)[~sfDestinationNode])
489 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from recipient.";
495 (*sled)[sfBalance] = (*sled)[sfBalance] + (*slep)[sfAmount];
536 if (!(*slep)[~sfCancelAfter])
540 if (!
after(now, (*slep)[sfCancelAfter]))
546 if (!(*slep)[~sfCancelAfter] ||
548 (*slep)[sfCancelAfter])
552 AccountID const account = (*slep)[sfAccount];
556 auto const page = (*slep)[sfOwnerNode];
560 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from owner.";
566 if (
auto const optPage = (*slep)[~sfDestinationNode]; optPage)
574 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from recipient.";
581 (*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
SeqProxy getSeqProxy() const
uint256 getTransactionID() const
constexpr std::uint32_t value() 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 after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
constexpr std::uint32_t tfUniversalMask
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)