Skip to content

Commit

Permalink
fix(sync-service): prevent underflows (ethereum-optimism#1015)
Browse files Browse the repository at this point in the history
* fix(sync-service): prevent underflows

* chore: add changeset

* chore: remove dead confirmation depth

* chore: remove eth1conf depth from rollup config
  • Loading branch information
gakonst authored Jun 3, 2021
1 parent b355be0 commit 5e4eaea
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-houses-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@eth-optimism/l2geth': patch
---

fix potential underflow when launching the chain when the last verified index is 0
2 changes: 0 additions & 2 deletions l2geth/rollup/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
type Config struct {
// Maximum calldata size for a Queue Origin Sequencer Tx
MaxCallDataSize int
// Number of confs before applying a L1 to L2 tx
Eth1ConfirmationDepth uint64
// Verifier mode
IsVerifier bool
// Enable the sync service
Expand Down
13 changes: 8 additions & 5 deletions l2geth/rollup/sync_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ type SyncService struct {
syncing atomic.Value
chainHeadSub event.Subscription
OVMContext OVMContext
confirmationDepth uint64
pollInterval time.Duration
timestampRefreshThreshold time.Duration
chainHeadCh chan core.ChainHeadEvent
Expand Down Expand Up @@ -103,7 +102,6 @@ func NewSyncService(ctx context.Context, cfg Config, txpool *core.TxPool, bc *co
cancel: cancel,
verifier: cfg.IsVerifier,
enable: cfg.Eth1SyncServiceEnable,
confirmationDepth: cfg.Eth1ConfirmationDepth,
syncing: atomic.Value{},
bc: bc,
txpool: txpool,
Expand Down Expand Up @@ -244,8 +242,12 @@ func (s *SyncService) initializeLatestL1(ctcDeployHeight *big.Int) error {
s.SetLatestL1Timestamp(context.Timestamp)
s.SetLatestL1BlockNumber(context.BlockNumber)
} else {
// Prevent underflows
if *index != 0 {
*index = *index - 1
}
log.Info("Found latest index", "index", *index)
block := s.bc.GetBlockByNumber(*index - 1)
block := s.bc.GetBlockByNumber(*index)
if block == nil {
block = s.bc.CurrentBlock()
idx := block.Number().Uint64()
Expand All @@ -254,11 +256,12 @@ func (s *SyncService) initializeLatestL1(ctcDeployHeight *big.Int) error {
return fmt.Errorf("Current block height greater than index")
}
s.SetLatestIndex(&idx)
log.Info("Block not found, resetting index", "new", idx, "old", *index-1)
log.Info("Block not found, resetting index", "new", idx, "old", *index)
}
txs := block.Transactions()
if len(txs) != 1 {
log.Error("Unexpected number of transactions in block: %d", len(txs))
log.Error("Unexpected number of transactions in block", "count", len(txs))
panic("Cannot recover OVM Context")
}
tx := txs[0]
s.SetLatestL1Timestamp(tx.L1Timestamp())
Expand Down

0 comments on commit 5e4eaea

Please sign in to comment.