From cb56268ce69a32c2ceb39e0c9fee912d23af0809 Mon Sep 17 00:00:00 2001 From: yirenz Date: Mon, 9 Dec 2024 11:00:15 -0500 Subject: [PATCH] fix: method handler crash due to nil min fee per gas (#1982) * fix: method handler crash due to nil min fee per gas * unit test --- x/evm/keeper/fee.go | 6 +++++- x/evm/keeper/fee_test.go | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/x/evm/keeper/fee.go b/x/evm/keeper/fee.go index 514b0d511d..4695b18685 100644 --- a/x/evm/keeper/fee.go +++ b/x/evm/keeper/fee.go @@ -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) diff --git a/x/evm/keeper/fee_test.go b/x/evm/keeper/fee_test.go index 78e0595422..a4088fbed0 100644 --- a/x/evm/keeper/fee_test.go +++ b/x/evm/keeper/fee_test.go @@ -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" ) @@ -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()) +}