diff --git a/proto/feemarket/feemarket/v1/genesis.proto b/proto/feemarket/feemarket/v1/genesis.proto index 27e202b..19fdf39 100644 --- a/proto/feemarket/feemarket/v1/genesis.proto +++ b/proto/feemarket/feemarket/v1/genesis.proto @@ -43,4 +43,12 @@ message State { // Index is the index of the current block in the block utilization window. uint64 index = 4; + + // MaxBlockUtilization is the maximum utilization of a given block. This is denominated + // in the number of gas units consumed per block. + uint64 max_block_utilization = 5; + + // TargetBlockUtilization is the target utilization of a given block. This is denominated + // in the number of gas units consumed per block. + uint64 target_block_utilization = 6; } diff --git a/x/feemarket/types/eip1559.go b/x/feemarket/types/eip1559.go index c0df26e..40ed3ed 100644 --- a/x/feemarket/types/eip1559.go +++ b/x/feemarket/types/eip1559.go @@ -66,7 +66,13 @@ func DefaultParams() Params { // DefaultState returns the default state for the EIP-1559 fee market // implementation without the AIMD learning rate adjustment algorithm. func DefaultState() State { - return NewState(DefaultWindow, DefaultMinBaseFee, DefaultMinLearningRate) + return NewState( + DefaultWindow, + DefaultTargetBlockSize, + DefaultMaxBlockSize, + DefaultMinBaseFee, + DefaultMinLearningRate, + ) } // DefaultGenesisState returns a default genesis state that implements diff --git a/x/feemarket/types/eip1559_aimd.go b/x/feemarket/types/eip1559_aimd.go index 5f8b83b..4153739 100644 --- a/x/feemarket/types/eip1559_aimd.go +++ b/x/feemarket/types/eip1559_aimd.go @@ -80,7 +80,13 @@ func DefaultAIMDParams() Params { // block utilization and dynamically adjusts the learning rate based on the // utilization within the window. func DefaultAIMDState() State { - return NewState(DefaultAIMDWindow, DefaultAIMDMinBaseFee, DefaultAIMDMinLearningRate) + return NewState( + DefaultAIMDWindow, + DefaultAIMDTargetBlockSize, + DefaultAIMDMaxBlockSize, + DefaultAIMDMinBaseFee, + DefaultAIMDMinLearningRate, + ) } // DefaultAIMDGenesisState returns a default genesis state that implements diff --git a/x/feemarket/types/genesis.pb.go b/x/feemarket/types/genesis.pb.go index accc279..6bc0172 100644 --- a/x/feemarket/types/genesis.pb.go +++ b/x/feemarket/types/genesis.pb.go @@ -31,7 +31,7 @@ type GenesisState struct { // can be utilized to implement both the base EIP-1559 fee market and // and the AIMD EIP-1559 fee market. Params Params `protobuf:"bytes,1,opt,name=params,proto3" json:"params"` - // Utilization contains the current state of the AIMD fee market. + // State contains the current state of the AIMD fee market. State State `protobuf:"bytes,2,opt,name=state,proto3" json:"state"` } @@ -86,17 +86,22 @@ func (m *GenesisState) GetState() State { // the current base fee, learning rate, and block utilization within the specified // AIMD window. type State struct { - // BaseFee is the current base fee. This is denominated in the fee - // per gas unit. + // BaseFee is the current base fee. This is denominated in the fee per gas unit. BaseFee cosmossdk_io_math.Int `protobuf:"bytes,1,opt,name=base_fee,json=baseFee,proto3,customtype=cosmossdk.io/math.Int" json:"base_fee"` // LearningRate is the current learning rate. LearningRate cosmossdk_io_math.LegacyDec `protobuf:"bytes,2,opt,name=learning_rate,json=learningRate,proto3,customtype=cosmossdk.io/math.LegacyDec" json:"learning_rate"` - // Window contains a list of the last blocks' utilization - // values. This is used to calculate the next base fee. This - // stores the number of units of gas consumed per block. + // Window contains a list of the last blocks' utilization values. This is used to + // calculate the next base fee. This stores the number of units of gas consumed + // per block. Window []uint64 `protobuf:"varint,3,rep,packed,name=window,proto3" json:"window,omitempty"` // Index is the index of the current block in the block utilization window. Index uint64 `protobuf:"varint,4,opt,name=index,proto3" json:"index,omitempty"` + // MaxBlockUtilization is the maximum utilization of a given block. This is denominated + // in the number of gas units consumed per block. + MaxBlockUtilization uint64 `protobuf:"varint,5,opt,name=max_block_utilization,json=maxBlockUtilization,proto3" json:"max_block_utilization,omitempty"` + // TargetBlockUtilization is the target utilization of a given block. This is denominated + // in the number of gas units consumed per block. + TargetBlockUtilization uint64 `protobuf:"varint,6,opt,name=target_block_utilization,json=targetBlockUtilization,proto3" json:"target_block_utilization,omitempty"` } func (m *State) Reset() { *m = State{} } @@ -146,6 +151,20 @@ func (m *State) GetIndex() uint64 { return 0 } +func (m *State) GetMaxBlockUtilization() uint64 { + if m != nil { + return m.MaxBlockUtilization + } + return 0 +} + +func (m *State) GetTargetBlockUtilization() uint64 { + if m != nil { + return m.TargetBlockUtilization + } + return 0 +} + func init() { proto.RegisterType((*GenesisState)(nil), "feemarket.feemarket.v1.GenesisState") proto.RegisterType((*State)(nil), "feemarket.feemarket.v1.State") @@ -156,31 +175,34 @@ func init() { } var fileDescriptor_2180652c84279298 = []byte{ - // 374 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x51, 0xcf, 0x6a, 0xe2, 0x40, - 0x18, 0xcf, 0xac, 0xd1, 0x5d, 0x67, 0xf5, 0x12, 0x5c, 0xc9, 0xba, 0x6c, 0x14, 0x77, 0x0f, 0xc2, - 0x62, 0x82, 0xbb, 0x7b, 0x29, 0xf4, 0x14, 0x8a, 0x45, 0xe8, 0xa1, 0xa4, 0xa7, 0xf6, 0x22, 0x63, - 0xfc, 0x8c, 0xc1, 0x26, 0x13, 0x32, 0x53, 0xff, 0x3c, 0x41, 0xaf, 0x7d, 0x18, 0x1f, 0xc2, 0xa3, - 0x78, 0x92, 0x1e, 0xa4, 0xe8, 0x8b, 0x94, 0x64, 0xc6, 0x2a, 0xb4, 0xde, 0x7e, 0xdf, 0x7c, 0xbf, - 0x7f, 0xc3, 0x87, 0x7f, 0x0f, 0x00, 0x02, 0x12, 0x8f, 0x80, 0x5b, 0x07, 0x34, 0x6e, 0x59, 0x1e, - 0x84, 0xc0, 0x7c, 0x66, 0x46, 0x31, 0xe5, 0x54, 0x2b, 0xbf, 0xed, 0xcc, 0x03, 0x1a, 0xb7, 0x2a, - 0x25, 0x8f, 0x7a, 0x34, 0xa5, 0x58, 0x09, 0x12, 0xec, 0xca, 0x77, 0x97, 0xb2, 0x80, 0xb2, 0xae, - 0x58, 0x88, 0x41, 0xae, 0x7e, 0x9d, 0x88, 0x8b, 0x48, 0x4c, 0x02, 0x49, 0xaa, 0x3f, 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 0x0c, 0x65, 0xb9, 0x33, 0x94, 0xf5, 0xce, 0x50, 0xee, 0x2c, 0xcf, 0xe7, 0xc3, - 0x87, 0x9e, 0xe9, 0xd2, 0xc0, 0x62, 0x23, 0x3f, 0x6a, 0x06, 0x30, 0x3e, 0xba, 0xd6, 0xf4, 0x08, - 0xf3, 0x59, 0x04, 0xac, 0x97, 0x4b, 0xcf, 0xf6, 0xef, 0x35, 0x00, 0x00, 0xff, 0xff, 0xd6, 0xcb, - 0x9b, 0x50, 0x4c, 0x02, 0x00, 0x00, + // 429 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x92, 0x41, 0x6b, 0xd4, 0x40, + 0x18, 0x86, 0x93, 0x36, 0x89, 0x76, 0x6c, 0x2f, 0xb1, 0x5d, 0x62, 0xc5, 0x74, 0xa9, 0x1e, 0x16, + 0x64, 0x13, 0x5a, 0x3d, 0x28, 0x78, 0x0a, 0x52, 0x59, 0xf0, 0x20, 0x11, 0x0f, 0x7a, 0x09, 0x93, + 0xec, 0xd7, 0x74, 0xc8, 0xce, 0x4c, 0xc8, 0x4c, 0xb7, 0xa9, 0x7f, 0xc0, 0xab, 0x3f, 0xa6, 0xbf, + 0x41, 0x7a, 0x2c, 0x3d, 0x89, 0x87, 0x22, 0xbb, 0x7f, 0x44, 0x32, 0x33, 0x75, 0x8b, 0xee, 0xde, + 0xbe, 0x2f, 0xef, 0xfb, 0xbc, 0xdf, 0x1b, 0x18, 0xf4, 0xec, 0x18, 0x80, 0xe2, 0xa6, 0x02, 0x19, + 0x2f, 0xa6, 0xe9, 0x41, 0x5c, 0x02, 0x03, 0x41, 0x44, 0x54, 0x37, 0x5c, 0x72, 0xbf, 0xf7, 0x57, + 0x8b, 0x16, 0xd3, 0xf4, 0x60, 0x77, 0xbb, 0xe4, 0x25, 0x57, 0x96, 0xb8, 0x9b, 0xb4, 0x7b, 0xf7, + 0x51, 0xc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0, 0xe9, 0xdb, 0x03, 0x27, 0xd5, 0x8b, 0x7f, 0x88, + 0x76, 0x28, 0x6e, 0xb3, 0x7c, 0xc2, 0x8b, 0x2a, 0x3b, 0x95, 0x64, 0x42, 0xbe, 0x62, 0x49, 0x38, + 0x0b, 0x5c, 0xe5, 0x7a, 0x48, 0x71, 0x9b, 0x74, 0xda, 0xa7, 0x85, 0xe4, 0xbf, 0x42, 0x81, 0xc4, + 0x4d, 0x09, 0x72, 0x09, 0xe6, 0x29, 0xac, 0xa7, 0xf5, 0x7f, 0xc9, 0x64, 0x74, 0x39, 0x0b, 0xed, + 0xab, 0x59, 0x68, 0xff, 0x9e, 0x85, 0xf6, 0xf7, 0x79, 0x68, 0x5d, 0xcd, 0x43, 0xeb, 0xe7, 0x3c, + 0xb4, 0xbe, 0xc4, 0x25, 0x91, 0x27, 0xa7, 0x79, 0x54, 0x70, 0x1a, 0x8b, 0x8a, 0xd4, 0x43, 0x0a, + 0xd3, 0x3b, 0x6f, 0xa3, 0xbd, 0x33, 0xcb, 0xf3, 0x1a, 0x44, 0xee, 0xa9, 0x47, 0xf2, 0xe2, 0x4f, + 0x00, 0x00, 0x00, 0xff, 0xff, 0x04, 0x03, 0x84, 0x7d, 0xba, 0x02, 0x00, 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -246,6 +268,16 @@ func (m *State) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.TargetBlockUtilization != 0 { + i = encodeVarintGenesis(dAtA, i, uint64(m.TargetBlockUtilization)) + i-- + dAtA[i] = 0x30 + } + if m.MaxBlockUtilization != 0 { + i = encodeVarintGenesis(dAtA, i, uint64(m.MaxBlockUtilization)) + i-- + dAtA[i] = 0x28 + } if m.Index != 0 { i = encodeVarintGenesis(dAtA, i, uint64(m.Index)) i-- @@ -336,6 +368,12 @@ func (m *State) Size() (n int) { if m.Index != 0 { n += 1 + sovGenesis(uint64(m.Index)) } + if m.MaxBlockUtilization != 0 { + n += 1 + sovGenesis(uint64(m.MaxBlockUtilization)) + } + if m.TargetBlockUtilization != 0 { + n += 1 + sovGenesis(uint64(m.TargetBlockUtilization)) + } return n } @@ -653,6 +691,44 @@ func (m *State) Unmarshal(dAtA []byte) error { break } } + case 5: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field MaxBlockUtilization", wireType) + } + m.MaxBlockUtilization = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.MaxBlockUtilization |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 6: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field TargetBlockUtilization", wireType) + } + m.TargetBlockUtilization = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.TargetBlockUtilization |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := skipGenesis(dAtA[iNdEx:]) diff --git a/x/feemarket/types/state.go b/x/feemarket/types/state.go index 879c37d..972f959 100644 --- a/x/feemarket/types/state.go +++ b/x/feemarket/types/state.go @@ -9,41 +9,64 @@ import ( // NewState instantiates a new fee market state instance. This is utilized // to implement both the base EIP-1559 fee market implementation and the // AIMD EIP-1559 fee market implementation. -func NewState(window uint64, baseFee math.Int, learningRate math.LegacyDec) State { +func NewState( + window, + target, max uint64, + baseFee math.Int, + learningRate math.LegacyDec, +) State { return State{ - Window: make([]uint64, window), - BaseFee: baseFee, - LearningRate: learningRate, - Index: 0, + Window: make([]uint64, window), + BaseFee: baseFee, + LearningRate: learningRate, + Index: 0, + TargetBlockUtilization: target, + MaxBlockUtilization: max, } } +// Update updates the block utilization for the current height with the given +// transaction utilization i.e. gas limit. +func (s *State) Update(gas uint64) error { + update := s.Window[s.Index] + gas + if update > s.MaxBlockUtilization { + return fmt.Errorf("block utilization cannot exceed max block utilization") + } + + s.Window[s.Index] = update + return nil +} + +// IncrementHeight increments the current height of the state. This is +// utilized to update the block utilization window. +func (s *State) IncrementHeight() { + s.Index = (s.Index + 1) % uint64(len(s.Window)) + s.Window[s.Index] = 0 +} + // UpdateBaseFee updates the learning rate and base fee based on the AIMD // learning rate adjustment algorithm. The learning rate is updated // based on the average utilization of the block window. The base fee is // update using the new learning rate and the delta adjustment. Please // see the EIP-1559 specification for more details. -func (s *State) UpdateBaseFee(params Params) math.Int { - // Update the learning rate. - newLR := s.UpdateLearningRate(params) - +func (s *State) UpdateBaseFee(delta math.LegacyDec) math.Int { // Calculate the new base fee with the learning rate adjustment. currentBlockSize := math.LegacyNewDecFromInt(math.NewIntFromUint64(s.Window[s.Index])) - targetBlockSize := math.LegacyNewDecFromInt(math.NewIntFromUint64(params.TargetBlockUtilization)) + targetBlockSize := math.LegacyNewDecFromInt(math.NewIntFromUint64(s.TargetBlockUtilization)) utilization := (currentBlockSize.Sub(targetBlockSize)).Quo(targetBlockSize) // Truncate the learning rate adjustment to an integer. // // This is equivalent to // 1 + (learningRate * (currentBlockSize - targetBlockSize) / targetBlockSize) - learningRateAdjustment := math.LegacyOneDec().Add(newLR.Mul(utilization)) + learningRateAdjustment := math.LegacyOneDec().Add(s.LearningRate.Mul(utilization)) // Calculate the delta adjustment. - net := s.GetNetUtilization(params.TargetBlockUtilization) - delta := params.Delta.Mul(math.LegacyNewDecFromInt(net)) + net := math.LegacyNewDecFromInt(s.GetNetUtilization()).Mul(delta) // Update the base fee. - s.BaseFee = (math.LegacyNewDecFromInt(s.BaseFee).Mul(learningRateAdjustment)).Add(delta).TruncateInt() + fee := (math.LegacyNewDecFromInt(s.BaseFee).Mul(learningRateAdjustment)).Add(net).TruncateInt() + s.BaseFee = fee return s.BaseFee } @@ -60,22 +83,22 @@ func (s *State) UpdateBaseFee(params Params) math.Int { // when blocks are relatively close to the target block utilization. // // For more details, please see the EIP-1559 specification. -func (s *State) UpdateLearningRate(params Params) math.LegacyDec { +func (s *State) UpdateLearningRate(theta, alpha, beta, minLR, maxLR math.LegacyDec) math.LegacyDec { // Calculate the average utilization of the block window. - avg := s.GetAverageUtilization(params.MaxBlockUtilization) + avg := s.GetAverageUtilization() // Determine if the average utilization is above or below the target // threshold and adjust the learning rate accordingly. var updatedLearningRate math.LegacyDec - if avg.LTE(params.Theta) || avg.GTE(math.LegacyOneDec().Sub(params.Theta)) { - updatedLearningRate = params.Alpha.Add(s.LearningRate) - if updatedLearningRate.GT(params.MaxLearningRate) { - updatedLearningRate = params.MaxLearningRate + if avg.LTE(theta) || avg.GTE(math.LegacyOneDec().Sub(theta)) { + updatedLearningRate = alpha.Add(s.LearningRate) + if updatedLearningRate.GT(maxLR) { + updatedLearningRate = maxLR } } else { - updatedLearningRate = s.LearningRate.Mul(params.Beta) - if updatedLearningRate.LT(params.MinLearningRate) { - updatedLearningRate = params.MinLearningRate + updatedLearningRate = s.LearningRate.Mul(beta) + if updatedLearningRate.LT(minLR) { + updatedLearningRate = minLR } } @@ -85,10 +108,10 @@ func (s *State) UpdateLearningRate(params Params) math.LegacyDec { } // GetNetUtilization returns the net utilization of the block window. -func (s *State) GetNetUtilization(target uint64) math.Int { +func (s *State) GetNetUtilization() math.Int { net := math.NewInt(0) - targetUtilization := math.NewIntFromUint64(target) + targetUtilization := math.NewIntFromUint64(s.TargetBlockUtilization) for _, utilization := range s.Window { diff := math.NewIntFromUint64(utilization).Sub(targetUtilization) net = net.Add(diff) @@ -99,7 +122,7 @@ func (s *State) GetNetUtilization(target uint64) math.Int { // GetAverageUtilization returns the average utilization of the block // window. -func (s *State) GetAverageUtilization(max uint64) math.LegacyDec { +func (s *State) GetAverageUtilization() math.LegacyDec { var total uint64 for _, utilization := range s.Window { total += utilization @@ -108,13 +131,25 @@ func (s *State) GetAverageUtilization(max uint64) math.LegacyDec { sum := math.LegacyNewDecFromInt(math.NewIntFromUint64(total)) multiple := math.LegacyNewDecFromInt(math.NewIntFromUint64(uint64(len(s.Window)))) - divisor := math.LegacyNewDecFromInt(math.NewIntFromUint64(max)).Mul(multiple) + divisor := math.LegacyNewDecFromInt(math.NewIntFromUint64(s.MaxBlockUtilization)).Mul(multiple) return sum.Quo(divisor) } // ValidateBasic performs basic validation on the state. func (s *State) ValidateBasic() error { + if s.MaxBlockUtilization == 0 { + return fmt.Errorf("max utilization cannot be zero") + } + + if s.TargetBlockUtilization == 0 { + return fmt.Errorf("target utilization cannot be zero") + } + + if s.TargetBlockUtilization > s.MaxBlockUtilization { + return fmt.Errorf("target utilization cannot be greater than max utilization") + } + if s.Window == nil || len(s.Window) == 0 { return fmt.Errorf("block utilization window cannot be nil or empty") } diff --git a/x/feemarket/types/state_test.go b/x/feemarket/types/state_test.go index 7e213e0..9fab5b9 100644 --- a/x/feemarket/types/state_test.go +++ b/x/feemarket/types/state_test.go @@ -19,7 +19,7 @@ func TestState_UpdateBaseFee(t *testing.T) { params := types.DefaultParams() - newBaseFee := state.UpdateBaseFee(params) + newBaseFee := state.UpdateBaseFee(params.Delta) expectedBaseFee := math.NewInt(875) require.True(t, expectedBaseFee.Equal(newBaseFee)) }) @@ -32,7 +32,7 @@ func TestState_UpdateBaseFee(t *testing.T) { state.Window[0] = params.TargetBlockUtilization - newBaseFee := state.UpdateBaseFee(params) + newBaseFee := state.UpdateBaseFee(params.Delta) expectedBaseFee := math.NewInt(1000) require.True(t, expectedBaseFee.Equal(newBaseFee)) }) @@ -45,7 +45,7 @@ func TestState_UpdateBaseFee(t *testing.T) { state.Window[0] = params.MaxBlockUtilization - newBaseFee := state.UpdateBaseFee(params) + newBaseFee := state.UpdateBaseFee(params.Delta) expectedBaseFee := math.NewInt(1125) require.True(t, expectedBaseFee.Equal(newBaseFee)) }) @@ -57,7 +57,9 @@ func TestState_UpdateBaseFee(t *testing.T) { params := types.DefaultAIMDParams() - newBaseFee := state.UpdateBaseFee(params) + state.UpdateLearningRate(params.Theta, params.Alpha, params.Beta, params.MinLearningRate, params.MaxLearningRate) + newBaseFee := state.UpdateBaseFee(params.Delta) + expectedBaseFee := math.NewInt(850) require.True(t, expectedBaseFee.Equal(newBaseFee)) }) @@ -73,7 +75,9 @@ func TestState_UpdateBaseFee(t *testing.T) { state.Window[i] = params.TargetBlockUtilization } - newBaseFee := state.UpdateBaseFee(params) + state.UpdateLearningRate(params.Theta, params.Alpha, params.Beta, params.MinLearningRate, params.MaxLearningRate) + newBaseFee := state.UpdateBaseFee(params.Delta) + expectedBaseFee := math.NewInt(1000) require.True(t, expectedBaseFee.Equal(newBaseFee)) }) @@ -89,7 +93,9 @@ func TestState_UpdateBaseFee(t *testing.T) { state.Window[i] = params.MaxBlockUtilization } - newBaseFee := state.UpdateBaseFee(params) + state.UpdateLearningRate(params.Theta, params.Alpha, params.Beta, params.MinLearningRate, params.MaxLearningRate) + newBaseFee := state.UpdateBaseFee(params.Delta) + expectedBaseFee := math.NewInt(1150) require.True(t, expectedBaseFee.Equal(newBaseFee)) }) @@ -100,7 +106,7 @@ func TestState_UpdateLearningRate(t *testing.T) { state := types.DefaultState() params := types.DefaultParams() - state.UpdateLearningRate(params) + state.UpdateLearningRate(params.Theta, params.Alpha, params.Beta, params.MinLearningRate, params.MaxLearningRate) expectedLearningRate := math.LegacyMustNewDecFromStr("0.125") require.True(t, expectedLearningRate.Equal(state.LearningRate)) }) @@ -109,9 +115,9 @@ func TestState_UpdateLearningRate(t *testing.T) { state := types.DefaultState() params := types.DefaultParams() - state.Window[0] = params.TargetBlockUtilization + state.Window[0] = state.TargetBlockUtilization - state.UpdateLearningRate(params) + state.UpdateLearningRate(params.Theta, params.Alpha, params.Beta, params.MinLearningRate, params.MaxLearningRate) expectedLearningRate := math.LegacyMustNewDecFromStr("0.125") require.True(t, expectedLearningRate.Equal(state.LearningRate)) }) @@ -120,9 +126,9 @@ func TestState_UpdateLearningRate(t *testing.T) { state := types.DefaultState() params := types.DefaultParams() - state.Window[0] = params.MaxBlockUtilization + state.Window[0] = state.MaxBlockUtilization - state.UpdateLearningRate(params) + state.UpdateLearningRate(params.Theta, params.Alpha, params.Beta, params.MinLearningRate, params.MaxLearningRate) expectedLearningRate := math.LegacyMustNewDecFromStr("0.125") require.True(t, expectedLearningRate.Equal(state.LearningRate)) }) @@ -133,7 +139,7 @@ func TestState_UpdateLearningRate(t *testing.T) { state.Window[0] = 50000 - state.UpdateLearningRate(params) + state.UpdateLearningRate(params.Theta, params.Alpha, params.Beta, params.MinLearningRate, params.MaxLearningRate) expectedLearningRate := math.LegacyMustNewDecFromStr("0.125") require.True(t, expectedLearningRate.Equal(state.LearningRate)) }) @@ -142,9 +148,9 @@ func TestState_UpdateLearningRate(t *testing.T) { state := types.DefaultState() params := types.DefaultParams() - state.Window[0] = 100000 + state.Window[0] = state.TargetBlockUtilization + 50000 - state.UpdateLearningRate(params) + state.UpdateLearningRate(params.Theta, params.Alpha, params.Beta, params.MinLearningRate, params.MaxLearningRate) expectedLearningRate := math.LegacyMustNewDecFromStr("0.125") require.True(t, expectedLearningRate.Equal(state.LearningRate)) }) @@ -156,7 +162,7 @@ func TestState_UpdateLearningRate(t *testing.T) { randomValue := rand.Int63n(1000000000) state.Window[0] = uint64(randomValue) - state.UpdateLearningRate(params) + state.UpdateLearningRate(params.Theta, params.Alpha, params.Beta, params.MinLearningRate, params.MaxLearningRate) expectedLearningRate := math.LegacyMustNewDecFromStr("0.125") require.True(t, expectedLearningRate.Equal(state.LearningRate)) }) @@ -165,7 +171,7 @@ func TestState_UpdateLearningRate(t *testing.T) { state := types.DefaultAIMDState() params := types.DefaultAIMDParams() - state.UpdateLearningRate(params) + state.UpdateLearningRate(params.Theta, params.Alpha, params.Beta, params.MinLearningRate, params.MaxLearningRate) expectedLearningRate := params.MinLearningRate.Add(params.Alpha) require.True(t, expectedLearningRate.Equal(state.LearningRate)) }) @@ -178,10 +184,10 @@ func TestState_UpdateLearningRate(t *testing.T) { params := types.DefaultAIMDParams() for i := 0; i < len(state.Window); i++ { - state.Window[i] = params.TargetBlockUtilization + state.Window[i] = state.TargetBlockUtilization } - state.UpdateLearningRate(params) + state.UpdateLearningRate(params.Theta, params.Alpha, params.Beta, params.MinLearningRate, params.MaxLearningRate) expectedLearningRate := defaultLR.Mul(params.Beta) require.True(t, expectedLearningRate.Equal(state.LearningRate)) }) @@ -194,10 +200,10 @@ func TestState_UpdateLearningRate(t *testing.T) { params := types.DefaultAIMDParams() for i := 0; i < len(state.Window); i++ { - state.Window[i] = params.MaxBlockUtilization + state.Window[i] = state.MaxBlockUtilization } - state.UpdateLearningRate(params) + state.UpdateLearningRate(params.Theta, params.Alpha, params.Beta, params.MinLearningRate, params.MaxLearningRate) expectedLearningRate := defaultLR.Add(params.Alpha) require.True(t, expectedLearningRate.Equal(state.LearningRate)) }) @@ -211,13 +217,13 @@ func TestState_UpdateLearningRate(t *testing.T) { for i := 0; i < len(state.Window); i++ { if i%2 == 0 { - state.Window[i] = params.MaxBlockUtilization + state.Window[i] = state.MaxBlockUtilization } else { state.Window[i] = 0 } } - state.UpdateLearningRate(params) + state.UpdateLearningRate(params.Theta, params.Alpha, params.Beta, params.MinLearningRate, params.MaxLearningRate) expectedLearningRate := defaultLR.Mul(params.Beta) require.True(t, expectedLearningRate.Equal(state.LearningRate)) }) @@ -231,13 +237,13 @@ func TestState_UpdateLearningRate(t *testing.T) { for i := 0; i < len(state.Window); i++ { if i%2 == 0 { - state.Window[i] = params.MaxBlockUtilization + state.Window[i] = state.MaxBlockUtilization } else { - state.Window[i] = params.TargetBlockUtilization + 1 + state.Window[i] = state.TargetBlockUtilization + 1 } } - state.UpdateLearningRate(params) + state.UpdateLearningRate(params.Theta, params.Alpha, params.Beta, params.MinLearningRate, params.MaxLearningRate) expectedLearningRate := defaultLR.Add(params.Alpha) require.True(t, expectedLearningRate.Equal(state.LearningRate)) }) @@ -246,74 +252,68 @@ func TestState_UpdateLearningRate(t *testing.T) { func TestState_GetNetUtilization(t *testing.T) { t.Run("empty block with default eip-1559", func(t *testing.T) { state := types.DefaultState() - params := types.DefaultParams() - netUtilization := state.GetNetUtilization(params.TargetBlockUtilization) - expectedUtilization := math.NewInt(0).Sub(math.NewIntFromUint64(params.TargetBlockUtilization)) + netUtilization := state.GetNetUtilization() + expectedUtilization := math.NewInt(0).Sub(math.NewIntFromUint64(state.TargetBlockUtilization)) require.True(t, expectedUtilization.Equal(netUtilization)) }) t.Run("target block with default eip-1559", func(t *testing.T) { state := types.DefaultState() - params := types.DefaultParams() - state.Window[0] = params.TargetBlockUtilization + state.Window[0] = state.TargetBlockUtilization - netUtilization := state.GetNetUtilization(params.TargetBlockUtilization) + netUtilization := state.GetNetUtilization() expectedUtilization := math.NewInt(0) require.True(t, expectedUtilization.Equal(netUtilization)) }) t.Run("full block with default eip-1559", func(t *testing.T) { state := types.DefaultState() - params := types.DefaultParams() - state.Window[0] = params.MaxBlockUtilization + state.Window[0] = state.MaxBlockUtilization - netUtilization := state.GetNetUtilization(params.TargetBlockUtilization) - expectedUtilization := math.NewIntFromUint64(params.MaxBlockUtilization - params.TargetBlockUtilization) + netUtilization := state.GetNetUtilization() + expectedUtilization := math.NewIntFromUint64(state.MaxBlockUtilization - state.TargetBlockUtilization) require.True(t, expectedUtilization.Equal(netUtilization)) }) t.Run("empty block with default aimd eip-1559", func(t *testing.T) { state := types.DefaultAIMDState() - params := types.DefaultAIMDParams() - netUtilization := state.GetNetUtilization(params.TargetBlockUtilization) + netUtilization := state.GetNetUtilization() - multiple := math.NewIntFromUint64(params.Window) - expectedUtilization := math.NewInt(0).Sub(math.NewIntFromUint64(params.TargetBlockUtilization)).Mul(multiple) + multiple := math.NewIntFromUint64(uint64(len(state.Window))) + expectedUtilization := math.NewInt(0).Sub(math.NewIntFromUint64(state.TargetBlockUtilization)).Mul(multiple) require.True(t, expectedUtilization.Equal(netUtilization)) }) t.Run("full blocks with default aimd eip-1559", func(t *testing.T) { state := types.DefaultAIMDState() - params := types.DefaultAIMDParams() for i := 0; i < len(state.Window); i++ { - state.Window[i] = params.MaxBlockUtilization + state.Window[i] = state.MaxBlockUtilization } - netUtilization := state.GetNetUtilization(params.TargetBlockUtilization) + netUtilization := state.GetNetUtilization() - multiple := math.NewIntFromUint64(params.Window) - expectedUtilization := math.NewIntFromUint64(params.MaxBlockUtilization).Sub(math.NewIntFromUint64(params.TargetBlockUtilization)).Mul(multiple) + multiple := math.NewIntFromUint64(uint64(len(state.Window))) + expectedUtilization := math.NewIntFromUint64(state.MaxBlockUtilization).Sub(math.NewIntFromUint64(state.TargetBlockUtilization)).Mul(multiple) require.True(t, expectedUtilization.Equal(netUtilization)) }) t.Run("varying blocks with default aimd eip-1559", func(t *testing.T) { state := types.DefaultAIMDState() - params := types.DefaultAIMDParams() for i := 0; i < len(state.Window); i++ { if i%2 == 0 { - state.Window[i] = params.MaxBlockUtilization + state.Window[i] = state.MaxBlockUtilization } else { state.Window[i] = 0 } } - netUtilization := state.GetNetUtilization(params.TargetBlockUtilization) + netUtilization := state.GetNetUtilization() expectedUtilization := math.ZeroInt() require.True(t, expectedUtilization.Equal(netUtilization)) }) @@ -330,7 +330,7 @@ func TestState_GetNetUtilization(t *testing.T) { } } - netUtilization := state.GetNetUtilization(params.TargetBlockUtilization) + netUtilization := state.GetNetUtilization() first := math.NewIntFromUint64(params.MaxBlockUtilization).Mul(math.NewIntFromUint64(params.Window / 2)) second := math.NewIntFromUint64(params.TargetBlockUtilization).Mul(math.NewIntFromUint64(params.Window / 2)) expectedUtilization := first.Add(second).Sub(math.NewIntFromUint64(params.TargetBlockUtilization).Mul(math.NewIntFromUint64(params.Window))) @@ -351,7 +351,7 @@ func TestState_GetNetUtilization(t *testing.T) { state.Window[2] = 0 state.Window[3] = 50 - netUtilization := state.GetNetUtilization(params.TargetBlockUtilization) + netUtilization := state.GetNetUtilization() expectedUtilization := math.NewIntFromUint64(50).Mul(math.NewInt(-1)) require.True(t, expectedUtilization.Equal(netUtilization)) }) @@ -370,7 +370,7 @@ func TestState_GetNetUtilization(t *testing.T) { state.Window[2] = 50 state.Window[3] = 75 - netUtilization := state.GetNetUtilization(params.TargetBlockUtilization) + netUtilization := state.GetNetUtilization() expectedUtilization := math.NewIntFromUint64(250).Mul(math.NewInt(-1)) require.True(t, expectedUtilization.Equal(netUtilization)) }) @@ -379,100 +379,92 @@ func TestState_GetNetUtilization(t *testing.T) { func TestState_GetAverageUtilization(t *testing.T) { t.Run("empty block with default eip-1559", func(t *testing.T) { state := types.DefaultState() - params := types.DefaultParams() - avgUtilization := state.GetAverageUtilization(params.MaxBlockUtilization) + avgUtilization := state.GetAverageUtilization() expectedUtilization := math.LegacyZeroDec() require.True(t, expectedUtilization.Equal(avgUtilization)) }) t.Run("target block with default eip-1559", func(t *testing.T) { state := types.DefaultState() - params := types.DefaultParams() - state.Window[0] = params.TargetBlockUtilization + state.Window[0] = state.TargetBlockUtilization - avgUtilization := state.GetAverageUtilization(params.MaxBlockUtilization) + avgUtilization := state.GetAverageUtilization() expectedUtilization := math.LegacyMustNewDecFromStr("0.5") require.True(t, expectedUtilization.Equal(avgUtilization)) }) t.Run("full block with default eip-1559", func(t *testing.T) { state := types.DefaultState() - params := types.DefaultParams() - state.Window[0] = params.MaxBlockUtilization + state.Window[0] = state.MaxBlockUtilization - avgUtilization := state.GetAverageUtilization(params.MaxBlockUtilization) + avgUtilization := state.GetAverageUtilization() expectedUtilization := math.LegacyMustNewDecFromStr("1.0") require.True(t, expectedUtilization.Equal(avgUtilization)) }) t.Run("empty block with default aimd eip-1559", func(t *testing.T) { state := types.DefaultAIMDState() - params := types.DefaultAIMDParams() - avgUtilization := state.GetAverageUtilization(params.MaxBlockUtilization) + avgUtilization := state.GetAverageUtilization() expectedUtilization := math.LegacyZeroDec() require.True(t, expectedUtilization.Equal(avgUtilization)) }) t.Run("target block with default aimd eip-1559", func(t *testing.T) { state := types.DefaultAIMDState() - params := types.DefaultAIMDParams() for i := 0; i < len(state.Window); i++ { - state.Window[i] = params.TargetBlockUtilization + state.Window[i] = state.TargetBlockUtilization } - avgUtilization := state.GetAverageUtilization(params.MaxBlockUtilization) + avgUtilization := state.GetAverageUtilization() expectedUtilization := math.LegacyMustNewDecFromStr("0.5") require.True(t, expectedUtilization.Equal(avgUtilization)) }) t.Run("full blocks with default aimd eip-1559", func(t *testing.T) { state := types.DefaultAIMDState() - params := types.DefaultAIMDParams() for i := 0; i < len(state.Window); i++ { - state.Window[i] = params.MaxBlockUtilization + state.Window[i] = state.MaxBlockUtilization } - avgUtilization := state.GetAverageUtilization(params.MaxBlockUtilization) + avgUtilization := state.GetAverageUtilization() expectedUtilization := math.LegacyMustNewDecFromStr("1.0") require.True(t, expectedUtilization.Equal(avgUtilization)) }) t.Run("varying blocks with default aimd eip-1559", func(t *testing.T) { state := types.DefaultAIMDState() - params := types.DefaultAIMDParams() for i := 0; i < len(state.Window); i++ { if i%2 == 0 { - state.Window[i] = params.MaxBlockUtilization + state.Window[i] = state.MaxBlockUtilization } else { state.Window[i] = 0 } } - avgUtilization := state.GetAverageUtilization(params.MaxBlockUtilization) + avgUtilization := state.GetAverageUtilization() expectedUtilization := math.LegacyMustNewDecFromStr("0.5") require.True(t, expectedUtilization.Equal(avgUtilization)) }) t.Run("exceeds target rate with default aimd eip-1559", func(t *testing.T) { state := types.DefaultAIMDState() - params := types.DefaultAIMDParams() for i := 0; i < len(state.Window); i++ { if i%2 == 0 { - state.Window[i] = params.MaxBlockUtilization + state.Window[i] = state.MaxBlockUtilization } else { - state.Window[i] = params.TargetBlockUtilization + state.Window[i] = state.TargetBlockUtilization } } - avgUtilization := state.GetAverageUtilization(params.MaxBlockUtilization) + avgUtilization := state.GetAverageUtilization() expectedUtilization := math.LegacyMustNewDecFromStr("0.75") require.True(t, expectedUtilization.Equal(avgUtilization)) }) @@ -480,18 +472,15 @@ func TestState_GetAverageUtilization(t *testing.T) { t.Run("state with 4 entries in window with different updates", func(t *testing.T) { state := types.DefaultAIMDState() state.Window = make([]uint64, 4) - - params := types.DefaultAIMDParams() - params.Window = 4 - params.TargetBlockUtilization = 100 - params.MaxBlockUtilization = 200 + state.TargetBlockUtilization = 100 + state.MaxBlockUtilization = 200 state.Window[0] = 100 state.Window[1] = 200 state.Window[2] = 0 state.Window[3] = 50 - avgUtilization := state.GetAverageUtilization(params.MaxBlockUtilization) + avgUtilization := state.GetAverageUtilization() expectedUtilization := math.LegacyMustNewDecFromStr("0.4375") require.True(t, expectedUtilization.Equal(avgUtilization)) }) @@ -502,15 +491,15 @@ func TestState_GetAverageUtilization(t *testing.T) { params := types.DefaultAIMDParams() params.Window = 4 - params.TargetBlockUtilization = 100 - params.MaxBlockUtilization = 200 + state.TargetBlockUtilization = 100 + state.MaxBlockUtilization = 200 state.Window[0] = 0 state.Window[1] = 25 state.Window[2] = 50 state.Window[3] = 75 - avgUtilization := state.GetAverageUtilization(params.MaxBlockUtilization) + avgUtilization := state.GetAverageUtilization() expectedUtilization := math.LegacyMustNewDecFromStr("0.1875") require.True(t, expectedUtilization.Equal(avgUtilization)) }) @@ -557,9 +546,11 @@ func TestState_ValidateBasic(t *testing.T) { { name: "valid other state", state: types.State{ - Window: make([]uint64, 1), - BaseFee: math.NewInt(1), - LearningRate: math.LegacyMustNewDecFromStr("0.5"), + Window: make([]uint64, 1), + BaseFee: math.NewInt(1), + LearningRate: math.LegacyMustNewDecFromStr("0.5"), + MaxBlockUtilization: 10, + TargetBlockUtilization: 5, }, expectErr: false, }, @@ -581,6 +572,39 @@ func TestState_ValidateBasic(t *testing.T) { }, expectErr: true, }, + { + name: "invalid target size", + state: types.State{ + Window: make([]uint64, 1), + BaseFee: math.NewInt(1), + LearningRate: math.LegacyMustNewDecFromStr("0.5"), + TargetBlockUtilization: 0, + MaxBlockUtilization: 100, + }, + expectErr: true, + }, + { + name: "invalid max size", + state: types.State{ + Window: make([]uint64, 1), + BaseFee: math.NewInt(1), + LearningRate: math.LegacyMustNewDecFromStr("0.5"), + TargetBlockUtilization: 10, + MaxBlockUtilization: 0, + }, + expectErr: true, + }, + { + name: "target is larger than max", + state: types.State{ + Window: make([]uint64, 1), + BaseFee: math.NewInt(1), + LearningRate: math.LegacyMustNewDecFromStr("0.5"), + TargetBlockUtilization: 10, + MaxBlockUtilization: 9, + }, + expectErr: true, + }, } for _, tc := range testCases {