paranoid underflow protection without error handling (#14044)

Co-authored-by: Kasey Kirkham <kasey@users.noreply.github.com>
This commit is contained in:
kasey
2024-05-23 12:57:53 -05:00
committed by GitHub
parent b04baa93cd
commit 62b5c43d87
2 changed files with 9 additions and 1 deletions

View File

@@ -124,6 +124,14 @@ func (s Slot) SubSlot(x Slot) Slot {
return s.Sub(uint64(x))
}
// FlooredSubSlot safely subtracts x from the slot, returning 0 if the result would underflow.
func (s Slot) FlooredSubSlot(x Slot) Slot {
if s < x {
return 0
}
return s - x
}
// SafeSubSlot finds difference between two slot values.
// In case of arithmetic issues (overflow/underflow/div by zero) error is returned.
func (s Slot) SafeSubSlot(x Slot) (Slot, error) {