Skip to content

Commit

Permalink
paranoid underflow protection without error handling (#14044)
Browse files Browse the repository at this point in the history
Co-authored-by: Kasey Kirkham <kasey@users.noreply.github.com>
  • Loading branch information
kasey and kasey authored May 23, 2024
1 parent b04baa9 commit 62b5c43
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion beacon-chain/sync/initial-sync/blocks_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ func (r *blobRange) Request() *p2ppb.BlobSidecarsByRangeRequest {
}
return &p2ppb.BlobSidecarsByRangeRequest{
StartSlot: r.low,
Count: uint64(r.high.SubSlot(r.low)) + 1,
Count: uint64(r.high.FlooredSubSlot(r.low)) + 1,
}
}

Expand Down
8 changes: 8 additions & 0 deletions consensus-types/primitives/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 62b5c43

Please sign in to comment.