Skip to content

Commit

Permalink
fix(rollup-relayer): add sanity checks (#1562)
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo authored Nov 18, 2024
1 parent 19b8230 commit fc33061
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions rollup/internal/controller/relayer/l2_relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,14 @@ func (r *Layer2Relayer) ProcessPendingBatches() {
return
}

// check codec version
for _, dbChunk := range dbChunks {
if dbBatch.CodecVersion != dbChunk.CodecVersion {
log.Error("batch codec version is different from chunk codec version", "batch index", dbBatch.Index, "chunk index", dbChunk.Index, "batch codec version", dbBatch.CodecVersion, "chunk codec version", dbChunk.CodecVersion)
return
}
}

chunks := make([]*encoding.Chunk, len(dbChunks))
for i, c := range dbChunks {
blocks, getErr := r.l2BlockOrm.GetL2BlocksInRange(r.ctx, c.StartBlockNumber, c.EndBlockNumber)
Expand All @@ -415,6 +423,11 @@ func (r *Layer2Relayer) ProcessPendingBatches() {
return
}

if dbParentBatch.CodecVersion > dbBatch.CodecVersion {
log.Error("parent batch codec version is greater than current batch codec version", "index", dbBatch.Index, "hash", dbBatch.Hash, "parent codec version", dbParentBatch.CodecVersion, "current codec version", dbBatch.CodecVersion)
return
}

var calldata []byte
var blob *kzg4844.Blob
codecVersion := encoding.CodecVersion(dbBatch.CodecVersion)
Expand Down Expand Up @@ -528,14 +541,41 @@ func (r *Layer2Relayer) ProcessPendingBundles() {
}

func (r *Layer2Relayer) finalizeBundle(bundle *orm.Bundle, withProof bool) error {
// Check if current bundle codec version is not less than the preceding one
if bundle.StartBatchIndex > 0 {
prevBatch, err := r.batchOrm.GetBatchByIndex(r.ctx, bundle.StartBatchIndex-1)
if err != nil {
log.Error("failed to get previous batch",
"current bundle index", bundle.Index,
"start batch index", bundle.StartBatchIndex,
"error", err)
return err
}
if bundle.CodecVersion < prevBatch.CodecVersion {
log.Error("current bundle codec version is less than the preceding batch",
"current bundle index", bundle.Index,
"current codec version", bundle.CodecVersion,
"prev batch index", prevBatch.Index,
"prev codec version", prevBatch.CodecVersion)
return errors.New("current bundle codec version cannot be less than the preceding batch")
}
}

// Check batch status before sending `finalizeBundle` tx.
if r.cfg.ChainMonitor.Enabled {
for batchIndex := bundle.StartBatchIndex; batchIndex <= bundle.EndBatchIndex; batchIndex++ {
tmpBatch, getErr := r.batchOrm.GetBatchByIndex(r.ctx, batchIndex)
if getErr != nil {
log.Error("failed to get batch by index", "batch index", batchIndex, "error", getErr)
return getErr
}
for batchIndex := bundle.StartBatchIndex; batchIndex <= bundle.EndBatchIndex; batchIndex++ {
tmpBatch, getErr := r.batchOrm.GetBatchByIndex(r.ctx, batchIndex)
if getErr != nil {
log.Error("failed to get batch by index", "batch index", batchIndex, "error", getErr)
return getErr
}

// check codec version
if tmpBatch.CodecVersion != bundle.CodecVersion {
log.Error("bundle codec version is different from batch codec version", "bundle index", bundle.Index, "batch index", tmpBatch.Index, "bundle codec version", bundle.CodecVersion, "batch codec version", tmpBatch.CodecVersion)
return errors.New("bundle codec version is different from batch codec version")
}

if r.cfg.ChainMonitor.Enabled {
batchStatus, getErr := r.getBatchStatusByIndex(tmpBatch)
if getErr != nil {
r.metrics.rollupL2ChainMonitorLatestFailedCall.Inc()
Expand Down

0 comments on commit fc33061

Please sign in to comment.