rippled
Loading...
Searching...
No Matches
DeleteAccount.cpp
1#include <xrpld/app/tx/detail/DID.h>
2#include <xrpld/app/tx/detail/DelegateSet.h>
3#include <xrpld/app/tx/detail/DeleteAccount.h>
4#include <xrpld/app/tx/detail/DeleteOracle.h>
5#include <xrpld/app/tx/detail/DepositPreauth.h>
6#include <xrpld/app/tx/detail/NFTokenUtils.h>
7#include <xrpld/app/tx/detail/SetSignerList.h>
8
9#include <xrpl/basics/Log.h>
10#include <xrpl/basics/mulDiv.h>
11#include <xrpl/beast/utility/instrumentation.h>
12#include <xrpl/ledger/CredentialHelpers.h>
13#include <xrpl/ledger/View.h>
14#include <xrpl/protocol/Feature.h>
15#include <xrpl/protocol/Indexes.h>
16#include <xrpl/protocol/Protocol.h>
17#include <xrpl/protocol/TxFlags.h>
18#include <xrpl/protocol/Units.h>
19
20namespace xrpl {
21
22bool
24{
25 if (ctx.tx.isFieldPresent(sfCredentialIDs) && !ctx.rules.enabled(featureCredentials))
26 return false;
27
28 return true;
29}
30
33{
34 if (ctx.tx[sfAccount] == ctx.tx[sfDestination])
35 // An account cannot be deleted and give itself the resulting XRP.
36 return temDST_IS_SRC;
37
38 if (auto const err = credentials::checkFields(ctx.tx, ctx.j); !isTesSuccess(err))
39 return err;
40
41 return tesSUCCESS;
42}
43
46{
47 // The fee required for AccountDelete is one owner reserve.
49}
50
51namespace {
52// Define a function pointer type that can be used to delete ledger node types.
53using DeleterFuncPtr = TER (*)(
54 Application& app,
55 ApplyView& view,
56 AccountID const& account,
57 uint256 const& delIndex,
58 std::shared_ptr<SLE> const& sleDel,
60
61// Local function definitions that provides signature compatibility.
62TER
64 Application& app,
65 ApplyView& view,
66 AccountID const& account,
67 uint256 const& delIndex,
68 std::shared_ptr<SLE> const& sleDel,
70{
71 return offerDelete(view, sleDel, j);
72}
73
74TER
76 Application& app,
77 ApplyView& view,
78 AccountID const& account,
79 uint256 const& delIndex,
80 std::shared_ptr<SLE> const& sleDel,
82{
83 return SetSignerList::removeFromLedger(app, view, account, j);
84}
85
86TER
87removeTicketFromLedger(
88 Application&,
89 ApplyView& view,
90 AccountID const& account,
91 uint256 const& delIndex,
94{
95 return Transactor::ticketDelete(view, account, delIndex, j);
96}
97
98TER
99removeDepositPreauthFromLedger(
100 Application&,
101 ApplyView& view,
102 AccountID const&,
103 uint256 const& delIndex,
106{
107 return DepositPreauth::removeFromLedger(view, delIndex, j);
108}
109
110TER
111removeNFTokenOfferFromLedger(
112 Application& app,
113 ApplyView& view,
114 AccountID const& account,
115 uint256 const& delIndex,
116 std::shared_ptr<SLE> const& sleDel,
118{
119 if (!nft::deleteTokenOffer(view, sleDel))
120 return tefBAD_LEDGER; // LCOV_EXCL_LINE
121
122 return tesSUCCESS;
123}
124
125TER
126removeDIDFromLedger(
127 Application& app,
128 ApplyView& view,
129 AccountID const& account,
130 uint256 const& delIndex,
131 std::shared_ptr<SLE> const& sleDel,
133{
134 return DIDDelete::deleteSLE(view, sleDel, account, j);
135}
136
137TER
138removeOracleFromLedger(
139 Application&,
140 ApplyView& view,
141 AccountID const& account,
142 uint256 const&,
143 std::shared_ptr<SLE> const& sleDel,
145{
146 return DeleteOracle::deleteOracle(view, sleDel, account, j);
147}
148
149TER
150removeCredentialFromLedger(
151 Application&,
152 ApplyView& view,
153 AccountID const&,
154 uint256 const&,
155 std::shared_ptr<SLE> const& sleDel,
157{
158 return credentials::deleteSLE(view, sleDel, j);
159}
160
161TER
162removeDelegateFromLedger(
163 Application& app,
164 ApplyView& view,
165 AccountID const& account,
166 uint256 const& delIndex,
167 std::shared_ptr<SLE> const& sleDel,
169{
170 return DelegateSet::deleteDelegate(view, sleDel, account, j);
171}
172
173// Return nullptr if the LedgerEntryType represents an obligation that can't
174// be deleted. Otherwise return the pointer to the function that can delete
175// the non-obligation
176DeleterFuncPtr
177nonObligationDeleter(LedgerEntryType t)
178{
179 switch (t)
180 {
181 case ltOFFER:
182 return offerDelete;
183 case ltSIGNER_LIST:
185 case ltTICKET:
186 return removeTicketFromLedger;
187 case ltDEPOSIT_PREAUTH:
188 return removeDepositPreauthFromLedger;
189 case ltNFTOKEN_OFFER:
190 return removeNFTokenOfferFromLedger;
191 case ltDID:
192 return removeDIDFromLedger;
193 case ltORACLE:
194 return removeOracleFromLedger;
195 case ltCREDENTIAL:
196 return removeCredentialFromLedger;
197 case ltDELEGATE:
198 return removeDelegateFromLedger;
199 default:
200 return nullptr;
201 }
202}
203
204} // namespace
205
206TER
208{
209 AccountID const account{ctx.tx[sfAccount]};
210 AccountID const dst{ctx.tx[sfDestination]};
211
212 auto sleDst = ctx.view.read(keylet::account(dst));
213
214 if (!sleDst)
215 return tecNO_DST;
216
217 if ((*sleDst)[sfFlags] & lsfRequireDestTag && !ctx.tx[~sfDestinationTag])
218 return tecDST_TAG_NEEDED;
219
220 // If credentials are provided - check them anyway
221 if (auto const err = credentials::valid(ctx.tx, ctx.view, account, ctx.j); !isTesSuccess(err))
222 return err;
223
224 // if credentials then postpone auth check to doApply, to check for expired
225 // credentials
226 if (!ctx.tx.isFieldPresent(sfCredentialIDs))
227 {
228 // Check whether the destination account requires deposit authorization.
229 if (sleDst->getFlags() & lsfDepositAuth)
230 {
231 if (!ctx.view.exists(keylet::depositPreauth(dst, account)))
232 return tecNO_PERMISSION;
233 }
234 }
235
236 auto sleAccount = ctx.view.read(keylet::account(account));
237 XRPL_ASSERT(sleAccount, "xrpl::DeleteAccount::preclaim : non-null account");
238 if (!sleAccount)
239 return terNO_ACCOUNT;
240
241 // If an issuer has any issued NFTs resident in the ledger then it
242 // cannot be deleted.
243 if ((*sleAccount)[~sfMintedNFTokens] != (*sleAccount)[~sfBurnedNFTokens])
244 return tecHAS_OBLIGATIONS;
245
246 // If the account owns any NFTs it cannot be deleted.
247 Keylet const first = keylet::nftpage_min(account);
248 Keylet const last = keylet::nftpage_max(account);
249
250 auto const cp = ctx.view.read(Keylet(ltNFTOKEN_PAGE, ctx.view.succ(first.key, last.key.next()).value_or(last.key)));
251 if (cp)
252 return tecHAS_OBLIGATIONS;
253
254 // We don't allow an account to be deleted if its sequence number
255 // is within 256 of the current ledger. This prevents replay of old
256 // transactions if this account is resurrected after it is deleted.
257 //
258 // We look at the account's Sequence rather than the transaction's
259 // Sequence in preparation for Tickets.
260 constexpr std::uint32_t seqDelta{255};
261 if ((*sleAccount)[sfSequence] + seqDelta > ctx.view.seq())
262 return tecTOO_SOON;
263
264 // We don't allow an account to be deleted if
265 // <FirstNFTokenSequence + MintedNFTokens> is within 256 of the
266 // current ledger. This is to prevent having duplicate NFTokenIDs after
267 // account re-creation.
268 //
269 // Without this restriction, duplicate NFTokenIDs can be reproduced when
270 // authorized minting is involved. Because when the minter mints a NFToken,
271 // the issuer's sequence does not change. So when the issuer re-creates
272 // their account and mints a NFToken, it is possible that the
273 // NFTokenSequence of this NFToken is the same as the one that the
274 // authorized minter minted in a previous ledger.
275 if ((*sleAccount)[~sfFirstNFTokenSequence].value_or(0) + (*sleAccount)[~sfMintedNFTokens].value_or(0) + seqDelta >
276 ctx.view.seq())
277 return tecTOO_SOON;
278
279 // Verify that the account does not own any objects that would prevent
280 // the account from being deleted.
281 Keylet const ownerDirKeylet{keylet::ownerDir(account)};
282 if (dirIsEmpty(ctx.view, ownerDirKeylet))
283 return tesSUCCESS;
284
285 std::shared_ptr<SLE const> sleDirNode{};
286 unsigned int uDirEntry{0};
287 uint256 dirEntry{beast::zero};
288
289 // Account has no directory at all. This _should_ have been caught
290 // by the dirIsEmpty() check earlier, but it's okay to catch it here.
291 if (!cdirFirst(ctx.view, ownerDirKeylet.key, sleDirNode, uDirEntry, dirEntry))
292 return tesSUCCESS;
293
294 std::int32_t deletableDirEntryCount{0};
295 do
296 {
297 // Make sure any directory node types that we find are the kind
298 // we can delete.
299 auto sleItem = ctx.view.read(keylet::child(dirEntry));
300 if (!sleItem)
301 {
302 // Directory node has an invalid index. Bail out.
303 // LCOV_EXCL_START
304 JLOG(ctx.j.fatal()) << "DeleteAccount: directory node in ledger " << ctx.view.seq()
305 << " has index to object that is missing: " << to_string(dirEntry);
306 return tefBAD_LEDGER;
307 // LCOV_EXCL_STOP
308 }
309
310 LedgerEntryType const nodeType{safe_cast<LedgerEntryType>((*sleItem)[sfLedgerEntryType])};
311
312 if (!nonObligationDeleter(nodeType))
313 return tecHAS_OBLIGATIONS;
314
315 // We found a deletable directory entry. Count it. If we find too
316 // many deletable directory entries then bail out.
317 if (++deletableDirEntryCount > maxDeletableDirEntries)
318 return tefTOO_BIG;
319
320 } while (cdirNext(ctx.view, ownerDirKeylet.key, sleDirNode, uDirEntry, dirEntry));
321
322 return tesSUCCESS;
323}
324
325TER
327{
328 auto src = view().peek(keylet::account(account_));
329 XRPL_ASSERT(src, "xrpl::DeleteAccount::doApply : non-null source account");
330
331 auto const dstID = ctx_.tx[sfDestination];
332 auto dst = view().peek(keylet::account(dstID));
333 XRPL_ASSERT(dst, "xrpl::DeleteAccount::doApply : non-null destination account");
334
335 if (!src || !dst)
336 return tefBAD_LEDGER; // LCOV_EXCL_LINE
337
338 if (ctx_.tx.isFieldPresent(sfCredentialIDs))
339 {
340 if (auto err = verifyDepositPreauth(ctx_.tx, ctx_.view(), account_, dstID, dst, ctx_.journal);
341 !isTesSuccess(err))
342 return err;
343 }
344
345 Keylet const ownerDirKeylet{keylet::ownerDir(account_)};
346 auto const ter = cleanupOnAccountDelete(
347 view(),
348 ownerDirKeylet,
349 [&](LedgerEntryType nodeType,
350 uint256 const& dirEntry,
352 if (auto deleter = nonObligationDeleter(nodeType))
353 {
354 TER const result{deleter(ctx_.app, view(), account_, dirEntry, sleItem, j_)};
355
356 return {result, SkipEntry::No};
357 }
358
359 // LCOV_EXCL_START
360 UNREACHABLE(
361 "xrpl::DeleteAccount::doApply : undeletable item not found "
362 "in preclaim");
363 JLOG(j_.error()) << "DeleteAccount undeletable item not "
364 "found in preclaim.";
366 // LCOV_EXCL_STOP
367 },
368 ctx_.journal);
369 if (ter != tesSUCCESS)
370 return ter;
371
372 // Transfer any XRP remaining after the fee is paid to the destination:
373 (*dst)[sfBalance] = (*dst)[sfBalance] + mSourceBalance;
374 (*src)[sfBalance] = (*src)[sfBalance] - mSourceBalance;
376
377 XRPL_ASSERT((*src)[sfBalance] == XRPAmount(0), "xrpl::DeleteAccount::doApply : source balance is zero");
378
379 // If there's still an owner directory associated with the source account
380 // delete it.
381 if (view().exists(ownerDirKeylet) && !view().emptyDirDelete(ownerDirKeylet))
382 {
383 JLOG(j_.error()) << "DeleteAccount cannot delete root dir node of " << toBase58(account_);
384 return tecHAS_OBLIGATIONS;
385 }
386
387 // Re-arm the password change fee if we can and need to.
388 if (mSourceBalance > XRPAmount(0) && dst->isFlag(lsfPasswordSpent))
389 dst->clearFlag(lsfPasswordSpent);
390
391 view().update(dst);
392 view().erase(src);
393
394 return tesSUCCESS;
395}
396
397} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:41
Stream fatal() const
Definition Journal.h:325
Stream error() const
Definition Journal.h:319
STTx const & tx
void deliver(STAmount const &amount)
Sets the DeliveredAmount field in the metadata.
beast::Journal const journal
ApplyView & view()
Application & app
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:115
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
virtual void erase(std::shared_ptr< SLE > const &sle)=0
Remove a peeked SLE.
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
static TER deleteSLE(ApplyContext &ctx, Keylet sleKeylet, AccountID const owner)
Definition DID.cpp:141
static TER deleteDelegate(ApplyView &view, std::shared_ptr< SLE > const &sle, AccountID const &account, beast::Journal j)
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
TER doApply() override
static NotTEC preflight(PreflightContext const &ctx)
static bool checkExtraFeatures(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
static TER deleteOracle(ApplyView &view, std::shared_ptr< SLE > const &sle, AccountID const &account, beast::Journal j)
static TER removeFromLedger(ApplyView &view, uint256 const &delIndex, beast::Journal j)
A view into a ledger.
Definition ReadView.h:32
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
LedgerIndex seq() const
Returns the sequence number of the base ledger.
Definition ReadView.h:98
virtual std::optional< key_type > succ(key_type const &key, std::optional< key_type > const &last=std::nullopt) const =0
Return the key of the next state item.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:118
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:439
static TER removeFromLedger(Application &app, ApplyView &view, AccountID const &account, beast::Journal j)
AccountID const account_
Definition Transactor.h:113
static XRPAmount calculateOwnerReserveFee(ReadView const &view, STTx const &tx)
beast::Journal const j_
Definition Transactor.h:111
ApplyView & view()
Definition Transactor.h:129
XRPAmount mSourceBalance
Definition Transactor.h:115
ApplyContext & ctx_
Definition Transactor.h:109
static TER ticketDelete(ApplyView &view, AccountID const &account, uint256 const &ticketIndex, beast::Journal j)
base_uint next() const
Definition base_uint.h:426
NotTEC checkFields(STTx const &tx, beast::Journal j)
TER valid(STTx const &tx, ReadView const &view, AccountID const &src, beast::Journal j)
TER deleteSLE(ApplyView &view, std::shared_ptr< SLE > const &sleCredential, beast::Journal j)
Keylet nftpage_max(AccountID const &owner)
A keylet for the owner's last possible NFT page.
Definition Indexes.cpp:360
Keylet depositPreauth(AccountID const &owner, AccountID const &preauthorized) noexcept
A DepositPreauth.
Definition Indexes.cpp:299
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:325
Keylet child(uint256 const &key) noexcept
Any item that can be in an owner dir.
Definition Indexes.cpp:166
Keylet nftpage_min(AccountID const &owner)
NFT page keylets.
Definition Indexes.cpp:352
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:160
bool deleteTokenOffer(ApplyView &view, std::shared_ptr< SLE > const &offer)
Deletes the given token offer.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
@ terNO_ACCOUNT
Definition TER.h:198
bool dirIsEmpty(ReadView const &view, Keylet const &k)
Returns true if the directory is empty.
Definition View.cpp:825
std::size_t constexpr maxDeletableDirEntries
The maximum number of owner directory entries for account to be deletable.
Definition Protocol.h:50
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:598
@ tefTOO_BIG
Definition TER.h:165
@ tefBAD_LEDGER
Definition TER.h:151
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:92
bool cdirNext(ReadView const &view, uint256 const &root, std::shared_ptr< SLE const > &page, unsigned int &index, uint256 &entry)
Returns the next entry in the directory, advancing the index.
Definition View.cpp:112
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:29
bool cdirFirst(ReadView const &view, uint256 const &root, std::shared_ptr< SLE const > &page, unsigned int &index, uint256 &entry)
Returns the first entry in the directory, advancing the index.
Definition View.cpp:101
TER verifyDepositPreauth(STTx const &tx, ApplyView &view, AccountID const &src, AccountID const &dst, std::shared_ptr< SLE > const &sleDst, beast::Journal j)
base_uint< 256 > uint256
Definition base_uint.h:527
TERSubset< CanCvtToTER > TER
Definition TER.h:621
static TER removeSignersFromLedger(Application &app, ApplyView &view, Keylet const &accountKeylet, Keylet const &ownerDirKeylet, Keylet const &signerListKeylet, beast::Journal j)
TER offerDelete(ApplyView &view, std::shared_ptr< SLE > const &sle, beast::Journal j)
Delete an offer.
Definition View.cpp:1672
@ temDST_IS_SRC
Definition TER.h:89
bool isTesSuccess(TER x) noexcept
Definition TER.h:650
LedgerEntryType
Identifiers for on-ledger objects.
@ tecTOO_SOON
Definition TER.h:300
@ tecNO_PERMISSION
Definition TER.h:287
@ tecDST_TAG_NEEDED
Definition TER.h:291
@ tecHAS_OBLIGATIONS
Definition TER.h:299
@ tecNO_DST
Definition TER.h:272
@ lsfDepositAuth
@ lsfPasswordSpent
@ lsfRequireDestTag
@ tesSUCCESS
Definition TER.h:226
TER cleanupOnAccountDelete(ApplyView &view, Keylet const &ownerDirKeylet, EntryDeleter const &deleter, beast::Journal j, std::optional< std::uint16_t > maxNodesToDelete=std::nullopt)
Cleanup owner directory entries on account delete.
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
uint256 key
Definition Keylet.h:21
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:54
ReadView const & view
Definition Transactor.h:57
beast::Journal const j
Definition Transactor.h:62
State information when preflighting a tx.
Definition Transactor.h:16
beast::Journal const j
Definition Transactor.h:23