From 4cfc5511fb709dd6d70111b04676cef77698dda2 Mon Sep 17 00:00:00 2001 From: Bin <49082129+songzhibin97@users.noreply.github.com> Date: Thu, 12 Dec 2024 14:15:32 +0800 Subject: [PATCH] fix: Using the Recommended Error Determination (#1546) Co-authored-by: 0xmountaintop <37070449+0xmountaintop@users.noreply.github.com> Co-authored-by: georgehao --- bridge-history-api/internal/orm/batch_event.go | 7 ++++--- bridge-history-api/internal/orm/cross_message.go | 9 +++++---- coordinator/internal/orm/prover_block_list.go | 3 ++- coordinator/internal/orm/prover_task.go | 3 ++- rollup/internal/orm/bundle.go | 2 +- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/bridge-history-api/internal/orm/batch_event.go b/bridge-history-api/internal/orm/batch_event.go index 6812dc3baf..1b3bea3bbd 100644 --- a/bridge-history-api/internal/orm/batch_event.go +++ b/bridge-history-api/internal/orm/batch_event.go @@ -2,6 +2,7 @@ package orm import ( "context" + "errors" "fmt" "time" @@ -45,7 +46,7 @@ func (c *BatchEvent) GetBatchEventSyncedHeightInDB(ctx context.Context) (uint64, db = db.Model(&BatchEvent{}) db = db.Order("l1_block_number desc") if err := db.First(&batch).Error; err != nil { - if err == gorm.ErrRecordNotFound { + if errors.Is(err, gorm.ErrRecordNotFound) { return 0, nil } return 0, fmt.Errorf("failed to get batch synced height in db, error: %w", err) @@ -62,7 +63,7 @@ func (c *BatchEvent) GetLastUpdatedFinalizedBlockHeight(ctx context.Context) (ui db = db.Where("update_status = ?", btypes.UpdateStatusTypeUpdated) db = db.Order("batch_index desc") if err := db.First(&batch).Error; err != nil { - if err == gorm.ErrRecordNotFound { + if errors.Is(err, gorm.ErrRecordNotFound) { // No finalized batch found, return genesis batch's end block number. return 0, nil } @@ -81,7 +82,7 @@ func (c *BatchEvent) GetUnupdatedFinalizedBatchesLEBlockHeight(ctx context.Conte db = db.Where("update_status = ?", btypes.UpdateStatusTypeUnupdated) db = db.Order("batch_index asc") if err := db.Find(&batches).Error; err != nil { - if err == gorm.ErrRecordNotFound { + if errors.Is(err, gorm.ErrRecordNotFound) { return nil, nil } return nil, fmt.Errorf("failed to get unupdated finalized batches >= block height, error: %w", err) diff --git a/bridge-history-api/internal/orm/cross_message.go b/bridge-history-api/internal/orm/cross_message.go index b06253aafd..936f79ef46 100644 --- a/bridge-history-api/internal/orm/cross_message.go +++ b/bridge-history-api/internal/orm/cross_message.go @@ -2,6 +2,7 @@ package orm import ( "context" + "errors" "fmt" "time" @@ -84,7 +85,7 @@ func (c *CrossMessage) GetMessageSyncedHeightInDB(ctx context.Context, messageTy db = db.Order("l2_block_number desc") } if err := db.First(&message).Error; err != nil { - if err == gorm.ErrRecordNotFound { + if errors.Is(err, gorm.ErrRecordNotFound) { return 0, nil } return 0, fmt.Errorf("failed to get latest processed height, type: %v, error: %w", messageType, err) @@ -108,7 +109,7 @@ func (c *CrossMessage) GetL2LatestFinalizedWithdrawal(ctx context.Context) (*Cro db = db.Where("rollup_status = ?", btypes.RollupStatusTypeFinalized) db = db.Order("message_nonce desc") if err := db.First(&message).Error; err != nil { - if err == gorm.ErrRecordNotFound { + if errors.Is(err, gorm.ErrRecordNotFound) { return nil, nil } return nil, fmt.Errorf("failed to get latest L2 finalized sent message event, error: %w", err) @@ -127,10 +128,10 @@ func (c *CrossMessage) GetL2WithdrawalsByBlockRange(ctx context.Context, startBl db = db.Where("message_type = ?", btypes.MessageTypeL2SentMessage) db = db.Order("message_nonce asc") if err := db.Find(&messages).Error; err != nil { - if err == gorm.ErrRecordNotFound { + if errors.Is(err, gorm.ErrRecordNotFound) { return nil, nil } - return nil, fmt.Errorf("failed to get latest L2 finalized sent message event, error: %w", err) + return nil, fmt.Errorf("failed to get L2 withdrawals by block range, error: %v", err) } return messages, nil } diff --git a/coordinator/internal/orm/prover_block_list.go b/coordinator/internal/orm/prover_block_list.go index 0a6d82caec..8503beb519 100644 --- a/coordinator/internal/orm/prover_block_list.go +++ b/coordinator/internal/orm/prover_block_list.go @@ -2,6 +2,7 @@ package orm import ( "context" + "errors" "fmt" "time" @@ -65,7 +66,7 @@ func (p *ProverBlockList) IsPublicKeyBlocked(ctx context.Context, publicKey stri db = db.Model(&ProverBlockList{}) db = db.Where("public_key = ?", publicKey) if err := db.First(&ProverBlockList{}).Error; err != nil { - if err == gorm.ErrRecordNotFound { + if errors.Is(err, gorm.ErrRecordNotFound) { return false, nil // Public key not found, hence it's not blocked. } return true, fmt.Errorf("ProverBlockList.IsPublicKeyBlocked error: %w, public key: %v", err, publicKey) diff --git a/coordinator/internal/orm/prover_task.go b/coordinator/internal/orm/prover_task.go index ace18e04ef..00d8b36167 100644 --- a/coordinator/internal/orm/prover_task.go +++ b/coordinator/internal/orm/prover_task.go @@ -2,6 +2,7 @@ package orm import ( "context" + "errors" "fmt" "time" @@ -60,7 +61,7 @@ func (o *ProverTask) IsProverAssigned(ctx context.Context, publicKey string) (bo var task ProverTask err := db.Where("prover_public_key = ? AND proving_status = ?", publicKey, types.ProverAssigned).First(&task).Error if err != nil { - if err == gorm.ErrRecordNotFound { + if errors.Is(err, gorm.ErrRecordNotFound) { return false, nil } return false, err diff --git a/rollup/internal/orm/bundle.go b/rollup/internal/orm/bundle.go index 3d35be29db..80825dde23 100644 --- a/rollup/internal/orm/bundle.go +++ b/rollup/internal/orm/bundle.go @@ -67,7 +67,7 @@ func (o *Bundle) getLatestBundle(ctx context.Context) (*Bundle, error) { var latestBundle Bundle if err := db.First(&latestBundle).Error; err != nil { - if err == gorm.ErrRecordNotFound { + if errors.Is(err, gorm.ErrRecordNotFound) { return nil, nil } return nil, fmt.Errorf("getLatestBundle error: %w", err)