mempool

package
v0.50.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 16, 2024 License: Apache-2.0 Imports: 11 Imported by: 86

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTxNotFound           = errors.New("tx not found in mempool")
	ErrMempoolTxMaxCapacity = errors.New("pool reached max tx capacity")
)
View Source
var DefaultMaxTx = -1

Functions

func IsEmpty

func IsEmpty[C comparable](mempool Mempool) error

func SelectBy added in v0.50.10

func SelectBy(ctx context.Context, mempool Mempool, txs [][]byte, callback func(sdk.Tx) bool)

SelectBy is compatible with old interface to avoid breaking api. In v0.52+, this function is removed and SelectBy is merged into Mempool interface.

Types

type DefaultSignerExtractionAdapter added in v0.50.3

type DefaultSignerExtractionAdapter struct{}

DefaultSignerExtractionAdapter is the default implementation of SignerExtractionAdapter. It extracts the signers from a cosmos-sdk tx via GetSignaturesV2.

func NewDefaultSignerExtractionAdapter added in v0.50.3

func NewDefaultSignerExtractionAdapter() DefaultSignerExtractionAdapter

NewDefaultSignerExtractionAdapter constructs a new DefaultSignerExtractionAdapter instance

func (DefaultSignerExtractionAdapter) GetSigners added in v0.50.3

GetSigners implements the Adapter interface

type ExtMempool added in v0.50.10

type ExtMempool interface {
	Mempool

	// SelectBy use callback to iterate over the mempool, it's thread-safe to use.
	SelectBy(context.Context, [][]byte, func(sdk.Tx) bool)
}

ExtMempool is a extension of Mempool interface introduced in v0.50 for not be breaking in a patch release. In v0.52+, this interface will be merged into Mempool interface.

type Iterator

type Iterator interface {
	// Next returns the next transaction from the mempool. If there are no more
	// transactions, it returns nil.
	Next() Iterator

	// Tx returns the transaction at the current position of the iterator.
	Tx() sdk.Tx
}

Iterator defines an app-side mempool iterator interface that is as minimal as possible. The order of iteration is determined by the app-side mempool implementation.

type Mempool

type Mempool interface {
	// Insert attempts to insert a Tx into the app-side mempool returning
	// an error upon failure.
	Insert(context.Context, sdk.Tx) error

	// Select returns an Iterator over the app-side mempool. If txs are specified,
	// then they shall be incorporated into the Iterator. The Iterator is not thread-safe to use.
	Select(context.Context, [][]byte) Iterator

	// CountTx returns the number of transactions currently in the mempool.
	CountTx() int

	// Remove attempts to remove a transaction from the mempool, returning an error
	// upon failure.
	Remove(sdk.Tx) error
}

type NoOpMempool

type NoOpMempool struct{}

NoOpMempool defines a no-op mempool. Transactions are completely discarded and ignored when BaseApp interacts with the mempool.

Note: When this mempool is used, it assumed that an application will rely on CometBFT's transaction ordering defined in `RequestPrepareProposal`, which is FIFO-ordered by default.

func (NoOpMempool) CountTx

func (NoOpMempool) CountTx() int

func (NoOpMempool) Insert

func (NoOpMempool) Remove

func (NoOpMempool) Remove(sdk.Tx) error

func (NoOpMempool) Select

func (NoOpMempool) Select(context.Context, [][]byte) Iterator

func (NoOpMempool) SelectBy added in v0.50.10

func (NoOpMempool) SelectBy(context.Context, [][]byte, func(sdk.Tx) bool)

type PriorityNonceIterator

type PriorityNonceIterator[C comparable] struct {
	// contains filtered or unexported fields
}

PriorityNonceIterator defines an iterator that is used for mempool iteration on Select().

func (*PriorityNonceIterator[C]) Next

func (i *PriorityNonceIterator[C]) Next() Iterator

func (*PriorityNonceIterator[C]) Tx

func (i *PriorityNonceIterator[C]) Tx() sdk.Tx

type PriorityNonceMempool

type PriorityNonceMempool[C comparable] struct {
	// contains filtered or unexported fields
}

PriorityNonceMempool is a mempool implementation that stores txs in a partially ordered set by 2 dimensions: priority, and sender-nonce (sequence number). Internally it uses one priority ordered skip list and one skip list per sender ordered by sender-nonce (sequence number). When there are multiple txs from the same sender, they are not always comparable by priority to other sender txs and must be partially ordered by both sender-nonce and priority.

