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();
227 auto const account =
ctx_.
tx[sfAccount];
239 auto const dst =
ctx_.
tx[sfDestination];
245 Keylet const payChanKeylet =
250 (*slep)[sfAmount] =
ctx_.
tx[sfAmount];
252 (*slep)[sfBalance] =
ctx_.
tx[sfAmount].zeroed();
253 (*slep)[sfAccount] = account;
254 (*slep)[sfDestination] = dst;
255 (*slep)[sfSettleDelay] =
ctx_.
tx[sfSettleDelay];
256 (*slep)[sfPublicKey] =
ctx_.
tx[sfPublicKey];
257 (*slep)[~sfCancelAfter] =
ctx_.
tx[~sfCancelAfter];
258 (*slep)[~sfSourceTag] =
ctx_.
tx[~sfSourceTag];
259 (*slep)[~sfDestinationTag] =
ctx_.
tx[~sfDestinationTag];
275 (*slep)[sfOwnerNode] = *page;
284 (*slep)[sfDestinationNode] = *page;
288 (*sle)[sfBalance] = (*sle)[sfBalance] -
ctx_.
tx[sfAmount];
306 if (!
isXRP(ctx.
tx[sfAmount]) || (ctx.
tx[sfAmount] <= beast::zero))
320 AccountID const src = (*slep)[sfAccount];
321 auto const txAccount =
ctx_.
tx[sfAccount];
322 auto const expiration = (*slep)[~sfExpiration];
325 auto const cancelAfter = (*slep)[~sfCancelAfter];
326 auto const closeTime =
328 if ((cancelAfter && closeTime >= *cancelAfter) ||
329 (expiration && closeTime >= *expiration))
334 if (src != txAccount)
338 if (
auto extend =
ctx_.
tx[~sfExpiration])
342 (*slep)[sfSettleDelay];
343 if (expiration && *expiration < minExpiration)
344 minExpiration = *expiration;
346 if (*extend < minExpiration)
348 (*slep)[~sfExpiration] = *extend;
358 auto const balance = (*sle)[sfBalance];
362 if (balance < reserve)
365 if (balance < reserve +
ctx_.
tx[sfAmount])
370 if (
AccountID const dst = (*slep)[sfDestination];
376 (*slep)[sfAmount] = (*slep)[sfAmount] +
ctx_.
tx[sfAmount];
379 (*sle)[sfBalance] = (*sle)[sfBalance] -
ctx_.
tx[sfAmount];
403 auto const bal = ctx.
tx[~sfBalance];
404 if (bal && (!
isXRP(*bal) || *bal <= beast::zero))
407 auto const amt = ctx.
tx[~sfAmount];
408 if (amt && (!
isXRP(*amt) || *amt <= beast::zero))
411 if (bal && amt && *bal > *amt)
421 if (
auto const sig = ctx.
tx[~sfSignature])
423 if (!(ctx.
tx[~sfPublicKey] && bal))
430 auto const reqBalance = bal->xrp();
431 auto const authAmt = amt ? amt->xrp() : reqBalance;
433 if (reqBalance > authAmt)
436 Keylet const k(ltPAYCHAN, ctx.
tx[sfChannel]);
476 AccountID const src = (*slep)[sfAccount];
477 AccountID const dst = (*slep)[sfDestination];
480 auto const curExpiration = (*slep)[~sfExpiration];
482 auto const cancelAfter = (*slep)[~sfCancelAfter];
483 auto const closeTime =
485 if ((cancelAfter && closeTime >= *cancelAfter) ||
486 (curExpiration && closeTime >= *curExpiration))
491 if (txAccount != src && txAccount != dst)
496 auto const chanBalance = slep->getFieldAmount(sfBalance).xrp();
497 auto const chanFunds = slep->getFieldAmount(sfAmount).xrp();
498 auto const reqBalance =
ctx_.
tx[sfBalance].xrp();
500 if (txAccount == dst && !
ctx_.
tx[~sfSignature])
503 if (
ctx_.
tx[~sfSignature])
505 PublicKey const pk((*slep)[sfPublicKey]);
506 if (
ctx_.
tx[sfPublicKey] != pk)
510 if (reqBalance > chanFunds)
513 if (reqBalance <= chanBalance)
526 (*slep)[sfBalance] =
ctx_.
tx[sfBalance];
527 XRPAmount const reqDelta = reqBalance - chanBalance;
529 reqDelta >= beast::zero,
530 "ripple::PayChanClaim::doApply : minimum balance delta");
531 (*sled)[sfBalance] = (*sled)[sfBalance] + reqDelta;
538 if (src != txAccount)
547 if (dst == txAccount || (*slep)[sfBalance] == (*slep)[sfAmount])
551 auto const settleExpiration =
553 (*slep)[sfSettleDelay];
555 if (!curExpiration || *curExpiration > settleExpiration)
557 (*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)