github.com/cosmos/cosmos-sdk@v0.50.10/types/mempool/mempool.go (about)

     1  package mempool
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  
     7  	sdk "github.com/cosmos/cosmos-sdk/types"
     8  )
     9  
    10  type Mempool interface {
    11  	// Insert attempts to insert a Tx into the app-side mempool returning
    12  	// an error upon failure.
    13  	Insert(context.Context, sdk.Tx) error
    14  
    15  	// Select returns an Iterator over the app-side mempool. If txs are specified,
    16  	// then they shall be incorporated into the Iterator. The Iterator is not thread-safe to use.
    17  	Select(context.Context, [][]byte) Iterator
    18  
    19  	// CountTx returns the number of transactions currently in the mempool.
    20  	CountTx() int
    21  
    22  	// Remove attempts to remove a transaction from the mempool, returning an error
    23  	// upon failure.
    24  	Remove(sdk.Tx) error
    25  }
    26  
    27  // ExtMempool is a extension of Mempool interface introduced in v0.50
    28  // for not be breaking in a patch release.
    29  // In v0.52+, this interface will be merged into Mempool interface.
    30  type ExtMempool interface {
    31  	Mempool
    32  
    33  	// SelectBy use callback to iterate over the mempool, it's thread-safe to use.
    34  	SelectBy(context.Context, [][]byte, func(sdk.Tx) bool)
    35  }
    36  
    37  // Iterator defines an app-side mempool iterator interface that is as minimal as
    38  // possible. The order of iteration is determined by the app-side mempool
    39  // implementation.
    40  type Iterator interface {
    41  	// Next returns the next transaction from the mempool. If there are no more
    42  	// transactions, it returns nil.
    43  	Next() Iterator
    44  
    45  	// Tx returns the transaction at the current position of the iterator.
    46  	Tx() sdk.Tx
    47  }
    48  
    49  var (
    50  	ErrTxNotFound           = errors.New("tx not found in mempool")
    51  	ErrMempoolTxMaxCapacity = errors.New("pool reached max tx capacity")
    52  )
    53  
    54  // SelectBy is compatible with old interface to avoid breaking api.
    55  // In v0.52+, this function is removed and SelectBy is merged into Mempool interface.
    56  func SelectBy(ctx context.Context, mempool Mempool, txs [][]byte, callback func(sdk.Tx) bool) {
    57  	if ext, ok := mempool.(ExtMempool); ok {
    58  		ext.SelectBy(ctx, txs, callback)
    59  		return
    60  	}
    61  
    62  	// fallback to old behavior, without holding the lock while iteration.
    63  	iter := mempool.Select(ctx, txs)
    64  	for iter != nil && callback(iter.Tx()) {
    65  		iter = iter.Next()
    66  	}
    67  }