Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP [do not merge] Hard max on gas wanted #270

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
545 changes: 291 additions & 254 deletions abci/types/types.pb.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion internal/consensus/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func TestMempoolRmBadTx(t *testing.T) {

// check for the tx
for {
txs := assertMempool(t, cs.txNotifier).ReapMaxBytesMaxGas(int64(len(txBytes)), -1, 0)
txs := assertMempool(t, cs.txNotifier).ReapMaxBytesMaxGas(int64(len(txBytes)), 0, -1, 0)
if len(txs) == 0 {
emptyMempoolCh <- struct{}{}
return
Expand Down
6 changes: 3 additions & 3 deletions internal/consensus/replay_stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ func (emptyMempool) Size() int { return 0 }
func (emptyMempool) CheckTx(context.Context, types.Tx, func(*abci.ResponseCheckTx), mempool.TxInfo) error {
return nil
}
func (emptyMempool) RemoveTxByKey(txKey types.TxKey) error { return nil }
func (emptyMempool) ReapMaxBytesMaxGas(_, _, _ int64) types.Txs { return types.Txs{} }
func (emptyMempool) ReapMaxTxs(n int) types.Txs { return types.Txs{} }
func (emptyMempool) RemoveTxByKey(txKey types.TxKey) error { return nil }
func (emptyMempool) ReapMaxBytesMaxGas(_, _, _, _ int64) types.Txs { return types.Txs{} }
func (emptyMempool) ReapMaxTxs(n int) types.Txs { return types.Txs{} }
func (emptyMempool) Update(
_ context.Context,
_ int64,
Expand Down
32 changes: 21 additions & 11 deletions internal/mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,18 +428,25 @@ func (txmp *TxMempool) Flush() {

// ReapMaxBytesMaxGas returns a list of transactions within the provided size
// and gas constraints. Transaction are retrieved in priority order.
// There are 4 types of constraints.
// 1. maxBytes - stops pulling txs from mempool once maxBytes is hit. Can be set to -1 to be ignored.
// 2. maxGasWanted - stops pulling txs from mempool once total gas wanted exceeds maxGasWanted.
// Can be set to -1 to be ignored.
// 3. maxGasEstimated - similar to maxGasWanted but will use the estimated gas used for EVM txs
// while still using gas wanted for cosmos txs. Can be set to -1 to be ignored.
//
// NOTE:
// - Transactions returned are not removed from the mempool transaction
// store or indexes.
func (txmp *TxMempool) ReapMaxBytesMaxGas(maxBytes, maxGas, minTxsInBlock int64) types.Txs {
func (txmp *TxMempool) ReapMaxBytesMaxGas(maxBytes, maxGasWanted, maxGasEstimated, minTxsInBlock int64) types.Txs {
txmp.mtx.Lock()
defer txmp.mtx.Unlock()

var (
totalGas int64
totalSize int64
nonzeroGasTxCnt int64
totalGasWanted int64
totalGasEstimated int64
totalSize int64
nonzeroGasTxCnt int64
)

var txs []types.Tx
Expand All @@ -455,21 +462,24 @@ func (txmp *TxMempool) ReapMaxBytesMaxGas(maxBytes, maxGas, minTxsInBlock int64)
}

// if the tx doesn't have a gas estimate, fallback to gas wanted
var gasEstimateOrWanted int64
var txGasEstimate int64
if wtx.estimatedGas > 0 {
gasEstimateOrWanted = wtx.estimatedGas
txGasEstimate = wtx.estimatedGas
} else {
gasEstimateOrWanted = wtx.gasWanted
txGasEstimate = wtx.gasWanted
}

totalSize += size
gas := totalGas + gasEstimateOrWanted
maxGasExceeded := maxGas > -1 && gas > maxGas
if nonzeroGasTxCnt >= minTxsInBlock && maxGasExceeded {
gasWanted := totalGasWanted + wtx.gasWanted
gasEstimated := totalGasEstimated + txGasEstimate
maxGasWantedExceeded := maxGasWanted > -1 && gasWanted > maxGasWanted
maxGasEstimatedExceeded := maxGasEstimated > -1 && gasEstimated > maxGasEstimated
if nonzeroGasTxCnt >= minTxsInBlock && (maxGasWantedExceeded || maxGasEstimatedExceeded) {
return false
}

totalGas = gas
totalGasWanted = gasWanted
totalGasEstimated = gasEstimated

txs = append(txs, wtx.tx)
if wtx.gasWanted > 0 {
Expand Down
12 changes: 6 additions & 6 deletions internal/mempool/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ func TestTxMempool_ReapMaxBytesMaxGas(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
reapedTxs := txmp.ReapMaxBytesMaxGas(-1, 50, 0)
reapedTxs := txmp.ReapMaxBytesMaxGas(-1, 50, -1, 0)
ensurePrioritized(reapedTxs)
require.Equal(t, len(tTxs), txmp.Size())
require.Equal(t, int64(5690), txmp.SizeBytes())
Expand All @@ -418,7 +418,7 @@ func TestTxMempool_ReapMaxBytesMaxGas(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
reapedTxs := txmp.ReapMaxBytesMaxGas(1000, -1, 0)
reapedTxs := txmp.ReapMaxBytesMaxGas(1000, -1, -1, 0)
ensurePrioritized(reapedTxs)
require.Equal(t, len(tTxs), txmp.Size())
require.Equal(t, int64(5690), txmp.SizeBytes())
Expand All @@ -430,7 +430,7 @@ func TestTxMempool_ReapMaxBytesMaxGas(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
reapedTxs := txmp.ReapMaxBytesMaxGas(1500, 30, 0)
reapedTxs := txmp.ReapMaxBytesMaxGas(1500, 30, -1, 0)
ensurePrioritized(reapedTxs)
require.Equal(t, len(tTxs), txmp.Size())
require.Equal(t, int64(5690), txmp.SizeBytes())
Expand All @@ -441,7 +441,7 @@ func TestTxMempool_ReapMaxBytesMaxGas(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
reapedTxs := txmp.ReapMaxBytesMaxGas(-1, 1, 2)
reapedTxs := txmp.ReapMaxBytesMaxGas(-1, 1, -1, 2)
ensurePrioritized(reapedTxs)
require.Equal(t, len(tTxs), txmp.Size())
require.Len(t, reapedTxs, 2)
Expand All @@ -451,7 +451,7 @@ func TestTxMempool_ReapMaxBytesMaxGas(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
reapedTxs := txmp.ReapMaxBytesMaxGas(-1, 50, 0)
reapedTxs := txmp.ReapMaxBytesMaxGas(-1, 50, -1, 0)
ensurePrioritized(reapedTxs)
require.Equal(t, len(tTxs), txmp.Size())
require.Len(t, reapedTxs, 50)
Expand Down Expand Up @@ -501,7 +501,7 @@ func TestTxMempool_ReapMaxBytesMaxGas_FallbackToGasWanted(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
reapedTxs := txmp.ReapMaxBytesMaxGas(-1, 50, 0)
reapedTxs := txmp.ReapMaxBytesMaxGas(-1, 50, -1, 0)
ensurePrioritized(reapedTxs)
require.Equal(t, len(tTxs), txmp.Size())
require.Len(t, reapedTxs, 50)
Expand Down
2 changes: 1 addition & 1 deletion internal/mempool/mocks/mempool.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/mempool/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Mempool interface {
//
// If all 3 maxes are negative, there is no cap on the size of all returned
// transactions (~ all available transactions).
ReapMaxBytesMaxGas(maxBytes, maxGas, minTxsInBlock int64) types.Txs
ReapMaxBytesMaxGas(maxBytes, maxGasWanted, maxGas, minTxsInBlock int64) types.Txs

// ReapMaxTxs reaps up to max transactions from the mempool. If max is
// negative, there is no cap on the size of all returned transactions
Expand Down
2 changes: 1 addition & 1 deletion internal/state/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (blockExec *BlockExecutor) CreateProposalBlock(
// Fetch a limited amount of valid txs
maxDataBytes := types.MaxDataBytes(maxBytes, evSize, state.Validators.Size())

txs := blockExec.mempool.ReapMaxBytesMaxGas(maxDataBytes, maxGas, state.ConsensusParams.Block.MinTxsInBlock)
txs := blockExec.mempool.ReapMaxBytesMaxGas(maxDataBytes, 0, maxGas, state.ConsensusParams.Block.MinTxsInBlock)
commit := lastExtCommit.ToCommit()
block := state.MakeBlock(height, txs, commit, evidence, proposerAddr)
rpp, err := blockExec.appClient.PrepareProposal(
Expand Down
2 changes: 2 additions & 0 deletions proto/tendermint/abci/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ message BlockParams {
int64 max_gas = 2;
// Minimum txs to include in a block regardless of gas limit
int64 min_txs_in_block = 3;
// Maximum gas wanted in a block. Must be greater than or equal to -1
int64 max_gas_wanted = 4;
}

message ConsensusParams {
Expand Down
142 changes: 92 additions & 50 deletions proto/tendermint/types/params.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions proto/tendermint/types/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ message BlockParams {
int64 max_gas = 2;
// Minimum txs to include in a block regardless of gas limit
int64 min_txs_in_block = 3;
// Max gas wanted per block
// Note: must be greater or equal to -1
int64 max_gas_wanted = 4;
}

// EvidenceParams determine how we handle evidence of malfeasance.
Expand Down
Loading
Loading