Skip to content

Commit

Permalink
add param
Browse files Browse the repository at this point in the history
  • Loading branch information
aljo242 committed Nov 15, 2023
1 parent 9f4d21b commit 364aaa1
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 52 deletions.
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
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
}
112 changes: 75 additions & 37 deletions x/feemarket/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,102 +31,115 @@ func TestParams(t *testing.T) {
expectedErr: true,
},
{
name: "nil alpha",
p: types.Params{Window: 1},
name: "nil alpha",
p: types.Params{
Window: 1,
FeeDenom: types.DefaultFeeDenom,
},
expectedErr: true,
},
{
name: "negative alpha",
p: types.Params{
Window: 1,
Alpha: math.LegacyMustNewDecFromStr("-0.1"),
Window: 1,
Alpha: math.LegacyMustNewDecFromStr("-0.1"),
FeeDenom: types.DefaultFeeDenom,
},
expectedErr: true,
},
{
name: "beta is nil",
p: types.Params{
Window: 1,
Alpha: math.LegacyMustNewDecFromStr("0.1"),
Window: 1,
Alpha: math.LegacyMustNewDecFromStr("0.1"),
FeeDenom: types.DefaultFeeDenom,
},
expectedErr: true,
},
{
name: "beta is negative",
p: types.Params{
Window: 1,
Alpha: math.LegacyMustNewDecFromStr("0.1"),
Beta: math.LegacyMustNewDecFromStr("-0.1"),
Window: 1,
Alpha: math.LegacyMustNewDecFromStr("0.1"),
Beta: math.LegacyMustNewDecFromStr("-0.1"),
FeeDenom: types.DefaultFeeDenom,
},
expectedErr: true,
},
{
name: "beta is greater than 1",
p: types.Params{
Window: 1,
Alpha: math.LegacyMustNewDecFromStr("0.1"),
Beta: math.LegacyMustNewDecFromStr("1.1"),
Window: 1,
Alpha: math.LegacyMustNewDecFromStr("0.1"),
Beta: math.LegacyMustNewDecFromStr("1.1"),
FeeDenom: types.DefaultFeeDenom,
},
expectedErr: true,
},
{
name: "theta is nil",
p: types.Params{
Window: 1,
Alpha: math.LegacyMustNewDecFromStr("0.1"),
Beta: math.LegacyMustNewDecFromStr("0.1"),
Window: 1,
Alpha: math.LegacyMustNewDecFromStr("0.1"),
Beta: math.LegacyMustNewDecFromStr("0.1"),
FeeDenom: types.DefaultFeeDenom,
},
expectedErr: true,
},
{
name: "theta is negative",
p: types.Params{
Window: 1,
Alpha: math.LegacyMustNewDecFromStr("0.1"),
Beta: math.LegacyMustNewDecFromStr("0.1"),
Theta: math.LegacyMustNewDecFromStr("-0.1"),
Window: 1,
Alpha: math.LegacyMustNewDecFromStr("0.1"),
Beta: math.LegacyMustNewDecFromStr("0.1"),
Theta: math.LegacyMustNewDecFromStr("-0.1"),
FeeDenom: types.DefaultFeeDenom,
},
expectedErr: true,
},
{
name: "theta is greater than 1",
p: types.Params{
Window: 1,
Alpha: math.LegacyMustNewDecFromStr("0.1"),
Beta: math.LegacyMustNewDecFromStr("0.1"),
Theta: math.LegacyMustNewDecFromStr("1.1"),
Window: 1,
Alpha: math.LegacyMustNewDecFromStr("0.1"),
Beta: math.LegacyMustNewDecFromStr("0.1"),
Theta: math.LegacyMustNewDecFromStr("1.1"),
FeeDenom: types.DefaultFeeDenom,
},
expectedErr: true,
},
{
name: "delta is nil",
p: types.Params{
Window: 1,
Alpha: math.LegacyMustNewDecFromStr("0.1"),
Beta: math.LegacyMustNewDecFromStr("0.1"),
Theta: math.LegacyMustNewDecFromStr("0.1"),
Window: 1,
Alpha: math.LegacyMustNewDecFromStr("0.1"),
Beta: math.LegacyMustNewDecFromStr("0.1"),
Theta: math.LegacyMustNewDecFromStr("0.1"),
FeeDenom: types.DefaultFeeDenom,
},
expectedErr: true,
},
{
name: "delta is negative",
p: types.Params{
Window: 1,
Alpha: math.LegacyMustNewDecFromStr("0.1"),
Beta: math.LegacyMustNewDecFromStr("0.1"),
Theta: math.LegacyMustNewDecFromStr("0.1"),
Delta: math.LegacyMustNewDecFromStr("-0.1"),
Window: 1,
Alpha: math.LegacyMustNewDecFromStr("0.1"),
Beta: math.LegacyMustNewDecFromStr("0.1"),
Theta: math.LegacyMustNewDecFromStr("0.1"),
Delta: math.LegacyMustNewDecFromStr("-0.1"),
FeeDenom: types.DefaultFeeDenom,
},
expectedErr: true,
},
{
name: "target block size is zero",
p: types.Params{
Window: 1,
Alpha: math.LegacyMustNewDecFromStr("0.1"),
Beta: math.LegacyMustNewDecFromStr("0.1"),
Theta: math.LegacyMustNewDecFromStr("0.1"),
Delta: math.LegacyMustNewDecFromStr("0.1"),
Window: 1,
Alpha: math.LegacyMustNewDecFromStr("0.1"),
Beta: math.LegacyMustNewDecFromStr("0.1"),
Theta: math.LegacyMustNewDecFromStr("0.1"),
Delta: math.LegacyMustNewDecFromStr("0.1"),
FeeDenom: types.DefaultFeeDenom,
},
expectedErr: true,
},
Expand All @@ -139,6 +152,7 @@ func TestParams(t *testing.T) {
Theta: math.LegacyMustNewDecFromStr("0.1"),
Delta: math.LegacyMustNewDecFromStr("0.1"),
TargetBlockUtilization: 1,
FeeDenom: types.DefaultFeeDenom,
},
expectedErr: true,
},
Expand All @@ -152,6 +166,7 @@ func TestParams(t *testing.T) {
Delta: math.LegacyMustNewDecFromStr("0.1"),
TargetBlockUtilization: 2,
MaxBlockUtilization: 1,
FeeDenom: types.DefaultFeeDenom,
},
expectedErr: true,
},
Expand All @@ -165,6 +180,7 @@ func TestParams(t *testing.T) {
Delta: math.LegacyMustNewDecFromStr("0.1"),
TargetBlockUtilization: 2,
MaxBlockUtilization: 3,
FeeDenom: types.DefaultFeeDenom,
},
expectedErr: true,
},
Expand All @@ -179,6 +195,7 @@ func TestParams(t *testing.T) {
TargetBlockUtilization: 2,
MaxBlockUtilization: 3,
MinBaseFee: math.NewInt(-1),
FeeDenom: types.DefaultFeeDenom,
},
expectedErr: true,
},
Expand All @@ -193,6 +210,7 @@ func TestParams(t *testing.T) {
TargetBlockUtilization: 2,
MaxBlockUtilization: 3,
MinBaseFee: math.NewInt(1),
FeeDenom: types.DefaultFeeDenom,
},
expectedErr: true,
},
Expand All @@ -208,6 +226,7 @@ func TestParams(t *testing.T) {
MaxBlockUtilization: 3,
MinBaseFee: math.NewInt(1),
MinLearningRate: math.LegacyMustNewDecFromStr("-0.1"),
FeeDenom: types.DefaultFeeDenom,
},
expectedErr: true,
},
Expand All @@ -223,6 +242,7 @@ func TestParams(t *testing.T) {
MaxBlockUtilization: 3,
MinBaseFee: math.NewInt(1),
MinLearningRate: math.LegacyMustNewDecFromStr("0.1"),
FeeDenom: types.DefaultFeeDenom,
},
expectedErr: true,
},
Expand All @@ -239,6 +259,7 @@ func TestParams(t *testing.T) {
MinBaseFee: math.NewInt(1),
MinLearningRate: math.LegacyMustNewDecFromStr("0.1"),
MaxLearningRate: math.LegacyMustNewDecFromStr("-0.1"),
FeeDenom: types.DefaultFeeDenom,
},
expectedErr: true,
},
Expand All @@ -255,6 +276,23 @@ func TestParams(t *testing.T) {
MinBaseFee: math.NewInt(1),
MinLearningRate: math.LegacyMustNewDecFromStr("0.1"),
MaxLearningRate: math.LegacyMustNewDecFromStr("0.05"),
FeeDenom: types.DefaultFeeDenom,
},
expectedErr: true,
},
{
name: "fee denom is empty",
p: types.Params{
Window: 1,
Alpha: math.LegacyMustNewDecFromStr("0.1"),
Beta: math.LegacyMustNewDecFromStr("0.1"),
Theta: math.LegacyMustNewDecFromStr("0.1"),
Delta: math.LegacyMustNewDecFromStr("0.1"),
TargetBlockUtilization: 2,
MaxBlockUtilization: 3,
MinBaseFee: math.NewInt(1),
MinLearningRate: math.LegacyMustNewDecFromStr("0.01"),
MaxLearningRate: math.LegacyMustNewDecFromStr("0.05"),
},
expectedErr: true,
},
Expand Down

0 comments on commit 364aaa1

Please sign in to comment.