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/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/SField.h>
14#include <xrpl/protocol/STAmount.h>
15#include <xrpl/protocol/STLedgerEntry.h>
16#include <xrpl/protocol/STTx.h>
17#include <xrpl/protocol/TER.h>
18#include <xrpl/protocol/XRPAmount.h>
19#include <xrpl/tx/Transactor.h>
20#include <xrpl/tx/applySteps.h>
21
22namespace xrpl {
23
26{
27 return TxConsequences{ctx.tx, ctx.tx[sfAmount].xrp()};
28}
29
32{
33 if (ctx.rules.enabled(fixCleanup3_2_0) && ctx.tx[sfChannel] == beast::kZero)
34 return temMALFORMED;
35
36 if (!isXRP(ctx.tx[sfAmount]) || (ctx.tx[sfAmount] <= beast::kZero))
37 return temBAD_AMOUNT;
38
39 return tesSUCCESS;
40}
41
42TER
44{
45 Keylet const k(ltPAYCHAN, ctx_.tx[sfChannel]);
46 auto const slep = ctx_.view().peek(k);
47 if (!slep)
48 return tecNO_ENTRY;
49
50 AccountID const src = (*slep)[sfAccount];
51 auto const txAccount = ctx_.tx[sfAccount];
52 auto const curExpiration = (*slep)[~sfExpiration];
53
54 if (isChannelExpired(ctx_.view(), (*slep)[~sfCancelAfter]) ||
55 isChannelExpired(ctx_.view(), curExpiration))
56 {
57 return closeChannel(slep, ctx_.view(), k.key, ctx_.registry.get().getJournal("View"));
58 }
59
60 if (src != txAccount)
61 {
62 // only the owner can add funds or extend
63 return tecNO_PERMISSION;
64 }
65
66 if (auto newExpiration = ctx_.tx[~sfExpiration])
67 {
68 auto minExpiration = saturatingAdd(
69 ctx_.view().rules(),
70 ctx_.view().header().parentCloseTime.time_since_epoch().count(),
71 (*slep)[sfSettleDelay]);
72 if (curExpiration && *curExpiration < minExpiration)
73 minExpiration = *curExpiration;
74
75 if (*newExpiration < minExpiration)
76 {
77 return ctx_.view().rules().enabled(fixCleanup3_2_0) ? TER{tecNO_PERMISSION}
79 }
80 (*slep)[~sfExpiration] = *newExpiration;
81 ctx_.view().update(slep);
82 }
83
84 auto const sle = ctx_.view().peek(keylet::account(txAccount));
85 if (!sle)
86 return tefINTERNAL; // LCOV_EXCL_LINE
87
88 {
89 // Check reserve and funds availability
90 auto const balance = (*sle)[sfBalance];
91 auto const reserve = ctx_.view().fees().accountReserve((*sle)[sfOwnerCount]);
92
93 if (balance < reserve)
95
96 if (balance < reserve + ctx_.tx[sfAmount])
97 return tecUNFUNDED;
98 }
99
100 // do not allow adding funds if dst does not exist
101 if (AccountID const dst = (*slep)[sfDestination]; !ctx_.view().read(keylet::account(dst)))
102 {
103 return tecNO_DST;
104 }
105
106 (*slep)[sfAmount] = (*slep)[sfAmount] + ctx_.tx[sfAmount];
107 ctx_.view().update(slep);
108
109 (*sle)[sfBalance] = (*sle)[sfBalance] - ctx_.tx[sfAmount];
110 ctx_.view().update(sle);
111
112 return tesSUCCESS;
113}
114
115void
117{
118 // No transaction-specific invariants yet (future work).
119}
120
121bool
123 STTx const&,
124 TER,
125 XRPAmount,
126 ReadView const&,
127 beast::Journal const&)
128{
129 // No transaction-specific invariants yet (future work).
130 return true;
131}
132} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
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:31
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:171
std::shared_ptr< STLedgerEntry const > const & const_ref
ApplyContext & ctx_
Definition Transactor.h:116
Class describing the consequences to the account of applying a transaction if the transaction consume...
Definition applySteps.h:38
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:186
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:70
@ tefINTERNAL
Definition TER.h:163
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:594
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:28
@ temBAD_EXPIRATION
Definition TER.h:77
@ temMALFORMED
Definition TER.h:73
@ temBAD_AMOUNT
Definition TER.h:75
TERSubset< CanCvtToTER > TER
Definition TER.h:634
@ tecNO_ENTRY
Definition TER.h:304
@ tecINSUFFICIENT_RESERVE
Definition TER.h:305
@ tecNO_PERMISSION
Definition TER.h:303
@ tecNO_DST
Definition TER.h:288
@ tecUNFUNDED
Definition TER.h:293
@ tesSUCCESS
Definition TER.h:240
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:19
uint256 key
Definition Keylet.h:20
State information when preflighting a tx.
Definition Transactor.h:18