20#include <xrpld/app/misc/CredentialHelpers.h>
21#include <xrpld/app/tx/detail/InvariantCheck.h>
22#include <xrpld/app/tx/detail/NFTokenUtils.h>
23#include <xrpld/app/tx/detail/PermissionedDomainSet.h>
24#include <xrpld/ledger/ReadView.h>
25#include <xrpld/ledger/View.h>
26#include <xrpl/basics/Log.h>
27#include <xrpl/protocol/Feature.h>
28#include <xrpl/protocol/FeeUnits.h>
29#include <xrpl/protocol/STArray.h>
30#include <xrpl/protocol/SystemParameters.h>
31#include <xrpl/protocol/TxFormats.h>
32#include <xrpl/protocol/nftPageMask.h>
56 JLOG(j.
fatal()) <<
"Invariant failed: fee paid was negative: "
65 JLOG(j.
fatal()) <<
"Invariant failed: fee paid exceeds system limit: "
74 JLOG(j.
fatal()) <<
"Invariant failed: fee paid is " << fee.
drops()
75 <<
" exceeds fee specified in transaction.";
99 switch (before->getType())
102 drops_ -= (*before)[sfBalance].xrp().drops();
106 ((*before)[sfAmount] - (*before)[sfBalance]).xrp().drops();
109 drops_ -= (*before)[sfAmount].xrp().drops();
118 switch (
after->getType())
121 drops_ += (*after)[sfBalance].xrp().drops();
125 drops_ += ((*after)[sfAmount] - (*after)[sfBalance])
131 drops_ += (*after)[sfAmount].xrp().drops();
151 JLOG(j.
fatal()) <<
"Invariant failed: XRP net change was positive: "
159 JLOG(j.
fatal()) <<
"Invariant failed: XRP net change of " <<
drops_
160 <<
" doesn't match fee " << fee.
drops();
175 auto isBad = [](
STAmount const& balance) {
176 if (!balance.native())
179 auto const drops = balance.xrp();
193 if (before && before->getType() == ltACCOUNT_ROOT)
194 bad_ |= isBad((*before)[sfBalance]);
196 if (
after &&
after->getType() == ltACCOUNT_ROOT)
210 JLOG(j.
fatal()) <<
"Invariant failed: incorrect account XRP balance";
227 if (pays < beast::zero)
230 if (gets < beast::zero)
234 return pays.
native() && gets.native();
237 if (before && before->getType() == ltOFFER)
238 bad_ |= isBad((*before)[sfTakerPays], (*before)[sfTakerGets]);
241 bad_ |= isBad((*
after)[sfTakerPays], (*after)[sfTakerGets]);
254 JLOG(j.
fatal()) <<
"Invariant failed: offer with a bad amount";
269 auto isBad = [](
STAmount const& amount) {
270 if (!amount.native())
282 if (before && before->getType() == ltESCROW)
283 bad_ |= isBad((*before)[sfAmount]);
299 JLOG(j.
fatal()) <<
"Invariant failed: escrow specifies invalid amount";
314 if (isDelete && before && before->getType() == ltACCOUNT_ROOT)
338 JLOG(j.
fatal()) <<
"Invariant failed: account deletion "
339 "succeeded without deleting an account";
341 JLOG(j.
fatal()) <<
"Invariant failed: account deletion "
342 "succeeded but deleted multiple accounts!";
357 JLOG(j.
fatal()) <<
"Invariant failed: an account root was deleted";
369 if (isDelete && before && before->getType() == ltACCOUNT_ROOT)
385 [[maybe_unused]]
bool const enforce =
388 auto const objectExists = [&view, enforce, &j](
auto const& keylet) {
389 if (
auto const sle = view.
read(keylet))
392 auto const typeName = [&sle]() {
397 return item->getName();
402 <<
"Invariant failed: account deletion left behind a "
403 << typeName <<
" object";
406 "ripple::AccountRootsDeletedClean::finalize::objectExists : "
407 "account deletion left no objects behind");
415 auto const accountID = accountSLE->getAccountID(sfAccount);
419 if (objectExists(
std::invoke(keyletfunc, accountID)) && enforce)
434 if (key && objectExists(
Keylet{ltNFTOKEN_PAGE, *key}) && enforce)
439 if (
auto const ammKey = accountSLE->at(~sfAMMID))
457 if (before &&
after && before->getType() !=
after->getType())
462 switch (
after->getType())
470 case ltLEDGER_HASHES:
476 case ltDEPOSIT_PREAUTH:
479 case ltNFTOKEN_OFFER:
482 case ltXCHAIN_OWNED_CLAIM_ID:
483 case ltXCHAIN_OWNED_CREATE_ACCOUNT_CLAIM_ID:
486 case ltMPTOKEN_ISSUANCE:
489 case ltPERMISSIONED_DOMAIN:
511 JLOG(j.
fatal()) <<
"Invariant failed: ledger entry type mismatch";
516 JLOG(j.
fatal()) <<
"Invariant failed: invalid ledger entry type added";
530 if (
after &&
after->getType() == ltRIPPLE_STATE)
552 JLOG(j.
fatal()) <<
"Invariant failed: an XRP trust line was created";
564 if (
after &&
after->getType() == ltRIPPLE_STATE)
574 (lowDeepFreeze && !lowFreeze) || (highDeepFreeze && !highFreeze);
589 JLOG(j.
fatal()) <<
"Invariant failed: a trust line with deep freeze flag "
590 "without normal freeze was created";
618 if (balanceChange.signum() == 0)
650 [[maybe_unused]]
bool const enforce =
655 auto const issuerSle =
findIssuer(issue.account, view);
662 "ripple::TransfersNotFrozen::finalize : enforce "
687 after,
"ripple::TransfersNotFrozen::isValidEntry : valid after.");
693 if (
after->getType() == ltACCOUNT_ROOT)
705 return after->getType() == ltRIPPLE_STATE &&
706 (!before || before->getType() == ltRIPPLE_STATE);
715 auto const getBalance = [](
auto const& line,
auto const& other,
bool zero) {
717 line ? line->at(sfBalance) : other->at(sfBalance).zeroed();
718 return zero ? amt.
zeroed() : amt;
726 auto const balanceBefore = getBalance(before,
after,
false);
733 auto const balanceAfter = getBalance(
after, before, isDelete);
735 return balanceAfter - balanceBefore;
743 "ripple::TransfersNotFrozen::recordBalance : valid trustline "
747 changes.senders.emplace_back(std::move(change));
749 changes.receivers.emplace_back(std::move(change));
757 auto const balanceChangeSign = balanceChange.
signum();
758 auto const currency =
after->at(sfBalance).getCurrency();
762 {currency,
after->at(sfHighLimit).getIssuer()},
763 {
after, balanceChangeSign});
767 {currency,
after->at(sfLowLimit).getIssuer()},
768 {
after, -balanceChangeSign});
810 for (
auto const& change : actors)
812 bool const high = change.line->at(sfLowLimit).getIssuer() ==
813 issuer->at(sfAccount);
816 change, high, tx, j, enforce, globalFreeze))
836 bool const deepFreeze =
838 bool const frozen = globalFreeze || deepFreeze || freeze;
848 if ((!isAMMLine || globalFreeze) && tx.
getTxnType() == ttAMM_CLAWBACK)
850 JLOG(j.
debug()) <<
"Invariant check allowing funds to be moved "
852 <<
" a frozen trustline for AMMClawback "
857 JLOG(j.
fatal()) <<
"Invariant failed: Attempting to move frozen funds for "
861 "ripple::TransfersNotFrozen::validateFrozenState : enforce "
880 if (!before &&
after->getType() == ltACCOUNT_ROOT)
900 JLOG(j.
fatal()) <<
"Invariant failed: multiple accounts "
901 "created in a single transaction";
907 tx.
getTxnType() == ttXCHAIN_ADD_CLAIM_ATTESTATION ||
908 tx.
getTxnType() == ttXCHAIN_ADD_ACCOUNT_CREATE_ATTESTATION) &&
916 JLOG(j.
fatal()) <<
"Invariant failed: account created with "
917 "wrong starting sequence number";
923 JLOG(j.
fatal()) <<
"Invariant failed: account root created "
924 "by a non-Payment, by an unsuccessful transaction, "
938 static constexpr uint256 const accountBits = ~pageBits;
940 if ((before && before->getType() != ltNFTOKEN_PAGE) ||
941 (
after &&
after->getType() != ltNFTOKEN_PAGE))
945 uint256 const account = sle->key() & accountBits;
946 uint256 const hiLimit = sle->key() & pageBits;
954 if (account != (*prev & accountBits))
957 if (hiLimit <= (*prev & pageBits))
961 if (
auto const next = (*sle)[~sfNextPageMin])
963 if (account != (*next & accountBits))
966 if (hiLimit >= (*next & pageBits))
971 auto const& nftokens = sle->getFieldArray(sfNFTokens);
974 if (
std::size_t const nftokenCount = nftokens.size();
975 (!isDelete && nftokenCount == 0) ||
982 prev ? *prev & pageBits :
uint256(beast::zero);
986 for (
auto const& obj : nftokens)
988 uint256 const tokenID = obj[sfNFTokenID];
995 if (
uint256 const tokenPageBits = tokenID & pageBits;
996 tokenPageBits < loLimit || tokenPageBits >= hiLimit)
999 if (
auto uri = obj[~sfURI]; uri && uri->empty())
1013 before->isFieldPresent(sfPreviousPageMin))
1022 if (!isDelete && before &&
after)
1030 before->isFieldPresent(sfNextPageMin) &&
1031 !
after->isFieldPresent(sfNextPageMin))
1048 JLOG(j.
fatal()) <<
"Invariant failed: NFT page is improperly linked.";
1054 JLOG(j.
fatal()) <<
"Invariant failed: NFT found in incorrect page.";
1060 JLOG(j.
fatal()) <<
"Invariant failed: NFTs on page are not sorted.";
1066 JLOG(j.
fatal()) <<
"Invariant failed: NFT contains empty URI.";
1072 JLOG(j.
fatal()) <<
"Invariant failed: NFT page has invalid size.";
1080 JLOG(j.
fatal()) <<
"Invariant failed: Last NFT page deleted with "
1081 "non-empty directory.";
1086 JLOG(j.
fatal()) <<
"Invariant failed: Lost NextMinPage link.";
1101 if (before && before->getType() == ltACCOUNT_ROOT)
1107 if (
after &&
after->getType() == ltACCOUNT_ROOT)
1123 txType != ttNFTOKEN_MINT && txType != ttNFTOKEN_BURN)
1127 JLOG(j.
fatal()) <<
"Invariant failed: the number of minted tokens "
1128 "changed without a mint transaction!";
1134 JLOG(j.
fatal()) <<
"Invariant failed: the number of burned tokens "
1135 "changed without a burn transaction!";
1147 <<
"Invariant failed: successful minting didn't increase "
1148 "the number of minted tokens.";
1154 JLOG(j.
fatal()) <<
"Invariant failed: failed minting changed the "
1155 "number of minted tokens.";
1162 <<
"Invariant failed: minting changed the number of "
1175 <<
"Invariant failed: successful burning didn't increase "
1176 "the number of burned tokens.";
1183 JLOG(j.
fatal()) <<
"Invariant failed: failed burning changed the "
1184 "number of burned tokens.";
1191 <<
"Invariant failed: burning changed the number of "
1208 if (before && before->getType() == ltRIPPLE_STATE)
1211 if (before && before->getType() == ltMPTOKEN)
1231 <<
"Invariant failed: more than one trustline changed.";
1238 <<
"Invariant failed: more than one mptokens changed.";
1250 if (holderBalance.
signum() < 0)
1253 <<
"Invariant failed: trustline balance is negative";
1262 JLOG(j.
fatal()) <<
"Invariant failed: some trustlines were changed "
1263 "despite failure of the transaction.";
1269 JLOG(j.
fatal()) <<
"Invariant failed: some mptokens were changed "
1270 "despite failure of the transaction.";
1286 if (
after &&
after->getType() == ltMPTOKEN_ISSUANCE)
1313 if (tx.
getTxnType() == ttMPTOKEN_ISSUANCE_CREATE)
1317 JLOG(j.
fatal()) <<
"Invariant failed: MPT issuance creation "
1318 "succeeded without creating a MPT issuance";
1322 JLOG(j.
fatal()) <<
"Invariant failed: MPT issuance creation "
1323 "succeeded while removing MPT issuances";
1327 JLOG(j.
fatal()) <<
"Invariant failed: MPT issuance creation "
1328 "succeeded but created multiple issuances";
1334 if (tx.
getTxnType() == ttMPTOKEN_ISSUANCE_DESTROY)
1338 JLOG(j.
fatal()) <<
"Invariant failed: MPT issuance deletion "
1339 "succeeded without removing a MPT issuance";
1343 JLOG(j.
fatal()) <<
"Invariant failed: MPT issuance deletion "
1344 "succeeded while creating MPT issuances";
1348 JLOG(j.
fatal()) <<
"Invariant failed: MPT issuance deletion "
1349 "succeeded but deleted multiple issuances";
1361 JLOG(j.
fatal()) <<
"Invariant failed: MPT authorize "
1362 "succeeded but created MPT issuances";
1367 JLOG(j.
fatal()) <<
"Invariant failed: MPT authorize "
1368 "succeeded but deleted issuances";
1372 submittedByIssuer &&
1376 <<
"Invariant failed: MPT authorize submitted by issuer "
1377 "succeeded but created/deleted mptokens";
1381 !submittedByIssuer &&
1387 <<
"Invariant failed: MPT authorize submitted by holder "
1388 "succeeded but created/deleted bad number of mptokens";
1395 if (tx.
getTxnType() == ttMPTOKEN_ISSUANCE_SET)
1399 JLOG(j.
fatal()) <<
"Invariant failed: MPT issuance set "
1400 "succeeded while removing MPT issuances";
1404 JLOG(j.
fatal()) <<
"Invariant failed: MPT issuance set "
1405 "succeeded while creating MPT issuances";
1409 JLOG(j.
fatal()) <<
"Invariant failed: MPT issuance set "
1410 "succeeded while removing MPTokens";
1414 JLOG(j.
fatal()) <<
"Invariant failed: MPT issuance set "
1415 "succeeded while creating MPTokens";
1425 JLOG(j.
fatal()) <<
"Invariant failed: a MPT issuance was created";
1429 JLOG(j.
fatal()) <<
"Invariant failed: a MPT issuance was deleted";
1433 JLOG(j.
fatal()) <<
"Invariant failed: a MPToken was created";
1437 JLOG(j.
fatal()) <<
"Invariant failed: a MPToken was deleted";
1452 if (before && before->getType() != ltPERMISSIONED_DOMAIN)
1454 if (
after &&
after->getType() != ltPERMISSIONED_DOMAIN)
1459 auto const& credentials = sle->getFieldArray(sfAcceptedCredentials);
1470 for (
auto const& cred : sorted)
1472 auto const& credTx = credentials[i++];
1473 sleStatus.
isSorted_ = (cred.first == credTx[sfIssuer]) &&
1474 (cred.second == credTx[sfCredentialType]);
1508 JLOG(j.
fatal()) <<
"Invariant failed: permissioned domain with "
1516 JLOG(j.
fatal()) <<
"Invariant failed: permissioned domain bad "
1525 <<
"Invariant failed: permissioned domain credentials "
1533 <<
"Invariant failed: permissioned domain credentials "
A generic endpoint for log messages.
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
std::vector< std::shared_ptr< SLE const > > accountsDeleted_
std::uint32_t accountsDeleted_
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
A currency issued by an account.
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
std::uint32_t afterMintedTotal
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
std::uint32_t afterBurnedTotal
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
std::uint32_t beforeBurnedTotal
std::uint32_t beforeMintedTotal
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool deepFreezeWithoutFreeze_
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
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.
LedgerIndex seq() const
Returns the sequence number of the base ledger.
virtual Rules const & rules() const =0
Returns the tx processing rules.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Currency const & getCurrency() const
int signum() const noexcept
AccountID const & getIssuer() const
bool native() const noexcept
STAmount zeroed() const
Returns a zero value with the same issuer and currency.
AccountID getAccountID(SField const &field) const
STAmount const & getFieldAmount(SField const &field) const
bool isFieldPresent(SField const &field) const
TxType getTxnType() const
uint256 getTransactionID() const
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
std::map< AccountID, std::shared_ptr< SLE const > const > possibleIssuers_
bool isValidEntry(std::shared_ptr< SLE const > const &before, std::shared_ptr< SLE const > const &after)
void recordBalance(Issue const &issue, BalanceChange change)
std::shared_ptr< SLE const > findIssuer(AccountID const &issuerID, ReadView const &view)
bool validateIssuerChanges(std::shared_ptr< SLE const > const &issuer, IssuerChanges const &changes, STTx const &tx, beast::Journal const &j, bool enforce)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
STAmount calculateBalanceChange(std::shared_ptr< SLE const > const &before, std::shared_ptr< SLE const > const &after, bool isDelete)
void recordBalanceChanges(std::shared_ptr< SLE const > const &after, STAmount const &balanceChange)
bool validateFrozenState(BalanceChange const &change, bool high, STTx const &tx, beast::Journal const &j, bool enforce, bool globalFreeze)
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
std::uint32_t trustlinesChanged
std::uint32_t mptokensChanged
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
std::uint32_t mptIssuancesCreated_
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
std::uint32_t mptokensCreated_
std::uint32_t mptIssuancesDeleted_
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
std::uint32_t mptokensDeleted_
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
std::uint32_t accountsCreated_
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
std::uint32_t accountSeq_
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
std::optional< SleStatus > sleStatus_[2]
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
constexpr value_type drops() const
Returns the number of drops.
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
std::set< std::pair< AccountID, Slice > > makeSorted(STArray const &credentials)
Keylet amm(Asset const &issue1, Asset const &issue2) noexcept
AMM entry.
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet nftpage_min(AccountID const &owner)
NFT page keylets.
Keylet nftpage_max(AccountID const &owner)
A keylet for the owner's last possible NFT page.
bool compareTokens(uint256 const &a, uint256 const &b)
uint256 constexpr pageMask(std::string_view("0000000000000000000000000000000000000000ffffffffffffffffffffffff"))
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
TxType
Transaction type identifiers.
std::size_t constexpr maxPermissionedDomainCredentialsArraySize
The maximum number of credentials can be passed in array for permissioned domain.
constexpr XRPAmount INITIAL_XRP
Configure the native currency.
std::size_t constexpr dirMaxTokensPerPage
The maximum number of items in an NFT page.
std::array< keyletDesc< AccountID const & >, 6 > const directAccountKeylets
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
static bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
A pair of SHAMap key and LedgerEntryType.
int const balanceChangeSign
std::shared_ptr< SLE const > const line
std::vector< BalanceChange > receivers
std::vector< BalanceChange > senders
std::size_t credentialsSize_