Skip to content

Commit

Permalink
l2geth: set latest queue index and index after tx applied
Browse files Browse the repository at this point in the history
Repair the latest known queue index on startup to prevent
skipped deposits
  • Loading branch information
tynes committed Nov 5, 2021
1 parent 7975568 commit e93838d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-pugs-rule.md
@@ -0,0 +1,5 @@
---
'@eth-optimism/l2geth': patch
---

Set the latest queue index and index after the tx has been applied to the chain
45 changes: 45 additions & 0 deletions l2geth/rollup/sync_service.go
Expand Up @@ -318,6 +318,46 @@ func (s *SyncService) initializeLatestL1(ctcDeployHeight *big.Int) error {
}
// No error, the queue element was found
queueIndex = enqueue.GetMeta().QueueIndex
} else {
log.Info("Found latest queue index", "queue-index", *queueIndex)
// The queue index is defined. Work backwards from the tip
// to make sure that the indexed queue index is the latest
// enqueued transaction
block := s.bc.CurrentBlock()
for {
// There are no blocks in the chain
// This should never happen
if block == nil {
log.Warn("Found no genesis block when fixing queue index")
break
}
num := block.Number().Uint64()
// Handle the genesis block
if num == 0 {
log.Info("Hit genesis block when fixing queue index")
queueIndex = nil
break
}
txs := block.Transactions()
// This should never happen
if len(txs) != 1 {
log.Warn("Found block with unexpected number of txs", "count", len(txs), "height", num)
break
}
tx := txs[0]
qi := tx.GetMeta().QueueIndex
// When the queue index is set
if qi != nil {
if qi == queueIndex {
log.Info("Found correct staring queue index", "queue-index", qi)
} else {
log.Info("Found incorrect staring queue index, fixing", "old", queueIndex, "new", qi)
queueIndex = qi
}
break
}
block = s.bc.GetBlockByNumber(num - 1)
}
}
s.SetLatestEnqueueIndex(queueIndex)
return nil
Expand Down Expand Up @@ -793,10 +833,15 @@ func (s *SyncService) applyTransactionToTip(tx *types.Transaction) error {
tx.SetIndex(*index + 1)
}
}

// On restart, these values are repaired to handle
// the case where the index is updated but the
// transaction isn't yet added to the chain
s.SetLatestIndex(tx.GetMeta().Index)
if tx.GetMeta().QueueIndex != nil {
s.SetLatestEnqueueIndex(tx.GetMeta().QueueIndex)
}

// The index was set above so it is safe to dereference
log.Debug("Applying transaction to tip", "index", *tx.GetMeta().Index, "hash", tx.Hash().Hex(), "origin", tx.QueueOrigin().String())

Expand Down

0 comments on commit e93838d

Please sign in to comment.