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
109 if (ctx.
tx[sfAmount] <= beast::zero)
113 if (!ctx.
tx[~sfCancelAfter] && !ctx.
tx[~sfFinishAfter])
118 if (ctx.
tx[~sfCancelAfter] && ctx.
tx[~sfFinishAfter] &&
119 ctx.
tx[sfCancelAfter] <= ctx.
tx[sfFinishAfter])
128 if (!ctx.
tx[~sfFinishAfter] && !ctx.
tx[~sfCondition])
132 if (
auto const cb = ctx.
tx[~sfCondition])
138 auto condition = Condition::deserialize(*cb, ec);
142 <<
"Malformed condition during escrow creation: "
149 if (condition->type != Type::preimageSha256 &&
163 if (sled->isFieldPresent(sfAMMID))
189 if (
ctx_.
tx[~sfCancelAfter])
191 auto const cancelAfter =
ctx_.
tx[sfCancelAfter];
193 if (closeTime.time_since_epoch().count() >= cancelAfter)
197 if (
ctx_.
tx[~sfFinishAfter])
199 auto const finishAfter =
ctx_.
tx[sfFinishAfter];
201 if (closeTime.time_since_epoch().count() >= finishAfter)
206 auto const account =
ctx_.
tx[sfAccount];
213 auto const balance =
STAmount((*sle)[sfBalance]).
xrp();
217 if (balance < reserve)
231 !
ctx_.
tx[~sfDestinationTag])
243 Keylet const escrowKeylet =
245 auto const slep = std::make_shared<SLE>(escrowKeylet);
246 (*slep)[sfAmount] =
ctx_.
tx[sfAmount];
247 (*slep)[sfAccount] = account;
248 (*slep)[~sfCondition] =
ctx_.
tx[~sfCondition];
249 (*slep)[~sfSourceTag] =
ctx_.
tx[~sfSourceTag];
250 (*slep)[sfDestination] =
ctx_.
tx[sfDestination];
251 (*slep)[~sfCancelAfter] =
ctx_.
tx[~sfCancelAfter];
252 (*slep)[~sfFinishAfter] =
ctx_.
tx[~sfFinishAfter];
253 (*slep)[~sfDestinationTag] =
ctx_.
tx[~sfDestinationTag];
263 (*slep)[sfOwnerNode] = *page;
267 if (
auto const dest =
ctx_.
tx[sfDestination]; dest !=
ctx_.
tx[sfAccount])
273 (*slep)[sfDestinationNode] = *page;
277 (*sle)[sfBalance] = (*sle)[sfBalance] -
ctx_.
tx[sfAmount];
293 auto condition = Condition::deserialize(c, ec);
297 auto fulfillment = Fulfillment::deserialize(f, ec);
301 return validate(*fulfillment, *condition);
317 auto const cb = ctx.
tx[~sfCondition];
318 auto const fb = ctx.
tx[~sfFulfillment];
322 if (
static_cast<bool>(cb) !=
static_cast<bool>(fb))
338 auto const flags = router.getFlags(
id);
343 if (!(flags & (SF_CF_INVALID | SF_CF_VALID)))
346 router.setFlags(
id, SF_CF_VALID);
348 router.setFlags(
id, SF_CF_INVALID);
363 if (
auto const fb = tx[~sfFulfillment])
365 extraFee +=
view.
fees().
base * (32 + (fb->size() / 16));
400 if ((*slep)[~sfFinishAfter] && !
after(now, (*slep)[sfFinishAfter]))
404 if ((*slep)[~sfCancelAfter] &&
after(now, (*slep)[sfCancelAfter]))
410 if ((*slep)[~sfFinishAfter] &&
412 (*slep)[sfFinishAfter])
416 if ((*slep)[~sfCancelAfter] &&
418 (*slep)[sfCancelAfter])
427 auto const cb =
ctx_.
tx[~sfCondition];
432 if (cb && !(flags & (SF_CF_INVALID | SF_CF_VALID)))
434 auto const fb =
ctx_.
tx[~sfFulfillment];
442 flags = SF_CF_INVALID;
449 if (flags & SF_CF_INVALID)
453 auto const cond = (*slep)[~sfCondition];
465 if (cond && (cond != cb))
470 AccountID const destID = (*slep)[sfDestination];
482 AccountID const account = (*slep)[sfAccount];
486 auto const page = (*slep)[sfOwnerNode];
490 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from owner.";
496 if (
auto const optPage = (*slep)[~sfDestinationNode])
501 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from recipient.";
507 (*sled)[sfBalance] = (*sled)[sfBalance] + (*slep)[sfAmount];
548 if (!(*slep)[~sfCancelAfter])
552 if (!
after(now, (*slep)[sfCancelAfter]))
558 if (!(*slep)[~sfCancelAfter] ||
560 (*slep)[sfCancelAfter])
564 AccountID const account = (*slep)[sfAccount];
568 auto const page = (*slep)[sfOwnerNode];
572 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from owner.";
578 if (
auto const optPage = (*slep)[~sfDestinationNode]; optPage)
586 JLOG(
j_.
fatal()) <<
"Unable to delete Escrow from recipient.";
593 (*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)
constexpr std::uint32_t tfUniversalMask
static bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
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)