rippled
Loading...
Searching...
No Matches
TxQ.h
1#pragma once
2
3#include <xrpld/app/tx/applySteps.h>
4
5#include <xrpl/ledger/ApplyView.h>
6#include <xrpl/ledger/OpenView.h>
7#include <xrpl/protocol/RippleLedgerHash.h>
8#include <xrpl/protocol/STTx.h>
9#include <xrpl/protocol/SeqProxy.h>
10#include <xrpl/protocol/TER.h>
11
12#include <boost/circular_buffer.hpp>
13#include <boost/intrusive/set.hpp>
14
15#include <optional>
16
17namespace xrpl {
18
19class Application;
20class Config;
21
40class TxQ
41{
42public:
44 static constexpr FeeLevel64 baseLevel{256};
45
139
168
175 {
178 FeeLevel64 feeLevel_,
179 std::optional<LedgerIndex> const& lastValid_,
180 TxConsequences const& consequences_,
181 AccountID const& account_,
182 SeqProxy seqProxy_,
183 std::shared_ptr<STTx const> const& txn_,
184 int retriesRemaining_,
185 TER preflightResult_,
186 std::optional<TER> lastResult_)
187 : feeLevel(feeLevel_)
188 , lastValid(lastValid_)
189 , consequences(consequences_)
190 , account(account_)
191 , seqProxy(seqProxy_)
192 , txn(txn_)
193 , retriesRemaining(retriesRemaining_)
194 , preflightResult(preflightResult_)
195 , lastResult(lastResult_)
196 {
197 }
198
236 };
237
239 TxQ(Setup const& setup, beast::Journal j);
240
242 virtual ~TxQ();
243
255
267 bool
268 accept(Application& app, OpenView& view);
269
282 void
283 processClosedLedger(Application& app, ReadView const& view, bool timeLeap);
284
287 nextQueuableSeq(std::shared_ptr<SLE const> const& sleAccount) const;
288
291 Metrics
292 getMetrics(OpenView const& view) const;
293
300
311 getTxRequiredFeeAndSeq(OpenView const& view, std::shared_ptr<STTx const> const& tx) const;
312
320 getAccountTxs(AccountID const& account) const;
321
329 getTxs() const;
330
336 doRPC(Application& app) const;
337
338private:
339 // Implementation for nextQueuableSeq(). The passed lock must be held.
342
349 {
350 private:
364 boost::circular_buffer<std::size_t> recentTxnCounts_;
370
371 public:
374 : minimumTxnCount_(setup.standAlone ? setup.minimumTxnInLedgerSA : setup.minimumTxnInLedger)
375 , targetTxnCount_(setup.targetTxnInLedger < minimumTxnCount_ ? minimumTxnCount_ : setup.targetTxnInLedger)
377 setup.maximumTxnInLedger
378 ? *setup.maximumTxnInLedger < targetTxnCount_ ? targetTxnCount_ : *setup.maximumTxnInLedger
379 : std::optional<std::size_t>(std::nullopt))
381 , recentTxnCounts_(setup.ledgersInQueue)
382 , escalationMultiplier_(setup.minimumEscalationMultiplier)
383 , j_(j)
384 {
385 }
386
398 update(Application& app, ReadView const& view, bool timeLeap, TxQ::Setup const& setup);
399
402 struct Snapshot
403 {
404 // Number of transactions expected per ledger.
405 // One more than this value will be accepted
406 // before escalation kicks in.
408 // Based on the median fee of the LCL. Used
409 // when fee escalation kicks in.
411 };
412
416 {
418 }
419
428 static FeeLevel64
429 scaleFeeLevel(Snapshot const& snapshot, OpenView const& view);
430
463 Snapshot const& snapshot,
464 OpenView const& view,
465 std::size_t extraCount,
466 std::size_t seriesSize);
467 };
468
474 {
475 public:
479 boost::intrusive::set_member_hook<> byFeeListHook;
480
483
487 TxID const txID;
525
540 static constexpr int retriesAllowed = 10;
541
551
552 public:
554 MaybeTx(
556 TxID const& txID,
558 ApplyFlags const flags,
560
564
567 TxConsequences const&
569 {
570 return pfResult->consequences;
571 }
572
576 {
577 return {
578 feeLevel,
579 lastValid,
580 consequences(),
581 account,
582 seqProxy,
583 txn,
585 pfResult->ter,
586 lastResult};
587 }
588 };
589
592 {
593 public:
595 explicit OrderCandidates() = default;
596
612 bool
613 operator()(MaybeTx const& lhs, MaybeTx const& rhs) const
614 {
615 if (lhs.feeLevel == rhs.feeLevel)
617 return lhs.feeLevel > rhs.feeLevel;
618 }
619 };
620
625 {
626 public:
628
633 /* If this account has had any transaction retry more than
634 `retriesAllowed` times so that it was dropped from the
635 queue, then all other transactions for this account will
636 be given at most 2 attempts before being removed. Helps
637 prevent wasting resources on retries that are more likely
638 to fail.
639 */
640 bool retryPenalty = false;
641 /* If this account has had any transaction fail or expire,
642 then when the queue is nearly full, transactions from
643 this account will be discarded. Helps prevent the queue
644 from getting filled and wedged.
645 */
646 bool dropPenalty = false;
647
648 public:
650 explicit TxQAccount(std::shared_ptr<STTx const> const& txn);
652 explicit TxQAccount(AccountID const& account);
653
657 {
658 return transactions.size();
659 }
660
662 bool
663 empty() const
664 {
665 return !getTxnCount();
666 }
667
669 TxMap::const_iterator
670 getPrevTx(SeqProxy seqProx) const;
671
673 MaybeTx&
674 add(MaybeTx&&);
675
681 bool
682 remove(SeqProxy seqProx);
683 };
684
685 // Helper function returns requiredFeeLevel.
688 OpenView& view,
689 ApplyFlags flags,
690 FeeMetrics::Snapshot const& metricsSnapshot,
691 std::lock_guard<std::mutex> const& lock) const;
692
693 // Helper function for TxQ::apply. If a transaction's fee is high enough,
694 // attempt to directly apply that transaction to the ledger.
697 Application& app,
698 OpenView& view,
700 ApplyFlags flags,
702
703 // Helper function that removes a replaced entry in _byFee.
706 std::optional<TxQAccount::TxMap::iterator> const& replacedTxIter,
708
709 using FeeHook =
710 boost::intrusive::member_hook<MaybeTx, boost::intrusive::set_member_hook<>, &MaybeTx::byFeeListHook>;
711
712 using FeeMultiSet = boost::intrusive::multiset<MaybeTx, FeeHook, boost::intrusive::compare<OrderCandidates>>;
713
715
720
746
751
756
757private:
759 template <size_t fillPercentage = 100>
760 bool
761 isFull() const;
762
766 TER
767 canBeHeld(
768 STTx const&,
769 ApplyFlags const,
770 OpenView const&,
771 std::shared_ptr<SLE const> const& sleAccount,
772 AccountMap::iterator const&,
774 std::lock_guard<std::mutex> const& lock);
775
777 FeeMultiSet::iterator_type erase(FeeMultiSet::const_iterator_type);
782 FeeMultiSet::iterator_type eraseAndAdvance(FeeMultiSet::const_iterator_type);
784 TxQAccount::TxMap::iterator
785 erase(TxQAccount& txQAccount, TxQAccount::TxMap::const_iterator begin, TxQAccount::TxMap::const_iterator end);
786
794 Application& app,
795 OpenView& view,
796 STTx const& tx,
797 AccountMap::iterator const& accountIter,
798 TxQAccount::TxMap::iterator,
799 FeeLevel64 feeLevelPaid,
800 PreflightResult const& pfResult,
801 std::size_t const txExtraCount,
802 ApplyFlags flags,
803 FeeMetrics::Snapshot const& metricsSnapshot,
805};
806
811setup_TxQ(Config const&);
812
813template <class T>
815toDrops(FeeLevel<T> const& level, XRPAmount baseFee)
816{
818}
819
820inline FeeLevel64
821toFeeLevel(XRPAmount const& drops, XRPAmount const& baseFee)
822{
824}
825
826} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
A generic endpoint for log messages.
Definition Journal.h:40
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 cMaxNativeN
Definition STAmount.h:56
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:37
Track and use the fee escalation metrics of the current open ledger.
Definition TxQ.h:349
Snapshot getSnapshot() const
Get the current Snapshot.
Definition TxQ.h:415
std::size_t txnsExpected_
Number of transactions expected per ledger.
Definition TxQ.h:361
std::size_t const targetTxnCount_
Number of transactions per ledger that fee escalation "works towards".
Definition TxQ.h:355
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:136
beast::Journal const j_
Journal.
Definition TxQ.h:369
std::optional< std::size_t > const maximumTxnCount_
Maximum value of txnsExpected.
Definition TxQ.h:357
FeeMetrics(Setup const &setup, beast::Journal j)
Constructor.
Definition TxQ.h:373
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:64
std::size_t const minimumTxnCount_
Minimum value of txnsExpected.
Definition TxQ.h:352
boost::circular_buffer< std::size_t > recentTxnCounts_
Recent history of transaction counts that exceed the targetTxnCount_.
Definition TxQ.h:364
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:194
FeeLevel64 escalationMultiplier_
Based on the median fee of the LCL.
Definition TxQ.h:367
Represents a transaction in the queue which may be applied later to the open ledger.
Definition TxQ.h:474
static LedgerHash parentHashComp
The hash of the parent ledger.
Definition TxQ.h:550
std::optional< LedgerIndex > const lastValid
Expiration ledger for the transaction (sfLastLedgerSequence field).
Definition TxQ.h:492
TxDetails getTxDetails() const
Return a TxDetails based on contained information.
Definition TxQ.h:575
static constexpr int retriesAllowed
Starting retry count for newly queued transactions.
Definition TxQ.h:540
TxID const txID
Transaction ID.
Definition TxQ.h:487
std::optional< TER > lastResult
If the transactor attempted to apply the transaction to the open ledger from the queue and failed,...
Definition TxQ.h:515
FeeLevel64 const feeLevel
Computed fee level that the transaction will pay.
Definition TxQ.h:485
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:479
TxConsequences const & consequences() const
Potential TxConsequences of applying this transaction to the open ledger.
Definition TxQ.h:568
ApplyFlags const flags
Flags provided to apply.
Definition TxQ.h:508
ApplyResult apply(Application &app, OpenView &view, beast::Journal j)
Attempt to apply the queued transaction to the open ledger.
Definition TxQ.cpp:257
SeqProxy const seqProxy
Transaction SeqProxy number (sfSequence or sfTicketSequence field).
Definition TxQ.h:495
std::shared_ptr< STTx const > txn
The complete transaction.
Definition TxQ.h:482
int retriesRemaining
A transaction at the front of the queue will be given several attempts to succeed before being droppe...
Definition TxQ.h:504
AccountID const account
Account submitting the transaction.
Definition TxQ.h:489
std::optional< PreflightResult const > pfResult
Cached result of the preflight operation.
Definition TxQ.h:524
Used for sorting MaybeTx.
Definition TxQ.h:592
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:613
Used to represent an account to the queue, and stores the transactions queued for that account by Seq...
Definition TxQ.h:625
TxMap::const_iterator getPrevTx(SeqProxy seqProx) const
Find the entry in transactions that precedes seqProx, if one does.
Definition TxQ.cpp:285
TxMap transactions
Sequence number will be used as the key.
Definition TxQ.h:632
bool empty() const
Checks if this account has no transactions queued.
Definition TxQ.h:663
MaybeTx & add(MaybeTx &&)
Add a transaction candidate to this account for queuing.
Definition TxQ.cpp:296
std::size_t getTxnCount() const
Return the number of transactions currently queued for this account.
Definition TxQ.h:656
bool remove(SeqProxy seqProx)
Remove the candidate with given SeqProxy value from this account.
Definition TxQ.cpp:308
AccountID const account
The account.
Definition TxQ.h:630
Transaction Queue.
Definition TxQ.h:41
Metrics getMetrics(OpenView const &view) const
Returns fee metrics in reference fee level units.
Definition TxQ.cpp:1603
FeeMetrics feeMetrics_
Tracks the current state of the queue.
Definition TxQ.h:725
FeeLevel64 getRequiredFeeLevel(OpenView &view, ApplyFlags flags, FeeMetrics::Snapshot const &metricsSnapshot, std::lock_guard< std::mutex > const &lock) const
Definition TxQ.cpp:1508
std::optional< size_t > maxSize_
Maximum number of transactions allowed in the queue based on the current metrics.
Definition TxQ.h:745
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:1236
SeqProxy nextQueuableSeqImpl(std::shared_ptr< SLE const > const &sleAccount, std::lock_guard< std::mutex > const &) const
Definition TxQ.cpp:1465
std::vector< TxDetails > getAccountTxs(AccountID const &account) const
Returns information about the transactions currently in the queue for the account.
Definition TxQ.cpp:1645
bool isFull() const
Is the queue at least fillPercentage full?
Definition TxQ.cpp:326
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:412
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:455
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:663
TER canBeHeld(STTx const &, ApplyFlags const, OpenView const &, std::shared_ptr< SLE const > const &sleAccount, AccountMap::iterator const &, std::optional< TxQAccount::TxMap::iterator > const &, std::lock_guard< std::mutex > const &lock)
Checks if the indicated transaction fits the conditions for being stored in the queue.
Definition TxQ.cpp:333
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:1624
std::optional< ApplyResult > tryDirectApply(Application &app, OpenView &view, std::shared_ptr< STTx const > const &tx, ApplyFlags flags, beast::Journal j)
Definition TxQ.cpp:1518
virtual ~TxQ()
Destructor.
Definition TxQ.cpp:319
static constexpr FeeLevel64 baseLevel
Fee level for single-signed reference transaction.
Definition TxQ.h:44
TxQAccount::TxMap::iterator erase(TxQAccount &txQAccount, TxQAccount::TxMap::const_iterator begin, TxQAccount::TxMap::const_iterator end)
Erase a range of items, based on TxQAccount::TxMap iterators.
bool accept(Application &app, OpenView &view)
Fill the new open ledger with transactions from the queue.
Definition TxQ.cpp:1303
std::mutex mutex_
Most queue operations are done under the master lock, but use this mutex for the RPC "fee" command,...
Definition TxQ.h:755
std::vector< TxDetails > getTxs() const
Returns information about all transactions currently in the queue.
Definition TxQ.cpp:1665
FeeMultiSet byFee_
The queue itself: the collection of transactions ordered by fee level.
Definition TxQ.h:731
beast::Journal const j_
Journal.
Definition TxQ.h:719
Json::Value doRPC(Application &app) const
Summarize current fee metrics for the fee RPC command.
Definition TxQ.cpp:1680
boost::intrusive::multiset< MaybeTx, FeeHook, boost::intrusive::compare< OrderCandidates > > FeeMultiSet
Definition TxQ.h:712
std::optional< TxQAccount::TxMap::iterator > removeFromByFee(std::optional< TxQAccount::TxMap::iterator > const &replacedTxIter, std::shared_ptr< STTx const > const &tx)
Definition TxQ.cpp:1583
LedgerHash parentHash_
parentHash_ used for logging only
Definition TxQ.h:750
FeeMultiSet::iterator_type erase(FeeMultiSet::const_iterator_type)
Erase and return the next entry in byFee_ (lower fee level)
SeqProxy nextQueuableSeq(std::shared_ptr< SLE const > const &sleAccount) const
Return the next sequence that would go in the TxQ for an account.
Definition TxQ.cpp:1452
boost::intrusive::member_hook< MaybeTx, boost::intrusive::set_member_hook<>, &MaybeTx::byFeeListHook > FeeHook
Definition TxQ.h:710
AccountMap byAccount_
All of the accounts which currently have any transactions in the queue.
Definition TxQ.h:738
Setup const setup_
Setup parameters used to control the behavior of the queue.
Definition TxQ.h:717
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
FeeLevel< std::uint64_t > FeeLevel64
Definition Units.h:418
std::optional< std::uint64_t > mulDiv(std::uint64_t value, std::uint64_t mul, std::uint64_t div)
Return value*mul/div accurately.
TxQ::Setup setup_TxQ(Config const &config)
Build a TxQ::Setup object from application configuration.
Definition TxQ.cpp:1733
XRPAmount toDrops(FeeLevel< T > const &level, XRPAmount baseFee)
Definition TxQ.h:815
FeeLevel64 toFeeLevel(XRPAmount const &drops, XRPAmount const &baseFee)
Definition TxQ.h:821
ApplyFlags
Definition ApplyView.h:10
T size(T... args)
Describes the results of the preflight check.
Definition applySteps.h:142
XRPAmount fee
Definition TxQ.h:296
std::uint32_t availableSeq
Definition TxQ.h:298
std::uint32_t accountSeq
Definition TxQ.h:297
Snapshot of the externally relevant FeeMetrics fields at any given time.
Definition TxQ.h:403
std::size_t const txnsExpected
Definition TxQ.h:407
FeeLevel64 const escalationMultiplier
Definition TxQ.h:410
Structure returned by TxQ::getMetrics, expressed in reference fee level units.
Definition TxQ.h:145
std::size_t txCount
Number of transactions in the queue.
Definition TxQ.h:150
Metrics()=default
Default constructor.
std::optional< std::size_t > txQMaxSize
Max transactions currently allowed in queue.
Definition TxQ.h:152
FeeLevel64 openLedgerFeeLevel
Minimum fee level to get into the current open ledger, bypassing the queue.
Definition TxQ.h:166
std::size_t txInLedger
Number of transactions currently in the open ledger.
Definition TxQ.h:154
FeeLevel64 minProcessingFeeLevel
Minimum fee level for a transaction to be considered for the open ledger or the queue.
Definition TxQ.h:161
FeeLevel64 referenceFeeLevel
Reference transaction fee level.
Definition TxQ.h:158
FeeLevel64 medFeeLevel
Median fee level of the last ledger.
Definition TxQ.h:163
std::size_t txPerLedger
Number of transactions expected per ledger.
Definition TxQ.h:156
Structure used to customize TxQ behavior.
Definition TxQ.h:50
bool standAlone
Use standalone mode behavior.
Definition TxQ.h:137
std::uint32_t maximumTxnPerAccount
Maximum number of transactions that can be queued by one account.
Definition TxQ.h:128
FeeLevel64 minimumEscalationMultiplier
Minimum value of the escalation multiplier, regardless of the prior ledger's median fee level.
Definition TxQ.h:80
std::optional< std::uint32_t > maximumTxnInLedger
Optional maximum allowed value of transactions per ledger before fee escalation kicks in.
Definition TxQ.h:100
Setup()=default
Default constructor.
std::uint32_t targetTxnInLedger
Number of transactions per ledger that fee escalation "works towards".
Definition TxQ.h:89
std::uint32_t minimumLastLedgerBuffer
Minimum difference between the current ledger sequence and a transaction's LastLedgerSequence for the...
Definition TxQ.h:135
std::size_t ledgersInQueue
Number of ledgers' worth of transactions to allow in the queue.
Definition TxQ.h:61
std::uint32_t retrySequencePercent
Extra percentage required on the fee level of a queued transaction to replace that transaction with a...
Definition TxQ.h:77
std::uint32_t minimumTxnInLedgerSA
Like minimumTxnInLedger for standalone mode.
Definition TxQ.h:86
std::uint32_t slowConsensusDecreasePercent
When consensus takes longer than appropriate, the expected ledger size is updated to the lesser of th...
Definition TxQ.h:126
std::size_t queueSizeMin
The smallest limit the queue is allowed.
Definition TxQ.h:67
std::uint32_t minimumTxnInLedger
Minimum number of transactions to allow into the ledger before escalation, regardless of the prior le...
Definition TxQ.h:83
std::uint32_t normalConsensusIncreasePercent
When the ledger has more transactions than "expected", and performance is humming along nicely,...
Definition TxQ.h:112
Structure that describes a transaction in the queue waiting to be applied to the current open ledger.
Definition TxQ.h:175
TER preflightResult
The intermediate result returned by preflight before this transaction was queued, or after it is queu...
Definition TxQ.h:228
std::shared_ptr< STTx const > txn
The full transaction.
Definition TxQ.h:212
FeeLevel64 feeLevel
Fee level of the queued transaction.
Definition TxQ.h:200
SeqProxy seqProxy
SeqProxy of the transaction.
Definition TxQ.h:210
std::optional< LedgerIndex > lastValid
LastValidLedger field of the queued transaction, if any.
Definition TxQ.h:202
std::optional< TER > lastResult
If the transactor attempted to apply the transaction to the open ledger from the queue and failed,...
Definition TxQ.h:235
int retriesRemaining
Number of times the transactor can return a retry / ter result when attempting to apply this transact...
Definition TxQ.h:218
TxConsequences consequences
Potential TxConsequences of applying the queued transaction to the open ledger.
Definition TxQ.h:206
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:177
AccountID account
The account the transaction is queued for.
Definition TxQ.h:208
T value_or(T... args)