1#include <xrpl/tx/transactors/account/AccountDelete.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/basics/safe_cast.h>
6#include <xrpl/beast/utility/Zero.h>
7#include <xrpl/beast/utility/instrumentation.h>
8#include <xrpl/core/ServiceRegistry.h>
9#include <xrpl/ledger/ApplyView.h>
10#include <xrpl/ledger/ReadView.h>
11#include <xrpl/ledger/View.h>
12#include <xrpl/ledger/helpers/CredentialHelpers.h>
13#include <xrpl/ledger/helpers/DirectoryHelpers.h>
14#include <xrpl/ledger/helpers/NFTokenHelpers.h>
15#include <xrpl/ledger/helpers/OfferHelpers.h>
16#include <xrpl/protocol/AccountID.h>
17#include <xrpl/protocol/Feature.h>
18#include <xrpl/protocol/Indexes.h>
19#include <xrpl/protocol/Keylet.h>
20#include <xrpl/protocol/LedgerFormats.h>
21#include <xrpl/protocol/Protocol.h>
22#include <xrpl/protocol/SField.h>
23#include <xrpl/protocol/STLedgerEntry.h>
24#include <xrpl/protocol/STTx.h>
25#include <xrpl/protocol/TER.h>
26#include <xrpl/protocol/XRPAmount.h>
27#include <xrpl/tx/Transactor.h>
28#include <xrpl/tx/transactors/account/SignerListSet.h>
29#include <xrpl/tx/transactors/delegate/DelegateSet.h>
30#include <xrpl/tx/transactors/did/DIDDelete.h>
31#include <xrpl/tx/transactors/oracle/OracleDelete.h>
32#include <xrpl/tx/transactors/payment/DepositPreauth.h>
47 if (ctx.
tx[sfAccount] == ctx.
tx[sfDestination])
68using DeleterFuncPtr =
TER (*)(
91 ServiceRegistry& registry,
102removeTicketFromLedger(
114removeDepositPreauthFromLedger(
126removeNFTokenOfferFromLedger(
153removeOracleFromLedger(
165removeCredentialFromLedger(
177removeDelegateFromLedger(
201 return removeTicketFromLedger;
202 case ltDEPOSIT_PREAUTH:
203 return removeDepositPreauthFromLedger;
204 case ltNFTOKEN_OFFER:
205 return removeNFTokenOfferFromLedger;
207 return removeDIDFromLedger;
209 return removeOracleFromLedger;
211 return removeCredentialFromLedger;
213 return removeDelegateFromLedger;
232 if (sleDst->isFlag(lsfRequireDestTag) && !ctx.
tx[~sfDestinationTag])
246 if (sleDst->isFlag(lsfDepositAuth))
254 XRPL_ASSERT(sleAccount,
"xrpl::AccountDelete::preclaim : non-null account");
260 if ((*sleAccount)[~sfMintedNFTokens] != (*sleAccount)[~sfBurnedNFTokens])
272 if (sleAccount->isFieldPresent(sfSponsor))
274 if (dst != sleAccount->getAccountID(sfSponsor))
277 if (sleAccount->isFieldPresent(sfSponsoringOwnerCount) ||
278 sleAccount->isFieldPresent(sfSponsoringAccountCount))
288 if ((*sleAccount)[sfSequence] + kSeqDelta > ctx.
view.
seq())
302 if ((*sleAccount)[~sfFirstNFTokenSequence].value_or(0) +
303 (*sleAccount)[~sfMintedNFTokens].value_or(0) + kSeqDelta >
314 unsigned int uDirEntry{0};
319 if (!
cdirFirst(ctx.
view, ownerDirKeylet.
key, sleDirNode, uDirEntry, dirEntry))
332 JLOG(ctx.
j.
fatal()) <<
"AccountDelete: directory node in ledger " << ctx.
view.
seq()
333 <<
" has index to object that is missing: " <<
to_string(dirEntry);
340 if (nonObligationDeleter(nodeType) ==
nullptr)
348 }
while (
cdirNext(ctx.
view, ownerDirKeylet.
key, sleDirNode, uDirEntry, dirEntry));
357 XRPL_ASSERT(src,
"xrpl::AccountDelete::doApply : non-null source account");
359 auto const dstID =
ctx_.tx[sfDestination];
361 XRPL_ASSERT(dst,
"xrpl::AccountDelete::doApply : non-null destination account");
366 if (
ctx_.tx.isFieldPresent(sfCredentialIDs))
381 if (
auto deleter = nonObligationDeleter(nodeType))
390 "xrpl::AccountDelete::doApply : undeletable item not found "
392 JLOG(
j_.error()) <<
"AccountDelete undeletable item not "
393 "found in preclaim.";
402 auto const remainingBalance = src->getFieldAmount(sfBalance).xrp();
403 (*dst)[sfBalance] = (*dst)[sfBalance] + remainingBalance;
404 (*src)[sfBalance] = (*src)[sfBalance] - remainingBalance;
405 ctx_.deliver(remainingBalance);
407 if (src->isFieldPresent(sfSponsor))
409 auto const sponsorID = src->getAccountID(sfSponsor);
415 auto const sponsoringAccountCount = sponsorSle->getFieldU32(sfSponsoringAccountCount);
418 sponsoringAccountCount != 0,
419 "xrpl::AccountDelete::doApply : sponsoring account count is present");
420 if (sponsoringAccountCount == 0)
427 sponsorSle->at(sfSponsoringAccountCount) = sponsoringAccountCount - 1;
433 src->makeFieldAbsent(sfSponsor);
437 (*src)[sfBalance] ==
XRPAmount(0),
"xrpl::AccountDelete::doApply : source balance is zero");
441 if (
view().exists(ownerDirKeylet) && !
view().emptyDirDelete(ownerDirKeylet))
448 if (remainingBalance >
XRPAmount(0) && dst->isFlag(lsfPasswordSpent))
449 dst->clearFlag(lsfPasswordSpent);
A generic endpoint for log messages.
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.
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
static NotTEC preflight(PreflightContext const &ctx)
static bool checkExtraFeatures(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
Writeable view to a ledger, for applying a transaction.
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void erase(SLE::ref sle)=0
Remove a peeked SLE.
virtual void update(SLE::ref sle)=0
Indicate changes to a peeked SLE.
static TER deleteSLE(ApplyContext &ctx, Keylet sleKeylet, AccountID const owner)
static TER deleteDelegate(ApplyView &view, SLE::ref sle, beast::Journal j)
static TER removeFromLedger(ApplyView &view, uint256 const &delIndex, beast::Journal j)
static TER deleteOracle(ApplyView &view, SLE::ref sle, AccountID const &account, beast::Journal j)
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
LedgerIndex seq() const
Returns the sequence number of the base ledger.
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.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
std::shared_ptr< STLedgerEntry > const & ref
std::shared_ptr< STLedgerEntry > pointer
std::shared_ptr< STLedgerEntry const > const & const_ref
std::shared_ptr< STLedgerEntry const > const_pointer
bool isFieldPresent(SField const &field) const
Service registry for dependency injection.
static TER removeFromLedger(ServiceRegistry ®istry, ApplyView &view, AccountID const &account, beast::Journal j)
static XRPAmount calculateOwnerReserveFee(ReadView const &view, STTx const &tx)
AccountID const accountID_
static TER ticketDelete(ApplyView &view, AccountID const &account, uint256 const &ticketIndex, beast::Journal j)
TER deleteSLE(ApplyView &view, SLE::ref sleCredential, beast::Journal j)
NotTEC checkFields(STTx const &tx, Rules const &rules, beast::Journal j)
TER valid(STTx const &tx, ReadView const &view, AccountID const &src, beast::Journal j)
Keylet depositPreauth(AccountID const &owner, AccountID const &preauthorized) noexcept
A DepositPreauth.
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Keylet nftokenPageMin(AccountID const &owner)
NFT page keylets.
Keylet child(uint256 const &key) noexcept
Any item that can be in an owner dir.
Keylet nftokenPageMax(AccountID const &owner)
A keylet for the owner's last possible NFT page.
Keylet account(AccountID const &id) noexcept
AccountID root.
bool deleteTokenOffer(ApplyView &view, SLE::ref offer)
Deletes the given token offer.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
bool dirIsEmpty(ReadView const &view, Keylet const &k)
Returns true if the directory is empty.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
constexpr Dest safeCast(Src s) noexcept
TER offerDelete(ApplyView &view, SLE::ref sle, beast::Journal j)
Delete an offer.
std::string to_string(BaseUInt< Bits, Tag > const &a)
TERSubset< CanCvtToNotTEC > NotTEC
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
bool cdirNext(ReadView const &view, uint256 const &root, SLE::const_pointer &page, unsigned int &index, uint256 &entry)
Returns the next entry in the directory, advancing the index.
bool isTesSuccess(TER x) noexcept
TERSubset< CanCvtToTER > TER
LedgerEntryType
Identifiers for on-ledger objects.
@ tecNO_SPONSOR_PERMISSION
bool cdirFirst(ReadView const &view, uint256 const &root, SLE::const_pointer &page, unsigned int &index, uint256 &entry)
Returns the first entry in the directory, advancing the index.
static TER removeSignersFromLedger(ServiceRegistry ®istry, ApplyView &view, Keylet const &accountKeylet, Keylet const &ownerDirKeylet, Keylet const &signerListKeylet, beast::Journal j)
constexpr std::size_t kMaxDeletableDirEntries
The maximum number of owner directory entries for account to be deletable.
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.
TER verifyDepositPreauth(STTx const &tx, ApplyView &view, AccountID const &src, AccountID const &dst, SLE::const_ref sleDst, beast::Journal j)
A pair of SHAMap key and LedgerEntryType.
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.