Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
51 changes: 44 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,38 @@ 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.Index > 0 {
prevBundle, err := r.bundleOrm.GetBundleByIndex(r.ctx, bundle.Index-1)
if err != nil {
log.Error("failed to get previous bundle", "current bundle index", bundle.Index, "error", err)
return err
}
if bundle.CodecVersion < prevBundle.CodecVersion {
log.Error("current bundle codec version is less than the preceding one",
"current bundle index", bundle.Index,
"current codec version", bundle.CodecVersion,
"prev bundle index", prevBundle.Index,
"prev codec version", prevBundle.CodecVersion)
return errors.New("current bundle codec version cannot be less than the preceding one")
}
}

// 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++ {
Comment thread
georgehao marked this conversation as resolved.
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
16 changes: 16 additions & 0 deletions rollup/internal/orm/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ func (o *Bundle) getLatestBundle(ctx context.Context) (*Bundle, error) {
return &latestBundle, nil
}

// GetBundleByIndex retrieves a bundle by its index from the database.
func (o *Bundle) GetBundleByIndex(ctx context.Context, index uint64) (*Bundle, error) {
db := o.db.WithContext(ctx)
db = db.Model(&Bundle{})
db = db.Where("index = ?", index)

var bundle Bundle
if err := db.First(&bundle).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return nil, fmt.Errorf("bundle not found with index: %d", index)
}
return nil, fmt.Errorf("failed to get bundle by index %d: %w", index, err)
}
return &bundle, nil
}

// GetBundles retrieves selected bundles from the database.
// The returned bundles are sorted in ascending order by their index.
// only used in unit tests.
Expand Down