feemarket: change basefee to be a module param (#943)

* change basefee to a module params

* add changelog and fix linter

* change params type of basefee and remove default base fee

* restaure event

* clean code

* fix proto

* fix protos

* fix logic

* update rpc tests

* fix comment

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
Thomas Nguy
2022-02-23 19:48:44 +01:00
committed by GitHub
co-authored by Federico Kunze Küllmer
parent f21592ebfe
commit bf54193669
20 changed files with 160 additions and 242 deletions
+4 -4
View File
@@ -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
}
parentGasUsed := k.GetBlockGasUsed(ctx)
@@ -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
}
parentGasTarget := parentGasTargetBig.Uint64()
+3 -4
View File
@@ -2,9 +2,8 @@ package keeper_test
import (
"fmt"
"math/big"
abci "github.com/tendermint/tendermint/abci/types"
"math/big"
)
func (suite *KeeperTestSuite) TestCalculateBaseFee() {
@@ -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",
@@ -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",
-26
View File
@@ -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"
@@ -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())
}
+23 -1
View File
@@ -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"
)
@@ -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.
// ----------------------------------------------------------------------------
// GetBaseFee get's the base fee from the paramSpace
// 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
}
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))
}