Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

change basefee to be a module param #943

Merged
merged 12 commits into from
Feb 23, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (evm) [tharsis#901](https://github.com/tharsis/ethermint/pull/901) Support multiple MsgEthereumTx in single tx.
* (config) [tharsis#908](https://github.com/tharsis/ethermint/pull/908) Add api.enable flag for Cosmos SDK Rest server
* (feemarket) [tharsis#919](https://github.com/tharsis/ethermint/pull/919) Initialize baseFee in default genesis state.
* (feemarket) [tharsis#943](https://github.com/tharsis/ethermint/pull/943) Store the base fee as a module param instead of using state storage.

### Bug Fixes

Expand Down
1 change: 0 additions & 1 deletion app/ante/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func (suite *AnteTestSuite) SetupTest() {
feemarketGenesis := feemarkettypes.DefaultGenesisState()
feemarketGenesis.Params.EnableHeight = 1
feemarketGenesis.Params.NoBaseFee = false
feemarketGenesis.BaseFee = sdk.NewInt(feemarketGenesis.Params.InitialBaseFee)
// Verify feeMarket genesis
err := feemarketGenesis.Validate()
suite.Require().NoError(err)
Expand Down
6 changes: 2 additions & 4 deletions client/docs/statik/statik.go

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions docs/api/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -923,8 +923,8 @@ Params defines the EVM module parameters
| `no_base_fee` | [bool](#bool) | | no base fee forces the EIP-1559 base fee to 0 (needed for 0 price calls) |
| `base_fee_change_denominator` | [uint32](#uint32) | | base fee change denominator bounds the amount the base fee can change between blocks. |
| `elasticity_multiplier` | [uint32](#uint32) | | elasticity multiplier bounds the maximum gas limit an EIP-1559 block may have. |
| `initial_base_fee` | [int64](#int64) | | initial base fee for EIP-1559 blocks. |
| `enable_height` | [int64](#int64) | | height at which the base fee calculation is enabled. |
| `base_fee` | [string](#string) | | base fee for EIP-1559 blocks. |



Expand Down Expand Up @@ -956,7 +956,6 @@ GenesisState defines the feemarket module's genesis state.
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `params` | [Params](#ethermint.feemarket.v1.Params) | | params defines all the paramaters of the module. |
| `base_fee` | [string](#string) | | base fee is the exported value from previous software version. Zero by default. |
| `block_gas` | [uint64](#uint64) | | block gas is the amount of gas used on the last block before the upgrade. Zero by default. |


Expand Down
12 changes: 10 additions & 2 deletions proto/ethermint/feemarket/v1/feemarket.proto
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
syntax = "proto3";
package ethermint.feemarket.v1;

import "gogoproto/gogo.proto";

option go_package = "github.com/tharsis/ethermint/x/feemarket/types";

// Params defines the EVM module parameters
Expand All @@ -13,8 +15,14 @@ message Params {
// elasticity multiplier bounds the maximum gas limit an EIP-1559 block may
// have.
uint32 elasticity_multiplier = 3;
// initial base fee for EIP-1559 blocks.
int64 initial_base_fee = 4;
// DEPRECATED: initial base fee for EIP-1559 blocks.
reserved 4;
reserved "initial_base_fee";
// height at which the base fee calculation is enabled.
int64 enable_height = 5;
// base fee for EIP-1559 blocks.
string base_fee = 6 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}
8 changes: 3 additions & 5 deletions proto/ethermint/feemarket/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ option go_package = "github.com/tharsis/ethermint/x/feemarket/types";
message GenesisState {
// params defines all the paramaters of the module.
Params params = 1 [ (gogoproto.nullable) = false ];
// base fee is the exported value from previous software version.
// DEPRECATED: base fee is the exported value from previous software version.
// Zero by default.
string base_fee = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
reserved 2;
reserved "base_fee";
// block gas is the amount of gas used on the last block before the upgrade.
// Zero by default.
uint64 block_gas = 3;
Expand Down
4 changes: 2 additions & 2 deletions tests/rpc/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ func TestEth_EthResend(t *testing.T) {
func TestEth_FeeHistory(t *testing.T) {
params := make([]interface{}, 0)
params = append(params, 4)
params = append(params, "0x1c")
params = append(params, "0xa")
thomas-nguy marked this conversation as resolved.
Show resolved Hide resolved
params = append(params, []int{25, 75})

rpcRes := call(t, "eth_feeHistory", params)
Expand All @@ -577,7 +577,7 @@ func TestEth_FeeHistory(t *testing.T) {
baseFeePerGas := info["baseFeePerGas"].([]interface{})
gasUsedRatio := info["gasUsedRatio"].([]interface{})

require.Equal(t, info["oldestBlock"].(string), "0x18")
require.Equal(t, info["oldestBlock"].(string), "0x6")
require.Equal(t, 4, len(gasUsedRatio))
require.Equal(t, 4, len(baseFeePerGas))
require.Equal(t, 4, len(reward))
Expand Down
2 changes: 0 additions & 2 deletions x/evm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,8 @@ func (suite *KeeperTestSuite) DoSetupTest(t require.TestingT) {
if suite.enableFeemarket {
feemarketGenesis.Params.EnableHeight = 1
feemarketGenesis.Params.NoBaseFee = false
feemarketGenesis.BaseFee = sdk.NewInt(feemarketGenesis.Params.InitialBaseFee)
} else {
feemarketGenesis.Params.NoBaseFee = true
feemarketGenesis.BaseFee = sdk.NewInt(0)
}
genesis[feemarkettypes.ModuleName] = app.AppCodec().MustMarshalJSON(feemarketGenesis)
if !suite.enableLondonHF {
Expand Down
8 changes: 0 additions & 8 deletions x/feemarket/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,15 @@ func InitGenesis(
data types.GenesisState,
) []abci.ValidatorUpdate {
k.SetParams(ctx, data.Params)
k.SetBaseFee(ctx, data.BaseFee.BigInt())
k.SetBlockGasUsed(ctx, data.BlockGas)

return []abci.ValidatorUpdate{}
}

// ExportGenesis exports genesis state of the fee market module
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
baseFee := sdk.ZeroInt()
baseFeeInt := k.GetBaseFee(ctx)
if baseFeeInt != nil {
baseFee = sdk.NewIntFromBigInt(baseFeeInt)
}

return &types.GenesisState{
Params: k.GetParams(ctx),
BaseFee: baseFee,
BlockGas: k.GetBlockGasUsed(ctx),
}
}
8 changes: 4 additions & 4 deletions x/feemarket/keeper/eip1559.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ func (k Keeper) CalculateBaseFee(ctx sdk.Context) *big.Int {

// If the current block is the first EIP-1559 block, return the InitialBaseFee.
if ctx.BlockHeight() == params.EnableHeight {
return new(big.Int).SetInt64(params.InitialBaseFee)
return params.BaseFee.BigInt()
}

// get the block gas used and the base fee values for the parent block.
parentBaseFee := k.GetBaseFee(ctx)
parentBaseFee := params.BaseFee.BigInt()
if parentBaseFee == nil {
parentBaseFee = new(big.Int).SetInt64(params.InitialBaseFee)
return nil
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
}

parentGasUsed := k.GetBlockGasUsed(ctx)
Expand All @@ -43,7 +43,7 @@ func (k Keeper) CalculateBaseFee(ctx sdk.Context) *big.Int {

parentGasTargetBig := new(big.Int).Div(gasLimit, new(big.Int).SetUint64(uint64(params.ElasticityMultiplier)))
if !parentGasTargetBig.IsUint64() {
return new(big.Int).SetInt64(params.InitialBaseFee)
return nil
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
}

parentGasTarget := parentGasTargetBig.Uint64()
Expand Down
7 changes: 3 additions & 4 deletions x/feemarket/keeper/eip1559_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package keeper_test

import (
"fmt"
"math/big"

abci "github.com/tendermint/tendermint/abci/types"
"math/big"
)

func (suite *KeeperTestSuite) TestCalculateBaseFee() {
Expand All @@ -26,7 +25,7 @@ func (suite *KeeperTestSuite) TestCalculateBaseFee() {
func() {
suite.ctx = suite.ctx.WithBlockHeight(0)
},
big.NewInt(suite.app.FeeMarketKeeper.GetParams(suite.ctx).InitialBaseFee),
suite.app.FeeMarketKeeper.GetParams(suite.ctx).BaseFee.BigInt(),
},
{
"with BaseFee - parent block used the same gas as its target",
Expand All @@ -51,7 +50,7 @@ func (suite *KeeperTestSuite) TestCalculateBaseFee() {
params.ElasticityMultiplier = 1
suite.app.FeeMarketKeeper.SetParams(suite.ctx, params)
},
big.NewInt(suite.app.FeeMarketKeeper.GetParams(suite.ctx).InitialBaseFee),
suite.app.FeeMarketKeeper.GetParams(suite.ctx).BaseFee.BigInt(),
},
{
"with BaseFee - parent block used more gas than its target",
Expand Down
26 changes: 0 additions & 26 deletions x/feemarket/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package keeper

import (
"math/big"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
Expand Down Expand Up @@ -65,27 +63,3 @@ func (k Keeper) SetBlockGasUsed(ctx sdk.Context, gas uint64) {
gasBz := sdk.Uint64ToBigEndian(gas)
store.Set(types.KeyPrefixBlockGasUsed, gasBz)
}

// ----------------------------------------------------------------------------
// Parent Base Fee
// Required by EIP1559 base fee calculation.
// ----------------------------------------------------------------------------

// GetBaseFee returns the last base fee value from the store.
// returns nil if base fee is not enabled.
func (k Keeper) GetBaseFee(ctx sdk.Context) *big.Int {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.KeyPrefixBaseFee)
if len(bz) == 0 {
return nil
}

return new(big.Int).SetBytes(bz)
}

// SetBaseFee set the last base fee value to the store.
// CONTRACT: this should be only called during EndBlock.
func (k Keeper) SetBaseFee(ctx sdk.Context, baseFee *big.Int) {
store := ctx.KVStore(k.storeKey)
store.Set(types.KeyPrefixBaseFee, baseFee.Bytes())
}
24 changes: 23 additions & 1 deletion x/feemarket/keeper/params.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package keeper

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

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tharsis/ethermint/x/feemarket/types"
)

Expand All @@ -16,3 +17,24 @@ func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, &params)
}

// ----------------------------------------------------------------------------
// Parent Base Fee
// Required by EIP1559 base fee calculation.
// ----------------------------------------------------------------------------

// GetConstantFee get's the base fee from the paramSpace
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
// return nil if base fee is not enabled
func (k Keeper) GetBaseFee(ctx sdk.Context) *big.Int {
params := k.GetParams(ctx)
if params.NoBaseFee {
return nil
}
fedekunze marked this conversation as resolved.
Show resolved Hide resolved

return params.BaseFee.BigInt()
}

// SetBaseFee set's the base fee in the paramSpace
func (k Keeper) SetBaseFee(ctx sdk.Context, baseFee *big.Int) {
k.paramSpace.Set(ctx, types.ParamStoreKeyBaseFee, sdk.NewIntFromBigInt(baseFee))
}
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 2 additions & 4 deletions x/feemarket/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import (

"github.com/cosmos/cosmos-sdk/types/module"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tharsis/ethermint/x/feemarket/types"
)

// RandomizedGenState generates a random GenesisState for nft
func RandomizedGenState(simState *module.SimulationState) {
params := types.NewParams(simState.Rand.Uint32()%2 == 0, simState.Rand.Uint32(), simState.Rand.Uint32(), simState.Rand.Int63(), simState.Rand.Int63())
baseFee := sdk.NewInt(simState.Rand.Int63())
params := types.NewParams(simState.Rand.Uint32()%2 == 0, simState.Rand.Uint32(), simState.Rand.Uint32(), simState.Rand.Uint64(), simState.Rand.Int63())
blockGas := simState.Rand.Uint64()
feemarketGenesis := types.NewGenesisState(params, baseFee, blockGas)
feemarketGenesis := types.NewGenesisState(params, blockGas)

bz, err := json.MarshalIndent(feemarketGenesis, "", " ")
if err != nil {
Expand Down
Loading