xrpld
Loading...
Searching...
No Matches
TxQ.h
1#pragma once
2
3#include <xrpl/ledger/ApplyView.h>
4#include <xrpl/ledger/OpenView.h>
5#include <xrpl/protocol/RippleLedgerHash.h>
6#include <xrpl/protocol/STTx.h>
7#include <xrpl/protocol/SeqProxy.h>
8#include <xrpl/protocol/TER.h>
9#include <xrpl/tx/applySteps.h>
10
11#include <boost/circular_buffer.hpp>
12#include <boost/intrusive/set.hpp>
13
14#include <optional>
15
16namespace xrpl {
17
18class Application;
19class Config;
20
39class TxQ
40{
41public:
43 static constexpr FeeLevel64 kBaseLevel{256};
44
138
167
236
238 TxQ(Setup const& setup, beast::Journal j);
239
241 virtual ~TxQ();
242
253 apply(
254 Application& app,
255 OpenView& view,
257 ApplyFlags flags,
259
271 bool
272 accept(Application& app, OpenView& view);
273
286 void
287 processClosedLedger(Application& app, ReadView const& view, bool timeLeap);
288
291 nextQueuableSeq(SLE::const_ref sleAccount) const;
292
295 Metrics
296 getMetrics(OpenView const& view) const;
297
304
315 getTxRequiredFeeAndSeq(OpenView const& view, std::shared_ptr<STTx const> const& tx) const;
316
324 getAccountTxs(AccountID const& account) const;
325
333 getTxs() const;
334
340 doRPC(Application& app) const;
341
342private:
343 // Implementation for nextQueuableSeq(). The passed lock must be held.
346
353 {
354 private:
368 boost::circular_buffer<std::size_t> recentTxnCounts_;
374
375 public:
379 setup.standAlone ? setup.minimumTxnInLedgerSA : setup.minimumTxnInLedger)
381 setup.targetTxnInLedger < minimumTxnCount_ ? minimumTxnCount_
382 : setup.targetTxnInLedger)
383 , maximumTxnCount_([&]() -> std::optional<std::size_t> {
384 if (!setup.maximumTxnInLedger)
385 return std::nullopt;
387 : *setup.maximumTxnInLedger;
388 }())
390 , recentTxnCounts_(setup.ledgersInQueue)
391 , escalationMultiplier_(setup.minimumEscalationMultiplier)
392 , j_(j)
393 {
394 }
395
407 update(Application& app, ReadView const& view, bool timeLeap, TxQ::Setup const& setup);
408
411 struct Snapshot
412 {
413 // Number of transactions expected per ledger.
414 // One more than this value will be accepted
415 // before escalation kicks in.
417 // Based on the median fee of the LCL. Used
418 // when fee escalation kicks in.
420 };
421
423 [[nodiscard]] Snapshot
425 {
426 return {.txnsExpected = txnsExpected_, .escalationMultiplier = escalationMultiplier_};
427 }
428
437 static FeeLevel64
438 scaleFeeLevel(Snapshot const& snapshot, OpenView const& view);
439
472 Snapshot const& snapshot,
473 OpenView const& view,
474 std::size_t extraCount,
475 std::size_t seriesSize);
476 };
477
483 {
484 public:
488 boost::intrusive::set_member_hook<> byFeeListHook;
489
492
496 TxID const txID;
534
549 static constexpr int kRetriesAllowed = 10;
550
560
561 public:
563 MaybeTx(
565 TxID const& txID,
567 ApplyFlags const flags,
569
573
576 [[nodiscard]] TxConsequences const&
578 {
579 return pfResult->consequences; // NOLINT(bugprone-unchecked-optional-access) invariant:
580 // pfResult is never empty
581 }
582
584 [[nodiscard]] TxDetails
586 {
587 return {
588 feeLevel,
589 lastValid,
590 consequences(),
591 account,
592 seqProxy,
593 txn,
595 pfResult->ter, // NOLINT(bugprone-unchecked-optional-access) invariant: pfResult is
596 // never empty
597 lastResult};
598 }
599 };
600
603 {
604 public:
606 explicit OrderCandidates() = default;
607
623 bool
624 operator()(MaybeTx const& lhs, MaybeTx const& rhs) const
625 {
626 if (lhs.feeLevel == rhs.feeLevel)
628 return lhs.feeLevel > rhs.feeLevel;
629 }
630 };
631
636 {
637 public:
639
644 /* If this account has had any transaction retry more than
645 `retriesAllowed` times so that it was dropped from the
646 queue, then all other transactions for this account will
647 be given at most 2 attempts before being removed. Helps
648 prevent wasting resources on retries that are more likely
649 to fail.
650 */
651 bool retryPenalty = false;
652 /* If this account has had any transaction fail or expire,
653 then when the queue is nearly full, transactions from
654 this account will be discarded. Helps prevent the queue
655 from getting filled and wedged.
656 */
657 bool dropPenalty = false;
658
659 public:
661 explicit TxQAccount(std::shared_ptr<STTx const> const& txn);
663 explicit TxQAccount(AccountID const& account);
664
666 [[nodiscard]] std::size_t
668 {
669 return transactions.size();
670 }
671
673 [[nodiscard]] bool
674 empty() const
675 {
676 return getTxnCount() == 0u;
677 }
678
680 [[nodiscard]] TxMap::const_iterator
681 getPrevTx(SeqProxy seqProx) const;
682
684 MaybeTx&
685 add(MaybeTx&&);
686
692 bool
693 remove(SeqProxy seqProx);
694 };
695
696 // Helper function returns requiredFeeLevel.
697 static FeeLevel64
699 OpenView& view,
700 ApplyFlags flags,
701 FeeMetrics::Snapshot const& metricsSnapshot,
702 std::scoped_lock<std::mutex> const& lock);
703
704 // Helper function for TxQ::apply. If a transaction's fee is high enough,
705 // attempt to directly apply that transaction to the ledger.
708 Application& app,
709 OpenView& view,
711 ApplyFlags flags,
713
714 // Helper function that removes a replaced entry in _byFee.
717 std::optional<TxQAccount::TxMap::iterator> const& replacedTxIter,
719
720 using FeeHook = boost::intrusive::
721 member_hook<MaybeTx, boost::intrusive::set_member_hook<>, &MaybeTx::byFeeListHook>;
722
724 boost::intrusive::multiset<MaybeTx, FeeHook, boost::intrusive::compare<OrderCandidates>>;
725
727
732
758
762 LedgerHash parentHash_{beast::kZero};
763
768
769private:
771 template <size_t FillPercentage = 100>
772 bool
773 isFull() const;
774
778 TER
779 canBeHeld(
780 STTx const&,
781 ApplyFlags const,
782 OpenView const&,
783 SLE::const_ref sleAccount,
784 AccountMap::iterator const&,
786 std::scoped_lock<std::mutex> const& lock);
787
789 FeeMultiSet::iterator_type erase(FeeMultiSet::const_iterator_type);
794 FeeMultiSet::iterator_type eraseAndAdvance(FeeMultiSet::const_iterator_type);
796 TxQAccount::TxMap::iterator
797 erase(
798 TxQAccount& txQAccount,
799 TxQAccount::TxMap::const_iterator begin,
800 TxQAccount::TxMap::const_iterator end);
801
809 Application& app,
810 OpenView& view,
811 STTx const& tx,
812 AccountMap::iterator const& accountIter,
813 TxQAccount::TxMap::iterator,
814 FeeLevel64 feeLevelPaid,
815 PreflightResult const& pfResult,
816 std::size_t const txExtraCount,
817 ApplyFlags flags,
818 FeeMetrics::Snapshot const& metricsSnapshot,
820};
821
826setupTxQ(Config const&);
827
828template <class T>
830toDrops(FeeLevel<T> const& level, XRPAmount baseFee)
831{
833}
834
835inline FeeLevel64
836toFeeLevel(XRPAmount const& drops, XRPAmount const& baseFee)
837{
838 return mulDiv(drops, TxQ::kBaseLevel, baseFee)
840}
841
842} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
Represents a JSON value.
Definition json_value.h:130
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:45
A view into a ledger.
Definition ReadView.h:31
static constexpr std::uint64_t kMaxNativeN
Definition STAmount.h:58
std::shared_ptr< STLedgerEntry const > const & const_ref
A type that represents either a sequence value or a ticket value.
Definition SeqProxy.h:36
Class describing the consequences to the account of applying a transaction if the transaction consume...
Definition applySteps.h:38
Track and use the fee escalation metrics of the current open ledger.
Definition TxQ.h:353
Snapshot getSnapshot() const
Get the current Snapshot.
Definition TxQ.h:424
std::size_t txnsExpected_
Number of transactions expected per ledger.
Definition TxQ.h:365
std::size_t const targetTxnCount_
Number of transactions per ledger that fee escalation "workstowards".
Definition TxQ.h:359
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:181
beast::Journal const j_
Journal.
Definition TxQ.h:373
std::optional< std::size_t > const maximumTxnCount_
Maximum value of txnsExpected.
Definition TxQ.h:361
FeeMetrics(Setup const &setup, beast::Journal j)
Constructor.
Definition TxQ.h:377
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:101
std::size_t const minimumTxnCount_
Minimum value of txnsExpected.
Definition TxQ.h:356
boost::circular_buffer< std::size_t > recentTxnCounts_
Recent history of transaction counts that exceed the targetTxnCount_.
Definition TxQ.h:368
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:239
FeeLevel64 escalationMultiplier_
Based on the median fee of the LCL.
Definition TxQ.h:371
Represents a transaction in the queue which may be applied later to the open ledger.
Definition TxQ.h:483
static LedgerHash parentHashComp
The hash of the parent ledger.
Definition TxQ.h:559
std::optional< LedgerIndex > const lastValid
Expiration ledger for the transaction (sfLastLedgerSequence field).
Definition TxQ.h:501
TxDetails getTxDetails() const
Return a TxDetails based on contained information.
Definition TxQ.h:585
TxID const txID
Transaction ID.
Definition TxQ.h:496
std::optional< TER > lastResult
If the transactor attempted to apply the transaction to the open ledger from the queue and failed,...
Definition TxQ.h:524
FeeLevel64 const feeLevel
Computed fee level that the transaction will pay.
Definition TxQ.h:494
boost::intrusive::set_member_hook byFeeListHook
Used by the TxQ::FeeHook and TxQ::FeeMultiSet below to put each MaybeTx object into more than one set...
Definition TxQ.h:488
MaybeTx(std::shared_ptr< STTx const > const &, TxID const &txID, FeeLevel64 feeLevel, ApplyFlags const flags, PreflightResult const &pfResult)
Constructor.
Definition TxQ.cpp:285
TxConsequences const & consequences() const
Potential TxConsequences of applying this transaction to the open ledger.
Definition TxQ.h:577
ApplyFlags const flags
Flags provided to apply.
Definition TxQ.h:517
ApplyResult apply(Application &app, OpenView &view, beast::Journal j)
Attempt to apply the queued transaction to the open ledger.
Definition TxQ.cpp:303
SeqProxy const seqProxy
Transaction SeqProxy number (sfSequence or sfTicketSequence field).
Definition TxQ.h:504
std::shared_ptr< STTx const > txn
The complete transaction.
Definition TxQ.h:491
static constexpr int kRetriesAllowed
Starting retry count for newly queued transactions.
Definition TxQ.h:549
int retriesRemaining
A transaction at the front of the queue will be given several attempts to succeed before being droppe...
Definition TxQ.h:513
AccountID const account
Account submitting the transaction.
Definition TxQ.h:498
std::optional< PreflightResult const > pfResult
Cached result of the preflight operation.
Definition TxQ.h:533
OrderCandidates()=default
Default constructor.
bool operator()(MaybeTx const &lhs, MaybeTx const &rhs) const
Sort MaybeTx by feeLevel descending, then by pseudo-randomized transaction ID ascending.
Definition TxQ.h:624
Used to represent an account to the queue, and stores the transactions queued for that account by Seq...
Definition TxQ.h:636
TxMap::const_iterator getPrevTx(SeqProxy seqProx) const
Find the entry in transactions that precedes seqProx, if one does.
Definition TxQ.cpp:334
TxMap transactions
Sequence number will be used as the key.
Definition TxQ.h:643
bool empty() const
Checks if this account has no transactions queued.
Definition TxQ.h:674
MaybeTx & add(MaybeTx &&)
Add a transaction candidate to this account for queuing.
Definition TxQ.cpp:345
std::size_t getTxnCount() const
Return the number of transactions currently queued for this account.
Definition TxQ.h:667
TxQAccount(std::shared_ptr< STTx const > const &txn)
Construct from a transaction.
Definition TxQ.cpp:324
bool remove(SeqProxy seqProx)
Remove the candidate with given SeqProxy value from this account.
Definition TxQ.cpp:358
AccountID const account
The account.
Definition TxQ.h:641
std::map< SeqProxy, MaybeTx > TxMap
Definition TxQ.h:638
Metrics getMetrics(OpenView const &view) const
Returns fee metrics in reference fee level units.
Definition TxQ.cpp:1739
json::Value doRPC(Application &app) const
Summarize current fee metrics for the fee RPC command.
Definition TxQ.cpp:1818
TxQ(Setup const &setup, beast::Journal j)
Constructor.
Definition TxQ.cpp:365
SeqProxy nextQueuableSeq(SLE::const_ref sleAccount) const
Return the next sequence that would go in the TxQ for an account.
Definition TxQ.cpp:1579
FeeMetrics feeMetrics_
Tracks the current state of the queue.
Definition TxQ.h:737
std::optional< size_t > maxSize_
Maximum number of transactions allowed in the queue based on the current metrics.
Definition TxQ.h:757
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:1336
std::map< AccountID, TxQAccount > AccountMap
Definition TxQ.h:726
std::vector< TxDetails > getAccountTxs(AccountID const &account) const
Returns information about the transactions currently in the queue for the account.
Definition TxQ.cpp:1783
SeqProxy nextQueuableSeqImpl(SLE::const_ref sleAccount, std::scoped_lock< std::mutex > const &) const
Definition TxQ.cpp:1592
bool isFull() const
Is the queue at least fillPercentage full?
Definition TxQ.cpp:377
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:474
ApplyResult tryClearAccountQueueUpThruTx(Application &app, OpenView &view, STTx const &tx, AccountMap::iterator const &accountIter, TxQAccount::TxMap::iterator, 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:520
static FeeLevel64 getRequiredFeeLevel(OpenView &view, ApplyFlags flags, FeeMetrics::Snapshot const &metricsSnapshot, std::scoped_lock< std::mutex > const &lock)
Definition TxQ.cpp:1637
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:728
FeeAndSeq 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:1761
std::optional< ApplyResult > tryDirectApply(Application &app, OpenView &view, std::shared_ptr< STTx const > const &tx, ApplyFlags flags, beast::Journal j)
Definition TxQ.cpp:1647
virtual ~TxQ()
Destructor.
Definition TxQ.cpp:370
bool accept(Application &app, OpenView &view)
Fill the new open ledger with transactions from the queue.
Definition TxQ.cpp:1407
std::mutex mutex_
Most queue operations are done under the master lock, but use this mutex for the RPC "fee" command,...
Definition TxQ.h:767
std::vector< TxDetails > getTxs() const
Returns information about all transactions currently in the queue.
Definition TxQ.cpp:1803
FeeMultiSet byFee_
The queue itself: the collection of transactions ordered by fee level.
Definition TxQ.h:743
beast::Journal const j_
Journal.
Definition TxQ.h:731
boost::intrusive:: member_hook< MaybeTx, boost::intrusive::set_member_hook<>, &MaybeTx::byFeeListHook > FeeHook
Definition TxQ.h:720
std::optional< TxQAccount::TxMap::iterator > removeFromByFee(std::optional< TxQAccount::TxMap::iterator > const &replacedTxIter, std::shared_ptr< STTx const > const &tx)
Definition TxQ.cpp:1713
boost::intrusive::multiset< MaybeTx, FeeHook, boost::intrusive::compare< OrderCandidates > > FeeMultiSet
Definition TxQ.h:723
LedgerHash parentHash_
parentHash_ used for logging only
Definition TxQ.h:762
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:43
TER canBeHeld(STTx const &, ApplyFlags const, OpenView const &, SLE::const_ref sleAccount, AccountMap::iterator const &, std::optional< TxQAccount::TxMap::iterator > const &, std::scoped_lock< std::mutex > const &lock)
Checks if the indicated transaction fits the conditions for being stored in the queue.
Definition TxQ.cpp:384
AccountMap byAccount_
All of the accounts which currently have any transactions in the queue.
Definition TxQ.h:750
Setup const setup_
Setup parameters used to control the behavior of the queue.
Definition TxQ.h:729
T max(T... args)
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::optional< std::uint64_t > mulDiv(std::uint64_t value, std::uint64_t mul, std::uint64_t div)
Return value*mul/div accurately.
XRPAmount toDrops(FeeLevel< T > const &level, XRPAmount baseFee)
Definition TxQ.h:830
uint256 LedgerHash
FeeLevel< std::uint64_t > FeeLevel64
Definition Units.h:428
FeeLevel64 toFeeLevel(XRPAmount const &drops, XRPAmount const &baseFee)
Definition TxQ.h:836
ApplyFlags
Definition ApplyView.h:12
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
TERSubset< CanCvtToTER > TER
Definition TER.h:634
unit::ValueUnit< unit::feelevelTag, T > FeeLevel
Definition Units.h:427
TxQ::Setup setupTxQ(Config const &config)
Build a TxQ::Setup object from application configuration.
Definition TxQ.cpp:1872
uint256 TxID
A transaction identifier.
Definition Protocol.h:275
Describes the results of the preflight check.
Definition applySteps.h:143
XRPAmount fee
Definition TxQ.h:300
std::uint32_t availableSeq
Definition TxQ.h:302
std::uint32_t accountSeq
Definition TxQ.h:301
Snapshot of the externally relevant FeeMetrics fields at any given time.
Definition TxQ.h:412
std::size_t const txnsExpected
Definition TxQ.h:416
FeeLevel64 const escalationMultiplier
Definition TxQ.h:419
Structure returned by TxQ::getMetrics, expressed in reference fee level units.
Definition TxQ.h:144
std::size_t txCount
Number of transactions in the queue.
Definition TxQ.h:149
Metrics()=default
Default constructor.
std::optional< std::size_t > txQMaxSize
Max transactions currently allowed in queue.
Definition TxQ.h:151
FeeLevel64 openLedgerFeeLevel
Minimum fee level to get into the current open ledger, bypassing the queue.
Definition TxQ.h:165
std::size_t txInLedger
Number of transactions currently in the open ledger.
Definition TxQ.h:153
FeeLevel64 minProcessingFeeLevel
Minimum fee level for a transaction to be considered for the open ledger or the queue.
Definition TxQ.h:160
FeeLevel64 referenceFeeLevel
Reference transaction fee level.
Definition TxQ.h:157
FeeLevel64 medFeeLevel
Median fee level of the last ledger.
Definition TxQ.h:162
std::size_t txPerLedger
Number of transactions expected per ledger.
Definition TxQ.h:155
Structure used to customize TxQ behavior.
Definition TxQ.h:49
bool standAlone
Use standalone mode behavior.
Definition TxQ.h:136
std::uint32_t maximumTxnPerAccount
Maximum number of transactions that can be queued by one account.
Definition TxQ.h:127
FeeLevel64 minimumEscalationMultiplier
Minimum value of the escalation multiplier, regardless of the prior ledger's median fee level.
Definition TxQ.h:79
std::optional< std::uint32_t > maximumTxnInLedger
Optional maximum allowed value of transactions per ledger before fee escalation kicks in.
Definition TxQ.h:99
Setup()=default
Default constructor.
std::uint32_t targetTxnInLedger
Number of transactions per ledger that fee escalation "workstowards".
Definition TxQ.h:88
std::uint32_t minimumLastLedgerBuffer
Minimum difference between the current ledger sequence and a transaction's LastLedgerSequence for the...
Definition TxQ.h:134
std::size_t ledgersInQueue
Number of ledgers' worth of transactions to allow in the queue.
Definition TxQ.h:60
std::uint32_t retrySequencePercent
Extra percentage required on the fee level of a queued transaction to replace that transaction with a...
Definition TxQ.h:76
std::uint32_t minimumTxnInLedgerSA
Like minimumTxnInLedger for standalone mode.
Definition TxQ.h:85
std::uint32_t slowConsensusDecreasePercent
When consensus takes longer than appropriate, the expected ledger size is updated to the lesser of th...
Definition TxQ.h:125
std::size_t queueSizeMin
The smallest limit the queue is allowed.
Definition TxQ.h:66
std::uint32_t minimumTxnInLedger
Minimum number of transactions to allow into the ledger before escalation, regardless of the prior le...
Definition TxQ.h:82
std::uint32_t normalConsensusIncreasePercent
When the ledger has more transactions than "expected", and performance is humming along nicely,...
Definition TxQ.h:111
Structure that describes a transaction in the queue waiting to be applied to the current open ledger.
Definition TxQ.h:174
TER preflightResult
The intermediate result returned by preflight before this transaction was queued, or after it is queu...
Definition TxQ.h:227
std::shared_ptr< STTx const > txn
The full transaction.
Definition TxQ.h:211
FeeLevel64 feeLevel
Fee level of the queued transaction.
Definition TxQ.h:199
SeqProxy seqProxy
SeqProxy of the transaction.
Definition TxQ.h:209
std::optional< LedgerIndex > lastValid
LastValidLedger field of the queued transaction, if any.
Definition TxQ.h:201
std::optional< TER > lastResult
If the transactor attempted to apply the transaction to the open ledger from the queue and failed,...
Definition TxQ.h:234
int retriesRemaining
Number of times the transactor can return a retry / ter result when attempting to apply this transact...
Definition TxQ.h:217
TxConsequences consequences
Potential TxConsequences of applying the queued transaction to the open ledger.
Definition TxQ.h:205
TxDetails(FeeLevel64 feeLevel, std::optional< LedgerIndex > const &lastValid, TxConsequences const &consequences, AccountID const &account, SeqProxy seqProxy, std::shared_ptr< STTx const > const &txn, int retriesRemaining, TER preflightResult, std::optional< TER > lastResult)
Full initialization.
Definition TxQ.h:176
AccountID account
The account the transaction is queued for.
Definition TxQ.h:207
T value_or(T... args)