Mempool

Abstract

The mempool module stores transactions which have not yet been included in a block, and provides an interface to check the validity of incoming transactions. It's defined by an interface here, with an implementation here.

Component Description

Full nodes instantiate a mempool here. A p2p.GossipValidator is constructed from the node's mempool here, which is used by Rollkit's P2P code to deal with peers who gossip invalid transactions. The mempool is also passed into the block manager constructor, which creates a BlockExecutor from the mempool.

The BlockExecutor calls ReapMaxBytesMaxGas in CreateBlock to get transactions from the pool for the new block. When commit is called, the BlockExecutor calls Update(...) on the mempool, removing the old transactions from the pool.

Communication

Several RPC methods query the mempool module: BroadcastTxCommit, BroadcastTxAsync, BroadcastTxSync call the mempool's CheckTx(...) method.

Interface

Function NameInput ArgumentsOutput TypeIntended Behavior
CheckTxtx types.Tx, callback func(*abci.Response), txInfo TxInfoerrorExecutes a new transaction against the application to determine its validity and whether it should be added to the mempool.
RemoveTxByKeytxKey types.TxKeyerrorRemoves a transaction, identified by its key, from the mempool.
ReapMaxBytesMaxGasmaxBytes, maxGas int64types.TxsReaps transactions from the mempool up to maxBytes bytes total with the condition that the total gasWanted must be less than maxGas. If both maxes are negative, there is no cap on the size of all returned transactions (~ all available transactions).
ReapMaxTxsmax inttypes.TxsReaps up to max transactions from the mempool. If max is negative, there is no cap on the size of all returned transactions (~ all available transactions).
LockN/AN/ALocks the mempool. The consensus must be able to hold the lock to safely update.
UnlockN/AN/AUnlocks the mempool.
UpdateblockHeight uint64, blockTxs types.Txs, deliverTxResponses []*abci.ResponseDeliverTx, newPreFn PreCheckFunc, newPostFn PostCheckFuncerrorInforms the mempool that the given txs were committed and can be discarded. This should be called after block is committed by consensus. Lock/Unlock must be managed by the caller.
FlushAppConnN/AerrorFlushes the mempool connection to ensure async callback calls are done, e.g., from CheckTx. Lock/Unlock must be managed by the caller.
FlushN/AN/ARemoves all transactions from the mempool and caches.
TxsAvailableN/A<-chan struct{}Returns a channel which fires once for every height when transactions are available in the mempool. The returned channel may be nil if EnableTxsAvailable was not called.
EnableTxsAvailableN/AN/AInitializes the TxsAvailable channel, ensuring it will trigger once every height when transactions are available.
SizeN/AintReturns the number of transactions in the mempool.
SizeBytesN/Aint64Returns the total size of all txs in the mempool.