bitbucket.org/number571/tendermint@v0.8.14/internal/mempool/mempool.go (about) 1 package mempool 2 3 import ( 4 "context" 5 "fmt" 6 "math" 7 8 abci "bitbucket.org/number571/tendermint/abci/types" 9 "bitbucket.org/number571/tendermint/internal/p2p" 10 "bitbucket.org/number571/tendermint/types" 11 ) 12 13 const ( 14 MempoolChannel = p2p.ChannelID(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(ctx context.Context, tx types.Tx, callback func(*abci.Response), txInfo TxInfo) error 34 35 // ReapMaxBytesMaxGas reaps transactions from the mempool up to maxBytes 36 // bytes total with the condition that the total gasWanted must be less than 37 // maxGas. 38 // 39 // If both maxes are negative, there is no cap on the size of all returned 40 // transactions (~ all available transactions). 41 ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs 42 43 // ReapMaxTxs reaps up to max transactions from the mempool. If max is 44 // negative, there is no cap on the size of all returned transactions 45 // (~ all available transactions). 46 ReapMaxTxs(max int) types.Txs 47 48 // Lock locks the mempool. The consensus must be able to hold lock to safely 49 // update. 50 Lock() 51 52 // Unlock unlocks the mempool. 53 Unlock() 54 55 // Update informs the mempool that the given txs were committed and can be 56 // discarded. 57 // 58 // NOTE: 59 // 1. This should be called *after* block is committed by consensus. 60 // 2. Lock/Unlock must be managed by the caller. 61 Update( 62 blockHeight int64, 63 blockTxs types.Txs, 64 deliverTxResponses []*abci.ResponseDeliverTx, 65 newPreFn PreCheckFunc, 66 newPostFn PostCheckFunc, 67 ) error 68 69 // FlushAppConn flushes the mempool connection to ensure async callback calls 70 // are done, e.g. from CheckTx. 71 // 72 // NOTE: 73 // 1. Lock/Unlock must be managed by caller. 74 FlushAppConn() error 75 76 // Flush removes all transactions from the mempool and caches. 77 Flush() 78 79 // TxsAvailable returns a channel which fires once for every height, and only 80 // when transactions are available in the mempool. 81 // 82 // NOTE: 83 // 1. The returned channel may be nil if EnableTxsAvailable was not called. 84 TxsAvailable() <-chan struct{} 85 86 // EnableTxsAvailable initializes the TxsAvailable channel, ensuring it will 87 // trigger once every height when transactions are available. 88 EnableTxsAvailable() 89 90 // Size returns the number of transactions in the mempool. 91 Size() int 92 93 // SizeBytes returns the total size of all txs in the mempool. 94 SizeBytes() int64 95 } 96 97 // PreCheckFunc is an optional filter executed before CheckTx and rejects 98 // transaction if false is returned. An example would be to ensure that a 99 // transaction doesn't exceeded the block size. 100 type PreCheckFunc func(types.Tx) error 101 102 // PostCheckFunc is an optional filter executed after CheckTx and rejects 103 // transaction if false is returned. An example would be to ensure a 104 // transaction doesn't require more gas than available for the block. 105 type PostCheckFunc func(types.Tx, *abci.ResponseCheckTx) error 106 107 // PreCheckMaxBytes checks that the size of the transaction is smaller or equal 108 // to the expected maxBytes. 109 func PreCheckMaxBytes(maxBytes int64) PreCheckFunc { 110 return func(tx types.Tx) error { 111 txSize := types.ComputeProtoSizeForTxs([]types.Tx{tx}) 112 113 if txSize > maxBytes { 114 return fmt.Errorf("tx size is too big: %d, max: %d", txSize, maxBytes) 115 } 116 117 return nil 118 } 119 } 120 121 // PostCheckMaxGas checks that the wanted gas is smaller or equal to the passed 122 // maxGas. Returns nil if maxGas is -1. 123 func PostCheckMaxGas(maxGas int64) PostCheckFunc { 124 return func(tx types.Tx, res *abci.ResponseCheckTx) error { 125 if maxGas == -1 { 126 return nil 127 } 128 if res.GasWanted < 0 { 129 return fmt.Errorf("gas wanted %d is negative", 130 res.GasWanted) 131 } 132 if res.GasWanted > maxGas { 133 return fmt.Errorf("gas wanted %d is greater than max gas %d", 134 res.GasWanted, maxGas) 135 } 136 137 return nil 138 } 139 }