func DefaultPriorityMempool

func DefaultPriorityMempool() *PriorityNonceMempool[int64]

DefaultPriorityMempool returns a priorityNonceMempool with no options.

func NewPriorityMempool

func NewPriorityMempool[C comparable](cfg PriorityNonceMempoolConfig[C]) *PriorityNonceMempool[C]

NewPriorityMempool returns the SDK's default mempool implementation which returns txs in a partial order by 2 dimensions; priority, and sender-nonce.

func (*PriorityNonceMempool[C]) CountTx

func (mp *PriorityNonceMempool[C]) CountTx() int

CountTx returns the number of transactions in the mempool.

func (*PriorityNonceMempool[C]) Insert

func (mp *PriorityNonceMempool[C]) Insert(ctx context.Context, tx sdk.Tx) error

Insert attempts to insert a Tx into the app-side mempool in O(log n) time, returning an error if unsuccessful. Sender and nonce are derived from the transaction's first signature.

Transactions are unique by sender and nonce. Inserting a duplicate tx is an O(log n) no-op.

Inserting a duplicate tx with a different priority overwrites the existing tx, changing the total order of the mempool.

func (*PriorityNonceMempool[C]) NextSenderTx

func (mp *PriorityNonceMempool[C]) NextSenderTx(sender string) sdk.Tx

NextSenderTx returns the next transaction for a given sender by nonce order, i.e. the next valid transaction for the sender. If no such transaction exists, nil will be returned.

func (*PriorityNonceMempool[C]) Remove

func (mp *PriorityNonceMempool[C]) Remove(tx sdk.Tx) error

Remove removes a transaction from the mempool in O(log n) time, returning an error if unsuccessful.

func (*PriorityNonceMempool[C]) Select

func (mp *PriorityNonceMempool[C]) Select(ctx context.Context, txs [][]byte) Iterator

Select returns a set of transactions from the mempool, ordered by priority and sender-nonce in O(n) time. The passed in list of transactions are ignored. This is a readonly operation, the mempool is not modified.

The maxBytes parameter defines the maximum number of bytes of transactions to return.

NOTE: It is not safe to use this iterator while removing transactions from the underlying mempool.

func (*PriorityNonceMempool[C]) SelectBy added in v0.50.10

func (mp *PriorityNonceMempool[C]) SelectBy(ctx context.Context, txs [][]byte, callback func(sdk.Tx) bool)

SelectBy will hold the mutex during the iteration, callback returns if continue.

type PriorityNonceMempoolConfig added in v0.50.0

type PriorityNonceMempoolConfig[C comparable] struct {
	// TxPriority defines the transaction priority and comparator.
	TxPriority TxPriority[C]

	// OnRead is a callback to be called when a tx is read from the mempool.
	OnRead func(tx sdk.Tx)

	// TxReplacement is a callback to be called when duplicated transaction nonce
	// detected during mempool insert. An application can define a transaction
	// replacement rule based on tx priority or certain transaction fields.
	TxReplacement func(op, np C, oTx, nTx sdk.Tx) bool

	// MaxTx sets the maximum number of transactions allowed in the mempool with
	// the semantics:
	// - if MaxTx == 0, there is no cap on the number of transactions in the mempool
	// - if MaxTx > 0, the mempool will cap the number of transactions it stores,
	//   and will prioritize transactions by their priority and sender-nonce
	//   (sequence number) when evicting transactions.
	// - if MaxTx < 0, `Insert` is a no-op.
	MaxTx int

	// SignerExtractor is an implementation which retrieves signer data from a sdk.Tx
	SignerExtractor SignerExtractionAdapter
}

PriorityNonceMempoolConfig defines the configuration used to configure the PriorityNonceMempool.

func DefaultPriorityNonceMempoolConfig added in v0.50.0

func DefaultPriorityNonceMempoolConfig() PriorityNonceMempoolConfig[int64]

type SenderNonceMempool

type SenderNonceMempool struct {
	// contains filtered or unexported fields
}

SenderNonceMempool is a mempool that prioritizes transactions within a sender by nonce, the lowest first, but selects a random sender on each iteration. The mempool is iterated by:

