Skip to content

Commit

Permalink
fix: method handler crash due to nil min fee per gas (#1982)
Browse files Browse the repository at this point in the history
* fix: method handler crash due to nil min fee per gas

* unit test
  • Loading branch information
blindchaser authored Dec 9, 2024
1 parent 5ddb9ab commit cb56268
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion x/evm/keeper/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ func (k *Keeper) GetDynamicBaseFeePerGas(ctx sdk.Context) sdk.Dec {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.BaseFeePerGasPrefix)
if bz == nil {
return k.GetMinimumFeePerGas(ctx)
minFeePerGas := k.GetMinimumFeePerGas(ctx)
if minFeePerGas.IsNil() {
minFeePerGas = types.DefaultParams().MinimumFeePerGas
}
return minFeePerGas
}
d := sdk.Dec{}
err := d.UnmarshalJSON(bz)
Expand Down
22 changes: 22 additions & 0 deletions x/evm/keeper/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
testkeeper "github.com/sei-protocol/sei-chain/testutil/keeper"
"github.com/sei-protocol/sei-chain/x/evm/types"
"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)
Expand Down Expand Up @@ -177,3 +178,24 @@ func TestAdjustBaseFeePerGas(t *testing.T) {
})
}
}

func TestGetDynamicBaseFeePerGasWithNilMinFee(t *testing.T) {
k, ctx := testkeeper.MockEVMKeeper()

// Test case 1: When dynamic base fee doesn't exist and minimum fee is nil
store := ctx.KVStore(k.GetStoreKey())
store.Delete(types.BaseFeePerGasPrefix)

// Clear the dynamic base fee from store
fee := k.GetDynamicBaseFeePerGas(ctx)
require.Equal(t, types.DefaultParams().MinimumFeePerGas, fee)
require.False(t, fee.IsNil())

// Test case 2: When dynamic base fee exists
expectedFee := sdk.NewDec(100)
k.SetDynamicBaseFeePerGas(ctx, expectedFee)

fee = k.GetDynamicBaseFeePerGas(ctx)
require.Equal(t, expectedFee, fee)
require.False(t, fee.IsNil())
}

0 comments on commit cb56268

Please sign in to comment.