Skip to content

Commit

Permalink
Add Gas Overflow Check (sei-protocol#1753)
Browse files Browse the repository at this point in the history
* Add Overflow Check

* Add unit tests
  • Loading branch information
Kbhat1 authored Jul 12, 2024
1 parent 8a11b77 commit e6b8aed
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -1844,7 +1845,13 @@ func (app *App) checkTotalBlockGasWanted(ctx sdk.Context, txs [][]byte) bool {
if isGasless {
continue
}
totalGasWanted += feeTx.GetGas()
// Check for overflow before adding
gasWanted := feeTx.GetGas()
if int64(gasWanted) < 0 || int64(totalGasWanted) > math.MaxInt64-int64(gasWanted) {
return false
}

totalGasWanted += gasWanted
if totalGasWanted > uint64(ctx.ConsensusParams().Block.MaxGas) {
// early return
return false
Expand Down
28 changes: 28 additions & 0 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/hex"
"fmt"
"math"
"math/big"
"reflect"
"testing"
Expand Down Expand Up @@ -311,6 +312,33 @@ func TestInvalidProposalWithExcessiveGasWanted(t *testing.T) {
require.Equal(t, abci.ResponseProcessProposal_REJECT, res.Status)
}

func TestOverflowGas(t *testing.T) {
tm := time.Now().UTC()
valPub := secp256k1.GenPrivKey().PubKey()

testWrapper := app.NewTestWrapper(t, tm, valPub, false)
ap := testWrapper.App
ctx := testWrapper.Ctx.WithConsensusParams(&types.ConsensusParams{
Block: &types.BlockParams{MaxGas: math.MaxInt64},
})
emptyTxBuilder := app.MakeEncodingConfig().TxConfig.NewTxBuilder()
txEncoder := app.MakeEncodingConfig().TxConfig.TxEncoder()
emptyTxBuilder.SetGasLimit(uint64(math.MaxInt64))
emptyTx, _ := txEncoder(emptyTxBuilder.GetTx())

secondEmptyTxBuilder := app.MakeEncodingConfig().TxConfig.NewTxBuilder()
secondEmptyTxBuilder.SetGasLimit(10)
secondTx, _ := txEncoder(secondEmptyTxBuilder.GetTx())

proposal := abci.RequestProcessProposal{
Txs: [][]byte{emptyTx, secondTx},
Height: 1,
}
res, err := ap.ProcessProposalHandler(ctx, &proposal)
require.Nil(t, err)
require.Equal(t, abci.ResponseProcessProposal_REJECT, res.Status)
}

func TestDecodeTransactionsConcurrently(t *testing.T) {
tm := time.Now().UTC()
valPub := secp256k1.GenPrivKey().PubKey()
Expand Down

0 comments on commit e6b8aed

Please sign in to comment.