Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sync-service): prevent underflows #1015

Merged
merged 4 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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