github.com/vipernet-xyz/tm@v0.34.24/mempool/mempool.go (about)

     1  package mempool
     2  
     3  import (
     4  	"crypto/sha256"
     5  	"errors"
     6  	"fmt"
     7  	"math"
     8  
     9  	abci "github.com/vipernet-xyz/tm/abci/types"
    10  	"github.com/vipernet-xyz/tm/types"
    11  )
    12  
    13  const (
    14  	MempoolChannel = byte(0x30)
    15  
    16  	// PeerCatchupSleepIntervalMS defines how much time to sleep if a peer is behind
    17  	PeerCatchupSleepIntervalMS = 100
    18  
    19  	// UnknownPeerID is the peer ID to use when running CheckTx when there is
    20  	// no peer (e.g. RPC)
    21  	UnknownPeerID uint16 = 0
    22  
    23  	MaxActiveIDs = math.MaxUint16
    24  )
    25  
    26  // Mempool defines the mempool interface.
    27  //
    28  // Updates to the mempool need to be synchronized with committing a block so
    29  // applications can reset their transient state on Commit.
    30  type Mempool interface {
    31  	// CheckTx executes a new transaction against the application to determine
    32  	// its validity and whether it should be added to the mempool.
    33  	CheckTx(tx types.Tx, callback func(*abci.Response), txInfo TxInfo) error
    34  
    35  	// RemoveTxByKey removes a transaction, identified by its key,
    36  	// from the mempool.
    37  	RemoveTxByKey(txKey types.TxKey) error
    38  
    39  	// ReapMaxBytesMaxGas reaps transactions from the mempool up to maxBytes
    40  	// bytes total with the condition that the total gasWanted must be less than
    41  	// maxGas.
    42  	//
    43  	// If both maxes are negative, there is no cap on the size of all returned
    44  	// transactions (~ all available transactions).
    45  	ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs
    46  
    47  	// ReapMaxTxs reaps up to max transactions from the mempool. If max is
    48  	// negative, there is no cap on the size of all returned transactions
    49  	// (~ all available transactions).
    50  	ReapMaxTxs(max int) types.Txs
    51  
    52  	// Lock locks the mempool. The consensus must be able to hold lock to safely
    53  	// update.
    54  	Lock()
    55  
    56  	// Unlock unlocks the mempool.
    57  	Unlock()
    58  
    59  	// Update informs the mempool that the given txs were committed and can be
    60  	// discarded.
    61  	//
    62  	// NOTE:
    63  	// 1. This should be called *after* block is committed by consensus.
    64  	// 2. Lock/Unlock must be managed by the caller.
    65  	Update(
    66  		blockHeight int64,
    67  		blockTxs types.Txs,
    68  		deliverTxResponses []*abci.ResponseDeliverTx,
    69  		newPreFn PreCheckFunc,
    70  		newPostFn PostCheckFunc,
    71  	) error
    72  
    73  	// FlushAppConn flushes the mempool connection to ensure async callback calls
    74  	// are done, e.g. from CheckTx.
    75  	//
    76  	// NOTE:
    77  	// 1. Lock/Unlock must be managed by caller.
    78  	FlushAppConn() error
    79  
    80  	// Flush removes all transactions from the mempool and caches.
    81  	Flush()
    82  
    83  	// TxsAvailable returns a channel which fires once for every height, and only
    84  	// when transactions are available in the mempool.
    85  	//
    86  	// NOTE:
    87  	// 1. The returned channel may be nil if EnableTxsAvailable was not called.
    88  	TxsAvailable() <-chan struct{}
    89  
    90  	// EnableTxsAvailable initializes the TxsAvailable channel, ensuring it will
    91  	// trigger once every height when transactions are available.
    92  	EnableTxsAvailable()
    93  
    94  	// Size returns the number of transactions in the mempool.
    95  	Size() int
    96  
    97  	// SizeBytes returns the total size of all txs in the mempool.
    98  	SizeBytes() int64
    99  }
   100  
   101  // PreCheckFunc is an optional filter executed before CheckTx and rejects
   102  // transaction if false is returned. An example would be to ensure that a
   103  // transaction doesn't exceeded the block size.
   104  type PreCheckFunc func(types.Tx) error
   105  
   106  // PostCheckFunc is an optional filter executed after CheckTx and rejects
   107  // transaction if false is returned. An example would be to ensure a
   108  // transaction doesn't require more gas than available for the block.
   109  type PostCheckFunc func(types.Tx, *abci.ResponseCheckTx) error
   110  
   111  // PreCheckMaxBytes checks that the size of the transaction is smaller or equal
   112  // to the expected maxBytes.
   113  func PreCheckMaxBytes(maxBytes int64) PreCheckFunc {
   114  	return func(tx types.Tx) error {
   115  		txSize := types.ComputeProtoSizeForTxs([]types.Tx{tx})
   116  
   117  		if txSize > maxBytes {
   118  			return fmt.Errorf("tx size is too big: %d, max: %d", txSize, maxBytes)
   119  		}
   120  
   121  		return nil
   122  	}
   123  }
   124  
   125  // PostCheckMaxGas checks that the wanted gas is smaller or equal to the passed
   126  // maxGas. Returns nil if maxGas is -1.
   127  func PostCheckMaxGas(maxGas int64) PostCheckFunc {
   128  	return func(tx types.Tx, res *abci.ResponseCheckTx) error {
   129  		if maxGas == -1 {
   130  			return nil
   131  		}
   132  		if res.GasWanted < 0 {
   133  			return fmt.Errorf("gas wanted %d is negative",
   134  				res.GasWanted)
   135  		}
   136  		if res.GasWanted > maxGas {
   137  			return fmt.Errorf("gas wanted %d is greater than max gas %d",
   138  				res.GasWanted, maxGas)
   139  		}
   140  
   141  		return nil
   142  	}
   143  }
   144  
   145  // ErrTxInCache is returned to the client if we saw tx earlier
   146  var ErrTxInCache = errors.New("tx already exists in cache")
   147  
   148  // TxKey is the fixed length array key used as an index.
   149  type TxKey [sha256.Size]byte
   150  
   151  // ErrTxTooLarge defines an error when a transaction is too big to be sent in a
   152  // message to other peers.
   153  type ErrTxTooLarge struct {
   154  	Max    int
   155  	Actual int
   156  }
   157  
   158  func (e ErrTxTooLarge) Error() string {
   159  	return fmt.Sprintf("Tx too large. Max size is %d, but got %d", e.Max, e.Actual)
   160  }
   161  
   162  // ErrMempoolIsFull defines an error where Tendermint and the application cannot
   163  // handle that much load.
   164  type ErrMempoolIsFull struct {
   165  	NumTxs      int
   166  	MaxTxs      int
   167  	TxsBytes    int64
   168  	MaxTxsBytes int64
   169  }
   170  
   171  func (e ErrMempoolIsFull) Error() string {
   172  	return fmt.Sprintf(
   173  		"mempool is full: number of txs %d (max: %d), total txs bytes %d (max: %d)",
   174  		e.NumTxs,
   175  		e.MaxTxs,
   176  		e.TxsBytes,
   177  		e.MaxTxsBytes,
   178  	)
   179  }
   180  
   181  // ErrPreCheck defines an error where a transaction fails a pre-check.
   182  type ErrPreCheck struct {
   183  	Reason error
   184  }
   185  
   186  func (e ErrPreCheck) Error() string {
   187  	return e.Reason.Error()
   188  }
   189  
   190  // IsPreCheckError returns true if err is due to pre check failure.
   191  func IsPreCheckError(err error) bool {
   192  	return errors.As(err, &ErrPreCheck{})
   193  }