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>
48 if (ctx.
tx[sfAccount] == ctx.
tx[sfDestination])
69using DeleterFuncPtr =
TER (*)(
92 ServiceRegistry& registry,
103removeTicketFromLedger(
115removeDepositPreauthFromLedger(
127removeNFTokenOfferFromLedger(
154removeOracleFromLedger(
166removeCredentialFromLedger(
178removeDelegateFromLedger(
202 return removeTicketFromLedger;
203 case ltDEPOSIT_PREAUTH:
204 return removeDepositPreauthFromLedger;
205 case ltNFTOKEN_OFFER:
206 return removeNFTokenOfferFromLedger;
208 return removeDIDFromLedger;
210 return removeOracleFromLedger;
212 return removeCredentialFromLedger;
214 return removeDelegateFromLedger;
233 if (sleDst->isFlag(lsfRequireDestTag) && !ctx.
tx[~sfDestinationTag])
245 if (sleDst->isFlag(lsfDepositAuth))
253 XRPL_ASSERT(sleAccount,
"xrpl::AccountDelete::preclaim : non-null account");
259 if ((*sleAccount)[~sfMintedNFTokens] != (*sleAccount)[~sfBurnedNFTokens])
278 if ((*sleAccount)[sfSequence] + kSeqDelta > ctx.
view.
seq())
292 if ((*sleAccount)[~sfFirstNFTokenSequence].value_or(0) +
293 (*sleAccount)[~sfMintedNFTokens].value_or(0) + kSeqDelta >
304 unsigned int uDirEntry{0};
305 uint256 dirEntry{beast::kZero};
309 if (!
cdirFirst(ctx.
view, ownerDirKeylet.
key, sleDirNode, uDirEntry, dirEntry))
322 JLOG(ctx.
j.
fatal()) <<
"AccountDelete: directory node in ledger " << ctx.
view.
seq()
323 <<
" has index to object that is missing: " <<
to_string(dirEntry);
330 if (nonObligationDeleter(nodeType) ==
nullptr)
338 }
while (
cdirNext(ctx.
view, ownerDirKeylet.
key, sleDirNode, uDirEntry, dirEntry));
347 XRPL_ASSERT(src,
"xrpl::AccountDelete::doApply : non-null source account");
349 auto const dstID =
ctx_.tx[sfDestination];
351 XRPL_ASSERT(dst,
"xrpl::AccountDelete::doApply : non-null destination account");
356 if (
ctx_.tx.isFieldPresent(sfCredentialIDs))
371 if (
auto deleter = nonObligationDeleter(nodeType))
380 "xrpl::AccountDelete::doApply : undeletable item not found "
382 JLOG(
j_.error()) <<
"AccountDelete undeletable item not "
383 "found in preclaim.";
392 auto const remainingBalance = src->getFieldAmount(sfBalance).xrp();
393 (*dst)[sfBalance] = (*dst)[sfBalance] + remainingBalance;
394 (*src)[sfBalance] = (*src)[sfBalance] - remainingBalance;
395 ctx_.deliver(remainingBalance);
398 (*src)[sfBalance] ==
XRPAmount(0),
"xrpl::AccountDelete::doApply : source balance is zero");
402 if (
view().exists(ownerDirKeylet) && !
view().emptyDirDelete(ownerDirKeylet))
409 if (remainingBalance >
XRPAmount(0) && dst->isFlag(lsfPasswordSpent))
410 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, 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.
constexpr std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > safeCast(Src s) noexcept
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
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.
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.