rippled
Loading...
Searching...
No Matches
TxQ.h
1#ifndef XRPL_TXQ_H_INCLUDED
2#define XRPL_TXQ_H_INCLUDED
3
4#include <xrpld/app/tx/applySteps.h>
5
6#include <xrpl/ledger/ApplyView.h>
7#include <xrpl/ledger/OpenView.h>
8#include <xrpl/protocol/RippleLedgerHash.h>
9#include <xrpl/protocol/STTx.h>
10#include <xrpl/protocol/SeqProxy.h>
11#include <xrpl/protocol/TER.h>
12
13#include <boost/circular_buffer.hpp>
14#include <boost/intrusive/set.hpp>
15
16#include <optional>
17
18namespace ripple {
19
20class Application;
21class Config;
22
41class TxQ
42{
43public:
45 static constexpr FeeLevel64 baseLevel{256};
46
140
169
176 {
179 FeeLevel64 feeLevel_,
180 std::optional<LedgerIndex> const& lastValid_,
181 TxConsequences const& consequences_,
182 AccountID const& account_,
183 SeqProxy seqProxy_,
184 std::shared_ptr<STTx const> const& txn_,
185 int retriesRemaining_,
186 TER preflightResult_,
187 std::optional<TER> lastResult_)
188 : feeLevel(feeLevel_)
189 , lastValid(lastValid_)
190 , consequences(consequences_)
191 , account(account_)
192 , seqProxy(seqProxy_)
193 , txn(txn_)
194 , retriesRemaining(retriesRemaining_)
195 , preflightResult(preflightResult_)
196 , lastResult(lastResult_)
197 {
198 }
199
237 };
238
240 TxQ(Setup const& setup, beast::Journal j);
241
243 virtual ~TxQ();
244
255 apply(
256 Application& app,
257 OpenView& view,
259 ApplyFlags flags,
261
273 bool
274 accept(Application& app, OpenView& view);
275
288 void
289 processClosedLedger(Application& app, ReadView const& view, bool timeLeap);
290
293 nextQueuableSeq(std::shared_ptr<SLE const> const& sleAccount) const;
294
297 Metrics
298 getMetrics(OpenView const& view) const;
299
306
318 OpenView const& view,
319 std::shared_ptr<STTx const> const& tx) const;
320
328 getAccountTxs(AccountID const& account) const;
329
337 getTxs() const;
338
344 doRPC(Application& app) const;
345
346private:
347 // Implementation for nextQueuableSeq(). The passed lock must be held.
350 std::shared_ptr<SLE const> const& sleAccount,
351 std::lock_guard<std::mutex> const&) const;
352
359 {
360 private:
374 boost::circular_buffer<std::size_t> recentTxnCounts_;
380
381 public:
385 setup.standAlone ? setup.minimumTxnInLedgerSA
386 : setup.minimumTxnInLedger)
388 setup.targetTxnInLedger < minimumTxnCount_
390 : setup.targetTxnInLedger)
392 setup.maximumTxnInLedger
393 ? *setup.maximumTxnInLedger < targetTxnCount_
395 : *setup.maximumTxnInLedger
396 : std::optional<std::size_t>(std::nullopt))
398 , recentTxnCounts_(setup.ledgersInQueue)
399 , escalationMultiplier_(setup.minimumEscalationMultiplier)
400 , j_(j)
401 {
402 }
403
415 update(
416 Application& app,
417 ReadView const& view,
418 bool timeLeap,
419 TxQ::Setup const& setup);
420
423 struct Snapshot
424 {
425 // Number of transactions expected per ledger.
426 // One more than this value will be accepted
427 // before escalation kicks in.
429 // Based on the median fee of the LCL. Used
430 // when fee escalation kicks in.
432 };
433
437 {
439 }
440
449 static FeeLevel64
450 scaleFeeLevel(Snapshot const& snapshot, OpenView const& view);
451
484 Snapshot const& snapshot,
485 OpenView const& view,
486 std::size_t extraCount,
487 std::size_t seriesSize);
488 };
489
495 {
496 public:
500 boost::intrusive::set_member_hook<> byFeeListHook;
501
504
508 TxID const txID;
546
561 static constexpr int retriesAllowed = 10;
562
572
573 public:
575 MaybeTx(
577 TxID const& txID,
579 ApplyFlags const flags,
581
585
588 TxConsequences const&
590 {
591 return pfresult->consequences;
592 }
593
597 {
598 return {
599 feeLevel,
600 lastValid,
601 consequences(),
602 account,
603 seqProxy,
604 txn,
606 pfresult->ter,
607 lastResult};
608 }
609 };
610
613 {
614 public:
616 explicit OrderCandidates() = default;
617
633 bool
634 operator()(MaybeTx const& lhs, MaybeTx const& rhs) const
635 {
636 if (lhs.feeLevel == rhs.feeLevel)
637 return (lhs.txID ^ MaybeTx::parentHashComp) <
639 return lhs.feeLevel > rhs.feeLevel;
640 }
641 };
642
647 {
648 public:
650
655 /* If this account has had any transaction retry more than
656 `retriesAllowed` times so that it was dropped from the
657 queue, then all other transactions for this account will
658 be given at most 2 attempts before being removed. Helps
659 prevent wasting resources on retries that are more likely
660 to fail.
661 */
662 bool retryPenalty = false;
663 /* If this account has had any transaction fail or expire,
664 then when the queue is nearly full, transactions from
665 this account will be discarded. Helps prevent the queue
666 from getting filled and wedged.
667 */
668 bool dropPenalty = false;
669
670 public:
672 explicit TxQAccount(std::shared_ptr<STTx const> const& txn);
674 explicit TxQAccount(AccountID const& account);
675
679 {
680 return transactions.size();
681 }
682
684 bool
685 empty() const
686 {
687 return !getTxnCount();
688 }
689
691 TxMap::const_iterator
692 getPrevTx(SeqProxy seqProx) const;
693
695 MaybeTx&
696 add(MaybeTx&&);
697
703 bool
704 remove(SeqProxy seqProx);
705 };
706
707 // Helper function returns requiredFeeLevel.
710 OpenView& view,
711 ApplyFlags flags,
712 FeeMetrics::Snapshot const& metricsSnapshot,
713 std::lock_guard<std::mutex> const& lock) const;
714
715 // Helper function for TxQ::apply. If a transaction's fee is high enough,
716 // attempt to directly apply that transaction to the ledger.
719 Application& app,
720 OpenView& view,
722 ApplyFlags flags,
724
725 // Helper function that removes a replaced entry in _byFee.
728 std::optional<TxQAccount::TxMap::iterator> const& replacedTxIter,
730
731 using FeeHook = boost::intrusive::member_hook<
732 MaybeTx,
733 boost::intrusive::set_member_hook<>,
735
736 using FeeMultiSet = boost::intrusive::
737 multiset<MaybeTx, FeeHook, boost::intrusive::compare<OrderCandidates>>;
738
740
745
771
776
781
782private:
784 template <size_t fillPercentage = 100>
785 bool
786 isFull() const;
787
791 TER
792 canBeHeld(
793 STTx const&,
794 ApplyFlags const,
795 OpenView const&,
796 std::shared_ptr<SLE const> const& sleAccount,
797 AccountMap::iterator const&,
799 std::lock_guard<std::mutex> const& lock);
800
802 FeeMultiSet::iterator_type erase(FeeMultiSet::const_iterator_type);
807 FeeMultiSet::iterator_type eraseAndAdvance(
808 FeeMultiSet::const_iterator_type);
810 TxQAccount::TxMap::iterator
812 TxQAccount& txQAccount,
813 TxQAccount::TxMap::const_iterator begin,
814 TxQAccount::TxMap::const_iterator end);
815
823 Application& app,
824 OpenView& view,
825 STTx const& tx,
826 AccountMap::iterator const& accountIter,
827 TxQAccount::TxMap::iterator,
828 FeeLevel64 feeLevelPaid,
829 PreflightResult const& pfresult,
830 std::size_t const txExtraCount,
831 ApplyFlags flags,
832 FeeMetrics::Snapshot const& metricsSnapshot,
834};
835
840setup_TxQ(Config const&);
841
842template <class T>
844toDrops(FeeLevel<T> const& level, XRPAmount baseFee)
845{
846 return mulDiv(level, baseFee, TxQ::baseLevel)
848}
849
850inline FeeLevel64
851toFeeLevel(XRPAmount const& drops, XRPAmount const& baseFee)
852{
853 return mulDiv(drops, TxQ::baseLevel, baseFee)
855}
856
857} // namespace ripple
858
859#endif
Represents a JSON value.
Definition json_value.h:131
A generic endpoint for log messages.
Definition Journal.h:41
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:46
A view into a ledger.
Definition ReadView.h:32
static std::uint64_t const cMaxNativeN
Definition STAmount.h:55
A type that represents either a sequence value or a ticket value.
Definition SeqProxy.h:37
Class describing the consequences to the account of applying a transaction if the transaction consume...
Definition applySteps.h:39
Track and use the fee escalation metrics of the current open ledger.
Definition TxQ.h:359
std::size_t txnsExpected_
Number of transactions expected per ledger.
Definition TxQ.h:371
beast::Journal const j_
Journal.
Definition TxQ.h:379
FeeMetrics(Setup const &setup, beast::Journal j)
Constructor.
Definition TxQ.h:383
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:154
std::size_t const minimumTxnCount_
Minimum value of txnsExpected.
Definition TxQ.h:362
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:215
Snapshot getSnapshot() const
Get the current Snapshot.
Definition TxQ.h:436
std::optional< std::size_t > const maximumTxnCount_
Maximum value of txnsExpected.
Definition TxQ.h:367
std::size_t const targetTxnCount_
Number of transactions per ledger that fee escalation "works towards".
Definition TxQ.h:365
boost::circular_buffer< std::size_t > recentTxnCounts_
Recent history of transaction counts that exceed the targetTxnCount_.
Definition TxQ.h:374
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:65
FeeLevel64 escalationMultiplier_
Based on the median fee of the LCL.
Definition TxQ.h:377
Represents a transaction in the queue which may be applied later to the open ledger.
Definition TxQ.h:495
SeqProxy const seqProxy
Transaction SeqProxy number (sfSequence or sfTicketSequence field).
Definition TxQ.h:516
ApplyResult apply(Application &app, OpenView &view, beast::Journal j)
Attempt to apply the queued transaction to the open ledger.
Definition TxQ.cpp:279
ApplyFlags const flags
Flags provided to apply.
Definition TxQ.h:529
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:500
int retriesRemaining
A transaction at the front of the queue will be given several attempts to succeed before being droppe...
Definition TxQ.h:525
FeeLevel64 const feeLevel
Computed fee level that the transaction will pay.
Definition TxQ.h:506
std::optional< PreflightResult const > pfresult
Cached result of the preflight operation.
Definition TxQ.h:545
static constexpr int retriesAllowed
Starting retry count for newly queued transactions.
Definition TxQ.h:561
AccountID const account
Account submitting the transaction.
Definition TxQ.h:510
TxDetails getTxDetails() const
Return a TxDetails based on contained information.
Definition TxQ.h:596
std::shared_ptr< STTx const > txn
The complete transaction.
Definition TxQ.h:503
std::optional< LedgerIndex > const lastValid
Expiration ledger for the transaction (sfLastLedgerSequence field).
Definition TxQ.h:513
TxID const txID
Transaction ID.
Definition TxQ.h:508
static LedgerHash parentHashComp
The hash of the parent ledger.
Definition TxQ.h:571
std::optional< TER > lastResult
If the transactor attempted to apply the transaction to the open ledger from the queue and failed,...
Definition TxQ.h:536
TxConsequences const & consequences() const
Potential TxConsequences of applying this transaction to the open ledger.
Definition TxQ.h:589
Used for sorting MaybeTx.
Definition TxQ.h:613
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:634
Used to represent an account to the queue, and stores the transactions queued for that account by Seq...
Definition TxQ.h:647
TxMap transactions
Sequence number will be used as the key.
Definition TxQ.h:654
std::size_t getTxnCount() const
Return the number of transactions currently queued for this account.
Definition TxQ.h:678
AccountID const account
The account.
Definition TxQ.h:652
TxMap::const_iterator getPrevTx(SeqProxy seqProx) const
Find the entry in transactions that precedes seqProx, if one does.
Definition TxQ.cpp:311
bool remove(SeqProxy seqProx)
Remove the candidate with given SeqProxy value from this account.
Definition TxQ.cpp:337
MaybeTx & add(MaybeTx &&)
Add a transaction candidate to this account for queuing.
Definition TxQ.cpp:322
bool empty() const
Checks if this account has no transactions queued.
Definition TxQ.h:685
Transaction Queue.
Definition TxQ.h:42
boost::intrusive::multiset< MaybeTx, FeeHook, boost::intrusive::compare< OrderCandidates > > FeeMultiSet
Definition TxQ.h:737
std::vector< TxDetails > getTxs() const
Returns information about all transactions currently in the queue.
Definition TxQ.cpp:1824
std::optional< TxQAccount::TxMap::iterator > removeFromByFee(std::optional< TxQAccount::TxMap::iterator > const &replacedTxIter, std::shared_ptr< STTx const > const &tx)
Definition TxQ.cpp:1729
std::optional< size_t > maxSize_
Maximum number of transactions allowed in the queue based on the current metrics.
Definition TxQ.h:770
FeeMultiSet::iterator_type erase(FeeMultiSet::const_iterator_type)
Erase and return the next entry in byFee_ (lower fee level)
Metrics getMetrics(OpenView const &view) const
Returns fee metrics in reference fee level units.
Definition TxQ.cpp:1757
Json::Value doRPC(Application &app) const
Summarize current fee metrics for the fee RPC command.
Definition TxQ.cpp:1839
FeeMultiSet byFee_
The queue itself: the collection of transactions ordered by fee level.
Definition TxQ.h:756
std::vector< TxDetails > getAccountTxs(AccountID const &account) const
Returns information about the transactions currently in the queue for the account.
Definition TxQ.cpp:1803
beast::Journal const j_
Journal.
Definition TxQ.h:744
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:364
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:1588
std::mutex mutex_
Most queue operations are done under the master lock, but use this mutex for the RPC "fee" command,...
Definition TxQ.h:780
AccountMap byAccount_
All of the accounts which currently have any transactions in the queue.
Definition TxQ.h:763
boost::intrusive::member_hook< MaybeTx, boost::intrusive::set_member_hook<>, &MaybeTx::byFeeListHook > FeeHook
Definition TxQ.h:734
LedgerHash parentHash_
parentHash_ used for logging only
Definition TxQ.h:775
SeqProxy nextQueuableSeqImpl(std::shared_ptr< SLE const > const &sleAccount, std::lock_guard< std::mutex > const &) const
Definition TxQ.cpp:1601
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:498
bool isFull() const
Is the queue at least fillPercentage full?
Definition TxQ.cpp:356
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:1779
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:446
FeeMetrics feeMetrics_
Tracks the current state of the queue.
Definition TxQ.h:750
virtual ~TxQ()
Destructor.
Definition TxQ.cpp:349
FeeLevel64 getRequiredFeeLevel(OpenView &view, ApplyFlags flags, FeeMetrics::Snapshot const &metricsSnapshot, std::lock_guard< std::mutex > const &lock) const
Definition TxQ.cpp:1648
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:1414
static constexpr FeeLevel64 baseLevel
Fee level for single-signed reference transaction.
Definition TxQ.h:45
Setup const setup_
Setup parameters used to control the behavior of the queue.
Definition TxQ.h:742
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:1345
std::optional< ApplyResult > tryDirectApply(Application &app, OpenView &view, std::shared_ptr< STTx const > const &tx, ApplyFlags flags, beast::Journal j)
Definition TxQ.cpp:1658
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:710
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
TxQ::Setup setup_TxQ(Config const &config)
Build a TxQ::Setup object from application configuration.
Definition TxQ.cpp:1894
FeeLevel64 toFeeLevel(XRPAmount const &drops, XRPAmount const &baseFee)
Definition TxQ.h:851
FeeLevel< std::uint64_t > FeeLevel64
Definition Units.h:433
XRPAmount toDrops(FeeLevel< T > const &level, XRPAmount baseFee)
Definition TxQ.h:844
std::optional< std::uint64_t > mulDiv(std::uint64_t value, std::uint64_t mul, std::uint64_t div)
Return value*mul/div accurately.
STL namespace.
T size(T... args)
Describes the results of the preflight check.
Definition applySteps.h:144
XRPAmount fee
Definition TxQ.h:302
std::uint32_t availableSeq
Definition TxQ.h:304
std::uint32_t accountSeq
Definition TxQ.h:303
Snapshot of the externally relevant FeeMetrics fields at any given time.
Definition TxQ.h:424
std::size_t const txnsExpected
Definition TxQ.h:428
FeeLevel64 const escalationMultiplier
Definition TxQ.h:431
Structure returned by TxQ::getMetrics, expressed in reference fee level units.
Definition TxQ.h:146
FeeLevel64 minProcessingFeeLevel
Minimum fee level for a transaction to be considered for the open ledger or the queue.
Definition TxQ.h:162
FeeLevel64 openLedgerFeeLevel
Minimum fee level to get into the current open ledger, bypassing the queue.
Definition TxQ.h:167
std::size_t txPerLedger
Number of transactions expected per ledger.
Definition TxQ.h:157
Metrics()=default
Default constructor.
std::optional< std::size_t > txQMaxSize
Max transactions currently allowed in queue.
Definition TxQ.h:153
FeeLevel64 referenceFeeLevel
Reference transaction fee level.
Definition TxQ.h:159
std::size_t txInLedger
Number of transactions currently in the open ledger.
Definition TxQ.h:155
std::size_t txCount
Number of transactions in the queue.
Definition TxQ.h:151
FeeLevel64 medFeeLevel
Median fee level of the last ledger.
Definition TxQ.h:164
Structure used to customize TxQ behavior.
Definition TxQ.h:51
std::uint32_t slowConsensusDecreasePercent
When consensus takes longer than appropriate, the expected ledger size is updated to the lesser of th...
Definition TxQ.h:127
Setup()=default
Default constructor.
std::uint32_t minimumTxnInLedger
Minimum number of transactions to allow into the ledger before escalation, regardless of the prior le...
Definition TxQ.h:84
std::uint32_t maximumTxnPerAccount
Maximum number of transactions that can be queued by one account.
Definition TxQ.h:129
FeeLevel64 minimumEscalationMultiplier
Minimum value of the escalation multiplier, regardless of the prior ledger's median fee level.
Definition TxQ.h:81
std::size_t queueSizeMin
The smallest limit the queue is allowed.
Definition TxQ.h:68
std::optional< std::uint32_t > maximumTxnInLedger
Optional maximum allowed value of transactions per ledger before fee escalation kicks in.
Definition TxQ.h:101
std::uint32_t targetTxnInLedger
Number of transactions per ledger that fee escalation "works towards".
Definition TxQ.h:90
std::uint32_t retrySequencePercent
Extra percentage required on the fee level of a queued transaction to replace that transaction with a...
Definition TxQ.h:78
std::uint32_t minimumLastLedgerBuffer
Minimum difference between the current ledger sequence and a transaction's LastLedgerSequence for the...
Definition TxQ.h:136
std::uint32_t minimumTxnInLedgerSA
Like minimumTxnInLedger for standalone mode.
Definition TxQ.h:87
std::size_t ledgersInQueue
Number of ledgers' worth of transactions to allow in the queue.
Definition TxQ.h:62
bool standAlone
Use standalone mode behavior.
Definition TxQ.h:138
std::uint32_t normalConsensusIncreasePercent
When the ledger has more transactions than "expected", and performance is humming along nicely,...
Definition TxQ.h:113
Structure that describes a transaction in the queue waiting to be applied to the current open ledger.
Definition TxQ.h:176
std::optional< LedgerIndex > lastValid
LastValidLedger field of the queued transaction, if any.
Definition TxQ.h:203
SeqProxy seqProxy
SeqProxy of the transaction.
Definition TxQ.h:211
TER preflightResult
The intermediate result returned by preflight before this transaction was queued, or after it is queu...
Definition TxQ.h:229
TxConsequences consequences
Potential TxConsequences of applying the queued transaction to the open ledger.
Definition TxQ.h:207
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:178
AccountID account
The account the transaction is queued for.
Definition TxQ.h:209
FeeLevel64 feeLevel
Fee level of the queued transaction.
Definition TxQ.h:201
std::shared_ptr< STTx const > txn
The full transaction.
Definition TxQ.h:213
std::optional< TER > lastResult
If the transactor attempted to apply the transaction to the open ledger from the queue and failed,...
Definition TxQ.h:236
int retriesRemaining
Number of times the transactor can return a retry / ter result when attempting to apply this transact...
Definition TxQ.h:219
T value_or(T... args)