github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/tendermint/mempool/mempool.go (about) 1 package mempool 2 3 import ( 4 "crypto/sha256" 5 "fmt" 6 7 abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types" 8 cfg "github.com/fibonacci-chain/fbc/libs/tendermint/config" 9 "github.com/fibonacci-chain/fbc/libs/tendermint/p2p" 10 "github.com/fibonacci-chain/fbc/libs/tendermint/types" 11 ) 12 13 // Mempool defines the mempool interface. 14 // 15 // Updates to the mempool need to be synchronized with committing a block so 16 // apps can reset their transient state on Commit. 17 type Mempool interface { 18 // CheckTx executes a new transaction against the application to determine 19 // its validity and whether it should be added to the mempool. 20 CheckTx(tx types.Tx, callback func(*abci.Response), txInfo TxInfo) error 21 22 // ReapMaxBytesMaxGas reaps transactions from the mempool up to maxBytes 23 // bytes total with the condition that the total gasWanted must be less than 24 // maxGas. 25 // If both maxes are negative, there is no cap on the size of all returned 26 // transactions (~ all available transactions). 27 ReapMaxBytesMaxGas(maxBytes, maxGas int64) []types.Tx 28 ReapEssentialTx(tx types.Tx) abci.TxEssentials 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 ReapUserTxsCnt(address string) int 36 37 // only for checkTx used! 38 GetUserPendingTxsCnt(address string) int 39 40 ReapUserTxs(address string, max int) types.Txs 41 GetPendingNonce(address string) (uint64, bool) 42 43 // Lock locks the mempool. The consensus must be able to hold lock to safely update. 44 Lock() 45 46 // Unlock unlocks the mempool. 47 Unlock() 48 49 // Update informs the mempool that the given txs were committed and can be discarded. 50 // NOTE: this should be called *after* block is committed by consensus. 51 // NOTE: Lock/Unlock must be managed by caller 52 Update( 53 blockHeight int64, 54 blockTxs types.Txs, 55 deliverTxResponses []*abci.ResponseDeliverTx, 56 newPreFn PreCheckFunc, 57 newPostFn PostCheckFunc, 58 ) error 59 60 // FlushAppConn flushes the mempool connection to ensure async reqResCb calls are 61 // done. E.g. from CheckTx. 62 // NOTE: Lock/Unlock must be managed by caller 63 FlushAppConn() error 64 65 // Flush removes all transactions from the mempool and cache 66 Flush() 67 68 // TxsAvailable returns a channel which fires once for every height, 69 // and only when transactions are available in the mempool. 70 // NOTE: the returned channel may be nil if EnableTxsAvailable was not called. 71 TxsAvailable() <-chan struct{} 72 73 // EnableTxsAvailable initializes the TxsAvailable channel, ensuring it will 74 // trigger once every height when transactions are available. 75 EnableTxsAvailable() 76 77 // Size returns the number of transactions in the mempool. 78 Size() int 79 80 // TxsBytes returns the total size of all txs in the mempool. 81 TxsBytes() int64 82 83 SetEventBus(eventBus types.TxEventPublisher) 84 85 GetConfig() *cfg.MempoolConfig 86 GetTxByHash(hash [sha256.Size]byte) (types.Tx, error) 87 88 GetAddressList() []string 89 SetAccountRetriever(retriever AccountRetriever) 90 91 SetTxInfoParser(parser TxInfoParser) 92 93 GetTxSimulateGas(txHash string) int64 94 95 GetEnableDeleteMinGPTx() bool 96 } 97 98 //-------------------------------------------------------------------------------- 99 100 // PreCheckFunc is an optional filter executed before CheckTx and rejects 101 // transaction if false is returned. An example would be to ensure that a 102 // transaction doesn't exceeded the block size. 103 type PreCheckFunc func(types.Tx) error 104 105 // PostCheckFunc is an optional filter executed after CheckTx and rejects 106 // transaction if false is returned. An example would be to ensure a 107 // transaction doesn't require more gas than available for the block. 108 type PostCheckFunc func(types.Tx, *abci.ResponseCheckTx) error 109 110 // TxInfo are parameters that get passed when attempting to add a tx to the 111 // mempool. 112 type TxInfo struct { 113 // SenderID is the internal peer ID used in the mempool to identify the 114 // sender, storing 2 bytes with each tx instead of 20 bytes for the p2p.ID. 115 SenderID uint16 116 // SenderP2PID is the actual p2p.ID of the sender, used e.g. for logging. 117 SenderP2PID p2p.ID 118 119 from string 120 wtx *WrappedTx 121 checkType abci.CheckTxType 122 } 123 124 //-------------------------------------------------------------------------------- 125 126 // PreCheckAminoMaxBytes checks that the size of the transaction plus the amino 127 // overhead is smaller or equal to the expected maxBytes. 128 func PreCheckAminoMaxBytes(maxBytes int64) PreCheckFunc { 129 return func(tx types.Tx) error { 130 // We have to account for the amino overhead in the tx size as well 131 // NOTE: fieldNum = 1 as types.Block.Data contains Txs []Tx as first field. 132 // If this field order ever changes this needs to updated here accordingly. 133 // NOTE: if some []Tx are encoded without a parenting struct, the 134 // fieldNum is also equal to 1. 135 aminoOverhead := types.ComputeAminoOverhead(tx, 1) 136 txSize := int64(len(tx)) + aminoOverhead 137 if txSize > maxBytes { 138 return fmt.Errorf("tx size (including amino overhead) is too big: %d, max: %d", 139 txSize, maxBytes) 140 } 141 return nil 142 } 143 } 144 145 // PostCheckMaxGas checks that the wanted gas is smaller or equal to the passed 146 // maxGas. Returns nil if maxGas is -1. 147 func PostCheckMaxGas(maxGas int64) PostCheckFunc { 148 return func(tx types.Tx, res *abci.ResponseCheckTx) error { 149 if maxGas == -1 { 150 return nil 151 } 152 if res.GasWanted < 0 { 153 return fmt.Errorf("gas wanted %d is negative", 154 res.GasWanted) 155 } 156 if res.GasWanted > maxGas { 157 return fmt.Errorf("gas wanted %d is greater than max gas %d", 158 res.GasWanted, maxGas) 159 } 160 return nil 161 } 162 }