xrpld
Loading...
Searching...
No Matches
EscrowCancel.cpp
1#include <xrpl/tx/transactors/escrow/EscrowCancel.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/ledger/ApplyView.h>
5#include <xrpl/ledger/View.h>
6#include <xrpl/ledger/entries/EscrowEntry.h>
7#include <xrpl/ledger/helpers/AccountRootHelpers.h>
8#include <xrpl/ledger/helpers/EscrowHelpers.h>
9#include <xrpl/protocol/AccountID.h>
10#include <xrpl/protocol/Concepts.h>
11#include <xrpl/protocol/Feature.h>
12#include <xrpl/protocol/Indexes.h>
13#include <xrpl/protocol/Rate.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/SeqProxy.h>
19#include <xrpl/protocol/TER.h>
20#include <xrpl/protocol/XRPAmount.h>
21#include <xrpl/tx/Transactor.h>
22
23#include <variant>
24
25namespace xrpl {
26
32
33TER
35{
36 if (ctx.view.rules().enabled(featureTokenEscrow))
37 {
38 auto const seqProxy = SeqProxy::rawSequence(ctx.tx[sfOfferSequence]);
39 EscrowEntryR const escrow(ctx.tx[sfOwner], seqProxy, ctx.view);
40 if (!escrow)
41 return tecNO_TARGET;
42
43 AccountID const account = (*escrow)[sfAccount];
44 STAmount const amount = (*escrow)[sfAmount];
45
46 if (!isXRP(amount))
47 {
48 if (auto const ret = std::visit(
49 [&]<typename T>(T const&) {
50 return escrowUnlockPreclaimHelper<T>(ctx.view, account, amount, false);
51 },
52 amount.asset().value());
53 !isTesSuccess(ret))
54 return ret;
55 }
56 }
57 return tesSUCCESS;
58}
59
60TER
62{
63 auto const seqProxy = SeqProxy::rawSequence(ctx_.tx[sfOfferSequence]);
64 EscrowEntryW escrow(ctx_.tx[sfOwner], seqProxy, ctx_.view(), j_);
65 if (!escrow)
66 {
67 if (ctx_.view().rules().enabled(featureTokenEscrow))
68 return tecINTERNAL; // LCOV_EXCL_LINE
69
70 return tecNO_TARGET;
71 }
72
73 auto const now = ctx_.view().header().parentCloseTime;
74
75 // No cancel time specified: can't execute at all.
76 if (!(*escrow)[~sfCancelAfter])
77 return tecNO_PERMISSION;
78
79 // Too soon: can't execute before the cancel time.
80 if (!after(now, (*escrow)[sfCancelAfter]))
81 return tecNO_PERMISSION;
82
83 AccountID const account = (*escrow)[sfAccount];
84
85 // Remove escrow from owner directory
86 {
87 auto const page = (*escrow)[sfOwnerNode];
88 if (!ctx_.view().dirRemove(keylet::ownerDir(account), page, escrow.key(), true))
89 {
90 // LCOV_EXCL_START
91 JLOG(j_.fatal()) << "Unable to delete Escrow from owner.";
92 return tefBAD_LEDGER;
93 // LCOV_EXCL_STOP
94 }
95 }
96
97 // Remove escrow from recipient's owner directory, if present.
98 if (auto const optPage = (*escrow)[~sfDestinationNode]; optPage)
99 {
100 if (!ctx_.view().dirRemove(
101 keylet::ownerDir((*escrow)[sfDestination]), *optPage, escrow.key(), true))
102 {
103 // LCOV_EXCL_START
104 JLOG(j_.fatal()) << "Unable to delete Escrow from recipient.";
105 return tefBAD_LEDGER;
106 // LCOV_EXCL_STOP
107 }
108 }
109
110 auto const sle = ctx_.view().peek(keylet::account(account));
111 STAmount const amount = escrow->getFieldAmount(sfAmount);
112
113 auto const reserveToSubtract = calculateAdditionalReserve((*escrow)[~sfBytecode]);
114
115 // The return can re-create a holding the owner deleted while the escrow
116 // was pending; the removed escrow must not be counted against its reserve.
117 bool const recycleReserve = ctx_.view().rules().enabled(fixCleanup3_4_0);
118 if (recycleReserve)
119 {
121 ctx_.view(), sle, escrow.mutableRawSle(), reserveToSubtract, ctx_.journal);
122 }
123
124 // Transfer amount back to the owner
125 if (isXRP(amount))
126 {
127 (*sle)[sfBalance] = (*sle)[sfBalance] + amount;
128 }
129 else
130 {
131 if (!ctx_.view().rules().enabled(featureTokenEscrow))
132 return temDISABLED; // LCOV_EXCL_LINE
133
134 auto const issuer = amount.getIssuer();
135 bool const createAsset = account == accountID_;
136 if (auto const ret = std::visit(
137 [&]<typename T>(T const&) {
139 ctx_.getApplyViewContext(),
141 ctx_.view().rules().enabled(fixCleanup3_2_0) ? sle : escrow.mutableRawSle(),
143 amount,
144 issuer,
145 account, // sender and receiver are the same
146 account,
147 createAsset,
148 j_);
149 },
150 amount.asset().value());
151 !isTesSuccess(ret))
152 return ret; // LCOV_EXCL_LINE
153
154 // Remove escrow from issuers owner directory, if present.
155 if (auto const optPage = (*escrow)[~sfIssuerNode]; optPage)
156 {
157 if (!ctx_.view().dirRemove(keylet::ownerDir(issuer), *optPage, escrow.key(), true))
158 {
159 // LCOV_EXCL_START
160 JLOG(j_.fatal()) << "Unable to delete Escrow from recipient.";
161 return tefBAD_LEDGER;
162 // LCOV_EXCL_STOP
163 }
164 }
165 }
166
167 if (!recycleReserve)
168 {
170 ctx_.view(), sle, escrow.mutableRawSle(), reserveToSubtract, ctx_.journal);
171 }
172
173 // Remove escrow from ledger
174 escrow.erase();
175
176 return tesSUCCESS;
177}
178
179void
181{
182 // No transaction-specific invariants yet (future work).
183}
184
185bool
187 STTx const&,
188 TER,
189 XRPAmount,
190 ReadView const&,
191 beast::Journal const&)
192{
193 // No transaction-specific invariants yet (future work).
194 return true;
195}
196} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
void visitInvariantEntry(bool isDelete, SLE::ConstRef before, SLE::ConstRef after) override
Inspect a single ledger entry modified by this transaction.
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext 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.
TER doApply() override
A view into a ledger.
Definition ReadView.h:41
virtual Rules const & rules() const =0
Returns the tx processing rules.
bool enabled(UInt256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:182
Asset const & asset() const
Definition STAmount.h:496
AccountID const & getIssuer() const
Definition STAmount.h:516
std::shared_ptr< STLedgerEntry const > const & ConstRef
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition SeqProxy.h:62
beast::Journal const j_
Definition Transactor.h:164
AccountID const accountID_
Definition Transactor.h:166
XRPAmount preFeeBalance_
Definition Transactor.h:167
ApplyContext & ctx_
Definition Transactor.h:162
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:415
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool isXRP(AccountID const &c)
Definition AccountID.h:84
int32_t calculateAdditionalReserve(T const &finishFunction)
Owner count an escrow costs.
@ tefBAD_LEDGER
Definition TER.h:164
TER escrowUnlockApplyHelper(ApplyViewContext ctx, Rate lockedRate, SLE::Ref sleDest, XRPAmount xrpBalance, STAmount const &amount, AccountID const &issuer, AccountID const &sender, AccountID const &receiver, bool createAsset, beast::Journal journal)
EscrowEntry< ReadView > EscrowEntryR
Definition EscrowEntry.h:34
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:617
void decreaseOwnerCountForObject(ApplyView &view, SLE::Ref accountSle, SLE::Ref objectSle, std::uint32_t count, beast::Journal j)
Decrease owner-count fields for an existing ledger object.
TER escrowUnlockPreclaimHelper(ReadView const &view, AccountID const &account, STAmount const &amount, bool checkFreeze=true)
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition View.cpp:668
Rate const kParityRate
A transfer rate signifying a 1:1 exchange.
EscrowEntry< ApplyView > EscrowEntryW
Definition EscrowEntry.h:35
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
@ temDISABLED
Definition TER.h:102
bool isTesSuccess(TER x) noexcept
Definition TER.h:686
TERSubset< CanCvtToTER > TER
Definition TER.h:657
@ tecNO_TARGET
Definition TER.h:315
@ tecINTERNAL
Definition TER.h:321
@ tecNO_PERMISSION
Definition TER.h:316
@ tesSUCCESS
Definition TER.h:253
UInt256 key
Definition Keylet.h:21
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:92
ReadView const & view
Definition Transactor.h:95
State information when preflighting a tx.
Definition Transactor.h:39
T visit(T... args)