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