rippled
Loading...
Searching...
No Matches
AccountDelete.cpp
1#include <xrpl/basics/Log.h>
2#include <xrpl/beast/utility/instrumentation.h>
3#include <xrpl/ledger/View.h>
4#include <xrpl/ledger/helpers/CredentialHelpers.h>
5#include <xrpl/ledger/helpers/DirectoryHelpers.h>
6#include <xrpl/ledger/helpers/OfferHelpers.h>
7#include <xrpl/protocol/Feature.h>
8#include <xrpl/protocol/Indexes.h>
9#include <xrpl/protocol/Protocol.h>
10#include <xrpl/protocol/TxFlags.h>
11#include <xrpl/protocol/Units.h>
12#include <xrpl/tx/transactors/account/AccountDelete.h>
13#include <xrpl/tx/transactors/account/SignerListSet.h>
14#include <xrpl/tx/transactors/delegate/DelegateSet.h>
15#include <xrpl/tx/transactors/did/DIDDelete.h>
16#include <xrpl/tx/transactors/nft/NFTokenUtils.h>
17#include <xrpl/tx/transactors/oracle/OracleDelete.h>
18#include <xrpl/tx/transactors/payment/DepositPreauth.h>
19
20namespace xrpl {
21
22bool
24{
25 return !ctx.tx.isFieldPresent(sfCredentialIDs) || ctx.rules.enabled(featureCredentials);
26}
27
30{
31 if (ctx.tx[sfAccount] == ctx.tx[sfDestination])
32 {
33 // An account cannot be deleted and give itself the resulting XRP.
34 return temDST_IS_SRC;
35 }
36
37 if (auto const err = credentials::checkFields(ctx.tx, ctx.j); !isTesSuccess(err))
38 return err;
39
40 return tesSUCCESS;
41}
42
45{
46 // The fee required for AccountDelete is one owner reserve.
48}
49
50namespace {
51// Define a function pointer type that can be used to delete ledger node types.
52using DeleterFuncPtr = TER (*)(
53 ServiceRegistry& registry,
54 ApplyView& view,
55 AccountID const& account,
56 uint256 const& delIndex,
57 std::shared_ptr<SLE> const& sleDel,
59
60// Local function definitions that provides signature compatibility.
61TER
64 ApplyView& view,
65 AccountID const& account,
66 uint256 const& delIndex,
67 std::shared_ptr<SLE> const& sleDel,
69{
70 return offerDelete(view, sleDel, j);
71}
72
73TER
75 ServiceRegistry& registry,
76 ApplyView& view,
77 AccountID const& account,
78 uint256 const& delIndex,
79 std::shared_ptr<SLE> const& sleDel,
81{
82 return SignerListSet::removeFromLedger(registry, view, account, j);
83}
84
85TER
86removeTicketFromLedger(
87 ServiceRegistry&,
88 ApplyView& view,
89 AccountID const& account,
90 uint256 const& delIndex,
93{
94 return Transactor::ticketDelete(view, account, delIndex, j);
95}
96
97TER
98removeDepositPreauthFromLedger(
99 ServiceRegistry&,
100 ApplyView& view,
101 AccountID const&,
102 uint256 const& delIndex,
105{
106 return DepositPreauth::removeFromLedger(view, delIndex, j);
107}
108
109TER
110removeNFTokenOfferFromLedger(
111 ServiceRegistry&,
112 ApplyView& view,
113 AccountID const& account,
114 uint256 const& delIndex,
115 std::shared_ptr<SLE> const& sleDel,
117{
118 if (!nft::deleteTokenOffer(view, sleDel))
119 return tefBAD_LEDGER; // LCOV_EXCL_LINE
120
121 return tesSUCCESS;
122}
123
124TER
125removeDIDFromLedger(
126 ServiceRegistry&,
127 ApplyView& view,
128 AccountID const& account,
129 uint256 const& delIndex,
130 std::shared_ptr<SLE> const& sleDel,
132{
133 return DIDDelete::deleteSLE(view, sleDel, account, j);
134}
135
136TER
137removeOracleFromLedger(
138 ServiceRegistry&,
139 ApplyView& view,
140 AccountID const& account,
141 uint256 const&,
142 std::shared_ptr<SLE> const& sleDel,
144{
145 return OracleDelete::deleteOracle(view, sleDel, account, j);
146}
147
148TER
149removeCredentialFromLedger(
150 ServiceRegistry&,
151 ApplyView& view,
152 AccountID const&,
153 uint256 const&,
154 std::shared_ptr<SLE> const& sleDel,
156{
157 return credentials::deleteSLE(view, sleDel, j);
158}
159
160TER
161removeDelegateFromLedger(
162 ServiceRegistry&,
163 ApplyView& view,
164 AccountID const& account,
165 uint256 const& delIndex,
166 std::shared_ptr<SLE> const& sleDel,
168{
169 return DelegateSet::deleteDelegate(view, sleDel, account, j);
170}
171
172// Return nullptr if the LedgerEntryType represents an obligation that can't
173// be deleted. Otherwise return the pointer to the function that can delete
174// the non-obligation
175DeleterFuncPtr
176nonObligationDeleter(LedgerEntryType t)
177{
178 switch (t)
179 {
180 case ltOFFER:
181 return offerDelete;
182 case ltSIGNER_LIST:
184 case ltTICKET:
185 return removeTicketFromLedger;
186 case ltDEPOSIT_PREAUTH:
187 return removeDepositPreauthFromLedger;
188 case ltNFTOKEN_OFFER:
189 return removeNFTokenOfferFromLedger;
190 case ltDID:
191 return removeDIDFromLedger;
192 case ltORACLE:
193 return removeOracleFromLedger;
194 case ltCREDENTIAL:
195 return removeCredentialFromLedger;
196 case ltDELEGATE:
197 return removeDelegateFromLedger;
198 default:
199 return nullptr;
200 }
201}
202
203} // namespace
204
205TER
207{
208 AccountID const account{ctx.tx[sfAccount]};
209 AccountID const dst{ctx.tx[sfDestination]};
210
211 auto sleDst = ctx.view.read(keylet::account(dst));
212
213 if (!sleDst)
214 return tecNO_DST;
215
216 if ((((*sleDst)[sfFlags] & lsfRequireDestTag) != 0u) && !ctx.tx[~sfDestinationTag])
217 return tecDST_TAG_NEEDED;
218
219 // If credentials are provided - check them anyway
220 if (auto const err = credentials::valid(ctx.tx, ctx.view, account, ctx.j); !isTesSuccess(err))
221 return err;
222
223 // if credentials then postpone auth check to doApply, to check for expired
224 // credentials
225 if (!ctx.tx.isFieldPresent(sfCredentialIDs))
226 {
227 // Check whether the destination account requires deposit authorization.
228 if ((sleDst->getFlags() & lsfDepositAuth) != 0u)
229 {
230 if (!ctx.view.exists(keylet::depositPreauth(dst, account)))
231 return tecNO_PERMISSION;
232 }
233 }
234
235 auto sleAccount = ctx.view.read(keylet::account(account));
236 XRPL_ASSERT(sleAccount, "xrpl::AccountDelete::preclaim : non-null account");
237 if (!sleAccount)
238 return terNO_ACCOUNT;
239
240 // If an issuer has any issued NFTs resident in the ledger then it
241 // cannot be deleted.
242 if ((*sleAccount)[~sfMintedNFTokens] != (*sleAccount)[~sfBurnedNFTokens])
243 return tecHAS_OBLIGATIONS;
244
245 // If the account owns any NFTs it cannot be deleted.
246 Keylet const first = keylet::nftpage_min(account);
247 Keylet const last = keylet::nftpage_max(account);
248
249 auto const cp = ctx.view.read(
250 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) +
276 (*sleAccount)[~sfMintedNFTokens].value_or(0) + seqDelta >
277 ctx.view.seq())
278 return tecTOO_SOON;
279
280 // Verify that the account does not own any objects that would prevent
281 // the account from being deleted.
282 Keylet const ownerDirKeylet{keylet::ownerDir(account)};
283 if (dirIsEmpty(ctx.view, ownerDirKeylet))
284 return tesSUCCESS;
285
286 std::shared_ptr<SLE const> sleDirNode{};
287 unsigned int uDirEntry{0};
288 uint256 dirEntry{beast::zero};
289
290 // Account has no directory at all. This _should_ have been caught
291 // by the dirIsEmpty() check earlier, but it's okay to catch it here.
292 if (!cdirFirst(ctx.view, ownerDirKeylet.key, sleDirNode, uDirEntry, dirEntry))
293 return tesSUCCESS;
294
295 std::uint32_t deletableDirEntryCount{0};
296 do
297 {
298 // Make sure any directory node types that we find are the kind
299 // we can delete.
300 auto sleItem = ctx.view.read(keylet::child(dirEntry));
301 if (!sleItem)
302 {
303 // Directory node has an invalid index. Bail out.
304 // LCOV_EXCL_START
305 JLOG(ctx.j.fatal()) << "AccountDelete: directory node in ledger " << ctx.view.seq()
306 << " has index to object that is missing: " << to_string(dirEntry);
307 return tefBAD_LEDGER;
308 // LCOV_EXCL_STOP
309 }
310
311 LedgerEntryType const nodeType{safe_cast<LedgerEntryType>((*sleItem)[sfLedgerEntryType])};
312
313 if (nonObligationDeleter(nodeType) == nullptr)
314 return tecHAS_OBLIGATIONS;
315
316 // We found a deletable directory entry. Count it. If we find too
317 // many deletable directory entries then bail out.
318 if (++deletableDirEntryCount > maxDeletableDirEntries)
319 return tefTOO_BIG;
320
321 } while (cdirNext(ctx.view, ownerDirKeylet.key, sleDirNode, uDirEntry, dirEntry));
322
323 return tesSUCCESS;
324}
325
326TER
328{
329 auto src = view().peek(keylet::account(account_));
330 XRPL_ASSERT(src, "xrpl::AccountDelete::doApply : non-null source account");
331
332 auto const dstID = ctx_.tx[sfDestination];
333 auto dst = view().peek(keylet::account(dstID));
334 XRPL_ASSERT(dst, "xrpl::AccountDelete::doApply : non-null destination account");
335
336 if (!src || !dst)
337 return tefBAD_LEDGER; // LCOV_EXCL_LINE
338
339 if (ctx_.tx.isFieldPresent(sfCredentialIDs))
340 {
341 if (auto err =
343 !isTesSuccess(err))
344 return err;
345 }
346
347 Keylet const ownerDirKeylet{keylet::ownerDir(account_)};
348 auto const ter = cleanupOnAccountDelete(
349 view(),
350 ownerDirKeylet,
351 [&](LedgerEntryType nodeType,
352 uint256 const& dirEntry,
354 if (auto deleter = nonObligationDeleter(nodeType))
355 {
356 TER const result{deleter(ctx_.registry, view(), account_, dirEntry, sleItem, j_)};
357
358 return {result, SkipEntry::No};
359 }
360
361 // LCOV_EXCL_START
362 UNREACHABLE(
363 "xrpl::AccountDelete::doApply : undeletable item not found "
364 "in preclaim");
365 JLOG(j_.error()) << "AccountDelete undeletable item not "
366 "found in preclaim.";
368 // LCOV_EXCL_STOP
369 },
370 ctx_.journal);
371 if (!isTesSuccess(ter))
372 return ter;
373
374 // Transfer any XRP remaining after the fee is paid to the destination:
375 auto const remainingBalance = src->getFieldAmount(sfBalance).xrp();
376 (*dst)[sfBalance] = (*dst)[sfBalance] + remainingBalance;
377 (*src)[sfBalance] = (*src)[sfBalance] - remainingBalance;
378 ctx_.deliver(remainingBalance);
379
380 XRPL_ASSERT(
381 (*src)[sfBalance] == XRPAmount(0), "xrpl::AccountDelete::doApply : source balance is zero");
382
383 // If there's still an owner directory associated with the source account
384 // delete it.
385 if (view().exists(ownerDirKeylet) && !view().emptyDirDelete(ownerDirKeylet))
386 {
387 JLOG(j_.error()) << "AccountDelete cannot delete root dir node of " << toBase58(account_);
388 return tecHAS_OBLIGATIONS;
389 }
390
391 // Re-arm the password change fee if we can and need to.
392 if (remainingBalance > XRPAmount(0) && dst->isFlag(lsfPasswordSpent))
393 dst->clearFlag(lsfPasswordSpent);
394
395 view().update(dst);
396 view().erase(src);
397
398 return tesSUCCESS;
399}
400
401} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
Stream fatal() const
Definition Journal.h:325
Stream error() const
Definition Journal.h:319
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)
STTx const & tx
std::reference_wrapper< ServiceRegistry > registry
void deliver(STAmount const &amount)
Sets the DeliveredAmount field in the metadata.
beast::Journal const journal
ApplyView & view()
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:116
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 DIDDelete.cpp:16
static TER deleteDelegate(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)
static TER deleteOracle(ApplyView &view, std::shared_ptr< SLE > const &sle, AccountID const &account, beast::Journal j)
A view into a ledger.
Definition ReadView.h:31
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:97
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:120
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:456
Service registry for dependency injection.
static TER removeFromLedger(ServiceRegistry &registry, ApplyView &view, AccountID const &account, beast::Journal j)
AccountID const account_
Definition Transactor.h:116
static XRPAmount calculateOwnerReserveFee(ReadView const &view, STTx const &tx)
beast::Journal const j_
Definition Transactor.h:114
ApplyView & view()
Definition Transactor.h:132
ApplyContext & ctx_
Definition Transactor.h:112
static TER ticketDelete(ApplyView &view, AccountID const &account, uint256 const &ticketIndex, beast::Journal j)
base_uint next() const
Definition base_uint.h:429
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:371
Keylet depositPreauth(AccountID const &owner, AccountID const &preauthorized) noexcept
A DepositPreauth.
Definition Indexes.cpp:307
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:336
Keylet child(uint256 const &key) noexcept
Any item that can be in an owner dir.
Definition Indexes.cpp:171
Keylet nftpage_min(AccountID const &owner)
NFT page keylets.
Definition Indexes.cpp:363
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:165
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:5
@ terNO_ACCOUNT
Definition TER.h:197
bool dirIsEmpty(ReadView const &view, Keylet const &k)
Returns true if the directory is empty.
std::size_t constexpr maxDeletableDirEntries
The maximum number of owner directory entries for account to be deletable.
Definition Protocol.h:49
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:602
TER verifyDepositPreauth(STTx const &tx, ApplyView &view, AccountID const &src, AccountID const &dst, std::shared_ptr< SLE const > const &sleDst, beast::Journal j)
@ tefTOO_BIG
Definition TER.h:164
@ tefBAD_LEDGER
Definition TER.h:150
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.
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
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.
base_uint< 256 > uint256
Definition base_uint.h:531
TERSubset< CanCvtToTER > TER
Definition TER.h:622
TER offerDelete(ApplyView &view, std::shared_ptr< SLE > const &sle, beast::Journal j)
Delete an offer.
@ temDST_IS_SRC
Definition TER.h:88
bool isTesSuccess(TER x) noexcept
Definition TER.h:651
LedgerEntryType
Identifiers for on-ledger objects.
@ tecTOO_SOON
Definition TER.h:299
@ tecNO_PERMISSION
Definition TER.h:286
@ tecDST_TAG_NEEDED
Definition TER.h:290
@ tecHAS_OBLIGATIONS
Definition TER.h:298
@ tecNO_DST
Definition TER.h:271
static TER removeSignersFromLedger(ServiceRegistry &registry, ApplyView &view, Keylet const &accountKeylet, Keylet const &ownerDirKeylet, Keylet const &signerListKeylet, beast::Journal j)
@ tesSUCCESS
Definition TER.h:225
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:19
uint256 key
Definition Keylet.h:20
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:57
ReadView const & view
Definition Transactor.h:60
beast::Journal const j
Definition Transactor.h:65
State information when preflighting a tx.
Definition Transactor.h:14
beast::Journal const j
Definition Transactor.h:21