1#include <xrpl/basics/Expected.h>
2#include <xrpl/basics/Log.h>
3#include <xrpl/basics/chrono.h>
4#include <xrpl/beast/utility/instrumentation.h>
5#include <xrpl/ledger/CredentialHelpers.h>
6#include <xrpl/ledger/Credit.h>
7#include <xrpl/ledger/ReadView.h>
8#include <xrpl/ledger/View.h>
9#include <xrpl/protocol/Feature.h>
10#include <xrpl/protocol/Indexes.h>
11#include <xrpl/protocol/LedgerFormats.h>
12#include <xrpl/protocol/MPTIssue.h>
13#include <xrpl/protocol/Protocol.h>
14#include <xrpl/protocol/Quality.h>
15#include <xrpl/protocol/TER.h>
16#include <xrpl/protocol/TxFlags.h>
17#include <xrpl/protocol/digest.h>
18#include <xrpl/protocol/st.h>
41 auto const& svIndexes = page->getFieldV256(sfIndexes);
43 index <= svIndexes.size(),
44 "xrpl::detail::internalDirNext : index inside range");
46 if (index >= svIndexes.size())
48 auto const next = page->getFieldU64(sfIndexNext);
61 XRPL_ASSERT(page,
"xrpl::detail::internalDirNext : non-null root");
71 entry = svIndexes[index++];
203 if (issuer != account)
240 if (issuer != account)
273 for (
auto const& account : accounts)
279 for (
auto const& account : accounts)
301 auto const mptIssuance =
303 if (mptIssuance ==
nullptr)
306 auto const issuer = mptIssuance->getAccountID(sfIssuer);
308 if (mptIssuer ==
nullptr)
311 UNREACHABLE(
"xrpl::isVaultPseudoAccountFrozen : null MPToken issuer");
316 if (!mptIssuer->isFieldPresent(sfVaultID))
321 if (vault ==
nullptr)
323 UNREACHABLE(
"xrpl::isVaultPseudoAccountFrozen : null vault");
328 return isAnyFrozen(view, {issuer, account}, vault->at(sfAsset), depth + 1);
343 if (issuer == account)
386 if (
isFrozen(view, account, currency, issuer) ||
401 else if (sleIssuer->isFieldPresent(sfAMMID))
410 (*sleAmm)[sfAsset].get<Issue>(),
411 (*sleAmm)[sfAsset2].get<Issue>()))
429 bool includeOppositeLimit,
435 amount = sle->getFieldAmount(sfBalance);
436 bool const accountHigh = account > issuer;
437 auto const& oppositeField = accountHigh ? sfLowLimit : sfHighLimit;
443 if (includeOppositeLimit)
445 amount += sle->getFieldAmount(oppositeField);
447 amount.setIssuer(issuer);
451 amount.clear(
Issue{currency, issuer});
454 JLOG(j.
trace()) <<
"getTrustLineBalance:" <<
" account="
455 <<
to_string(account) <<
" amount=" << amount.getFullText();
476 bool const returnSpendable = (includeFullBalance ==
shFULL_BALANCE);
477 if (returnSpendable && account == issuer)
488 view, sle, account, currency, issuer, returnSpendable, j);
520 bool const returnSpendable = (includeFullBalance ==
shFULL_BALANCE);
522 if (returnSpendable && account == mptIssue.
getIssuer())
526 auto const issuance =
536 issuance->at(sfOutstandingAmount)};
545 amount.clear(mptIssue);
548 amount.clear(mptIssue);
551 amount =
STAmount{mptIssue, sleMpt->getFieldU64(sfMPTAmount)};
561 amount.clear(mptIssue);
565 auto const sleIssuance =
572 amount.clear(mptIssue);
579[[nodiscard]] STAmount
594 view, account, value, zeroIfFrozen, j, includeFullBalance);
653 <<
"Account " << *
id <<
" owner count exceeds max!";
666 <<
"Account " << *
id <<
" owner count set below 0!";
669 XRPL_ASSERT(!
id,
"xrpl::confineOwnerCount : id is not set");
688 view.
ownerCountHook(
id, sle->getFieldU32(sfOwnerCount)), ownerCountAdj);
695 auto const fullBalance = sle->getFieldAmount(sfBalance);
700 (balance < reserve) ?
STAmount{0} : balance - reserve;
702 JLOG(j.
trace()) <<
"accountHolds:" <<
" account=" <<
to_string(
id)
703 <<
" amount=" << amount.getFullText()
704 <<
" fullBalance=" << fullBalance.getFullText()
705 <<
" balance=" << balance.getFullText()
706 <<
" reserve=" << reserve <<
" ownerCount=" << ownerCount
707 <<
" ownerCountAdj=" << ownerCountAdj;
718 XRPL_ASSERT(
root.type == ltDIR_NODE,
"xrpl::forEachItem : valid root type");
720 if (
root.type != ltDIR_NODE)
727 auto sle = view.
read(pos);
730 for (
auto const& key : sle->getFieldV256(sfIndexes))
732 auto const next = sle->getFieldU64(sfIndexNext);
749 root.type == ltDIR_NODE,
"xrpl::forEachItemAfter : valid root type");
751 if (
root.type != ltDIR_NODE)
754 auto currentIndex =
root;
757 if (
after.isNonZero())
761 if (
auto hintDir = view.
read(hintIndex))
763 for (
auto const& key : hintDir->getFieldV256(sfIndexes))
768 currentIndex = hintIndex;
777 auto const ownerDir = view.
read(currentIndex);
780 for (
auto const& key : ownerDir->getFieldV256(sfIndexes))
793 auto const uNodeNext = ownerDir->getFieldU64(sfIndexNext);
803 auto const ownerDir = view.
read(currentIndex);
806 for (
auto const& key : ownerDir->getFieldV256(sfIndexes))
809 auto const uNodeNext = ownerDir->getFieldU64(sfIndexNext);
822 if (sle && sle->isFieldPresent(sfTransferRate))
823 return Rate{sle->getFieldU32(sfTransferRate)};
835 sle && sle->isFieldPresent(sfTransferFee))
836 return Rate{1'000'000'000u + 10'000 * sle->getFieldU16(sfTransferFee)};
851 amount.asset().value());
870 if (hash && (*hash != validLedger.
header().
hash))
872 JLOG(s) << reason <<
" incompatible with valid ledger";
874 JLOG(s) <<
"Hash(VSeq): " <<
to_string(*hash);
886 if (hash && (*hash != testLedger.
header().
hash))
888 JLOG(s) << reason <<
" incompatible preceding ledger";
890 JLOG(s) <<
"Hash(NSeq): " <<
to_string(*hash);
900 JLOG(s) << reason <<
" incompatible ledger";
907 JLOG(s) <<
"Val: " << validLedger.
header().
seq <<
" "
910 JLOG(s) <<
"New: " << testLedger.
header().
seq <<
" "
927 if (testLedger.
header().
seq > validIndex)
934 if (hash && (*hash != validHash))
936 JLOG(s) << reason <<
" incompatible following ledger";
937 JLOG(s) <<
"Hash(VSeq): " <<
to_string(*hash);
943 (validIndex == testLedger.
header().
seq) &&
946 JLOG(s) << reason <<
" incompatible ledger";
953 JLOG(s) <<
"Val: " << validIndex <<
" " <<
to_string(validHash);
955 JLOG(s) <<
"New: " << testLedger.
header().
seq <<
" "
965 auto const sleNode = view.
read(k);
968 if (!sleNode->getFieldV256(sfIndexes).empty())
973 return sleNode->getFieldU64(sfIndexNext) == 0;
983 if (sle->isFieldPresent(sfAmendments))
985 auto const& v = sle->getFieldV256(sfAmendments);
986 amendments.insert(v.begin(), v.end());
1000 if (sle->isFieldPresent(sfMajorities))
1003 using d = tp::duration;
1005 auto const majorities = sle->getFieldArray(sfMajorities);
1007 for (
auto const& m : majorities)
1008 ret[m.getFieldH256(sfAmendment)] =
1009 tp(d(m.getFieldU32(sfCloseTime)));
1020 if (seq > ledger.
seq())
1022 JLOG(journal.
warn())
1023 <<
"Can't get seq " << seq <<
" from " << ledger.
seq() <<
" future";
1026 if (seq == ledger.
seq())
1028 if (seq == (ledger.
seq() - 1))
1031 if (
int diff = ledger.
seq() - seq; diff <= 256)
1038 hashIndex->getFieldU32(sfLastLedgerSequence) ==
1040 "xrpl::hashOfSeq : matching ledger sequence");
1041 STVector256 vec = hashIndex->getFieldV256(sfHashes);
1042 if (vec.
size() >= diff)
1043 return vec[vec.
size() - diff];
1044 JLOG(journal.
warn())
1045 <<
"Ledger " << ledger.
seq() <<
" missing hash for " << seq
1046 <<
" (" << vec.
size() <<
"," << diff <<
")";
1050 JLOG(journal.
warn())
1052 <<
" missing normal list";
1056 if ((seq & 0xff) != 0)
1058 JLOG(journal.
debug())
1059 <<
"Can't get seq " << seq <<
" from " << ledger.
seq() <<
" past";
1067 auto const lastSeq = hashIndex->getFieldU32(sfLastLedgerSequence);
1068 XRPL_ASSERT(lastSeq >= seq,
"xrpl::hashOfSeq : minimum last ledger");
1070 (lastSeq & 0xff) == 0,
"xrpl::hashOfSeq : valid last ledger");
1071 auto const diff = (lastSeq - seq) >> 8;
1072 STVector256 vec = hashIndex->getFieldV256(sfHashes);
1073 if (vec.
size() > diff)
1074 return vec[vec.
size() - diff - 1];
1076 JLOG(journal.
warn()) <<
"Can't get seq " << seq <<
" from " << ledger.
seq()
1096 XRPL_ASSERT(amount,
"xrpl::adjustOwnerCount : nonzero amount input");
1101 sle->at(sfOwnerCount) = adjusted;
1109 (*sle)[sfOwner] = account;
1124 object->setFieldU64(node, *page);
1138 rsh(hash.data(), hash.size());
1161 "xrpl::getPseudoAccountFields : unable to find account root "
1165 auto const& soTemplate = ar->getSOTemplate();
1168 for (
auto const& field : soTemplate)
1173 return pseudoFields;
1175 return pseudoFields;
1187 return sleAcct && sleAcct->getType() == ltACCOUNT_ROOT &&
1191 [&sleAcct, &pseudoFieldFilter](
SField const* sf) ->
bool {
1192 return sleAcct->isFieldPresent(*sf) &&
1193 (pseudoFieldFilter.empty() ||
1194 pseudoFieldFilter.contains(sf));
1198Expected<std::shared_ptr<SLE>,
TER>
1201 uint256 const& pseudoOwnerKey,
1202 SField const& ownerField)
1210 [&ownerField](
SField const* sf) ->
bool {
1211 return *sf == ownerField;
1213 "xrpl::createPseudoAccount : valid owner field");
1216 if (accountId == beast::zero)
1221 account->setAccountID(sfAccount, accountId);
1222 account->setFieldAmount(sfBalance,
STAmount{});
1232 account->setFieldU32(sfSequence, seqno);
1236 account->setFieldU32(
1239 account->setFieldH256(ownerField, pseudoOwnerKey);
1287 if (toSle ==
nullptr)
1321 auto const& issuer = amount.getIssuer();
1322 if (from == to || to == issuer ||
isXRP(issuer))
1329 auto const& currency = issue.currency;
1330 auto const owed =
creditBalance(view, to, issuer, currency);
1331 if (owed <= beast::zero)
1333 auto const limit =
creditLimit(view, to, issuer, currency);
1334 if (-owed >= limit || amount > (limit + owed))
1340 amount.asset().value());
1350 bool hasDestinationTag)
1373 bool hasDestinationTag)
1377 return canWithdraw(view, from, to, toSle, amount, hasDestinationTag);
1383 auto const from = tx[sfAccount];
1384 auto const to = tx[~sfDestination].value_or(from);
1387 view, from, to, tx[sfAmount], tx.
isFieldPresent(sfDestinationTag));
1402 if (dstAcct == senderAcct)
1405 view, senderAcct, priorBalance, amount.asset(), j);
1427 JLOG(j.
error()) <<
"LoanBrokerCoverWithdraw: negative balance of "
1428 "broker cover assets.";
1451 auto const& issuerId = issue.
getIssuer();
1452 auto const& currency = issue.
currency;
1456 auto const& srcId = issuerId;
1457 auto const& dstId = accountID;
1458 auto const high = srcId > dstId;
1459 auto const index =
keylet::line(srcId, dstId, currency);
1462 if (!sleDst || !sleSrc)
1467 if (view.
read(index))
1486 STAmount{Issue{currency, noAccount()}},
1487 STAmount{Issue{currency, dstId}},
1501 auto const& mptID = mptIssue.
getMptID();
1502 auto const mpt = view.
peek(keylet::mptIssuance(mptID));
1507 if (view.
peek(keylet::mptoken(mptID, accountID)))
1519 MPTID const& mptIssuanceID,
1525 auto const sleAcct = view.
peek(keylet::account(account));
1539 auto const mptokenKey = keylet::mptoken(mptIssuanceID, account);
1540 auto const sleMpt = view.
peek(mptokenKey);
1541 if (!sleMpt || (*sleMpt)[sfMPTAmount] != 0)
1545 keylet::ownerDir(account),
1546 (*sleMpt)[sfOwnerNode],
1566 std::uint32_t const uOwnerCount = sleAcct->getFieldU32(sfOwnerCount);
1568 (uOwnerCount < 2) ?
XRPAmount(beast::zero)
1571 if (priorBalance < reserveCreate)
1575 auto const mpt = view.
read(keylet::mptIssuance(mptIssuanceID));
1576 if (!mpt || mpt->getAccountID(sfIssuer) == account)
1580 "xrpl::authorizeMPToken : invalid issuance or issuers token");
1586 auto const mptokenKey = keylet::mptoken(mptIssuanceID, account);
1588 if (
auto ter =
dirLink(view, account, mptoken))
1591 (*mptoken)[sfAccount] = account;
1592 (*mptoken)[sfMPTokenIssuanceID] = mptIssuanceID;
1593 (*mptoken)[sfFlags] = 0;
1602 auto const sleMptIssuance = view.
read(keylet::mptIssuance(mptIssuanceID));
1603 if (!sleMptIssuance)
1609 if (account != (*sleMptIssuance)[sfIssuer])
1612 auto const sleMpt = view.
peek(keylet::mptoken(mptIssuanceID, *holderID));
1622 flagsOut &= ~lsfMPTAuthorized;
1628 if (flagsIn != flagsOut)
1629 sleMpt->setFieldU32(sfFlags, flagsOut);
1638 bool const bSrcHigh,
1644 bool const bNoRipple,
1655 JLOG(j.
trace()) <<
"trustCreate: " << to_string(uSrcAccountID) <<
", "
1656 << to_string(uDstAccountID) <<
", "
1659 auto const& uLowAccountID = !bSrcHigh ? uSrcAccountID : uDstAccountID;
1660 auto const& uHighAccountID = bSrcHigh ? uSrcAccountID : uDstAccountID;
1661 if (uLowAccountID == uHighAccountID)
1664 UNREACHABLE(
"xrpl::trustCreate : trust line to self");
1671 view.
insert(sleRippleState);
1674 keylet::ownerDir(uLowAccountID),
1675 sleRippleState->key(),
1682 keylet::ownerDir(uHighAccountID),
1683 sleRippleState->key(),
1689 bool const bSetDst = saLimit.
getIssuer() == uDstAccountID;
1690 bool const bSetHigh = bSrcHigh ^ bSetDst;
1692 XRPL_ASSERT(sleAccount,
"xrpl::trustCreate : non-null SLE");
1697 sleAccount->getAccountID(sfAccount) ==
1698 (bSetHigh ? uHighAccountID : uLowAccountID),
1699 "xrpl::trustCreate : matching account ID");
1700 auto const slePeer =
1701 view.
peek(keylet::account(bSetHigh ? uLowAccountID : uHighAccountID));
1706 sleRippleState->setFieldU64(sfLowNode, *lowNode);
1707 sleRippleState->setFieldU64(sfHighNode, *highNode);
1709 sleRippleState->setFieldAmount(
1710 bSetHigh ? sfHighLimit : sfLowLimit, saLimit);
1711 sleRippleState->setFieldAmount(
1712 bSetHigh ? sfLowLimit : sfHighLimit,
1714 saBalance.
getCurrency(), bSetDst ? uSrcAccountID : uDstAccountID}));
1717 sleRippleState->setFieldU32(
1718 bSetHigh ? sfHighQualityIn : sfLowQualityIn, uQualityIn);
1721 sleRippleState->setFieldU32(
1722 bSetHigh ? sfHighQualityOut : sfLowQualityOut, uQualityOut);
1749 sleRippleState->setFieldU32(sfFlags, uFlags);
1753 sleRippleState->setFieldAmount(
1754 sfBalance, bSetHigh ? -saBalance : saBalance);
1757 uSrcAccountID, uDstAccountID, saBalance, saBalance.
zeroed());
1771 auto const sle = view.
read(keylet::account(accountID));
1775 auto const balance = sle->getFieldAmount(sfBalance);
1776 if (balance.xrp() != 0)
1785 bool const accountIsIssuer = accountID == issue.
account;
1786 auto const line = view.
peek(keylet::line(accountID, issue));
1789 if (!accountIsIssuer && line->at(sfBalance)->iou() != beast::zero)
1796 auto sleLowAccount =
1797 view.
peek(keylet::account(line->at(sfLowLimit)->getIssuer()));
1811 auto sleHighAccount =
1812 view.
peek(keylet::account(line->at(sfHighLimit)->getIssuer()));
1813 if (!sleHighAccount)
1826 line->at(sfLowLimit)->getIssuer(),
1827 line->at(sfHighLimit)->getIssuer(),
1841 bool const accountIsIssuer = accountID == mptIssue.
getIssuer();
1842 auto const& mptID = mptIssue.
getMptID();
1843 auto const mptoken = view.
peek(keylet::mptoken(mptID, accountID));
1850 if (mptoken->at(sfMPTAmount) != 0)
1872 std::uint64_t uLowNode = sleRippleState->getFieldU64(sfLowNode);
1873 std::uint64_t uHighNode = sleRippleState->getFieldU64(sfHighNode);
1875 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: low";
1878 keylet::ownerDir(uLowAccountID),
1880 sleRippleState->key(),
1886 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: high";
1889 keylet::ownerDir(uHighAccountID),
1891 sleRippleState->key(),
1897 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: state";
1898 view.
erase(sleRippleState);
1908 auto offerIndex = sle->key();
1909 auto owner = sle->getAccountID(sfAccount);
1912 uint256 uDirectory = sle->getFieldH256(sfBookDirectory);
1915 keylet::ownerDir(owner),
1916 sle->getFieldU64(sfOwnerNode),
1924 keylet::page(uDirectory),
1925 sle->getFieldU64(sfBookNode),
1932 if (sle->isFieldPresent(sfAdditionalBooks))
1935 sle->isFlag(
lsfHybrid) && sle->isFieldPresent(sfDomainID),
1936 "xrpl::offerDelete : should be a hybrid domain offer");
1938 auto const& additionalBookDirs = sle->getFieldArray(sfAdditionalBooks);
1940 for (
auto const& bookDir : additionalBookDirs)
1942 auto const& dirIndex = bookDir.getFieldH256(sfBookDirectory);
1943 auto const& dirNode = bookDir.getFieldU64(sfBookNode);
1946 keylet::page(dirIndex), dirNode, offerIndex,
false))
1978 !bCheckIssuer || uSenderID == issuer || uReceiverID == issuer,
1979 "xrpl::rippleCreditIOU : matching issuer or don't care");
1984 uSenderID != uReceiverID,
1985 "xrpl::rippleCreditIOU : sender is not receiver");
1987 bool const bSenderHigh = uSenderID > uReceiverID;
1988 auto const index = keylet::line(uSenderID, uReceiverID, currency);
1992 "xrpl::rippleCreditIOU : sender is not XRP");
1995 "xrpl::rippleCreditIOU : receiver is not XRP");
1998 if (
auto const sleRippleState = view.
peek(index))
2000 STAmount saBalance = sleRippleState->getFieldAmount(sfBalance);
2005 view.
creditHook(uSenderID, uReceiverID, saAmount, saBalance);
2007 STAmount const saBefore = saBalance;
2009 saBalance -= saAmount;
2011 JLOG(j.
trace()) <<
"rippleCreditIOU: " << to_string(uSenderID) <<
" -> "
2012 << to_string(uReceiverID)
2017 std::uint32_t const uFlags(sleRippleState->getFieldU32(sfFlags));
2018 bool bDelete =
false;
2022 if (saBefore > beast::zero
2024 && saBalance <= beast::zero
2032 view.
read(keylet::account(uSenderID))->getFlags() &
2035 !sleRippleState->getFieldAmount(
2036 !bSenderHigh ? sfLowLimit : sfHighLimit)
2038 && !sleRippleState->getFieldU32(
2039 !bSenderHigh ? sfLowQualityIn : sfHighQualityIn)
2041 && !sleRippleState->getFieldU32(
2042 !bSenderHigh ? sfLowQualityOut : sfHighQualityOut))
2047 view, view.
peek(keylet::account(uSenderID)), -1, j);
2050 sleRippleState->setFieldU32(
2052 uFlags & (!bSenderHigh ? ~
lsfLowReserve : ~lsfHighReserve));
2055 bDelete = !saBalance
2064 sleRippleState->setFieldAmount(sfBalance, saBalance);
2072 bSenderHigh ? uReceiverID : uSenderID,
2073 !bSenderHigh ? uReceiverID : uSenderID,
2077 view.
update(sleRippleState);
2081 STAmount const saReceiverLimit(
Issue{currency, uReceiverID});
2086 JLOG(j.
debug()) <<
"rippleCreditIOU: "
2088 << to_string(uSenderID) <<
" -> " << to_string(uReceiverID)
2091 auto const sleAccount = view.
peek(keylet::account(uReceiverID));
2128 auto const& issuer = saAmount.
getIssuer();
2132 "xrpl::rippleSendIOU : neither sender nor receiver is XRP");
2134 uSenderID != uReceiverID,
2135 "xrpl::rippleSendIOU : sender is not receiver");
2137 if (uSenderID == issuer || uReceiverID == issuer || issuer ==
noAccount())
2144 saActual = saAmount;
2152 saActual = (waiveFee == WaiveTransferFee::Yes)
2156 JLOG(j.
debug()) <<
"rippleSendIOU> " << to_string(uSenderID) <<
" - > "
2157 << to_string(uReceiverID)
2165 terResult =
rippleCreditIOU(view, uSenderID, issuer, saActual,
true, j);
2186 !
isXRP(senderID),
"xrpl::rippleSendMultiIOU : sender is not XRP");
2190 actual = takeFromSender;
2193 for (
auto const& r : receivers)
2195 auto const& receiverID = r.first;
2201 if (!amount || (senderID == receiverID))
2206 "xrpl::rippleSendMultiIOU : receiver is not XRP");
2208 if (senderID == issuer || receiverID == issuer || issuer ==
noAccount())
2212 view, senderID, receiverID, amount,
false, j))
2225 STAmount actualSend = (waiveFee == WaiveTransferFee::Yes)
2228 actual += actualSend;
2229 takeFromSender += actualSend;
2231 JLOG(j.
debug()) <<
"rippleSendMultiIOU> " << to_string(senderID)
2232 <<
" - > " << to_string(receiverID)
2233 <<
" : deliver=" << amount.getFullText()
2236 if (
TER const terResult =
2241 if (senderID != issuer && takeFromSender)
2244 view, senderID, issuer, takeFromSender,
true, j))
2272 "xrpl::accountSendIOU : minimum amount and not MPT");
2279 if (!saAmount || (uSenderID == uReceiverID))
2286 JLOG(j.
trace()) <<
"accountSendIOU: " << to_string(uSenderID) <<
" -> "
2287 << to_string(uReceiverID) <<
" : "
2291 view, uSenderID, uReceiverID, saAmount, saActual, j, waiveFee);
2302 ? view.
peek(keylet::account(uSenderID))
2305 ? view.
peek(keylet::account(uReceiverID))
2308 if (
auto stream = j.
trace())
2314 sender_bal = sender->getFieldAmount(sfBalance).getFullText();
2317 receiver_bal = receiver->getFieldAmount(sfBalance).getFullText();
2319 stream <<
"accountSendIOU> " << to_string(uSenderID) <<
" ("
2320 << sender_bal <<
") -> " << to_string(uReceiverID) <<
" ("
2321 << receiver_bal <<
") : " << saAmount.
getFullText();
2326 if (sender->getFieldAmount(sfBalance) < saAmount)
2337 auto const sndBal = sender->getFieldAmount(sfBalance);
2341 sender->setFieldAmount(sfBalance, sndBal - saAmount);
2349 auto const rcvBal = receiver->getFieldAmount(sfBalance);
2350 receiver->setFieldAmount(sfBalance, rcvBal + saAmount);
2356 if (
auto stream = j.
trace())
2362 sender_bal = sender->getFieldAmount(sfBalance).getFullText();
2365 receiver_bal = receiver->getFieldAmount(sfBalance).getFullText();
2367 stream <<
"accountSendIOU< " << to_string(uSenderID) <<
" ("
2368 << sender_bal <<
") -> " << to_string(uReceiverID) <<
" ("
2369 << receiver_bal <<
") : " << saAmount.
getFullText();
2385 receivers.
size() > 1,
2386 "xrpl::accountSendMultiIOU",
2387 "multiple recipients provided");
2392 JLOG(j.
trace()) <<
"accountSendMultiIOU: " << to_string(senderID)
2393 <<
" sending " << receivers.
size() <<
" IOUs";
2396 view, senderID, issue, receivers, actual, j, waiveFee);
2406 ? view.
peek(keylet::account(senderID))
2409 if (
auto stream = j.
trace())
2414 sender_bal = sender->getFieldAmount(sfBalance).getFullText();
2416 stream <<
"accountSendMultiIOU> " << to_string(senderID) <<
" ("
2417 << sender_bal <<
") -> " << receivers.
size() <<
" receivers.";
2422 for (
auto const& r : receivers)
2424 auto const& receiverID = r.first;
2427 if (amount < beast::zero)
2435 if (!amount || (senderID == receiverID))
2439 ? view.
peek(keylet::account(receiverID))
2442 if (
auto stream = j.
trace())
2448 receiver->getFieldAmount(sfBalance).getFullText();
2450 stream <<
"accountSendMultiIOU> " << to_string(senderID) <<
" -> "
2451 << to_string(receiverID) <<
" (" << receiver_bal
2452 <<
") : " << amount.getFullText();
2458 auto const rcvBal = receiver->getFieldAmount(sfBalance);
2459 receiver->setFieldAmount(sfBalance, rcvBal + amount);
2465 takeFromSender += amount;
2468 if (
auto stream = j.
trace())
2474 receiver->getFieldAmount(sfBalance).getFullText();
2476 stream <<
"accountSendMultiIOU< " << to_string(senderID) <<
" -> "
2477 << to_string(receiverID) <<
" (" << receiver_bal
2478 <<
") : " << amount.getFullText();
2484 if (sender->getFieldAmount(sfBalance) < takeFromSender)
2490 auto const sndBal = sender->getFieldAmount(sfBalance);
2494 sender->setFieldAmount(sfBalance, sndBal - takeFromSender);
2499 if (
auto stream = j.
trace())
2505 sender_bal = sender->getFieldAmount(sfBalance).getFullText();
2507 stream <<
"accountSendMultiIOU< " << to_string(senderID) <<
" ("
2508 << sender_bal <<
") -> " << receivers.
size() <<
" receivers.";
2523 auto const& issuer = saAmount.
getIssuer();
2524 auto sleIssuance = view.
peek(mptID);
2527 if (uSenderID == issuer)
2529 (*sleIssuance)[sfOutstandingAmount] += saAmount.
mpt().
value();
2530 view.
update(sleIssuance);
2534 auto const mptokenID = keylet::mptoken(mptID.key, uSenderID);
2535 if (
auto sle = view.
peek(mptokenID))
2537 auto const amt = sle->getFieldU64(sfMPTAmount);
2538 auto const pay = saAmount.
mpt().
value();
2541 (*sle)[sfMPTAmount] = amt - pay;
2548 if (uReceiverID == issuer)
2550 auto const outstanding = sleIssuance->getFieldU64(sfOutstandingAmount);
2551 auto const redeem = saAmount.
mpt().
value();
2552 if (outstanding >= redeem)
2554 sleIssuance->setFieldU64(sfOutstandingAmount, outstanding - redeem);
2555 view.
update(sleIssuance);
2562 auto const mptokenID = keylet::mptoken(mptID.key, uReceiverID);
2563 if (
auto sle = view.
peek(mptokenID))
2565 (*sle)[sfMPTAmount] += saAmount.
mpt().
value();
2586 uSenderID != uReceiverID,
2587 "xrpl::rippleSendMPT : sender is not receiver");
2590 auto const& issuer = saAmount.
getIssuer();
2597 if (uSenderID == issuer || uReceiverID == issuer)
2601 if (uSenderID == issuer)
2603 auto const sendAmount = saAmount.
mpt().
value();
2604 auto const maximumAmount =
2606 if (sendAmount > maximumAmount ||
2607 sle->getFieldU64(sfOutstandingAmount) >
2608 maximumAmount - sendAmount)
2617 saActual = saAmount;
2622 saActual = (waiveFee == WaiveTransferFee::Yes)
2628 JLOG(j.
debug()) <<
"rippleSendMPT> " << to_string(uSenderID) <<
" - > "
2629 << to_string(uReceiverID)
2633 if (
auto const terResult =
2653 auto const& issuer = mptIssue.
getIssuer();
2655 auto const sle = view.
read(keylet::mptIssuance(mptIssue.
getMptID()));
2661 actual = takeFromSender;
2663 for (
auto const& r : receivers)
2665 auto const& receiverID = r.first;
2666 STAmount amount{mptIssue, r.second};
2668 if (amount < beast::zero)
2676 if (!amount || (senderID == receiverID))
2679 if (senderID == issuer || receiverID == issuer)
2683 if (senderID == issuer)
2686 takeFromSender == beast::zero,
2687 "rippler::rippleSendMultiMPT",
2688 "sender == issuer, takeFromSender == zero");
2689 auto const sendAmount = amount.mpt().value();
2690 auto const maximumAmount =
2692 if (sendAmount > maximumAmount ||
2693 sle->getFieldU64(sfOutstandingAmount) >
2694 maximumAmount - sendAmount)
2699 if (
auto const ter =
2710 STAmount actualSend = (waiveFee == WaiveTransferFee::Yes)
2715 actual += actualSend;
2716 takeFromSender += actualSend;
2718 JLOG(j.
debug()) <<
"rippleSendMultiMPT> " << to_string(senderID)
2719 <<
" - > " << to_string(receiverID)
2720 <<
" : deliver=" << amount.getFullText()
2723 if (
auto const terResult =
2727 if (senderID != issuer && takeFromSender)
2729 if (
TER const terResult =
2748 "xrpl::accountSendMPT : minimum amount and MPT");
2753 if (!saAmount || (uSenderID == uReceiverID))
2759 view, uSenderID, uReceiverID, saAmount, saActual, j, waiveFee);
2774 view, senderID, mptIssue, receivers, actual, j, waiveFee);
2790 view, uSenderID, uReceiverID, saAmount, j, waiveFee);
2793 view, uSenderID, uReceiverID, saAmount, j, waiveFee);
2808 receivers.
size() > 1,
2809 "xrpl::accountSendMulti",
2810 "multiple recipients provided");
2815 view, senderID, issue, receivers, j, waiveFee);
2818 view, senderID, issue, receivers, j, waiveFee);
2837 auto sle = view.
peek(keylet::account(sender));
2842 if (before > beast::zero
2844 &&
after <= beast::zero
2848 &&
static_cast<bool>(
2852 !state->getFieldAmount(!bSenderHigh ? sfLowLimit : sfHighLimit)
2854 && !state->getFieldU32(!bSenderHigh ? sfLowQualityIn : sfHighQualityIn)
2857 !state->getFieldU32(!bSenderHigh ? sfLowQualityOut : sfHighQualityOut))
2866 sfFlags, flags & (!bSenderHigh ? ~
lsfLowReserve : ~lsfHighReserve));
2886 "xrpl::issueIOU : neither account nor issuer is XRP");
2889 XRPL_ASSERT(issue == amount.issue(),
"xrpl::issueIOU : matching issue");
2893 issue.
account != account,
"xrpl::issueIOU : not issuer account");
2895 JLOG(j.
trace()) <<
"issueIOU: " << to_string(account) <<
": "
2896 << amount.getFullText();
2898 bool bSenderHigh = issue.
account > account;
2900 auto const index = keylet::line(issue.
account, account, issue.
currency);
2902 if (
auto state = view.
peek(index))
2904 STAmount final_balance = state->getFieldAmount(sfBalance);
2909 STAmount const start_balance = final_balance;
2911 final_balance -= amount;
2930 state->setFieldAmount(sfBalance, final_balance);
2935 bSenderHigh ? account : issue.
account,
2936 bSenderHigh ? issue.
account : account,
2952 auto const receiverAccount = view.
peek(keylet::account(account));
2953 if (!receiverAccount)
2986 "xrpl::redeemIOU : neither account nor issuer is XRP");
2989 XRPL_ASSERT(issue == amount.issue(),
"xrpl::redeemIOU : matching issue");
2993 issue.
account != account,
"xrpl::redeemIOU : not issuer account");
2995 JLOG(j.
trace()) <<
"redeemIOU: " << to_string(account) <<
": "
2996 << amount.getFullText();
2998 bool bSenderHigh = account > issue.
account;
3003 STAmount final_balance = state->getFieldAmount(sfBalance);
3008 STAmount const start_balance = final_balance;
3010 final_balance -= amount;
3013 view, state, bSenderHigh, account, start_balance, final_balance, j);
3023 state->setFieldAmount(sfBalance, final_balance);
3030 bSenderHigh ? issue.
account : account,
3031 bSenderHigh ? account : issue.
account,
3043 JLOG(j.
fatal()) <<
"redeemIOU: " << to_string(account)
3044 <<
" attempts to redeem " << amount.getFullText()
3045 <<
" but no trust line exists!";
3060 from != beast::zero,
"xrpl::transferXRP : nonzero from account");
3061 XRPL_ASSERT(to != beast::zero,
"xrpl::transferXRP : nonzero to account");
3062 XRPL_ASSERT(from != to,
"xrpl::transferXRP : sender is not receiver");
3063 XRPL_ASSERT(amount.native(),
"xrpl::transferXRP : amount is XRP");
3067 if (!sender || !receiver)
3070 JLOG(j.
trace()) <<
"transferXRP: " << to_string(from) <<
" -> "
3071 << to_string(to) <<
") : " << amount.getFullText();
3073 if (sender->getFieldAmount(sfBalance) < amount)
3085 sender->setFieldAmount(
3086 sfBalance, sender->getFieldAmount(sfBalance) - amount);
3089 receiver->setFieldAmount(
3090 sfBalance, receiver->getFieldAmount(sfBalance) + amount);
3106 auto const trustLine =
3109 if (!trustLine && authType == AuthType::StrongAuth)
3114 if (
auto const issuerAccount = view.
read(keylet::account(issue.
account));
3118 return ((*trustLine)[sfFlags] &
3136 auto const mptID = keylet::mptIssuance(mptIssue.
getMptID());
3137 auto const sleIssuance = view.
read(mptID);
3141 auto const mptIssuer = sleIssuance->getAccountID(sfIssuer);
3144 if (mptIssuer == account)
3147 bool const featureSAVEnabled =
3150 if (featureSAVEnabled)
3156 auto const sleIssuer = view.
read(keylet::account(mptIssuer));
3160 if (sleIssuer->isFieldPresent(sfVaultID))
3162 auto const sleVault =
3163 view.
read(keylet::vault(sleIssuer->getFieldH256(sfVaultID)));
3167 auto const asset = sleVault->at(sfAsset);
3171 return requireAuth(view, issue, account, authType);
3174 view, issue, account, authType, depth + 1);
3182 auto const mptokenID = keylet::mptoken(mptID.key, account);
3183 auto const sleToken = view.
read(mptokenID);
3187 (authType == AuthType::StrongAuth || authType == AuthType::Legacy))
3192 auto const maybeDomainID = sleIssuance->at(~sfDomainID);
3197 "xrpl::requireAuth : issuance requires authorization");
3199 if (
auto const ter =
3200 credentials::validDomain(view, *maybeDomainID, account);
3209 if (featureSAVEnabled)
3227 MPTID const& mptIssuanceID,
3232 auto const sleIssuance = view.
read(keylet::mptIssuance(mptIssuanceID));
3238 "xrpl::enforceMPTokenAuthorization : authorization required");
3240 if (account == sleIssuance->at(sfIssuer))
3243 auto const keylet = keylet::mptoken(mptIssuanceID, account);
3244 auto const sleToken = view.
read(keylet);
3245 auto const maybeDomainID = sleIssuance->at(~sfDomainID);
3247 bool const authorizedByDomain = [&]() ->
bool {
3249 if (!maybeDomainID.has_value())
3260 if (!authorizedByDomain && sleToken ==
nullptr)
3271 else if (!authorizedByDomain && maybeDomainID.has_value())
3278 else if (!authorizedByDomain)
3283 sleToken !=
nullptr && !maybeDomainID.has_value(),
3284 "xrpl::enforceMPTokenAuthorization : found MPToken");
3290 else if (authorizedByDomain && sleToken !=
nullptr)
3295 maybeDomainID.has_value(),
3296 "xrpl::enforceMPTokenAuthorization : found MPToken for domain");
3299 else if (authorizedByDomain)
3304 maybeDomainID.has_value() && sleToken ==
nullptr,
3305 "xrpl::enforceMPTokenAuthorization : new MPToken for domain");
3320 "xrpl::enforceMPTokenAuthorization : condition list is incomplete");
3332 auto const mptID = keylet::mptIssuance(mptIssue.
getMptID());
3333 auto const sleIssuance = view.
read(mptID);
3339 if (from != (*sleIssuance)[sfIssuer] && to != (*sleIssuance)[sfIssuer])
3355 auto const& issuerId = issue.
getIssuer();
3356 if (issuerId == from || issuerId == to)
3358 auto const sleIssuer = view.
read(keylet::account(issuerId));
3359 if (sleIssuer ==
nullptr)
3362 auto const isRippleDisabled = [&](
AccountID account) ->
bool {
3365 auto const line = view.
read(keylet::line(account, issue));
3368 bool const issuerHigh = issuerId > account;
3375 if (isRippleDisabled(from) && isRippleDisabled(to))
3384 Keylet const& ownerDirKeylet,
3391 unsigned int uDirEntry{0};
3392 uint256 dirEntry{beast::zero};
3395 if (view.
exists(ownerDirKeylet) &&
3396 dirFirst(view, ownerDirKeylet.
key, sleDirNode, uDirEntry, dirEntry))
3400 if (maxNodesToDelete && ++deleted > *maxNodesToDelete)
3404 auto sleItem = view.
peek(keylet::child(dirEntry));
3410 <<
"DeleteAccount: Directory node in ledger " << view.
seq()
3411 <<
" has index to object that is missing: "
3412 << to_string(dirEntry);
3418 sleItem->getFieldU16(sfLedgerEntryType))};
3422 auto const [ter, skipEntry] = deleter(nodeType, dirEntry, sleItem);
3444 "xrpl::cleanupOnAccountDelete : minimum dir entries");
3449 <<
"DeleteAccount iterator re-validation failed.";
3453 if (skipEntry == SkipEntry::No)
3457 dirNext(view, ownerDirKeylet.
key, sleDirNode, uDirEntry, dirEntry));
3470 if (!sleState || sleState->getType() != ltRIPPLE_STATE)
3474 sleState->getFieldAmount(sfLowLimit).getIssuer(),
3475 sleState->getFieldAmount(sfHighLimit).getIssuer());
3476 auto sleLow = view.
peek(keylet::account(low));
3477 auto sleHigh = view.
peek(keylet::account(high));
3478 if (!sleLow || !sleHigh)
3481 bool const ammLow = sleLow->isFieldPresent(sfAMMID);
3482 bool const ammHigh = sleHigh->isFieldPresent(sfAMMID);
3485 if (ammLow && ammHigh)
3489 if (!ammLow && !ammHigh)
3493 if (ammAccountID && (low != *ammAccountID && high != *ammAccountID))
3496 if (
auto const ter =
trustDelete(view, sleState, low, high, j);
3500 <<
"deleteAMMTrustLine: failed to delete the trustline.";
3505 if (!(sleState->getFlags() & uFlags))
3527 view, uSenderID, uReceiverID, saAmount, bCheckIssuer, j);
3532 !bCheckIssuer,
"xrpl::rippleCredit : not checking issuer");
3534 view, uSenderID, uReceiverID, saAmount, j);
3548 "xrpl::assetsToSharesDeposit : non-negative assets");
3550 assets.
asset() == vault->at(sfAsset),
3551 "xrpl::assetsToSharesDeposit : assets and vault match");
3552 if (assets.
negative() || assets.
asset() != vault->at(sfAsset))
3555 Number const assetTotal = vault->at(sfAssetsTotal);
3556 STAmount shares{vault->at(sfShareMPTID)};
3557 if (assetTotal == 0)
3563 Number const shareTotal = issuance->at(sfOutstandingAmount);
3564 shares = ((shareTotal * assets) / assetTotal).truncate();
3576 "xrpl::sharesToAssetsDeposit : non-negative shares");
3578 shares.
asset() == vault->at(sfShareMPTID),
3579 "xrpl::sharesToAssetsDeposit : shares and vault match");
3580 if (shares.
negative() || shares.
asset() != vault->at(sfShareMPTID))
3583 Number const assetTotal = vault->at(sfAssetsTotal);
3584 STAmount assets{vault->at(sfAsset)};
3585 if (assetTotal == 0)
3589 shares.
exponent() - vault->at(sfScale),
3592 Number const shareTotal = issuance->at(sfOutstandingAmount);
3593 assets = (assetTotal * shares) / shareTotal;
3606 "xrpl::assetsToSharesDeposit : non-negative assets");
3608 assets.
asset() == vault->at(sfAsset),
3609 "xrpl::assetsToSharesWithdraw : assets and vault match");
3610 if (assets.
negative() || assets.
asset() != vault->at(sfAsset))
3613 Number assetTotal = vault->at(sfAssetsTotal);
3614 assetTotal -= vault->at(sfLossUnrealized);
3615 STAmount shares{vault->at(sfShareMPTID)};
3616 if (assetTotal == 0)
3618 Number const shareTotal = issuance->at(sfOutstandingAmount);
3619 Number result = (shareTotal * assets) / assetTotal;
3620 if (truncate == TruncateShares::yes)
3634 "xrpl::sharesToAssetsDeposit : non-negative shares");
3636 shares.
asset() == vault->at(sfShareMPTID),
3637 "xrpl::sharesToAssetsWithdraw : shares and vault match");
3638 if (shares.
negative() || shares.
asset() != vault->at(sfShareMPTID))
3641 Number assetTotal = vault->at(sfAssetsTotal);
3642 assetTotal -= vault->at(sfLossUnrealized);
3643 STAmount assets{vault->at(sfAsset)};
3644 if (assetTotal == 0)
3646 Number const shareTotal = issuance->at(sfOutstandingAmount);
3647 assets = (assetTotal * shares) / shareTotal;
3658 auto const mptIssue = amount.get<
MPTIssue>();
3659 auto const mptID = keylet::mptIssuance(mptIssue.getMptID());
3660 auto sleIssuance = view.
peek(mptID);
3663 JLOG(j.
error()) <<
"rippleLockEscrowMPT: MPT issuance not found for "
3664 << mptIssue.getMptID();
3668 if (amount.getIssuer() == sender)
3671 <<
"rippleLockEscrowMPT: sender is the issuer, cannot lock MPTs.";
3678 auto const mptokenID = keylet::mptoken(mptID.key, sender);
3679 auto sle = view.
peek(mptokenID);
3683 <<
"rippleLockEscrowMPT: MPToken not found for " << sender;
3687 auto const amt = sle->getFieldU64(sfMPTAmount);
3688 auto const pay = amount.mpt().value();
3694 <<
"rippleLockEscrowMPT: insufficient MPTAmount for "
3695 << to_string(sender) <<
": " << amt <<
" < " << pay;
3699 (*sle)[sfMPTAmount] = amt - pay;
3702 uint64_t
const locked = (*sle)[~sfLockedAmount].value_or(0);
3707 <<
"rippleLockEscrowMPT: overflow on locked amount for "
3708 << to_string(sender) <<
": " << locked <<
" + " << pay;
3712 if (sle->isFieldPresent(sfLockedAmount))
3713 (*sle)[sfLockedAmount] += pay;
3715 sle->setFieldU64(sfLockedAmount, pay);
3723 uint64_t
const issuanceEscrowed =
3724 (*sleIssuance)[~sfLockedAmount].value_or(0);
3725 auto const pay = amount.mpt().value();
3731 JLOG(j.
error()) <<
"rippleLockEscrowMPT: overflow on issuance "
3732 "locked amount for "
3733 << mptIssue.getMptID() <<
": " << issuanceEscrowed
3738 if (sleIssuance->isFieldPresent(sfLockedAmount))
3739 (*sleIssuance)[sfLockedAmount] += pay;
3741 sleIssuance->setFieldU64(sfLockedAmount, pay);
3743 view.
update(sleIssuance);
3759 netAmount == grossAmount,
3760 "xrpl::rippleUnlockEscrowMPT : netAmount == grossAmount");
3762 auto const& issuer = netAmount.
getIssuer();
3764 auto const mptID = keylet::mptIssuance(mptIssue.getMptID());
3765 auto sleIssuance = view.
peek(mptID);
3768 JLOG(j.
error()) <<
"rippleUnlockEscrowMPT: MPT issuance not found for "
3769 << mptIssue.getMptID();
3775 if (!sleIssuance->isFieldPresent(sfLockedAmount))
3778 <<
"rippleUnlockEscrowMPT: no locked amount in issuance for "
3779 << mptIssue.getMptID();
3783 auto const locked = sleIssuance->getFieldU64(sfLockedAmount);
3784 auto const redeem = grossAmount.
mpt().
value();
3791 <<
"rippleUnlockEscrowMPT: insufficient locked amount for "
3792 << mptIssue.getMptID() <<
": " << locked <<
" < " << redeem;
3796 auto const newLocked = locked - redeem;
3798 sleIssuance->makeFieldAbsent(sfLockedAmount);
3800 sleIssuance->setFieldU64(sfLockedAmount, newLocked);
3801 view.
update(sleIssuance);
3804 if (issuer != receiver)
3807 auto const mptokenID = keylet::mptoken(mptID.key, receiver);
3808 auto sle = view.
peek(mptokenID);
3812 <<
"rippleUnlockEscrowMPT: MPToken not found for " << receiver;
3816 auto current = sle->getFieldU64(sfMPTAmount);
3817 auto delta = netAmount.
mpt().
value();
3823 <<
"rippleUnlockEscrowMPT: overflow on MPTAmount for "
3824 << to_string(receiver) <<
": " <<
current <<
" + " << delta;
3828 (*sle)[sfMPTAmount] += delta;
3834 auto const outstanding = sleIssuance->getFieldU64(sfOutstandingAmount);
3835 auto const redeem = netAmount.
mpt().
value();
3842 <<
"rippleUnlockEscrowMPT: insufficient outstanding amount for "
3843 << mptIssue.getMptID() <<
": " << outstanding <<
" < "
3848 sleIssuance->setFieldU64(sfOutstandingAmount, outstanding - redeem);
3849 view.
update(sleIssuance);
3852 if (issuer == sender)
3854 JLOG(j.
error()) <<
"rippleUnlockEscrowMPT: sender is the issuer, "
3855 "cannot unlock MPTs.";
3861 auto const mptokenID = keylet::mptoken(mptID.key, sender);
3862 auto sle = view.
peek(mptokenID);
3866 <<
"rippleUnlockEscrowMPT: MPToken not found for " << sender;
3870 if (!sle->isFieldPresent(sfLockedAmount))
3873 <<
"rippleUnlockEscrowMPT: no locked amount in MPToken for "
3874 << to_string(sender);
3878 auto const locked = sle->getFieldU64(sfLockedAmount);
3879 auto const delta = grossAmount.
mpt().
value();
3885 <<
"rippleUnlockEscrowMPT: insufficient locked amount for "
3886 << to_string(sender) <<
": " << locked <<
" < " << delta;
3890 auto const newLocked = locked - delta;
3892 sle->makeFieldAbsent(sfLockedAmount);
3894 sle->setFieldU64(sfLockedAmount, newLocked);
3905 auto const outstanding = sleIssuance->getFieldU64(sfOutstandingAmount);
3911 <<
"rippleUnlockEscrowMPT: insufficient outstanding amount for "
3912 << mptIssue.getMptID() <<
": " << outstanding <<
" < " << diff;
3916 sleIssuance->setFieldU64(sfOutstandingAmount, outstanding - diff);
3917 view.
update(sleIssuance);
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 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 erase(std::shared_ptr< SLE > const &sle)=0
Remove a peeked SLE.
virtual void insert(std::shared_ptr< SLE > const &sle)=0
Insert a new state SLE.
virtual void creditHook(AccountID const &from, AccountID const &to, STAmount const &amount, STAmount const &preCreditBalance)
virtual void adjustOwnerCountHook(AccountID const &account, std::uint32_t cur, std::uint32_t next)
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.
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
AccountID const & getIssuer() const
std::chrono::time_point< NetClock > time_point
std::chrono::duration< rep, period > duration
Number is a floating point type that can represent a wide range of values.
Number truncate() const noexcept
virtual Rules const & rules() const =0
Returns the tx processing rules.
NetClock::time_point parentCloseTime() const
Returns the close time of the previous 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.
virtual LedgerHeader const & header() const =0
Returns information about the ledger.
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.
LedgerIndex seq() const
Returns the sequence number of the base ledger.
virtual std::uint32_t ownerCountHook(AccountID const &account, std::uint32_t count) const
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
constexpr bool holds() const noexcept
constexpr TIss const & get() const
std::string getFullText() const override
static constexpr std::uint64_t cMaxValue
void setIssuer(AccountID const &uIssuer)
std::uint64_t mantissa() const noexcept
bool negative() const noexcept
STAmount zeroed() const
Returns a zero value with the same issuer and currency.
static int const cMaxOffset
Currency const & getCurrency() const
bool native() const noexcept
Asset const & asset() const
int exponent() const noexcept
AccountID const & getIssuer() const
std::shared_ptr< STLedgerEntry > const & ref
std::shared_ptr< STLedgerEntry const > const_pointer
bool isFieldPresent(SField const &field) const
T emplace_back(T... args)
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 const & skip() noexcept
The index of the "short" skip list.
Keylet depositPreauth(AccountID const &owner, AccountID const &preauthorized) noexcept
A DepositPreauth.
Keylet const & amendments() noexcept
The index of the amendment table.
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Keylet mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Keylet amm(Asset const &issue1, Asset const &issue2) noexcept
AMM entry.
Keylet child(uint256 const &key) noexcept
Any item that can be in an owner dir.
Keylet vault(AccountID const &owner, 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 mptoken(MPTID const &issuanceID, AccountID const &holder) 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.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
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 uSrcQualityIn, std::uint32_t uSrcQualityOut, beast::Journal j)
Create a trust line.
std::optional< STAmount > sharesToAssetsDeposit(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &shares)
std::vector< SField const * > const & getPseudoAccountFields()
XRPAmount xrpLiquid(ReadView const &view, AccountID const &id, std::int32_t ownerCountAdj, beast::Journal j)
TER addEmptyHolding(ApplyView &view, AccountID const &accountID, XRPAmount priorBalance, Issue const &issue, beast::Journal journal)
Any transactors that call addEmptyHolding() in doApply must call canAddHolding() in preflight with th...
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
FreezeHandling
Controls the treatment of frozen account balances.
bool dirFirst(ApplyView &view, uint256 const &root, std::shared_ptr< SLE > &page, unsigned int &index, uint256 &entry)
AccountID pseudoAccountAddress(ReadView const &view, uint256 const &pseudoOwnerKey)
bool dirIsEmpty(ReadView const &view, Keylet const &k)
Returns true if the directory is empty.
SpendableHandling
Controls whether to include the account's full spendable balance.
bool isXRP(AccountID const &c)
TER canAddHolding(ReadView const &view, Asset const &asset)
std::uint8_t constexpr maxAssetCheckDepth
Maximum recursion depth for vault shares being put as an asset inside another vault; counted from 0.
std::map< uint256, NetClock::time_point > majorityAmendments_t
std::set< uint256 > getEnabledAmendments(ReadView const &view)
TER issueIOU(ApplyView &view, AccountID const &account, STAmount const &amount, Issue const &issue, beast::Journal j)
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
TER verifyValidDomain(ApplyView &view, AccountID const &account, uint256 domainID, beast::Journal j)
TER removeEmptyHolding(ApplyView &view, AccountID const &accountID, Issue const &issue, beast::Journal journal)
bool isVaultPseudoAccountFrozen(ReadView const &view, AccountID const &account, MPTIssue const &mptShare, int depth)
std::string to_string(base_uint< Bits, Tag > const &a)
TER rippleUnlockEscrowMPT(ApplyView &view, AccountID const &uGrantorID, AccountID const &uGranteeID, STAmount const &netAmount, STAmount const &grossAmount, beast::Journal j)
TER doWithdraw(ApplyView &view, STTx const &tx, AccountID const &senderAcct, AccountID const &dstAcct, AccountID const &sourceAcct, XRPAmount priorBalance, STAmount const &amount, beast::Journal j)
static TER accountSendMultiIOU(ApplyView &view, AccountID const &senderID, Issue const &issue, MultiplePaymentDestinations const &receivers, beast::Journal j, WaiveTransferFee waiveFee)
bool hasExpired(ReadView const &view, std::optional< std::uint32_t > const &exp)
Determines whether the given expiration time has passed.
static TER rippleSendMPT(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, STAmount &saActual, beast::Journal j, WaiveTransferFee waiveFee)
@ expired
List is expired, but has the largest non-pending sequence seen so far.
std::uint64_t constexpr maxMPTokenAmount
The maximum amount of MPTokenIssuance.
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.
bool isAnyFrozen(ReadView const &view, std::initializer_list< AccountID > const &accounts, MPTIssue const &mptIssue, int depth=0)
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=shSIMPLE_BALANCE)
Number root(Number f, unsigned d)
static TER accountSendMPT(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee)
static TER rippleCreditMPT(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, beast::Journal j)
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.
STAmount multiply(STAmount const &amount, Rate const &rate)
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
TER authorizeMPToken(ApplyView &view, XRPAmount const &priorBalance, MPTID const &mptIssuanceID, AccountID const &account, beast::Journal journal, std::uint32_t flags=0, std::optional< AccountID > holderID=std::nullopt)
TER deleteAMMTrustLine(ApplyView &view, std::shared_ptr< SLE > sleState, std::optional< AccountID > const &ammAccountID, beast::Journal j)
Delete trustline to AMM.
STAmount creditLimit(ReadView const &view, AccountID const &account, AccountID const &issuer, Currency const ¤cy)
Calculate the maximum amount of IOUs that an account can hold.
static TER rippleCreditIOU(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, bool bCheckIssuer, beast::Journal j)
bool isDeepFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
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.
static TER rippleSendIOU(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, STAmount &saActual, beast::Journal j, WaiveTransferFee waiveFee)
static TER rippleSendMultiMPT(ApplyView &view, AccountID const &senderID, MPTIssue const &mptIssue, MultiplePaymentDestinations const &receivers, STAmount &actual, beast::Journal j, WaiveTransferFee waiveFee)
TER verifyDepositPreauth(STTx const &tx, ApplyView &view, AccountID const &src, AccountID const &dst, std::shared_ptr< SLE > const &sleDst, beast::Journal j)
@ current
This was a new validation and was added.
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,...
bool isPseudoAccount(std::shared_ptr< SLE const > sleAcct, std::set< SField const * > const &pseudoFieldFilter={})
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()})
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee=WaiveTransferFee::No)
Calls static accountSendIOU if saAmount represents Issue.
std::optional< STAmount > assetsToSharesDeposit(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &assets)
STAmount accountFunds(ReadView const &view, AccountID const &id, STAmount const &saDefault, FreezeHandling freezeHandling, beast::Journal j)
bool isGlobalFrozen(ReadView const &view, AccountID const &issuer)
std::optional< uint256 > hashOfSeq(ReadView const &ledger, LedgerIndex seq, beast::Journal journal)
Return the hash of a ledger by sequence.
std::optional< STAmount > assetsToSharesWithdraw(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &assets, TruncateShares truncate=TruncateShares::no)
static TER rippleSendMultiIOU(ApplyView &view, AccountID const &senderID, Issue const &issue, MultiplePaymentDestinations const &receivers, STAmount &actual, beast::Journal j, WaiveTransferFee waiveFee)
bool canAdd(STAmount const &amt1, STAmount const &amt2)
Safely checks if two STAmount values can be added without overflow, underflow, or precision loss.
TERSubset< CanCvtToTER > TER
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 STAmount getTrustLineBalance(ReadView const &view, SLE::const_ref sle, AccountID const &account, Currency const ¤cy, AccountID const &issuer, bool includeOppositeLimit, beast::Journal j)
static TER withdrawToDestExceedsLimit(ReadView const &view, AccountID const &from, AccountID const &to, STAmount const &amount)
AuthHandling
Controls the treatment of unauthorized MPT balances.
Rate transferRate(ReadView const &view, AccountID const &issuer)
Returns IOU issuer transfer fee as Rate.
static SLE::const_pointer getLineIfUsable(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
majorityAmendments_t getMajorityAmendments(ReadView const &view)
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
STAmount creditBalance(ReadView const &view, AccountID const &account, AccountID const &issuer, Currency const ¤cy)
Returns the amount of IOUs issued by issuer that are held by an account.
TER requireAuth(ReadView const &view, Issue const &issue, AccountID const &account, AuthType authType=AuthType::Legacy)
Check if the account lacks required authorization.
TER canTransfer(ReadView const &view, MPTIssue const &mptIssue, AccountID const &from, AccountID const &to)
Check if the destination account is allowed to receive MPT.
bool isFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
TER transferXRP(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &amount, beast::Journal j)
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
constexpr std::uint32_t const tfMPTUnauthorize
TER dirLink(ApplyView &view, AccountID const &owner, std::shared_ptr< SLE > &object, SF_UINT64 const &node=sfOwnerNode)
bool isIndividualFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
TER offerDelete(ApplyView &view, std::shared_ptr< SLE > const &sle, beast::Journal j)
Delete an offer.
AccountID const & noAccount()
A placeholder for empty accounts.
static bool updateTrustLine(ApplyView &view, SLE::pointer state, bool bSenderHigh, AccountID const &sender, STAmount const &before, STAmount const &after, beast::Journal j)
TER redeemIOU(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)
bool canSubtract(STAmount const &amt1, STAmount const &amt2)
Determines if it is safe to subtract one STAmount from another.
bool isTesSuccess(TER x) noexcept
std::optional< STAmount > sharesToAssetsWithdraw(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &shares)
TER rippleLockEscrowMPT(ApplyView &view, AccountID const &uGrantorID, STAmount const &saAmount, beast::Journal j)
LedgerEntryType
Identifiers for on-ledger objects.
AccountID const & xrpAccount()
Compute AccountID from public key.
@ tecNO_LINE_INSUF_RESERVE
@ tecINSUFFICIENT_RESERVE
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.
static TER accountSendMultiMPT(ApplyView &view, AccountID const &senderID, MPTIssue const &mptIssue, MultiplePaymentDestinations const &receivers, beast::Journal j, WaiveTransferFee waiveFee)
TER enforceMPTokenAuthorization(ApplyView &view, MPTID const &mptIssuanceID, AccountID const &account, XRPAmount const &priorBalance, beast::Journal j)
Enforce account has MPToken to match its authorization.
TER canWithdraw(ReadView const &view, AccountID const &from, AccountID const &to, SLE::const_ref toSle, STAmount const &amount, bool hasDestinationTag)
Checks that can withdraw funds from an object to itself or a destination.
Expected< std::shared_ptr< SLE >, TER > createPseudoAccount(ApplyView &view, uint256 const &pseudoOwnerKey, SField const &ownerField)
Create pseudo-account, storing pseudoOwnerKey into ownerField.
TER checkDestinationAndTag(SLE::const_ref toSle, bool hasDestinationTag)
Validates that the destination SLE and tag are valid.
TER accountSendMulti(ApplyView &view, AccountID const &senderID, Asset const &asset, MultiplePaymentDestinations const &receivers, beast::Journal j, WaiveTransferFee waiveFee=WaiveTransferFee::No)
Like accountSend, except one account is sending multiple payments (with the same asset!...
TER trustDelete(ApplyView &view, std::shared_ptr< SLE > const &sleRippleState, AccountID const &uLowAccountID, AccountID const &uHighAccountID, beast::Journal j)
bool isLPTokenFrozen(ReadView const &view, AccountID const &account, Issue const &asset, Issue const &asset2)
Rate const parityRate
A transfer rate signifying a 1:1 exchange.
bool dirNext(ApplyView &view, uint256 const &root, std::shared_ptr< SLE > &page, unsigned int &index, uint256 &entry)
TER cleanupOnAccountDelete(ApplyView &view, Keylet const &ownerDirKeylet, EntryDeleter const &deleter, beast::Journal j, std::optional< std::uint16_t > maxNodesToDelete=std::nullopt)
Cleanup owner directory entries on account delete.
TER rippleCredit(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, bool bCheckIssuer, beast::Journal j)
Calls static rippleCreditIOU if saAmount represents Issue.
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.
A field with a type known at compile time.
Returns the RIPEMD-160 digest of the SHA256 hash of the message.
T time_since_epoch(T... args)