Skip to content

Commit

Permalink
feat: denom parameter (#29)
Browse files Browse the repository at this point in the history
* proto

* add param

* add get

* test

* format
  • Loading branch information
aljo242 committed Nov 20, 2023
1 parent 3011c5e commit ba2dff4
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 94 deletions.
8 changes: 4 additions & 4 deletions proto/feemarket/feemarket/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ 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.
// 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.
// 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;
}
5 changes: 4 additions & 1 deletion proto/feemarket/feemarket/v1/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ message Params {
// over a moving window of blocks.
uint64 window = 10;

// FeeDenom is the denom that will be used for all fee payments.
string fee_denom = 11;

// Enabled is a boolean that determines whether the EIP1559 fee market is
// enabled.
bool enabled = 11;
bool enabled = 12;
}
18 changes: 18 additions & 0 deletions x/feemarket/keeper/feemarket.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,21 @@ func (k *Keeper) GetLearningRate(ctx sdk.Context) (math.LegacyDec, error) {

return state.LearningRate, nil
}

// GetMinGasPrices returns the mininum gas prices as sdk.Coins from the fee market state.
func (k *Keeper) GetMinGasPrices(ctx sdk.Context) (sdk.Coins, error) {
baseFee, err := k.GetBaseFee(ctx)
if err != nil {
return sdk.NewCoins(), err
}

params, err := k.GetParams(ctx)
if err != nil {
return sdk.NewCoins(), err
}

fee := sdk.NewCoin(params.FeeDenom, baseFee)
minGasPrices := sdk.NewCoins(fee)

return minGasPrices, nil
}
26 changes: 26 additions & 0 deletions x/feemarket/keeper/feemarket_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper_test

import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/skip-mev/feemarket/x/feemarket/types"
)

Expand Down Expand Up @@ -47,3 +49,27 @@ func (s *KeeperTestSuite) TestGetLearningRate() {
s.Require().Equal(lr, gs.State.LearningRate)
})
}

func (s *KeeperTestSuite) TestGetMinGasPrices() {
s.Run("can retrieve min gas prices with default eip-1559", func() {
gs := types.DefaultGenesisState()
s.feemarketKeeper.InitGenesis(s.ctx, *gs)

expected := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, gs.State.BaseFee))

mgp, err := s.feemarketKeeper.GetMinGasPrices(s.ctx)
s.Require().NoError(err)
s.Require().Equal(expected, mgp)
})

s.Run("can retrieve min gas prices with aimd eip-1559", func() {
gs := types.DefaultAIMDGenesisState()
s.feemarketKeeper.InitGenesis(s.ctx, *gs)

expected := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, gs.State.BaseFee))

mgp, err := s.feemarketKeeper.GetMinGasPrices(s.ctx)
s.Require().NoError(err)
s.Require().Equal(expected, mgp)
})
}
23 changes: 15 additions & 8 deletions x/feemarket/types/eip1559.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package types

