github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/bft/mempool/mempool.go (about)

     1  package mempool
     2  
     3  import (
     4  	abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
     5  	"github.com/gnolang/gno/tm2/pkg/bft/types"
     6  )
     7  
     8  // Mempool defines the mempool interface.
     9  //
    10  // Updates to the mempool need to be synchronized with committing a block so
    11  // apps can reset their transient state on Commit.
    12  type Mempool interface {
    13  	// CheckTx executes a new transaction against the application to determine
    14  	// its validity and whether it should be added to the mempool.
    15  	CheckTx(tx types.Tx, callback func(abci.Response)) error
    16  
    17  	// CheckTxWithInfo performs the same operation as CheckTx, but with extra
    18  	// meta data about the tx.
    19  	// Currently this metadata is the peer who sent it, used to prevent the tx
    20  	// from being gossiped back to them.
    21  	CheckTxWithInfo(tx types.Tx, callback func(abci.Response), txInfo TxInfo) error
    22  
    23  	// ReapMaxBytesMaxGas reaps transactions from the mempool up to maxDataBytes
    24  	// bytes total with the condition that the total gasWanted must be less than
    25  	// maxGas.
    26  	// If both maxes are negative, there is no cap on the size of all returned
    27  	// transactions (~ all available transactions).
    28  	ReapMaxBytesMaxGas(maxDataBytes, maxGas int64) types.Txs
    29  
    30  	// ReapMaxTxs reaps up to max transactions from the mempool.
    31  	// If max is negative, there is no cap on the size of all returned
    32  	// transactions (~ all available transactions).
    33  	ReapMaxTxs(max int) types.Txs
    34  
    35  	// Lock locks the mempool. The consensus must be able to hold lock to safely update.
    36  	Lock()
    37  
    38  	// Unlock unlocks the mempool.
    39  	Unlock()
    40  
    41  	// Update informs the mempool that the given txs were committed and can be discarded.
    42  	// NOTE: this should be called *after* block is committed by consensus.
    43  	// NOTE: unsafe; Lock/Unlock must be managed by caller
    44  	Update(blockHeight int64, blockTxs types.Txs, deliverTxResponses []abci.ResponseDeliverTx, newPreFn PreCheckFunc, maxTxBytes int64) error
    45  
    46  	// FlushAppConn flushes the mempool connection to ensure async reqResCb calls are
    47  	// done. E.g. from CheckTx.
    48  	FlushAppConn() error
    49  
    50  	// Flush removes all transactions from the mempool and cache
    51  	Flush()
    52  
    53  	// TxsAvailable returns a channel which fires once for every height,
    54  	// and only when transactions are available in the mempool.
    55  	// NOTE: the returned channel may be nil if EnableTxsAvailable was not called.
    56  	TxsAvailable() <-chan struct{}
    57  
    58  	// EnableTxsAvailable initializes the TxsAvailable channel, ensuring it will
    59  	// trigger once every height when transactions are available.
    60  	EnableTxsAvailable()
    61  
    62  	// Size returns the number of transactions in the mempool.
    63  	Size() int
    64  
    65  	// TxsBytes returns the total size of all txs in the mempool.
    66  	TxsBytes() int64
    67  
    68  	// Maximum allowable tx size.
    69  	MaxTxBytes() int64
    70  
    71  	// InitWAL creates a directory for the WAL file and opens a file itself.
    72  	InitWAL()
    73  
    74  	// CloseWAL closes and discards the underlying WAL file.
    75  	// Any further writes will not be relayed to disk.
    76  	CloseWAL()
    77  }
    78  
    79  //--------------------------------------------------------------------------------
    80  
    81  // PreCheckFunc is an optional filter executed before CheckTx and rejects
    82  // transaction if false is returned. An example would be to ensure that a
    83  // transaction doesn't exceeded the block size.
    84  //
    85  // NOTE: there is no PostCheckFunc, for otherwise a checktx transaction
    86  // that passes in the app's checktx state would increment sequence etc,
    87  // causing an unexpected signature error until the next block.
    88  type PreCheckFunc func(types.Tx) error
    89  
    90  // TxInfo are parameters that get passed when attempting to add a tx to the
    91  // mempool.
    92  type TxInfo struct {
    93  	// We don't use p2p.ID here because it's too big. The gain is to store max 2
    94  	// bytes with each tx to identify the sender rather than 20 bytes.
    95  	SenderID uint16
    96  }