Skip to content

Commit 8ad46ff

Browse files
committed
Revert "(chore): move feemarket update BaseFee to EndBlock (cosmos#190)" (cosmos#255)
1 parent d8909d8 commit 8ad46ff

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
lines changed

testutil/integration/common/factory/helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (tf *baseTxFactory) calculateFees(gasPrice *sdkmath.Int, gasLimit uint64) (
8787
return sdktypes.Coins{}, errorsmod.Wrap(err, "failed to get base fee")
8888
}
8989
price := resp.BaseFee
90-
fees = sdktypes.Coins{{Denom: denom, Amount: price.MulInt64(int64(gasLimit)).Ceil().RoundInt()}} //#nosec G115
90+
fees = sdktypes.Coins{{Denom: denom, Amount: price.MulInt64(int64(gasLimit)).TruncateInt()}} //#nosec G115
9191
}
9292
return fees, nil
9393
}

x/feemarket/keeper/abci.go

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,41 @@ import (
1212
sdk "github.com/cosmos/cosmos-sdk/types"
1313
)
1414

15-
// EndBlock handles both gas tracking and base fee calculation for the next block
15+
// BeginBlock updates base fee
16+
func (k *Keeper) BeginBlock(ctx sdk.Context) error {
17+
baseFee := k.CalculateBaseFee(ctx)
18+
19+
// return immediately if base fee is nil
20+
if baseFee.IsNil() {
21+
return nil
22+
}
23+
24+
k.SetBaseFee(ctx, baseFee)
25+
26+
defer func() {
27+
floatBaseFee, err := baseFee.Float64()
28+
if err != nil {
29+
ctx.Logger().Error("error converting base fee to float64", "error", err.Error())
30+
return
31+
}
32+
// there'll be no panic if fails to convert to float32. Will only loose precision
33+
telemetry.SetGauge(float32(floatBaseFee), "feemarket", "base_fee")
34+
}()
35+
36+
// Store current base fee in event
37+
ctx.EventManager().EmitEvents(sdk.Events{
38+
sdk.NewEvent(
39+
types.EventTypeFeeMarket,
40+
sdk.NewAttribute(types.AttributeKeyBaseFee, baseFee.String()),
41+
),
42+
})
43+
44+
return nil
45+
}
46+
47+
// EndBlock update block gas wanted.
48+
// The EVM end block logic doesn't update the validator set, thus it returns
49+
// an empty slice.
1650
func (k *Keeper) EndBlock(ctx sdk.Context) error {
1751
if ctx.BlockGasMeter() == nil {
1852
err := errors.New("block gas meter is nil when setting block gas wanted")
@@ -44,32 +78,8 @@ func (k *Keeper) EndBlock(ctx sdk.Context) error {
4478
updatedGasWanted := math.LegacyMaxDec(limitedGasWanted, math.LegacyNewDec(gasUsed.Int64())).TruncateInt().Uint64()
4579
k.SetBlockGasWanted(ctx, updatedGasWanted)
4680

47-
nextBaseFee := k.CalculateBaseFee(ctx)
48-
49-
// Set the calculated base fee for use in the next block
50-
if !nextBaseFee.IsNil() {
51-
k.SetBaseFee(ctx, nextBaseFee)
52-
53-
ctx.EventManager().EmitEvents(sdk.Events{
54-
sdk.NewEvent(
55-
types.EventTypeFeeMarket,
56-
sdk.NewAttribute(types.AttributeKeyBaseFee, nextBaseFee.String()),
57-
sdk.NewAttribute("calculated_at_block", fmt.Sprintf("%d", ctx.BlockHeight())),
58-
),
59-
})
60-
}
61-
6281
defer func() {
6382
telemetry.SetGauge(float32(updatedGasWanted), "feemarket", "block_gas")
64-
65-
if !nextBaseFee.IsNil() {
66-
floatBaseFee, err := nextBaseFee.Float64()
67-
if err != nil {
68-
ctx.Logger().Error("error converting next base fee to float64", "error", err.Error())
69-
return
70-
}
71-
telemetry.SetGauge(float32(floatBaseFee), "feemarket", "next_base_fee")
72-
}
7383
}()
7484

7585
ctx.EventManager().EmitEvent(sdk.NewEvent(

x/feemarket/module.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ var (
3232
_ module.AppModule = AppModule{}
3333
_ module.AppModuleBasic = AppModuleBasic{}
3434

35-
_ appmodule.HasEndBlocker = AppModule{}
36-
_ module.HasABCIGenesis = AppModule{}
35+
_ appmodule.HasEndBlocker = AppModule{}
36+
_ appmodule.HasBeginBlocker = AppModule{}
37+
_ module.HasABCIGenesis = AppModule{}
3738
)
3839

3940
// AppModuleBasic defines the basic application module used by the fee market module.
@@ -119,6 +120,12 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
119120
types.RegisterMsgServer(cfg.MsgServer(), &am.keeper)
120121
}
121122

123+
// BeginBlock returns the begin block for the fee market module.
124+
func (am AppModule) BeginBlock(ctx context.Context) error {
125+
c := sdk.UnwrapSDKContext(ctx)
126+
return am.keeper.BeginBlock(c)
127+
}
128+
122129
// EndBlock returns the end blocker for the fee market module. It returns no validator
123130
// updates.
124131
func (am AppModule) EndBlock(ctx context.Context) error {

0 commit comments

Comments
 (0)