1#include <xrpl/tx/transactors/payment_channel/PaymentChannelClaim.h>
3#include <xrpl/beast/utility/Zero.h>
4#include <xrpl/beast/utility/instrumentation.h>
5#include <xrpl/ledger/ApplyView.h>
6#include <xrpl/ledger/helpers/CredentialHelpers.h>
7#include <xrpl/ledger/helpers/PaymentChannelHelpers.h>
8#include <xrpl/protocol/AccountID.h>
9#include <xrpl/protocol/Feature.h>
10#include <xrpl/protocol/Indexes.h>
11#include <xrpl/protocol/Keylet.h>
12#include <xrpl/protocol/LedgerFormats.h>
13#include <xrpl/protocol/PayChan.h>
14#include <xrpl/protocol/PublicKey.h>
15#include <xrpl/protocol/SField.h>
16#include <xrpl/protocol/STAmount.h>
17#include <xrpl/protocol/STLedgerEntry.h>
18#include <xrpl/protocol/STTx.h>
19#include <xrpl/protocol/Serializer.h>
20#include <xrpl/protocol/TER.h>
21#include <xrpl/protocol/TxFlags.h>
22#include <xrpl/protocol/XRPAmount.h>
23#include <xrpl/tx/Transactor.h>
39 return tfPaymentChannelClaimMask;
45 if (ctx.
rules.
enabled(fixCleanup3_2_0) && ctx.
tx[sfChannel] == beast::kZero)
48 auto const bal = ctx.
tx[~sfBalance];
49 if (bal && (!
isXRP(*bal) || *bal <= beast::kZero))
52 auto const amt = ctx.
tx[~sfAmount];
53 if (amt && (!
isXRP(*amt) || *amt <= beast::kZero))
56 if (bal && amt && *bal > *amt)
64 if (
auto const sig = ctx.
tx[~sfSignature])
66 if (!(ctx.
tx[~sfPublicKey] && bal))
73 auto const reqBalance = bal->xrp();
74 auto const authAmt = amt ? amt->xrp() : reqBalance;
76 if (reqBalance > authAmt)
79 Keylet const k(ltPAYCHAN, ctx.
tx[sfChannel]);
112 Keylet const k(ltPAYCHAN,
ctx_.tx[sfChannel]);
113 auto const slep =
ctx_.view().peek(k);
117 AccountID const src = (*slep)[sfAccount];
118 AccountID const dst = (*slep)[sfDestination];
121 auto const curExpiration = (*slep)[~sfExpiration];
128 if (txAccount != src && txAccount != dst)
131 if (
ctx_.tx[~sfBalance])
133 auto const chanBalance = slep->getFieldAmount(sfBalance).xrp();
134 auto const chanFunds = slep->getFieldAmount(sfAmount).xrp();
135 auto const reqBalance =
ctx_.tx[sfBalance].xrp();
137 if (txAccount == dst && !
ctx_.tx[~sfSignature])
143 if (
ctx_.tx[~sfSignature])
145 PublicKey const pk((*slep)[sfPublicKey]);
146 if (
ctx_.tx[sfPublicKey] != pk)
153 if (reqBalance > chanFunds)
156 if (reqBalance <= chanBalance)
171 (*slep)[sfBalance] =
ctx_.tx[sfBalance];
172 XRPAmount const reqDelta = reqBalance - chanBalance;
174 reqDelta >= beast::kZero,
"xrpl::PaymentChannelClaim::doApply : minimum balance delta");
175 (*sled)[sfBalance] = (*sled)[sfBalance] + reqDelta;
176 ctx_.view().update(sled);
177 ctx_.view().update(slep);
180 if (
ctx_.tx.isFlag(tfRenew))
182 if (src != txAccount)
184 (*slep)[~sfExpiration] = std::nullopt;
185 ctx_.view().update(slep);
188 if (
ctx_.tx.isFlag(tfClose))
191 if (dst == txAccount || (*slep)[sfBalance] == (*slep)[sfAmount])
196 ctx_.view().header().parentCloseTime.time_since_epoch().count(),
197 (*slep)[sfSettleDelay]);
199 if (!curExpiration || *curExpiration > settleExpiration)
201 (*slep)[~sfExpiration] = settleExpiration;
202 ctx_.view().update(slep);
A generic endpoint for log messages.
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
static bool checkExtraFeatures(PreflightContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
virtual Rules const & rules() const =0
Returns the tx processing rules.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
std::shared_ptr< STLedgerEntry const > const & const_ref
bool isFlag(std::uint32_t) const
bool isFieldPresent(SField const &field) const
Slice slice() const noexcept
static TER preclaim(PreclaimContext const &ctx)
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.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
bool isChannelExpired(ApplyView const &view, std::optional< std::uint32_t > timeField)
Determine whether a payment channel time field represents an expired time.
TER closeChannel(SLE::ref slep, ApplyView &view, uint256 const &key, beast::Journal j)
Close a payment channel and return its remaining funds to the channel owner.
bool isXRP(AccountID const &c)
bool verify(PublicKey const &publicKey, Slice const &m, Slice const &sig) noexcept
Verify a signature on a message.
void serializePayChanAuthorization(Serializer &msg, uint256 const &key, XRPAmount const &amt)
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
TERSubset< CanCvtToNotTEC > NotTEC
uint32_t saturatingAdd(Rules const &rules, uint32_t const lhs, uint32_t const rhs)
Add two uint32_t values with saturation at UINT32_MAX.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
bool isTesSuccess(TER x) noexcept
TERSubset< CanCvtToTER > TER
TER verifyDepositPreauth(STTx const &tx, ApplyView &view, AccountID const &src, AccountID const &dst, SLE::const_ref sleDst, beast::Journal j)
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.