Skip to content

Commit

Permalink
fix(audit): use block gas limit in block context (#171)
Browse files Browse the repository at this point in the history
* fix(audit): use block gas limit in block context

* test: make test setup with inf block gas meter

* fix to use consensus params if block gas meter is not found

---------

Co-authored-by: beer-1 <[email protected]>
  • Loading branch information
djm07073 and beer-1 authored Feb 27, 2025
1 parent 429c0d1 commit 316533e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion x/evm/keeper/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (k Keeper) buildBlockContext(ctx context.Context, defaultBlockCtx vm.BlockC
Time: defaultBlockCtx.Time,
Random: defaultBlockCtx.Random,
BaseFee: baseFee,
GasLimit: k.computeGasLimit(sdkCtx),
GasLimit: types.BlockGasLimit(sdkCtx),
CanTransfer: func(sd vm.StateDB, a common.Address, i *uint256.Int) bool {
if i == nil || i.IsZero() {
return true
Expand Down
25 changes: 24 additions & 1 deletion x/evm/types/gas.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package types

import "github.com/ethereum/go-ethereum/params"
import (
"math"

"github.com/ethereum/go-ethereum/params"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// CalGasUsed calculates gas used.
//
Expand All @@ -14,3 +20,20 @@ func CalGasUsed(gasBalance, gasRemaining, gasRefunded uint64) uint64 {

return gasUsed - refund
}

func BlockGasLimit(ctx sdk.Context) uint64 {
if blockGasMeter := ctx.BlockGasMeter(); blockGasMeter != nil && blockGasMeter.Limit() != math.MaxUint64 {
return blockGasMeter.Limit()
}

cp := ctx.ConsensusParams()
if cp.Block != nil {
if cp.Block.MaxGas == -1 {
return math.MaxUint64
}

return uint64(cp.Block.MaxGas)
} else {
return 0
}
}

0 comments on commit 316533e

Please sign in to comment.