1) Maintaining a separate list of nonce ordered txs per sender 2) For each select iteration, randomly choose a sender and pick the next nonce ordered tx from their list 3) Repeat 1,2 until the mempool is exhausted

Note that PrepareProposal could choose to stop iteration before reaching the end if maxBytes is reached.

func NewSenderNonceMempool

func NewSenderNonceMempool(opts ...SenderNonceOptions) *SenderNonceMempool

NewSenderNonceMempool creates a new mempool that prioritizes transactions by nonce, the lowest first, picking a random sender on each iteration.

func (*SenderNonceMempool) CountTx

func (snm *SenderNonceMempool) CountTx() int

CountTx returns the total count of txs in the mempool.

func (*SenderNonceMempool) Insert

func (snm *SenderNonceMempool) Insert(_ context.Context, tx sdk.Tx) error

Insert adds a tx to the mempool. It returns an error if the tx does not have at least one signer. Note, priority is ignored.

func (*SenderNonceMempool) NextSenderTx

func (snm *SenderNonceMempool) NextSenderTx(sender string) sdk.Tx

NextSenderTx returns the next transaction for a given sender by nonce order, i.e. the next valid transaction for the sender. If no such transaction exists, nil will be returned.

func (*SenderNonceMempool) Remove

func (snm *SenderNonceMempool) Remove(tx sdk.Tx) error

Remove removes a tx from the mempool. It returns an error if the tx does not have at least one signer or the tx was not found in the pool.

func (*SenderNonceMempool) Select

func (snm *SenderNonceMempool) Select(ctx context.Context, txs [][]byte) Iterator

Select returns an iterator ordering transactions the mempool with the lowest nonce of a random selected sender first.

NOTE: It is not safe to use this iterator while removing transactions from the underlying mempool.

func (*SenderNonceMempool) SelectBy added in v0.50.10

func (snm *SenderNonceMempool) SelectBy(ctx context.Context, txs [][]byte, callback func(sdk.Tx) bool)

SelectBy will hold the mutex during the iteration, callback returns if continue.

type SenderNonceOptions

type SenderNonceOptions func(*SenderNonceMempool)

func SenderNonceMaxTxOpt

func SenderNonceMaxTxOpt(maxTx int) SenderNonceOptions

SenderNonceMaxTxOpt Option To set limit of max tx when calling the constructor NewSenderNonceMempool.

Example:

NewSenderNonceMempool(SenderNonceMaxTxOpt(100))

func SenderNonceSeedOpt

func SenderNonceSeedOpt(seed int64) SenderNonceOptions

SenderNonceSeedOpt Option To add a Seed for random type when calling the constructor NewSenderNonceMempool.

Example:

random_seed := int64(1000)
NewSenderNonceMempool(SenderNonceSeedTxOpt(random_seed))

type SignerData added in v0.50.3

type SignerData struct {
	Signer   sdk.AccAddress
	Sequence uint64
}

SignerData contains canonical useful information about the signer of a transaction

func NewSignerData added in v0.50.3

func NewSignerData(signer sdk.AccAddress, sequence uint64) SignerData

NewSignerData returns a new SignerData instance.

func (SignerData) String added in v0.50.3

func (s SignerData) String() string

String implements the fmt.Stringer interface.

type SignerExtractionAdapter added in v0.50.3

type SignerExtractionAdapter interface {
	GetSigners(sdk.Tx) ([]SignerData, error)
}

SignerExtractionAdapter is an interface used to determine how the signers of a transaction should be extracted from the transaction.

type TxPriority added in v0.50.0

type TxPriority[C comparable] struct {
	// GetTxPriority returns the priority of the transaction. A priority must be
	// comparable via Compare.
	GetTxPriority func(ctx context.Context, tx sdk.Tx) C

	// Compare compares two transaction priorities. The result must be 0 if
	// a == b, -1 if a < b, and +1 if a > b.
	Compare func(a, b C) int

	// MinValue defines the minimum priority value, e.g. MinInt64. This value is
	// used when instantiating a new iterator and comparing weights.
	MinValue C
}

TxPriority defines a type that is used to retrieve and compare transaction priorities. Priorities must be comparable.

func NewDefaultTxPriority added in v0.50.0

func NewDefaultTxPriority() TxPriority[int64]

NewDefaultTxPriority returns a TxPriority comparator using ctx.Priority as the defining transaction priority.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL