Skip to content

Commit

Permalink
feat: change call chain_monitor api (#1026)
Browse files Browse the repository at this point in the history
Co-authored-by: georgehao <[email protected]>
  • Loading branch information
georgehao and georgehao authored Dec 8, 2023
1 parent 144c7ed commit 99454e5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
2 changes: 1 addition & 1 deletion common/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 27 additions & 3 deletions rollup/internal/controller/relayer/l2_relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math/big"
"sort"
"sync"
"time"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
20 changes: 19 additions & 1 deletion rollup/internal/controller/relayer/l2_relayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 99454e5

Please sign in to comment.