Skip to content

Commit

Permalink
test for overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
davidterpay committed Dec 5, 2023
1 parent 90adbf5 commit 536319a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
8 changes: 4 additions & 4 deletions x/feemarket/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"cosmossdk.io/math"
)

// MaxRatio is the maximum ratio of the max block size to the target block size. This
// MaxBlockUtilizationRatio is the maximum ratio of the max block size to the target block size. This
// can be trivially understood to be the maximum base fee increase that can occur in between
// blocks. This is a constant that is used to prevent the base fee from increasing too quickly.
const MaxRatio = 10
const MaxBlockUtilizationRatio = 10

// NewParams instantiates a new EIP-1559 Params object. This params object is utilized
// to implement both the base EIP-1559 fee and AIMD EIP-1559 fee market implementations.
Expand Down Expand Up @@ -73,8 +73,8 @@ func (p *Params) ValidateBasic() error {
return fmt.Errorf("target block size cannot be greater than max block size")
}

if p.MaxBlockUtilization/p.TargetBlockUtilization > MaxRatio {
return fmt.Errorf("max block size cannot be greater than target block size times %d", MaxRatio)
if p.MaxBlockUtilization/p.TargetBlockUtilization > MaxBlockUtilizationRatio {
return fmt.Errorf("max block size cannot be greater than target block size times %d", MaxBlockUtilizationRatio)
}

if p.MinBaseFee.IsNil() || !p.MinBaseFee.GTE(math.ZeroInt()) {
Expand Down
26 changes: 26 additions & 0 deletions x/feemarket/types/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,32 @@ func TestState_UpdateBaseFee(t *testing.T) {
require.Equal(t, expectedLR, lr)
require.Equal(t, expectedFee, bf)
})

t.Run("recovers from overflow with large max block utilization ratio", func(t *testing.T) {
state := types.DefaultAIMDState()
state.Window = make([]uint64, 50)
state.BaseFee = state.BaseFee.Mul(math.NewInt(10))

params := types.DefaultAIMDParams()
params.Window = 50
// This should overflow the base fee after a few iterations.
params.TargetBlockUtilization = 1
params.MaxBlockUtilization = 9_999_999_999_999_999_999

for {
state.Update(params.MaxBlockUtilization, params)
state.UpdateLearningRate(params)
baseFee := state.UpdateBaseFee(params)

// An overflow should have occurred.
if baseFee.Equal(params.MinBaseFee) {
return
}

// Update the height and try again.
state.IncrementHeight()
}
})
}

func TestState_UpdateLearningRate(t *testing.T) {
Expand Down

0 comments on commit 536319a

Please sign in to comment.