diff --git a/app/app.go b/app/app.go index ffc156f..eba427d 100644 --- a/app/app.go +++ b/app/app.go @@ -67,6 +67,7 @@ import ( "github.com/initia-labs/minievm/app/posthandler" evmindexer "github.com/initia-labs/minievm/indexer" evmconfig "github.com/initia-labs/minievm/x/evm/config" + evmtypes "github.com/initia-labs/minievm/x/evm/types" // kvindexer kvindexermodule "github.com/initia-labs/kvindexer/x/kvindexer" @@ -228,6 +229,7 @@ func NewMinitiaApp( // NOTE: upgrade module is required to be prioritized app.ModuleManager.SetOrderPreBlockers( upgradetypes.ModuleName, + evmtypes.ModuleName, ) // set order of module operations diff --git a/indexer/abci.go b/indexer/abci.go index 9a14fc9..cab4b47 100644 --- a/indexer/abci.go +++ b/indexer/abci.go @@ -201,12 +201,14 @@ func (e *EVMIndexerImpl) ListenFinalizeBlock(ctx context.Context, req abci.Reque } } - // TODO - is this part of the indexer? can we move this to the evm module? - err = e.evmKeeper.TrackBlockHash(sdkCtx, uint64(blockHeight), blockHash) - if err != nil { - e.logger.Error("failed to track block hash", "err", err) - return err - } + // TODO - currently state changes are not supported in abci listener, so we track cosmos block hash at x/evm endblocker. + // - https://github.com/cosmos/cosmos-sdk/issues/22246 + // + // err = e.evmKeeper.TrackBlockHash(sdkCtx, uint64(blockHeight), blockHash) + // if err != nil { + // e.logger.Error("failed to track block hash", "err", err) + // return err + // } return nil } diff --git a/x/evm/abci.go b/x/evm/abci.go new file mode 100644 index 0000000..5a1b8db --- /dev/null +++ b/x/evm/abci.go @@ -0,0 +1,23 @@ +package evm + +import ( + "time" + + "github.com/cosmos/cosmos-sdk/telemetry" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/ethereum/go-ethereum/common" + "github.com/initia-labs/minievm/x/evm/keeper" + "github.com/initia-labs/minievm/x/evm/types" +) + +// PreBlock track latest 256 block hashes +func PreBlock(ctx sdk.Context, k *keeper.Keeper) (sdk.ResponsePreBlock, error) { + defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker) + height := uint64(ctx.BlockHeight()) + if height <= 1 { + return sdk.ResponsePreBlock{}, nil + } + + // current block header hash is created by the previous block, so we track it with height-1 + return sdk.ResponsePreBlock{}, k.TrackBlockHash(ctx, height-1, common.BytesToHash(ctx.HeaderHash())) +} diff --git a/x/evm/module.go b/x/evm/module.go index 0b2642e..b3c17f3 100644 --- a/x/evm/module.go +++ b/x/evm/module.go @@ -29,7 +29,8 @@ var ( _ module.HasConsensusVersion = AppModule{} _ module.HasName = AppModule{} - _ appmodule.AppModule = AppModule{} + _ appmodule.AppModule = AppModule{} + _ appmodule.HasPreBlocker = AppModule{} ) // ---------------------------------------------------------------------------- @@ -157,3 +158,9 @@ func (am AppModule) IsAppModule() {} func (am AppModule) IsOnePerModuleType() {} // ___________________________________________________________________________ + +// PreBlock returns the pre-blocker for the evm module. +func (am AppModule) PreBlock(ctx context.Context) (appmodule.ResponsePreBlock, error) { + c := sdk.UnwrapSDKContext(ctx) + return PreBlock(c, am.keeper) +}