Skip to content

Commit 577206e

Browse files
committed
Revert "fix(mempool): wait for reorg height (#900)"
This reverts commit 9c3e1fb.
1 parent d3f9aac commit 577206e

File tree

3 files changed

+39
-57
lines changed

3 files changed

+39
-57
lines changed

mempool/iterator.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,7 @@ type EVMMempoolIterator struct {
4545
// It combines iterators from both transaction pools and selects transactions based on fee priority.
4646
// Returns nil if both iterators are empty or nil. The bondDenom parameter specifies the native
4747
// token denomination for fee comparisons, and chainId is used for EVM transaction conversion.
48-
func NewEVMMempoolIterator(
49-
evmIterator *miner.TransactionsByPriceAndNonce,
50-
cosmosIterator mempool.Iterator,
51-
logger log.Logger,
52-
txConfig client.TxConfig,
53-
bondDenom string,
54-
blockchain *Blockchain,
55-
) mempool.Iterator {
48+
func NewEVMMempoolIterator(evmIterator *miner.TransactionsByPriceAndNonce, cosmosIterator mempool.Iterator, logger log.Logger, txConfig client.TxConfig, bondDenom string, chainID *big.Int, blockchain *Blockchain) mempool.Iterator {
5649
// Check if we have any transactions at all
5750
hasEVM := evmIterator != nil && !evmIterator.Empty()
5851
hasCosmos := cosmosIterator != nil && cosmosIterator.Tx() != nil
@@ -71,7 +64,7 @@ func NewEVMMempoolIterator(
7164
logger: logger,
7265
txConfig: txConfig,
7366
bondDenom: bondDenom,
74-
chainID: blockchain.Config().ChainID,
67+
chainID: chainID,
7568
blockchain: blockchain,
7669
}
7770
}

mempool/mempool.go

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -404,45 +404,17 @@ func (m *ExperimentalEVMMempool) ReapNewValidTxs(maxBytes uint64, maxGas uint64)
404404
func (m *ExperimentalEVMMempool) Select(goCtx context.Context, i [][]byte) sdkmempool.Iterator {
405405
m.mtx.Lock()
406406
defer m.mtx.Unlock()
407+
ctx := sdk.UnwrapSDKContext(goCtx)
407408

408-
return m.buildIterator(goCtx, i)
409-
}
410-
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()
417-
418-
iter := m.buildIterator(goCtx, txs)
419-
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
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())
433412

434-
m.legacyTxPool.WaitForReorgHeight(ctx, committedHeight)
413+
evmIterator, cosmosIterator := m.getIterators(goCtx, i)
435414

436-
evmIterator, cosmosIterator := m.getIterators(ctx, txs)
415+
combinedIterator := NewEVMMempoolIterator(evmIterator, cosmosIterator, m.logger, m.txConfig, m.vmKeeper.GetEvmCoinInfo(ctx).Denom, m.blockchain.Config().ChainID, m.blockchain)
437416

438-
return NewEVMMempoolIterator(
439-
evmIterator,
440-
cosmosIterator,
441-
m.logger,
442-
m.txConfig,
443-
m.vmKeeper.GetEvmCoinInfo(sdkCtx).Denom,
444-
m.blockchain,
445-
)
417+
return combinedIterator
446418
}
447419

