20#include <xrpld/app/misc/CredentialHelpers.h>
21#include <xrpld/app/tx/detail/MPTokenAuthorize.h>
22#include <xrpld/ledger/ReadView.h>
23#include <xrpld/ledger/View.h>
25#include <xrpl/basics/Expected.h>
26#include <xrpl/basics/Log.h>
27#include <xrpl/basics/chrono.h>
28#include <xrpl/beast/utility/instrumentation.h>
29#include <xrpl/protocol/Feature.h>
30#include <xrpl/protocol/Indexes.h>
31#include <xrpl/protocol/LedgerFormats.h>
32#include <xrpl/protocol/MPTIssue.h>
33#include <xrpl/protocol/Protocol.h>
34#include <xrpl/protocol/Quality.h>
35#include <xrpl/protocol/TER.h>
36#include <xrpl/protocol/TxFlags.h>
37#include <xrpl/protocol/digest.h>
38#include <xrpl/protocol/st.h>
51 std::is_same_v<std::remove_cv_t<N>,
SLE> &&
52 std::is_base_of_v<ReadView, V>>>
61 auto const& svIndexes = page->getFieldV256(sfIndexes);
63 index <= svIndexes.size(),
64 "ripple::detail::internalDirNext : index inside range");
66 if (index >= svIndexes.size())
68 auto const next = page->getFieldU64(sfIndexNext);
76 if constexpr (std::is_const_v<N>)
81 XRPL_ASSERT(page,
"ripple::detail::internalDirNext : non-null root");
91 entry = svIndexes[index++];
99 std::is_same_v<std::remove_cv_t<N>,
SLE> &&
100 std::is_base_of_v<ReadView, V>>>
109 if constexpr (std::is_const_v<N>)
206 if constexpr (std::is_same_v<TIss, Issue>)
223 if (issuer != account)
260 if (issuer != account)
293 for (
auto const& account : accounts)
299 for (
auto const& account : accounts)
321 auto const mptIssuance =
323 if (mptIssuance ==
nullptr)
326 auto const issuer = mptIssuance->getAccountID(sfIssuer);
328 if (mptIssuer ==
nullptr)
330 UNREACHABLE(
"ripple::isVaultPseudoAccountFrozen : null MPToken issuer");
334 if (!mptIssuer->isFieldPresent(sfVaultID))
339 if (vault ==
nullptr)
341 UNREACHABLE(
"ripple::isVaultPseudoAccountFrozen : null vault");
345 return isAnyFrozen(view, {issuer, account}, vault->at(sfAsset), depth + 1);
360 if (issuer == account)
402 auto const allowBalance = [&]() {
410 if (
isFrozen(view, account, currency, issuer) ||
425 else if (sleIssuer->isFieldPresent(sfAMMID))
434 (*sleAmm)[sfAsset].get<Issue>(),
435 (*sleAmm)[sfAsset2].get<Issue>()))
448 amount = sle->getFieldAmount(sfBalance);
449 if (account > issuer)
461 JLOG(j.
trace()) <<
"accountHolds:"
495 amount.
clear(mptIssue);
498 amount.
clear(mptIssue);
501 amount =
STAmount{mptIssue, sleMpt->getFieldU64(sfMPTAmount)};
511 amount.
clear(mptIssue);
515 auto const sleIssuance =
522 amount.
clear(mptIssue);
529[[nodiscard]] STAmount
539 [&](
auto const& value) {
544 return accountHolds(view, account, value, zeroIfFrozen, j);
547 view, account, value, zeroIfFrozen, zeroIfUnauthorized, j);
594 <<
"Account " << *
id <<
" owner count exceeds max!";
607 <<
"Account " << *
id <<
" owner count set below 0!";
610 XRPL_ASSERT(!
id,
"ripple::confineOwnerCount : id is not set");
629 view.
ownerCountHook(
id, sle->getFieldU32(sfOwnerCount)), ownerCountAdj);
632 auto const reserve = sle->isFieldPresent(sfAMMID)
636 auto const fullBalance = sle->getFieldAmount(sfBalance);
641 (balance < reserve) ?
STAmount{0} : balance - reserve;
643 JLOG(j.
trace()) <<
"accountHolds:"
646 <<
" fullBalance=" << fullBalance.getFullText()
647 <<
" balance=" << balance.getFullText()
648 <<
" reserve=" << reserve <<
" ownerCount=" << ownerCount
649 <<
" ownerCountAdj=" << ownerCountAdj;
661 root.type == ltDIR_NODE,
"ripple::forEachItem : valid root type");
663 if (
root.type != ltDIR_NODE)
670 auto sle = view.
read(pos);
673 for (
auto const& key : sle->getFieldV256(sfIndexes))
675 auto const next = sle->getFieldU64(sfIndexNext);
692 root.type == ltDIR_NODE,
"ripple::forEachItemAfter : valid root type");
694 if (
root.type != ltDIR_NODE)
697 auto currentIndex =
root;
700 if (
after.isNonZero())
704 if (
auto hintDir = view.
read(hintIndex))
706 for (
auto const& key : hintDir->getFieldV256(sfIndexes))
711 currentIndex = hintIndex;
720 auto const ownerDir = view.
read(currentIndex);
723 for (
auto const& key : ownerDir->getFieldV256(sfIndexes))
736 auto const uNodeNext = ownerDir->getFieldU64(sfIndexNext);
746 auto const ownerDir = view.
read(currentIndex);
749 for (
auto const& key : ownerDir->getFieldV256(sfIndexes))
752 auto const uNodeNext = ownerDir->getFieldU64(sfIndexNext);
765 if (sle && sle->isFieldPresent(sfTransferRate))
766 return Rate{sle->getFieldU32(sfTransferRate)};
778 sle && sle->isFieldPresent(sfTransferFee))
779 return Rate{1'000'000'000u + 10'000 * sle->getFieldU16(sfTransferFee)};
800 if (hash && (*hash != validLedger.
info().
hash))
802 JLOG(s) << reason <<
" incompatible with valid ledger";
804 JLOG(s) <<
"Hash(VSeq): " <<
to_string(*hash);
816 if (hash && (*hash != testLedger.
info().
hash))
818 JLOG(s) << reason <<
" incompatible preceding ledger";
820 JLOG(s) <<
"Hash(NSeq): " <<
to_string(*hash);
830 JLOG(s) << reason <<
" incompatible ledger";
837 JLOG(s) <<
"Val: " << validLedger.
info().
seq <<
" "
840 JLOG(s) <<
"New: " << testLedger.
info().
seq <<
" "
857 if (testLedger.
info().
seq > validIndex)
864 if (hash && (*hash != validHash))
866 JLOG(s) << reason <<
" incompatible following ledger";
867 JLOG(s) <<
"Hash(VSeq): " <<
to_string(*hash);
873 (validIndex == testLedger.
info().
seq) &&
874 (testLedger.
info().
hash != validHash))
876 JLOG(s) << reason <<
" incompatible ledger";
883 JLOG(s) <<
"Val: " << validIndex <<
" " <<
to_string(validHash);
885 JLOG(s) <<
"New: " << testLedger.
info().
seq <<
" "
895 auto const sleNode = view.
read(k);
898 if (!sleNode->getFieldV256(sfIndexes).empty())
903 return sleNode->getFieldU64(sfIndexNext) == 0;
913 if (sle->isFieldPresent(sfAmendments))
915 auto const& v = sle->getFieldV256(sfAmendments);
916 amendments.insert(v.begin(), v.end());
930 if (sle->isFieldPresent(sfMajorities))
933 using d = tp::duration;
935 auto const majorities = sle->getFieldArray(sfMajorities);
937 for (
auto const& m : majorities)
938 ret[m.getFieldH256(sfAmendment)] =
939 tp(d(m.getFieldU32(sfCloseTime)));
950 if (seq > ledger.
seq())
953 <<
"Can't get seq " << seq <<
" from " << ledger.
seq() <<
" future";
956 if (seq == ledger.
seq())
958 if (seq == (ledger.
seq() - 1))
961 if (
int diff = ledger.
seq() - seq; diff <= 256)
968 hashIndex->getFieldU32(sfLastLedgerSequence) ==
970 "ripple::hashOfSeq : matching ledger sequence");
971 STVector256 vec = hashIndex->getFieldV256(sfHashes);
972 if (vec.
size() >= diff)
973 return vec[vec.
size() - diff];
975 <<
"Ledger " << ledger.
seq() <<
" missing hash for " << seq
976 <<
" (" << vec.
size() <<
"," << diff <<
")";
981 <<
"Ledger " << ledger.
seq() <<
":" << ledger.
info().
hash
982 <<
" missing normal list";
986 if ((seq & 0xff) != 0)
988 JLOG(journal.
debug())
989 <<
"Can't get seq " << seq <<
" from " << ledger.
seq() <<
" past";
997 auto const lastSeq = hashIndex->getFieldU32(sfLastLedgerSequence);
998 XRPL_ASSERT(lastSeq >= seq,
"ripple::hashOfSeq : minimum last ledger");
1000 (lastSeq & 0xff) == 0,
"ripple::hashOfSeq : valid last ledger");
1001 auto const diff = (lastSeq - seq) >> 8;
1002 STVector256 vec = hashIndex->getFieldV256(sfHashes);
1003 if (vec.
size() > diff)
1004 return vec[vec.
size() - diff - 1];
1006 JLOG(journal.
warn()) <<
"Can't get seq " << seq <<
" from " << ledger.
seq()
1008 return std::nullopt;
1026 XRPL_ASSERT(amount,
"ripple::adjustOwnerCount : nonzero amount input");
1031 sle->setFieldU32(sfOwnerCount, adjusted);
1039 (*sle)[sfOwner] = account;
1050 object->setFieldU64(sfOwnerNode, *page);
1063 rsh(hash.data(), hash.size());
1081Expected<std::shared_ptr<SLE>,
TER>
1084 uint256 const& pseudoOwnerKey,
1085 SField const& ownerField)
1091 [&ownerField](
SField const* sf) ->
bool {
1092 return *sf == ownerField;
1094 "ripple::createPseudoAccount : valid owner field");
1097 if (accountId == beast::zero)
1102 account->setAccountID(sfAccount, accountId);
1103 account->setFieldAmount(sfBalance,
STAmount{});
1112 account->setFieldU32(sfSequence, seqno);
1116 account->setFieldU32(
1119 account->setFieldH256(ownerField, pseudoOwnerKey);
1131 return sleAcct && sleAcct->getType() == ltACCOUNT_ROOT &&
1135 [&sleAcct](
SField const* sf) ->
bool {
1136 return sleAcct->isFieldPresent(*sf);
1152 auto const& issuerId = issue.
getIssuer();
1153 auto const& currency = issue.
currency;
1157 auto const& srcId = issuerId;
1158 auto const& dstId = accountID;
1159 auto const high = srcId > dstId;
1160 auto const index =
keylet::line(srcId, dstId, currency);
1163 if (!sleDst || !sleSrc)
1168 if (view.
read(index))
1181 STAmount{Issue{currency, noAccount()}},
1182 STAmount{Issue{currency, dstId}},
1196 auto const& mptID = mptIssue.
getMptID();
1197 auto const mpt = view.
peek(keylet::mptIssuance(mptID));
1202 if (view.
peek(keylet::mptoken(mptID, accountID)))
1205 return MPTokenAuthorize::authorize(
1208 {.priorBalance = priorBalance,
1209 .mptIssuanceID = mptID,
1210 .account = accountID});
1216 bool const bSrcHigh,
1222 bool const bNoRipple,
1233 JLOG(j.
trace()) <<
"trustCreate: " << to_string(uSrcAccountID) <<
", "
1234 << to_string(uDstAccountID) <<
", "
1237 auto const& uLowAccountID = !bSrcHigh ? uSrcAccountID : uDstAccountID;
1238 auto const& uHighAccountID = bSrcHigh ? uSrcAccountID : uDstAccountID;
1240 auto const sleRippleState = std::make_shared<SLE>(ltRIPPLE_STATE, uIndex);
1241 view.
insert(sleRippleState);
1244 keylet::ownerDir(uLowAccountID),
1245 sleRippleState->key(),
1252 keylet::ownerDir(uHighAccountID),
1253 sleRippleState->key(),
1259 bool const bSetDst = saLimit.
getIssuer() == uDstAccountID;
1260 bool const bSetHigh = bSrcHigh ^ bSetDst;
1262 XRPL_ASSERT(sleAccount,
"ripple::trustCreate : non-null SLE");
1267 sleAccount->getAccountID(sfAccount) ==
1268 (bSetHigh ? uHighAccountID : uLowAccountID),
1269 "ripple::trustCreate : matching account ID");
1270 auto const slePeer =
1271 view.
peek(keylet::account(bSetHigh ? uLowAccountID : uHighAccountID));
1276 sleRippleState->setFieldU64(sfLowNode, *lowNode);
1277 sleRippleState->setFieldU64(sfHighNode, *highNode);
1279 sleRippleState->setFieldAmount(
1280 bSetHigh ? sfHighLimit : sfLowLimit, saLimit);
1281 sleRippleState->setFieldAmount(
1282 bSetHigh ? sfLowLimit : sfHighLimit,
1284 saBalance.
getCurrency(), bSetDst ? uSrcAccountID : uDstAccountID}));
1287 sleRippleState->setFieldU32(
1288 bSetHigh ? sfHighQualityIn : sfLowQualityIn, uQualityIn);
1291 sleRippleState->setFieldU32(
1292 bSetHigh ? sfHighQualityOut : sfLowQualityOut, uQualityOut);
1319 sleRippleState->setFieldU32(sfFlags, uFlags);
1323 sleRippleState->setFieldAmount(
1324 sfBalance, bSetHigh ? -saBalance : saBalance);
1327 uSrcAccountID, uDstAccountID, saBalance, saBalance.
zeroed());
1341 auto const sle = view.
read(keylet::account(accountID));
1344 auto const balance = sle->getFieldAmount(sfBalance);
1345 if (balance.xrp() != 0)
1351 auto const line = view.
peek(keylet::line(accountID, issue));
1354 if (line->at(sfBalance)->iou() != beast::zero)
1361 auto sleLowAccount =
1362 view.
peek(keylet::account(line->at(sfLowLimit)->getIssuer()));
1375 auto sleHighAccount =
1376 view.
peek(keylet::account(line->at(sfHighLimit)->getIssuer()));
1377 if (!sleHighAccount)
1389 line->at(sfLowLimit)->getIssuer(),
1390 line->at(sfHighLimit)->getIssuer(),
1401 auto const& mptID = mptIssue.
getMptID();
1402 auto const mptoken = view.
peek(keylet::mptoken(mptID, accountID));
1405 if (mptoken->at(sfMPTAmount) != 0)
1408 return MPTokenAuthorize::authorize(
1411 {.priorBalance = {},
1412 .mptIssuanceID = mptID,
1413 .account = accountID,
1426 std::uint64_t uLowNode = sleRippleState->getFieldU64(sfLowNode);
1427 std::uint64_t uHighNode = sleRippleState->getFieldU64(sfHighNode);
1429 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: low";
1432 keylet::ownerDir(uLowAccountID),
1434 sleRippleState->key(),
1440 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: high";
1443 keylet::ownerDir(uHighAccountID),
1445 sleRippleState->key(),
1451 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: state";
1452 view.
erase(sleRippleState);
1462 auto offerIndex = sle->key();
1463 auto owner = sle->getAccountID(sfAccount);
1466 uint256 uDirectory = sle->getFieldH256(sfBookDirectory);
1469 keylet::ownerDir(owner),
1470 sle->getFieldU64(sfOwnerNode),
1478 keylet::page(uDirectory),
1479 sle->getFieldU64(sfBookNode),
1486 if (sle->isFieldPresent(sfAdditionalBooks))
1489 sle->isFlag(
lsfHybrid) && sle->isFieldPresent(sfDomainID),
1490 "ripple::offerDelete : should be a hybrid domain offer");
1492 auto const& additionalBookDirs = sle->getFieldArray(sfAdditionalBooks);
1494 for (
auto const& bookDir : additionalBookDirs)
1496 auto const& dirIndex = bookDir.getFieldH256(sfBookDirectory);
1497 auto const& dirNode = bookDir.getFieldU64(sfBookNode);
1500 keylet::page(dirIndex), dirNode, offerIndex,
false))
1532 !bCheckIssuer || uSenderID == issuer || uReceiverID == issuer,
1533 "ripple::rippleCreditIOU : matching issuer or don't care");
1538 uSenderID != uReceiverID,
1539 "ripple::rippleCreditIOU : sender is not receiver");
1541 bool const bSenderHigh = uSenderID > uReceiverID;
1542 auto const index = keylet::line(uSenderID, uReceiverID, currency);
1546 "ripple::rippleCreditIOU : sender is not XRP");
1549 "ripple::rippleCreditIOU : receiver is not XRP");
1552 if (
auto const sleRippleState = view.
peek(index))
1554 STAmount saBalance = sleRippleState->getFieldAmount(sfBalance);
1559 view.
creditHook(uSenderID, uReceiverID, saAmount, saBalance);
1561 STAmount const saBefore = saBalance;
1563 saBalance -= saAmount;
1565 JLOG(j.
trace()) <<
"rippleCreditIOU: " << to_string(uSenderID) <<
" -> "
1566 << to_string(uReceiverID)
1571 std::uint32_t const uFlags(sleRippleState->getFieldU32(sfFlags));
1572 bool bDelete =
false;
1576 if (saBefore > beast::zero
1578 && saBalance <= beast::zero
1586 view.
read(keylet::account(uSenderID))->getFlags() &
1589 !sleRippleState->getFieldAmount(
1590 !bSenderHigh ? sfLowLimit : sfHighLimit)
1592 && !sleRippleState->getFieldU32(
1593 !bSenderHigh ? sfLowQualityIn : sfHighQualityIn)
1595 && !sleRippleState->getFieldU32(
1596 !bSenderHigh ? sfLowQualityOut : sfHighQualityOut))
1601 view, view.
peek(keylet::account(uSenderID)), -1, j);
1604 sleRippleState->setFieldU32(
1609 bDelete = !saBalance
1618 sleRippleState->setFieldAmount(sfBalance, saBalance);
1626 bSenderHigh ? uReceiverID : uSenderID,
1627 !bSenderHigh ? uReceiverID : uSenderID,
1631 view.
update(sleRippleState);
1635 STAmount const saReceiverLimit(
Issue{currency, uReceiverID});
1640 JLOG(j.
debug()) <<
"rippleCreditIOU: "
1642 << to_string(uSenderID) <<
" -> " << to_string(uReceiverID)
1645 auto const sleAccount = view.
peek(keylet::account(uReceiverID));
1682 auto const issuer = saAmount.
getIssuer();
1686 "ripple::rippleSendIOU : neither sender nor receiver is XRP");
1688 uSenderID != uReceiverID,
1689 "ripple::rippleSendIOU : sender is not receiver");
1691 if (uSenderID == issuer || uReceiverID == issuer || issuer ==
noAccount())
1698 saActual = saAmount;
1706 saActual = (waiveFee == WaiveTransferFee::Yes)
1710 JLOG(j.
debug()) <<
"rippleSendIOU> " << to_string(uSenderID) <<
" - > "
1711 << to_string(uReceiverID)
1719 terResult =
rippleCreditIOU(view, uSenderID, issuer, saActual,
true, j);
1744 "ripple::accountSendIOU : minimum amount and not MPT");
1750 if (!saAmount || (uSenderID == uReceiverID))
1757 JLOG(j.
trace()) <<
"accountSendIOU: " << to_string(uSenderID) <<
" -> "
1758 << to_string(uReceiverID) <<
" : "
1762 view, uSenderID, uReceiverID, saAmount, saActual, j, waiveFee);
1773 ? view.
peek(keylet::account(uSenderID))
1776 ? view.
peek(keylet::account(uReceiverID))
1779 if (
auto stream = j.
trace())
1785 sender_bal = sender->getFieldAmount(sfBalance).getFullText();
1788 receiver_bal = receiver->getFieldAmount(sfBalance).getFullText();
1790 stream <<
"accountSendIOU> " << to_string(uSenderID) <<
" ("
1791 << sender_bal <<
") -> " << to_string(uReceiverID) <<
" ("
1792 << receiver_bal <<
") : " << saAmount.
getFullText();
1797 if (sender->getFieldAmount(sfBalance) < saAmount)
1806 auto const sndBal = sender->getFieldAmount(sfBalance);
1810 sender->setFieldAmount(sfBalance, sndBal - saAmount);
1818 auto const rcvBal = receiver->getFieldAmount(sfBalance);
1819 receiver->setFieldAmount(sfBalance, rcvBal + saAmount);
1825 if (
auto stream = j.
trace())
1831 sender_bal = sender->getFieldAmount(sfBalance).getFullText();
1834 receiver_bal = receiver->getFieldAmount(sfBalance).getFullText();
1836 stream <<
"accountSendIOU< " << to_string(uSenderID) <<
" ("
1837 << sender_bal <<
") -> " << to_string(uReceiverID) <<
" ("
1838 << receiver_bal <<
") : " << saAmount.
getFullText();
1854 auto const issuer = saAmount.
getIssuer();
1855 auto sleIssuance = view.
peek(mptID);
1858 if (uSenderID == issuer)
1860 (*sleIssuance)[sfOutstandingAmount] += saAmount.
mpt().
value();
1861 view.
update(sleIssuance);
1865 auto const mptokenID = keylet::mptoken(mptID.key, uSenderID);
1866 if (
auto sle = view.
peek(mptokenID))
1868 auto const amt = sle->getFieldU64(sfMPTAmount);
1869 auto const pay = saAmount.
mpt().
value();
1872 (*sle)[sfMPTAmount] = amt - pay;
1879 if (uReceiverID == issuer)
1881 auto const outstanding = sleIssuance->getFieldU64(sfOutstandingAmount);
1882 auto const redeem = saAmount.
mpt().
value();
1883 if (outstanding >= redeem)
1885 sleIssuance->setFieldU64(sfOutstandingAmount, outstanding - redeem);
1886 view.
update(sleIssuance);
1893 auto const mptokenID = keylet::mptoken(mptID.key, uReceiverID);
1894 if (
auto sle = view.
peek(mptokenID))
1896 (*sle)[sfMPTAmount] += saAmount.
mpt().
value();
1917 uSenderID != uReceiverID,
1918 "ripple::rippleSendMPT : sender is not receiver");
1921 auto const issuer = saAmount.
getIssuer();
1928 if (uSenderID == issuer || uReceiverID == issuer)
1932 if (uSenderID == issuer)
1934 auto const sendAmount = saAmount.
mpt().
value();
1935 auto const maximumAmount =
1937 if (sendAmount > maximumAmount ||
1938 sle->getFieldU64(sfOutstandingAmount) >
1939 maximumAmount - sendAmount)
1948 saActual = saAmount;
1953 saActual = (waiveFee == WaiveTransferFee::Yes)
1959 JLOG(j.
debug()) <<
"rippleSendMPT> " << to_string(uSenderID) <<
" - > "
1960 << to_string(uReceiverID)
1964 if (
auto const terResult =
1983 "ripple::accountSendMPT : minimum amount and MPT");
1988 if (!saAmount || (uSenderID == uReceiverID))
1994 view, uSenderID, uReceiverID, saAmount, saActual, j, waiveFee);
2008 if constexpr (std::is_same_v<TIss, Issue>)
2010 view, uSenderID, uReceiverID, saAmount, j, waiveFee);
2013 view, uSenderID, uReceiverID, saAmount, j, waiveFee);
2032 auto sle = view.
peek(keylet::account(sender));
2037 if (before > beast::zero
2039 &&
after <= beast::zero
2043 &&
static_cast<bool>(
2047 !state->getFieldAmount(!bSenderHigh ? sfLowLimit : sfHighLimit)
2049 && !state->getFieldU32(!bSenderHigh ? sfLowQualityIn : sfHighQualityIn)
2052 !state->getFieldU32(!bSenderHigh ? sfLowQualityOut : sfHighQualityOut))
2081 "ripple::issueIOU : neither account nor issuer is XRP");
2084 XRPL_ASSERT(issue == amount.
issue(),
"ripple::issueIOU : matching issue");
2088 issue.
account != account,
"ripple::issueIOU : not issuer account");
2090 JLOG(j.
trace()) <<
"issueIOU: " << to_string(account) <<
": "
2093 bool bSenderHigh = issue.
account > account;
2095 auto const index = keylet::line(issue.
account, account, issue.
currency);
2097 if (
auto state = view.
peek(index))
2099 STAmount final_balance = state->getFieldAmount(sfBalance);
2104 STAmount const start_balance = final_balance;
2106 final_balance -= amount;
2125 state->setFieldAmount(sfBalance, final_balance);
2130 bSenderHigh ? account : issue.
account,
2131 bSenderHigh ? issue.
account : account,
2147 auto const receiverAccount = view.
peek(keylet::account(account));
2148 if (!receiverAccount)
2181 "ripple::redeemIOU : neither account nor issuer is XRP");
2184 XRPL_ASSERT(issue == amount.
issue(),
"ripple::redeemIOU : matching issue");
2188 issue.
account != account,
"ripple::redeemIOU : not issuer account");
2190 JLOG(j.
trace()) <<
"redeemIOU: " << to_string(account) <<
": "
2193 bool bSenderHigh = account > issue.
account;
2198 STAmount final_balance = state->getFieldAmount(sfBalance);
2203 STAmount const start_balance = final_balance;
2205 final_balance -= amount;
2208 view, state, bSenderHigh, account, start_balance, final_balance, j);
2218 state->setFieldAmount(sfBalance, final_balance);
2225 bSenderHigh ? issue.
account : account,
2226 bSenderHigh ? account : issue.
account,
2237 JLOG(j.
fatal()) <<
"redeemIOU: " << to_string(account)
2239 <<
" but no trust line exists!";
2253 from != beast::zero,
"ripple::transferXRP : nonzero from account");
2254 XRPL_ASSERT(to != beast::zero,
"ripple::transferXRP : nonzero to account");
2255 XRPL_ASSERT(from != to,
"ripple::transferXRP : sender is not receiver");
2256 XRPL_ASSERT(amount.
native(),
"ripple::transferXRP : amount is XRP");
2260 if (!sender || !receiver)
2263 JLOG(j.
trace()) <<
"transferXRP: " << to_string(from) <<
" -> "
2264 << to_string(to) <<
") : " << amount.
getFullText();
2266 if (sender->getFieldAmount(sfBalance) < amount)
2276 sender->setFieldAmount(
2277 sfBalance, sender->getFieldAmount(sfBalance) - amount);
2280 receiver->setFieldAmount(
2281 sfBalance, receiver->getFieldAmount(sfBalance) + amount);
2292 if (
auto const issuerAccount = view.
read(keylet::account(issue.
account));
2295 if (
auto const trustLine =
2297 return ((*trustLine)[sfFlags] &
2315 auto const mptID = keylet::mptIssuance(mptIssue.
getMptID());
2316 auto const sleIssuance = view.
read(mptID);
2320 auto const mptIssuer = sleIssuance->getAccountID(sfIssuer);
2323 if (mptIssuer == account)
2332 auto const sleIssuer = view.
read(keylet::account(mptIssuer));
2336 if (sleIssuer->isFieldPresent(sfVaultID))
2338 auto const sleVault =
2339 view.
read(keylet::vault(sleIssuer->getFieldH256(sfVaultID)));
2343 auto const asset = sleVault->at(sfAsset);
2346 if constexpr (std::is_same_v<TIss, Issue>)
2350 view, issue, account, authType, depth + 1);
2358 auto const mptokenID = keylet::mptoken(mptID.key, account);
2359 auto const sleToken = view.
read(mptokenID);
2362 if (!sleToken && authType == MPTAuthType::StrongAuth)
2367 auto const maybeDomainID = sleIssuance->at(~sfDomainID);
2372 "ripple::requireAuth : issuance requires authorization");
2374 if (
auto const ter =
2375 credentials::validDomain(view, *maybeDomainID, account);
2395 MPTID const& mptIssuanceID,
2400 auto const sleIssuance = view.
read(keylet::mptIssuance(mptIssuanceID));
2406 "ripple::enforceMPTokenAuthorization : authorization required");
2408 if (account == sleIssuance->at(sfIssuer))
2411 auto const keylet = keylet::mptoken(mptIssuanceID, account);
2412 auto const sleToken = view.
read(keylet);
2413 auto const maybeDomainID = sleIssuance->at(~sfDomainID);
2415 bool const authorizedByDomain = [&]() ->
bool {
2417 if (!maybeDomainID.has_value())
2428 if (!authorizedByDomain && sleToken ==
nullptr)
2439 else if (!authorizedByDomain && maybeDomainID.has_value())
2446 else if (!authorizedByDomain)
2451 sleToken !=
nullptr && !maybeDomainID.has_value(),
2452 "ripple::enforceMPTokenAuthorization : found MPToken");
2458 else if (authorizedByDomain && sleToken !=
nullptr)
2463 maybeDomainID.has_value(),
2464 "ripple::enforceMPTokenAuthorization : found MPToken for domain");
2467 else if (authorizedByDomain)
2472 maybeDomainID.has_value() && sleToken ==
nullptr,
2473 "ripple::enforceMPTokenAuthorization : new MPToken for domain");
2474 if (
auto const err = MPTokenAuthorize::authorize(
2478 .priorBalance = priorBalance,
2479 .mptIssuanceID = mptIssuanceID,
2491 "ripple::enforceMPTokenAuthorization : condition list is incomplete");
2502 auto const mptID = keylet::mptIssuance(mptIssue.
getMptID());
2503 auto const sleIssuance = view.
read(mptID);
2509 if (from != (*sleIssuance)[sfIssuer] && to != (*sleIssuance)[sfIssuer])
2518 Keylet const& ownerDirKeylet,
2525 unsigned int uDirEntry{0};
2526 uint256 dirEntry{beast::zero};
2529 if (view.
exists(ownerDirKeylet) &&
2530 dirFirst(view, ownerDirKeylet.
key, sleDirNode, uDirEntry, dirEntry))
2534 if (maxNodesToDelete && ++deleted > *maxNodesToDelete)
2538 auto sleItem = view.
peek(keylet::child(dirEntry));
2543 <<
"DeleteAccount: Directory node in ledger " << view.
seq()
2544 <<
" has index to object that is missing: "
2545 << to_string(dirEntry);
2550 sleItem->getFieldU16(sfLedgerEntryType))};
2554 auto const [ter, skipEntry] = deleter(nodeType, dirEntry, sleItem);
2576 "ripple::cleanupOnAccountDelete : minimum dir entries");
2580 <<
"DeleteAccount iterator re-validation failed.";
2583 if (skipEntry == SkipEntry::No)
2587 dirNext(view, ownerDirKeylet.
key, sleDirNode, uDirEntry, dirEntry));
2600 if (!sleState || sleState->getType() != ltRIPPLE_STATE)
2604 sleState->getFieldAmount(sfLowLimit).getIssuer(),
2605 sleState->getFieldAmount(sfHighLimit).getIssuer());
2606 auto sleLow = view.
peek(keylet::account(low));
2607 auto sleHigh = view.
peek(keylet::account(high));
2608 if (!sleLow || !sleHigh)
2610 bool const ammLow = sleLow->isFieldPresent(sfAMMID);
2611 bool const ammHigh = sleHigh->isFieldPresent(sfAMMID);
2614 if (ammLow && ammHigh)
2618 if (!ammLow && !ammHigh)
2622 if (ammAccountID && (low != *ammAccountID && high != *ammAccountID))
2625 if (
auto const ter =
trustDelete(view, sleState, low, high, j);
2629 <<
"deleteAMMTrustLine: failed to delete the trustline.";
2634 if (!(sleState->getFlags() & uFlags))
2653 if constexpr (std::is_same_v<TIss, Issue>)
2656 view, uSenderID, uReceiverID, saAmount, bCheckIssuer, j);
2662 "ripple::rippleCredit : not checking issuer");
2664 view, uSenderID, uReceiverID, saAmount, j);
2670[[nodiscard]] STAmount
2677 assets.
asset() == vault->at(sfAsset),
2678 "ripple::assetsToSharesDeposit : assets and vault match");
2679 Number assetTotal = vault->at(sfAssetsTotal);
2680 STAmount shares{vault->at(sfShareMPTID),
static_cast<Number>(assets)};
2681 if (assetTotal == 0)
2683 Number shareTotal = issuance->at(sfOutstandingAmount);
2684 shares = shareTotal * (assets / assetTotal);
2688[[nodiscard]] STAmount
2695 assets.
asset() == vault->at(sfAsset),
2696 "ripple::assetsToSharesWithdraw : assets and vault match");
2697 Number assetTotal = vault->at(sfAssetsTotal);
2698 assetTotal -= vault->at(sfLossUnrealized);
2699 STAmount shares{vault->at(sfShareMPTID)};
2700 if (assetTotal == 0)
2702 Number shareTotal = issuance->at(sfOutstandingAmount);
2703 shares = shareTotal * (assets / assetTotal);
2707[[nodiscard]] STAmount
2714 shares.
asset() == vault->at(sfShareMPTID),
2715 "ripple::sharesToAssetsWithdraw : shares and vault match");
2716 Number assetTotal = vault->at(sfAssetsTotal);
2717 assetTotal -= vault->at(sfLossUnrealized);
2718 STAmount assets{vault->at(sfAsset)};
2719 if (assetTotal == 0)
2721 Number shareTotal = issuance->at(sfOutstandingAmount);
2722 assets = assetTotal * (shares / shareTotal);
Provide a light-weight way to check active() before string formatting.
A generic endpoint for log messages.
static Sink & getNullSink()
Returns a Sink which does nothing.
Stream trace() const
Severity stream access functions.
Writeable view to a ledger, for applying a transaction.
virtual void creditHook(AccountID const &from, AccountID const &to, STAmount const &amount, STAmount const &preCreditBalance)
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
bool dirRemove(Keylet const &directory, std::uint64_t page, uint256 const &key, bool keepRoot)
Remove an entry from a directory.
virtual void adjustOwnerCountHook(AccountID const &account, std::uint32_t cur, std::uint32_t next)
virtual void insert(std::shared_ptr< SLE > const &sle)=0
Insert a new state SLE.
std::optional< std::uint64_t > dirInsert(Keylet const &directory, uint256 const &key, std::function< void(std::shared_ptr< SLE > const &)> const &describe)
Insert an entry to a directory.
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void erase(std::shared_ptr< SLE > const &sle)=0
Remove a peeked SLE.
constexpr value_type const & value() const
A currency issued by an account.
AccountID const & getIssuer() const
constexpr value_type value() const
Returns the underlying value.
constexpr MPTID const & getMptID() const
std::chrono::time_point< NetClock > time_point
std::chrono::duration< rep, period > duration
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
NetClock::time_point parentCloseTime() const
Returns the close time of the previous ledger.
virtual std::uint32_t ownerCountHook(AccountID const &account, std::uint32_t count) const
virtual STAmount balanceHook(AccountID const &account, AccountID const &issuer, STAmount const &amount) const
virtual bool open() const =0
Returns true if this reflects an open ledger.
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
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 LedgerInfo const & info() const =0
Returns information about the 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.
constexpr bool holds() const noexcept
Asset const & asset() const
constexpr TIss const & get() const
void setIssuer(AccountID const &uIssuer)
Currency const & getCurrency() const
AccountID const & getIssuer() const
Issue const & issue() const
std::string getFullText() const override
bool native() const noexcept
STAmount zeroed() const
Returns a zero value with the same issuer and currency.
std::shared_ptr< STLedgerEntry > const & ref
bool internalDirFirst(V &view, uint256 const &root, std::shared_ptr< N > &page, unsigned int &index, uint256 &entry)
bool internalDirNext(V &view, uint256 const &root, std::shared_ptr< N > &page, unsigned int &index, uint256 &entry)
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Keylet child(uint256 const &key) noexcept
Any item that can be in an owner dir.
Keylet amm(Asset const &issue1, Asset const &issue2) noexcept
AMM entry.
Keylet line(AccountID const &id0, AccountID const &id1, Currency const ¤cy) noexcept
The index of a trust line for a given currency.
Keylet const & amendments() noexcept
The index of the amendment table.
Keylet mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet page(uint256 const &root, std::uint64_t index=0) noexcept
A page in a directory.
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Keylet const & skip() noexcept
The index of the "short" skip list.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
AccountID const & noAccount()
A placeholder for empty accounts.
Expected< std::shared_ptr< SLE >, TER > createPseudoAccount(ApplyView &view, uint256 const &pseudoOwnerKey, SField const &ownerField)
Create pseudo-account, storing pseudoOwnerKey into ownerField.
static bool updateTrustLine(ApplyView &view, SLE::pointer state, bool bSenderHigh, AccountID const &sender, STAmount const &before, STAmount const &after, beast::Journal j)
std::uint8_t constexpr maxAssetCheckDepth
Maximum recursion depth for vault shares being put as an asset inside another vault; counted from 0.
STAmount accountFunds(ReadView const &view, AccountID const &id, STAmount const &saDefault, FreezeHandling freezeHandling, beast::Journal j)
FreezeHandling
Controls the treatment of frozen account balances.
bool cdirFirst(ReadView const &view, uint256 const &root, std::shared_ptr< SLE const > &page, unsigned int &index, uint256 &entry)
Returns the first entry in the directory, advancing the index.
bool isXRP(AccountID const &c)
AccountID const & xrpAccount()
Compute AccountID from public key.
STAmount sharesToAssetsWithdraw(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &shares)
bool isIndividualFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
TER deleteAMMTrustLine(ApplyView &view, std::shared_ptr< SLE > sleState, std::optional< AccountID > const &ammAccountID, beast::Journal j)
Delete trustline to AMM.
static TER rippleSendMPT(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, STAmount &saActual, beast::Journal j, WaiveTransferFee waiveFee)
bool dirFirst(ApplyView &view, uint256 const &root, std::shared_ptr< SLE > &page, unsigned int &index, uint256 &entry)
bool dirNext(ApplyView &view, uint256 const &root, std::shared_ptr< SLE > &page, unsigned int &index, uint256 &entry)
TER requireAuth(ReadView const &view, Issue const &issue, AccountID const &account)
Check if the account lacks required authorization.
bool isDeepFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
std::optional< uint256 > hashOfSeq(ReadView const &ledger, LedgerIndex seq, beast::Journal journal)
Return the hash of a ledger by sequence.
Rate transferRate(ReadView const &view, AccountID const &issuer)
Returns IOU issuer transfer fee as Rate.
std::uint64_t constexpr maxMPTokenAmount
The maximum amount of MPTokenIssuance.
TER redeemIOU(ApplyView &view, AccountID const &account, STAmount const &amount, Issue const &issue, beast::Journal j)
STAmount multiply(STAmount const &amount, Rate const &rate)
AuthHandling
Controls the treatment of unauthorized MPT balances.
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
TER transferXRP(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &amount, beast::Journal j)
TER enforceMPTokenAuthorization(ApplyView &view, MPTID const &mptIssuanceID, AccountID const &account, XRPAmount const &priorBalance, beast::Journal j)
Enforce account has MPToken to match its authorization.
static std::array< SField const *, 2 > const pseudoAccountOwnerFields
@ current
This was a new validation and was added.
TER offerDelete(ApplyView &view, std::shared_ptr< SLE > const &sle, beast::Journal j)
Delete an offer.
bool isFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
@ expired
List is expired, but has the largest non-pending sequence seen so far.
std::set< uint256 > getEnabledAmendments(ReadView const &view)
TER canTransfer(ReadView const &view, MPTIssue const &mptIssue, AccountID const &from, AccountID const &to)
Check if the destination account is allowed to receive MPT.
void adjustOwnerCount(ApplyView &view, std::shared_ptr< SLE > const &sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
static std::uint32_t confineOwnerCount(std::uint32_t current, std::int32_t adjustment, std::optional< AccountID > const &id=std::nullopt, beast::Journal j=beast::Journal{beast::Journal::getNullSink()})
static TER rippleCreditIOU(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, bool bCheckIssuer, beast::Journal j)
static bool adjustOwnerCount(ApplyContext &ctx, int count)
constexpr std::uint32_t const tfMPTUnauthorize
bool isVaultPseudoAccountFrozen(ReadView const &view, AccountID const &account, MPTIssue const &mptShare, int depth)
TER issueIOU(ApplyView &view, AccountID const &account, STAmount const &amount, Issue const &issue, beast::Journal j)
static TER accountSendIOU(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee)
std::map< uint256, NetClock::time_point > majorityAmendments_t
bool cdirNext(ReadView const &view, uint256 const &root, std::shared_ptr< SLE const > &page, unsigned int &index, uint256 &entry)
Returns the next entry in the directory, advancing the index.
TER trustCreate(ApplyView &view, bool const bSrcHigh, AccountID const &uSrcAccountID, AccountID const &uDstAccountID, uint256 const &uIndex, SLE::ref sleAccount, bool const bAuth, bool const bNoRipple, bool const bFreeze, bool bDeepFreeze, STAmount const &saBalance, STAmount const &saLimit, std::uint32_t uQualityIn, std::uint32_t uQualityOut, beast::Journal j)
Create a trust line.
TER trustDelete(ApplyView &view, std::shared_ptr< SLE > const &sleRippleState, AccountID const &uLowAccountID, AccountID const &uHighAccountID, beast::Journal j)
TER addEmptyHolding(ApplyView &view, AccountID const &accountID, XRPAmount priorBalance, Issue const &issue, beast::Journal journal)
AccountID pseudoAccountAddress(ReadView const &view, uint256 const &pseudoOwnerKey)
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
bool isLPTokenFrozen(ReadView const &view, AccountID const &account, Issue const &asset, Issue const &asset2)
bool isTesSuccess(TER x) noexcept
majorityAmendments_t getMajorityAmendments(ReadView const &view)
std::string to_string(base_uint< Bits, Tag > const &a)
LedgerEntryType
Identifiers for on-ledger objects.
bool forEachItemAfter(ReadView const &view, Keylet const &root, uint256 const &after, std::uint64_t const hint, unsigned int limit, std::function< bool(std::shared_ptr< SLE const > const &)> const &f)
Iterate all items after an item in the given directory.
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
STAmount assetsToSharesWithdraw(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &assets)
TER cleanupOnAccountDelete(ApplyView &view, Keylet const &ownerDirKeylet, EntryDeleter const &deleter, beast::Journal j, std::optional< uint16_t > maxNodesToDelete)
void forEachItem(ReadView const &view, Keylet const &root, std::function< void(std::shared_ptr< SLE const > const &)> const &f)
Iterate all items in the given directory.
Number root(Number f, unsigned d)
TER verifyValidDomain(ApplyView &view, AccountID const &account, uint256 domainID, beast::Journal j)
STAmount assetsToSharesDeposit(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &assets)
bool hasExpired(ReadView const &view, std::optional< std::uint32_t > const &exp)
Determines whether the given expiration time has passed.
static TER rippleSendIOU(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, STAmount &saActual, beast::Journal j, WaiveTransferFee waiveFee)
TER rippleCredit(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, bool bCheckIssuer, beast::Journal j)
Calls static rippleCreditIOU if saAmount represents Issue.
bool isAnyFrozen(ReadView const &view, std::initializer_list< AccountID > const &accounts, MPTIssue const &mptIssue, int depth)
TERSubset< CanCvtToTER > TER
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
TER removeEmptyHolding(ApplyView &view, AccountID const &accountID, Issue const &issue, beast::Journal journal)
bool areCompatible(ReadView const &validLedger, ReadView const &testLedger, beast::Journal::Stream &s, char const *reason)
Return false if the test ledger is provably incompatible with the valid ledger, that is,...
static TER rippleCreditMPT(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, beast::Journal j)
bool dirIsEmpty(ReadView const &view, Keylet const &k)
Returns true if the directory is empty.
bool isPseudoAccount(std::shared_ptr< SLE const > sleAcct)
TER accountSend(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee)
Calls static accountSendIOU if saAmount represents Issue.
static TER accountSendMPT(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee)
TER dirLink(ApplyView &view, AccountID const &owner, std::shared_ptr< SLE > &object)
Rate const parityRate
A transfer rate signifying a 1:1 exchange.
XRPAmount xrpLiquid(ReadView const &view, AccountID const &id, std::int32_t ownerCountAdj, beast::Journal j)
bool isGlobalFrozen(ReadView const &view, AccountID const &issuer)
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
A pair of SHAMap key and LedgerEntryType.
Represents a transfer rate.
Returns the RIPEMD-160 digest of the SHA256 hash of the message.
T time_since_epoch(T... args)