@@ -12,7 +12,41 @@ import (
12
12
sdk "github.com/cosmos/cosmos-sdk/types"
13
13
)
14
14
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.
16
50
func (k * Keeper ) EndBlock (ctx sdk.Context ) error {
17
51
if ctx .BlockGasMeter () == nil {
18
52
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 {
44
78
updatedGasWanted := math .LegacyMaxDec (limitedGasWanted , math .LegacyNewDec (gasUsed .Int64 ())).TruncateInt ().Uint64 ()
45
79
k .SetBlockGasWanted (ctx , updatedGasWanted )
46
80
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
-
62
81
defer func () {
63
82
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
- }
73
83
}()
74
84
75
85
ctx .EventManager ().EmitEvent (sdk .NewEvent (
0 commit comments