1#include <xrpld/app/misc/TxQ.h>
3#include <xrpld/app/ledger/OpenLedger.h>
4#include <xrpld/app/main/Application.h>
6#include <xrpl/basics/Log.h>
7#include <xrpl/basics/base_uint.h>
8#include <xrpl/basics/contract.h>
9#include <xrpl/basics/mulDiv.h>
10#include <xrpl/beast/utility/Journal.h>
11#include <xrpl/beast/utility/Zero.h>
12#include <xrpl/beast/utility/instrumentation.h>
13#include <xrpl/config/BasicConfig.h>
14#include <xrpl/config/Constants.h>
15#include <xrpl/json/json_value.h>
16#include <xrpl/ledger/ApplyView.h>
17#include <xrpl/ledger/ApplyViewImpl.h>
18#include <xrpl/ledger/OpenView.h>
19#include <xrpl/ledger/ReadView.h>
20#include <xrpl/ledger/helpers/SponsorHelpers.h>
21#include <xrpl/protocol/AccountID.h>
22#include <xrpl/protocol/Indexes.h>
23#include <xrpl/protocol/Keylet.h>
24#include <xrpl/protocol/LedgerFormats.h>
25#include <xrpl/protocol/Protocol.h>
26#include <xrpl/protocol/RippleLedgerHash.h>
27#include <xrpl/protocol/SField.h>
28#include <xrpl/protocol/STTx.h>
29#include <xrpl/protocol/SeqProxy.h>
30#include <xrpl/protocol/TER.h>
31#include <xrpl/protocol/TxFormats.h>
32#include <xrpl/protocol/Units.h>
33#include <xrpl/protocol/XRPAmount.h>
34#include <xrpl/protocol/jss.h>
35#include <xrpl/tx/Transactor.h>
36#include <xrpl/tx/apply.h>
37#include <xrpl/tx/applySteps.h>
39#include <boost/function/function_base.hpp>
66static std::expected<FeeLevel64, TER>
73 auto const [baseFee, effectiveFeePaid] = [&view, &tx, fee = *computedBaseFee] {
74 XRPAmount const feePaid = tx[sfFee].xrp();
82 return def.signum() == 0 ?
XRPAmount{1} : def;
84 return std::pair{fee + mod, feePaid + mod};
87 XRPL_ASSERT(baseFee.signum() > 0,
"xrpl::getFeeLevelPaid : positive fee");
88 if (effectiveFeePaid.signum() <= 0 || baseFee.signum() <= 0)
108 return mulDiv(level, 100 + increasePercent, 100)
227 std::expected<bool, ApplyResult>
282 if (firstIter == acctTxs.
end())
302 JLOG(
j_.trace()) <<
"Rejecting blocker transaction " <<
txID_
303 <<
". Account has other queued transactions.";
311 JLOG(
j_.trace()) <<
"Rejecting blocker transaction " <<
txID_
312 <<
". Blocker does not replace lone queued transaction.";
327 if (
auto const existingIter = txQAcct.find(
txSeq_); existingIter != txQAcct.end())
337 [[maybe_unused]]
auto const& [replacedSeq, replacedTx] = **
txToReplaceIter_;
343 auto const requiredRetryLevel =
increase(replacedTx.feeLevel,
txq_.setup_.retrySequencePercent);
344 JLOG(
j_.trace()) <<
"Found transaction in queue for account " <<
accID_ <<
" with " <<
txSeq_
345 <<
" new txn fee level is " <<
feeLevelPaid_ <<
", old txn fee level is "
346 << replacedTx.feeLevel <<
", new txn needs fee level of "
347 << requiredRetryLevel;
353 JLOG(
j_.trace()) <<
"Removing transaction from queue " << replacedTx.txID <<
" in favor of "
359 JLOG(
j_.trace()) <<
"Ignoring transaction " <<
txID_ <<
" in favor of queued "
380 auto const& seq =
prevTxs_->first->first;
381 auto const& tx =
prevTxs_->first->second;
395std::expected<bool, ApplyResult>
404 bool requiresMultiTxn =
false;
419 requiresMultiTxn =
true;
422 return requiresMultiTxn;
437 XRPL_ASSERT(prevIter != endIter,
"xrpl::TxQApplyImpl::checkFrontTx : not end");
476 UNREACHABLE(
"xrpl::TxQApplyImpl::calcSpendAndFee !prevTxs_");
477 return {potentialSpend, totalFee};
480 for (
auto iter =
prevTxs_->first, end =
prevTxs_->second; iter != end; ++iter)
482 [[maybe_unused]]
auto const& [seq, tx] = *iter;
489 totalFee += tx.consequences().fee();
490 potentialSpend += tx.consequences().potentialSpend();
498 potentialSpend +=
preflightRes_.consequences.potentialSpend();
502 return {potentialSpend, totalFee};
517 if (!requiresMultiTxn)
518 return requiresMultiTxn.error();
519 if (!*requiresMultiTxn)
537 UNREACHABLE(
"xrpl::TxQApplyImpl::processAccountTxs !prevTxs_");
579 auto const balance = (*accSle_)[sfBalance].xrp();
597 auto const reserve =
view_.fees().reserve;
598 auto const base =
view_.fees().base;
599 if (totalFee >= balance || (reserve > 10 * base && totalFee >= reserve))
602 JLOG(
j_.trace()) <<
"Ignoring transaction " <<
txID_ <<
". Total fees in flight too high.";
616 auto const potentialTotalSpend =
621 "xrpl::TxQApplyImpl::processAccountTxs : total spend check");
622 sleBump->setFieldAmount(sfBalance, balance - potentialTotalSpend);
630 sleBump->at(sfSequence) =
665 auto result =
txq_.tryClearAccountQueueUpThruTx(
707 JLOG(
j_.trace()) <<
"Transaction " <<
txID_ <<
" cannot be held";
719 auto lastRIter =
txq_.byFee_.rbegin();
720 while (lastRIter !=
txq_.byFee_.rend() && lastRIter->account ==
accID_)
724 if (lastRIter ==
txq_.byFee_.rend())
732 JLOG(
j_.info()) <<
"Queue is full, and transaction " <<
txID_
733 <<
" would kick a transaction from the same account (" <<
accID_
734 <<
") out of the queue.";
737 auto const& endAccount =
txq_.byAccount_.at(lastRIter->account);
738 auto const endEffectiveFeeLevel = [&] {
742 if (lastRIter->feeLevel >
feeLevelPaid_ || endAccount.transactions.size() == 1)
743 return lastRIter->feeLevel;
747 endAccount.transactions.begin(),
748 endAccount.transactions.end(),
752 auto const next = txn.second.feeLevel / endAccount.transactions.size();
753 auto const mod = txn.second.feeLevel % endAccount.transactions.size();
754 if (total.first >= kMax - next || total.second >= kMax - mod)
755 return {kMax, FeeLevel64{0}};
757 return {total.first + next, total.second + mod};
759 return endTotal.first + endTotal.second / endAccount.transactions.size();
766 auto const dropRIter = endAccount.transactions.rbegin();
768 dropRIter->second.account == lastRIter->account,
769 "xrpl::TxQApplyImpl::processQueueIsFull : cheapest transaction found");
770 JLOG(
j_.info()) <<
"Removing last item of account " << lastRIter->account
771 <<
" from queue with average fee of " << endEffectiveFeeLevel
773 txq_.erase(
txq_.byFee_.iterator_to(dropRIter->second));
777 JLOG(
j_.info()) <<
"Queue is full, and transaction " <<
txID_
778 <<
" fee is lower than end item's account average fee";
789 bool created =
false;
794 XRPL_ASSERT(created,
"xrpl::TxQApplyImpl::addTxToQueue : account created");
808 txq_.byFee_.insert(candidate);
809 JLOG(
j_.debug()) <<
"Added transaction " << candidate.txID <<
" with result "
811 <<
" account " << candidate.account <<
" to queue."
825 auto const txBegin = view.
txs.
begin();
826 auto const txEnd = view.
txs.
end();
831 if (maybeFeeLevel.has_value())
838 JLOG(
j_.warn()) <<
"Unable to compute the fee level for a validated transaction "
839 << tx.first->getTransactionID() <<
" in ledger " << view.
header().
seq
845 JLOG((timeLeap ?
j_.warn() :
j_.debug()))
846 <<
"Ledger " << view.
header().
seq <<
" has " << size <<
" transactions. "
847 <<
"Ledgers are processing " << (timeLeap ?
"slowly" :
"as expected")
848 <<
". Expected transactions is currently " <<
txnsExpected_ <<
" and multiplier is "
870 auto const next = [&] {
890 if (feeLevels.
empty())
900 auto const count = feeLevels.
size();
902 (feeLevels[count / 2] + feeLevels[(count - 1) / 2] +
FeeLevel64{1}) / 2;
905 JLOG(
j_.debug()) <<
"Expected transactions updated to " <<
txnsExpected_
915 auto const current = view.
txCount();
922 if (current > target)
926 return mulDiv(multiplier, current * current, target * target)
951 return {
true, (x * (x + 1) * ((2 * x) + 1)) / 6};
955static_assert(sumOfFirstSquares(1).first);
956static_assert(sumOfFirstSquares(1).second == 1);
958static_assert(sumOfFirstSquares(2).first);
959static_assert(sumOfFirstSquares(2).second == 5);
961static_assert(sumOfFirstSquares(0x1FFFFF).first);
962static_assert(sumOfFirstSquares(0x1FFFFF).second == 0x2AAAA8AAAAB00000ul);
964static_assert(!sumOfFirstSquares(0x200000).first);
980 auto const current = view.
txCount() + extraCount;
984 auto const last = current + seriesSize - 1;
991 "xrpl::TxQ::FeeMetrics::escalatedSeriesFeeLevel : current over "
1005 if (!sumNlast.first)
1006 return {sumNlast.first,
FeeLevel64{sumNlast.second}};
1007 auto const totalFeeLevel =
1008 mulDiv(multiplier, sumNlast.second - sumNcurrent.second, target * target);
1011 return {totalFeeLevel.has_value(), *totalFeeLevel};
1037 XRPL_ASSERT(
pfResult,
"xrpl::TxQ::MaybeTx::apply : preflight result is set");
1042 JLOG(j.
debug()) <<
"Queued transaction " <<
txID
1043 <<
" rules or flags have changed. Flags from " <<
pfResult->flags <<
" to "
1052 return doApply(pcresult, app, view);
1064TxQ::TxQAccount::TxMap::const_iterator
1069 auto sameOrPrevIter =
transactions.lower_bound(seqProx);
1072 return sameOrPrevIter;
1079 [[maybe_unused]]
auto const* txnPtr = &txn;
1081 auto const result =
transactions.emplace(seqProx, std::move(txn));
1082 XRPL_ASSERT(result.second,
"xrpl::TxQ::TxQAccount::add : emplace succeeded");
1083 XRPL_ASSERT(&result.first->second != txnPtr,
"xrpl::TxQ::TxQAccount::add : transaction moved");
1085 return result.first->second;
1106template <
size_t FillPercentage>
1110 static_assert(FillPercentage > 0 && FillPercentage <= 100,
"Invalid fill percentage");
1120 AccountMap::const_iterator
const& accountIter,
1150 if (lastValid && *lastValid < view.
header().
seq +
setup_.minimumLastLedgerBuffer)
1159 if (replacementIter)
1163 TxQAccount const& txQAcct = accountIter->second;
1170 if (txSeqProx.isTicket())
1179 if (txSeqProx != nextQueuable)
1188 nextTxIter != txQAcct.
transactions.
end() && nextTxIter->first.isSeq())
1199TxQ::erase(TxQ::FeeMultiSet::const_iterator_type candidateIter) -> FeeMultiSet::iterator_type
1201 auto& txQAccount = byAccount_.at(candidateIter->account);
1202 auto const seqProx = candidateIter->seqProxy;
1203 auto const newCandidateIter = byFee_.erase(candidateIter);
1207 [[maybe_unused]]
auto const found = txQAccount.remove(seqProx);
1208 XRPL_ASSERT(found,
"xrpl::TxQ::erase : account removed");
1210 return newCandidateIter;
1215 -> FeeMultiSet::iterator_type
1217 auto& txQAccount =
byAccount_.at(candidateIter->account);
1218 auto const accountIter = txQAccount.transactions.find(candidateIter->seqProxy);
1220 accountIter != txQAccount.transactions.end(),
"xrpl::TxQ::eraseAndAdvance : account found");
1226 candidateIter->seqProxy.isTicket() || accountIter == txQAccount.transactions.begin(),
1227 "xrpl::TxQ::eraseAndAdvance : ticket or sequence");
1229 byFee_.iterator_to(accountIter->second) == candidateIter,
1230 "xrpl::TxQ::eraseAndAdvance : found in byFee");
1231 auto const accountNextIter =
std::next(accountIter);
1235 auto const feeNextIter =
std::next(candidateIter);
1236 bool const useAccountNext = accountNextIter != txQAccount.transactions.end() &&
1237 accountNextIter->first > candidateIter->seqProxy &&
1238 (feeNextIter ==
byFee_.end() ||
byFee_.value_comp()(accountNextIter->second, *feeNextIter));
1240 auto const candidateNextIter =
byFee_.erase(candidateIter);
1241 txQAccount.transactions.erase(accountIter);
1243 return useAccountNext ?
byFee_.iterator_to(accountNextIter->second) : candidateNextIter;
1249 TxQ::TxQAccount::TxMap::const_iterator begin,
1250 TxQ::TxQAccount::TxMap::const_iterator end) -> TxQAccount::TxMap::iterator
1252 for (
auto it = begin; it != end; ++it)
1256 return txQAccount.transactions.erase(begin, end);
1264 TxQ::AccountMap::iterator
const& accountIter,
1265 TxQAccount::TxMap::iterator
const& beginTxIter,
1275 beginTxIter != accountIter->second.transactions.end(),
1276 "xrpl::TxQ::tryClearAccountQueueUpThruTx : non-empty accounts input");
1280 auto endTxIter = accountIter->second.transactions.lower_bound(tSeqProx);
1283 auto const requiredTotalFeeLevel =
1288 if (!requiredTotalFeeLevel.first)
1292 beginTxIter, endTxIter, feeLevelPaid, [](
auto const& total,
auto const& txn) {
1293 return total + txn.second.feeLevel;
1297 if (totalFeeLevelPaid < requiredTotalFeeLevel.second)
1302 for (
auto it = beginTxIter; it != endTxIter; ++it)
1304 auto const txResult = it->second.apply(app, view, j);
1309 --it->second.retriesRemaining;
1310 it->second.lastResult = txResult.ter;
1331 if (!txResult.applied)
1334 return {txResult.ter,
false};
1339 auto const txResult =
doApply(
preclaim(pfResult, app, view), app, view);
1341 if (txResult.applied)
1345 endTxIter =
erase(accountIter->second, beginTxIter, endTxIter);
1347 if (endTxIter != accountIter->second.transactions.end() && endTxIter->first == tSeqProx)
1477 return impl.
apply();
1503 return directApplied;
1560 if (!computedFeeLevelPaid.has_value())
1561 return {computedFeeLevelPaid.error(),
false};
1605 if (!pcresult.likelyToClaimFee)
1606 return {pcresult.ter,
false};
1611 JLOG(
j_.trace()) <<
"Transaction " <<
txID_ <<
" from account " <<
accID_
1612 <<
" has fee level of " <<
feeLevelPaid_ <<
" needs at least "
1614 <<
view_.txCount() <<
" entries.";
1621 if (
auto const err =
canBeHeld(); err.has_value())
1662 auto const ledgerSeq = view.
header().
seq;
1668 for (
auto candidateIter =
byFee_.begin(); candidateIter !=
byFee_.end();)
1670 if (candidateIter->lastValid && *candidateIter->lastValid <= ledgerSeq)
1672 byAccount_.at(candidateIter->account).dropPenalty =
true;
1673 candidateIter =
erase(candidateIter);
1685 if (txQAccountIter->second.empty())
1687 txQAccountIter =
byAccount_.erase(txQAccountIter);
1702 JLOG(
j_.warn()) <<
"Parent ledger hash unchanged from " << parentHash;
1709 [[maybe_unused]]
auto const startingSize =
byFee_.size();
1724 for (
auto& [_, candidate] : account.transactions)
1726 byFee_.insert(candidate);
1729 XRPL_ASSERT(
byFee_.size() == startingSize,
"xrpl::TxQ::rebuildQueue : byFee size match");
1770 bool ledgerChanged =
false;
1774 auto const metricsSnapshot =
feeMetrics_.getSnapshot();
1776 for (
auto candidateIter =
byFee_.begin(); candidateIter !=
byFee_.end();)
1778 auto& account =
byAccount_.at(candidateIter->account);
1779 auto const beginIter = account.transactions.begin();
1780 if (candidateIter->seqProxy.isSeq() && candidateIter->seqProxy > beginIter->first)
1785 JLOG(
j_.trace()) <<
"Skipping queued transaction " << candidateIter->txID
1786 <<
" from account " << candidateIter->account
1787 <<
" as it is not the first.";
1792 auto const feeLevelPaid = candidateIter->feeLevel;
1793 JLOG(
j_.trace()) <<
"Queued transaction " << candidateIter->txID <<
" from account "
1794 << candidateIter->account <<
" has fee level of " << feeLevelPaid
1795 <<
" needs at least " << requiredFeeLevel;
1796 if (feeLevelPaid >= requiredFeeLevel)
1798 JLOG(
j_.trace()) <<
"Applying queued transaction " << candidateIter->txID
1799 <<
" to open ledger.";
1801 auto const [txnResult, didApply, metadata] = candidateIter->apply(app, view,
j_);
1806 JLOG(
j_.debug()) <<
"Queued transaction " << candidateIter->txID
1807 <<
" applied successfully with " <<
transToken(txnResult)
1808 <<
". Remove from queue.";
1811 ledgerChanged =
true;
1815 candidateIter->retriesRemaining <= 0)
1817 if (candidateIter->retriesRemaining <= 0)
1819 account.retryPenalty =
true;
1823 account.dropPenalty =
true;
1825 JLOG(
j_.debug()) <<
"Queued transaction " << candidateIter->txID <<
" failed with "
1826 <<
transToken(txnResult) <<
". Remove from queue.";
1831 JLOG(
j_.debug()) <<
"Queued transaction " << candidateIter->txID <<
" failed with "
1832 <<
transToken(txnResult) <<
". Leave in queue."
1833 <<
" Applied: " << didApply <<
". Flags: " << candidateIter->flags;
1834 if (account.retryPenalty && candidateIter->retriesRemaining > 2)
1836 candidateIter->retriesRemaining = 1;
1840 --candidateIter->retriesRemaining;
1842 candidateIter->lastResult = txnResult;
1843 if (account.dropPenalty && account.transactions.size() > 1 &&
isFull<95>())
1848 if (candidateIter->seqProxy.isTicket())
1853 <<
"Queue is nearly full, and transaction " << candidateIter->txID
1855 <<
". Removing ticketed tx from account " << account.account;
1864 auto const dropRIter = account.transactions.rbegin();
1866 dropRIter->second.account == candidateIter->account,
1867 "xrpl::TxQ::accept : account check");
1870 <<
"Queue is nearly full, and transaction " << candidateIter->txID
1872 <<
". Removing last item from account " << account.account;
1873 auto const endIter =
byFee_.iterator_to(dropRIter->second);
1874 if (endIter != candidateIter)
1897 return ledgerChanged;
1921 if (!sleAccount || sleAccount->getType() != ltACCOUNT_ROOT)
1927 auto const accountIter =
byAccount_.find((*sleAccount)[sfAccount]);
1928 if (accountIter ==
byAccount_.end() || accountIter->second.transactions.empty())
1938 if (txIter == acctTxs.
end() || !txIter->first.isSeq() || txIter->first != acctSeqProx)
1950 SeqProxy attempt = txIter->second.consequences().followingSeq();
1951 while (++txIter != acctTxs.
cend())
1953 if (attempt < txIter->first)
1956 attempt = txIter->second.consequences().followingSeq();
1979 auto const account = (*tx)[sfAccount];
1987 SeqProxy const txSeqProx = tx->getSeqProxy();
1991 if (txSeqProx.
isSeq() && txSeqProx != acctSeqProx)
1994 FeeLevel64 const requiredFeeLevel = [
this, &view, flags] {
2004 if (!computedFeeLevelPaid.has_value())
2006 return ApplyResult{computedFeeLevelPaid.error(),
false};
2008 FeeLevel64 const feeLevelPaid = *computedFeeLevelPaid;
2010 if (feeLevelPaid >= requiredFeeLevel)
2013 auto const transactionID = tx->getTransactionID();
2014 JLOG(
j_.trace()) <<
"Applying transaction " << transactionID <<
" to open ledger.";
2016 auto const [txnResult, didApply, metadata] =
xrpl::apply(app, view, *tx, flags, j);
2018 JLOG(
j_.trace()) <<
"New transaction " << transactionID
2019 << (didApply ?
" applied successfully with " :
" failed with ")
2028 auto const accountIter =
byAccount_.find(account);
2039 return ApplyResult{txnResult, didApply, metadata};
2049 if (replacedTxIter && tx)
2053 auto const deleteIter =
byFee_.iterator_to((*replacedTxIter)->second);
2054 XRPL_ASSERT(deleteIter !=
byFee_.end(),
"xrpl::TxQ::removeFromByFee : found in byFee");
2056 &(*replacedTxIter)->second == &*deleteIter,
2057 "xrpl::TxQ::removeFromByFee : matching transaction");
2059 deleteIter->seqProxy == tx->getSeqProxy(),
2060 "xrpl::TxQ::removeFromByFee : matching sequence");
2062 deleteIter->account == (*tx)[sfAccount],
2063 "xrpl::TxQ::removeFromByFee : matching account");
2085 result.
medFeeLevel = snapshot.escalationMultiplier;
2091std::expected<TxQ::FeeAndSeq, TER>
2094 auto const account = (*tx)[sfAccount];
2100 if (!maybeBaseFee.has_value())
2104 auto const baseFee = *maybeBaseFee;
2109 std::uint32_t const accountSeq = sle ? (*sle)[sfSequence] : 0;
2114 .accountSeq = accountSeq,
2115 .availableSeq = availableSeq,
2126 AccountMap::const_iterator
const accountIter{
byAccount_.find(account)};
2128 if (accountIter ==
byAccount_.end() || accountIter->second.transactions.empty())
2131 result.
reserve(accountIter->second.transactions.size());
2132 for (
auto const& tx : accountIter->second.transactions)
2148 for (
auto const& tx :
byFee_)
2160 BOOST_ASSERT(
false);
2170 ret[jss::ledger_current_index] = view->header().seq;
2182 auto const baseFee = view->fees().base;
2186 auto const effectiveBaseFee = [&baseFee, &
metrics] {
2187 if (!baseFee &&
metrics.openLedgerFeeLevel !=
metrics.referenceFeeLevel)
2193 drops[jss::base_fee] =
to_string(baseFee);
2196 metrics.minProcessingFeeLevel,
2197 metrics.txCount >=
metrics.txQMaxSize ? effectiveBaseFee : baseFee));
2198 auto openFee =
toDrops(
metrics.openLedgerFeeLevel, effectiveBaseFee);
2199 if (effectiveBaseFee &&
toFeeLevel(openFee, effectiveBaseFee) <
metrics.openLedgerFeeLevel)
2201 drops[jss::open_ledger_fee] =
to_string(openFee);
2226 "The minimum number of low-fee transactions allowed "
2227 "per ledger (minimum_txn_in_ledger) exceeds "
2228 "the maximum number of low-fee transactions allowed per "
2229 "ledger (maximum_txn_in_ledger).");
2234 "The minimum number of low-fee transactions allowed "
2235 "per ledger (minimum_txn_in_ledger_standalone) exceeds "
2236 "the maximum number of low-fee transactions allowed per "
2237 "ledger (maximum_txn_in_ledger).");
A generic endpoint for log messages.
Editable, discardable view that can build metadata for one tx.
Section & section(std::string const &name)
Returns the section with the given name.
std::shared_ptr< OpenView const > current() const
Returns a view to the current open ledger.
Writable ledger view that accumulates state and tx changes.
std::size_t txCount() const
Return the number of tx inserted since creation.
SLE::const_pointer read(Keylet const &k) const override
Return the state item associated with a key.
LedgerHeader const & header() const override
Returns information about the ledger.
void apply(TxsRawView &to) const
Apply changes.
Rules const & rules() const override
Returns the tx processing rules.
virtual LedgerHeader const & header() const =0
Returns information about the ledger.
std::shared_ptr< STLedgerEntry const > const & ConstRef
std::shared_ptr< STLedgerEntry const > const_pointer
std::uint32_t getFieldU32(SField const &field) const
bool isFieldPresent(SField const &field) const
SeqProxy getSeqProxy() const
TxType getTxnType() const
A type that represents either a sequence value or a ticket value.
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
constexpr std::uint32_t value() const
constexpr bool isSeq() const
virtual OpenLedger & getOpenLedger()=0
std::optional< TxMapCIter > txToReplaceIter_
std::shared_ptr< STTx const > const & tx_
std::expected< bool, ApplyResult > checkMultiTxn() const
PreflightResult const preflightRes_
TxQ::TxQAccount::TxMap::iterator TxMapIter
std::optional< std::reference_wrapper< std::scoped_lock< std::mutex > const > > txqLock_
std::optional< ApplyResult > checkFeeLevel() const
std::optional< ApplyResult > checkFrontTx(TxMapCIter const &prevIter, TxMapCIter const &endIter) const
std::optional< TxQ::FeeMetrics::Snapshot > metricsSnapshot_
std::optional< ApplyResult > checkIsTxBlocker() const
std::pair< TxMapIter, TxMapIter > TxRange
TxQApplyImpl(TxQ &txq, Application &app, OpenView &view, std::shared_ptr< STTx const > const &tx, ApplyFlags flags, beast::Journal j)
std::optional< MultiTxn > multiTxn_
std::optional< ApplyResult > canBeHeld() const
std::pair< XRPAmount, XRPAmount > calcSpendAndFee() const
std::optional< ApplyResult > checkBlockerInAccQue() const
std::optional< ApplyResult > processAccountTxs()
std::optional< ApplyResult > applyImplPrelock()
std::uint32_t accTxCount_
AccMapIter byAccountIter_
std::optional< ApplyResult > checkTicketInLedger() const
std::optional< TxRange > prevTxs_
TxQ::AccountMap::iterator AccMapIter
void retrieveTxToReplace()
std::optional< ApplyResult > tryClearAccountQueueUpThruTx()
TxQ::TxQAccount::TxMap::const_iterator TxMapCIter
FeeLevel64 requiredFeeLevel_
std::optional< ApplyResult > processQueueIsFull()
SLE::const_pointer accSle_
std::size_t txnsExpected_
Number of transactions expected per ledger.
std::size_t const targetTxnCount_
Number of transactions per ledger that fee escalation "workstowards".
static FeeLevel64 scaleFeeLevel(Snapshot const &snapshot, OpenView const &view)
Use the number of transactions in the current open ledger to compute the fee level a transaction must...
beast::Journal const j_
Journal.
std::optional< std::size_t > const maximumTxnCount_
Maximum value of txnsExpected.
std::size_t update(Application &app, ReadView const &view, bool timeLeap, TxQ::Setup const &setup)
Updates fee metrics based on the transactions in the ReadView for use in fee escalation calculations.
std::size_t const minimumTxnCount_
Minimum value of txnsExpected.
boost::circular_buffer< std::size_t > recentTxnCounts_
Recent history of transaction counts that exceed the targetTxnCount_.
static std::pair< bool, FeeLevel64 > escalatedSeriesFeeLevel(Snapshot const &snapshot, OpenView const &view, std::size_t extraCount, std::size_t seriesSize)
Computes the total fee level for all transactions in a series.
FeeLevel64 escalationMultiplier_
Based on the median fee of the LCL.
Represents a transaction in the queue which may be applied later to the open ledger.
static LedgerHash parentHashComp
The hash of the parent ledger.
std::optional< LedgerIndex > const lastValid
Expiration ledger for the transaction (sfLastLedgerSequence field).
TxID const txID
Transaction ID.
MaybeTx(std::shared_ptr< STTx const > const &txn, TxID const &txID, FeeLevel64 feeLevel, ApplyFlags const flags, PreflightResult const &pfResult)
Constructor.
FeeLevel64 const feeLevel
Computed fee level that the transaction will pay.
ApplyFlags const flags
Flags provided to apply.
ApplyResult apply(Application &app, OpenView &view, beast::Journal j)
Attempt to apply the queued transaction to the open ledger.
SeqProxy const seqProxy
Transaction SeqProxy number (sfSequence or sfTicketSequence field).
std::shared_ptr< STTx const > const txn
The complete transaction.
static constexpr int kRetriesAllowed
Starting retry count for newly queued transactions.
AccountID const account
Account "owner" of the transaction.
std::optional< PreflightResult const > pfResult
Cached result of the preflight operation.
Used to represent an account to the queue, and stores the transactions queued for that account by Seq...
TxMap::const_iterator getPrevTx(SeqProxy seqProx) const
Find the entry in transactions that precedes seqProx, if one does.
TxMap transactions
Sequence number will be used as the key.
MaybeTx & add(MaybeTx &&)
Add a transaction candidate to this account for queuing.
std::size_t getTxnCount() const
Return the number of transactions currently queued for this account.
TxQAccount(std::shared_ptr< STTx const > const &txn)
Construct from a transaction.
bool remove(SeqProxy seqProx)
Remove the candidate with given SeqProxy value from this account.
AccountID const account
The account.
std::map< SeqProxy, MaybeTx > TxMap
void rebuildQueue(OpenView &view)
Metrics getMetrics(OpenView const &view) const
Returns fee metrics in reference fee level units.
json::Value doRPC(Application &app) const
Summarize current fee metrics for the fee RPC command.
TxQ(Setup const &setup, beast::Journal j)
Constructor.
FeeMetrics feeMetrics_
Tracks the current state of the queue.
SeqProxy nextQueuableSeq(SLE::ConstRef sleAccount) const
Return the next sequence that would go in the TxQ for an account.
void removeFromByFee(std::optional< TxQAccount::TxMap::const_iterator > const &replacedTxIter, std::shared_ptr< STTx const > const &tx)
std::optional< size_t > maxSize_
Maximum number of transactions allowed in the queue based on the current metrics.
TER canBeHeld(STTx const &, ApplyFlags const, OpenView const &, SLE::ConstRef sleAccount, AccountMap::const_iterator const &, std::optional< TxQAccount::TxMap::const_iterator > const &, std::scoped_lock< std::mutex > const &lock) const
Checks if the indicated transaction fits the conditions for being stored in the queue.
std::expected< FeeAndSeq, TER > getTxRequiredFeeAndSeq(OpenView const &view, std::shared_ptr< STTx const > const &tx) const
Returns minimum required fee for tx and two sequences: first valid sequence for this account in curre...
void processClosedLedger(Application &app, ReadView const &view, bool timeLeap)
Update fee metrics and clean up the queue in preparation for the next ledger.
std::vector< TxDetails > getAccountTxs(AccountID const &account) const
Returns information about the transactions currently in the queue for the account.
bool isFull() const
Is the queue at least fillPercentage full?
FeeMultiSet::iterator_type eraseAndAdvance(FeeMultiSet::const_iterator_type)
Erase and return the next entry for the account (if fee level is higher), or next entry in byFee_ (lo...
static FeeLevel64 getRequiredFeeLevel(OpenView &view, ApplyFlags flags, FeeMetrics::Snapshot const &metricsSnapshot, std::scoped_lock< std::mutex > const &lock)
ApplyResult apply(Application &app, OpenView &view, std::shared_ptr< STTx const > const &tx, ApplyFlags flags, beast::Journal j)
Add a new transaction to the open ledger, hold it in the queue, or reject it.
std::optional< ApplyResult > tryDirectApply(Application &app, OpenView &view, std::shared_ptr< STTx const > const &tx, ApplyFlags flags, beast::Journal j)
virtual ~TxQ()
Destructor.
SeqProxy nextQueuableSeqImpl(SLE::ConstRef sleAccount, std::scoped_lock< std::mutex > const &) const
bool accept(Application &app, OpenView &view)
Fill the new open ledger with transactions from the queue.
std::mutex mutex_
Most queue operations are done under the master lock, but use this mutex for the RPC "fee" command,...
std::vector< TxDetails > getTxs() const
Returns information about all transactions currently in the queue.
FeeMultiSet byFee_
The queue itself: the collection of transactions ordered by fee level.
beast::Journal const j_
Journal.
friend class TxQApplyImpl
LedgerHash parentHash_
parentHash_ used for logging only
FeeMultiSet::iterator_type erase(FeeMultiSet::const_iterator_type)
Erase and return the next entry in byFee_ (lower fee level).
static constexpr FeeLevel64 kBaseLevel
Fee level for single-signed reference transaction.
AccountMap byAccount_
All of the accounts which currently have any transactions in the queue.
Setup const setup_
Setup parameters used to control the behavior of the queue.
ApplyResult tryClearAccountQueueUpThruTx(Application &app, OpenView &view, STTx const &tx, AccountMap::iterator const &accountIter, TxQAccount::TxMap::iterator const &beginTxIter, FeeLevel64 feeLevelPaid, PreflightResult const &pfResult, std::size_t const txExtraCount, ApplyFlags flags, FeeMetrics::Snapshot const &metricsSnapshot, beast::Journal j)
All-or-nothing attempt to try to apply the queued txs for accountIter up to and including tx.
T emplace_back(T... args)
@ Object
object value (collection of name/value pairs).
static constexpr std::pair< bool, std::uint64_t > sumOfFirstSquares(std::size_t xIn)
Keylet computation functions.
Keylet ticket(AccountID const &id, SeqProxy const &ticketSeq)
A ticket belonging to an account.
Keylet account(AccountID const &id) noexcept
AccountID root.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
@ telCAN_NOT_QUEUE_BLOCKED
@ telCAN_NOT_QUEUE_BALANCE
@ telCAN_NOT_QUEUE_BLOCKS
bool set(T &target, std::string const &name, Section const §ion)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
static std::optional< LedgerIndex > getLastLedgerSequence(STTx const &tx)
PreflightResult preflight(ServiceRegistry ®istry, Rules const &rules, STTx const &tx, ApplyFlags flags, beast::Journal j)
Gate a transaction based on static information.
std::optional< std::uint64_t > mulDiv(std::uint64_t value, std::uint64_t mul, std::uint64_t div)
Return value*mul/div accurately.
PreclaimResult preclaim(PreflightResult const &preflightResult, ServiceRegistry ®istry, OpenView const &view)
Gate a transaction based on static ledger information.
std::expected< XRPAmount, TER > calculateBaseFee(ReadView const &view, STTx const &tx)
Compute only the expected base fee for a transaction.
ApplyResult apply(ServiceRegistry ®istry, OpenView &view, STTx const &tx, ApplyFlags flags, beast::Journal journal)
Apply a transaction to an OpenView.
UInt256 TxID
A transaction identifier.
XRPAmount toDrops(FeeLevel< T > const &level, XRPAmount baseFee)
static FeeLevel64 increase(FeeLevel64 level, std::uint32_t increasePercent)
std::string transToken(TER code)
std::string to_string(BaseUInt< Bits, Tag > const &a)
static std::expected< FeeLevel64, TER > getFeeLevelPaid(ReadView const &view, STTx const &tx)
Compute the fee level that a transaction pays.
bool isTefFailure(TER x) noexcept
bool isFeeSponsored(STTx const &tx)
Whether the transaction's fee is sponsored (sfSponsor present + spfSponsorFee set).
FeeLevel< std::uint64_t > FeeLevel64
FeeLevel64 toFeeLevel(XRPAmount const &drops, XRPAmount const &baseFee)
constexpr auto kMuldivMax
XRPAmount calculateDefaultBaseFee(ReadView const &view, STTx const &tx)
Return the minimum fee that an "ordinary" transaction would pay.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
bool isTesSuccess(TER x) noexcept
TERSubset< CanCvtToTER > TER
constexpr struct xrpl::OpenLedgerT kOpenLedger
ApplyResult doApply(PreclaimResult const &preclaimResult, ServiceRegistry ®istry, OpenView &view)
Apply a prechecked transaction to an OpenView.
TxQ::Setup setupTxQ(Config const &config)
Build a TxQ::Setup object from application configuration.
bool isTemMalformed(TER x) noexcept
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
A pair of SHAMap key and LedgerEntryType.
static constexpr auto kRetrySequencePercent
static constexpr auto kMinimumEscalationMultiplier
static constexpr auto kMaximumTxnInLedger
static constexpr auto kSlowConsensusDecreasePercent
static constexpr auto kMinimumLastLedgerBuffer
static constexpr auto kMaximumTxnPerAccount
static constexpr auto kTargetTxnInLedger
static constexpr auto kMinimumQueueSize
static constexpr auto kMinimumTxnInLedger
static constexpr auto kNormalConsensusIncreasePercent
static constexpr auto kLedgersInQueue
static constexpr auto kMinimumTxnInLedgerStandalone
MultiTxn(OpenView &view, ApplyFlags flags)
Describes the results of the preflight check.
static constexpr auto kTransactionQueue
Snapshot of the externally relevant FeeMetrics fields at any given time.
std::size_t const txnsExpected
FeeLevel64 const escalationMultiplier
Structure returned by TxQ::getMetrics, expressed in reference fee level units.
std::size_t txCount
Number of transactions in the queue.
std::optional< std::size_t > txQMaxSize
Max transactions currently allowed in queue.
FeeLevel64 openLedgerFeeLevel
Minimum fee level to get into the current open ledger, bypassing the queue.
std::size_t txInLedger
Number of transactions currently in the open ledger.
FeeLevel64 minProcessingFeeLevel
Minimum fee level for a transaction to be considered for the open ledger or the queue.
FeeLevel64 referenceFeeLevel
Reference transaction fee level.
FeeLevel64 medFeeLevel
Median fee level of the last ledger.
std::size_t txPerLedger
Number of transactions expected per ledger.
Structure used to customize TxQ behavior.
bool standAlone
Use standalone mode behavior.
std::uint32_t maximumTxnPerAccount
Maximum number of transactions that can be queued by one account.
FeeLevel64 minimumEscalationMultiplier
Minimum value of the escalation multiplier, regardless of the prior ledger's median fee level.
std::optional< std::uint32_t > maximumTxnInLedger
Optional maximum allowed value of transactions per ledger before fee escalation kicks in.
std::uint32_t targetTxnInLedger
Number of transactions per ledger that fee escalation "workstowards".
std::uint32_t minimumLastLedgerBuffer
Minimum difference between the current ledger sequence and a transaction's LastLedgerSequence for the...
std::size_t ledgersInQueue
Number of ledgers' worth of transactions to allow in the queue.
std::uint32_t retrySequencePercent
Extra percentage required on the fee level of a queued transaction to replace that transaction with a...
std::uint32_t minimumTxnInLedgerSA
Like minimumTxnInLedger for standalone mode.
std::uint32_t slowConsensusDecreasePercent
When consensus takes longer than appropriate, the expected ledger size is updated to the lesser of th...
std::size_t queueSizeMin
The smallest limit the queue is allowed.
std::uint32_t minimumTxnInLedger
Minimum number of transactions to allow into the ledger before escalation, regardless of the prior le...
std::uint32_t normalConsensusIncreasePercent
When the ledger has more transactions than "expected", and performance is humming along nicely,...