Skip to content

Commit

Permalink
Optimize sequencer wait time by distinguishing between 'txpool not re…
Browse files Browse the repository at this point in the history
…ady' and 'txpool empty' states. (#1037)
  • Loading branch information
louisliu2048 authored Aug 28, 2024
1 parent aa2a2cd commit 656b349
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
11 changes: 9 additions & 2 deletions zk/stages/stage_sequence_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func SpawnSequencingStage(

log.Info(fmt.Sprintf("[%s] Starting batch %d...", logPrefix, batchState.batchNumber))

var allConditionsOK bool
for blockNumber := executionAt + 1; runLoopBlocks; blockNumber++ {
log.Info(fmt.Sprintf("[%s] Starting block %d (forkid %v)...", logPrefix, blockNumber, batchState.forkId))
logTicker.Reset(10 * time.Second)
Expand Down Expand Up @@ -223,12 +224,18 @@ func SpawnSequencingStage(
return err
}
} else if !batchState.isL1Recovery() {
batchState.blockState.transactionsForInclusion, err = getNextPoolTransactions(ctx, cfg, executionAt, batchState.forkId, batchState.yieldedTransactions)
batchState.blockState.transactionsForInclusion, allConditionsOK, err = getNextPoolTransactions(ctx, cfg, executionAt, batchState.forkId, batchState.yieldedTransactions)
if err != nil {
return err
}

if len(batchState.blockState.transactionsForInclusion) == 0 {
time.Sleep(batchContext.cfg.zk.SequencerTimeoutOnEmptyTxPool)
if allConditionsOK {
time.Sleep(batchContext.cfg.zk.SequencerTimeoutOnEmptyTxPool)
} else {
time.Sleep(batchContext.cfg.zk.SequencerTimeoutOnEmptyTxPool / 5) // we do not need to sleep too long for txpool not ready
}

} else {
log.Trace(fmt.Sprintf("[%s] Yielded transactions from the pool", logPrefix), "txCount", len(batchState.blockState.transactionsForInclusion))
}
Expand Down
9 changes: 5 additions & 4 deletions zk/stages/stage_sequence_execute_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ import (
"github.com/ledgerwatch/log/v3"
)

func getNextPoolTransactions(ctx context.Context, cfg SequenceBlockCfg, executionAt, forkId uint64, alreadyYielded mapset.Set[[32]byte]) ([]types.Transaction, error) {
func getNextPoolTransactions(ctx context.Context, cfg SequenceBlockCfg, executionAt, forkId uint64, alreadyYielded mapset.Set[[32]byte]) ([]types.Transaction, bool, error) {
cfg.txPool.LockFlusher()
defer cfg.txPool.UnlockFlusher()

var transactions []types.Transaction
var allConditionsOk bool
var err error

gasLimit := utils.GetBlockGasLimitForFork(forkId)

if err := cfg.txPoolDb.View(ctx, func(poolTx kv.Tx) error {
slots := types2.TxsRlp{}
if _, _, err = cfg.txPool.YieldBest(cfg.yieldSize, &slots, poolTx, executionAt, gasLimit, alreadyYielded); err != nil {
if allConditionsOk, _, err = cfg.txPool.YieldBest(cfg.yieldSize, &slots, poolTx, executionAt, gasLimit, alreadyYielded); err != nil {
return err
}
yieldedTxs, err := extractTransactionsFromSlot(&slots)
Expand All @@ -42,10 +43,10 @@ func getNextPoolTransactions(ctx context.Context, cfg SequenceBlockCfg, executio
transactions = append(transactions, yieldedTxs...)
return nil
}); err != nil {
return nil, err
return nil, allConditionsOk, err
}

return transactions, err
return transactions, allConditionsOk, err
}

func getLimboTransaction(ctx context.Context, cfg SequenceBlockCfg, txHash *common.Hash) ([]types.Transaction, error) {
Expand Down

0 comments on commit 656b349

Please sign in to comment.