Skip to content

Commit

Permalink
refactor: use decimal as min_base_fee (#66)
Browse files Browse the repository at this point in the history
* proto: change min_base_fee to decimal; build protos

* refactor: use decimal instead of int for base fee operations

* e2e: update e2e init

* tests: update unit tests - TruncateDec instead of TruncateInt

* docs: update readme files

* proto: update wrong labels

* refactor: Coins -> DecCoins in GetMinGasPrices

* refactor; tests: Coins -> DecCoins in GetMinGasPrices

* tests: use 1 as default min base (change from 1000000)

* tests: additional tests with < 1 min base fee

* tests: fix broken test assertion

* rm unnecessary comment

---------

Co-authored-by: Alex Johnson <[email protected]>
  • Loading branch information
MSalopek and Alex Johnson authored Apr 8, 2024
1 parent cd36944 commit b8d3623
Show file tree
Hide file tree
Showing 29 changed files with 257 additions and 217 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ message State {
// BaseFee is the current base fee. This is denominated in the fee per gas
// unit.
string base_fee = 1 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
// LearningRate is the current learning rate.
string learning_rate = 2 [
(cosmos_proto.scalar) = "cosmos.Legacy",
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
Expand Down Expand Up @@ -309,8 +309,8 @@ message Params {
// minimum
// for the network. This is denominated in fee per gas unit.
string min_base_fee = 5 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
Expand Down Expand Up @@ -385,7 +385,7 @@ enabled: true
fee_denom: stake
max_block_utilization: "30000000"
max_learning_rate: "0.125000000000000000"
min_base_fee: "1000000"
min_base_fee: "1.000000000000000000"
min_learning_rate: "0.125000000000000000"
target_block_utilization: "15000000"
theta: "0.000000000000000000"
Expand All @@ -409,7 +409,7 @@ feemarketd query feemarket state
Example Output:

```yml
base_fee: "1000000"
base_fee: "1.000000000000000000"
index: "0"
learning_rate: "0.125000000000000000"
window:
Expand Down
6 changes: 3 additions & 3 deletions proto/feemarket/feemarket/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ message State {
// BaseFee is the current base fee. This is denominated in the fee per gas
// unit.
string base_fee = 1 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];

// LearningRate is the current learning rate.
string learning_rate = 2 [
(cosmos_proto.scalar) = "cosmos.Legacy",
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
Expand Down
4 changes: 2 additions & 2 deletions proto/feemarket/feemarket/v1/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ message Params {
// MinBaseFee determines the initial base fee of the module and the global
// minimum for the network. This is denominated in fee per gas unit.
string min_base_fee = 5 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];

Expand Down
5 changes: 2 additions & 3 deletions proto/feemarket/feemarket/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ message BaseFeeRequest {}

// StateResponse is the response type for the Query/BaseFee RPC method.
message BaseFeeResponse {
repeated cosmos.base.v1beta1.Coin fees = 1 [
repeated cosmos.base.v1beta1.DecCoin fees = 1 [
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true,
(amino.encoding) = "legacy_coins",
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins"
];
}
7 changes: 4 additions & 3 deletions tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"fmt"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkmath "cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/types/module/testutil"
interchaintest "github.com/strangelove-ventures/interchaintest/v7"
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
Expand Down Expand Up @@ -42,7 +43,7 @@ var (
feemarkettypes.DefaultDelta,
feemarkettypes.DefaultTargetBlockUtilization,
feemarkettypes.DefaultMaxBlockUtilization,
sdk.NewInt(1000),
sdkmath.LegacyNewDec(1000),
feemarkettypes.DefaultMinLearningRate,
feemarkettypes.DefaultMaxLearningRate,
feemarkettypes.DefaultFeeDenom,
Expand All @@ -53,7 +54,7 @@ var (
Key: "app_state.feemarket.state",
Value: feemarkettypes.NewState(
feemarkettypes.DefaultWindow,
sdk.NewInt(1000),
sdkmath.LegacyNewDec(1000),
feemarkettypes.DefaultMaxLearningRate,
),
},
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (s *IntegrationTestSuite) TestParams() {
Beta: math.LegacyMustNewDecFromStr("0.1"),
Theta: math.LegacyMustNewDecFromStr("0.1"),
Delta: math.LegacyMustNewDecFromStr("0.1"),
MinBaseFee: math.NewInt(10),
MinBaseFee: math.LegacyNewDec(10),
MinLearningRate: math.LegacyMustNewDecFromStr("0.1"),
MaxLearningRate: math.LegacyMustNewDecFromStr("0.1"),
TargetBlockUtilization: 5,
Expand Down
2 changes: 1 addition & 1 deletion x/feemarket/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ newBaseFee := currentBaseFee * (1 + newLearningRate * (currentBlockSize - target
> * `MAX_LEARNING_RATE = 1.0`
> * `MIN_LEARNING_RATE = 0.0125`
> * `Current Learning Rate = 0.125`
> * `Previous Base Fee = 10`
> * `Previous Base Fee = 10.0`
> * `Delta = 0`
### Block is Completely Empty
Expand Down
2 changes: 1 addition & 1 deletion x/feemarket/ante/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type BankKeeper interface {
//go:generate mockery --name FeeMarketKeeper --filename mock_feemarket_keeper.go
type FeeMarketKeeper interface {
GetState(ctx sdk.Context) (feemarkettypes.State, error)
GetMinGasPrices(ctx sdk.Context) (sdk.Coins, error)
GetMinGasPrices(ctx sdk.Context) (sdk.DecCoins, error)
GetParams(ctx sdk.Context) (feemarkettypes.Params, error)
SetState(ctx sdk.Context, state feemarkettypes.State) error
SetParams(ctx sdk.Context, params feemarkettypes.Params) error
Expand Down
6 changes: 2 additions & 4 deletions x/feemarket/ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,13 @@ func (dfd FeeMarketCheckDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula
}
}

minGasPricesDecCoins := sdk.NewDecCoinsFromCoins(minGasPrices...)
newCtx := ctx.WithPriority(getTxPriority(fee, int64(gas))).WithMinGasPrices(minGasPricesDecCoins)
newCtx := ctx.WithPriority(getTxPriority(fee, int64(gas))).WithMinGasPrices(minGasPrices)
return next(newCtx, tx, simulate)
}

// CheckTxFees implements the logic for the fee market to check if a Tx has provided sufficient
// fees given the current state of the fee market. Returns an error if insufficient fees.
func CheckTxFees(ctx sdk.Context, minFees sdk.Coins, feeTx sdk.FeeTx, isCheck bool) (feeCoins sdk.Coins, tip sdk.Coins, err error) {
minFeesDecCoins := sdk.NewDecCoinsFromCoins(minFees...)
func CheckTxFees(ctx sdk.Context, minFeesDecCoins sdk.DecCoins, feeTx sdk.FeeTx, isCheck bool) (feeCoins sdk.Coins, tip sdk.Coins, err error) {
feeCoins = feeTx.GetFee()

// Ensure that the provided fees meet the minimum
Expand Down
4 changes: 2 additions & 2 deletions x/feemarket/ante/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
func TestAnteHandle(t *testing.T) {
// Same data for every test case
gasLimit := antesuite.NewTestGasLimit()
validFeeAmount := types.DefaultMinBaseFee.MulRaw(int64(gasLimit))
validFee := sdk.NewCoins(sdk.NewCoin("stake", validFeeAmount))
validFeeAmount := types.DefaultMinBaseFee.MulInt64(int64(gasLimit))
validFee := sdk.NewCoins(sdk.NewCoin("stake", validFeeAmount.TruncateInt()))

testCases := []antesuite.TestCase{
{
Expand Down
2 changes: 1 addition & 1 deletion x/feemarket/fuzz/eip1559_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestBaseFee(t *testing.T) {
params := CreateRandomParams(t)

// Update the current base fee to be 10% higher than the minimum base fee.
prevBaseFee := state.BaseFee.Mul(math.NewInt(11)).Quo(math.NewInt(10))
prevBaseFee := state.BaseFee.Mul(math.LegacyNewDec(11)).Quo(math.LegacyNewDec(10))
state.BaseFee = prevBaseFee

// Randomly generate the block utilization.
Expand Down
14 changes: 7 additions & 7 deletions x/feemarket/keeper/feemarket.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ func (k *Keeper) UpdateFeeMarket(ctx sdk.Context) error {
}

// GetBaseFee returns the base fee from the fee market state.
func (k *Keeper) GetBaseFee(ctx sdk.Context) (math.Int, error) {
func (k *Keeper) GetBaseFee(ctx sdk.Context) (math.LegacyDec, error) {
state, err := k.GetState(ctx)
if err != nil {
return math.Int{}, err
return math.LegacyDec{}, err
}

return state.BaseFee, nil
Expand All @@ -69,19 +69,19 @@ func (k *Keeper) GetLearningRate(ctx sdk.Context) (math.LegacyDec, error) {
}

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

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

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

return minGasPrices, nil
}
Loading

0 comments on commit b8d3623

Please sign in to comment.