From e7ee85ecf8685d8b6f3ba1be3d7e09f25c4e3815 Mon Sep 17 00:00:00 2001 From: colinlyguo Date: Tue, 9 Jul 2024 01:42:26 +0800 Subject: [PATCH] add corner cases handling --- bridge-history-api/internal/logic/event_update.go | 10 ++++++++++ bridge-history-api/internal/orm/batch_event.go | 1 + 2 files changed, 11 insertions(+) diff --git a/bridge-history-api/internal/logic/event_update.go b/bridge-history-api/internal/logic/event_update.go index edf6db1d25..7b6f90e869 100644 --- a/bridge-history-api/internal/logic/event_update.go +++ b/bridge-history-api/internal/logic/event_update.go @@ -187,6 +187,16 @@ func (b *EventUpdateLogic) UpdateL2WithdrawMessageProofs(ctx context.Context, he return err } + if len(finalizedBatches) == 0 { + log.Warn("no finalized batches to update", "lastUpdatedFinalizedBlockHeight", lastUpdatedFinalizedBlockHeight) + return nil + } + + if finalizedBatches[0].EndBlockNumber <= lastUpdatedFinalizedBlockHeight { + log.Warn("no finalized batches to update", "lastUpdatedFinalizedBlockHeight", lastUpdatedFinalizedBlockHeight, "endBlockNumber", finalizedBatches[0].EndBlockNumber) + return nil + } + for _, finalizedBatch := range finalizedBatches { log.Info("update finalized batch info of L2 withdrawals", "index", finalizedBatch.BatchIndex, "lastlockHeight", lastUpdatedFinalizedBlockHeight, "start", finalizedBatch.StartBlockNumber, "end", finalizedBatch.EndBlockNumber) // This method is compatible with both "finalize by batch" and "finalize by bundle" modes: diff --git a/bridge-history-api/internal/orm/batch_event.go b/bridge-history-api/internal/orm/batch_event.go index 04045d8706..a876e65835 100644 --- a/bridge-history-api/internal/orm/batch_event.go +++ b/bridge-history-api/internal/orm/batch_event.go @@ -63,6 +63,7 @@ func (c *BatchEvent) GetUpdatedFinalizedBlockHeight(ctx context.Context) (uint64 db = db.Order("batch_index desc") if err := db.First(&batch).Error; err != nil { if err == gorm.ErrRecordNotFound { + // No finalized batch found, return genesis batch's end block number. return 0, nil } return 0, fmt.Errorf("failed to get latest processed finalized batch, error: %w", err)