xrpld
Loading...
Searching...
No Matches
TxQ.cpp
1#include <xrpld/app/misc/TxQ.h>
2
3#include <xrpld/app/ledger/OpenLedger.h>
4#include <xrpld/app/main/Application.h>
5
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>
38
39#include <boost/function/function_base.hpp>
40
41#include <algorithm>
42#include <cstddef>
43#include <cstdint>
44#include <expected>
45#include <functional>
46#include <iterator>
47#include <limits>
48#include <memory>
49#include <mutex>
50#include <numeric>
51#include <optional>
52#include <stdexcept>
53#include <string>
54#include <tuple>
55#include <utility>
56#include <vector>
57
58namespace xrpl {
59
61
66static std::expected<FeeLevel64, TER>
67getFeeLevelPaid(ReadView const& view, STTx const& tx)
68{
69 auto const computedBaseFee = calculateBaseFee(view, tx);
70 if (!computedBaseFee)
71 return std::unexpected(computedBaseFee.error());
72
73 auto const [baseFee, effectiveFeePaid] = [&view, &tx, fee = *computedBaseFee] {
74 XRPAmount const feePaid = tx[sfFee].xrp();
75
76 // If baseFee is 0 then the cost of a basic transaction is free, but we
77 // need the effective fee level to be non-zero.
78 XRPAmount const mod = [&view, &tx, fee] {
79 if (fee.signum() > 0)
80 return XRPAmount{0};
81 auto const def = calculateDefaultBaseFee(view, tx);
82 return def.signum() == 0 ? XRPAmount{1} : def;
83 }();
84 return std::pair{fee + mod, feePaid + mod};
85 }();
86
87 XRPL_ASSERT(baseFee.signum() > 0, "xrpl::getFeeLevelPaid : positive fee");
88 if (effectiveFeePaid.signum() <= 0 || baseFee.signum() <= 0)
89 {
90 return FeeLevel64(0);
91 }
92
93 return mulDiv(effectiveFeePaid, TxQ::kBaseLevel, baseFee)
95}
96
99{
100 if (!tx.isFieldPresent(sfLastLedgerSequence))
101 return std::nullopt;
102 return tx.getFieldU32(sfLastLedgerSequence);
103}
104
105static FeeLevel64
106increase(FeeLevel64 level, std::uint32_t increasePercent)
107{
108 return mulDiv(level, 100 + increasePercent, 100)
109 .value_or(static_cast<FeeLevel64>(xrpl::kMuldivMax));
110}
111
113
115{
118
119 MultiTxn(OpenView& view, ApplyFlags flags) : applyView(&view, flags), openView(&applyView)
120 {
121 }
122};
123
125{
126 using TxMapCIter = TxQ::TxQAccount::TxMap::const_iterator;
127 using TxMapIter = TxQ::TxQAccount::TxMap::iterator;
129 using AccMapIter = TxQ::AccountMap::iterator;
130
133
137 // TxQ's own journal (logs_->journal("TxQ"))
139 // Passed in by the caller forwarded to the helpers (preflight, tryDirectApply...).
142
147
151
155
156 // A set of queued transactions for this account that will be applied before the current
157 // transaction so we can make correct balance and expense calculations
159 // size of prevTxs_ range
161
162 // Current account present in TxQ byAccount_ map.
163 // Can be used only under txq_.mutex_ lock
165
166 // If current tx can replace tx that already in TxQ.
168
169 // view for updating balance in case of several queued txs for the account
171
173
174public:
176 TxQ& txq,
177 Application& app,
178 OpenView& view,
180 ApplyFlags flags,
182 : txq_(txq)
183 , app_(app)
184 , view_(view)
185 , flags_(flags)
186 , j_(txq_.j_)
187 , jExt_(j)
188 , tx_(tx)
189 , accID_(tx_->at(sfAccount))
190 , accKey_(keylet::account(accID_))
191 , txID_(tx_->getTransactionID())
192 , txSeq_(tx_->getSeqProxy())
194 {
195 }
196
198 apply();
199
200private:
203
205 applyImpl();
206
207 // Retrieve prevTxs_.
208 void
210
212 checkTicketInLedger() const;
213
215 checkIsTxBlocker() const;
216
217 void
219
221 checkFeeLevel() const;
222
224 checkBlockerInAccQue() const;
225
226 // Returns true if require Multitxn, false if not. Returns ApplyResult in case of error.
227 std::expected<bool, ApplyResult>
228 checkMultiTxn() const;
229
232
234 checkFrontTx(TxMapCIter const& prevIter, TxMapCIter const& endIter) const;
235
236 // Returns potential spend and fee of the TX
238 calcSpendAndFee() const;
239
242
244 canBeHeld() const;
245
248
249 void
250 addTxToQueue();
251};
252
255{
256 if (txSeq_.isTicket() && !view_.exists(keylet::ticket(accID_, txSeq_)))
257 {
258 if (txSeq_.value() < accSeq_.value())
259 {
260 // The ticket number is low enough that it should already be
261 // in the ledger if it were ever going to exist.
262 return ApplyResult{tefNO_TICKET, false};
263 }
264
265 // We don't queue transactions that use Tickets unless
266 // we can find the Ticket in the ledger.
267 return ApplyResult{terPRE_TICKET, false};
268 }
269
270 return {};
271}
272
273void
275{
276 if (byAccountIter_ == txq_.byAccount_.end())
277 return;
278
279 // Find the first transaction in the queue that we might apply.
280 TxQ::TxQAccount::TxMap& acctTxs = byAccountIter_->second.transactions;
281 auto const firstIter = acctTxs.lower_bound(accSeq_);
282 if (firstIter == acctTxs.end())
283 {
284 // Even though there may be transactions in the queue, there are none that we should pay
285 // attention to.
286 return;
287 }
288
289 prevTxs_ = {{firstIter, acctTxs.end()}};
290 accTxCount_ = std::distance(prevTxs_->first, prevTxs_->second);
291}
292
295{
296 if (preflightRes_.consequences.isBlocker())
297 {
298 if (accTxCount_ > 1)
299 {
300 // A blocker may not be co-resident with other transactions in
301 // the account's queue.
302 JLOG(j_.trace()) << "Rejecting blocker transaction " << txID_
303 << ". Account has other queued transactions.";
305 }
306
307 // NOLINTNEXTLINE(bugprone-unchecked-optional-access) acctTxCount == 1 implies txIter is set
308 if (accTxCount_ == 1 && (txSeq_ != prevTxs_->first->first))
309 {
310 // The blocker is not replacing the lone queued transaction.
311 JLOG(j_.trace()) << "Rejecting blocker transaction " << txID_
312 << ". Blocker does not replace lone queued transaction.";
314 }
315 }
316
317 return {};
318}
319
320void
322{
323 if (byAccountIter_ == txq_.byAccount_.end())
324 return;
325
326 auto const& txQAcct = byAccountIter_->second.transactions;
327 if (auto const existingIter = txQAcct.find(txSeq_); existingIter != txQAcct.end())
328 txToReplaceIter_ = existingIter;
329}
330
333{
334 if (!txToReplaceIter_)
335 return {};
336
337 [[maybe_unused]] auto const& [replacedSeq, replacedTx] = **txToReplaceIter_;
338
339 // We are attempting to replace a transaction in the queue.
340 //
341 // Is the current transaction's fee higher than
342 // the queued transaction's fee + a percentage
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;
348 if (feeLevelPaid_ > requiredRetryLevel)
349 {
350 // Continue, leaving the queued transaction marked for removal.
351 // DO NOT REMOVE if the new tx fails, because there may
352 // be other txs dependent on it in the queue.
353 JLOG(j_.trace()) << "Removing transaction from queue " << replacedTx.txID << " in favor of "
354 << txID_;
355 }
356 else
357 {
358 // Drop the current transaction
359 JLOG(j_.trace()) << "Ignoring transaction " << txID_ << " in favor of queued "
360 << replacedTx.txID;
361 return ApplyResult{telCAN_NOT_QUEUE_FEE, false};
362 }
363
364 return {};
365}
366
369{
370 if (accTxCount_ == 0)
371 return {};
372
373 // Allow tx to replace a blocker. Otherwise, if there's a
374 // blocker, we can't queue tx.
375 //
376 // We only need to check if txIter->first is a blocker because we
377 // require that a blocker be alone in the account's queue.
378
379 // NOLINTBEGIN(bugprone-unchecked-optional-access) acctTxCount == 1 implies txIter is set
380 auto const& seq = prevTxs_->first->first;
381 auto const& tx = prevTxs_->first->second;
382 if (accTxCount_ == 1 && tx.consequences().isBlocker() && (seq != txSeq_))
384 // NOLINTEND(bugprone-unchecked-optional-access)
385
386 // Is there a transaction for the same account with the same
387 // SeqProxy already in the queue? If so we may replace the
388 // existing entry with this new transaction.
389 if (auto const err = checkFeeLevel(); err.has_value())
390 return err;
391
392 return {};
393}
394
395std::expected<bool, ApplyResult>
397{
398 // Determine if we need a multiTxn object. Assuming the account
399 // is in the queue, there are two situations where we need to
400 // build multiTx:
401 // 1. If there are two or more transactions in the account's queue, or
402 // 2. If the account has a single queue entry, we may still need
403 // multiTxn, but only if that lone entry will not be replaced by tx.
404 bool requiresMultiTxn = false;
405 if (accTxCount_ > 1 || !txToReplaceIter_)
406 {
407 // If the transaction is queueable, create the multiTxn
408 // object to hold the info we need to adjust for prior txns.
409
410 // NOLINTBEGIN(bugprone-unchecked-optional-access)
411 TER const ter{
412 txq_.canBeHeld(
414 };
415 if (!isTesSuccess(ter))
416 return std::unexpected(ApplyResult{ter, false});
417 // NOLINTEND(bugprone-unchecked-optional-access)
418
419 requiresMultiTxn = true;
420 }
421
422 return requiresMultiTxn;
423}
424
426TxQApplyImpl::checkFrontTx(TxMapCIter const& prevIter, TxMapCIter const& endIter) const
427{
428 // Does the new transaction go to the front of the queue?
429 // This can happen if:
430 // o A transaction in the queue with a Sequence expired, or
431 // o The current first thing in the queue has a Ticket and
432 // * The tx has a Ticket that precedes it or
433 // * txSeq == acctSeq.
434 // implies txIter is set
435
436 // NOLINTBEGIN(bugprone-unchecked-optional-access)
437 XRPL_ASSERT(prevIter != endIter, "xrpl::TxQApplyImpl::checkFrontTx : not end");
438 if (prevIter == endIter || txSeq_ < prevIter->first)
439 {
440 // The first Sequence number in the queue must be the
441 // account's sequence.
442 if (txSeq_.isSeq())
443 {
444 if (txSeq_ < accSeq_)
445 return ApplyResult{tefPAST_SEQ, false};
446 if (txSeq_ > accSeq_)
447 return ApplyResult{terPRE_SEQ, false};
448 }
449 }
450 else if (!txToReplaceIter_)
451 {
452 // The current transaction is not replacing a transaction
453 // in the queue. So apparently there's a transaction in
454 // front of this one in the queue. Make sure the current
455 // transaction fits in proper sequence order with the
456 // previous transaction or is a ticket.
457 if (txSeq_.isSeq() && txq_.nextQueuableSeqImpl(accSle_, *txqLock_) != txSeq_)
458 return ApplyResult{telCAN_NOT_QUEUE, false};
459 }
460 // NOLINTEND(bugprone-unchecked-optional-access)
461
462 return {};
463}
464
467{
468 // Sum fees and spending for all of the queued transactions
469 // so we know how much to remove from the account balance
470 // for the trial preclaim.
471 XRPAmount potentialSpend = beast::kZero;
472 XRPAmount totalFee = beast::kZero;
473
474 if (!prevTxs_)
475 {
476 UNREACHABLE("xrpl::TxQApplyImpl::calcSpendAndFee !prevTxs_"); // LCOV_EXCL_LINE
477 return {potentialSpend, totalFee}; // LCOV_EXCL_LINE
478 }
479
480 for (auto iter = prevTxs_->first, end = prevTxs_->second; iter != end; ++iter)
481 {
482 [[maybe_unused]] auto const& [seq, tx] = *iter;
483
484 // If we're replacing this transaction don't include
485 // the replaced transaction's XRP spend. Otherwise add
486 // it to potentialSpend.
487 if (seq != txSeq_)
488 {
489 totalFee += tx.consequences().fee();
490 potentialSpend += tx.consequences().potentialSpend();
491 }
492 else if (std::next(iter) != end)
493 {
494 // The fee for the candidate transaction _should_ be
495 // counted if it's replacing a transaction in the middle
496 // of the queue.
497 totalFee += preflightRes_.consequences.fee();
498 potentialSpend += preflightRes_.consequences.potentialSpend();
499 }
500 }
501
502 return {potentialSpend, totalFee};
503}
504
507{
508 // There are probably other transactions in the queue for this
509 // account. Make sure the new transaction can work with the others
510 // in the queue.
511 if (accSeq_ > txSeq_)
512 return ApplyResult{tefPAST_SEQ, false};
513
514 {
515 // Check if there are previous txs that will be applied
516 auto const requiresMultiTxn = checkMultiTxn();
517 if (!requiresMultiTxn)
518 return requiresMultiTxn.error();
519 if (!*requiresMultiTxn)
520 return {};
521 }
522
523 // See if adding this entry to the queue makes sense.
524 //
525 // o Transactions with sequences should start with the
526 // account's Sequence.
527 //
528 // o Additional transactions with Sequences should
529 // follow preceding sequence-based transactions with no
530 // gaps (except for those required by TicketCreate
531 // transactions).
532
533 // Find the entry in the queue that precedes the new
534 // transaction, if one does.
535 if (!prevTxs_)
536 {
537 UNREACHABLE("xrpl::TxQApplyImpl::processAccountTxs !prevTxs_"); // LCOV_EXCL_LINE
538 return ApplyResult{tefINTERNAL, false}; // LCOV_EXCL_LINE
539 }
540 TxQ::TxQAccount const& txQAcct = byAccountIter_->second;
541 if (auto const err = checkFrontTx(txQAcct.getPrevTx(txSeq_), prevTxs_->second); err.has_value())
542 return err;
543
544 // Sum fees and spending for all of the queued transactions
545 // so we know how much to remove from the account balance
546 // for the trial preclaim.
547 auto const [potentialSpend, totalFee] = calcSpendAndFee();
548
549 /* Check if the total fees in flight are greater
550 than the account's current balance, or the
551 minimum reserve. If it is, then there's a risk
552 that the fees won't get paid, so drop this
553 transaction with a telCAN_NOT_QUEUE_BALANCE result.
554 Assume: Minimum account reserve is 20 XRP.
555 Example 1: If I have 1,000,000 XRP, I can queue
556 a transaction with a 1,000,000 XRP fee. In
557 the meantime, some other transaction may
558 lower my balance (eg. taking an offer). When
559 the transaction executes, I will either
560 spend the 1,000,000 XRP, or the transaction
561 will get stuck in the queue with a
562 `terINSUF_FEE_B`.
563 Example 2: If I have 1,000,000 XRP, and I queue
564 10 transactions with 0.1 XRP fee, I have 1 XRP
565 in flight. I can now queue another tx with a
566 999,999 XRP fee. When the first 10 execute,
567 they're guaranteed to pay their fee, because
568 nothing can eat into my reserve. The last
569 transaction, again, will either spend the
570 999,999 XRP, or get stuck in the queue.
571 Example 3: If I have 1,000,000 XRP, and I queue
572 7 transactions with 3 XRP fee, I have 21 XRP
573 in flight. I can not queue any more transactions,
574 no matter how small or large the fee.
575 Transactions stuck in the queue are mitigated by
576 LastLedgerSeq and MaybeTx::retriesRemaining.
577 */
578
579 auto const balance = (*accSle_)[sfBalance].xrp();
580 /* Get the minimum possible account reserve. If it
581 is at least 10 * the base fee, and fees exceed
582 this amount, the transaction can't be queued.
583
584 Currently typical fees are several orders
585 of magnitude smaller than any current or expected
586 future reserve. This calculation is simpler than
587 trying to figure out the potential changes to
588 the ownerCount that may occur to the account
589 as a result of these transactions, and removes
590 any need to account for other transactions that
591 may affect the owner count while these are queued.
592
593 However, in case the account reserve is on a
594 comparable scale to the base fee, ignore the
595 reserve. Only check the account balance
596 */
597 auto const reserve = view_.fees().reserve;
598 auto const base = view_.fees().base;
599 if (totalFee >= balance || (reserve > 10 * base && totalFee >= reserve))
600 {
601 // Drop the current transaction
602 JLOG(j_.trace()) << "Ignoring transaction " << txID_ << ". Total fees in flight too high.";
604 }
605
606 // Create the test view from the current view.
607 multiTxn_.emplace(view_, flags_);
608
609 auto const sleBump = multiTxn_->applyView.peek(accKey_);
610 if (!sleBump)
611 return ApplyResult{tefINTERNAL, false};
612
613 // Subtract the fees and XRP spend from all of the other
614 // transactions in the queue. That prevents a transaction
615 // inserted in the middle from fouling up later transactions.
616 auto const potentialTotalSpend =
617 totalFee + std::min(balance - std::min(balance, reserve), potentialSpend);
618 XRPL_ASSERT(
619 potentialTotalSpend > XRPAmount{0} ||
620 (potentialTotalSpend == XRPAmount{0} && multiTxn_->applyView.fees().base == 0),
621 "xrpl::TxQApplyImpl::processAccountTxs : total spend check");
622 sleBump->setFieldAmount(sfBalance, balance - potentialTotalSpend);
623 // The transaction's sequence/ticket will be valid when the other
624 // transactions in the queue have been processed. If the tx has a
625 // sequence, set the account to match it. If it has a ticket, use
626 // the next queueable sequence, which is the closest approximation
627 // to the most successful case.
628
629 // NOLINTBEGIN(bugprone-unchecked-optional-access)
630 sleBump->at(sfSequence) =
631 txSeq_.isSeq() ? txSeq_.value() : txq_.nextQueuableSeqImpl(accSle_, *txqLock_).value();
632 // NOLINTEND(bugprone-unchecked-optional-access)
633
634 return {};
635}
636
639{
640 /* Quick heuristic check to see if it's worth checking that this tx has
641 a high enough fee to clear all the txs in front of it in the queue.
642 1) Transaction is trying to get into the open ledger.
643 2) Transaction must be Sequence-based.
644 3) Must be an account already in the queue.
645 4) Must be have passed the multiTxn checks (tx is not the next
646 account seq, the skipped seqs are in the queue, the reserve
647 doesn't get exhausted, etc).
648 5) The next transaction must not have previously tried and failed
649 to apply to an open ledger.
650 6) Tx must be paying more than just the required fee level to
651 get itself into the queue.
652 7) Fee level must be escalated above the default (if it's not,
653 then the first tx _must_ have failed to process in `accept`
654 for some other reason. Tx is allowed to queue in case
655 conditions change, but don't waste the effort to clear).
656 */
657
658 if (txSeq_.isSeq() && prevTxs_ && multiTxn_ &&
659 prevTxs_->first->second.retriesRemaining == TxQ::MaybeTx::kRetriesAllowed &&
661 {
662 OpenView sandbox(kOpenLedger, &view_, view_.rules());
663
664 // NOLINTBEGIN(bugprone-unchecked-optional-access)
665 auto result = txq_.tryClearAccountQueueUpThruTx(
666 app_,
667 sandbox,
668 *tx_,
670 prevTxs_->first,
673 view_.txCount(),
674 flags_,
676 jExt_);
677 // NOLINTEND(bugprone-unchecked-optional-access)
678
679 if (result.applied)
680 {
681 sandbox.apply(view_);
682 /* Can't erase (*replacedTxIter) here because success
683 implies that it has already been deleted.
684 */
685 return result;
686 }
687 }
688
689 return {};
690}
691
694{
695 // If `multiTxn` has a value, then `canBeHeld` has already been verified
696 if (multiTxn_.has_value())
697 return {};
698
699 // NOLINTBEGIN(bugprone-unchecked-optional-access)
700 TER const ter =
702 // NOLINTEND(bugprone-unchecked-optional-access)
703
704 if (!isTesSuccess(ter))
705 {
706 // Bail, transaction cannot be held
707 JLOG(j_.trace()) << "Transaction " << txID_ << " cannot be held";
708 return ApplyResult{ter, false};
709 }
710
711 return {};
712}
713
716{
717 if (!txToReplaceIter_ && txq_.isFull())
718 {
719 auto lastRIter = txq_.byFee_.rbegin();
720 while (lastRIter != txq_.byFee_.rend() && lastRIter->account == accID_)
721 {
722 ++lastRIter;
723 }
724 if (lastRIter == txq_.byFee_.rend())
725 {
726 // The only way this condition can happen is if the entire
727 // queue is filled with transactions from this account. This
728 // is impossible with default settings - minimum queue size
729 // is 2000, and an account can only have 10 transactions
730 // queued. However, it can occur if settings are changed,
731 // and there is unit test coverage.
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.";
735 return ApplyResult{telCAN_NOT_QUEUE_FULL, false};
736 }
737 auto const& endAccount = txq_.byAccount_.at(lastRIter->account);
738 auto const endEffectiveFeeLevel = [&] {
739 // Compute the average of all the txs for the endAccount,
740 // but only if the last tx in the queue has a lower fee
741 // level than this candidate tx.
742 if (lastRIter->feeLevel > feeLevelPaid_ || endAccount.transactions.size() == 1)
743 return lastRIter->feeLevel;
744
746 auto const endTotal = std::accumulate(
747 endAccount.transactions.begin(),
748 endAccount.transactions.end(),
750 [&](auto const& total, auto const& txn) -> std::pair<FeeLevel64, FeeLevel64> {
751 // Check for overflow.
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}};
756
757 return {total.first + next, total.second + mod};
758 });
759 return endTotal.first + endTotal.second / endAccount.transactions.size();
760 }();
761
762 if (feeLevelPaid_ > endEffectiveFeeLevel)
763 {
764 // The queue is full, and this transaction is more
765 // valuable, so kick out the cheapest transaction.
766 auto const dropRIter = endAccount.transactions.rbegin();
767 XRPL_ASSERT(
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
772 << " in favor of " << txID_ << " with fee of " << feeLevelPaid_;
773 txq_.erase(txq_.byFee_.iterator_to(dropRIter->second));
774 }
775 else
776 {
777 JLOG(j_.info()) << "Queue is full, and transaction " << txID_
778 << " fee is lower than end item's account average fee";
779 return ApplyResult{telCAN_NOT_QUEUE_FULL, false};
780 }
781 }
782
783 return {};
784}
785
786void
788{
789 bool created = false;
790 if (byAccountIter_ == txq_.byAccount_.end())
791 {
792 // Create a new TxQAccount object and add the byAccount lookup.
793 std::tie(byAccountIter_, created) = txq_.byAccount_.emplace(accID_, TxQ::TxQAccount(tx_));
794 XRPL_ASSERT(created, "xrpl::TxQApplyImpl::addTxToQueue : account created");
795 }
796 // Modify the flags for use when coming out of the queue.
797 // These changes _may_ cause an extra `preflight`, but as long as
798 // the `HashRouter` still knows about the transaction, the signature
799 // will not be checked again, so the cost should be minimal.
800
801 // Don't allow soft failures, which can lead to retries
802 flags_ &= ~TapRetry;
803
804 auto& candidate =
806
807 // Then index it into the byFee lookup.
808 txq_.byFee_.insert(candidate);
809 JLOG(j_.debug()) << "Added transaction " << candidate.txID << " with result "
810 << transToken(preflightRes_.ter) << " from " << (!created ? "existing" : "new")
811 << " account " << candidate.account << " to queue."
812 << " Flags: " << flags_;
813}
814
816
819 Application& app,
820 ReadView const& view,
821 bool timeLeap,
822 TxQ::Setup const& setup)
823{
824 std::vector<FeeLevel64> feeLevels;
825 auto const txBegin = view.txs.begin();
826 auto const txEnd = view.txs.end();
827 auto const size = std::distance(txBegin, txEnd);
828 feeLevels.reserve(size);
829 std::for_each(txBegin, txEnd, [&](auto const& tx) {
830 auto const maybeFeeLevel = getFeeLevelPaid(view, *tx.first);
831 if (maybeFeeLevel.has_value())
832 {
833 feeLevels.push_back(*maybeFeeLevel);
834 }
835 else
836 {
837 // Excluded from the median sample below.
838 JLOG(j_.warn()) << "Unable to compute the fee level for a validated transaction "
839 << tx.first->getTransactionID() << " in ledger " << view.header().seq
840 << ": " << transToken(maybeFeeLevel.error());
841 }
842 });
843 std::ranges::sort(feeLevels);
844
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 "
850
851 if (timeLeap)
852 {
853 // Ledgers are taking to long to process,
854 // so clamp down on limits.
855 auto const cutPct = 100 - setup.slowConsensusDecreasePercent;
856 // upperLimit must be >= minimumTxnCount_ or std::clamp can give
857 // unexpected results
858 auto const upperLimit = std::max<std::uint64_t>(
859 mulDiv(txnsExpected_, cutPct, 100).value_or(xrpl::kMuldivMax), minimumTxnCount_);
861 mulDiv(size, cutPct, 100).value_or(xrpl::kMuldivMax), minimumTxnCount_, upperLimit);
862 recentTxnCounts_.clear();
863 }
864 else if (size > txnsExpected_ || size > targetTxnCount_)
865 {
866 recentTxnCounts_.push_back(mulDiv(size, 100 + setup.normalConsensusIncreasePercent, 100)
867 .value_or(xrpl::kMuldivMax));
869 BOOST_ASSERT(iter != recentTxnCounts_.end());
870 auto const next = [&] {
871 // Grow quickly: If the max_element is >= the
872 // current size limit, use it.
873 if (*iter >= txnsExpected_)
874 return *iter;
875 // Shrink slowly: If the max_element is < the
876 // current size limit, use a limit that is
877 // 90% of the way from max_element to the
878 // current size limit.
879 return ((txnsExpected_ * 9) + *iter) / 10;
880 }();
881 // Ledgers are processing in a timely manner,
882 // so keep the limit high, but don't let it
883 // grow without bound.
884 txnsExpected_ = std::min(next, maximumTxnCount_.value_or(next));
885 }
886
887 // The median is taken over the transactions whose fee level could be
888 // computed, while txnsExpected_ above deliberately uses the full
889 // transaction count.
890 if (feeLevels.empty())
891 {
893 }
894 else
895 {
896 // In the case of an odd number of elements, this
897 // evaluates to the middle element; for an even
898 // number of elements, it will add the two elements
899 // on either side of the "middle" and average them.
900 auto const count = feeLevels.size();
902 (feeLevels[count / 2] + feeLevels[(count - 1) / 2] + FeeLevel64{1}) / 2;
904 }
905 JLOG(j_.debug()) << "Expected transactions updated to " << txnsExpected_
906 << " and multiplier updated to " << escalationMultiplier_;
907
908 return size;
909}
910
913{
914 // Transactions in the open ledger so far
915 auto const current = view.txCount();
916
917 auto const target = snapshot.txnsExpected;
918 auto const multiplier = snapshot.escalationMultiplier;
919
920 // Once the open ledger bypasses the target,
921 // escalate the fee quickly.
922 if (current > target)
923 {
924 // Compute escalated fee level
925 // Don't care about the overflow flag
926 return mulDiv(multiplier, current * current, target * target)
927 .value_or(static_cast<FeeLevel64>(xrpl::kMuldivMax));
928 }
929
930 return kBaseLevel;
931}
932
933namespace detail {
934
935static constexpr std::pair<bool, std::uint64_t>
937{
938 // sum(n = 1->x) : n * n = x(x + 1)(2x + 1) / 6
939
940 // We expect that size_t == std::uint64_t but, just in case, guarantee
941 // we lose no bits.
942 std::uint64_t const x{xIn};
943
944 // If x is anywhere on the order of 2^^21, it's going
945 // to completely dominate the computation and is likely
946 // enough to overflow that we're just going to assume
947 // it does. If we have anywhere near 2^^21 transactions
948 // in a ledger, this is the least of our problems.
949 if (x >= (1 << 21))
951 return {true, (x * (x + 1) * ((2 * x) + 1)) / 6};
952}
953
954// Unit tests for sumOfSquares()
955static_assert(sumOfFirstSquares(1).first);
956static_assert(sumOfFirstSquares(1).second == 1);
957
958static_assert(sumOfFirstSquares(2).first);
959static_assert(sumOfFirstSquares(2).second == 5);
960
961static_assert(sumOfFirstSquares(0x1FFFFF).first);
962static_assert(sumOfFirstSquares(0x1FFFFF).second == 0x2AAAA8AAAAB00000ul);
963
964static_assert(!sumOfFirstSquares(0x200000).first);
965static_assert(sumOfFirstSquares(0x200000).second == std::numeric_limits<std::uint64_t>::max());
966
967} // namespace detail
968
971 Snapshot const& snapshot,
972 OpenView const& view,
973 std::size_t extraCount,
974 std::size_t seriesSize)
975{
976 /* Transactions in the open ledger so far.
977 AKA Transactions that will be in the open ledger when
978 the first tx in the series is attempted.
979 */
980 auto const current = view.txCount() + extraCount;
981 /* Transactions that will be in the open ledger when
982 the last tx in the series is attempted.
983 */
984 auto const last = current + seriesSize - 1;
985
986 auto const target = snapshot.txnsExpected;
987 auto const multiplier = snapshot.escalationMultiplier;
988
989 XRPL_ASSERT(
990 current > target,
991 "xrpl::TxQ::FeeMetrics::escalatedSeriesFeeLevel : current over "
992 "target");
993
994 /* Calculate (apologies for the terrible notation)
995 sum(n = current -> last) : multiplier * n * n / (target * target)
996 multiplier / (target * target) * (sum(n = current -> last) : n * n)
997 multiplier / (target * target) * ((sum(n = 1 -> last) : n * n) -
998 (sum(n = 1 -> current - 1) : n * n))
999 */
1000 auto const sumNlast = detail::sumOfFirstSquares(last);
1001 auto const sumNcurrent = detail::sumOfFirstSquares(current - 1);
1002 // because `last` is bigger, if either sum overflowed, then
1003 // `sumNlast` definitely overflowed. Also the odds of this
1004 // are nearly nil.
1005 if (!sumNlast.first)
1006 return {sumNlast.first, FeeLevel64{sumNlast.second}};
1007 auto const totalFeeLevel =
1008 mulDiv(multiplier, sumNlast.second - sumNcurrent.second, target * target);
1009
1010 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1011 return {totalFeeLevel.has_value(), *totalFeeLevel};
1012}
1013
1015
1018 TxID const& txId,
1020 ApplyFlags const flags,
1022 : txn(txn)
1024 , txID(txId)
1025 , account(txn->getAccountID(sfAccount))
1027 , seqProxy(txn->getSeqProxy())
1028 , flags(flags)
1030{
1031}
1032
1035{
1036 // If the rules or flags change, preflight again
1037 XRPL_ASSERT(pfResult, "xrpl::TxQ::MaybeTx::apply : preflight result is set");
1038
1039 // NOLINTBEGIN(bugprone-unchecked-optional-access) assert above
1040 if (pfResult->rules != view.rules() || pfResult->flags != flags)
1041 {
1042 JLOG(j.debug()) << "Queued transaction " << txID
1043 << " rules or flags have changed. Flags from " << pfResult->flags << " to "
1044 << flags;
1045
1046 pfResult.emplace(preflight(app, view.rules(), pfResult->tx, flags, pfResult->j));
1047 }
1048
1049 auto const pcresult = preclaim(*pfResult, app, view);
1050 // NOLINTEND(bugprone-unchecked-optional-access)
1051
1052 return doApply(pcresult, app, view);
1053}
1054
1056 : TxQAccount(txn->getAccountID(sfAccount))
1057{
1058}
1059
1063
1064TxQ::TxQAccount::TxMap::const_iterator
1066{
1067 // Find the entry that is greater than or equal to the new transaction,
1068 // then decrement the iterator.
1069 auto sameOrPrevIter = transactions.lower_bound(seqProx);
1070 if (sameOrPrevIter != transactions.begin())
1071 --sameOrPrevIter;
1072 return sameOrPrevIter;
1073}
1074
1077{
1078 auto const seqProx = txn.seqProxy;
1079 [[maybe_unused]] auto const* txnPtr = &txn;
1080
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");
1084
1085 return result.first->second;
1086}
1087
1088bool
1090{
1091 return transactions.erase(seqProx) != 0;
1092}
1093
1095
1097 : setup_(setup), j_(j), feeMetrics_(setup, j), maxSize_(std::nullopt)
1098{
1099}
1100
1102{
1103 byFee_.clear();
1104}
1105
1106template <size_t FillPercentage>
1107bool
1109{
1110 static_assert(FillPercentage > 0 && FillPercentage <= 100, "Invalid fill percentage");
1111 return maxSize_ && byFee_.size() >= (*maxSize_ * FillPercentage / 100);
1112}
1113
1114TER
1116 STTx const& tx,
1117 ApplyFlags const flags,
1118 OpenView const& view,
1119 SLE::ConstRef sleAccount,
1120 AccountMap::const_iterator const& accountIter,
1122 std::scoped_lock<std::mutex> const& lock) const
1123{
1124 // A Batch is never queued: its inner transactions can change the sequence
1125 // numbers of multiple accounts, which the TxQ's per-account model cannot
1126 // forecast. It must apply straight to the open ledger or not at all.
1127 if (tx.getTxnType() == ttBATCH)
1128 return telCAN_NOT_QUEUE;
1129
1130 // PreviousTxnID is deprecated and should never be used.
1131 // AccountTxnID is not supported by the transaction
1132 // queue yet, but should be added in the future.
1133 // TapFailHard transactions are never held
1134 if (tx.isFieldPresent(sfPreviousTxnID) || tx.isFieldPresent(sfAccountTxnID) ||
1135 ((flags & TapFailHard) != 0u))
1136 return telCAN_NOT_QUEUE;
1137
1138 // Disallow delegated transactions from being queued.
1139 if (tx.isFieldPresent(sfDelegate))
1140 return telCAN_NOT_QUEUE;
1141 // Disallow fee-sponsored transactions from being queued.
1142 if (isFeeSponsored(tx))
1143 return telCAN_NOT_QUEUE;
1144
1145 {
1146 // To be queued and relayed, the transaction needs to
1147 // promise to stick around for long enough that it has
1148 // a realistic chance of getting into a ledger.
1149 auto const lastValid = getLastLedgerSequence(tx);
1150 if (lastValid && *lastValid < view.header().seq + setup_.minimumLastLedgerBuffer)
1151 return telCAN_NOT_QUEUE;
1152 }
1153
1154 // Allow if the account is not in the queue at all.
1155 if (accountIter == byAccount_.end())
1156 return tesSUCCESS;
1157
1158 // Allow this tx to replace another one.
1159 if (replacementIter)
1160 return tesSUCCESS;
1161
1162 // Allow if there are fewer than the limit.
1163 TxQAccount const& txQAcct = accountIter->second;
1164 if (txQAcct.getTxnCount() < setup_.maximumTxnPerAccount)
1165 return tesSUCCESS;
1166
1167 // If we get here the queue limit is exceeded. Only allow if this
1168 // transaction fills the _first_ sequence hole for the account.
1169 auto const txSeqProx = tx.getSeqProxy();
1170 if (txSeqProx.isTicket())
1171 {
1172 // Tickets always follow sequence-based transactions, so a ticket
1173 // cannot unblock a sequence-based transaction.
1174 return telCAN_NOT_QUEUE_FULL;
1175 }
1176
1177 // This is the next queuable sequence-based SeqProxy for the account.
1178 SeqProxy const nextQueuable = nextQueuableSeqImpl(sleAccount, lock);
1179 if (txSeqProx != nextQueuable)
1180 {
1181 // The provided transaction does not fill the next open sequence gap.
1182 return telCAN_NOT_QUEUE_FULL;
1183 }
1184
1185 // Make sure they are not just topping off the account's queued
1186 // sequence-based transactions.
1187 if (auto const nextTxIter = txQAcct.transactions.upper_bound(nextQueuable);
1188 nextTxIter != txQAcct.transactions.end() && nextTxIter->first.isSeq())
1189 {
1190 // There is a next transaction and it is sequence based. They are
1191 // filling a real gap. Allow it.
1192 return tesSUCCESS;
1193 }
1194
1195 return telCAN_NOT_QUEUE_FULL;
1196}
1197
1198auto
1199TxQ::erase(TxQ::FeeMultiSet::const_iterator_type candidateIter) -> FeeMultiSet::iterator_type
1200{
1201 auto& txQAccount = byAccount_.at(candidateIter->account);
1202 auto const seqProx = candidateIter->seqProxy;
1203 auto const newCandidateIter = byFee_.erase(candidateIter);
1204 // Now that the candidate has been removed from the
1205 // intrusive list remove it from the TxQAccount
1206 // so the memory can be freed.
1207 [[maybe_unused]] auto const found = txQAccount.remove(seqProx);
1208 XRPL_ASSERT(found, "xrpl::TxQ::erase : account removed");
1209
1210 return newCandidateIter;
1211}
1212
1213auto
1214TxQ::eraseAndAdvance(TxQ::FeeMultiSet::const_iterator_type candidateIter)
1215 -> FeeMultiSet::iterator_type
1216{
1217 auto& txQAccount = byAccount_.at(candidateIter->account);
1218 auto const accountIter = txQAccount.transactions.find(candidateIter->seqProxy);
1219 XRPL_ASSERT(
1220 accountIter != txQAccount.transactions.end(), "xrpl::TxQ::eraseAndAdvance : account found");
1221
1222 // Note that sequence-based transactions must be applied in sequence order
1223 // from smallest to largest. But ticket-based transactions can be
1224 // applied in any order.
1225 XRPL_ASSERT(
1226 candidateIter->seqProxy.isTicket() || accountIter == txQAccount.transactions.begin(),
1227 "xrpl::TxQ::eraseAndAdvance : ticket or sequence");
1228 XRPL_ASSERT(
1229 byFee_.iterator_to(accountIter->second) == candidateIter,
1230 "xrpl::TxQ::eraseAndAdvance : found in byFee");
1231 auto const accountNextIter = std::next(accountIter);
1232
1233 // Check if the next transaction for this account is earlier in the queue,
1234 // which means we skipped it earlier, and need to try it again.
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));
1239
1240 auto const candidateNextIter = byFee_.erase(candidateIter);
1241 txQAccount.transactions.erase(accountIter);
1242
1243 return useAccountNext ? byFee_.iterator_to(accountNextIter->second) : candidateNextIter;
1244}
1245
1246auto
1248 TxQ::TxQAccount& txQAccount,
1249 TxQ::TxQAccount::TxMap::const_iterator begin,
1250 TxQ::TxQAccount::TxMap::const_iterator end) -> TxQAccount::TxMap::iterator
1251{
1252 for (auto it = begin; it != end; ++it)
1253 {
1254 byFee_.erase(byFee_.iterator_to(it->second));
1255 }
1256 return txQAccount.transactions.erase(begin, end);
1257}
1258
1261 Application& app,
1262 OpenView& view,
1263 STTx const& tx,
1264 TxQ::AccountMap::iterator const& accountIter,
1265 TxQAccount::TxMap::iterator const& beginTxIter,
1266 FeeLevel64 feeLevelPaid,
1267 PreflightResult const& pfResult,
1268 std::size_t const txExtraCount,
1269 ApplyFlags flags,
1270 FeeMetrics::Snapshot const& metricsSnapshot,
1272{
1273 SeqProxy const tSeqProx{tx.getSeqProxy()};
1274 XRPL_ASSERT(
1275 beginTxIter != accountIter->second.transactions.end(),
1276 "xrpl::TxQ::tryClearAccountQueueUpThruTx : non-empty accounts input");
1277
1278 // This check is only concerned with the range from
1279 // [aSeqProxy, tSeqProxy)
1280 auto endTxIter = accountIter->second.transactions.lower_bound(tSeqProx);
1281 auto const dist = std::distance(beginTxIter, endTxIter);
1282
1283 auto const requiredTotalFeeLevel =
1284 FeeMetrics::escalatedSeriesFeeLevel(metricsSnapshot, view, txExtraCount, dist + 1);
1285 // If the computation for the total manages to overflow (however extremely
1286 // unlikely), then there's no way we can confidently verify if the queue
1287 // can be cleared.
1288 if (!requiredTotalFeeLevel.first)
1289 return {telINSUF_FEE_P, false};
1290
1291 auto const totalFeeLevelPaid = std::accumulate(
1292 beginTxIter, endTxIter, feeLevelPaid, [](auto const& total, auto const& txn) {
1293 return total + txn.second.feeLevel;
1294 });
1295
1296 // This transaction did not pay enough, so fall back to the normal process.
1297 if (totalFeeLevelPaid < requiredTotalFeeLevel.second)
1298 return {telINSUF_FEE_P, false};
1299
1300 // This transaction paid enough to clear out the queue.
1301 // Attempt to apply the queued transactions.
1302 for (auto it = beginTxIter; it != endTxIter; ++it)
1303 {
1304 auto const txResult = it->second.apply(app, view, j);
1305 // Succeed or fail, use up a retry, because if the overall
1306 // process fails, we want the attempt to count. If it all
1307 // succeeds, the MaybeTx will be destructed, so it'll be
1308 // moot.
1309 --it->second.retriesRemaining;
1310 it->second.lastResult = txResult.ter;
1311
1312 // In TxQ::apply we note that it's possible for a transaction with
1313 // a ticket to both be in the queue and in the ledger. And, while
1314 // we're in TxQ::apply, it's too expensive to filter those out.
1315 //
1316 // So here in tryClearAccountQueueUpThruTx we just received a batch of
1317 // queued transactions. And occasionally one of those is a ticketed
1318 // transaction that is both in the queue and in the ledger. When
1319 // that happens the queued transaction returns tefNO_TICKET.
1320 //
1321 // The transaction that returned tefNO_TICKET can never succeed
1322 // and we'd like to get it out of the queue as soon as possible.
1323 // The easiest way to do that from here is to treat the transaction
1324 // as though it succeeded and attempt to clear the remaining
1325 // transactions in the account queue. Then, if clearing the account
1326 // is successful, we will have removed any ticketed transactions
1327 // that can never succeed.
1328 if (txResult.ter == tefNO_TICKET)
1329 continue;
1330
1331 if (!txResult.applied)
1332 {
1333 // Transaction failed to apply. Fall back to the normal process.
1334 return {txResult.ter, false};
1335 }
1336 }
1337 // Apply the current tx. Because the state of the view has been changed
1338 // by the queued txs, we also need to preclaim again.
1339 auto const txResult = doApply(preclaim(pfResult, app, view), app, view);
1340
1341 if (txResult.applied)
1342 {
1343 // All of the queued transactions applied, so remove them from the
1344 // queue.
1345 endTxIter = erase(accountIter->second, beginTxIter, endTxIter);
1346 // If `tx` is replacing a queued tx, delete that one, too.
1347 if (endTxIter != accountIter->second.transactions.end() && endTxIter->first == tSeqProx)
1348 erase(accountIter->second, endTxIter, std::next(endTxIter));
1349 }
1350
1351 return txResult;
1352}
1353
1354// Overview of considerations for when a transaction is accepted into the TxQ:
1355//
1356// These rules apply to the transactions in the queue owned by a single
1357// account. Briefly, the primary considerations are:
1358//
1359// 1. Is the new transaction blocking?
1360// 2. Is there an expiration gap in the account's sequence-based transactions?
1361// 3. Does the new transaction replace one that is already in the TxQ?
1362// 4. Is the transaction's sequence or ticket value acceptable for this account?
1363// 5. Is the transaction likely to claim a fee?
1364// 6. Is the queue full?
1365//
1366// Here are more details.
1367//
1368// 1. A blocking transaction is one that would change the validity of following
1369// transactions for the issuing account. Examples of blocking transactions
1370// include SetRegularKey and SignerListSet.
1371//
1372// A blocking transaction can only be added to the queue for an account if:
1373//
1374// a. The queue for that account is empty, or
1375//
1376// b. The blocking transaction replaces the only transaction in the
1377// account's queue.
1378//
1379// While a blocker is in the account's queue no additional transactions
1380// can be added to the queue.
1381//
1382// As a consequence, any blocker is always alone in the account's queue.
1383//
1384// 2. Transactions are given unique identifiers using either Sequence numbers
1385// or Tickets. In general, sequence numbers in the queue are expected to
1386// start with the account root sequence and increment from there. There
1387// are two exceptions:
1388//
1389// a. Sequence holes left by ticket creation. If a transaction creates
1390// more than one ticket, then the account sequence number will jump
1391// by the number of tickets created. These holes are fine.
1392//
1393// b. Sequence gaps left by transaction expiration. If transactions stay
1394// in the queue long enough they may expire. If that happens it leaves
1395// gaps in the sequence numbers held by the queue. These gaps are
1396// important because, if left in place, they will block any later
1397// sequence-based transactions in the queue from working. Remember,
1398// for any given account sequence numbers must be used consecutively
1399// (with the exception of ticket-induced holes).
1400//
1401// 3. Transactions in the queue may be replaced. If a transaction in the
1402// queue has the same SeqProxy as the incoming transaction, then the
1403// transaction in the queue will be replaced if the following conditions
1404// are met:
1405//
1406// a. The replacement must provide a fee that is at least 1.25 times the
1407// fee of the transaction it is replacing.
1408//
1409// b. If the transaction being replaced has a sequence number, then
1410// the transaction may not be after any expiration-based sequence
1411// gaps in the account's queue.
1412//
1413// c. A replacement that is a blocker is only allowed if the transaction
1414// it replaces is the only transaction in the account's queue.
1415//
1416// 4. The transaction that is not a replacement must have an acceptable
1417// sequence or ticket ID:
1418//
1419// Sequence: For a given account's queue configuration there is at most
1420// one sequence number that is acceptable to the queue for that account.
1421// The rules are:
1422//
1423// a. If there are no sequence-based transactions in the queue and the
1424// candidate transaction has a sequence number, that value must match
1425// the account root's sequence.
1426//
1427// b. If there are sequence-based transactions in the queue for that
1428// account and there are no expiration-based gaps, then the candidate's
1429// sequence number must belong at the end of the list of sequences.
1430//
1431// c. If there are expiration-based gaps in the sequence-based
1432// transactions in the account's queue, then the candidate's sequence
1433// value must go precisely at the front of the first gap.
1434//
1435// Ticket: If there are no blockers or sequence gaps in the account's
1436// queue, then there are many tickets that are acceptable to the queue
1437// for that account. The rules are:
1438//
1439// a. If there are no blockers in the account's queue and the ticket
1440// required by the transaction is in the ledger then the transaction
1441// may be added to the account's queue.
1442//
1443// b. If there is a ticket-based blocker in the account's queue then
1444// that blocker can be replaced.
1445//
1446// Note that it is not sufficient for the transaction that would create
1447// the necessary ticket to be in the account's queue. The required ticket
1448// must already be in the ledger. This avoids problems that can occur if
1449// a ticket-creating transaction enters the queue but expires out of the
1450// queue before its tickets are created.
1451//
1452// 5. The transaction must be likely to claim a fee. In general that is
1453// checked by having preclaim return a tes or tec code.
1454//
1455// Extra work is done here to account for funds that other transactions
1456// in the queue remove from the account.
1457//
1458// 6. The queue must not be full.
1459//
1460// a. Each account can queue up to a maximum of 10 transactions. Beyond
1461// that transactions are rejected. There is an exception for this case
1462// when filling expiration-based sequence gaps.
1463//
1464// b. The entire queue also has a (dynamic) maximum size. Transactions
1465// beyond that limit are rejected.
1466//
1467
1470 Application& app,
1471 OpenView& view,
1473 ApplyFlags flags,
1475{
1476 TxQApplyImpl impl(*this, app, view, tx, flags, j);
1477 return impl.apply();
1478}
1479
1482{
1483 // See if the transaction is valid, properly formed, etc.
1484 // Before doing potentially expensive queue replace and multi-transaction operations.
1485 if (!isTesSuccess(preflightRes_.ter))
1486 return {preflightRes_.ter, false};
1487
1488 if (auto const err = applyImplPrelock(); err.has_value())
1489 return *err;
1490
1491 std::scoped_lock const lock(txq_.mutex_);
1492 txqLock_ = lock;
1493
1494 return applyImpl();
1495}
1496
1499{
1500 // See if the transaction paid a high enough fee that it can go straight
1501 // into the ledger.
1502 if (auto const directApplied = txq_.tryDirectApply(app_, view_, tx_, flags_, jExt_))
1503 return directApplied;
1504
1505 if ((flags_ & TapDryRun) != 0u)
1506 return ApplyResult{telCAN_NOT_QUEUE, false};
1507
1508 // If we get past tryDirectApply() without returning then we expect
1509 // one of the following to occur:
1510 //
1511 // o We will decide the transaction is unlikely to claim a fee.
1512 // o The transaction paid a high enough fee that fee averaging will apply.
1513 // o The transaction will be queued.
1514
1515 // If the account is not currently in the ledger, don't queue its tx.
1516 accSle_ = view_.read(accKey_);
1517 if (!accSle_)
1518 return ApplyResult{terNO_ACCOUNT, false};
1519 accSeq_ = SeqProxy::rawSequence(accSle_->at(sfSequence));
1520
1521 // If the transaction needs a Ticket is that Ticket in the ledger?
1522 if (auto const err = checkTicketInLedger(); err.has_value())
1523 return err;
1524
1525 return {};
1526}
1527
1530{
1531 byAccountIter_ = txq_.byAccount_.find(accID_);
1532
1533 // _If_ the account is in the queue, then ignore any sequence-based
1534 // queued transactions that slipped into the ledger while we were not
1535 // watching. This does actually happen in the wild, but it's uncommon.
1536 //
1537 // Note that we _don't_ ignore queued ticket-based transactions that
1538 // slipped into the ledger while we were not watching. It would be
1539 // desirable to do so, but the measured cost was too high since we have
1540 // to individually check each queued ticket against the ledger.
1542
1543 // Is tx a blocker? If so there are very limited conditions when it
1544 // is allowed in the TxQ:
1545 // 1. If the account's queue is empty or
1546 // 2. If the blocker replaces the only entry in the account's queue.
1547 if (auto const err = checkIsTxBlocker(); err.has_value())
1548 return *err;
1549
1550 // If the transaction is intending to replace a transaction in the queue
1551 // identify the one that might be replaced.
1553
1554 // We may need the base fee for multiple transactions or transaction
1555 // replacement, so just pull it up now.
1556 metricsSnapshot_.emplace(txq_.feeMetrics_.getSnapshot());
1557 auto const computedFeeLevelPaid = getFeeLevelPaid(view_, *tx_);
1558 // Without a fee level there is no way to tell whether the transaction
1559 // pays enough, so it can be neither applied nor queued.
1560 if (!computedFeeLevelPaid.has_value())
1561 return {computedFeeLevelPaid.error(), false};
1562 feeLevelPaid_ = *computedFeeLevelPaid;
1563 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1564 requiredFeeLevel_ = txq_.getRequiredFeeLevel(view_, flags_, *metricsSnapshot_, *txqLock_);
1565
1566 // Is there a blocker already in the account's queue? If so, don't
1567 // allow additional transactions in the queue.
1568 if (auto const err = checkBlockerInAccQue(); err.has_value())
1569 return *err;
1570
1571 if (accTxCount_ == 0)
1572 {
1573 // There are no queued transactions for this account. If the
1574 // transaction has a sequence make sure it's valid (tickets
1575 // are checked elsewhere).
1576 if (txSeq_.isSeq())
1577 {
1578 if (accSeq_ > txSeq_)
1579 return {tefPAST_SEQ, false};
1580 if (accSeq_ < txSeq_)
1581 return {terPRE_SEQ, false};
1582 }
1583 }
1584 else
1585 {
1586 // Calculate expenses and fees
1587 // Create multiTxn_ test view from the current view and apply balance and accSeq to it,
1588 // preclaim(...) will use it.
1589 if (auto const err = processAccountTxs(); err.has_value())
1590 return *err;
1591 }
1592
1593 // See if the transaction is likely to claim a fee.
1594 //
1595 // We assume that if the transaction survives preclaim(), then it
1596 // is likely to claim a fee. However we can't allow preclaim to
1597 // check the sequence/ticket. Transactions in the queue may be
1598 // responsible for increasing the sequence, and mocking those up
1599 // is non-trivially expensive.
1600 //
1601 // Note that earlier code has already verified that the sequence/ticket
1602 // is valid. So we use a special entry point that runs all of the
1603 // preclaim checks with the exception of the sequence check.
1604 auto const pcresult = preclaim(preflightRes_, app_, multiTxn_ ? multiTxn_->openView : view_);
1605 if (!pcresult.likelyToClaimFee)
1606 return {pcresult.ter, false};
1607
1608 // Too low of a fee should get caught by preclaim
1609 XRPL_ASSERT(feeLevelPaid_ >= TxQ::kBaseLevel, "xrpl::TxQApplyImpl::applyImpl : minimum fee");
1610
1611 JLOG(j_.trace()) << "Transaction " << txID_ << " from account " << accID_
1612 << " has fee level of " << feeLevelPaid_ << " needs at least "
1613 << requiredFeeLevel_ << " to get in the open ledger, which has "
1614 << view_.txCount() << " entries.";
1615
1616 // Quick heuristic check to see if it's worth checking that this tx has a high enough fee to
1617 // clear all the txs in front of it in the queue.
1618 if (auto const res = tryClearAccountQueueUpThruTx(); res.has_value())
1619 return *res;
1620
1621 if (auto const err = canBeHeld(); err.has_value())
1622 return *err;
1623
1624 // If the queue is full, decide whether to drop the current
1625 // transaction or the last transaction for the account with
1626 // the lowest fee.
1627 if (auto const err = processQueueIsFull(); err.has_value())
1628 return *err;
1629
1630 // Remove existing tx (will be replaced by new in addTxToQueue).
1631 if (txToReplaceIter_)
1632 {
1633 txq_.removeFromByFee(txToReplaceIter_, tx_);
1634 txToReplaceIter_.reset();
1635 }
1636
1637 addTxToQueue();
1638
1639 return {terQUEUED, false};
1640}
1641
1642/*
1643 1. Update the fee metrics based on the fee levels of the
1644 txs in the validated ledger and whether consensus is
1645 slow.
1646 2. Adjust the maximum queue size to be enough to hold
1647 `ledgersInQueue` ledgers.
1648 3. Remove any transactions from the queue for which the
1649 `LastLedgerSequence` has passed.
1650 4. Remove any account objects that have no candidates
1651 under them.
1652
1653*/
1654void
1655TxQ::processClosedLedger(Application& app, ReadView const& view, bool timeLeap)
1656{
1657 std::scoped_lock const lock(mutex_);
1658
1659 feeMetrics_.update(app, view, timeLeap, setup_);
1660 auto const& snapshot = feeMetrics_.getSnapshot();
1661
1662 auto const ledgerSeq = view.header().seq;
1663
1664 if (!timeLeap)
1665 maxSize_ = std::max(snapshot.txnsExpected * setup_.ledgersInQueue, setup_.queueSizeMin);
1666
1667 // Remove any queued candidates whose LastLedgerSequence has gone by.
1668 for (auto candidateIter = byFee_.begin(); candidateIter != byFee_.end();)
1669 {
1670 if (candidateIter->lastValid && *candidateIter->lastValid <= ledgerSeq)
1671 {
1672 byAccount_.at(candidateIter->account).dropPenalty = true;
1673 candidateIter = erase(candidateIter);
1674 }
1675 else
1676 {
1677 ++candidateIter;
1678 }
1679 }
1680
1681 // Remove any TxQAccounts that don't have candidates
1682 // under them
1683 for (auto txQAccountIter = byAccount_.begin(); txQAccountIter != byAccount_.end();)
1684 {
1685 if (txQAccountIter->second.empty())
1686 {
1687 txQAccountIter = byAccount_.erase(txQAccountIter);
1688 }
1689 else
1690 {
1691 ++txQAccountIter;
1692 }
1693 }
1694}
1695
1696void
1698{
1699 LedgerHash const& parentHash = view.header().parentHash;
1700 if (parentHash == parentHash_)
1701 {
1702 JLOG(j_.warn()) << "Parent ledger hash unchanged from " << parentHash;
1703 }
1704 else
1705 {
1706 parentHash_ = parentHash;
1707 }
1708
1709 [[maybe_unused]] auto const startingSize = byFee_.size();
1710 // byFee_ doesn't "own" the candidate objects inside it, so it's
1711 // perfectly safe to wipe it and start over, repopulating from
1712 // byAccount_.
1713 //
1714 // In the absence of a "re-sort the list in place" function, this
1715 // was the fastest method tried to repopulate the list.
1716 // Other methods included: create a new list and moving items over one at a
1717 // time, create a new list and merge the old list into it.
1718 byFee_.clear();
1719
1720 MaybeTx::parentHashComp = parentHash;
1721
1722 for (auto& [_, account] : byAccount_)
1723 {
1724 for (auto& [_, candidate] : account.transactions)
1725 {
1726 byFee_.insert(candidate);
1727 }
1728 }
1729 XRPL_ASSERT(byFee_.size() == startingSize, "xrpl::TxQ::rebuildQueue : byFee size match");
1730}
1731
1732/*
1733 How the txs are moved from the queue to the new open ledger.
1734
1735 1. Iterate over the txs from highest fee level to lowest.
1736 For each tx:
1737 a) Is this the first tx in the queue for this account?
1738 No: Skip this tx. We'll come back to it later.
1739 Yes: Continue to the next sub-step.
1740 b) Is the tx fee level less than the current required
1741 fee level?
1742 Yes: Stop iterating. Continue to the next step.
1743 No: Try to apply the transaction. Did it apply?
1744 Yes: Take it out of the queue. Continue with
1745 the next appropriate candidate (see below).
1746 No: Did it get a tef, tem, or tel, or has it
1747 retried `MaybeTx::retriesAllowed`
1748 times already?
1749 Yes: Take it out of the queue. Continue
1750 with the next appropriate candidate
1751 (see below).
1752 No: Leave it in the queue, track the retries,
1753 and continue iterating.
1754 2. Return indicator of whether the open ledger was modified.
1755
1756 "Appropriate candidate" is defined as the tx that has the
1757 highest fee level of:
1758 * the tx for the current account with the next sequence.
1759 * the next tx in the queue, simply ordered by fee.
1760*/
1761bool
1763{
1764 /* Move transactions from the queue from largest fee level to smallest.
1765 As we add more transactions, the required fee level will increase.
1766 Stop when the transaction fee level gets lower than the required fee
1767 level.
1768 */
1769
1770 bool ledgerChanged = false;
1771
1772 std::scoped_lock const lock(mutex_);
1773
1774 auto const metricsSnapshot = feeMetrics_.getSnapshot();
1775
1776 for (auto candidateIter = byFee_.begin(); candidateIter != byFee_.end();)
1777 {
1778 auto& account = byAccount_.at(candidateIter->account);
1779 auto const beginIter = account.transactions.begin();
1780 if (candidateIter->seqProxy.isSeq() && candidateIter->seqProxy > beginIter->first)
1781 {
1782 // There is a sequence transaction at the front of the queue and
1783 // candidate has a later sequence, so skip this candidate. We
1784 // need to process sequence-based transactions in sequence order.
1785 JLOG(j_.trace()) << "Skipping queued transaction " << candidateIter->txID
1786 << " from account " << candidateIter->account
1787 << " as it is not the first.";
1788 candidateIter++;
1789 continue;
1790 }
1791 auto const requiredFeeLevel = getRequiredFeeLevel(view, TapNone, metricsSnapshot, lock);
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)
1797 {
1798 JLOG(j_.trace()) << "Applying queued transaction " << candidateIter->txID
1799 << " to open ledger.";
1800
1801 auto const [txnResult, didApply, metadata] = candidateIter->apply(app, view, j_);
1802
1803 if (didApply)
1804 {
1805 // Remove the candidate from the queue
1806 JLOG(j_.debug()) << "Queued transaction " << candidateIter->txID
1807 << " applied successfully with " << transToken(txnResult)
1808 << ". Remove from queue.";
1809
1810 candidateIter = eraseAndAdvance(candidateIter);
1811 ledgerChanged = true;
1812 }
1813 else if (
1814 isTefFailure(txnResult) || isTemMalformed(txnResult) ||
1815 candidateIter->retriesRemaining <= 0)
1816 {
1817 if (candidateIter->retriesRemaining <= 0)
1818 {
1819 account.retryPenalty = true;
1820 }
1821 else
1822 {
1823 account.dropPenalty = true;
1824 }
1825 JLOG(j_.debug()) << "Queued transaction " << candidateIter->txID << " failed with "
1826 << transToken(txnResult) << ". Remove from queue.";
1827 candidateIter = eraseAndAdvance(candidateIter);
1828 }
1829 else
1830 {
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)
1835 {
1836 candidateIter->retriesRemaining = 1;
1837 }
1838 else
1839 {
1840 --candidateIter->retriesRemaining;
1841 }
1842 candidateIter->lastResult = txnResult;
1843 if (account.dropPenalty && account.transactions.size() > 1 && isFull<95>())
1844 {
1845 // The queue is close to full, this account has multiple
1846 // txs queued, and this account has had a transaction
1847 // fail.
1848 if (candidateIter->seqProxy.isTicket())
1849 {
1850 // Since the failed transaction has a ticket, order
1851 // doesn't matter. Drop this one.
1852 JLOG(j_.info())
1853 << "Queue is nearly full, and transaction " << candidateIter->txID
1854 << " failed with " << transToken(txnResult)
1855 << ". Removing ticketed tx from account " << account.account;
1856 candidateIter = eraseAndAdvance(candidateIter);
1857 }
1858 else
1859 {
1860 // Even though we're giving this transaction another
1861 // chance, chances are it won't recover. To avoid
1862 // making things worse, drop the _last_ transaction for
1863 // this account.
1864 auto const dropRIter = account.transactions.rbegin();
1865 XRPL_ASSERT(
1866 dropRIter->second.account == candidateIter->account,
1867 "xrpl::TxQ::accept : account check");
1868
1869 JLOG(j_.info())
1870 << "Queue is nearly full, and transaction " << candidateIter->txID
1871 << " failed with " << transToken(txnResult)
1872 << ". Removing last item from account " << account.account;
1873 auto const endIter = byFee_.iterator_to(dropRIter->second);
1874 if (endIter != candidateIter)
1875 erase(endIter);
1876 ++candidateIter;
1877 }
1878 }
1879 else
1880 {
1881 ++candidateIter;
1882 }
1883 }
1884 }
1885 else
1886 {
1887 break;
1888 }
1889 }
1890
1891 // All transactions that can be moved out of the queue into the open
1892 // ledger have been. Rebuild the queue using the open ledger's
1893 // parent hash, so that transactions paying the same fee are
1894 // reordered.
1895 rebuildQueue(view);
1896
1897 return ledgerChanged;
1898}
1899
1900// Public entry point for nextQueuableSeq().
1901//
1902// Acquires a lock and calls the implementation.
1905{
1906 std::scoped_lock const lock(mutex_);
1907 return nextQueuableSeqImpl(sleAccount, lock);
1908}
1909
1910// The goal is to return a SeqProxy for a sequence that will fill the next
1911// available hole in the queue for the passed in account.
1912//
1913// If there are queued transactions for the account then the first viable
1914// sequence number, that is not used by a transaction in the queue, must
1915// be found and returned.
1918{
1919 // If the account is not in the ledger or a non-account was passed
1920 // then return zero. We have no idea.
1921 if (!sleAccount || sleAccount->getType() != ltACCOUNT_ROOT)
1922 return SeqProxy::rawSequence(0);
1923
1924 SeqProxy const acctSeqProx = SeqProxy::rawSequence((*sleAccount)[sfSequence]);
1925
1926 // If the account is not in the queue then acctSeqProx is good enough.
1927 auto const accountIter = byAccount_.find((*sleAccount)[sfAccount]);
1928 if (accountIter == byAccount_.end() || accountIter->second.transactions.empty())
1929 return acctSeqProx;
1930
1931 TxQAccount::TxMap const& acctTxs = accountIter->second.transactions;
1932
1933 // Ignore any sequence-based queued transactions that slipped into the
1934 // ledger while we were not watching. This does actually happen in the
1935 // wild, but it's uncommon.
1936 auto txIter = acctTxs.lower_bound(acctSeqProx);
1937
1938 if (txIter == acctTxs.end() || !txIter->first.isSeq() || txIter->first != acctSeqProx)
1939 {
1940 // Either...
1941 // o There are no queued sequence-based transactions equal to or
1942 // following acctSeqProx or
1943 // o acctSeqProx is not currently in the queue.
1944 // So acctSeqProx is as good as it gets.
1945 return acctSeqProx;
1946 }
1947
1948 // There are sequence-based transactions queued that follow acctSeqProx.
1949 // Locate the first opening to put a transaction into.
1950 SeqProxy attempt = txIter->second.consequences().followingSeq();
1951 while (++txIter != acctTxs.cend())
1952 {
1953 if (attempt < txIter->first)
1954 break;
1955
1956 attempt = txIter->second.consequences().followingSeq();
1957 }
1958 return attempt;
1959}
1960
1963 OpenView& view,
1964 ApplyFlags flags,
1965 FeeMetrics::Snapshot const& metricsSnapshot,
1966 std::scoped_lock<std::mutex> const& lock)
1967{
1968 return FeeMetrics::scaleFeeLevel(metricsSnapshot, view);
1969}
1970
1973 Application& app,
1974 OpenView& view,
1976 ApplyFlags flags,
1978{
1979 auto const account = (*tx)[sfAccount];
1980 auto const sleAccount = view.read(keylet::account(account));
1981
1982 // Don't attempt to direct apply if the account is not in the ledger.
1983 if (!sleAccount)
1984 return {};
1985
1986 SeqProxy const acctSeqProx = SeqProxy::rawSequence((*sleAccount)[sfSequence]);
1987 SeqProxy const txSeqProx = tx->getSeqProxy();
1988
1989 // Can only directly apply if the transaction sequence matches the account
1990 // sequence or if the transaction uses a ticket.
1991 if (txSeqProx.isSeq() && txSeqProx != acctSeqProx)
1992 return {};
1993
1994 FeeLevel64 const requiredFeeLevel = [this, &view, flags] {
1995 std::scoped_lock const lock(mutex_);
1996 return getRequiredFeeLevel(view, flags, feeMetrics_.getSnapshot(), lock);
1997 }();
1998
1999 // If the transaction's fee is high enough we may be able to put the
2000 // transaction straight into the ledger.
2001 auto const computedFeeLevelPaid = getFeeLevelPaid(view, *tx);
2002 // The fee level is unknown, so the transaction cannot be applied here,
2003 // and queueing it would only run into the same failure. Reject it.
2004 if (!computedFeeLevelPaid.has_value())
2005 {
2006 return ApplyResult{computedFeeLevelPaid.error(), false};
2007 }
2008 FeeLevel64 const feeLevelPaid = *computedFeeLevelPaid;
2009
2010 if (feeLevelPaid >= requiredFeeLevel)
2011 {
2012 // Attempt to apply the transaction directly.
2013 auto const transactionID = tx->getTransactionID();
2014 JLOG(j_.trace()) << "Applying transaction " << transactionID << " to open ledger.";
2015
2016 auto const [txnResult, didApply, metadata] = xrpl::apply(app, view, *tx, flags, j);
2017
2018 JLOG(j_.trace()) << "New transaction " << transactionID
2019 << (didApply ? " applied successfully with " : " failed with ")
2020 << transToken(txnResult);
2021
2022 if (didApply)
2023 {
2024 // If the applied transaction replaced a transaction in the
2025 // queue then remove the replaced transaction.
2026 std::scoped_lock const lock(mutex_);
2027
2028 auto const accountIter = byAccount_.find(account);
2029 if (accountIter != byAccount_.end())
2030 {
2031 TxQAccount& txQAcct = accountIter->second;
2032 if (auto const existingIter = txQAcct.transactions.find(txSeqProx);
2033 existingIter != txQAcct.transactions.end())
2034 {
2035 removeFromByFee(existingIter, tx);
2036 }
2037 }
2038 }
2039 return ApplyResult{txnResult, didApply, metadata};
2040 }
2041 return {};
2042}
2043
2044void
2048{
2049 if (replacedTxIter && tx)
2050 {
2051 // If the transaction we're holding replaces a transaction in the
2052 // queue, remove the transaction that is being replaced.
2053 auto const deleteIter = byFee_.iterator_to((*replacedTxIter)->second);
2054 XRPL_ASSERT(deleteIter != byFee_.end(), "xrpl::TxQ::removeFromByFee : found in byFee");
2055 XRPL_ASSERT(
2056 &(*replacedTxIter)->second == &*deleteIter,
2057 "xrpl::TxQ::removeFromByFee : matching transaction");
2058 XRPL_ASSERT(
2059 deleteIter->seqProxy == tx->getSeqProxy(),
2060 "xrpl::TxQ::removeFromByFee : matching sequence");
2061 XRPL_ASSERT(
2062 deleteIter->account == (*tx)[sfAccount],
2063 "xrpl::TxQ::removeFromByFee : matching account");
2064
2065 erase(deleteIter);
2066 }
2067}
2068
2070TxQ::getMetrics(OpenView const& view) const
2071{
2072 Metrics result;
2073
2074 std::scoped_lock const lock(mutex_);
2075
2076 auto const snapshot = feeMetrics_.getSnapshot();
2077
2078 result.txCount = byFee_.size();
2079 result.txQMaxSize = maxSize_;
2080 result.txInLedger = view.txCount();
2081 result.txPerLedger = snapshot.txnsExpected;
2083 result.minProcessingFeeLevel =
2084 isFull() ? byFee_.rbegin()->feeLevel + FeeLevel64{1} : kBaseLevel;
2085 result.medFeeLevel = snapshot.escalationMultiplier;
2086 result.openLedgerFeeLevel = FeeMetrics::scaleFeeLevel(snapshot, view);
2087
2088 return result;
2089}
2090
2091std::expected<TxQ::FeeAndSeq, TER>
2093{
2094 auto const account = (*tx)[sfAccount];
2095
2096 std::scoped_lock const lock(mutex_);
2097
2098 auto const snapshot = feeMetrics_.getSnapshot();
2099 auto const maybeBaseFee = calculateBaseFee(view, *tx);
2100 if (!maybeBaseFee.has_value())
2101 {
2102 return std::unexpected(maybeBaseFee.error());
2103 }
2104 auto const baseFee = *maybeBaseFee;
2105 auto const fee = FeeMetrics::scaleFeeLevel(snapshot, view);
2106
2107 auto const sle = view.read(keylet::account(account));
2108
2109 std::uint32_t const accountSeq = sle ? (*sle)[sfSequence] : 0;
2110 std::uint32_t const availableSeq = nextQueuableSeqImpl(sle, lock).value();
2111 return FeeAndSeq{
2112 .fee = mulDiv(fee, baseFee, kBaseLevel)
2114 .accountSeq = accountSeq,
2115 .availableSeq = availableSeq,
2116 };
2117}
2118
2120TxQ::getAccountTxs(AccountID const& account) const
2121{
2123
2124 std::scoped_lock const lock(mutex_);
2125
2126 AccountMap::const_iterator const accountIter{byAccount_.find(account)};
2127
2128 if (accountIter == byAccount_.end() || accountIter->second.transactions.empty())
2129 return result;
2130
2131 result.reserve(accountIter->second.transactions.size());
2132 for (auto const& tx : accountIter->second.transactions)
2133 {
2134 result.emplace_back(tx.second.getTxDetails());
2135 }
2136 return result;
2137}
2138
2141{
2143
2144 std::scoped_lock const lock(mutex_);
2145
2146 result.reserve(byFee_.size());
2147
2148 for (auto const& tx : byFee_)
2149 result.emplace_back(tx.getTxDetails());
2150
2151 return result;
2152}
2153
2156{
2157 auto const view = app.getOpenLedger().current();
2158 if (!view)
2159 {
2160 BOOST_ASSERT(false);
2161 return {};
2162 }
2163
2164 auto const metrics = getMetrics(*view);
2165
2167
2168 auto& levels = ret[jss::levels] = json::ValueType::Object;
2169
2170 ret[jss::ledger_current_index] = view->header().seq;
2171 ret[jss::expected_ledger_size] = std::to_string(metrics.txPerLedger);
2172 ret[jss::current_ledger_size] = std::to_string(metrics.txInLedger);
2173 ret[jss::current_queue_size] = std::to_string(metrics.txCount);
2174 if (metrics.txQMaxSize)
2175 ret[jss::max_queue_size] = std::to_string(*metrics.txQMaxSize);
2176
2177 levels[jss::reference_level] = to_string(metrics.referenceFeeLevel);
2178 levels[jss::minimum_level] = to_string(metrics.minProcessingFeeLevel);
2179 levels[jss::median_level] = to_string(metrics.medFeeLevel);
2180 levels[jss::open_ledger_level] = to_string(metrics.openLedgerFeeLevel);
2181
2182 auto const baseFee = view->fees().base;
2183 // If the base fee is 0 drops, but escalation has kicked in, treat the
2184 // base fee as if it is 1 drop, which makes the rest of the math
2185 // work.
2186 auto const effectiveBaseFee = [&baseFee, &metrics] {
2187 if (!baseFee && metrics.openLedgerFeeLevel != metrics.referenceFeeLevel)
2188 return XRPAmount{1};
2189 return baseFee;
2190 }();
2191 auto& drops = ret[jss::drops] = json::Value();
2192
2193 drops[jss::base_fee] = to_string(baseFee);
2194 drops[jss::median_fee] = to_string(toDrops(metrics.medFeeLevel, baseFee));
2195 drops[jss::minimum_fee] = to_string(toDrops(
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)
2200 openFee += 1;
2201 drops[jss::open_ledger_fee] = to_string(openFee);
2202
2203 return ret;
2204}
2205
2207
2209setupTxQ(Config const& config)
2210{
2211 TxQ::Setup setup;
2212 auto const& section = config.section(Sections::kTransactionQueue);
2213 set(setup.ledgersInQueue, Keys::kLedgersInQueue, section);
2214 set(setup.queueSizeMin, Keys::kMinimumQueueSize, section);
2220 std::uint32_t max = 0;
2221 if (set(max, Keys::kMaximumTxnInLedger, section))
2222 {
2223 if (max < setup.minimumTxnInLedger)
2224 {
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).");
2230 }
2231 if (max < setup.minimumTxnInLedgerSA)
2232 {
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).");
2238 }
2239
2240 setup.maximumTxnInLedger.emplace(max);
2241 }
2242
2243 /* The math works as expected for any value up to and including
2244 MAXINT, but put a reasonable limit on this percentage so that
2245 the factor can't be configured to render escalation effectively
2246 moot. (There are other ways to do that, including
2247 minimum_txn_in_ledger_.)
2248 */
2252
2253 /* If this percentage is outside of the 0-100 range, the results
2254 are nonsensical (uint overflows happen, so the limit grows
2255 instead of shrinking). 0 is not recommended.
2256 */
2259
2262
2263 setup.standAlone = config.standalone();
2264 return setup;
2265}
2266
2267} // namespace xrpl
T accumulate(T... args)
T clamp(T... args)
A generic endpoint for log messages.
Definition Journal.h:44
Stream debug() const
Definition Journal.h:344
Represents a JSON value.
Definition json_value.h:121
Editable, discardable view that can build metadata for one tx.
Section & section(std::string const &name)
Returns the section with the given name.
bool standalone() const
std::shared_ptr< OpenView const > current() const
Returns a view to the current open ledger.
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:59
std::size_t txCount() const
Return the number of tx inserted since creation.
Definition OpenView.cpp:123
SLE::const_pointer read(Keylet const &k) const override
Return the state item associated with a key.
Definition OpenView.cpp:170
LedgerHeader const & header() const override
Returns information about the ledger.
Definition OpenView.cpp:139
void apply(TxsRawView &to) const
Apply changes.
Definition OpenView.cpp:129
Rules const & rules() const override
Returns the tx processing rules.
Definition OpenView.cpp:151
A view into a ledger.
Definition ReadView.h:41
TxsType txs
Definition ReadView.h:270
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
Definition STObject.cpp:601
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:464
SeqProxy getSeqProxy() const
Definition STTx.cpp:198
TxType getTxnType() const
Definition STTx.h:250
A type that represents either a sequence value or a ticket value.
Definition SeqProxy.h:37
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition SeqProxy.h:62
constexpr std::uint32_t value() const
Definition SeqProxy.h:80
constexpr bool isSeq() const
Definition SeqProxy.h:86
virtual OpenLedger & getOpenLedger()=0
std::optional< TxMapCIter > txToReplaceIter_
Definition TxQ.cpp:167
std::shared_ptr< STTx const > const & tx_
Definition TxQ.cpp:141
SeqProxy const txSeq_
Definition TxQ.cpp:149
std::expected< bool, ApplyResult > checkMultiTxn() const
Definition TxQ.cpp:396
PreflightResult const preflightRes_
Definition TxQ.cpp:150
TxQ::TxQAccount::TxMap::iterator TxMapIter
Definition TxQ.cpp:127
std::optional< std::reference_wrapper< std::scoped_lock< std::mutex > const > > txqLock_
Definition TxQ.cpp:132
std::optional< ApplyResult > checkFeeLevel() const
Definition TxQ.cpp:332
beast::Journal j_
Definition TxQ.cpp:138
OpenView & view_
Definition TxQ.cpp:135
std::optional< ApplyResult > checkFrontTx(TxMapCIter const &prevIter, TxMapCIter const &endIter) const
Definition TxQ.cpp:426
Application & app_
Definition TxQ.cpp:134
void addTxToQueue()
Definition TxQ.cpp:787
std::optional< TxQ::FeeMetrics::Snapshot > metricsSnapshot_
Definition TxQ.cpp:152
std::optional< ApplyResult > checkIsTxBlocker() const
Definition TxQ.cpp:294
std::pair< TxMapIter, TxMapIter > TxRange
Definition TxQ.cpp:128
TxQApplyImpl(TxQ &txq, Application &app, OpenView &view, std::shared_ptr< STTx const > const &tx, ApplyFlags flags, beast::Journal j)
Definition TxQ.cpp:175
std::optional< MultiTxn > multiTxn_
Definition TxQ.cpp:170
SeqProxy accSeq_
Definition TxQ.cpp:146
ApplyResult applyImpl()
Definition TxQ.cpp:1529
std::optional< ApplyResult > canBeHeld() const
Definition TxQ.cpp:693
std::pair< XRPAmount, XRPAmount > calcSpendAndFee() const
Definition TxQ.cpp:466
std::optional< ApplyResult > checkBlockerInAccQue() const
Definition TxQ.cpp:368
std::optional< ApplyResult > processAccountTxs()
Definition TxQ.cpp:506
AccountID const accID_
Definition TxQ.cpp:143
std::optional< ApplyResult > applyImplPrelock()
Definition TxQ.cpp:1498
std::uint32_t accTxCount_
Definition TxQ.cpp:160
AccMapIter byAccountIter_
Definition TxQ.cpp:164
std::optional< ApplyResult > checkTicketInLedger() const
Definition TxQ.cpp:254
std::optional< TxRange > prevTxs_
Definition TxQ.cpp:158
FeeLevel64 feeLevelPaid_
Definition TxQ.cpp:153
TxQ::AccountMap::iterator AccMapIter
Definition TxQ.cpp:129
void retrieveTxToReplace()
Definition TxQ.cpp:321
beast::Journal jExt_
Definition TxQ.cpp:140
Keylet const accKey_
Definition TxQ.cpp:144
void retrievePrevTxs()
Definition TxQ.cpp:274
ApplyFlags flags_
Definition TxQ.cpp:136
std::optional< ApplyResult > tryClearAccountQueueUpThruTx()
Definition TxQ.cpp:638
TxQ::TxQAccount::TxMap::const_iterator TxMapCIter
Definition TxQ.cpp:126
ApplyResult apply()
Definition TxQ.cpp:1481
FeeLevel64 requiredFeeLevel_
Definition TxQ.cpp:154
uint256 const txID_
Definition TxQ.cpp:148
std::optional< ApplyResult > processQueueIsFull()
Definition TxQ.cpp:715
SLE::const_pointer accSle_
Definition TxQ.cpp:145
std::size_t txnsExpected_
Number of transactions expected per ledger.
Definition TxQ.h:459
std::size_t const targetTxnCount_
Number of transactions per ledger that fee escalation "workstowards".
Definition TxQ.h:449
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...
Definition TxQ.cpp:912
beast::Journal const j_
Journal.
Definition TxQ.h:473
std::optional< std::size_t > const maximumTxnCount_
Maximum value of txnsExpected.
Definition TxQ.h:453
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.
Definition TxQ.cpp:818
std::size_t const minimumTxnCount_
Minimum value of txnsExpected.
Definition TxQ.h:444
boost::circular_buffer< std::size_t > recentTxnCounts_
Recent history of transaction counts that exceed the targetTxnCount_.
Definition TxQ.h:464
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.
Definition TxQ.cpp:970
FeeLevel64 escalationMultiplier_
Based on the median fee of the LCL.
Definition TxQ.h:469
Represents a transaction in the queue which may be applied later to the open ledger.
Definition TxQ.h:590
static LedgerHash parentHashComp
The hash of the parent ledger.
Definition TxQ.h:686
std::optional< LedgerIndex > const lastValid
Expiration ledger for the transaction (sfLastLedgerSequence field).
Definition TxQ.h:620
TxID const txID
Transaction ID.
Definition TxQ.h:611
MaybeTx(std::shared_ptr< STTx const > const &txn, TxID const &txID, FeeLevel64 feeLevel, ApplyFlags const flags, PreflightResult const &pfResult)
Constructor.
Definition TxQ.cpp:1016
FeeLevel64 const feeLevel
Computed fee level that the transaction will pay.
Definition TxQ.h:607
ApplyFlags const flags
Flags provided to apply.
Definition TxQ.h:640
ApplyResult apply(Application &app, OpenView &view, beast::Journal j)
Attempt to apply the queued transaction to the open ledger.
Definition TxQ.cpp:1034
SeqProxy const seqProxy
Transaction SeqProxy number (sfSequence or sfTicketSequence field).
Definition TxQ.h:625
std::shared_ptr< STTx const > const txn
The complete transaction.
Definition TxQ.h:602
static constexpr int kRetriesAllowed
Starting retry count for newly queued transactions.
Definition TxQ.h:675
AccountID const account
Account "owner" of the transaction.
Definition TxQ.h:615
std::optional< PreflightResult const > pfResult
Cached result of the preflight operation.
Definition TxQ.h:658
Used to represent an account to the queue, and stores the transactions queued for that account by Seq...
Definition TxQ.h:776
TxMap::const_iterator getPrevTx(SeqProxy seqProx) const
Find the entry in transactions that precedes seqProx, if one does.
Definition TxQ.cpp:1065
TxMap transactions
Sequence number will be used as the key.
Definition TxQ.h:787
MaybeTx & add(MaybeTx &&)
Add a transaction candidate to this account for queuing.
Definition TxQ.cpp:1076
std::size_t getTxnCount() const
Return the number of transactions currently queued for this account.
Definition TxQ.h:817
TxQAccount(std::shared_ptr< STTx const > const &txn)
Construct from a transaction.
Definition TxQ.cpp:1055
bool remove(SeqProxy seqProx)
Remove the candidate with given SeqProxy value from this account.
Definition TxQ.cpp:1089
AccountID const account
The account.
Definition TxQ.h:783
std::map< SeqProxy, MaybeTx > TxMap
Definition TxQ.h:778
Transaction Queue.
Definition TxQ.h:59
void rebuildQueue(OpenView &view)
Definition TxQ.cpp:1697
Metrics getMetrics(OpenView const &view) const
Returns fee metrics in reference fee level units.
Definition TxQ.cpp:2070
json::Value doRPC(Application &app) const
Summarize current fee metrics for the fee RPC command.
Definition TxQ.cpp:2155
TxQ(Setup const &setup, beast::Journal j)
Constructor.
Definition TxQ.cpp:1096
FeeMetrics feeMetrics_
Tracks the current state of the queue.
Definition TxQ.h:899
SeqProxy nextQueuableSeq(SLE::ConstRef sleAccount) const
Return the next sequence that would go in the TxQ for an account.
Definition TxQ.cpp:1904
void removeFromByFee(std::optional< TxQAccount::TxMap::const_iterator > const &replacedTxIter, std::shared_ptr< STTx const > const &tx)
Definition TxQ.cpp:2045
std::optional< size_t > maxSize_
Maximum number of transactions allowed in the queue based on the current metrics.
Definition TxQ.h:922
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.
Definition TxQ.cpp:1115
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...
Definition TxQ.cpp:2092
void processClosedLedger(Application &app, ReadView const &view, bool timeLeap)
Update fee metrics and clean up the queue in preparation for the next ledger.
Definition TxQ.cpp:1655
std::vector< TxDetails > getAccountTxs(AccountID const &account) const
Returns information about the transactions currently in the queue for the account.
Definition TxQ.cpp:2120
bool isFull() const
Is the queue at least fillPercentage full?
Definition TxQ.cpp:1108
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...
Definition TxQ.cpp:1214
static FeeLevel64 getRequiredFeeLevel(OpenView &view, ApplyFlags flags, FeeMetrics::Snapshot const &metricsSnapshot, std::scoped_lock< std::mutex > const &lock)
Definition TxQ.cpp:1962
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.
Definition TxQ.cpp:1469
std::optional< ApplyResult > tryDirectApply(Application &app, OpenView &view, std::shared_ptr< STTx const > const &tx, ApplyFlags flags, beast::Journal j)
Definition TxQ.cpp:1972
virtual ~TxQ()
Destructor.
Definition TxQ.cpp:1101
SeqProxy nextQueuableSeqImpl(SLE::ConstRef sleAccount, std::scoped_lock< std::mutex > const &) const
Definition TxQ.cpp:1917
bool accept(Application &app, OpenView &view)
Fill the new open ledger with transactions from the queue.
Definition TxQ.cpp:1762
std::mutex mutex_
Most queue operations are done under the master lock, but use this mutex for the RPC "fee" command,...
Definition TxQ.h:933
std::vector< TxDetails > getTxs() const
Returns information about all transactions currently in the queue.
Definition TxQ.cpp:2140
FeeMultiSet byFee_
The queue itself: the collection of transactions ordered by fee level.
Definition TxQ.h:906
beast::Journal const j_
Journal.
Definition TxQ.h:892
friend class TxQApplyImpl
Definition TxQ.h:1000
LedgerHash parentHash_
parentHash_ used for logging only
Definition TxQ.h:927
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.
Definition TxQ.h:64
AccountMap byAccount_
All of the accounts which currently have any transactions in the queue.
Definition TxQ.h:914
Setup const setup_
Setup parameters used to control the behavior of the queue.
Definition TxQ.h:888
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.
Definition TxQ.cpp:1260
T distance(T... args)
T emplace_back(T... args)
T emplace(T... args)
T empty(T... args)
T end(T... args)
T find(T... args)
T for_each(T... args)
T lower_bound(T... args)
T max_element(T... args)
T max(T... args)
T min(T... args)
constexpr Zero kZero
Definition Zero.h:30
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
STL namespace.
static constexpr std::pair< bool, std::uint64_t > sumOfFirstSquares(std::size_t xIn)
Definition TxQ.cpp:936
Keylet computation functions.
Definition Indexes.h:39
Keylet ticket(AccountID const &id, SeqProxy const &ticketSeq)
A ticket belonging to an account.
Definition Indexes.cpp:344
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ telCAN_NOT_QUEUE_FULL
Definition TER.h:50
@ telCAN_NOT_QUEUE_FEE
Definition TER.h:49
@ telCAN_NOT_QUEUE_BLOCKED
Definition TER.h:48
@ telINSUF_FEE_P
Definition TER.h:43
@ telCAN_NOT_QUEUE
Definition TER.h:45
@ telCAN_NOT_QUEUE_BALANCE
Definition TER.h:46
@ telCAN_NOT_QUEUE_BLOCKS
Definition TER.h:47
@ terPRE_SEQ
Definition TER.h:223
@ terNO_ACCOUNT
Definition TER.h:219
@ terPRE_TICKET
Definition TER.h:228
@ terQUEUED
Definition TER.h:227
bool set(T &target, std::string const &name, Section const &section)
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)
Definition TxQ.cpp:98
PreflightResult preflight(ServiceRegistry &registry, 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 &registry, 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 &registry, OpenView &view, STTx const &tx, ApplyFlags flags, beast::Journal journal)
Apply a transaction to an OpenView.
Definition apply.cpp:181
@ tefNO_TICKET
Definition TER.h:179
@ tefINTERNAL
Definition TER.h:167
@ tefPAST_SEQ
Definition TER.h:169
UInt256 TxID
A transaction identifier.
Definition Protocol.h:430
XRPAmount toDrops(FeeLevel< T > const &level, XRPAmount baseFee)
Definition TxQ.h:1011
UInt256 LedgerHash
static FeeLevel64 increase(FeeLevel64 level, std::uint32_t increasePercent)
Definition TxQ.cpp:106
std::string transToken(TER code)
Definition TER.cpp:259
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
static std::expected< FeeLevel64, TER > getFeeLevelPaid(ReadView const &view, STTx const &tx)
Compute the fee level that a transaction pays.
Definition TxQ.cpp:67
bool isTefFailure(TER x) noexcept
Definition TER.h:674
bool isFeeSponsored(STTx const &tx)
Whether the transaction's fee is sponsored (sfSponsor present + spfSponsorFee set).
FeeLevel< std::uint64_t > FeeLevel64
Definition Units.h:443
FeeLevel64 toFeeLevel(XRPAmount const &drops, XRPAmount const &baseFee)
Definition TxQ.h:1017
constexpr auto kMuldivMax
Definition mulDiv.h:8
XRPAmount calculateDefaultBaseFee(ReadView const &view, STTx const &tx)
Return the minimum fee that an "ordinary" transaction would pay.
ApplyFlags
Definition ApplyView.h:27
@ TapDryRun
Definition ApplyView.h:46
@ TapFailHard
Definition ApplyView.h:32
@ TapNone
Definition ApplyView.h:28
@ TapRetry
Definition ApplyView.h:36
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
bool isTesSuccess(TER x) noexcept
Definition TER.h:686
TERSubset< CanCvtToTER > TER
Definition TER.h:657
constexpr struct xrpl::OpenLedgerT kOpenLedger
ApplyResult doApply(PreclaimResult const &preclaimResult, ServiceRegistry &registry, OpenView &view)
Apply a prechecked transaction to an OpenView.
TxQ::Setup setupTxQ(Config const &config)
Build a TxQ::Setup object from application configuration.
Definition TxQ.cpp:2209
bool isTemMalformed(TER x) noexcept
Definition TER.h:668
@ tesSUCCESS
Definition TER.h:253
UInt256 uint256
Definition base_uint.h:587
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
T next(T... args)
T push_back(T... args)
T reserve(T... args)
T size(T... args)
T sort(T... args)
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
static constexpr auto kRetrySequencePercent
Definition Constants.h:155
static constexpr auto kMinimumEscalationMultiplier
Definition Constants.h:133
static constexpr auto kMaximumTxnInLedger
Definition Constants.h:128
static constexpr auto kSlowConsensusDecreasePercent
Definition Constants.h:163
static constexpr auto kMinimumLastLedgerBuffer
Definition Constants.h:134
static constexpr auto kMaximumTxnPerAccount
Definition Constants.h:129
static constexpr auto kTargetTxnInLedger
Definition Constants.h:171
static constexpr auto kMinimumQueueSize
Definition Constants.h:135
static constexpr auto kMinimumTxnInLedger
Definition Constants.h:136
static constexpr auto kNormalConsensusIncreasePercent
Definition Constants.h:138
static constexpr auto kLedgersInQueue
Definition Constants.h:120
static constexpr auto kMinimumTxnInLedgerStandalone
Definition Constants.h:137
ApplyViewImpl applyView
Definition TxQ.cpp:116
MultiTxn(OpenView &view, ApplyFlags flags)
Definition TxQ.cpp:119
OpenView openView
Definition TxQ.cpp:117
Describes the results of the preflight check.
Definition applySteps.h:201
Iterator end() const
Definition ReadView.cpp:56
Iterator begin() const
Definition ReadView.cpp:50
static constexpr auto kTransactionQueue
Definition Constants.h:66
Snapshot of the externally relevant FeeMetrics fields at any given time.
Definition TxQ.h:516
std::size_t const txnsExpected
Definition TxQ.h:520
FeeLevel64 const escalationMultiplier
Definition TxQ.h:523
Structure returned by TxQ::getMetrics, expressed in reference fee level units.
Definition TxQ.h:186
std::size_t txCount
Number of transactions in the queue.
Definition TxQ.h:195
std::optional< std::size_t > txQMaxSize
Max transactions currently allowed in queue.
Definition TxQ.h:199
FeeLevel64 openLedgerFeeLevel
Minimum fee level to get into the current open ledger, bypassing the queue.
Definition TxQ.h:225
std::size_t txInLedger
Number of transactions currently in the open ledger.
Definition TxQ.h:203
FeeLevel64 minProcessingFeeLevel
Minimum fee level for a transaction to be considered for the open ledger or the queue.
Definition TxQ.h:216
FeeLevel64 referenceFeeLevel
Reference transaction fee level.
Definition TxQ.h:211
FeeLevel64 medFeeLevel
Median fee level of the last ledger.
Definition TxQ.h:220
std::size_t txPerLedger
Number of transactions expected per ledger.
Definition TxQ.h:207
Structure used to customize TxQ behavior.
Definition TxQ.h:70
bool standAlone
Use standalone mode behavior.
Definition TxQ.h:178
std::uint32_t maximumTxnPerAccount
Maximum number of transactions that can be queued by one account.
Definition TxQ.h:166
FeeLevel64 minimumEscalationMultiplier
Minimum value of the escalation multiplier, regardless of the prior ledger's median fee level.
Definition TxQ.h:107
std::optional< std::uint32_t > maximumTxnInLedger
Optional maximum allowed value of transactions per ledger before fee escalation kicks in.
Definition TxQ.h:134
std::uint32_t targetTxnInLedger
Number of transactions per ledger that fee escalation "workstowards".
Definition TxQ.h:122
std::uint32_t minimumLastLedgerBuffer
Minimum difference between the current ledger sequence and a transaction's LastLedgerSequence for the...
Definition TxQ.h:174
std::size_t ledgersInQueue
Number of ledgers' worth of transactions to allow in the queue.
Definition TxQ.h:84
std::uint32_t retrySequencePercent
Extra percentage required on the fee level of a queued transaction to replace that transaction with a...
Definition TxQ.h:102
std::uint32_t minimumTxnInLedgerSA
Like minimumTxnInLedger for standalone mode.
Definition TxQ.h:117
std::uint32_t slowConsensusDecreasePercent
When consensus takes longer than appropriate, the expected ledger size is updated to the lesser of th...
Definition TxQ.h:162
std::size_t queueSizeMin
The smallest limit the queue is allowed.
Definition TxQ.h:91
std::uint32_t minimumTxnInLedger
Minimum number of transactions to allow into the ledger before escalation, regardless of the prior le...
Definition TxQ.h:112
std::uint32_t normalConsensusIncreasePercent
When the ledger has more transactions than "expected", and performance is humming along nicely,...
Definition TxQ.h:147
T tie(T... args)
T to_string(T... args)
T unexpected(T... args)
T upper_bound(T... args)
T value_or(T... args)