Skip to content

Commit

Permalink
Merge pull request #8 from bnb-chain/feat-handle-tx-errors
Browse files Browse the repository at this point in the history
Feat handle tx errors
  • Loading branch information
bendanzhentan authored Nov 24, 2023
2 parents 92a81f6 + 2255cdc commit b97601d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
28 changes: 20 additions & 8 deletions cmd/bot/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func ProcessUnprovenBotDelegatedWithdrawals(ctx context.Context, log log.Logger,
maxBlockTime := time.Now().Unix() - cfg.Misc.ProposeTimeWindow

unprovens := make([]core.L2ContractEvent, 0)
result := db.Order("id asc").Where("proven = false AND block_time < ?", maxBlockTime).Limit(limit).Find(&unprovens)
result := db.Order("id asc").Where("proven = false AND block_time < ? AND failure_reason IS NULL", maxBlockTime).Limit(limit).Find(&unprovens)
if result.Error != nil {
log.Error("failed to query l2_contract_events", "error", result.Error)
return
Expand All @@ -107,15 +107,21 @@ func ProcessUnprovenBotDelegatedWithdrawals(ctx context.Context, log log.Logger,
result := db.Model(&unproven).Update("proven", true)
if result.Error != nil {
log.Error("failed to update proven l2_contract_events", "error", result.Error)
return
}
} else if strings.Contains(err.Error(), "L2OutputOracle: cannot get output for a block that has not been proposed") {
// Since the unproven withdrawals are sorted by the on-chain order, we can break here because we know
// that the subsequent of the withdrawals are not ready to be proven yet.
return
} else if strings.Contains(err.Error(), "execution reverted") {
// Proven transaction reverted, mark it with the failure reason
result := db.Model(&unproven).Update("failure_reason", err.Error())
if result.Error != nil {
log.Error("failed to update failure reason of l2_contract_events", "error", result.Error)
}
} else {
// TODO handle other errors
log.Error("FinalizeMessage", "error", err.Error())
// non-revert error, stop processing the subsequent withdrawals
log.Error("ProveWithdrawalTransaction", "non-revert error", err.Error())
return
}
}
}
Expand All @@ -127,7 +133,7 @@ func ProcessUnfinalizedBotDelegatedWithdrawals(ctx context.Context, log log.Logg
maxBlockTime := time.Now().Unix() - cfg.Misc.ChallengeTimeWindow

unfinalizeds := make([]core.L2ContractEvent, 0)
result := db.Order("block_time asc").Where("proven = true AND finalized = false AND block_time < ?", maxBlockTime).Limit(limit).Find(&unfinalizeds)
result := db.Order("block_time asc").Where("proven = true AND finalized = false AND block_time < ? AND failure_reason IS NULL", maxBlockTime).Limit(limit).Find(&unfinalizeds)
if result.Error != nil {
log.Error("failed to query l2_contract_events", "error", result.Error)
return
Expand All @@ -141,17 +147,23 @@ func ProcessUnfinalizedBotDelegatedWithdrawals(ctx context.Context, log log.Logg
result := db.Model(&unfinalized).Update("finalized", true)
if result.Error != nil {
log.Error("failed to update finalized l2_contract_events", "error", result.Error)
return
}
} else if strings.Contains(err.Error(), "OptimismPortal: withdrawal has not been proven yet") {
log.Error("detected a unproven withdrawal when send finalized transaction", "withdrawal", unfinalized)
continue
} else if strings.Contains(err.Error(), "OptimismPortal: proven withdrawal finalization period has not elapsed") {
// Continue to handle the subsequent unfinalized withdrawals
continue
} else if strings.Contains(err.Error(), "execution reverted") {
// Finalized transaction reverted, mark it with the failure reason
result := db.Model(&unfinalized).Update("failure_reason", err.Error())
if result.Error != nil {
log.Error("failed to update failure reason of l2_contract_events", "error", result.Error)
}
} else {
// TODO handle other errors
log.Error("FinalizeMessage", "error", err.Error())
// non-revert error, stop processing the subsequent withdrawals
log.Error("FinalizedMessage", "non-revert error", err.Error())
return
}
}
}
Expand Down
1 change: 1 addition & 0 deletions core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ type L2ContractEvent struct {
EventSignature string `gorm:"type:varchar(256);not null"`
Proven bool `gorm:"type:boolean;not null;default:false"`
Finalized bool `gorm:"type:boolean;not null;default:false"`
FailureReason string `gorm:"type:text"`
}

0 comments on commit b97601d

Please sign in to comment.