Skip to content

Commit

Permalink
Make AtLeastAsSafeAs a function of SafetyLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
axelKingsley committed Aug 27, 2024
1 parent 0f04e4c commit 78db120
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
28 changes: 5 additions & 23 deletions op-supervisor/supervisor/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,34 +171,16 @@ func (su *SupervisorBackend) CheckMessages(
if err != nil {
return fmt.Errorf("failed to check message: %w", err)
}
if !atLeastAsSafe(safety, minSafety) {
return fmt.Errorf("message %v is not safe: %v", identifier, safety)
if !safety.AtLeastAsSafe(minSafety) {
return fmt.Errorf("message %v (safety level: %v) does not meet the minimum safety %v",
identifier,
safety,
minSafety)
}
}
return nil
}

// TODO: Really this should be a method on types.SafetyLevel
// for now it's easy to do this calculation here
func atLeastAsSafe(safety, minSafety types.SafetyLevel) bool {
switch minSafety {
// If for some reason the minimum safety level is invalid, then nothing is unsafe by comparison
case types.Invalid:
return true
// Unsafe is the lowest safety level, so it is safe so long as it isn't invalid
case types.Unsafe:
return safety != types.Invalid
// Safe is satisfied by Safe or Finalized
case types.Safe:
return safety == types.Safe || safety == types.Finalized
// Finalized is only satisfied by Finalized
case types.Finalized:
return safety == types.Finalized
default:
return false
}
}

// CheckBlock checks if the block is safe according to the safety level
// The block is considered safe if all logs in the block are safe
// this is decided by finding the last log in the block and
Expand Down
16 changes: 16 additions & 0 deletions op-supervisor/supervisor/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ func (lvl *SafetyLevel) UnmarshalText(text []byte) error {
return nil
}

// AtLeastAsSafe returns true if the receiver is at least as safe as the other SafetyLevel.
func (lvl *SafetyLevel) AtLeastAsSafe(other SafetyLevel) bool {
switch *lvl {
case Invalid:
return true
case Unsafe:
return other != Invalid
case Safe:
return other == Safe || other == Finalized
case Finalized:
return other == Finalized
default:
return false
}
}

const (
CrossFinalized SafetyLevel = "cross-finalized"
Finalized SafetyLevel = "finalized"
Expand Down

0 comments on commit 78db120

Please sign in to comment.