Skip to content

Commit

Permalink
chore: introduce saturating sub usage at slot.go
Browse files Browse the repository at this point in the history
  • Loading branch information
EclesioMeloJunior committed Oct 5, 2023
1 parent 7d9d328 commit 31c3832
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions dot/state/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/database"
"github.com/ChainSafe/gossamer/lib/primitives"
"github.com/ChainSafe/gossamer/pkg/scale"
)

Expand Down Expand Up @@ -48,7 +49,7 @@ func (s *SlotState) CheckEquivocation(slotNow, slot uint64, header *types.Header
signer types.AuthorityID) (*types.BabeEquivocationProof, error) {
// We don't check equivocations for old headers out of our capacity.
// checking slotNow is greater than slot to avoid overflow, same as saturating_sub
if saturatingSub(slotNow, slot) > maxSlotCapacity {
if primitives.SaturatingSub(slotNow, slot) > maxSlotCapacity {
return nil, nil
}

Expand Down Expand Up @@ -127,7 +128,7 @@ func (s *SlotState) CheckEquivocation(slotNow, slot uint64, header *types.Header
newFirstSavedSlot := firstSavedSlot

if slotNow-firstSavedSlot >= pruningBound {
newFirstSavedSlot = saturatingSub(slotNow, maxSlotCapacity)
newFirstSavedSlot = primitives.SaturatingSub(slotNow, maxSlotCapacity)

for s := firstSavedSlot; s < newFirstSavedSlot; s++ {
slotEncoded := make([]byte, 8)
Expand Down Expand Up @@ -184,10 +185,3 @@ func (s *SlotState) CheckEquivocation(slotNow, slot uint64, header *types.Header

return nil, nil
}

func saturatingSub(a, b uint64) uint64 {
if a > b {
return a - b
}
return 0
}

0 comments on commit 31c3832

Please sign in to comment.