From aef876a45620adc2c8fd418c0bdda3205ffa4e9b Mon Sep 17 00:00:00 2001 From: mmsqe Date: Fri, 29 Aug 2025 08:20:42 +0800 Subject: [PATCH 1/4] chore: remove NotifyNewBlock in EndBlocker this change should have been included in https://github.com/cosmos/evm/commit/642c8e989405b8604b61001c61cfaa6928a35ec1 --- CHANGELOG.md | 1 + x/vm/keeper/abci.go | 5 ----- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6abdf0aec..39d2da04a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ - [\#467](https://github.com/cosmos/evm/pull/467) Replace GlobalEVMMempool by passing to JSONRPC on initiate. - [\#352](https://github.com/cosmos/evm/pull/352) Remove the creation of a Geth EVM instance, stateDB during the AnteHandler balance check. - [\#496](https://github.com/cosmos/evm/pull/496) Simplify mempool instantiation by using configs instead of objects. +- [\#563](https://github.com/cosmos/evm/pull/563) Cleanup unnecessary GetBlockchain.NotifyNewBlock() in EndBlocker. ### FEATURES diff --git a/x/vm/keeper/abci.go b/x/vm/keeper/abci.go index b10026070..6c59ca765 100644 --- a/x/vm/keeper/abci.go +++ b/x/vm/keeper/abci.go @@ -42,11 +42,6 @@ func (k *Keeper) BeginBlock(ctx sdk.Context) error { func (k *Keeper) EndBlock(ctx sdk.Context) error { // Gas costs are handled within msg handler so costs should be ignored infCtx := ctx.WithGasMeter(storetypes.NewInfiniteGasMeter()) - - if k.evmMempool != nil { - k.evmMempool.GetBlockchain().NotifyNewBlock() - } - bloom := ethtypes.BytesToBloom(k.GetBlockBloomTransient(infCtx).Bytes()) k.EmitBlockBloomEvent(infCtx, bloom) From ae9ab69789abf32dae314044c8288619e29fc2fd Mon Sep 17 00:00:00 2001 From: mmsqe Date: Fri, 29 Aug 2025 09:08:29 +0800 Subject: [PATCH 2/4] fix test --- testutil/integration/evm/network/abci.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/testutil/integration/evm/network/abci.go b/testutil/integration/evm/network/abci.go index fc5bf9c54..25395313d 100644 --- a/testutil/integration/evm/network/abci.go +++ b/testutil/integration/evm/network/abci.go @@ -7,6 +7,8 @@ import ( cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" cmttypes "github.com/cometbft/cometbft/types" + evmmempool "github.com/cosmos/evm/mempool" + storetypes "cosmossdk.io/store/types" ) @@ -49,6 +51,9 @@ func (n *IntegrationNetwork) finalizeBlockAndCommit(duration time.Duration, txBy if err != nil { return nil, err } + if evmMempool, ok := n.app.GetMempool().(*evmmempool.ExperimentalEVMMempool); ok { + evmMempool.GetBlockchain().NotifyNewBlock() + } newCtx := n.app.GetBaseApp().NewContextLegacy(false, header) From b3f6d8fe008a35b14e0de509ed399cd40b978f56 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Sat, 30 Aug 2025 09:33:09 +0800 Subject: [PATCH 3/4] ensures block notify only triggered for legacy or non-event bus --- CHANGELOG.md | 2 +- mempool/mempool.go | 6 +++++- testutil/integration/evm/network/abci.go | 5 ----- x/vm/keeper/abci.go | 5 +++++ 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16c7e5f37..793314255 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,7 @@ - [\#467](https://github.com/cosmos/evm/pull/467) Replace GlobalEVMMempool by passing to JSONRPC on initiate. - [\#352](https://github.com/cosmos/evm/pull/352) Remove the creation of a Geth EVM instance, stateDB during the AnteHandler balance check. - [\#496](https://github.com/cosmos/evm/pull/496) Simplify mempool instantiation by using configs instead of objects. -- [\#563](https://github.com/cosmos/evm/pull/563) Cleanup unnecessary GetBlockchain.NotifyNewBlock() in EndBlocker. +- [\#563](https://github.com/cosmos/evm/pull/563) Removes unnecessary block notifications when the event bus is already set up. ### FEATURES diff --git a/mempool/mempool.go b/mempool/mempool.go index e2a574e3e..97351061f 100644 --- a/mempool/mempool.go +++ b/mempool/mempool.go @@ -381,7 +381,7 @@ func (m *ExperimentalEVMMempool) SelectBy(goCtx context.Context, i [][]byte, f f // SetEventBus sets CometBFT event bus to listen for new block header event. func (m *ExperimentalEVMMempool) SetEventBus(eventBus *cmttypes.EventBus) { - if m.eventBus != nil { + if m.HasEventBus() { m.eventBus.Unsubscribe(context.Background(), SubscriberName, stream.NewBlockHeaderEvents) //nolint: errcheck } m.eventBus = eventBus @@ -396,6 +396,10 @@ func (m *ExperimentalEVMMempool) SetEventBus(eventBus *cmttypes.EventBus) { }() } +func (m *ExperimentalEVMMempool) HasEventBus() bool { + return m.eventBus != nil +} + // Close unsubscribes from the CometBFT event bus and shuts down the mempool. func (m *ExperimentalEVMMempool) Close() error { var errs []error diff --git a/testutil/integration/evm/network/abci.go b/testutil/integration/evm/network/abci.go index 25395313d..fc5bf9c54 100644 --- a/testutil/integration/evm/network/abci.go +++ b/testutil/integration/evm/network/abci.go @@ -7,8 +7,6 @@ import ( cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" cmttypes "github.com/cometbft/cometbft/types" - evmmempool "github.com/cosmos/evm/mempool" - storetypes "cosmossdk.io/store/types" ) @@ -51,9 +49,6 @@ func (n *IntegrationNetwork) finalizeBlockAndCommit(duration time.Duration, txBy if err != nil { return nil, err } - if evmMempool, ok := n.app.GetMempool().(*evmmempool.ExperimentalEVMMempool); ok { - evmMempool.GetBlockchain().NotifyNewBlock() - } newCtx := n.app.GetBaseApp().NewContextLegacy(false, header) diff --git a/x/vm/keeper/abci.go b/x/vm/keeper/abci.go index 6c59ca765..558c230be 100644 --- a/x/vm/keeper/abci.go +++ b/x/vm/keeper/abci.go @@ -42,6 +42,11 @@ func (k *Keeper) BeginBlock(ctx sdk.Context) error { func (k *Keeper) EndBlock(ctx sdk.Context) error { // Gas costs are handled within msg handler so costs should be ignored infCtx := ctx.WithGasMeter(storetypes.NewInfiniteGasMeter()) + + if k.evmMempool != nil && !k.evmMempool.HasEventBus() { + k.evmMempool.GetBlockchain().NotifyNewBlock() + } + bloom := ethtypes.BytesToBloom(k.GetBlockBloomTransient(infCtx).Bytes()) k.EmitBlockBloomEvent(infCtx, bloom) From 67e6529b2e0181ee5c5c29221ff9826fa502e7d3 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Mon, 1 Sep 2025 09:21:42 +0800 Subject: [PATCH 4/4] fix reorg on restart cleanup --- CHANGELOG.md | 2 +- mempool/blockchain.go | 5 +++++ mempool/mempool.go | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61a0e3d0f..3ee859ff7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,7 @@ - [\#467](https://github.com/cosmos/evm/pull/467) Replace GlobalEVMMempool by passing to JSONRPC on initiate. - [\#352](https://github.com/cosmos/evm/pull/352) Remove the creation of a Geth EVM instance, stateDB during the AnteHandler balance check. - [\#496](https://github.com/cosmos/evm/pull/496) Simplify mempool instantiation by using configs instead of objects. -- [\#563](https://github.com/cosmos/evm/pull/563) Removes unnecessary block notifications when the event bus is already set up. +- [\#568](https://github.com/cosmos/evm/pull/568) Avoid unnecessary block notifications when the event bus is already set up. - [\#511](https://github.com/cosmos/evm/pull/511) Minor code cleanup for `AddPrecompileFn`. ### FEATURES diff --git a/mempool/blockchain.go b/mempool/blockchain.go index 00fc7dd40..fc4be909b 100644 --- a/mempool/blockchain.go +++ b/mempool/blockchain.go @@ -86,6 +86,11 @@ func (b Blockchain) CurrentBlock() *types.Header { } blockHeight := ctx.BlockHeight() + // prevent the reorg from triggering after a restart since previousHeaderHash is stored as an in-memory variable + if blockHeight > 1 && b.previousHeaderHash == (common.Hash{}) { + return b.zeroHeader + } + blockTime := ctx.BlockTime().Unix() gasUsed := b.feeMarketKeeper.GetBlockGasWanted(ctx) appHash := common.BytesToHash(ctx.BlockHeader().AppHash) diff --git a/mempool/mempool.go b/mempool/mempool.go index 97351061f..23ad670a2 100644 --- a/mempool/mempool.go +++ b/mempool/mempool.go @@ -396,6 +396,7 @@ func (m *ExperimentalEVMMempool) SetEventBus(eventBus *cmttypes.EventBus) { }() } +// HasEventBus returns true if the blockchain is configured to use an event bus for block notifications. func (m *ExperimentalEVMMempool) HasEventBus() bool { return m.eventBus != nil }