From f28ffd4dddcdae430cb54ad40c6564a652e98552 Mon Sep 17 00:00:00 2001 From: georgehao Date: Thu, 7 Dec 2023 23:34:48 +0800 Subject: [PATCH 1/2] feat: change call chain_monitor api --- .../internal/controller/relayer/l2_relayer.go | 30 +++++++++++++++++-- .../controller/relayer/l2_relayer_test.go | 20 ++++++++++++- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/rollup/internal/controller/relayer/l2_relayer.go b/rollup/internal/controller/relayer/l2_relayer.go index ca10f6ba89..fdcf637926 100644 --- a/rollup/internal/controller/relayer/l2_relayer.go +++ b/rollup/internal/controller/relayer/l2_relayer.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "math/big" + "sort" "sync" "time" @@ -488,7 +489,7 @@ func (r *Layer2Relayer) finalizeBatch(batch *orm.Batch, withProof bool) error { // Check batch status before send `finalizeBatch` tx. if r.cfg.ChainMonitor.Enabled { var batchStatus bool - batchStatus, err := r.getBatchStatusByIndex(batch.Index) + batchStatus, err := r.getBatchStatusByIndex(batch) if err != nil { r.metrics.rollupL2ChainMonitorLatestFailedCall.Inc() log.Warn("failed to get batch status, please check chain_monitor api server", "batch_index", batch.Index, "err", err) @@ -602,9 +603,32 @@ type batchStatusResponse struct { Data bool `json:"data"` } -func (r *Layer2Relayer) getBatchStatusByIndex(batchIndex uint64) (bool, error) { +func (r *Layer2Relayer) getBatchStatusByIndex(batch *orm.Batch) (bool, error) { + chunks, getChunkErr := r.chunkOrm.GetChunksInRange(r.ctx, batch.StartChunkIndex, batch.EndChunkIndex) + if getChunkErr != nil { + log.Error("Layer2Relayer.getBatchStatusByIndex get chunks range failed", "startChunkIndex", batch.StartChunkIndex, "endChunkIndex", batch.EndChunkIndex, "err", getChunkErr) + return false, getChunkErr + } + if len(chunks) == 0 { + log.Error("Layer2Relayer.getBatchStatusByIndex get empty chunks", "startChunkIndex", batch.StartChunkIndex, "endChunkIndex", batch.EndChunkIndex) + return false, fmt.Errorf("startChunksIndex:%d endChunkIndex:%d get empty chunks", batch.StartChunkIndex, batch.EndChunkIndex) + } + + sort.Slice(chunks, func(i, j int) bool { + return chunks[i].StartBlockNumber < chunks[j].StartBlockNumber + }) + + startBlockNum := chunks[0].StartBlockNumber + endBlockNum := chunks[len(chunks)-1].EndBlockNumber var response batchStatusResponse - resp, err := r.chainMonitorClient.R().SetResult(&response).Get(fmt.Sprintf("%s/v1/batch_status?batch_index=%d", r.cfg.ChainMonitor.BaseURL, batchIndex)) + resp, err := r.chainMonitorClient.R(). + SetQueryParams(map[string]string{ + "batch_index": fmt.Sprintf("%d", batch.Index), + "start_block_number": fmt.Sprintf("%d", startBlockNum), + "end_block_number": fmt.Sprintf("%d", endBlockNum), + }). + SetResult(&response). + Get(fmt.Sprintf("%s/v1/batch_status", r.cfg.ChainMonitor.BaseURL)) if err != nil { return false, err } diff --git a/rollup/internal/controller/relayer/l2_relayer_test.go b/rollup/internal/controller/relayer/l2_relayer_test.go index 5b46be7946..6bd81aa648 100644 --- a/rollup/internal/controller/relayer/l2_relayer_test.go +++ b/rollup/internal/controller/relayer/l2_relayer_test.go @@ -423,12 +423,30 @@ func testGetBatchStatusByIndex(t *testing.T) { db := setupL2RelayerDB(t) defer database.CloseDB(db) + l2BlockOrm := orm.NewL2Block(db) + err := l2BlockOrm.InsertL2Blocks(context.Background(), []*types.WrappedBlock{wrappedBlock1, wrappedBlock2}) + assert.NoError(t, err) + chunkOrm := orm.NewChunk(db) + dbChunk1, err := chunkOrm.InsertChunk(context.Background(), chunk1) + assert.NoError(t, err) + dbChunk2, err := chunkOrm.InsertChunk(context.Background(), chunk2) + assert.NoError(t, err) + batchMeta := &types.BatchMeta{ + StartChunkIndex: 0, + StartChunkHash: dbChunk1.Hash, + EndChunkIndex: 1, + EndChunkHash: dbChunk2.Hash, + } + batchOrm := orm.NewBatch(db) + batch, err := batchOrm.InsertBatch(context.Background(), []*types.Chunk{chunk1, chunk2}, batchMeta) + assert.NoError(t, err) + cfg.L2Config.RelayerConfig.ChainMonitor.Enabled = true relayer, err := NewLayer2Relayer(context.Background(), l2Cli, db, cfg.L2Config.RelayerConfig, false, nil) assert.NoError(t, err) assert.NotNil(t, relayer) - status, err := relayer.getBatchStatusByIndex(1) + status, err := relayer.getBatchStatusByIndex(batch) assert.NoError(t, err) assert.Equal(t, true, status) } From dbe1e3601a75cc3e514f329ec44eaf773f54f4a3 Mon Sep 17 00:00:00 2001 From: georgehao Date: Thu, 7 Dec 2023 15:40:22 +0000 Subject: [PATCH 2/2] =?UTF-8?q?chore:=20auto=20version=20bump=E2=80=89[bot?= =?UTF-8?q?]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/version/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/version/version.go b/common/version/version.go index 5eb2f54af4..ca6aadc483 100644 --- a/common/version/version.go +++ b/common/version/version.go @@ -5,7 +5,7 @@ import ( "runtime/debug" ) -var tag = "v4.3.41" +var tag = "v4.3.42" var commit = func() string { if info, ok := debug.ReadBuildInfo(); ok {