forked from cerc-io/laconicd-deprecated
imp(feemarket): update BaseFee based on GasWanted (#1105)
* add gasWanted transient store keys * add gasWanted transient store keeper functions * add gasWanted transient store tracker * add comment * remove unncesary comment * remove unnecesary function * fix tests * fix bad comment * remove unnecesary comment * update comment * update changelog * Update CHANGELOG.md Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * add GasWantedDecorator * remove unnecesary comments * gasWanted decorator test * fix tests * fix tests and build * fix lint * updated end block event * Update app/ante/fee_market.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * fix undeclared variable * Update app/ante/fee_market_test.go * remove unnecesary line * migrate MinGasMultiplier to FeeMarket module * set limited gas wanted * remove old newKeeper param * update proto comment * fix test * update comments * Update x/feemarket/keeper/abci.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * address comments from review * tidy * tests Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
co-authored by
Federico Kunze Küllmer
parent
8155e1f319
commit
620f6a6770
@@ -34,26 +34,34 @@ func (k *Keeper) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
|
||||
})
|
||||
}
|
||||
|
||||
// EndBlock update block gas used.
|
||||
// EndBlock update block gas wanted.
|
||||
// The EVM end block logic doesn't update the validator set, thus it returns
|
||||
// an empty slice.
|
||||
func (k *Keeper) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) {
|
||||
if ctx.BlockGasMeter() == nil {
|
||||
k.Logger(ctx).Error("block gas meter is nil when setting block gas used")
|
||||
k.Logger(ctx).Error("block gas meter is nil when setting block gas wanted")
|
||||
return
|
||||
}
|
||||
|
||||
gasWanted := k.GetTransientGasWanted(ctx)
|
||||
gasUsed := ctx.BlockGasMeter().GasConsumedToLimit()
|
||||
|
||||
k.SetBlockGasUsed(ctx, gasUsed)
|
||||
// to prevent BaseFee manipulation we limit the gasWanted so that
|
||||
// gasWanted = max(gasWanted * MinGasMultiplier, gasUsed)
|
||||
// this will be keep BaseFee protected from un-penalized manipulation
|
||||
// more info here https://github.com/tharsis/ethermint/pull/1105#discussion_r888798925
|
||||
minGasMultiplier := k.GetParams(ctx).MinGasMultiplier
|
||||
limitedGasWanted := sdk.NewDec(int64(gasWanted)).Mul(minGasMultiplier)
|
||||
gasWanted = sdk.MaxDec(limitedGasWanted, sdk.NewDec(int64(gasUsed))).TruncateInt().Uint64()
|
||||
k.SetBlockGasWanted(ctx, gasWanted)
|
||||
|
||||
defer func() {
|
||||
telemetry.SetGauge(float32(gasUsed), "feemarket", "block_gas")
|
||||
telemetry.SetGauge(float32(gasWanted), "feemarket", "block_gas")
|
||||
}()
|
||||
|
||||
ctx.EventManager().EmitEvent(sdk.NewEvent(
|
||||
"block_gas",
|
||||
sdk.NewAttribute("height", fmt.Sprintf("%d", ctx.BlockHeight())),
|
||||
sdk.NewAttribute("amount", fmt.Sprintf("%d", ctx.BlockGasMeter().GasConsumedToLimit())),
|
||||
sdk.NewAttribute("amount", fmt.Sprintf("%d", gasWanted)),
|
||||
))
|
||||
}
|
||||
|
||||
@@ -2,39 +2,32 @@ package keeper_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
"github.com/tendermint/tendermint/abci/types"
|
||||
)
|
||||
|
||||
func (suite *KeeperTestSuite) TestEndBlock() {
|
||||
testCases := []struct {
|
||||
name string
|
||||
NoBaseFee bool
|
||||
malleate func()
|
||||
expGasUsed uint64
|
||||
name string
|
||||
NoBaseFee bool
|
||||
malleate func()
|
||||
expGasWanted uint64
|
||||
}{
|
||||
{
|
||||
"basFee nil",
|
||||
"baseFee nil",
|
||||
true,
|
||||
func() {},
|
||||
uint64(0),
|
||||
},
|
||||
{
|
||||
"Block gas meter is nil",
|
||||
false,
|
||||
func() {},
|
||||
uint64(0),
|
||||
},
|
||||
{
|
||||
"pass",
|
||||
false,
|
||||
func() {
|
||||
meter := sdk.NewGasMeter(uint64(1000000000))
|
||||
suite.ctx = suite.ctx.WithBlockGasMeter(meter)
|
||||
suite.ctx.BlockGasMeter().ConsumeGas(uint64(5000000), "consume gas")
|
||||
suite.app.FeeMarketKeeper.SetTransientBlockGasWanted(suite.ctx, 5000000)
|
||||
},
|
||||
uint64(5000000),
|
||||
uint64(2500000),
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
@@ -45,11 +38,9 @@ func (suite *KeeperTestSuite) TestEndBlock() {
|
||||
suite.app.FeeMarketKeeper.SetParams(suite.ctx, params)
|
||||
|
||||
tc.malleate()
|
||||
|
||||
req := abci.RequestEndBlock{Height: 1}
|
||||
suite.app.FeeMarketKeeper.EndBlock(suite.ctx, req)
|
||||
gasUsed := suite.app.FeeMarketKeeper.GetBlockGasUsed(suite.ctx)
|
||||
suite.Require().Equal(tc.expGasUsed, gasUsed, tc.name)
|
||||
suite.app.FeeMarketKeeper.EndBlock(suite.ctx, types.RequestEndBlock{Height: 1})
|
||||
gasWanted := suite.app.FeeMarketKeeper.GetBlockGasWanted(suite.ctx)
|
||||
suite.Require().Equal(tc.expGasWanted, gasWanted, tc.name)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ func (k Keeper) CalculateBaseFee(ctx sdk.Context) *big.Int {
|
||||
return nil
|
||||
}
|
||||
|
||||
parentGasUsed := k.GetBlockGasUsed(ctx)
|
||||
parentGasUsed := k.GetBlockGasWanted(ctx)
|
||||
|
||||
gasLimit := new(big.Int).SetUint64(math.MaxUint64)
|
||||
|
||||
|
||||
@@ -28,14 +28,14 @@ func (suite *KeeperTestSuite) TestCalculateBaseFee() {
|
||||
suite.app.FeeMarketKeeper.GetParams(suite.ctx).BaseFee.BigInt(),
|
||||
},
|
||||
{
|
||||
"with BaseFee - parent block used the same gas as its target",
|
||||
"with BaseFee - parent block wanted the same gas as its target",
|
||||
false,
|
||||
func() {
|
||||
// non initial block
|
||||
suite.ctx = suite.ctx.WithBlockHeight(1)
|
||||
|
||||
// Set gas used
|
||||
suite.app.FeeMarketKeeper.SetBlockGasUsed(suite.ctx, 100)
|
||||
suite.app.FeeMarketKeeper.SetBlockGasWanted(suite.ctx, 100)
|
||||
|
||||
// Set target/gasLimit through Consensus Param MaxGas
|
||||
blockParams := abci.BlockParams{
|
||||
@@ -53,12 +53,12 @@ func (suite *KeeperTestSuite) TestCalculateBaseFee() {
|
||||
suite.app.FeeMarketKeeper.GetParams(suite.ctx).BaseFee.BigInt(),
|
||||
},
|
||||
{
|
||||
"with BaseFee - parent block used more gas than its target",
|
||||
"with BaseFee - parent block wanted more gas than its target",
|
||||
false,
|
||||
func() {
|
||||
suite.ctx = suite.ctx.WithBlockHeight(1)
|
||||
|
||||
suite.app.FeeMarketKeeper.SetBlockGasUsed(suite.ctx, 200)
|
||||
suite.app.FeeMarketKeeper.SetBlockGasWanted(suite.ctx, 200)
|
||||
|
||||
blockParams := abci.BlockParams{
|
||||
MaxGas: 100,
|
||||
@@ -74,12 +74,12 @@ func (suite *KeeperTestSuite) TestCalculateBaseFee() {
|
||||
big.NewInt(1125000000),
|
||||
},
|
||||
{
|
||||
"with BaseFee - Parent gas used smaller than parent gas target",
|
||||
"with BaseFee - Parent gas wanted smaller than parent gas target",
|
||||
false,
|
||||
func() {
|
||||
suite.ctx = suite.ctx.WithBlockHeight(1)
|
||||
|
||||
suite.app.FeeMarketKeeper.SetBlockGasUsed(suite.ctx, 50)
|
||||
suite.app.FeeMarketKeeper.SetBlockGasWanted(suite.ctx, 50)
|
||||
|
||||
blockParams := abci.BlockParams{
|
||||
MaxGas: 100,
|
||||
|
||||
@@ -38,7 +38,7 @@ func (k Keeper) BaseFee(c context.Context, _ *types.QueryBaseFeeRequest) (*types
|
||||
// BlockGas implements the Query/BlockGas gRPC method
|
||||
func (k Keeper) BlockGas(c context.Context, _ *types.QueryBlockGasRequest) (*types.QueryBlockGasResponse, error) {
|
||||
ctx := sdk.UnwrapSDKContext(c)
|
||||
gas := k.GetBlockGasUsed(ctx)
|
||||
gas := k.GetBlockGasWanted(ctx)
|
||||
|
||||
return &types.QueryBlockGasResponse{
|
||||
Gas: int64(gas),
|
||||
|
||||
@@ -86,7 +86,7 @@ func (suite *KeeperTestSuite) TestQueryBlockGas() {
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
gas := suite.app.FeeMarketKeeper.GetBlockGasUsed(suite.ctx)
|
||||
gas := suite.app.FeeMarketKeeper.GetBlockGasWanted(suite.ctx)
|
||||
exp := &types.QueryBlockGasResponse{Gas: int64(gas)}
|
||||
|
||||
res, err := suite.queryClient.BlockGas(suite.ctx.Context(), &types.QueryBlockGasRequest{})
|
||||
|
||||
@@ -14,14 +14,15 @@ type Keeper struct {
|
||||
// Protobuf codec
|
||||
cdc codec.BinaryCodec
|
||||
// Store key required for the Fee Market Prefix KVStore.
|
||||
storeKey sdk.StoreKey
|
||||
storeKey sdk.StoreKey
|
||||
transientKey sdk.StoreKey
|
||||
// module specific parameter space that can be configured through governance
|
||||
paramSpace paramtypes.Subspace
|
||||
}
|
||||
|
||||
// NewKeeper generates new fee market module keeper
|
||||
func NewKeeper(
|
||||
cdc codec.BinaryCodec, storeKey sdk.StoreKey, paramSpace paramtypes.Subspace,
|
||||
cdc codec.BinaryCodec, paramSpace paramtypes.Subspace, storeKey, transientKey sdk.StoreKey,
|
||||
) Keeper {
|
||||
// set KeyTable if it has not already been set
|
||||
if !paramSpace.HasKeyTable() {
|
||||
@@ -29,9 +30,10 @@ func NewKeeper(
|
||||
}
|
||||
|
||||
return Keeper{
|
||||
cdc: cdc,
|
||||
storeKey: storeKey,
|
||||
paramSpace: paramSpace,
|
||||
cdc: cdc,
|
||||
storeKey: storeKey,
|
||||
paramSpace: paramSpace,
|
||||
transientKey: transientKey,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,10 +47,18 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
|
||||
// Required by EIP1559 base fee calculation.
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// GetBlockGasUsed returns the last block gas used value from the store.
|
||||
func (k Keeper) GetBlockGasUsed(ctx sdk.Context) uint64 {
|
||||
// SetBlockGasWanted sets the block gas wanted to the store.
|
||||
// CONTRACT: this should be only called during EndBlock.
|
||||
func (k Keeper) SetBlockGasWanted(ctx sdk.Context, gas uint64) {
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
bz := store.Get(types.KeyPrefixBlockGasUsed)
|
||||
gasBz := sdk.Uint64ToBigEndian(gas)
|
||||
store.Set(types.KeyPrefixBlockGasWanted, gasBz)
|
||||
}
|
||||
|
||||
// GetBlockGasWanted returns the last block gas wanted value from the store.
|
||||
func (k Keeper) GetBlockGasWanted(ctx sdk.Context) uint64 {
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
bz := store.Get(types.KeyPrefixBlockGasWanted)
|
||||
if len(bz) == 0 {
|
||||
return 0
|
||||
}
|
||||
@@ -56,10 +66,26 @@ func (k Keeper) GetBlockGasUsed(ctx sdk.Context) uint64 {
|
||||
return sdk.BigEndianToUint64(bz)
|
||||
}
|
||||
|
||||
// SetBlockGasUsed gets the block gas consumed to the store.
|
||||
// CONTRACT: this should be only called during EndBlock.
|
||||
func (k Keeper) SetBlockGasUsed(ctx sdk.Context, gas uint64) {
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
gasBz := sdk.Uint64ToBigEndian(gas)
|
||||
store.Set(types.KeyPrefixBlockGasUsed, gasBz)
|
||||
// GetTransientGasWanted returns the gas wanted in the current block from transient store.
|
||||
func (k Keeper) GetTransientGasWanted(ctx sdk.Context) uint64 {
|
||||
store := ctx.TransientStore(k.transientKey)
|
||||
bz := store.Get(types.KeyPrefixTransientBlockGasWanted)
|
||||
if len(bz) == 0 {
|
||||
return 0
|
||||
}
|
||||
return sdk.BigEndianToUint64(bz)
|
||||
}
|
||||
|
||||
// SetTransientBlockGasWanted sets the block gas wanted to the transient store.
|
||||
func (k Keeper) SetTransientBlockGasWanted(ctx sdk.Context, gasWanted uint64) {
|
||||
store := ctx.TransientStore(k.transientKey)
|
||||
gasBz := sdk.Uint64ToBigEndian(gasWanted)
|
||||
store.Set(types.KeyPrefixTransientBlockGasWanted, gasBz)
|
||||
}
|
||||
|
||||
// AddTransientGasWanted adds the cumulative gas wanted in the transient store
|
||||
func (k Keeper) AddTransientGasWanted(ctx sdk.Context, gasWanted uint64) (uint64, error) {
|
||||
result := k.GetTransientGasWanted(ctx) + gasWanted
|
||||
k.SetTransientBlockGasWanted(ctx, result)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -166,7 +166,7 @@ func (suite *KeeperTestSuite) CommitAfter(t time.Duration) {
|
||||
suite.queryClient = types.NewQueryClient(queryHelper)
|
||||
}
|
||||
|
||||
func (suite *KeeperTestSuite) TestSetGetBlockGasUsed() {
|
||||
func (suite *KeeperTestSuite) TestSetGetBlockGasWanted() {
|
||||
testCases := []struct {
|
||||
name string
|
||||
malleate func()
|
||||
@@ -175,7 +175,7 @@ func (suite *KeeperTestSuite) TestSetGetBlockGasUsed() {
|
||||
{
|
||||
"with last block given",
|
||||
func() {
|
||||
suite.app.FeeMarketKeeper.SetBlockGasUsed(suite.ctx, uint64(1000000))
|
||||
suite.app.FeeMarketKeeper.SetBlockGasWanted(suite.ctx, uint64(1000000))
|
||||
},
|
||||
uint64(1000000),
|
||||
},
|
||||
@@ -183,7 +183,7 @@ func (suite *KeeperTestSuite) TestSetGetBlockGasUsed() {
|
||||
for _, tc := range testCases {
|
||||
tc.malleate()
|
||||
|
||||
gas := suite.app.FeeMarketKeeper.GetBlockGasUsed(suite.ctx)
|
||||
gas := suite.app.FeeMarketKeeper.GetBlockGasWanted(suite.ctx)
|
||||
suite.Require().Equal(tc.expGas, gas, tc.name)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user