xrpld
Loading...
Searching...
No Matches
PaymentChannelHelpers.cpp
1#include <xrpl/ledger/helpers/PaymentChannelHelpers.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/beast/utility/Journal.h>
6#include <xrpl/beast/utility/instrumentation.h>
7#include <xrpl/ledger/ApplyView.h>
8#include <xrpl/ledger/View.h>
9#include <xrpl/ledger/helpers/AccountRootHelpers.h>
10#include <xrpl/protocol/AccountID.h>
11#include <xrpl/protocol/Feature.h>
12#include <xrpl/protocol/Indexes.h>
13#include <xrpl/protocol/SField.h>
14#include <xrpl/protocol/STLedgerEntry.h>
15#include <xrpl/protocol/TER.h>
16
17#include <algorithm>
18#include <cstdint>
19#include <limits>
20#include <optional>
21
22namespace xrpl {
23
24TER
26{
27 AccountID const src = (*slep)[sfAccount];
28 // Remove PayChan from owner directory
29 {
30 auto const page = (*slep)[sfOwnerNode];
31 if (!view.dirRemove(keylet::ownerDir(src), page, key, true))
32 {
33 // LCOV_EXCL_START
34 JLOG(j.fatal()) << "Could not remove paychan from src owner directory";
35 return tefBAD_LEDGER;
36 // LCOV_EXCL_STOP
37 }
38 }
39
40 // Remove PayChan from recipient's owner directory, if present.
41 if (auto const page = (*slep)[~sfDestinationNode])
42 {
43 auto const dst = (*slep)[sfDestination];
44 if (!view.dirRemove(keylet::ownerDir(dst), *page, key, true))
45 {
46 // LCOV_EXCL_START
47 JLOG(j.fatal()) << "Could not remove paychan from dst owner directory";
48 return tefBAD_LEDGER;
49 // LCOV_EXCL_STOP
50 }
51 }
52
53 // Transfer amount back to owner, decrement owner count
54 auto const sle = view.peek(keylet::account(src));
55 if (!sle)
56 return tefINTERNAL; // LCOV_EXCL_LINE
57
58 XRPL_ASSERT(
59 (*slep)[sfAmount] >= (*slep)[sfBalance], "xrpl::closeChannel : minimum channel amount");
60 (*sle)[sfBalance] = (*sle)[sfBalance] + (*slep)[sfAmount] - (*slep)[sfBalance];
61 adjustOwnerCount(view, sle, -1, j);
62 view.update(sle);
63
64 // Remove PayChan from ledger
65 view.erase(slep);
66 return tesSUCCESS;
67}
68
69uint32_t
70saturatingAdd(Rules const& rules, uint32_t const lhs, uint32_t const rhs)
71{
72 if (rules.enabled(fixCleanup3_2_0))
73 {
74 static constexpr auto kUint32Max =
75 static_cast<uint64_t>(std::numeric_limits<uint32_t>::max());
76 uint64_t const saturatedResult = std::min(uint64_t{lhs} + rhs, kUint32Max);
77 return static_cast<uint32_t>(saturatedResult);
78 }
79
80 return lhs + rhs;
81}
82
83bool
85{
86 if (!timeField)
87 return false;
88 if (view.rules().enabled(fixCleanup3_2_0))
89 return after(view.header().parentCloseTime, *timeField);
90 return view.header().parentCloseTime.time_since_epoch().count() >= *timeField;
91}
92
93} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
Stream fatal() const
Definition Journal.h:321
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:118
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
bool dirRemove(Keylet const &directory, std::uint64_t page, uint256 const &key, bool keepRoot)
Remove an entry from a directory.
virtual void erase(SLE::ref sle)=0
Remove a peeked SLE.
virtual void update(SLE::ref sle)=0
Indicate changes to a peeked SLE.
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual LedgerHeader const & header() const =0
Returns information about the ledger.
Rules controlling protocol behavior.
Definition Rules.h:33
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:171
std::shared_ptr< STLedgerEntry > const & ref
T max(T... args)
T min(T... args)
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:357
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.
@ tefBAD_LEDGER
Definition TER.h:160
@ tefINTERNAL
Definition TER.h:163
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition View.cpp:554
uint32_t saturatingAdd(Rules const &rules, uint32_t const lhs, uint32_t const rhs)
Add two uint32_t values with saturation at UINT32_MAX.
void adjustOwnerCount(ApplyView &view, SLE::ref sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
TERSubset< CanCvtToTER > TER
Definition TER.h:634
BaseUInt< 256 > uint256
Definition base_uint.h:562
@ tesSUCCESS
Definition TER.h:240
NetClock::time_point parentCloseTime
T time_since_epoch(T... args)