Skip to content

Commit

Permalink
fix AtLeastAsSafe
Browse files Browse the repository at this point in the history
  • Loading branch information
axelKingsley committed Nov 6, 2024
1 parent 34d8f1f commit bef390a
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions op-supervisor/supervisor/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,27 @@ func (lvl *SafetyLevel) UnmarshalText(text []byte) error {
}

// AtLeastAsSafe returns true if the receiver is at least as safe as the other SafetyLevel.
// Safety levels are assumed to graduate from LocalUnsafe to LocalSafe to CrossUnsafe to CrossSafe, with Finalized as the strongest.
func (lvl *SafetyLevel) AtLeastAsSafe(min SafetyLevel) bool {
switch min {
case Invalid:
return true
case CrossUnsafe:
return *lvl != Invalid
case CrossSafe:
return *lvl == CrossSafe || *lvl == Finalized
case Finalized:
return *lvl == Finalized
default:
relativeSafety := map[SafetyLevel]int{
Invalid: 0,
LocalUnsafe: 1,
LocalSafe: 2,
CrossUnsafe: 3,
CrossSafe: 4,
Finalized: 5,
}
// if either level is not recognized, return false
_, ok := relativeSafety[*lvl]
if !ok {
return false
}
_, ok = relativeSafety[min]
if !ok {
return false
}
// compare the relative safety levels to determine if the receiver is at least as safe as the other
return relativeSafety[*lvl] >= relativeSafety[min]
}

const (
Expand Down

0 comments on commit bef390a

Please sign in to comment.