@@ -404,17 +404,45 @@ func (m *ExperimentalEVMMempool) ReapNewValidTxs(maxBytes uint64, maxGas uint64)
404404func (m * ExperimentalEVMMempool ) Select (goCtx context.Context , i [][]byte ) sdkmempool.Iterator {
405405 m .mtx .Lock ()
406406 defer m .mtx .Unlock ()
407- ctx := sdk .UnwrapSDKContext (goCtx )
408407
409- // Wait for the legacypool to Reset at >= blockHeight (this may have
410- // already happened), to ensure all txs in pending pool are valid.
411- m .legacyTxPool .WaitForReorgHeight (ctx , ctx .BlockHeight ())
408+ return m .buildIterator (goCtx , i )
409+ }
412410
413- evmIterator , cosmosIterator := m .getIterators (goCtx , i )
411+ // SelectBy iterates through transactions until the provided filter function returns false.
412+ // It uses the same unified iterator as Select but allows early termination based on
413+ // custom criteria defined by the filter function.
414+ func (m * ExperimentalEVMMempool ) SelectBy (goCtx context.Context , txs [][]byte , filter func (sdk.Tx ) bool ) {
415+ m .mtx .Lock ()
416+ defer m .mtx .Unlock ()
414417
415- combinedIterator := NewEVMMempoolIterator ( evmIterator , cosmosIterator , m . logger , m . txConfig , m . vmKeeper . GetEvmCoinInfo ( ctx ). Denom , m . blockchain . Config (). ChainID , m . blockchain )
418+ iter := m . buildIterator ( goCtx , txs )
416419
417- return combinedIterator
420+ for iter != nil && filter (iter .Tx ()) {
421+ iter = iter .Next ()
422+ }
423+ }
424+
425+ // buildIterator ensures that EVM mempool has checked txs for reorgs up to COMMITTED
426+ // block height and then returns a combined iterator over EVM & Cosmos txs.
427+ func (m * ExperimentalEVMMempool ) buildIterator (ctx context.Context , txs [][]byte ) sdkmempool.Iterator {
428+ sdkCtx := sdk .UnwrapSDKContext (ctx )
429+
430+ // context has a block height of the next PROPOSED block,
431+ // but we need to wait for the reorg to complete on the previous COMMITTED block.
432+ committedHeight := sdkCtx .BlockHeight () - 1
433+
434+ m .legacyTxPool .WaitForReorgHeight (ctx , committedHeight )
435+
436+ evmIterator , cosmosIterator := m .getIterators (ctx , txs )
437+
438+ return NewEVMMempoolIterator (
439+ evmIterator ,
440+ cosmosIterator ,
441+ m .logger ,
442+ m .txConfig ,
443+ m .vmKeeper .GetEvmCoinInfo (sdkCtx ).Denom ,
444+ m .blockchain ,
445+ )
418446}
419447
420448// CountTx returns the total number of transactions in both EVM and Cosmos pools.
@@ -519,23 +547,6 @@ func (m *ExperimentalEVMMempool) shouldRemoveFromEVMPool(hash common.Hash, reaso
519547 return true
520548}
521549
522- // SelectBy iterates through transactions until the provided filter function returns false.
523- // It uses the same unified iterator as Select but allows early termination based on
524- // custom criteria defined by the filter function.
525- func (m * ExperimentalEVMMempool ) SelectBy (goCtx context.Context , i [][]byte , f func (sdk.Tx ) bool ) {
526- m .mtx .Lock ()
527- defer m .mtx .Unlock ()
528- ctx := sdk .UnwrapSDKContext (goCtx )
529-
530- evmIterator , cosmosIterator := m .getIterators (goCtx , i )
531-
532- combinedIterator := NewEVMMempoolIterator (evmIterator , cosmosIterator , m .logger , m .txConfig , m .vmKeeper .GetEvmCoinInfo (ctx ).Denom , m .blockchain .Config ().ChainID , m .blockchain )
533-
534- for combinedIterator != nil && f (combinedIterator .Tx ()) {
535- combinedIterator = combinedIterator .Next ()
536- }
537- }
538-
539550// SetEventBus sets CometBFT event bus to listen for new block header event.
540551func (m * ExperimentalEVMMempool ) SetEventBus (eventBus * cmttypes.EventBus ) {
541552 if m .HasEventBus () {
@@ -594,30 +605,27 @@ func (m *ExperimentalEVMMempool) getEVMMessage(tx sdk.Tx) (*evmtypes.MsgEthereum
594605// getIterators prepares iterators over pending EVM and Cosmos transactions.
595606// It configures EVM transactions with proper base fee filtering and priority ordering,
596607// while setting up the Cosmos iterator with the provided exclusion list.
597- func (m * ExperimentalEVMMempool ) getIterators (goCtx context.Context , i [][]byte ) (* miner.TransactionsByPriceAndNonce , sdkmempool.Iterator ) {
608+ func (m * ExperimentalEVMMempool ) getIterators (goCtx context.Context , txs [][]byte ) (* miner.TransactionsByPriceAndNonce , sdkmempool.Iterator ) {
598609 ctx := sdk .UnwrapSDKContext (goCtx )
599610 baseFee := m .vmKeeper .GetBaseFee (ctx )
600611 var baseFeeUint * uint256.Int
601612 if baseFee != nil {
602613 baseFeeUint = uint256 .MustFromBig (baseFee )
603614 }
604615
605- m .logger .Debug ("getting iterators" )
606-
607- pendingFilter := txpool.PendingFilter {
616+ evmPendingTxs := m .txPool .Pending (txpool.PendingFilter {
608617 MinTip : m .minTip ,
609618 BaseFee : baseFeeUint ,
610619 BlobFee : nil ,
611620 OnlyPlainTxs : true ,
612621 OnlyBlobTxs : false ,
613622 MaxTxs : 2500 ,
614- }
615- evmPendingTxes := m .txPool .Pending (pendingFilter )
616- orderedEVMPendingTxes := miner .NewTransactionsByPriceAndNonce (nil , evmPendingTxes , baseFee )
623+ })
617624
618- cosmosPendingTxes := m .cosmosPool .Select (ctx , i )
625+ evmIterator := miner .NewTransactionsByPriceAndNonce (nil , evmPendingTxs , baseFee )
626+ cosmosIterator := m .cosmosPool .Select (ctx , txs )
619627
620- return orderedEVMPendingTxes , cosmosPendingTxes
628+ return evmIterator , cosmosIterator
621629}
622630
623631func (m * ExperimentalEVMMempool ) TrackTx (hash common.Hash ) error {
0 commit comments