Skip to content

Commit

Permalink
Cherry-pick #3480, #3542: Stop syncing l1inforoot when invalid l1info…
Browse files Browse the repository at this point in the history
…root is detected (#3552)

* Add sanity-check on checkL1InfoTreeUpdate to check state L1 blockhash matches ethereum blockhash (L1 reorg check) (#3480)

* Add sanity check on checkL1InfoTreeUpdate to check state L1 blockhash matches ethereum blockhash (L1 reorg check)

* fix L1 block is 0 (empty l1infotree)

* Update Prover image to v5.0.9

* Stop syncing l1inforoot when invalid l1inforoot is detected (#3542)

* stop syncing l1inforoot when invalid l1inforoot is detected

* fix linter
  • Loading branch information
agnusmor authored Apr 10, 2024
1 parent bf4aa03 commit 239af37
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
2 changes: 2 additions & 0 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const (
EventID_UsedZKCountersOverflow EventID = "USED ZKCOUNTERS OVERFLOW"
// EventID_ReservedZKCountersOverflow is triggered when reserved ZK counters exceeds remaining batch ZK counters
EventID_ReservedZKCountersOverflow EventID = "RESERVED ZKCOUNTERS OVERFLOW"
// EventID_InvalidInfoRoot is triggered when an invalid l1InfoRoot was synced
EventID_InvalidInfoRoot EventID = "INVALID INFOROOT"
// Source_Node is the source of the event
Source_Node Source = "node"

Expand Down
48 changes: 44 additions & 4 deletions sequencer/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,30 @@ func (f *finalizer) updateFlushIDs(newPendingFlushID, newStoredFlushID uint64) {

func (f *finalizer) checkL1InfoTreeUpdate(ctx context.Context) {
firstL1InfoRootUpdate := true
skipFirstSleep := true

if f.cfg.L1InfoTreeCheckInterval.Duration.Seconds() == 999999 { //nolint:gomnd
if !f.lastL1InfoTreeValid {
f.lastL1InfoTreeCond.L.Lock()
f.lastL1InfoTreeValid = true
f.lastL1InfoTreeCond.Broadcast()
f.lastL1InfoTreeCond.L.Unlock()
}

return
}

for {
if skipFirstSleep {
skipFirstSleep = false
} else {
time.Sleep(f.cfg.L1InfoTreeCheckInterval.Duration)
}

lastL1BlockNumber, err := f.etherman.GetLatestBlockNumber(ctx)
if err != nil {
log.Errorf("error getting latest L1 block number, error: %v", err)
continue
}

maxBlockNumber := uint64(0)
Expand All @@ -246,9 +265,32 @@ func (f *finalizer) checkL1InfoTreeUpdate(ctx context.Context) {
}

if firstL1InfoRootUpdate || l1InfoRoot.L1InfoTreeIndex > f.lastL1InfoTree.L1InfoTreeIndex {
firstL1InfoRootUpdate = false
log.Infof("received new L1InfoRoot, l1InfoTreeIndex: %d, l1InfoTreeRoot: %s, l1Block: %d",
l1InfoRoot.L1InfoTreeIndex, l1InfoRoot.L1InfoTreeRoot, l1InfoRoot.BlockNumber)

log.Debugf("received new L1InfoRoot. L1InfoTreeIndex: %d", l1InfoRoot.L1InfoTreeIndex)
// Sanity check l1BlockState (l1InfoRoot.BlockNumber) blockhash matches blockhash on ethereum. We skip it if l1InfoRoot.BlockNumber == 0 (empty tree)
if l1InfoRoot.BlockNumber > 0 {
l1BlockState, err := f.stateIntf.GetBlockByNumber(ctx, l1InfoRoot.BlockNumber, nil)
if err != nil {
log.Errorf("error getting L1 block %d from the state, error: %v", l1InfoRoot.BlockNumber, err)
continue
}

l1BlockEth, err := f.etherman.HeaderByNumber(ctx, new(big.Int).SetUint64(l1InfoRoot.BlockNumber))
if err != nil {
log.Errorf("error getting L1 block %d from ethereum, error: %v", l1InfoRoot.BlockNumber, err)
continue
}
if l1BlockState.BlockHash != l1BlockEth.Hash() {
warnmsg := fmt.Sprintf("invalid l1InfoTreeIndex %d, L1 block %d blockhash %s doesn't match blockhash on ethereum %s (L1 reorg?). Stopping syncing l1IntroTreeIndex",
l1InfoRoot.L1InfoTreeIndex, l1InfoRoot.BlockNumber, l1BlockState.BlockHash, l1BlockEth.Hash())
log.Warn(warnmsg)
f.LogEvent(ctx, event.Level_Critical, event.EventID_InvalidInfoRoot, warnmsg, nil)
return
}
}

firstL1InfoRootUpdate = false

f.lastL1InfoTreeMux.Lock()
f.lastL1InfoTree = l1InfoRoot
Expand All @@ -261,8 +303,6 @@ func (f *finalizer) checkL1InfoTreeUpdate(ctx context.Context) {
f.lastL1InfoTreeCond.L.Unlock()
}
}

time.Sleep(f.cfg.L1InfoTreeCheckInterval.Duration)
}
}

Expand Down
1 change: 1 addition & 0 deletions sequencer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type etherman interface {
TrustedSequencer() (common.Address, error)
GetLatestBatchNumber() (uint64, error)
GetLatestBlockNumber(ctx context.Context) (uint64, error)
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
}

// stateInterface gathers the methods required to interact with the state.
Expand Down
33 changes: 33 additions & 0 deletions sequencer/mock_etherman.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 239af37

Please sign in to comment.