1#include <xrpld/app/tx/detail/PayChan.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/chrono.h>
5#include <xrpl/ledger/ApplyView.h>
6#include <xrpl/ledger/CredentialHelpers.h>
7#include <xrpl/ledger/View.h>
8#include <xrpl/protocol/Feature.h>
9#include <xrpl/protocol/Indexes.h>
10#include <xrpl/protocol/PayChan.h>
11#include <xrpl/protocol/PublicKey.h>
12#include <xrpl/protocol/TxFlags.h>
13#include <xrpl/protocol/XRPAmount.h>
14#include <xrpl/protocol/digest.h>
104 AccountID const src = (*slep)[sfAccount];
107 auto const page = (*slep)[sfOwnerNode];
112 <<
"Could not remove paychan from src owner directory";
119 if (
auto const page = (*slep)[~sfDestinationNode])
121 auto const dst = (*slep)[sfDestination];
126 <<
"Could not remove paychan from dst owner directory";
138 (*slep)[sfAmount] >= (*slep)[sfBalance],
139 "ripple::closeChannel : minimum channel amount");
141 (*sle)[sfBalance] + (*slep)[sfAmount] - (*slep)[sfBalance];
161 if (!
isXRP(ctx.
tx[sfAmount]) || (ctx.
tx[sfAmount] <= beast::zero))
164 if (ctx.
tx[sfAccount] == ctx.
tx[sfDestination])
176 auto const account = ctx.
tx[sfAccount];
183 auto const balance = (*sle)[sfBalance];
187 if (balance < reserve)
190 if (balance < reserve + ctx.
tx[sfAmount])
194 auto const dst = ctx.
tx[sfDestination];
202 auto const flags = sled->getFlags();
228 auto const account =
ctx_.
tx[sfAccount];
240 auto const dst =
ctx_.
tx[sfDestination];
246 Keylet const payChanKeylet =
251 (*slep)[sfAmount] =
ctx_.
tx[sfAmount];
253 (*slep)[sfBalance] =
ctx_.
tx[sfAmount].zeroed();
254 (*slep)[sfAccount] = account;
255 (*slep)[sfDestination] = dst;
256 (*slep)[sfSettleDelay] =
ctx_.
tx[sfSettleDelay];
257 (*slep)[sfPublicKey] =
ctx_.
tx[sfPublicKey];
258 (*slep)[~sfCancelAfter] =
ctx_.
tx[~sfCancelAfter];
259 (*slep)[~sfSourceTag] =
ctx_.
tx[~sfSourceTag];
260 (*slep)[~sfDestinationTag] =
ctx_.
tx[~sfDestinationTag];
276 (*slep)[sfOwnerNode] = *page;
285 (*slep)[sfDestinationNode] = *page;
289 (*sle)[sfBalance] = (*sle)[sfBalance] -
ctx_.
tx[sfAmount];
307 if (!
isXRP(ctx.
tx[sfAmount]) || (ctx.
tx[sfAmount] <= beast::zero))
321 AccountID const src = (*slep)[sfAccount];
322 auto const txAccount =
ctx_.
tx[sfAccount];
323 auto const expiration = (*slep)[~sfExpiration];
326 auto const cancelAfter = (*slep)[~sfCancelAfter];
327 auto const closeTime =
329 if ((cancelAfter && closeTime >= *cancelAfter) ||
330 (expiration && closeTime >= *expiration))
335 if (src != txAccount)
339 if (
auto extend =
ctx_.
tx[~sfExpiration])
343 (*slep)[sfSettleDelay];
344 if (expiration && *expiration < minExpiration)
345 minExpiration = *expiration;
347 if (*extend < minExpiration)
349 (*slep)[~sfExpiration] = *extend;
359 auto const balance = (*sle)[sfBalance];
363 if (balance < reserve)
366 if (balance < reserve +
ctx_.
tx[sfAmount])
371 if (
AccountID const dst = (*slep)[sfDestination];
377 (*slep)[sfAmount] = (*slep)[sfAmount] +
ctx_.
tx[sfAmount];
380 (*sle)[sfBalance] = (*sle)[sfBalance] -
ctx_.
tx[sfAmount];
404 auto const bal = ctx.
tx[~sfBalance];
405 if (bal && (!
isXRP(*bal) || *bal <= beast::zero))
408 auto const amt = ctx.
tx[~sfAmount];
409 if (amt && (!
isXRP(*amt) || *amt <= beast::zero))
412 if (bal && amt && *bal > *amt)
422 if (
auto const sig = ctx.
tx[~sfSignature])
424 if (!(ctx.
tx[~sfPublicKey] && bal))
431 auto const reqBalance = bal->xrp();
432 auto const authAmt = amt ? amt->xrp() : reqBalance;
434 if (reqBalance > authAmt)
437 Keylet const k(ltPAYCHAN, ctx.
tx[sfChannel]);
477 AccountID const src = (*slep)[sfAccount];
478 AccountID const dst = (*slep)[sfDestination];
481 auto const curExpiration = (*slep)[~sfExpiration];
483 auto const cancelAfter = (*slep)[~sfCancelAfter];
484 auto const closeTime =
486 if ((cancelAfter && closeTime >= *cancelAfter) ||
487 (curExpiration && closeTime >= *curExpiration))
492 if (txAccount != src && txAccount != dst)
497 auto const chanBalance = slep->getFieldAmount(sfBalance).xrp();
498 auto const chanFunds = slep->getFieldAmount(sfAmount).xrp();
499 auto const reqBalance =
ctx_.
tx[sfBalance].xrp();
501 if (txAccount == dst && !
ctx_.
tx[~sfSignature])
504 if (
ctx_.
tx[~sfSignature])
506 PublicKey const pk((*slep)[sfPublicKey]);
507 if (
ctx_.
tx[sfPublicKey] != pk)
511 if (reqBalance > chanFunds)
514 if (reqBalance <= chanBalance)
527 (*slep)[sfBalance] =
ctx_.
tx[sfBalance];
528 XRPAmount const reqDelta = reqBalance - chanBalance;
530 reqDelta >= beast::zero,
531 "ripple::PayChanClaim::doApply : minimum balance delta");
532 (*sled)[sfBalance] = (*sled)[sfBalance] + reqDelta;
539 if (src != txAccount)
548 if (dst == txAccount || (*slep)[sfBalance] == (*slep)[sfAmount])
552 auto const settleExpiration =
554 (*slep)[sfSettleDelay];
556 if (!curExpiration || *curExpiration > settleExpiration)
558 (*slep)[~sfExpiration] = settleExpiration;
A generic endpoint for log messages.
virtual beast::Journal journal(std::string const &name)=0
beast::Journal const journal
Writeable view to a ledger, for applying a transaction.
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 TER preclaim(PreclaimContext const &ctx)
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
static bool checkExtraFeatures(PreflightContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
static TxConsequences makeTxConsequences(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
static TxConsequences makeTxConsequences(PreflightContext const &ctx)
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).
Slice slice() const noexcept
static TER preclaim(PreclaimContext const &ctx)
Class describing the consequences to the account of applying a transaction if the transaction consume...
NotTEC checkFields(STTx const &tx, beast::Journal j)
TER valid(STTx const &tx, ReadView const &view, AccountID const &src, beast::Journal j)
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Keylet payChan(AccountID const &src, AccountID const &dst, std::uint32_t seq) noexcept
A PaymentChannel.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
static TER closeChannel(std::shared_ptr< SLE > const &slep, ApplyView &view, uint256 const &key, beast::Journal j)
bool isXRP(AccountID const &c)
constexpr std::uint32_t tfRenew
bool verify(PublicKey const &publicKey, Slice const &m, Slice const &sig, bool mustBeFullyCanonical=true) noexcept
Verify a signature on a message.
@ lsfDisallowIncomingPayChan
void adjustOwnerCount(ApplyView &view, std::shared_ptr< SLE > const &sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
TER verifyDepositPreauth(STTx const &tx, ApplyView &view, AccountID const &src, AccountID const &dst, std::shared_ptr< SLE > const &sleDst, beast::Journal j)
void serializePayChanAuthorization(Serializer &msg, uint256 const &key, XRPAmount const &amt)
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
@ tecINSUFFICIENT_RESERVE
bool isTesSuccess(TER x) noexcept
constexpr std::uint32_t tfClose
constexpr std::uint32_t tfPayChanClaimMask
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
TERSubset< CanCvtToTER > TER
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)