From 8471838cd4132ecd11eac41869dce85b0fb171ca Mon Sep 17 00:00:00 2001 From: colin <102356659+colinlyguo@users.noreply.github.com> Date: Wed, 10 Jul 2024 22:56:16 +0800 Subject: [PATCH] fix(rollup-relayer): catch errors (#1427) --- common/version/version.go | 2 +- .../controller/watcher/batch_proposer.go | 3 +- .../controller/watcher/chunk_proposer.go | 3 +- rollup/internal/utils/utils.go | 6 ++- rollup/internal/utils/utils_test.go | 53 +++++++++++++++++++ 5 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 rollup/internal/utils/utils_test.go diff --git a/common/version/version.go b/common/version/version.go index 1f8e91e73a..0d14768470 100644 --- a/common/version/version.go +++ b/common/version/version.go @@ -5,7 +5,7 @@ import ( "runtime/debug" ) -var tag = "v4.4.26" +var tag = "v4.4.27" var commit = func() string { if info, ok := debug.ReadBuildInfo(); ok { diff --git a/rollup/internal/controller/watcher/batch_proposer.go b/rollup/internal/controller/watcher/batch_proposer.go index b479988c29..ffa349a496 100644 --- a/rollup/internal/controller/watcher/batch_proposer.go +++ b/rollup/internal/controller/watcher/batch_proposer.go @@ -240,7 +240,8 @@ func (p *BatchProposer) proposeBatch() error { batch.Chunks = append(batch.Chunks, chunk) metrics, calcErr := utils.CalculateBatchMetrics(&batch, codecVersion) - if errors.Is(calcErr, &encoding.CompressedDataCompatibilityError{}) { + var compressErr *encoding.CompressedDataCompatibilityError + if errors.As(calcErr, &compressErr) { if i == 0 { // The first chunk fails compressed data compatibility check, manual fix is needed. return fmt.Errorf("the first chunk fails compressed data compatibility check; start block number: %v, end block number: %v", dbChunks[0].StartBlockNumber, dbChunks[0].EndBlockNumber) diff --git a/rollup/internal/controller/watcher/chunk_proposer.go b/rollup/internal/controller/watcher/chunk_proposer.go index 041695614a..4354384504 100644 --- a/rollup/internal/controller/watcher/chunk_proposer.go +++ b/rollup/internal/controller/watcher/chunk_proposer.go @@ -242,7 +242,8 @@ func (p *ChunkProposer) proposeChunk() error { chunk.Blocks = append(chunk.Blocks, block) metrics, calcErr := utils.CalculateChunkMetrics(&chunk, codecVersion) - if errors.Is(calcErr, &encoding.CompressedDataCompatibilityError{}) { + var compressErr *encoding.CompressedDataCompatibilityError + if errors.As(calcErr, &compressErr) { if i == 0 { // The first block fails compressed data compatibility check, manual fix is needed. return fmt.Errorf("the first block fails compressed data compatibility check; block number: %v", block.Header.Number) diff --git a/rollup/internal/utils/utils.go b/rollup/internal/utils/utils.go index d230252734..8161623b0b 100644 --- a/rollup/internal/utils/utils.go +++ b/rollup/internal/utils/utils.go @@ -92,7 +92,8 @@ func CalculateChunkMetrics(chunk *encoding.Chunk, codecVersion encoding.CodecVer metrics.L1CommitUncompressedBatchBytesSize, metrics.L1CommitBlobSize, err = codecv2.EstimateChunkL1CommitBatchSizeAndBlobSize(chunk) metrics.EstimateBlobSizeTime = time.Since(start) if err != nil { - if errors.Is(err, &encoding.CompressedDataCompatibilityError{}) { + var compressErr *encoding.CompressedDataCompatibilityError + if errors.As(err, &compressErr) { return nil, err } else { return nil, fmt.Errorf("failed to estimate codecv2 chunk L1 commit batch size and blob size: %w", err) @@ -176,7 +177,8 @@ func CalculateBatchMetrics(batch *encoding.Batch, codecVersion encoding.CodecVer metrics.L1CommitUncompressedBatchBytesSize, metrics.L1CommitBlobSize, err = codecv2.EstimateBatchL1CommitBatchSizeAndBlobSize(batch) metrics.EstimateBlobSizeTime = time.Since(start) if err != nil { - if errors.Is(err, &encoding.CompressedDataCompatibilityError{}) { + var compressErr *encoding.CompressedDataCompatibilityError + if errors.As(err, &compressErr) { return nil, err } else { return nil, fmt.Errorf("failed to estimate codecv2 batch L1 commit batch size and blob size: %w", err) diff --git a/rollup/internal/utils/utils_test.go b/rollup/internal/utils/utils_test.go new file mode 100644 index 0000000000..4b3efeb30a --- /dev/null +++ b/rollup/internal/utils/utils_test.go @@ -0,0 +1,53 @@ +package utils + +import ( + "errors" + "math/big" + "testing" + + "github.com/agiledragon/gomonkey/v2" + "github.com/scroll-tech/da-codec/encoding" + "github.com/scroll-tech/da-codec/encoding/codecv2" + "github.com/scroll-tech/go-ethereum/common" + "github.com/scroll-tech/go-ethereum/core/types" + "github.com/stretchr/testify/assert" +) + +// regression test +func TestCompressedDataCompatibilityErrorCatching(t *testing.T) { + block := &encoding.Block{ + Header: &types.Header{ + Number: big.NewInt(0), + }, + RowConsumption: &types.RowConsumption{}, + } + chunk := &encoding.Chunk{ + Blocks: []*encoding.Block{block}, + } + batch := &encoding.Batch{ + Index: 0, + TotalL1MessagePoppedBefore: 0, + ParentBatchHash: common.Hash{}, + Chunks: []*encoding.Chunk{chunk}, + } + + patchGuard1 := gomonkey.ApplyFunc(codecv2.EstimateChunkL1CommitBatchSizeAndBlobSize, func(b *encoding.Chunk) (uint64, uint64, error) { + return 0, 0, &encoding.CompressedDataCompatibilityError{Err: errors.New("test-error-1")} + }) + defer patchGuard1.Reset() + + var compressErr *encoding.CompressedDataCompatibilityError + + _, err := CalculateChunkMetrics(chunk, encoding.CodecV2) + assert.Error(t, err) + assert.ErrorAs(t, err, &compressErr) + + patchGuard2 := gomonkey.ApplyFunc(codecv2.EstimateBatchL1CommitBatchSizeAndBlobSize, func(b *encoding.Batch) (uint64, uint64, error) { + return 0, 0, &encoding.CompressedDataCompatibilityError{Err: errors.New("test-error-2")} + }) + defer patchGuard2.Reset() + + _, err = CalculateBatchMetrics(batch, encoding.CodecV2) + assert.Error(t, err) + assert.ErrorAs(t, err, &compressErr) +}