448420
// CountTx returns the total number of transactions in both EVM and Cosmos pools.
@@ -547,6 +519,23 @@ func (m *ExperimentalEVMMempool) shouldRemoveFromEVMPool(hash common.Hash, reaso
547519
return true
548520
}
549521

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+
550539
// SetEventBus sets CometBFT event bus to listen for new block header event.
551540
func (m *ExperimentalEVMMempool) SetEventBus(eventBus *cmttypes.EventBus) {
552541
if m.HasEventBus() {
@@ -605,26 +594,29 @@ func (m *ExperimentalEVMMempool) getEVMMessage(tx sdk.Tx) (*evmtypes.MsgEthereum
605594
// getIterators prepares iterators over pending EVM and Cosmos transactions.
606595
// It configures EVM transactions with proper base fee filtering and priority ordering,
607596
// while setting up the Cosmos iterator with the provided exclusion list.
608-
func (m *ExperimentalEVMMempool) getIterators(goCtx context.Context, txs [][]byte) (*miner.TransactionsByPriceAndNonce, sdkmempool.Iterator) {
597+
func (m *ExperimentalEVMMempool) getIterators(goCtx context.Context, i [][]byte) (*miner.TransactionsByPriceAndNonce, sdkmempool.Iterator) {
609598
ctx := sdk.UnwrapSDKContext(goCtx)
610599
baseFee := m.vmKeeper.GetBaseFee(ctx)
611600
var baseFeeUint *uint256.Int
612601
if baseFee != nil {
613602
baseFeeUint = uint256.MustFromBig(baseFee)
614603
}
615604

616-
evmPendingTxs := m.txPool.Pending(txpool.PendingFilter{
605+
m.logger.Debug("getting iterators")
606+
607+
pendingFilter := txpool.PendingFilter{
617608
MinTip: m.minTip,
618609
BaseFee: baseFeeUint,
619610
BlobFee: nil,
620611
OnlyPlainTxs: true,
621612
OnlyBlobTxs: false,
622-
})
613+
}
614+
evmPendingTxes := m.txPool.Pending(pendingFilter)
615+
orderedEVMPendingTxes := miner.NewTransactionsByPriceAndNonce(nil, evmPendingTxes, baseFee)
623616

624-
evmIterator := miner.NewTransactionsByPriceAndNonce(nil, evmPendingTxs, baseFee)
625-
cosmosIterator := m.cosmosPool.Select(ctx, txs)
617+
cosmosPendingTxes := m.cosmosPool.Select(ctx, i)
626618

627-
return evmIterator, cosmosIterator
619+
return orderedEVMPendingTxes, cosmosPendingTxes
628620
}
629621

630622
func (m *ExperimentalEVMMempool) TrackTx(hash common.Hash) error {

mempool/txpool/legacypool/legacypool.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,8 @@ var (
162162
// throttleTxMeter counts how many transactions are rejected due to too-many-changes between
163163
// txpool reorgs.
164164
throttleTxMeter = metrics.NewRegisteredMeter("txpool/throttle", nil)
165-
166165
// reorgDurationTimer measures how long time a txpool reorg takes.
167-
reorgDurationTimer = metrics.NewRegisteredTimer("txpool/reorgtime", nil)
168-
reorgWaitDurationTimer = metrics.NewRegisteredTimer("txpool/reorgwaittime", nil)
169-
166+
reorgDurationTimer = metrics.NewRegisteredTimer("txpool/reorgtime", nil)
170167
// dropBetweenReorgHistogram counts how many drops we experience between two reorg runs. It is expected
171168
// that this number is pretty low, since txpool reorgs happen very frequently.
172169
dropBetweenReorgHistogram = metrics.NewRegisteredHistogram("txpool/dropbetweenreorg", nil, metrics.NewExpDecaySample(1028, 0.015))
@@ -1395,7 +1392,9 @@ func (pool *LegacyPool) scheduleReorgLoop() {
13951392

13961393
// runReorg runs reset and promoteExecutables on behalf of scheduleReorgLoop.
13971394
func (pool *LegacyPool) runReorg(done chan struct{}, reset *txpoolResetRequest, dirtyAccounts *accountSet, events map[common.Address]*SortedMap) {
1398-
defer func(t0 time.Time) { reorgDurationTimer.UpdateSince(t0) }(time.Now())
1395+
defer func(t0 time.Time) {
1396+
reorgDurationTimer.Update(time.Since(t0))
1397+
}(time.Now())
13991398
defer close(done)
14001399

14011400
var promoteAddrs []common.Address
@@ -2108,8 +2107,6 @@ func (pool *LegacyPool) Clear() {
21082107
// height >= height. If the context is cancelled or the pool is shutting down,
21092108
// this will also return.
21102109
func (pool *LegacyPool) WaitForReorgHeight(ctx context.Context, height int64) {
2111-
defer func(start time.Time) { reorgWaitDurationTimer.UpdateSince(start) }(time.Now())
2112-
21132110
for pool.latestReorgHeight.Load() < height {
21142111
// reorg loop has not run at the target height, subscribe to the
21152112
// outcome of the next reorg loop iteration to know when to check again

0 commit comments

Comments
 (0)