From 536319ad35e7816f7ebf57326ca3fb4a5c65e49a Mon Sep 17 00:00:00 2001 From: David Terpay Date: Tue, 5 Dec 2023 12:32:17 -0500 Subject: [PATCH] test for overflow --- x/feemarket/types/params.go | 8 ++++---- x/feemarket/types/state_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/x/feemarket/types/params.go b/x/feemarket/types/params.go index 5c3dacf..a268b6d 100644 --- a/x/feemarket/types/params.go +++ b/x/feemarket/types/params.go @@ -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. @@ -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()) { diff --git a/x/feemarket/types/state_test.go b/x/feemarket/types/state_test.go index 758bc50..f724303 100644 --- a/x/feemarket/types/state_test.go +++ b/x/feemarket/types/state_test.go @@ -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) {