rippled
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 baseLevel{256};
44
138
167
174 {
177 FeeLevel64 feeLevel_,
178 std::optional<LedgerIndex> const& lastValid_,
179 TxConsequences const& consequences_,
180 AccountID const& account_,
181 SeqProxy seqProxy_,
182 std::shared_ptr<STTx const> const& txn_,
183 int retriesRemaining_,
184 TER preflightResult_,
185 std::optional<TER> lastResult_)
186 : feeLevel(feeLevel_)
187 , lastValid(lastValid_)
188 , consequences(consequences_)
189 , account(account_)
190 , seqProxy(seqProxy_)
191 , txn(txn_)
192 , retriesRemaining(retriesRemaining_)
193 , preflightResult(preflightResult_)
194 , lastResult(lastResult_)
195 {
196 }
197
235 };
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(std::shared_ptr<SLE const> const& 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 std::shared_ptr<SLE const> const& sleAccount,
347 std::lock_guard<std::mutex> const&) const;
348
355 {
356 private:
370 boost::circular_buffer<std::size_t> recentTxnCounts_;
376
377 public:
381 setup.standAlone ? setup.minimumTxnInLedgerSA : setup.minimumTxnInLedger)
383 setup.targetTxnInLedger < minimumTxnCount_ ? minimumTxnCount_
384 : setup.targetTxnInLedger)
386 setup.maximumTxnInLedger ? *setup.maximumTxnInLedger < targetTxnCount_
388 : *setup.maximumTxnInLedger
389 : std::optional<std::size_t>(std::nullopt))
391 , recentTxnCounts_(setup.ledgersInQueue)
392 , escalationMultiplier_(setup.minimumEscalationMultiplier)
393 , j_(j)
394 {
395 }
396
408 update(Application& app, ReadView const& view, bool timeLeap, TxQ::Setup const& setup);
409
412 struct Snapshot
413 {
414 // Number of transactions expected per ledger.
415 // One more than this value will be accepted
416 // before escalation kicks in.
418 // Based on the median fee of the LCL. Used
419 // when fee escalation kicks in.
421 };
422
426 {
428 }
429
438 static FeeLevel64
439 scaleFeeLevel(Snapshot const& snapshot, OpenView const& view);
440
473 Snapshot const& snapshot,
474 OpenView const& view,
475 std::size_t extraCount,
476 std::size_t seriesSize);
477 };
478
484 {
485 public:
489 boost::intrusive::set_member_hook<> byFeeListHook;
490
493
497 TxID const txID;
535
550 static constexpr int retriesAllowed = 10;
551
561
562 public:
564 MaybeTx(
566 TxID const& txID,
568 ApplyFlags const flags,
570
574
577 TxConsequences const&
579 {
580 return pfResult->consequences;
581 }
582
586 {
587 return {
588 feeLevel,
589 lastValid,
590 consequences(),
591 account,
592 seqProxy,
593 txn,
595 pfResult->ter,
596 lastResult};
597 }
598 };
599
602 {
603 public:
605 explicit OrderCandidates() = default;
606
622 bool
623 operator()(MaybeTx const& lhs, MaybeTx const& rhs) const
624 {
625 if (lhs.feeLevel == rhs.feeLevel)
627 return lhs.feeLevel > rhs.feeLevel;
628 }
629 };
630
635 {
636 public:
638
643 /* If this account has had any transaction retry more than
644 `retriesAllowed` times so that it was dropped from the
645 queue, then all other transactions for this account will
646 be given at most 2 attempts before being removed. Helps
647 prevent wasting resources on retries that are more likely
648 to fail.
649 */
650 bool retryPenalty = false;
651 /* If this account has had any transaction fail or expire,
652 then when the queue is nearly full, transactions from
653 this account will be discarded. Helps prevent the queue
654 from getting filled and wedged.
655 */
656 bool dropPenalty = false;
657
658 public:
660 explicit TxQAccount(std::shared_ptr<STTx const> const& txn);
662 explicit TxQAccount(AccountID const& account);
663
667 {
668 return transactions.size();
669 }
670
672 bool
673 empty() const
674 {
675 return !getTxnCount();
676 }
677
679 TxMap::const_iterator
680 getPrevTx(SeqProxy seqProx) const;
681
683 MaybeTx&
684 add(MaybeTx&&);
685
691 bool
692 remove(SeqProxy seqProx);
693 };
694
695 // Helper function returns requiredFeeLevel.
696 static FeeLevel64
698 OpenView& view,
699 ApplyFlags flags,
700 FeeMetrics::Snapshot const& metricsSnapshot,
701 std::lock_guard<std::mutex> const& lock);
702
703 // Helper function for TxQ::apply. If a transaction's fee is high enough,
704 // attempt to directly apply that transaction to the ledger.
707 Application& app,
708 OpenView& view,
710 ApplyFlags flags,
712
713 // Helper function that removes a replaced entry in _byFee.
716 std::optional<TxQAccount::TxMap::iterator> const& replacedTxIter,
718
719 using FeeHook = boost::intrusive::
720 member_hook<MaybeTx, boost::intrusive::set_member_hook<>, &MaybeTx::byFeeListHook>;
721
723 boost::intrusive::multiset<MaybeTx, FeeHook, boost::intrusive::compare<OrderCandidates>>;
724
726
731
757
762
767
768private:
770 template <size_t fillPercentage = 100>
771 bool
772 isFull() const;
773
777 TER
778 canBeHeld(
779 STTx const&,
780 ApplyFlags const,
781 OpenView const&,
782 std::shared_ptr<SLE const> const& sleAccount,
783 AccountMap::iterator const&,
785 std::lock_guard<std::mutex> const& lock);
786
788 FeeMultiSet::iterator_type erase(FeeMultiSet::const_iterator_type);
793 FeeMultiSet::iterator_type eraseAndAdvance(FeeMultiSet::const_iterator_type);
795 TxQAccount::TxMap::iterator
797 TxQAccount& txQAccount,
798 TxQAccount::TxMap::const_iterator begin,
799 TxQAccount::TxMap::const_iterator end);
800
808 Application& app,
809 OpenView& view,
810 STTx const& tx,
811 AccountMap::iterator const& accountIter,
812 TxQAccount::TxMap::iterator,
813 FeeLevel64 feeLevelPaid,
814 PreflightResult const& pfResult,
815 std::size_t const txExtraCount,
816 ApplyFlags flags,
817 FeeMetrics::Snapshot const& metricsSnapshot,
819};
820
825setup_TxQ(Config const&);
826
827template <class T>
829toDrops(FeeLevel<T> const& level, XRPAmount baseFee)
830{
832}
833
834inline FeeLevel64
835toFeeLevel(XRPAmount const& drops, XRPAmount const& baseFee)
836{
837 return mulDiv(drops, TxQ::baseLevel, baseFee)
839}
840
841} // 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:38
Track and use the fee escalation metrics of the current open ledger.
Definition TxQ.h:355
Snapshot getSnapshot() const
Get the current Snapshot.
Definition TxQ.h:425
std::size_t txnsExpected_
Number of transactions expected per ledger.
Definition TxQ.h:367
std::size_t const targetTxnCount_
Number of transactions per ledger that fee escalation "works towards".
Definition TxQ.h:361
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:145
beast::Journal const j_
Journal.
Definition TxQ.h:375
std::optional< std::size_t > const maximumTxnCount_
Maximum value of txnsExpected.
Definition TxQ.h:363
FeeMetrics(Setup const &setup, beast::Journal j)
Constructor.
Definition TxQ.h:379
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
std::size_t const minimumTxnCount_
Minimum value of txnsExpected.
Definition TxQ.h:358
boost::circular_buffer< std::size_t > recentTxnCounts_
Recent history of transaction counts that exceed the targetTxnCount_.
Definition TxQ.h:370
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:203
FeeLevel64 escalationMultiplier_
Based on the median fee of the LCL.
Definition TxQ.h:373
Represents a transaction in the queue which may be applied later to the open ledger.
Definition TxQ.h:484
static LedgerHash parentHashComp
The hash of the parent ledger.
Definition TxQ.h:560
std::optional< LedgerIndex > const lastValid
Expiration ledger for the transaction (sfLastLedgerSequence field).
Definition TxQ.h:502
TxDetails getTxDetails() const
Return a TxDetails based on contained information.
Definition TxQ.h:585
static constexpr int retriesAllowed
Starting retry count for newly queued transactions.
Definition TxQ.h:550
TxID const txID
Transaction ID.
Definition TxQ.h:497
std::optional< TER > lastResult
If the transactor attempted to apply the transaction to the open ledger from the queue and failed,...
Definition TxQ.h:525
FeeLevel64 const feeLevel
Computed fee level that the transaction will pay.
Definition TxQ.h:495
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:489
TxConsequences const & consequences() const
Potential TxConsequences of applying this transaction to the open ledger.
Definition TxQ.h:578
ApplyFlags const flags
Flags provided to apply.
Definition TxQ.h:518
ApplyResult apply(Application &app, OpenView &view, beast::Journal j)
Attempt to apply the queued transaction to the open ledger.
Definition TxQ.cpp:266
SeqProxy const seqProxy
Transaction SeqProxy number (sfSequence or sfTicketSequence field).
Definition TxQ.h:505
std::shared_ptr< STTx const > txn
The complete transaction.
Definition TxQ.h:492
int retriesRemaining
A transaction at the front of the queue will be given several attempts to succeed before being droppe...
Definition TxQ.h:514
AccountID const account
Account submitting the transaction.
Definition TxQ.h:499
std::optional< PreflightResult const > pfResult
Cached result of the preflight operation.
Definition TxQ.h:534
Used for sorting MaybeTx.
Definition TxQ.h:602
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:623
Used to represent an account to the queue, and stores the transactions queued for that account by Seq...
Definition TxQ.h:635
TxMap::const_iterator getPrevTx(SeqProxy seqProx) const
Find the entry in transactions that precedes seqProx, if one does.
Definition TxQ.cpp:296
TxMap transactions
Sequence number will be used as the key.
Definition TxQ.h:642
bool empty() const
Checks if this account has no transactions queued.
Definition TxQ.h:673
MaybeTx & add(MaybeTx &&)
Add a transaction candidate to this account for queuing.
Definition TxQ.cpp:307
std::size_t getTxnCount() const
Return the number of transactions currently queued for this account.
Definition TxQ.h:666
bool remove(SeqProxy seqProx)
Remove the candidate with given SeqProxy value from this account.
Definition TxQ.cpp:320
AccountID const account
The account.
Definition TxQ.h:640
Transaction Queue.
Definition TxQ.h:40
boost::intrusive::member_hook< MaybeTx, boost::intrusive::set_member_hook<>, &MaybeTx::byFeeListHook > FeeHook
Definition TxQ.h:720
Metrics getMetrics(OpenView const &view) const
Returns fee metrics in reference fee level units.
Definition TxQ.cpp:1692
FeeMetrics feeMetrics_
Tracks the current state of the queue.
Definition TxQ.h:736
std::optional< size_t > maxSize_
Maximum number of transactions allowed in the queue based on the current metrics.
Definition TxQ.h:756
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:1287
SeqProxy nextQueuableSeqImpl(std::shared_ptr< SLE const > const &sleAccount, std::lock_guard< std::mutex > const &) const
Definition TxQ.cpp:1543
std::vector< TxDetails > getAccountTxs(AccountID const &account) const
Returns information about the transactions currently in the queue for the account.
Definition TxQ.cpp:1735
bool isFull() const
Is the queue at least fillPercentage full?
Definition TxQ.cpp:339
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:432
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:478
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:686
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:346
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:1713
std::optional< ApplyResult > tryDirectApply(Application &app, OpenView &view, std::shared_ptr< STTx const > const &tx, ApplyFlags flags, beast::Journal j)
Definition TxQ.cpp:1600
virtual ~TxQ()
Destructor.
Definition TxQ.cpp:332
static constexpr FeeLevel64 baseLevel
Fee level for single-signed reference transaction.
Definition TxQ.h:43
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:1358
std::mutex mutex_
Most queue operations are done under the master lock, but use this mutex for the RPC "fee" command,...
Definition TxQ.h:766
std::vector< TxDetails > getTxs() const
Returns information about all transactions currently in the queue.
Definition TxQ.cpp:1755
FeeMultiSet byFee_
The queue itself: the collection of transactions ordered by fee level.
Definition TxQ.h:742
beast::Journal const j_
Journal.
Definition TxQ.h:730
Json::Value doRPC(Application &app) const
Summarize current fee metrics for the fee RPC command.
Definition TxQ.cpp:1770
boost::intrusive::multiset< MaybeTx, FeeHook, boost::intrusive::compare< OrderCandidates > > FeeMultiSet
Definition TxQ.h:723
static FeeLevel64 getRequiredFeeLevel(OpenView &view, ApplyFlags flags, FeeMetrics::Snapshot const &metricsSnapshot, std::lock_guard< std::mutex > const &lock)
Definition TxQ.cpp:1590
std::optional< TxQAccount::TxMap::iterator > removeFromByFee(std::optional< TxQAccount::TxMap::iterator > const &replacedTxIter, std::shared_ptr< STTx const > const &tx)
Definition TxQ.cpp:1666
LedgerHash parentHash_
parentHash_ used for logging only
Definition TxQ.h:761
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:1530
AccountMap byAccount_
All of the accounts which currently have any transactions in the queue.
Definition TxQ.h:749
Setup const setup_
Setup parameters used to control the behavior of the queue.
Definition TxQ.h:728
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:426
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:1824
XRPAmount toDrops(FeeLevel< T > const &level, XRPAmount baseFee)
Definition TxQ.h:829
FeeLevel64 toFeeLevel(XRPAmount const &drops, XRPAmount const &baseFee)
Definition TxQ.h:835
ApplyFlags
Definition ApplyView.h:10
T size(T... args)
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:413
std::size_t const txnsExpected
Definition TxQ.h:417
FeeLevel64 const escalationMultiplier
Definition TxQ.h:420
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 "works towards".
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)