mempool

package
v0.50.0-alpha.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2023 License: Apache-2.0 Imports: 10 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 = 0

Functions

func IsEmpty

func IsEmpty[C comparable](mempool Mempool) error

Types

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 must
	// closed by the caller.
	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

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(_ context.Context, _ [][]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.

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
}

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(_ context.Context, _ [][]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.

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 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