Skip to content

Commit

Permalink
prev base fee per gas avoids returning nil to avoid method handler cr…
Browse files Browse the repository at this point in the history
…ashed on replay
  • Loading branch information
jewei1997 committed Jan 7, 2025
1 parent 6e7dffe commit 76d9f7b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
13 changes: 7 additions & 6 deletions x/evm/keeper/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ func (k *Keeper) AdjustDynamicBaseFeePerGas(ctx sdk.Context, blockGasUsed uint64
return nil
}
prevBaseFee := k.GetPrevBlockBaseFeePerGas(ctx)
if prevBaseFee.IsNil() {
prevBaseFee = types.DefaultParams().MinimumFeePerGas
}
// set the resulting base fee for block n-1 on block n
k.SetDynamicBaseFeePerGas(ctx, prevBaseFee)
targetGasUsed := sdk.NewDec(int64(k.GetTargetGasUsedPerBlock(ctx)))
if targetGasUsed.IsZero() {
return &prevBaseFee
if targetGasUsed.IsZero() { // avoid division by zero
return &prevBaseFee // return the previous base fee as is
}
minimumFeePerGas := k.GetParams(ctx).MinimumFeePerGas
maximumFeePerGas := k.GetParams(ctx).MaximumFeePerGas
Expand Down Expand Up @@ -104,7 +101,11 @@ func (k *Keeper) GetPrevBlockBaseFeePerGas(ctx sdk.Context) sdk.Dec {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.PrevBlockBaseFeePerGasPrefix)
if bz == nil {
return sdk.Dec{}
minFeePerGas := k.GetMinimumFeePerGas(ctx)
if minFeePerGas.IsNil() {
minFeePerGas = types.DefaultParams().MinimumFeePerGas
}
return minFeePerGas
}
d := sdk.Dec{}
err := d.UnmarshalJSON(bz)
Expand Down
13 changes: 13 additions & 0 deletions x/evm/keeper/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,16 @@ func TestGetDynamicBaseFeePerGasWithNilMinFee(t *testing.T) {
require.Equal(t, expectedFee, fee)
require.False(t, fee.IsNil())
}

func TestGetPrevBlockBaseFeePerGasWithNilMinFee(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())
}

0 comments on commit 76d9f7b

Please sign in to comment.