20#include <xrpld/app/misc/AMMHelpers.h>
21#include <xrpld/app/misc/AMMUtils.h>
22#include <xrpld/app/tx/detail/InvariantCheck.h>
23#include <xrpld/app/tx/detail/NFTokenUtils.h>
24#include <xrpld/app/tx/detail/PermissionedDomainSet.h>
26#include <xrpl/basics/Log.h>
27#include <xrpl/beast/utility/instrumentation.h>
28#include <xrpl/ledger/CredentialHelpers.h>
29#include <xrpl/ledger/ReadView.h>
30#include <xrpl/ledger/View.h>
31#include <xrpl/protocol/Feature.h>
32#include <xrpl/protocol/Indexes.h>
33#include <xrpl/protocol/LedgerFormats.h>
34#include <xrpl/protocol/MPTIssue.h>
35#include <xrpl/protocol/SField.h>
36#include <xrpl/protocol/STArray.h>
37#include <xrpl/protocol/STNumber.h>
38#include <xrpl/protocol/SystemParameters.h>
39#include <xrpl/protocol/TER.h>
40#include <xrpl/protocol/TxFormats.h>
41#include <xrpl/protocol/Units.h>
42#include <xrpl/protocol/nftPageMask.h>
96 return safe_cast<Privilege>(
101#pragma push_macro("TRANSACTION")
104#define TRANSACTION(tag, value, name, delegatable, amendment, privileges, ...) \
106 return (privileges) & priv; \
114#include <xrpl/protocol/detail/transactions.macro>
122#pragma pop_macro("TRANSACTION")
144 JLOG(j.
fatal()) <<
"Invariant failed: fee paid was negative: "
153 JLOG(j.
fatal()) <<
"Invariant failed: fee paid exceeds system limit: "
162 JLOG(j.
fatal()) <<
"Invariant failed: fee paid is " << fee.
drops()
163 <<
" exceeds fee specified in transaction.";
187 switch (before->getType())
190 drops_ -= (*before)[sfBalance].xrp().drops();
194 ((*before)[sfAmount] - (*before)[sfBalance]).xrp().drops();
197 if (
isXRP((*before)[sfAmount]))
198 drops_ -= (*before)[sfAmount].xrp().drops();
207 switch (
after->getType())
210 drops_ += (*after)[sfBalance].xrp().drops();
214 drops_ += ((*after)[sfAmount] - (*after)[sfBalance])
220 drops_ += (*after)[sfAmount].xrp().drops();
240 JLOG(j.
fatal()) <<
"Invariant failed: XRP net change was positive: "
248 JLOG(j.
fatal()) <<
"Invariant failed: XRP net change of " <<
drops_
249 <<
" doesn't match fee " << fee.
drops();
264 auto isBad = [](
STAmount const& balance) {
265 if (!balance.native())
268 auto const drops = balance.xrp();
282 if (before && before->getType() == ltACCOUNT_ROOT)
283 bad_ |= isBad((*before)[sfBalance]);
285 if (
after &&
after->getType() == ltACCOUNT_ROOT)
299 JLOG(j.
fatal()) <<
"Invariant failed: incorrect account XRP balance";
316 if (pays < beast::zero)
319 if (gets < beast::zero)
323 return pays.
native() && gets.native();
326 if (before && before->getType() == ltOFFER)
327 bad_ |= isBad((*before)[sfTakerPays], (*before)[sfTakerGets]);
330 bad_ |= isBad((*
after)[sfTakerPays], (*after)[sfTakerGets]);
343 JLOG(j.
fatal()) <<
"Invariant failed: offer with a bad amount";
358 auto isBad = [](
STAmount const& amount) {
371 if (amount.holds<
Issue>())
373 if (amount <= beast::zero)
383 if (amount <= beast::zero)
393 if (before && before->getType() == ltESCROW)
394 bad_ |= isBad((*before)[sfAmount]);
404 if (
after &&
after->getType() == ltMPTOKEN_ISSUANCE)
406 auto const outstanding = (*after)[sfOutstandingAmount];
407 checkAmount(outstanding);
408 if (
auto const locked = (*
after)[~sfLockedAmount])
410 checkAmount(*locked);
411 bad_ = outstanding < *locked;
417 auto const mptAmount = (*after)[sfMPTAmount];
418 checkAmount(mptAmount);
419 if (
auto const locked = (*
after)[~sfLockedAmount])
421 checkAmount(*locked);
436 JLOG(j.
fatal()) <<
"Invariant failed: escrow specifies invalid amount";
451 if (isDelete && before && before->getType() == ltACCOUNT_ROOT)
473 JLOG(j.
fatal()) <<
"Invariant failed: account deletion "
474 "succeeded without deleting an account";
476 JLOG(j.
fatal()) <<
"Invariant failed: account deletion "
477 "succeeded but deleted multiple accounts!";
491 JLOG(j.
fatal()) <<
"Invariant failed: an account root was deleted";
503 if (isDelete && before && before->getType() == ltACCOUNT_ROOT)
519 [[maybe_unused]]
bool const enforce =
523 auto const objectExists = [&view, enforce, &j](
auto const& keylet) {
525 if (
auto const sle = view.
read(keylet))
528 auto const typeName = [&sle]() {
533 return item->getName();
538 <<
"Invariant failed: account deletion left behind a "
539 << typeName <<
" object";
544 "ripple::AccountRootsDeletedClean::finalize::objectExists : "
545 "account deletion left no objects behind");
553 auto const accountID = accountSLE->getAccountID(sfAccount);
557 if (objectExists(
std::invoke(keyletfunc, accountID)) && enforce)
572 if (key && objectExists(
Keylet{ltNFTOKEN_PAGE, *key}) && enforce)
580 if (accountSLE->isFieldPresent(*field))
582 auto const key = accountSLE->getFieldH256(*field);
600 if (before &&
after && before->getType() !=
after->getType())
605#pragma push_macro("LEDGER_ENTRY")
608#define LEDGER_ENTRY(tag, ...) case tag:
610 switch (
after->getType())
612#include <xrpl/protocol/detail/ledger_entries.macro>
621#pragma pop_macro("LEDGER_ENTRY")
638 JLOG(j.
fatal()) <<
"Invariant failed: ledger entry type mismatch";
643 JLOG(j.
fatal()) <<
"Invariant failed: invalid ledger entry type added";
657 if (
after &&
after->getType() == ltRIPPLE_STATE)
679 JLOG(j.
fatal()) <<
"Invariant failed: an XRP trust line was created";
691 if (
after &&
after->getType() == ltRIPPLE_STATE)
701 (lowDeepFreeze && !lowFreeze) || (highDeepFreeze && !highFreeze);
716 JLOG(j.
fatal()) <<
"Invariant failed: a trust line with deep freeze flag "
717 "without normal freeze was created";
745 if (balanceChange.signum() == 0)
777 [[maybe_unused]]
bool const enforce =
782 auto const issuerSle =
findIssuer(issue.account, view);
791 "ripple::TransfersNotFrozen::finalize : enforce "
816 after,
"ripple::TransfersNotFrozen::isValidEntry : valid after.");
822 if (
after->getType() == ltACCOUNT_ROOT)
834 return after->getType() == ltRIPPLE_STATE &&
835 (!before || before->getType() == ltRIPPLE_STATE);
844 auto const getBalance = [](
auto const& line,
auto const& other,
bool zero) {
846 line ? line->at(sfBalance) : other->at(sfBalance).zeroed();
847 return zero ? amt.
zeroed() : amt;
855 auto const balanceBefore = getBalance(before,
after,
false);
862 auto const balanceAfter = getBalance(
after, before, isDelete);
864 return balanceAfter - balanceBefore;
872 "ripple::TransfersNotFrozen::recordBalance : valid trustline "
876 changes.senders.emplace_back(std::move(change));
878 changes.receivers.emplace_back(std::move(change));
886 auto const balanceChangeSign = balanceChange.
signum();
887 auto const currency =
after->at(sfBalance).getCurrency();
891 {currency,
after->at(sfHighLimit).getIssuer()},
892 {
after, balanceChangeSign});
896 {currency,
after->at(sfLowLimit).getIssuer()},
897 {
after, -balanceChangeSign});
939 for (
auto const& change : actors)
941 bool const high = change.line->at(sfLowLimit).getIssuer() ==
942 issuer->at(sfAccount);
945 change, high, tx, j, enforce, globalFreeze))
965 bool const deepFreeze =
967 bool const frozen = globalFreeze || deepFreeze || freeze;
979 JLOG(j.
debug()) <<
"Invariant check allowing funds to be moved "
981 <<
" a frozen trustline for AMMClawback "
986 JLOG(j.
fatal()) <<
"Invariant failed: Attempting to move frozen funds for "
991 "ripple::TransfersNotFrozen::validateFrozenState : enforce "
1010 if (!before &&
after->getType() == ltACCOUNT_ROOT)
1032 JLOG(j.
fatal()) <<
"Invariant failed: multiple accounts "
1033 "created in a single transaction";
1040 bool const pseudoAccount =
1045 JLOG(j.
fatal()) <<
"Invariant failed: pseudo-account created by a "
1046 "wrong transaction type";
1059 JLOG(j.
fatal()) <<
"Invariant failed: account created with "
1060 "wrong starting sequence number";
1071 <<
"Invariant failed: pseudo-account created with "
1080 JLOG(j.
fatal()) <<
"Invariant failed: account root created illegally";
1093 static constexpr uint256 const accountBits = ~pageBits;
1095 if ((before && before->getType() != ltNFTOKEN_PAGE) ||
1096 (
after &&
after->getType() != ltNFTOKEN_PAGE))
1100 uint256 const account = sle->key() & accountBits;
1101 uint256 const hiLimit = sle->key() & pageBits;
1109 if (account != (*prev & accountBits))
1112 if (hiLimit <= (*prev & pageBits))
1116 if (
auto const next = (*sle)[~sfNextPageMin])
1118 if (account != (*next & accountBits))
1121 if (hiLimit >= (*next & pageBits))
1126 auto const& nftokens = sle->getFieldArray(sfNFTokens);
1129 if (
std::size_t const nftokenCount = nftokens.size();
1130 (!isDelete && nftokenCount == 0) ||
1137 prev ? *prev & pageBits :
uint256(beast::zero);
1141 for (
auto const& obj : nftokens)
1143 uint256 const tokenID = obj[sfNFTokenID];
1150 if (
uint256 const tokenPageBits = tokenID & pageBits;
1151 tokenPageBits < loLimit || tokenPageBits >= hiLimit)
1154 if (
auto uri = obj[~sfURI]; uri && uri->empty())
1168 before->isFieldPresent(sfPreviousPageMin))
1177 if (!isDelete && before &&
after)
1185 before->isFieldPresent(sfNextPageMin) &&
1186 !
after->isFieldPresent(sfNextPageMin))
1203 JLOG(j.
fatal()) <<
"Invariant failed: NFT page is improperly linked.";
1209 JLOG(j.
fatal()) <<
"Invariant failed: NFT found in incorrect page.";
1215 JLOG(j.
fatal()) <<
"Invariant failed: NFTs on page are not sorted.";
1221 JLOG(j.
fatal()) <<
"Invariant failed: NFT contains empty URI.";
1227 JLOG(j.
fatal()) <<
"Invariant failed: NFT page has invalid size.";
1235 JLOG(j.
fatal()) <<
"Invariant failed: Last NFT page deleted with "
1236 "non-empty directory.";
1241 JLOG(j.
fatal()) <<
"Invariant failed: Lost NextMinPage link.";
1256 if (before && before->getType() == ltACCOUNT_ROOT)
1262 if (
after &&
after->getType() == ltACCOUNT_ROOT)
1281 JLOG(j.
fatal()) <<
"Invariant failed: the number of minted tokens "
1282 "changed without a mint transaction!";
1288 JLOG(j.
fatal()) <<
"Invariant failed: the number of burned tokens "
1289 "changed without a burn transaction!";
1301 <<
"Invariant failed: successful minting didn't increase "
1302 "the number of minted tokens.";
1308 JLOG(j.
fatal()) <<
"Invariant failed: failed minting changed the "
1309 "number of minted tokens.";
1316 <<
"Invariant failed: minting changed the number of "
1329 <<
"Invariant failed: successful burning didn't increase "
1330 "the number of burned tokens.";
1337 JLOG(j.
fatal()) <<
"Invariant failed: failed burning changed the "
1338 "number of burned tokens.";
1345 <<
"Invariant failed: burning changed the number of "
1362 if (before && before->getType() == ltRIPPLE_STATE)
1365 if (before && before->getType() == ltMPTOKEN)
1385 <<
"Invariant failed: more than one trustline changed.";
1392 <<
"Invariant failed: more than one mptokens changed.";
1404 if (holderBalance.
signum() < 0)
1407 <<
"Invariant failed: trustline balance is negative";
1416 JLOG(j.
fatal()) <<
"Invariant failed: some trustlines were changed "
1417 "despite failure of the transaction.";
1423 JLOG(j.
fatal()) <<
"Invariant failed: some mptokens were changed "
1424 "despite failure of the transaction.";
1440 if (
after &&
after->getType() == ltMPTOKEN_ISSUANCE)
1471 JLOG(j.
fatal()) <<
"Invariant failed: transaction "
1472 "succeeded without creating a MPT issuance";
1476 JLOG(j.
fatal()) <<
"Invariant failed: transaction "
1477 "succeeded while removing MPT issuances";
1481 JLOG(j.
fatal()) <<
"Invariant failed: transaction "
1482 "succeeded but created multiple issuances";
1492 JLOG(j.
fatal()) <<
"Invariant failed: MPT issuance deletion "
1493 "succeeded without removing a MPT issuance";
1497 JLOG(j.
fatal()) <<
"Invariant failed: MPT issuance deletion "
1498 "succeeded while creating MPT issuances";
1502 JLOG(j.
fatal()) <<
"Invariant failed: MPT issuance deletion "
1503 "succeeded but deleted multiple issuances";
1512 bool const enforceEscrowFinish = (tx.
getTxnType() == ttESCROW_FINISH) &&
1519 enforceEscrowFinish)
1525 JLOG(j.
fatal()) <<
"Invariant failed: MPT authorize "
1526 "succeeded but created MPT issuances";
1531 JLOG(j.
fatal()) <<
"Invariant failed: MPT authorize "
1532 "succeeded but deleted issuances";
1536 submittedByIssuer &&
1540 <<
"Invariant failed: MPT authorize submitted by issuer "
1541 "succeeded but created/deleted mptokens";
1551 <<
"Invariant failed: MPT authorize submitted by holder "
1552 "succeeded but created/deleted bad number of mptokens";
1564 !enforceEscrowFinish,
1565 "ripple::ValidMPTIssuance::finalize",
1566 "not escrow finish tx");
1578 JLOG(j.
fatal()) <<
"Invariant failed: a MPT issuance was created";
1582 JLOG(j.
fatal()) <<
"Invariant failed: a MPT issuance was deleted";
1586 JLOG(j.
fatal()) <<
"Invariant failed: a MPToken was created";
1590 JLOG(j.
fatal()) <<
"Invariant failed: a MPToken was deleted";
1605 if (before && before->getType() != ltPERMISSIONED_DOMAIN)
1607 if (
after &&
after->getType() != ltPERMISSIONED_DOMAIN)
1612 auto const& credentials = sle->getFieldArray(sfAcceptedCredentials);
1623 for (
auto const& cred : sorted)
1625 auto const& credTx = credentials[i++];
1626 sleStatus.
isSorted_ = (cred.first == credTx[sfIssuer]) &&
1627 (cred.second == credTx[sfCredentialType]);
1661 JLOG(j.
fatal()) <<
"Invariant failed: permissioned domain with "
1669 JLOG(j.
fatal()) <<
"Invariant failed: permissioned domain bad "
1678 <<
"Invariant failed: permissioned domain credentials "
1686 <<
"Invariant failed: permissioned domain credentials "
1710 if (
after &&
after->getType() == ltACCOUNT_ROOT)
1712 bool const isPseudo = [&]() {
1719 if (
after->at(sfSequence) == 0)
1740 return after->isFieldPresent(*sf);
1745 error <<
"pseudo-account has " << numFields
1746 <<
" pseudo-account fields set";
1750 if (before && before->at(sfSequence) !=
after->at(sfSequence))
1759 if (
after->isFieldPresent(sfRegularKey))
1775 bool const enforce = view.
rules().
enabled(featureSingleAssetVault);
1780 "ripple::ValidPseudoAccounts::finalize : no bad "
1781 "changes or enforce invariant");
1784 for (
auto const& error :
errors_)
1786 JLOG(j.
fatal()) <<
"Invariant failed: " << error;
1804 if (
after->isFieldPresent(sfDomainID))
1810 if (
after->isFieldPresent(sfDomainID))
1818 (!
after->isFieldPresent(sfDomainID) ||
1819 !
after->isFieldPresent(sfAdditionalBooks) ||
1820 after->getFieldArray(sfAdditionalBooks).size() > 1))
1834 if ((txType != ttPAYMENT && txType != ttOFFER_CREATE) ||
1842 JLOG(j.
fatal()) <<
"Invariant failed: hybrid offer is malformed";
1853 JLOG(j.
fatal()) <<
"Invariant failed: domain doesn't exist";
1863 JLOG(j.
fatal()) <<
"Invariant failed: transaction"
1864 " consumed wrong domains";
1871 JLOG(j.
fatal()) <<
"Invariant failed: domain transaction"
1872 " affected regular offers";
1890 auto const type =
after->getType();
1900 (type == ltACCOUNT_ROOT &&
after->isFieldPresent(sfAMMID)))
1909 if (before->getType() == ltAMM)
1923 bool const positive = amount > beast::zero && amount2 > beast::zero &&
1924 lptAMMBalance > beast::zero;
1927 (amount == beast::zero && amount2 == beast::zero &&
1928 lptAMMBalance == beast::zero);
1939 JLOG(j.
error()) <<
"AMMVote invariant failed: "
1958 JLOG(j.
error()) <<
"AMMBid invariant failed: pool changed";
1991 <<
"AMMCreate invariant failed: AMM object is not created";
2001 tx[sfAmount].get<Issue>(),
2002 tx[sfAmount2].get<Issue>(),
2013 JLOG(j.
error()) <<
"AMMCreate invariant failed: " << amount <<
" "
2030 ?
"AMM object is not deleted on tesSUCCESS"
2031 :
"AMM object is changed on tecINCOMPLETE";
2032 JLOG(j.
error()) <<
"AMMDelete invariant failed: " << msg;
2047 JLOG(j.
error()) <<
"AMM swap invariant failed: AMM object changed";
2066 tx[sfAsset].get<Issue>(),
2067 tx[sfAsset2].get<Issue>(),
2074 auto const poolProductMean =
root2(amount * amount2);
2075 bool const nonNegativeBalances =
2079 auto weakInvariantCheck = [&]() {
2084 if (!nonNegativeBalances ||
2085 (!strongInvariantCheck && !weakInvariantCheck()))
2090 <<
" " << poolProductMean <<
" "
2112 JLOG(j.
error()) <<
"AMMDeposit invariant failed: AMM object is deleted";
2164 case ttAMM_CLAWBACK:
2165 case ttAMM_WITHDRAW:
2174 case ttOFFER_CREATE:
2191 "ValidVault::Vault::make : from Vault object");
2195 self.
asset = from.
at(sfAsset);
2209 from.
getType() == ltMPTOKEN_ISSUANCE,
2210 "ValidVault::Shares::make : from MPTokenIssuance object");
2230 after !=
nullptr && (before !=
nullptr || !isDelete),
2231 "ripple::ValidVault::visitEntry : some object is available");
2243 switch (before->getType())
2248 case ltMPTOKEN_ISSUANCE:
2253 before->getFieldU64(sfOutstandingAmount));
2258 static_cast<std::int64_t>(before->getFieldU64(sfMPTAmount));
2261 case ltACCOUNT_ROOT:
2262 case ltRIPPLE_STATE:
2263 balance = before->getFieldAmount(sfBalance);
2270 if (!isDelete &&
after)
2272 switch (
after->getType())
2277 case ltMPTOKEN_ISSUANCE:
2282 after->getFieldU64(sfOutstandingAmount)));
2290 case ltACCOUNT_ROOT:
2291 case ltRIPPLE_STATE:
2292 balance -=
Number(
after->getFieldAmount(sfBalance));
2299 uint256 const key = (before ? before->key() :
after->key());
2312 bool const enforce = view.
rules().
enabled(featureSingleAssetVault);
2322 "Invariant failed: vault operation succeeded without modifying "
2325 enforce,
"ripple::ValidVault::finalize : vault noop invariant");
2334 "Invariant failed: vault updated by a wrong transaction type";
2337 "ripple::ValidVault::finalize : illegal vault transaction "
2345 "Invariant failed: vault operation updated more than single vault";
2347 enforce,
"ripple::ValidVault::finalize : single vault invariant");
2357 if (txnType != ttVAULT_DELETE)
2360 "Invariant failed: vault deleted by a wrong transaction type";
2363 "ripple::ValidVault::finalize : illegal vault deletion "
2378 if (e.share.getMptID() == beforeVault.shareMPTID)
2379 return std::move(e);
2386 JLOG(j.
fatal()) <<
"Invariant failed: deleted vault must also "
2390 "ripple::ValidVault::finalize : shares deletion invariant");
2395 if (deletedShares->sharesTotal != 0)
2397 JLOG(j.
fatal()) <<
"Invariant failed: deleted vault must have no "
2398 "shares outstanding";
2401 if (beforeVault.assetsTotal !=
zero)
2403 JLOG(j.
fatal()) <<
"Invariant failed: deleted vault must have no "
2404 "assets outstanding";
2407 if (beforeVault.assetsAvailable !=
zero)
2409 JLOG(j.
fatal()) <<
"Invariant failed: deleted vault must have no "
2416 else if (txnType == ttVAULT_DELETE)
2418 JLOG(j.
fatal()) <<
"Invariant failed: vault deletion succeeded without "
2421 enforce,
"ripple::ValidVault::finalize : vault deletion invariant");
2429 "ripple::ValidVault::finalize : single vault operation");
2439 if (e.share.getMptID() == afterVault.shareMPTID)
2443 auto const sleShares =
2456 if (afterVault.asset != beforeVault.asset ||
2457 afterVault.pseudoId != beforeVault.pseudoId ||
2458 afterVault.shareMPTID != beforeVault.shareMPTID)
2461 <<
"Invariant failed: violation of vault immutable data";
2468 JLOG(j.
fatal()) <<
"Invariant failed: updated vault must have shares";
2471 "ripple::ValidVault::finalize : vault has shares invariant");
2475 if (updatedShares->sharesTotal == 0)
2477 if (afterVault.assetsTotal !=
zero)
2479 JLOG(j.
fatal()) <<
"Invariant failed: updated zero sized "
2480 "vault must have no assets outstanding";
2483 if (afterVault.assetsAvailable !=
zero)
2485 JLOG(j.
fatal()) <<
"Invariant failed: updated zero sized "
2486 "vault must have no assets available";
2490 else if (updatedShares->sharesTotal > updatedShares->sharesMaximum)
2493 <<
"Invariant failed: updated shares must not exceed maximum "
2494 << updatedShares->sharesMaximum;
2498 if (afterVault.assetsAvailable <
zero)
2501 <<
"Invariant failed: assets available must be positive";
2505 if (afterVault.assetsAvailable > afterVault.assetsTotal)
2507 JLOG(j.
fatal()) <<
"Invariant failed: assets available must "
2508 "not be greater than assets outstanding";
2512 afterVault.lossUnrealized >
2513 afterVault.assetsTotal - afterVault.assetsAvailable)
2516 <<
"Invariant failed: loss unrealized must not exceed "
2517 "the difference between assets outstanding and available";
2521 if (afterVault.assetsTotal <
zero)
2524 <<
"Invariant failed: assets outstanding must be positive";
2528 if (afterVault.assetsMaximum <
zero)
2530 JLOG(j.
fatal()) <<
"Invariant failed: assets maximum must be positive";
2539 "Invariant failed: vault created by a wrong transaction type";
2541 enforce,
"ripple::ValidVault::finalize : vault creation invariant");
2546 afterVault.lossUnrealized !=
beforeVault_[0].lossUnrealized)
2549 "Invariant failed: vault transaction must not change loss "
2561 if (e.share.getMptID() == beforeVault.shareMPTID)
2562 return std::move(e);
2567 if (!beforeShares &&
2572 JLOG(j.
fatal()) <<
"Invariant failed: vault operation succeeded "
2573 "without updating shares";
2575 enforce,
"ripple::ValidVault::finalize : shares noop invariant");
2579 auto const& vaultAsset = afterVault.asset;
2586 return it->second *
sign;
2590 [&]<
typename TIss>(TIss
const& issue) {
2597 id > issue.getIssuer() ? -1 : 1);
2605 vaultAsset.value());
2608 auto const it = [&]() {
2609 if (
id == afterVault.pseudoId)
2626 case ttVAULT_CREATE: {
2632 <<
"Invariant failed: create operation must not have "
2637 if (afterVault.assetsAvailable !=
zero ||
2638 afterVault.assetsTotal !=
zero ||
2639 afterVault.lossUnrealized !=
zero ||
2640 updatedShares->sharesTotal != 0)
2643 <<
"Invariant failed: created vault must be empty";
2647 if (afterVault.pseudoId != updatedShares->share.getIssuer())
2650 <<
"Invariant failed: shares issuer and vault "
2651 "pseudo-account must be the same";
2655 auto const sleSharesIssuer = view.
read(
2657 if (!sleSharesIssuer)
2660 <<
"Invariant failed: shares issuer must exist";
2667 <<
"Invariant failed: shares issuer must be a "
2672 if (
auto const vaultId = (*sleSharesIssuer)[~sfVaultID];
2673 !vaultId || *vaultId != afterVault.key)
2676 <<
"Invariant failed: shares issuer pseudo-account "
2677 "must point back to the vault";
2688 "ripple::ValidVault::finalize : set updated a vault");
2691 auto const vaultDeltaAssets = deltaAssets(afterVault.pseudoId);
2692 if (vaultDeltaAssets)
2695 "Invariant failed: set must not change vault balance";
2699 if (beforeVault.assetsTotal != afterVault.assetsTotal)
2702 "Invariant failed: set must not change assets "
2707 if (afterVault.assetsMaximum >
zero &&
2708 afterVault.assetsTotal > afterVault.assetsMaximum)
2711 "Invariant failed: set assets outstanding must not "
2712 "exceed assets maximum";
2716 if (beforeVault.assetsAvailable != afterVault.assetsAvailable)
2719 "Invariant failed: set must not change assets "
2724 if (beforeShares && updatedShares &&
2725 beforeShares->sharesTotal != updatedShares->sharesTotal)
2728 "Invariant failed: set must not change shares "
2735 case ttVAULT_DEPOSIT: {
2740 "ripple::ValidVault::finalize : deposit updated a vault");
2743 auto const vaultDeltaAssets = deltaAssets(afterVault.pseudoId);
2745 if (!vaultDeltaAssets)
2748 "Invariant failed: deposit must change vault balance";
2752 if (*vaultDeltaAssets > tx[sfAmount])
2755 "Invariant failed: deposit must not change vault "
2756 "balance by more than deposited amount";
2760 if (*vaultDeltaAssets <=
zero)
2763 "Invariant failed: deposit must increase vault balance";
2769 bool const issuerDeposit = [&]() ->
bool {
2770 if (vaultAsset.native())
2772 return tx[sfAccount] == vaultAsset.getIssuer();
2777 auto const accountDeltaAssets =
2779 if (
auto ret = deltaAssets(tx[sfAccount]); ret)
2783 if (vaultAsset.native())
2784 *ret += fee.
drops();
2791 if (!accountDeltaAssets)
2794 "Invariant failed: deposit must change depositor "
2799 if (*accountDeltaAssets >=
zero)
2802 "Invariant failed: deposit must decrease depositor "
2807 if (*accountDeltaAssets * -1 != *vaultDeltaAssets)
2810 "Invariant failed: deposit must change vault and "
2811 "depositor balance by equal amount";
2816 if (afterVault.assetsMaximum >
zero &&
2817 afterVault.assetsTotal > afterVault.assetsMaximum)
2820 "Invariant failed: deposit assets outstanding must not "
2821 "exceed assets maximum";
2825 auto const accountDeltaShares = deltaShares(tx[sfAccount]);
2826 if (!accountDeltaShares)
2829 "Invariant failed: deposit must change depositor "
2834 if (*accountDeltaShares <=
zero)
2837 "Invariant failed: deposit must increase depositor "
2842 auto const vaultDeltaShares = deltaShares(afterVault.pseudoId);
2843 if (!vaultDeltaShares)
2846 "Invariant failed: deposit must change vault shares";
2850 if (*vaultDeltaShares * -1 != *accountDeltaShares)
2853 "Invariant failed: deposit must change depositor and "
2854 "vault shares by equal amount";
2858 if (beforeVault.assetsTotal + *vaultDeltaAssets !=
2859 afterVault.assetsTotal)
2861 JLOG(j.
fatal()) <<
"Invariant failed: deposit and assets "
2862 "outstanding must add up";
2865 if (beforeVault.assetsAvailable + *vaultDeltaAssets !=
2866 afterVault.assetsAvailable)
2868 JLOG(j.
fatal()) <<
"Invariant failed: deposit and assets "
2869 "available must add up";
2875 case ttVAULT_WITHDRAW: {
2880 "ripple::ValidVault::finalize : withdrawal updated a "
2884 auto const vaultDeltaAssets = deltaAssets(afterVault.pseudoId);
2886 if (!vaultDeltaAssets)
2888 JLOG(j.
fatal()) <<
"Invariant failed: withdrawal must "
2889 "change vault balance";
2893 if (*vaultDeltaAssets >=
zero)
2895 JLOG(j.
fatal()) <<
"Invariant failed: withdrawal must "
2896 "decrease vault balance";
2902 bool const issuerWithdrawal = [&]() ->
bool {
2903 if (vaultAsset.native())
2905 auto const destination =
2906 tx[~sfDestination].value_or(tx[sfAccount]);
2907 return destination == vaultAsset.getIssuer();
2910 if (!issuerWithdrawal)
2912 auto const accountDeltaAssets =
2914 if (
auto ret = deltaAssets(tx[sfAccount]); ret)
2918 if (vaultAsset.native())
2919 *ret += fee.
drops();
2926 auto const otherAccountDelta =
2928 if (
auto const destination = tx[~sfDestination];
2929 destination && *destination != tx[sfAccount])
2930 return deltaAssets(*destination);
2934 if (accountDeltaAssets.has_value() ==
2935 otherAccountDelta.has_value())
2938 "Invariant failed: withdrawal must change one "
2939 "destination balance";
2943 auto const destinationDelta =
2944 accountDeltaAssets ? *accountDeltaAssets
2945 : *otherAccountDelta;
2947 if (destinationDelta <=
zero)
2950 "Invariant failed: withdrawal must increase "
2951 "destination balance";
2955 if (*vaultDeltaAssets * -1 != destinationDelta)
2958 "Invariant failed: withdrawal must change vault "
2959 "and destination balance by equal amount";
2964 auto const accountDeltaShares = deltaShares(tx[sfAccount]);
2965 if (!accountDeltaShares)
2968 "Invariant failed: withdrawal must change depositor "
2973 if (*accountDeltaShares >=
zero)
2976 "Invariant failed: withdrawal must decrease depositor "
2981 auto const vaultDeltaShares = deltaShares(afterVault.pseudoId);
2982 if (!vaultDeltaShares)
2985 "Invariant failed: withdrawal must change vault shares";
2989 if (*vaultDeltaShares * -1 != *accountDeltaShares)
2992 "Invariant failed: withdrawal must change depositor "
2993 "and vault shares by equal amount";
2998 if (beforeVault.assetsTotal + *vaultDeltaAssets !=
2999 afterVault.assetsTotal)
3001 JLOG(j.
fatal()) <<
"Invariant failed: withdrawal and "
3002 "assets outstanding must add up";
3006 if (beforeVault.assetsAvailable + *vaultDeltaAssets !=
3007 afterVault.assetsAvailable)
3009 JLOG(j.
fatal()) <<
"Invariant failed: withdrawal and "
3010 "assets available must add up";
3016 case ttVAULT_CLAWBACK: {
3021 "ripple::ValidVault::finalize : clawback updated a vault");
3024 if (vaultAsset.native() ||
3025 vaultAsset.getIssuer() != tx[sfAccount])
3028 "Invariant failed: clawback may only be performed by "
3033 auto const vaultDeltaAssets = deltaAssets(afterVault.pseudoId);
3035 if (!vaultDeltaAssets)
3038 "Invariant failed: clawback must change vault balance";
3042 if (*vaultDeltaAssets >=
zero)
3045 "Invariant failed: clawback must decrease vault "
3050 auto const accountDeltaShares = deltaShares(tx[sfHolder]);
3051 if (!accountDeltaShares)
3054 "Invariant failed: clawback must change holder shares";
3058 if (*accountDeltaShares >=
zero)
3061 "Invariant failed: clawback must decrease holder "
3066 auto const vaultDeltaShares = deltaShares(afterVault.pseudoId);
3067 if (!vaultDeltaShares)
3070 "Invariant failed: clawback must change vault shares";
3074 if (*vaultDeltaShares * -1 != *accountDeltaShares)
3077 "Invariant failed: clawback must change holder and "
3078 "vault shares by equal amount";
3082 if (beforeVault.assetsTotal + *vaultDeltaAssets !=
3083 afterVault.assetsTotal)
3086 "Invariant failed: clawback and assets outstanding "
3091 if (beforeVault.assetsAvailable + *vaultDeltaAssets !=
3092 afterVault.assetsAvailable)
3095 "Invariant failed: clawback and assets available must "
3106 "ripple::ValidVault::finalize : unknown transaction type");
3116 XRPL_ASSERT(enforce,
"ripple::ValidVault::finalize : vault invariants");
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.
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.
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.
LedgerEntryType getType() const
uint256 const & key() const
Returns the 'key' (or 'index') of this item.
uint192 getFieldH192(SField const &field) const
AccountID getAccountID(SField const &field) const
T::value_type at(TypedField< T > const &f) const
Get the value of a field.
std::uint32_t getFieldU32(SField const &field) const
STAmount const & getFieldAmount(SField const &field) const
uint256 getHash(HashPrefix prefix) const
bool isFieldPresent(SField const &field) const
uint256 getFieldH256(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 &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
bool finalizeWithdraw(STTx const &, ReadView const &, bool enforce, beast::Journal const &) const
bool finalizeDEX(bool enforce, beast::Journal const &) const
std::optional< STAmount > lptAMMBalanceAfter_
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool finalizeBid(bool enforce, beast::Journal const &) const
std::optional< AccountID > ammAccount_
bool finalizeDelete(bool enforce, TER res, beast::Journal const &) const
bool finalizeCreate(STTx const &, ReadView const &, bool enforce, beast::Journal const &) const
bool finalizeVote(bool enforce, beast::Journal const &) const
bool finalizeDeposit(STTx const &, ReadView const &, bool enforce, beast::Journal const &) const
bool generalInvariant(STTx const &, ReadView const &, ZeroAllowed zeroAllowed, beast::Journal const &) const
std::optional< STAmount > lptAMMBalanceBefore_
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 &)
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 &)
hash_set< uint256 > domains_
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 &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
std::vector< std::string > errors_
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< Shares > beforeMPTs_
std::vector< Vault > beforeVault_
std::unordered_map< uint256, Number > deltas_
static Number constexpr zero
std::vector< Shares > afterMPTs_
std::vector< Vault > afterVault_
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > 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 &)
T emplace_back(T... args)
std::set< std::pair< AccountID, Slice > > makeSorted(STArray const &credentials)
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Keylet permissionedDomain(AccountID const &account, std::uint32_t seq) noexcept
Keylet line(AccountID const &id0, AccountID const &id1, Currency const ¤cy) noexcept
The index of a trust line for a given currency.
Keylet mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet unchecked(uint256 const &key) noexcept
Any ledger entry.
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.
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
bool isXRP(AccountID const &c)
constexpr base_uint< Bits, Tag > operator|(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
static bool validBalances(STAmount const &amount, STAmount const &amount2, STAmount const &lptAMMBalance, ValidAMM::ZeroAllowed zeroAllowed)
std::size_t constexpr maxPermissionedDomainCredentialsArraySize
The maximum number of credentials can be passed in array for permissioned domain.
bool hasPrivilege(STTx const &tx, Privilege priv)
constexpr std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > safe_cast(Src s) noexcept
std::uint64_t constexpr maxMPTokenAmount
The maximum amount of MPTokenIssuance.
constexpr XRPAmount INITIAL_XRP
Configure the native currency.
std::size_t constexpr dirMaxTokensPerPage
The maximum number of items in an NFT page.
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
std::array< keyletDesc< AccountID const & >, 6 > const directAccountKeylets
std::pair< STAmount, STAmount > ammPoolHolds(ReadView const &view, AccountID const &ammAccountID, Issue const &issue1, Issue const &issue2, FreezeHandling freezeHandling, beast::Journal const j)
Get AMM pool balances.
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
bool isTesSuccess(TER x) noexcept
STAmount ammLPTokens(STAmount const &asset1, STAmount const &asset2, Issue const &lptIssue)
Calculate LP Tokens given AMM pool reserves.
T get(Section const §ion, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
@ transactionID
transaction plus signature to give transaction ID
MPTID makeMptID(std::uint32_t sequence, AccountID const &account)
std::vector< SField const * > const & getPseudoAccountFields()
bool withinRelativeDistance(Quality const &calcQuality, Quality const &reqQuality, Number const &dist)
Check if the relative distance between the qualities is within the requested distance.
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_
std::uint64_t sharesMaximum
static Shares make(SLE const &)
std::uint64_t sharesTotal
static Vault make(SLE const &)