github.com/KYVENetwork/cometbft/v38@v38.0.3/mempool/mempool.go (about) 1 package mempool 2 3 import ( 4 "crypto/sha256" 5 "fmt" 6 "math" 7 8 abci "github.com/KYVENetwork/cometbft/v38/abci/types" 9 "github.com/KYVENetwork/cometbft/v38/types" 10 ) 11 12 const ( 13 MempoolChannel = byte(0x30) 14 15 // PeerCatchupSleepIntervalMS defines how much time to sleep if a peer is behind 16 PeerCatchupSleepIntervalMS = 100 17 18 // UnknownPeerID is the peer ID to use when running CheckTx when there is 19 // no peer (e.g. RPC) 20 UnknownPeerID uint16 = 0 21 22 MaxActiveIDs = math.MaxUint16 23 ) 24 25 //go:generate ../scripts/mockery_generate.sh Mempool 26 27 // Mempool defines the mempool interface. 28 // 29 // Updates to the mempool need to be synchronized with committing a block so 30 // applications can reset their transient state on Commit. 31 type Mempool interface { 32 // CheckTx executes a new transaction against the application to determine 33 // its validity and whether it should be added to the mempool. 34 CheckTx(tx types.Tx, callback func(*abci.ResponseCheckTx), txInfo TxInfo) error 35 36 // RemoveTxByKey removes a transaction, identified by its key, 37 // from the mempool. 38 RemoveTxByKey(txKey types.TxKey) error 39 40 // ReapMaxBytesMaxGas reaps transactions from the mempool up to maxBytes 41 // bytes total with the condition that the total gasWanted must be less than 42 // maxGas. 43 // 44 // If both maxes are negative, there is no cap on the size of all returned 45 // transactions (~ all available transactions). 46 ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs 47 48 // ReapMaxTxs reaps up to max transactions from the mempool. If max is 49 // negative, there is no cap on the size of all returned transactions 50 // (~ all available transactions). 51 ReapMaxTxs(max int) types.Txs 52 53 // Lock locks the mempool. The consensus must be able to hold lock to safely 54 // update. 55 Lock() 56 57 // Unlock unlocks the mempool. 58 Unlock() 59 60 // Update informs the mempool that the given txs were committed and can be 61 // discarded. 62 // 63 // NOTE: 64 // 1. This should be called *after* block is committed by consensus. 65 // 2. Lock/Unlock must be managed by the caller. 66 Update( 67 blockHeight int64, 68 blockTxs types.Txs, 69 deliverTxResponses []*abci.ExecTxResult, 70 newPreFn PreCheckFunc, 71 newPostFn PostCheckFunc, 72 ) error 73 74 // FlushAppConn flushes the mempool connection to ensure async callback calls 75 // are done, e.g. from CheckTx. 76 // 77 // NOTE: 78 // 1. Lock/Unlock must be managed by caller. 79 FlushAppConn() error 80 81 // Flush removes all transactions from the mempool and caches. 82 Flush() 83 84 // TxsAvailable returns a channel which fires once for every height, and only 85 // when transactions are available in the mempool. 86 // 87 // NOTE: 88 // 1. The returned channel may be nil if EnableTxsAvailable was not called. 89 TxsAvailable() <-chan struct{} 90 91 // EnableTxsAvailable initializes the TxsAvailable channel, ensuring it will 92 // trigger once every height when transactions are available. 93 EnableTxsAvailable() 94 95 // Size returns the number of transactions in the mempool. 96 Size() int 97 98 // SizeBytes returns the total size of all txs in the mempool. 99 SizeBytes() int64 100 } 101 102 // PreCheckFunc is an optional filter executed before CheckTx and rejects 103 // transaction if false is returned. An example would be to ensure that a 104 // transaction doesn't exceeded the block size. 105 type PreCheckFunc func(types.Tx) error 106 107 // PostCheckFunc is an optional filter executed after CheckTx and rejects 108 // transaction if false is returned. An example would be to ensure a 109 // transaction doesn't require more gas than available for the block. 110 type PostCheckFunc func(types.Tx, *abci.ResponseCheckTx) error 111 112 // PreCheckMaxBytes checks that the size of the transaction is smaller or equal 113 // to the expected maxBytes. 114 func PreCheckMaxBytes(maxBytes int64) PreCheckFunc { 115 return func(tx types.Tx) error { 116 txSize := types.ComputeProtoSizeForTxs([]types.Tx{tx}) 117 118 if txSize > maxBytes { 119 return fmt.Errorf("tx size is too big: %d, max: %d", txSize, maxBytes) 120 } 121 122 return nil 123 } 124 } 125 126 // PostCheckMaxGas checks that the wanted gas is smaller or equal to the passed 127 // maxGas. Returns nil if maxGas is -1. 128 func PostCheckMaxGas(maxGas int64) PostCheckFunc { 129 return func(tx types.Tx, res *abci.ResponseCheckTx) error { 130 if maxGas == -1 { 131 return nil 132 } 133 if res.GasWanted < 0 { 134 return fmt.Errorf("gas wanted %d is negative", 135 res.GasWanted) 136 } 137 if res.GasWanted > maxGas { 138 return fmt.Errorf("gas wanted %d is greater than max gas %d", 139 res.GasWanted, maxGas) 140 } 141 142 return nil 143 } 144 } 145 146 // TxKey is the fixed length array key used as an index. 147 type TxKey [sha256.Size]byte