Skip to content

Commit

Permalink
evm and feemarket module must implement beginBlocker and endBlocker i…
Browse files Browse the repository at this point in the history
…nterface
  • Loading branch information
dreamer-zq committed Jan 3, 2025
1 parent 4dfa007 commit dc2ecd5
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 45 deletions.
13 changes: 8 additions & 5 deletions x/evm/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,30 @@
package keeper

import (
"context"

storetypes "cosmossdk.io/store/types"
abci "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"

ethtypes "github.com/ethereum/go-ethereum/core/types"
)

// BeginBlock sets the sdk Context and EIP155 chain id to the Keeper.
func (k *Keeper) BeginBlock(ctx sdk.Context) {
k.WithChainID(ctx)
func (k *Keeper) BeginBlock(goCtx context.Context) error{
ctx := sdk.UnwrapSDKContext(goCtx)
return k.WithChainID(ctx)
}

// EndBlock also retrieves the bloom filter value from the transient store and commits it to the
// KVStore. The EVM end block logic doesn't update the validator set, thus it returns
// an empty slice.
func (k *Keeper) EndBlock(ctx sdk.Context) []abci.ValidatorUpdate {
func (k *Keeper) EndBlock(goCtx context.Context) error {
ctx := sdk.UnwrapSDKContext(goCtx)
// Gas costs are handled within msg handler so costs should be ignored
infCtx := ctx.WithGasMeter(storetypes.NewInfiniteGasMeter())

bloom := ethtypes.BytesToBloom(k.GetBlockBloomTransient(infCtx).Bytes())
k.EmitBlockBloomEvent(infCtx, bloom)

return []abci.ValidatorUpdate{}
return nil
}
8 changes: 5 additions & 3 deletions x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package keeper

import (
"errors"
"math/big"

errorsmod "cosmossdk.io/errors"
Expand Down Expand Up @@ -138,17 +139,18 @@ func (k *Keeper) WithOptions(opts types.Options) *Keeper {
}

// WithChainID sets the chain id to the local variable in the keeper
func (k *Keeper) WithChainID(ctx sdk.Context) {
func (k *Keeper) WithChainID(ctx sdk.Context) error {
chainID, err := ethermint.ParseChainID(ctx.ChainID())
if err != nil {
panic(err)
return err
}

if k.eip155ChainID != nil && k.eip155ChainID.Cmp(chainID) != 0 {
panic("chain id already set")
return errors.New("chain id already set")
}

k.eip155ChainID = chainID
return nil
}

// ChainID returns the EIP155 chain ID for the EVM context
Expand Down
20 changes: 9 additions & 11 deletions x/evm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import (
"encoding/json"
"fmt"

"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

abci "github.com/cometbft/cometbft/abci/types"

"cosmossdk.io/core/appmodule"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand All @@ -39,8 +39,10 @@ import (
)

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModule = AppModule{}
_ appmodule.HasBeginBlocker = AppModule{}
_ appmodule.HasEndBlocker = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
)

// AppModuleBasic defines the basic application module used by the evm module.
Expand Down Expand Up @@ -81,11 +83,7 @@ func (AppModuleBasic) ValidateGenesis(
return genesisState.Validate()
}

// RegisterRESTRoutes performs a no-op as the EVM module doesn't expose REST
// endpoints
func (AppModuleBasic) RegisterRESTRoutes(_ client.Context, _ *mux.Router) {
}

// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the evm module.
func (b AppModuleBasic) RegisterGRPCGatewayRoutes(c client.Context, serveMux *runtime.ServeMux) {
if err := types.RegisterQueryHandlerClient(context.Background(), serveMux, types.NewQueryClient(c)); err != nil {
panic(err)
Expand Down Expand Up @@ -166,13 +164,13 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
func (AppModule) QuerierRoute() string { return types.RouterKey }

// BeginBlock returns the begin block for the evm module.
func (am AppModule) BeginBlock(ctx sdk.Context) {
am.keeper.BeginBlock(ctx)
func (am AppModule) BeginBlock(ctx context.Context) error {
return am.keeper.BeginBlock(ctx)
}

// EndBlock returns the end blocker for the evm module. It returns no validator
// updates.
func (am AppModule) EndBlock(ctx sdk.Context) []abci.ValidatorUpdate {
func (am AppModule) EndBlock(ctx context.Context) error {
return am.keeper.EndBlock(ctx)
}

Expand Down
13 changes: 9 additions & 4 deletions x/feemarket/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package keeper

import (
"context"
"fmt"

"cosmossdk.io/math"
Expand All @@ -26,12 +27,13 @@ import (
)

// BeginBlock updates base fee
func (k *Keeper) BeginBlock(ctx sdk.Context) {
func (k *Keeper) BeginBlock(goCtx context.Context) error {
ctx := sdk.UnwrapSDKContext(goCtx)
baseFee := k.CalculateBaseFee(ctx)

// return immediately if base fee is nil
if baseFee == nil {
return
return nil
}

k.SetBaseFee(ctx, baseFee)
Expand All @@ -47,15 +49,17 @@ func (k *Keeper) BeginBlock(ctx sdk.Context) {
sdk.NewAttribute(types.AttributeKeyBaseFee, baseFee.String()),
),
})
return nil
}

// 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) {
func (k *Keeper) EndBlock(goCtx context.Context) error {
ctx := sdk.UnwrapSDKContext(goCtx)
if ctx.BlockGasMeter() == nil {
k.Logger(ctx).Error("block gas meter is nil when setting block gas wanted")
return
return nil
}

gasWanted := k.GetTransientGasWanted(ctx)
Expand All @@ -79,4 +83,5 @@ func (k *Keeper) EndBlock(ctx sdk.Context) {
sdk.NewAttribute("height", fmt.Sprintf("%d", ctx.BlockHeight())),
sdk.NewAttribute("amount", fmt.Sprintf("%d", gasWanted)),
))
return nil
}
34 changes: 12 additions & 22 deletions x/feemarket/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import (
"encoding/json"
"fmt"

"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

abci "github.com/cometbft/cometbft/abci/types"

"cosmossdk.io/core/appmodule"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand All @@ -39,8 +39,10 @@ import (
)

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModule = AppModule{}
_ appmodule.HasBeginBlocker = AppModule{}
_ appmodule.HasEndBlocker = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
)

// AppModuleBasic defines the basic application module used by the fee market module.
Expand Down Expand Up @@ -81,11 +83,7 @@ func (AppModuleBasic) ValidateGenesis(
return genesisState.Validate()
}

// RegisterRESTRoutes performs a no-op as the EVM module doesn't expose REST
// endpoints
func (AppModuleBasic) RegisterRESTRoutes(_ client.Context, _ *mux.Router) {
}

// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the fee market module.
func (b AppModuleBasic) RegisterGRPCGatewayRoutes(c client.Context, serveMux *runtime.ServeMux) {
if err := types.RegisterQueryHandlerClient(context.Background(), serveMux, types.NewQueryClient(c)); err != nil {
panic(err)
Expand Down Expand Up @@ -156,19 +154,15 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
}
}

// QuerierRoute returns the fee market module's querier route name.
func (AppModule) QuerierRoute() string { return types.RouterKey }

// BeginBlock returns the begin block for the fee market module.
func (am AppModule) BeginBlock(ctx sdk.Context) {
am.keeper.BeginBlock(ctx)
func (am AppModule) BeginBlock(ctx context.Context) error {
return am.keeper.BeginBlock(ctx)
}

// EndBlock returns the end blocker for the fee market module. It returns no validator
// updates.
func (am AppModule) EndBlock(ctx sdk.Context) []abci.ValidatorUpdate {
am.keeper.EndBlock(ctx)
return []abci.ValidatorUpdate{}
func (am AppModule) EndBlock(ctx context.Context) error {
return am.keeper.EndBlock(ctx)
}

// InitGenesis performs genesis initialization for the fee market module. It returns
Expand All @@ -192,12 +186,8 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
return cdc.MustMarshalJSON(gs)
}

// RegisterStoreDecoder registers a decoder for fee market module's types
func (am AppModule) RegisterStoreDecoder(_ simtypes.StoreDecoderRegistry) {}

// ProposalContents doesn't return any content functions for governance proposals.
func (AppModule) ProposalContents(_ module.SimulationState) []simtypes.WeightedProposalContent {
return nil
// RegisterStoreDecoder registers a decoder for fee module's types
func (am AppModule) RegisterStoreDecoder(_ simtypes.StoreDecoderRegistry) {
}

// GenerateGenesisState creates a randomized GenState of the fee market module.
Expand Down

0 comments on commit dc2ecd5

Please sign in to comment.