xrpld
Loading...
Searching...
No Matches
PaymentChannelFund.cpp
1#include <xrpl/tx/transactors/payment_channel/PaymentChannelFund.h>
2
3#include <xrpl/beast/utility/Journal.h>
4#include <xrpl/beast/utility/Zero.h>
5#include <xrpl/ledger/ApplyView.h>
6#include <xrpl/ledger/ReadView.h>
7#include <xrpl/ledger/helpers/AccountRootHelpers.h>
8#include <xrpl/ledger/helpers/PaymentChannelHelpers.h>
9#include <xrpl/protocol/AccountID.h>
10#include <xrpl/protocol/Feature.h>
11#include <xrpl/protocol/Indexes.h>
12#include <xrpl/protocol/Keylet.h>
13#include <xrpl/protocol/LedgerFormats.h>
14#include <xrpl/protocol/SField.h>
15#include <xrpl/protocol/STAmount.h>
16#include <xrpl/protocol/STLedgerEntry.h>
17#include <xrpl/protocol/STTx.h>
18#include <xrpl/protocol/TER.h>
19#include <xrpl/protocol/XRPAmount.h>
20#include <xrpl/tx/Transactor.h>
21#include <xrpl/tx/applySteps.h>
22
23namespace xrpl {
24
27{
28 return TxConsequences{ctx.tx, ctx.tx[sfAmount].xrp()};
29}
30
33{
34 if (ctx.rules.enabled(fixCleanup3_2_0) && ctx.tx[sfChannel] == beast::kZero)
35 return temMALFORMED;
36
37 if (!isXRP(ctx.tx[sfAmount]) || (ctx.tx[sfAmount] <= beast::kZero))
38 return temBAD_AMOUNT;
39
40 return tesSUCCESS;
41}
42
43TER
45{
46 Keylet const k(ltPAYCHAN, ctx_.tx[sfChannel]);
47 auto const slep = ctx_.view().peek(k);
48 if (!slep)
49 return tecNO_ENTRY;
50
51 AccountID const src = (*slep)[sfAccount];
52 auto const txAccount = ctx_.tx[sfAccount];
53 auto const curExpiration = (*slep)[~sfExpiration];
54
55 if (isChannelExpired(ctx_.view(), (*slep)[~sfCancelAfter]) ||
56 isChannelExpired(ctx_.view(), curExpiration))
57 {
58 return closeChannel(slep, ctx_.view(), k.key, ctx_.registry.get().getJournal("View"));
59 }
60
61 if (src != txAccount)
62 {
63 // only the owner can add funds or extend
64 return tecNO_PERMISSION;
65 }
66
67 if (auto newExpiration = ctx_.tx[~sfExpiration])
68 {
69 auto minExpiration = saturatingAdd(
70 ctx_.view().rules(),
71 ctx_.view().header().parentCloseTime.time_since_epoch().count(),
72 (*slep)[sfSettleDelay]);
73 if (curExpiration && *curExpiration < minExpiration)
74 minExpiration = *curExpiration;
75
76 if (*newExpiration < minExpiration)
77 {
78 return ctx_.view().rules().enabled(fixCleanup3_2_0) ? TER{tecNO_PERMISSION}
80 }
81 (*slep)[~sfExpiration] = *newExpiration;
82 ctx_.view().update(slep);
83 }
84
85 auto const sle = ctx_.view().peek(keylet::account(txAccount));
86 if (!sle)
87 return tefINTERNAL; // LCOV_EXCL_LINE
88
89 {
90 // Check reserve and funds availability
91 STAmount const balance = (*sle)[sfBalance];
92 if (auto const ret = checkReserve(ctx_.getApplyViewContext(), sle, balance.xrp(), {}, j_);
93 !isTesSuccess(ret))
94 return ret;
95
96 // After locking sfAmount in the channel, the source must still meet
97 // its own reserve floor. We compare directly (rather than via
98 // checkReserve) because that helper diverts to the sponsor's balance
99 // when a sponsor is present and would ignore the source's post-lock
100 // balance entirely. Funding an existing channel adds no owned object,
101 // so there is no owner-count delta.
102 if (balance < accountReserve(ctx_.view(), sle, j_) + ctx_.tx[sfAmount])
103 return tecUNFUNDED;
104 }
105
106 // do not allow adding funds if dst does not exist
107 if (AccountID const dst = (*slep)[sfDestination]; !ctx_.view().read(keylet::account(dst)))
108 {
109 return tecNO_DST;
110 }
111
112 (*slep)[sfAmount] = (*slep)[sfAmount] + ctx_.tx[sfAmount];
113 ctx_.view().update(slep);
114
115 (*sle)[sfBalance] = (*sle)[sfBalance] - ctx_.tx[sfAmount];
116 ctx_.view().update(sle);
117
118 return tesSUCCESS;
119}
120
121void
123{
124 // No transaction-specific invariants yet (future work).
125}
126
127bool
129 STTx const&,
130 TER,
131 XRPAmount,
132 ReadView const&,
133 beast::Journal const&)
134{
135 // No transaction-specific invariants yet (future work).
136 return true;
137}
138} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
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.
static TxConsequences makeTxConsequences(PreflightContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
static NotTEC preflight(PreflightContext const &ctx)
A view into a ledger.
Definition ReadView.h:41
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:180
XRPAmount xrp() const
Definition STAmount.cpp:271
std::shared_ptr< STLedgerEntry const > const & const_ref
beast::Journal const j_
Definition Transactor.h:155
ApplyContext & ctx_
Definition Transactor.h:153
Class describing the consequences to the account of applying a transaction if the transaction consume...
Definition applySteps.h:52
constexpr Zero kZero
Definition Zero.h:30
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:198
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
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)
Definition AccountID.h:84
@ tefINTERNAL
Definition TER.h:165
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:607
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.
Definition AccountID.h:34
TER checkReserve(ApplyViewContext ctx, SLE::const_ref accSle, XRPAmount accBalance, SLE::const_ref sponsorSle, Adjustment adj, beast::Journal j, TER insufReserveCode=tecINSUFFICIENT_RESERVE)
Check if an account has sufficient reserve.
@ temBAD_EXPIRATION
Definition TER.h:79
@ temMALFORMED
Definition TER.h:75
@ temBAD_AMOUNT
Definition TER.h:77
bool isTesSuccess(TER x) noexcept
Definition TER.h:676
TERSubset< CanCvtToTER > TER
Definition TER.h:647
@ tecNO_ENTRY
Definition TER.h:309
@ tecNO_PERMISSION
Definition TER.h:308
@ tecNO_DST
Definition TER.h:293
@ tecUNFUNDED
Definition TER.h:298
XRPAmount accountReserve(ReadView const &view, SLE::const_ref sle, beast::Journal j, Adjustment adj={})
Returns the account reserve, in drops.
@ tesSUCCESS
Definition TER.h:245
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
uint256 key
Definition Keylet.h:21
State information when preflighting a tx.
Definition Transactor.h:38