diff --git a/proto/feemarket/feemarket/v1/genesis.proto b/proto/feemarket/feemarket/v1/genesis.proto index 4b89195..4653282 100644 --- a/proto/feemarket/feemarket/v1/genesis.proto +++ b/proto/feemarket/feemarket/v1/genesis.proto @@ -30,8 +30,16 @@ message State { (gogoproto.nullable) = false ]; + // MinBaseFee is the minimum base fee that can be charged. This is denominated + // in the fee per gas unit. + string min_base_fee = 2 [ + (cosmos_proto.scalar) = "cosmos.Int", + (gogoproto.customtype) = "cosmossdk.io/math.Int", + (gogoproto.nullable) = false + ]; + // LearningRate is the current learning rate. - string learning_rate = 2 [ + string learning_rate = 3 [ (cosmos_proto.scalar) = "cosmos.Legacy", (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec", (gogoproto.nullable) = false @@ -40,16 +48,16 @@ message State { // 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. - repeated uint64 window = 3; + repeated uint64 window = 4; // Index is the index of the current block in the block utilization window. - uint64 index = 4; + uint64 index = 5; // 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; + uint64 max_block_utilization = 6; // 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; + uint64 target_block_utilization = 7; } diff --git a/x/feemarket/keeper/feemarket_test.go b/x/feemarket/keeper/feemarket_test.go index ec074c5..09565e8 100644 --- a/x/feemarket/keeper/feemarket_test.go +++ b/x/feemarket/keeper/feemarket_test.go @@ -1,13 +1,391 @@ package keeper_test import ( + "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/skip-mev/feemarket/x/feemarket/types" ) func (s *KeeperTestSuite) TestUpdateFeeMarket() { - // TODO: add tests. + s.Run("empty block with default eip1559 with min base fee", func() { + state := types.DefaultState() + params := types.DefaultParams() + s.setGenesisState(params, state) + + s.Require().NoError(s.feemarketKeeper.UpdateFeeMarket(s.ctx)) + + fee, err := s.feemarketKeeper.GetBaseFee(s.ctx) + s.Require().NoError(err) + s.Require().Equal(fee, params.MinBaseFee) + + lr, err := s.feemarketKeeper.GetLearningRate(s.ctx) + s.Require().NoError(err) + s.Require().Equal(math.LegacyMustNewDecFromStr("0.125"), lr) + }) + + s.Run("empty block with default eip1559 with preset base fee", func() { + state := types.DefaultState() + state.BaseFee = state.BaseFee.Mul(math.NewInt(2)) + params := types.DefaultParams() + s.setGenesisState(params, state) + + s.Require().NoError(s.feemarketKeeper.UpdateFeeMarket(s.ctx)) + + // We expect the base fee to decrease by 1/8th. + fee, err := s.feemarketKeeper.GetBaseFee(s.ctx) + s.Require().NoError(err) + + factor := math.LegacyMustNewDecFromStr("0.875") + expectedFee := state.BaseFee.ToLegacyDec().Mul(factor).TruncateInt() + s.Require().Equal(fee, expectedFee) + + lr, err := s.feemarketKeeper.GetLearningRate(s.ctx) + s.Require().NoError(err) + s.Require().Equal(math.LegacyMustNewDecFromStr("0.125"), lr) + }) + + s.Run("empty block default eip1559 with preset base fee that should default to min", func() { + // Set the base fee to just below the expected threshold decrease of 1/8th. This means it + // should default to the minimum base fee. + state := types.DefaultState() + factor := math.LegacyMustNewDecFromStr("0.125") + increase := state.BaseFee.ToLegacyDec().Mul(factor).TruncateInt() + state.BaseFee = types.DefaultMinBaseFee.Add(increase).Sub(math.NewInt(1)) + + params := types.DefaultParams() + s.setGenesisState(params, state) + + s.Require().NoError(s.feemarketKeeper.UpdateFeeMarket(s.ctx)) + + // We expect the base fee to decrease by 1/8th. + fee, err := s.feemarketKeeper.GetBaseFee(s.ctx) + s.Require().NoError(err) + s.Require().Equal(fee, params.MinBaseFee) + + lr, err := s.feemarketKeeper.GetLearningRate(s.ctx) + s.Require().NoError(err) + s.Require().Equal(math.LegacyMustNewDecFromStr("0.125"), lr) + }) + + s.Run("target block with default eip1559 at min base fee", func() { + state := types.DefaultState() + // Reaching the target block size means that we expect this to not + // increase. + err := state.Update(state.TargetBlockUtilization) + s.Require().NoError(err) + + params := types.DefaultParams() + s.setGenesisState(params, state) + + s.Require().NoError(s.feemarketKeeper.UpdateFeeMarket(s.ctx)) + + // We expect the base fee to remain the same. + fee, err := s.feemarketKeeper.GetBaseFee(s.ctx) + s.Require().NoError(err) + s.Require().Equal(fee, params.MinBaseFee) + + lr, err := s.feemarketKeeper.GetLearningRate(s.ctx) + s.Require().NoError(err) + s.Require().Equal(math.LegacyMustNewDecFromStr("0.125"), lr) + }) + + s.Run("target block with default eip1559 at preset base fee", func() { + state := types.DefaultState() + state.BaseFee = state.BaseFee.Mul(math.NewInt(2)) + // Reaching the target block size means that we expect this to not + // increase. + err := state.Update(state.TargetBlockUtilization) + s.Require().NoError(err) + + params := types.DefaultParams() + s.setGenesisState(params, state) + + s.Require().NoError(s.feemarketKeeper.UpdateFeeMarket(s.ctx)) + + // We expect the base fee to remain the same. + fee, err := s.feemarketKeeper.GetBaseFee(s.ctx) + s.Require().NoError(err) + s.Require().Equal(state.BaseFee, fee) + + lr, err := s.feemarketKeeper.GetLearningRate(s.ctx) + s.Require().NoError(err) + s.Require().Equal(math.LegacyMustNewDecFromStr("0.125"), lr) + }) + + s.Run("max block with default eip1559 at min base fee", func() { + state := types.DefaultState() + // Reaching the target block size means that we expect this to not + // increase. + err := state.Update(state.MaxBlockUtilization) + s.Require().NoError(err) + + params := types.DefaultParams() + s.setGenesisState(params, state) + + s.Require().NoError(s.feemarketKeeper.UpdateFeeMarket(s.ctx)) + + // We expect the base fee to increase by 1/8th. + fee, err := s.feemarketKeeper.GetBaseFee(s.ctx) + s.Require().NoError(err) + + factor := math.LegacyMustNewDecFromStr("1.125") + expectedFee := state.BaseFee.ToLegacyDec().Mul(factor).TruncateInt() + s.Require().Equal(fee, expectedFee) + + lr, err := s.feemarketKeeper.GetLearningRate(s.ctx) + s.Require().NoError(err) + s.Require().Equal(math.LegacyMustNewDecFromStr("0.125"), lr) + }) + + s.Run("max block with default eip1559 at preset base fee", func() { + state := types.DefaultState() + state.BaseFee = state.BaseFee.Mul(math.NewInt(2)) + // Reaching the target block size means that we expect this to not + // increase. + err := state.Update(state.MaxBlockUtilization) + s.Require().NoError(err) + + params := types.DefaultParams() + s.setGenesisState(params, state) + + s.Require().NoError(s.feemarketKeeper.UpdateFeeMarket(s.ctx)) + + // We expect the base fee to increase by 1/8th. + fee, err := s.feemarketKeeper.GetBaseFee(s.ctx) + s.Require().NoError(err) + + factor := math.LegacyMustNewDecFromStr("1.125") + expectedFee := state.BaseFee.ToLegacyDec().Mul(factor).TruncateInt() + s.Require().Equal(fee, expectedFee) + + lr, err := s.feemarketKeeper.GetLearningRate(s.ctx) + s.Require().NoError(err) + s.Require().Equal(math.LegacyMustNewDecFromStr("0.125"), lr) + }) + + s.Run("in-between min and target block with default eip1559 at min base fee", func() { + state := types.DefaultState() + state.MaxBlockUtilization = 100 + state.TargetBlockUtilization = 50 + err := state.Update(25) + s.Require().NoError(err) + + params := types.DefaultParams() + params.MaxBlockUtilization = 100 + params.TargetBlockUtilization = 50 + s.setGenesisState(params, state) + + s.Require().NoError(s.feemarketKeeper.UpdateFeeMarket(s.ctx)) + + // We expect the base fee to remain the same since it is at min base fee. + fee, err := s.feemarketKeeper.GetBaseFee(s.ctx) + s.Require().NoError(err) + s.Require().Equal(fee, params.MinBaseFee) + + lr, err := s.feemarketKeeper.GetLearningRate(s.ctx) + s.Require().NoError(err) + s.Require().Equal(math.LegacyMustNewDecFromStr("0.125"), lr) + }) + + s.Run("in-between min and target block with default eip1559 at preset base fee", func() { + state := types.DefaultState() + state.BaseFee = state.BaseFee.Mul(math.NewInt(2)) + state.MaxBlockUtilization = 100 + state.TargetBlockUtilization = 50 + err := state.Update(25) + s.Require().NoError(err) + + params := types.DefaultParams() + params.MaxBlockUtilization = 100 + params.TargetBlockUtilization = 50 + s.setGenesisState(params, state) + + s.Require().NoError(s.feemarketKeeper.UpdateFeeMarket(s.ctx)) + + // We expect the base fee to decrease by 1/8th * 1/2. + fee, err := s.feemarketKeeper.GetBaseFee(s.ctx) + s.Require().NoError(err) + + factor := math.LegacyMustNewDecFromStr("0.9375") + expectedFee := state.BaseFee.ToLegacyDec().Mul(factor).TruncateInt() + s.Require().Equal(fee, expectedFee) + + lr, err := s.feemarketKeeper.GetLearningRate(s.ctx) + s.Require().NoError(err) + s.Require().Equal(math.LegacyMustNewDecFromStr("0.125"), lr) + }) + + s.Run("in-between target and max block with default eip1559 at min base fee", func() { + state := types.DefaultState() + state.MaxBlockUtilization = 100 + state.TargetBlockUtilization = 50 + err := state.Update(75) + s.Require().NoError(err) + + params := types.DefaultParams() + params.MaxBlockUtilization = 100 + params.TargetBlockUtilization = 50 + s.setGenesisState(params, state) + + s.Require().NoError(s.feemarketKeeper.UpdateFeeMarket(s.ctx)) + + // We expect the base fee to increase by 1/8th * 1/2. + fee, err := s.feemarketKeeper.GetBaseFee(s.ctx) + s.Require().NoError(err) + + factor := math.LegacyMustNewDecFromStr("1.0625") + expectedFee := state.BaseFee.ToLegacyDec().Mul(factor).TruncateInt() + s.Require().Equal(fee, expectedFee) + + lr, err := s.feemarketKeeper.GetLearningRate(s.ctx) + s.Require().NoError(err) + s.Require().Equal(math.LegacyMustNewDecFromStr("0.125"), lr) + }) + + s.Run("in-between target and max block with default eip1559 at preset base fee", func() { + state := types.DefaultState() + state.BaseFee = state.BaseFee.Mul(math.NewInt(2)) + state.MaxBlockUtilization = 100 + state.TargetBlockUtilization = 50 + err := state.Update(75) + s.Require().NoError(err) + + params := types.DefaultParams() + params.MaxBlockUtilization = 100 + params.TargetBlockUtilization = 50 + s.setGenesisState(params, state) + + s.Require().NoError(s.feemarketKeeper.UpdateFeeMarket(s.ctx)) + + // We expect the base fee to increase by 1/8th * 1/2. + fee, err := s.feemarketKeeper.GetBaseFee(s.ctx) + s.Require().NoError(err) + + factor := math.LegacyMustNewDecFromStr("1.0625") + expectedFee := state.BaseFee.ToLegacyDec().Mul(factor).TruncateInt() + s.Require().Equal(fee, expectedFee) + + lr, err := s.feemarketKeeper.GetLearningRate(s.ctx) + s.Require().NoError(err) + s.Require().Equal(math.LegacyMustNewDecFromStr("0.125"), lr) + }) + + s.Run("empty blocks with aimd eip1559 with min base fee", func() { + state := types.DefaultAIMDState() + params := types.DefaultAIMDParams() + s.setGenesisState(params, state) + + s.Require().NoError(s.feemarketKeeper.UpdateFeeMarket(s.ctx)) + + fee, err := s.feemarketKeeper.GetBaseFee(s.ctx) + s.Require().NoError(err) + s.Require().Equal(fee, params.MinBaseFee) + + lr, err := s.feemarketKeeper.GetLearningRate(s.ctx) + s.Require().NoError(err) + expectedLR := state.LearningRate.Add(params.Alpha) + s.Require().Equal(expectedLR, lr) + }) + + s.Run("empty blocks with aimd eip1559 with preset base fee", func() { + state := types.DefaultAIMDState() + state.BaseFee = state.BaseFee.Mul(math.NewInt(2)) + params := types.DefaultAIMDParams() + s.setGenesisState(params, state) + + s.Require().NoError(s.feemarketKeeper.UpdateFeeMarket(s.ctx)) + + // We expect the base fee to decrease by 1/8th and the learning rate to + // increase by alpha. + lr, err := s.feemarketKeeper.GetLearningRate(s.ctx) + s.Require().NoError(err) + expectedLR := state.LearningRate.Add(params.Alpha) + s.Require().Equal(expectedLR, lr) + + fee, err := s.feemarketKeeper.GetBaseFee(s.ctx) + s.Require().NoError(err) + factor := math.LegacyOneDec().Add(math.LegacyMustNewDecFromStr("-1.0").Mul(lr)) + expectedFee := state.BaseFee.ToLegacyDec().Mul(factor).TruncateInt() + s.Require().Equal(fee, expectedFee) + }) + + s.Run("empty blocks aimd eip1559 with preset base fee that should default to min", func() { + params := types.DefaultAIMDParams() + + state := types.DefaultAIMDState() + lr := math.LegacyMustNewDecFromStr("0.125") + increase := state.BaseFee.ToLegacyDec().Mul(lr).TruncateInt() + + state.BaseFee = types.DefaultMinBaseFee.Add(increase).Sub(math.NewInt(1)) + state.LearningRate = lr + + s.setGenesisState(params, state) + + s.Require().NoError(s.feemarketKeeper.UpdateFeeMarket(s.ctx)) + + lr, err := s.feemarketKeeper.GetLearningRate(s.ctx) + s.Require().NoError(err) + expectedLR := state.LearningRate.Add(params.Alpha) + s.Require().Equal(expectedLR, lr) + + // We expect the base fee to decrease by 1/8th and the learning rate to + // increase by alpha. + fee, err := s.feemarketKeeper.GetBaseFee(s.ctx) + s.Require().NoError(err) + s.Require().Equal(fee, params.MinBaseFee) + }) + + s.Run("target block with aimd eip1559 at min base fee + LR", func() { + state := types.DefaultAIMDState() + // Reaching the target block size means that we expect this to not + // increase. + for i := 0; i < len(state.Window); i++ { + state.Window[i] = state.TargetBlockUtilization + } + + params := types.DefaultAIMDParams() + s.setGenesisState(params, state) + + s.Require().NoError(s.feemarketKeeper.UpdateFeeMarket(s.ctx)) + + // We expect the base fee to remain the same and the learning rate to + // remain at minimum. + lr, err := s.feemarketKeeper.GetLearningRate(s.ctx) + s.Require().NoError(err) + s.Require().Equal(params.MinLearningRate, lr) + + fee, err := s.feemarketKeeper.GetBaseFee(s.ctx) + s.Require().NoError(err) + s.Require().Equal(state.BaseFee, fee) + }) + + s.Run("target block with aimd eip1559 at preset base fee + LR", func() { + state := types.DefaultAIMDState() + state.BaseFee = state.BaseFee.Mul(math.NewInt(2)) + state.LearningRate = math.LegacyMustNewDecFromStr("0.125") + // Reaching the target block size means that we expect this to not + // increase. + for i := 0; i < len(state.Window); i++ { + state.Window[i] = state.TargetBlockUtilization + } + + params := types.DefaultAIMDParams() + s.setGenesisState(params, state) + + s.Require().NoError(s.feemarketKeeper.UpdateFeeMarket(s.ctx)) + + // We expect the base fee to decrease by 1/8th and the learning rate to + // decrease by lr * beta. + lr, err := s.feemarketKeeper.GetLearningRate(s.ctx) + s.Require().NoError(err) + expectedLR := state.LearningRate.Mul(params.Beta) + s.Require().Equal(expectedLR, lr) + + fee, err := s.feemarketKeeper.GetBaseFee(s.ctx) + s.Require().NoError(err) + s.Require().Equal(state.BaseFee, fee) + }) } func (s *KeeperTestSuite) TestGetBaseFee() { @@ -73,3 +451,10 @@ func (s *KeeperTestSuite) TestGetMinGasPrices() { s.Require().Equal(expected, mgp) }) } + +func (s *KeeperTestSuite) setGenesisState(params types.Params, state types.State) { + gs := types.NewGenesisState(params, state) + s.NotPanics(func() { + s.feemarketKeeper.InitGenesis(s.ctx, *gs) + }) +} diff --git a/x/feemarket/types/genesis.pb.go b/x/feemarket/types/genesis.pb.go index 83bf3ed..54bda5b 100644 --- a/x/feemarket/types/genesis.pb.go +++ b/x/feemarket/types/genesis.pb.go @@ -89,20 +89,23 @@ type State struct { // 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"` + // MinBaseFee is the minimum base fee that can be charged. This is denominated + // in the fee per gas unit. + MinBaseFee cosmossdk_io_math.Int `protobuf:"bytes,2,opt,name=min_base_fee,json=minBaseFee,proto3,customtype=cosmossdk.io/math.Int" json:"min_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"` + LearningRate cosmossdk_io_math.LegacyDec `protobuf:"bytes,3,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 []uint64 `protobuf:"varint,3,rep,packed,name=window,proto3" json:"window,omitempty"` + Window []uint64 `protobuf:"varint,4,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"` + Index uint64 `protobuf:"varint,5,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"` + MaxBlockUtilization uint64 `protobuf:"varint,6,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"` + TargetBlockUtilization uint64 `protobuf:"varint,7,opt,name=target_block_utilization,json=targetBlockUtilization,proto3" json:"target_block_utilization,omitempty"` } func (m *State) Reset() { *m = State{} } @@ -176,34 +179,35 @@ func init() { } var fileDescriptor_2180652c84279298 = []byte{ - // 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, 0x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0x18, 0xda, 0xc0, 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, + // 447 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x92, 0x41, 0x6b, 0xd4, 0x40, + 0x14, 0xc7, 0x37, 0xdd, 0x24, 0xd5, 0x71, 0x7b, 0x89, 0xed, 0x12, 0x2b, 0xa6, 0x4b, 0xf5, 0xb0, + 0x20, 0x9b, 0xd0, 0xea, 0x41, 0xc1, 0x53, 0x90, 0xca, 0x82, 0x82, 0x44, 0x3c, 0xe8, 0x25, 0x4c, + 0xb2, 0xaf, 0xe9, 0x90, 0x9d, 0x99, 0x90, 0x99, 0x6e, 0x53, 0xbf, 0x80, 0x57, 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0xb5, 0x3c, 0xad, 0x40, 0x64, 0xae, 0xfa, 0x73, + 0xcf, 0x7e, 0x07, 0x00, 0x00, 0xff, 0xff, 0x42, 0xf0, 0xf1, 0x69, 0x09, 0x03, 0x00, 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -272,17 +276,17 @@ func (m *State) MarshalToSizedBuffer(dAtA []byte) (int, error) { if m.TargetBlockUtilization != 0 { i = encodeVarintGenesis(dAtA, i, uint64(m.TargetBlockUtilization)) i-- - dAtA[i] = 0x30 + dAtA[i] = 0x38 } if m.MaxBlockUtilization != 0 { i = encodeVarintGenesis(dAtA, i, uint64(m.MaxBlockUtilization)) i-- - dAtA[i] = 0x28 + dAtA[i] = 0x30 } if m.Index != 0 { i = encodeVarintGenesis(dAtA, i, uint64(m.Index)) i-- - dAtA[i] = 0x20 + dAtA[i] = 0x28 } if len(m.Window) > 0 { dAtA4 := make([]byte, len(m.Window)*10) @@ -300,7 +304,7 @@ func (m *State) MarshalToSizedBuffer(dAtA []byte) (int, error) { copy(dAtA[i:], dAtA4[:j3]) i = encodeVarintGenesis(dAtA, i, uint64(j3)) i-- - dAtA[i] = 0x1a + dAtA[i] = 0x22 } { size := m.LearningRate.Size() @@ -311,6 +315,16 @@ func (m *State) MarshalToSizedBuffer(dAtA []byte) (int, error) { i = encodeVarintGenesis(dAtA, i, uint64(size)) } i-- + dAtA[i] = 0x1a + { + size := m.MinBaseFee.Size() + i -= size + if _, err := m.MinBaseFee.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- dAtA[i] = 0x12 { size := m.BaseFee.Size() @@ -357,6 +371,8 @@ func (m *State) Size() (n int) { _ = l l = m.BaseFee.Size() n += 1 + l + sovGenesis(uint64(l)) + l = m.MinBaseFee.Size() + n += 1 + l + sovGenesis(uint64(l)) l = m.LearningRate.Size() n += 1 + l + sovGenesis(uint64(l)) if len(m.Window) > 0 { @@ -564,6 +580,40 @@ func (m *State) Unmarshal(dAtA []byte) error { } iNdEx = postIndex case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field MinBaseFee", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.MinBaseFee.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 3: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field LearningRate", wireType) } @@ -597,7 +647,7 @@ func (m *State) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex - case 3: + case 4: if wireType == 0 { var v uint64 for shift := uint(0); ; shift += 7 { @@ -673,7 +723,7 @@ func (m *State) Unmarshal(dAtA []byte) error { } else { return fmt.Errorf("proto: wrong wireType = %d for field Window", wireType) } - case 4: + case 5: if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field Index", wireType) } @@ -692,7 +742,7 @@ func (m *State) Unmarshal(dAtA []byte) error { break } } - case 5: + case 6: if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field MaxBlockUtilization", wireType) } @@ -711,7 +761,7 @@ func (m *State) Unmarshal(dAtA []byte) error { break } } - case 6: + case 7: if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field TargetBlockUtilization", wireType) } diff --git a/x/feemarket/types/state.go b/x/feemarket/types/state.go index 1d1b525..3ce6d9b 100644 --- a/x/feemarket/types/state.go +++ b/x/feemarket/types/state.go @@ -8,7 +8,8 @@ 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. +// AIMD EIP-1559 fee market implementation. Note that on init, you initialize +// both the minimum and current base fee to the same value. func NewState( window, target, max uint64, @@ -18,6 +19,7 @@ func NewState( return State{ Window: make([]uint64, window), BaseFee: baseFee, + MinBaseFee: baseFee, LearningRate: learningRate, Index: 0, TargetBlockUtilization: target, @@ -65,6 +67,12 @@ func (s *State) UpdateBaseFee(delta math.LegacyDec) math.Int { // Update the base fee. fee := (math.LegacyNewDecFromInt(s.BaseFee).Mul(learningRateAdjustment)).Add(net).TruncateInt() + + // Ensure the base fee is greater than the minimum base fee. + if fee.LT(s.MinBaseFee) { + fee = s.MinBaseFee + } + s.BaseFee = fee return s.BaseFee } diff --git a/x/feemarket/types/state_fuzz_test.go b/x/feemarket/types/state_fuzz_test.go new file mode 100644 index 0000000..661eaca --- /dev/null +++ b/x/feemarket/types/state_fuzz_test.go @@ -0,0 +1,107 @@ +package types_test + +import ( + "testing" + + "cosmossdk.io/math" + "github.com/skip-mev/feemarket/x/feemarket/types" + "github.com/stretchr/testify/require" +) + +func FuzzDefaultFeeMarket(f *testing.F) { + testCases := []uint64{ + 0, + 1_000, + 10_000, + 100_000, + 1_000_000, + 10_000_000, + 100_000_000, + } + + for _, tc := range testCases { + f.Add(tc) + } + + defaultLR := math.LegacyMustNewDecFromStr("0.125") + params := types.DefaultParams() + + // Default fee market. + f.Fuzz(func(t *testing.T, blockGasUsed uint64) { + state := types.DefaultState() + state.MinBaseFee = math.NewIntFromUint64(100) + state.BaseFee = math.NewIntFromUint64(200) + err := state.Update(blockGasUsed) + + if blockGasUsed > state.MaxBlockUtilization { + require.Error(t, err) + return + } + + require.NoError(t, err) + require.Equal(t, blockGasUsed, state.Window[state.Index]) + + // Ensure the learning rate is always the default learning rate. + lr := state.UpdateLearningRate( + params.Theta, + params.Alpha, + params.Beta, + params.MinLearningRate, + params.MaxLearningRate, + ) + require.Equal(t, defaultLR, lr) + + oldFee := state.BaseFee + newFee := state.UpdateBaseFee(params.Delta) + + if blockGasUsed > state.TargetBlockUtilization { + require.True(t, newFee.GT(oldFee)) + } else { + require.True(t, newFee.LT(oldFee)) + } + }) +} + +func FuzzAIMDFeeMarket(f *testing.F) { + testCases := []uint64{ + 0, + 1_000, + 10_000, + 100_000, + 1_000_000, + 10_000_000, + 100_000_000, + } + + for _, tc := range testCases { + f.Add(tc) + } + + params := types.DefaultAIMDParams() + + // Fee market with adjustable learning rate. + f.Fuzz(func(t *testing.T, blockGasUsed uint64) { + state := types.DefaultAIMDState() + state.MinBaseFee = math.NewIntFromUint64(100) + state.BaseFee = math.NewIntFromUint64(200) + state.Window = make([]uint64, 1) + err := state.Update(blockGasUsed) + + if blockGasUsed > state.MaxBlockUtilization { + require.Error(t, err) + return + } + + require.NoError(t, err) + require.Equal(t, blockGasUsed, state.Window[state.Index]) + + oldFee := state.BaseFee + newFee := state.UpdateBaseFee(params.Delta) + + if blockGasUsed > state.TargetBlockUtilization { + require.True(t, newFee.GT(oldFee)) + } else { + require.True(t, newFee.LT(oldFee)) + } + }) +} diff --git a/x/feemarket/types/state_test.go b/x/feemarket/types/state_test.go index 3f97a52..bdf8365 100644 --- a/x/feemarket/types/state_test.go +++ b/x/feemarket/types/state_test.go @@ -119,6 +119,7 @@ func TestState_UpdateBaseFee(t *testing.T) { t.Run("empty block with default eip-1559", func(t *testing.T) { state := types.DefaultState() state.BaseFee = math.NewInt(1000) + state.MinBaseFee = math.NewInt(125) params := types.DefaultParams() @@ -130,6 +131,7 @@ func TestState_UpdateBaseFee(t *testing.T) { t.Run("target block with default eip-1559", func(t *testing.T) { state := types.DefaultState() state.BaseFee = math.NewInt(1000) + state.MinBaseFee = math.NewInt(125) params := types.DefaultParams() @@ -143,6 +145,7 @@ func TestState_UpdateBaseFee(t *testing.T) { t.Run("full block with default eip-1559", func(t *testing.T) { state := types.DefaultState() state.BaseFee = math.NewInt(1000) + state.MinBaseFee = math.NewInt(125) params := types.DefaultParams() @@ -156,6 +159,7 @@ func TestState_UpdateBaseFee(t *testing.T) { t.Run("empty block with default aimd eip-1559", func(t *testing.T) { state := types.DefaultAIMDState() state.BaseFee = math.NewInt(1000) + state.MinBaseFee = math.NewInt(125) state.LearningRate = math.LegacyMustNewDecFromStr("0.125") params := types.DefaultAIMDParams() @@ -170,6 +174,7 @@ func TestState_UpdateBaseFee(t *testing.T) { t.Run("target block with default aimd eip-1559", func(t *testing.T) { state := types.DefaultAIMDState() state.BaseFee = math.NewInt(1000) + state.MinBaseFee = math.NewInt(125) state.LearningRate = math.LegacyMustNewDecFromStr("0.125") params := types.DefaultAIMDParams() @@ -188,6 +193,7 @@ func TestState_UpdateBaseFee(t *testing.T) { t.Run("full blocks with default aimd eip-1559", func(t *testing.T) { state := types.DefaultAIMDState() state.BaseFee = math.NewInt(1000) + state.MinBaseFee = math.NewInt(125) state.LearningRate = math.LegacyMustNewDecFromStr("0.125") params := types.DefaultAIMDParams() @@ -202,6 +208,26 @@ func TestState_UpdateBaseFee(t *testing.T) { expectedBaseFee := math.NewInt(1150) require.True(t, expectedBaseFee.Equal(newBaseFee)) }) + + t.Run("never goes below min base fee with default eip1599", func(t *testing.T) { + state := types.DefaultState() + params := types.DefaultParams() + + // Empty block + newBaseFee := state.UpdateBaseFee(params.Delta) + expectedBaseFee := params.MinBaseFee + require.True(t, expectedBaseFee.Equal(newBaseFee)) + }) + + t.Run("never goes below min base fee with default aimd eip1599", func(t *testing.T) { + state := types.DefaultAIMDState() + params := types.DefaultAIMDParams() + + // Empty blocks + newBaseFee := state.UpdateBaseFee(params.Delta) + expectedBaseFee := params.MinBaseFee + require.True(t, expectedBaseFee.Equal(newBaseFee)) + }) } func TestState_UpdateLearningRate(t *testing.T) {