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>
27#include <xrpl/basics/Log.h>
28#include <xrpl/protocol/Feature.h>
29#include <xrpl/protocol/FeeUnits.h>
30#include <xrpl/protocol/STArray.h>
31#include <xrpl/protocol/SystemParameters.h>
32#include <xrpl/protocol/TxFormats.h>
33#include <xrpl/protocol/nftPageMask.h>
57 JLOG(j.
fatal()) <<
"Invariant failed: fee paid was negative: "
66 JLOG(j.
fatal()) <<
"Invariant failed: fee paid exceeds system limit: "
75 JLOG(j.
fatal()) <<
"Invariant failed: fee paid is " << fee.
drops()
76 <<
" exceeds fee specified in transaction.";
100 switch (before->getType())
103 drops_ -= (*before)[sfBalance].xrp().drops();
107 ((*before)[sfAmount] - (*before)[sfBalance]).xrp().drops();
110 drops_ -= (*before)[sfAmount].xrp().drops();
119 switch (
after->getType())
122 drops_ += (*after)[sfBalance].xrp().drops();
126 drops_ += ((*after)[sfAmount] - (*after)[sfBalance])
132 drops_ += (*after)[sfAmount].xrp().drops();
152 JLOG(j.
fatal()) <<
"Invariant failed: XRP net change was positive: "
160 JLOG(j.
fatal()) <<
"Invariant failed: XRP net change of " <<
drops_
161 <<
" doesn't match fee " << fee.
drops();
176 auto isBad = [](
STAmount const& balance) {
177 if (!balance.native())
180 auto const drops = balance.xrp();
194 if (before && before->getType() == ltACCOUNT_ROOT)
195 bad_ |= isBad((*before)[sfBalance]);
197 if (
after &&
after->getType() == ltACCOUNT_ROOT)
211 JLOG(j.
fatal()) <<
"Invariant failed: incorrect account XRP balance";
228 if (pays < beast::zero)
231 if (gets < beast::zero)
235 return pays.
native() && gets.native();
238 if (before && before->getType() == ltOFFER)
239 bad_ |= isBad((*before)[sfTakerPays], (*before)[sfTakerGets]);
242 bad_ |= isBad((*
after)[sfTakerPays], (*after)[sfTakerGets]);
255 JLOG(j.
fatal()) <<
"Invariant failed: offer with a bad amount";
270 auto isBad = [](
STAmount const& amount) {
271 if (!amount.native())
283 if (before && before->getType() == ltESCROW)
284 bad_ |= isBad((*before)[sfAmount]);
300 JLOG(j.
fatal()) <<
"Invariant failed: escrow specifies invalid amount";
315 if (isDelete && before && before->getType() == ltACCOUNT_ROOT)
340 JLOG(j.
fatal()) <<
"Invariant failed: account deletion "
341 "succeeded without deleting an account";
343 JLOG(j.
fatal()) <<
"Invariant failed: account deletion "
344 "succeeded but deleted multiple accounts!";
359 JLOG(j.
fatal()) <<
"Invariant failed: an account root was deleted";
371 if (isDelete && before && before->getType() == ltACCOUNT_ROOT)
387 [[maybe_unused]]
bool const enforce =
390 auto const objectExists = [&view, enforce, &j](
auto const& keylet) {
392 if (
auto const sle = view.
read(keylet))
395 auto const typeName = [&sle]() {
400 return item->getName();
405 <<
"Invariant failed: account deletion left behind a "
406 << typeName <<
" object";
409 "ripple::AccountRootsDeletedClean::finalize::objectExists : "
410 "account deletion left no objects behind");
418 auto const accountID = accountSLE->getAccountID(sfAccount);
422 if (objectExists(
std::invoke(keyletfunc, accountID)) && enforce)
437 if (key && objectExists(
Keylet{ltNFTOKEN_PAGE, *key}) && enforce)
442 if (
auto const ammKey = accountSLE->at(~sfAMMID))
460 if (before &&
after && before->getType() !=
after->getType())
465 switch (
after->getType())
474 case ltLEDGER_HASHES:
480 case ltDEPOSIT_PREAUTH:
483 case ltNFTOKEN_OFFER:
486 case ltXCHAIN_OWNED_CLAIM_ID:
487 case ltXCHAIN_OWNED_CREATE_ACCOUNT_CLAIM_ID:
490 case ltMPTOKEN_ISSUANCE:
493 case ltPERMISSIONED_DOMAIN:
516 JLOG(j.
fatal()) <<
"Invariant failed: ledger entry type mismatch";
521 JLOG(j.
fatal()) <<
"Invariant failed: invalid ledger entry type added";
535 if (
after &&
after->getType() == ltRIPPLE_STATE)
557 JLOG(j.
fatal()) <<
"Invariant failed: an XRP trust line was created";
569 if (
after &&
after->getType() == ltRIPPLE_STATE)
579 (lowDeepFreeze && !lowFreeze) || (highDeepFreeze && !highFreeze);
594 JLOG(j.
fatal()) <<
"Invariant failed: a trust line with deep freeze flag "
595 "without normal freeze was created";
623 if (balanceChange.signum() == 0)
655 [[maybe_unused]]
bool const enforce =
660 auto const issuerSle =
findIssuer(issue.account, view);
667 "ripple::TransfersNotFrozen::finalize : enforce "
692 after,
"ripple::TransfersNotFrozen::isValidEntry : valid after.");
698 if (
after->getType() == ltACCOUNT_ROOT)
710 return after->getType() == ltRIPPLE_STATE &&
711 (!before || before->getType() == ltRIPPLE_STATE);
720 auto const getBalance = [](
auto const& line,
auto const& other,
bool zero) {
722 line ? line->at(sfBalance) : other->at(sfBalance).zeroed();
723 return zero ? amt.
zeroed() : amt;
731 auto const balanceBefore = getBalance(before,
after,
false);
738 auto const balanceAfter = getBalance(
after, before, isDelete);
740 return balanceAfter - balanceBefore;
748 "ripple::TransfersNotFrozen::recordBalance : valid trustline "
752 changes.senders.emplace_back(std::move(change));
754 changes.receivers.emplace_back(std::move(change));
762 auto const balanceChangeSign = balanceChange.
signum();
763 auto const currency =
after->at(sfBalance).getCurrency();
767 {currency,
after->at(sfHighLimit).getIssuer()},
768 {
after, balanceChangeSign});
772 {currency,
after->at(sfLowLimit).getIssuer()},
773 {
after, -balanceChangeSign});
815 for (
auto const& change : actors)
817 bool const high = change.line->at(sfLowLimit).getIssuer() ==
818 issuer->at(sfAccount);
821 change, high, tx, j, enforce, globalFreeze))
841 bool const deepFreeze =
843 bool const frozen = globalFreeze || deepFreeze || freeze;
853 if ((!isAMMLine || globalFreeze) && tx.
getTxnType() == ttAMM_CLAWBACK)
855 JLOG(j.
debug()) <<
"Invariant check allowing funds to be moved "
857 <<
" a frozen trustline for AMMClawback "
862 JLOG(j.
fatal()) <<
"Invariant failed: Attempting to move frozen funds for "
866 "ripple::TransfersNotFrozen::validateFrozenState : enforce "
885 if (!before &&
after->getType() == ltACCOUNT_ROOT)
907 JLOG(j.
fatal()) <<
"Invariant failed: multiple accounts "
908 "created in a single transaction";
915 tx.
getTxnType() == ttXCHAIN_ADD_CLAIM_ATTESTATION ||
916 tx.
getTxnType() == ttXCHAIN_ADD_ACCOUNT_CREATE_ATTESTATION) &&
919 bool const pseudoAccount =
922 if (pseudoAccount && tx.
getTxnType() != ttAMM_CREATE &&
925 JLOG(j.
fatal()) <<
"Invariant failed: pseudo-account created by a "
926 "wrong transaction type";
939 JLOG(j.
fatal()) <<
"Invariant failed: account created with "
940 "wrong starting sequence number";
951 <<
"Invariant failed: pseudo-account created with "
960 JLOG(j.
fatal()) <<
"Invariant failed: account root created illegally";
973 static constexpr uint256 const accountBits = ~pageBits;
975 if ((before && before->getType() != ltNFTOKEN_PAGE) ||
976 (
after &&
after->getType() != ltNFTOKEN_PAGE))
980 uint256 const account = sle->key() & accountBits;
981 uint256 const hiLimit = sle->key() & pageBits;
989 if (account != (*prev & accountBits))
992 if (hiLimit <= (*prev & pageBits))
996 if (
auto const next = (*sle)[~sfNextPageMin])
998 if (account != (*next & accountBits))
1001 if (hiLimit >= (*next & pageBits))
1006 auto const& nftokens = sle->getFieldArray(sfNFTokens);
1009 if (
std::size_t const nftokenCount = nftokens.size();
1010 (!isDelete && nftokenCount == 0) ||
1017 prev ? *prev & pageBits :
uint256(beast::zero);
1021 for (
auto const& obj : nftokens)
1023 uint256 const tokenID = obj[sfNFTokenID];
1030 if (
uint256 const tokenPageBits = tokenID & pageBits;
1031 tokenPageBits < loLimit || tokenPageBits >= hiLimit)
1034 if (
auto uri = obj[~sfURI]; uri && uri->empty())
1048 before->isFieldPresent(sfPreviousPageMin))
1057 if (!isDelete && before &&
after)
1065 before->isFieldPresent(sfNextPageMin) &&
1066 !
after->isFieldPresent(sfNextPageMin))
1083 JLOG(j.
fatal()) <<
"Invariant failed: NFT page is improperly linked.";
1089 JLOG(j.
fatal()) <<
"Invariant failed: NFT found in incorrect page.";
1095 JLOG(j.
fatal()) <<
"Invariant failed: NFTs on page are not sorted.";
1101 JLOG(j.
fatal()) <<
"Invariant failed: NFT contains empty URI.";
1107 JLOG(j.
fatal()) <<
"Invariant failed: NFT page has invalid size.";
1115 JLOG(j.
fatal()) <<
"Invariant failed: Last NFT page deleted with "
1116 "non-empty directory.";
1121 JLOG(j.
fatal()) <<
"Invariant failed: Lost NextMinPage link.";
1136 if (before && before->getType() == ltACCOUNT_ROOT)
1142 if (
after &&
after->getType() == ltACCOUNT_ROOT)
1158 txType != ttNFTOKEN_MINT && txType != ttNFTOKEN_BURN)
1162 JLOG(j.
fatal()) <<
"Invariant failed: the number of minted tokens "
1163 "changed without a mint transaction!";
1169 JLOG(j.
fatal()) <<
"Invariant failed: the number of burned tokens "
1170 "changed without a burn transaction!";
1182 <<
"Invariant failed: successful minting didn't increase "
1183 "the number of minted tokens.";
1189 JLOG(j.
fatal()) <<
"Invariant failed: failed minting changed the "
1190 "number of minted tokens.";
1197 <<
"Invariant failed: minting changed the number of "
1210 <<
"Invariant failed: successful burning didn't increase "
1211 "the number of burned tokens.";
1218 JLOG(j.
fatal()) <<
"Invariant failed: failed burning changed the "
1219 "number of burned tokens.";
1226 <<
"Invariant failed: burning changed the number of "
1243 if (before && before->getType() == ltRIPPLE_STATE)
1246 if (before && before->getType() == ltMPTOKEN)
1266 <<
"Invariant failed: more than one trustline changed.";
1273 <<
"Invariant failed: more than one mptokens changed.";
1285 if (holderBalance.
signum() < 0)
1288 <<
"Invariant failed: trustline balance is negative";
1297 JLOG(j.
fatal()) <<
"Invariant failed: some trustlines were changed "
1298 "despite failure of the transaction.";
1304 JLOG(j.
fatal()) <<
"Invariant failed: some mptokens were changed "
1305 "despite failure of the transaction.";
1321 if (
after &&
after->getType() == ltMPTOKEN_ISSUANCE)
1348 if (tx.
getTxnType() == ttMPTOKEN_ISSUANCE_CREATE ||
1353 JLOG(j.
fatal()) <<
"Invariant failed: transaction "
1354 "succeeded without creating a MPT issuance";
1358 JLOG(j.
fatal()) <<
"Invariant failed: transaction "
1359 "succeeded while removing MPT issuances";
1363 JLOG(j.
fatal()) <<
"Invariant failed: transaction "
1364 "succeeded but created multiple issuances";
1370 if (tx.
getTxnType() == ttMPTOKEN_ISSUANCE_DESTROY ||
1375 JLOG(j.
fatal()) <<
"Invariant failed: MPT issuance deletion "
1376 "succeeded without removing a MPT issuance";
1380 JLOG(j.
fatal()) <<
"Invariant failed: MPT issuance deletion "
1381 "succeeded while creating MPT issuances";
1385 JLOG(j.
fatal()) <<
"Invariant failed: MPT issuance deletion "
1386 "succeeded but deleted multiple issuances";
1392 if (tx.
getTxnType() == ttMPTOKEN_AUTHORIZE ||
1399 JLOG(j.
fatal()) <<
"Invariant failed: MPT authorize "
1400 "succeeded but created MPT issuances";
1405 JLOG(j.
fatal()) <<
"Invariant failed: MPT authorize "
1406 "succeeded but deleted issuances";
1410 submittedByIssuer &&
1414 <<
"Invariant failed: MPT authorize submitted by issuer "
1415 "succeeded but created/deleted mptokens";
1419 !submittedByIssuer && (tx.
getTxnType() != ttVAULT_DEPOSIT) &&
1425 <<
"Invariant failed: MPT authorize submitted by holder "
1426 "succeeded but created/deleted bad number of mptokens";
1433 if (tx.
getTxnType() == ttMPTOKEN_ISSUANCE_SET)
1437 JLOG(j.
fatal()) <<
"Invariant failed: MPT issuance set "
1438 "succeeded while removing MPT issuances";
1442 JLOG(j.
fatal()) <<
"Invariant failed: MPT issuance set "
1443 "succeeded while creating MPT issuances";
1447 JLOG(j.
fatal()) <<
"Invariant failed: MPT issuance set "
1448 "succeeded while removing MPTokens";
1452 JLOG(j.
fatal()) <<
"Invariant failed: MPT issuance set "
1453 "succeeded while creating MPTokens";
1463 JLOG(j.
fatal()) <<
"Invariant failed: a MPT issuance was created";
1467 JLOG(j.
fatal()) <<
"Invariant failed: a MPT issuance was deleted";
1471 JLOG(j.
fatal()) <<
"Invariant failed: a MPToken was created";
1475 JLOG(j.
fatal()) <<
"Invariant failed: a MPToken was deleted";
1490 if (before && before->getType() != ltPERMISSIONED_DOMAIN)
1492 if (
after &&
after->getType() != ltPERMISSIONED_DOMAIN)
1497 auto const& credentials = sle->getFieldArray(sfAcceptedCredentials);
1508 for (
auto const& cred : sorted)
1510 auto const& credTx = credentials[i++];
1511 sleStatus.
isSorted_ = (cred.first == credTx[sfIssuer]) &&
1512 (cred.second == credTx[sfCredentialType]);
1546 JLOG(j.
fatal()) <<
"Invariant failed: permissioned domain with "
1554 JLOG(j.
fatal()) <<
"Invariant failed: permissioned domain bad "
1563 <<
"Invariant failed: permissioned domain credentials "
1571 <<
"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)
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
bool isPseudoAccount(std::shared_ptr< SLE const > sleAcct)
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_