import "cosmossdk.io/math"
import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// Note: We use the same default values as Ethereum for the EIP-1559
// fee market implementation. These parameters do not implement the
Expand All @@ -13,16 +16,16 @@ var (
DefaultWindow uint64 = 1

// DefaultAlpha is not used in the base EIP-1559 implementation.
DefaultAlpha math.LegacyDec = math.LegacyMustNewDecFromStr("0.0")
DefaultAlpha = math.LegacyMustNewDecFromStr("0.0")

// DefaultBeta is not used in the base EIP-1559 implementation.
DefaultBeta math.LegacyDec = math.LegacyMustNewDecFromStr("1.0")
DefaultBeta = math.LegacyMustNewDecFromStr("1.0")

// DefaultTheta is not used in the base EIP-1559 implementation.
DefaultTheta math.LegacyDec = math.LegacyMustNewDecFromStr("0.0")
DefaultTheta = math.LegacyMustNewDecFromStr("0.0")

// DefaultDelta is not used in the base EIP-1559 implementation.
DefaultDelta math.LegacyDec = math.LegacyMustNewDecFromStr("0.0")
DefaultDelta = math.LegacyMustNewDecFromStr("0.0")

// DefaultTargetBlockSize is the default target block utilization. This is the default
// on Ethereum. This denominated in units of gas consumed in a block.
Expand All @@ -35,13 +38,16 @@ var (
// DefaultMinBaseFee is the default minimum base fee. This is the default
// on Ethereum. Note that Ethereum is denominated in 1e18 wei. Cosmos chains will
// likely want to change this to 1e6.
DefaultMinBaseFee math.Int = math.NewInt(1_000_000_000)
DefaultMinBaseFee = math.NewInt(1_000_000_000)

// DefaultMinLearningRate is not used in the base EIP-1559 implementation.
DefaultMinLearningRate math.LegacyDec = math.LegacyMustNewDecFromStr("0.125")
DefaultMinLearningRate = math.LegacyMustNewDecFromStr("0.125")

// DefaultMaxLearningRate is not used in the base EIP-1559 implementation.
DefaultMaxLearningRate math.LegacyDec = math.LegacyMustNewDecFromStr("0.125")
DefaultMaxLearningRate = math.LegacyMustNewDecFromStr("0.125")

// DefaultFeeDenom is the Cosmos SDK default bond denom.
DefaultFeeDenom = sdk.DefaultBondDenom
)

// DefaultParams returns a default set of parameters that implements
Expand All @@ -59,6 +65,7 @@ func DefaultParams() Params {
DefaultMinBaseFee,
DefaultMinLearningRate,
DefaultMaxLearningRate,
DefaultFeeDenom,
true,
)
}
Expand Down
18 changes: 11 additions & 7 deletions x/feemarket/types/eip1559_aimd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ var (
// DefaultAIMDAlpha is the default alpha value for the learning
// rate calculation. This value determines how much we want to additively
// increase the learning rate when the target block size is exceeded.
DefaultAIMDAlpha math.LegacyDec = math.LegacyMustNewDecFromStr("0.025")
DefaultAIMDAlpha = math.LegacyMustNewDecFromStr("0.025")

// DefaultAIMDBeta is the default beta value for the learning rate
// calculation. This value determines how much we want to multiplicatively
// decrease the learning rate when the target utilization is not met.
DefaultAIMDBeta math.LegacyDec = math.LegacyMustNewDecFromStr("0.95")
DefaultAIMDBeta = math.LegacyMustNewDecFromStr("0.95")

// DefaultAIMDTheta is the default threshold for determining whether
// to increase or decrease the learning rate. In this case, we increase
// the learning rate if the block utilization within the window is greater
// than 0.75 or less than 0.25. Otherwise, we multiplicatively decrease
// the learning rate.
DefaultAIMDTheta math.LegacyDec = math.LegacyMustNewDecFromStr("0.25")
DefaultAIMDTheta = math.LegacyMustNewDecFromStr("0.25")

// DefaultAIMDDelta is the default delta value for how much we additively
// increase or decrease the base fee when the net block utilization within
// the window is not equal to the target utilization.
DefaultAIMDDelta math.LegacyDec = math.LegacyMustNewDecFromStr("0.0")
DefaultAIMDDelta = math.LegacyMustNewDecFromStr("0.0")

// DefaultAIMDTargetBlockSize is the default target block utilization. This
// is the default on Ethereum. This denominated in units of gas consumed in
Expand All @@ -46,13 +46,16 @@ var (
// DefaultAIMDMinBaseFee is the default minimum base fee. This is the
// default on Ethereum. Note that ethereum is denominated in 1e18 wei.
// Cosmos chains will likely want to change this to 1e6.
DefaultAIMDMinBaseFee math.Int = math.NewInt(1_000_000_000)
DefaultAIMDMinBaseFee = math.NewInt(1_000_000_000)

// DefaultAIMDMinLearningRate is the default minimum learning rate.
DefaultAIMDMinLearningRate math.LegacyDec = math.LegacyMustNewDecFromStr("0.01")
DefaultAIMDMinLearningRate = math.LegacyMustNewDecFromStr("0.01")

// DefaultAIMDMaxLearningRate is the default maximum learning rate.
DefaultAIMDMaxLearningRate math.LegacyDec = math.LegacyMustNewDecFromStr("0.50")
DefaultAIMDMaxLearningRate = math.LegacyMustNewDecFromStr("0.50")

// DefaultAIMDFeeDenom is the Cosmos SDK default bond denom.
DefaultAIMDFeeDenom = DefaultFeeDenom
)

// DefaultAIMDParams returns a default set of parameters that implements
Expand All @@ -71,6 +74,7 @@ func DefaultAIMDParams() Params {
DefaultAIMDMinBaseFee,
DefaultAIMDMinLearningRate,
DefaultAIMDMaxLearningRate,
DefaultAIMDFeeDenom,
true,
)
}
Expand Down
17 changes: 9 additions & 8 deletions x/feemarket/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions x/feemarket/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func NewParams(
minBaseFee math.Int,
minLearingRate math.LegacyDec,
maxLearningRate math.LegacyDec,
feeDenom string,
enabled bool,
) Params {
return Params{
Expand All @@ -32,6 +33,7 @@ func NewParams(
TargetBlockUtilization: targetBlockSize,
MaxBlockUtilization: maxBlockSize,
Window: window,
FeeDenom: feeDenom,
Enabled: enabled,
}
}
Expand Down Expand Up @@ -86,5 +88,9 @@ func (p *Params) ValidateBasic() error {
return fmt.Errorf("min learning rate cannot be greater than max learning rate")
}

if p.FeeDenom == "" {
return fmt.Errorf("fee denom must be set")
}

return nil
}
Loading

0 comments on commit ba2dff4

Please sign in to comment.