Skip to content

Commit

Permalink
fix: add divide by zero check (#4287)
Browse files Browse the repository at this point in the history
* fix: add saturating sub to prevent potential underflow

* fix: prevent div by zero and add wrapping add

Co-authored-by: aviator-app[bot] <48659329+aviator-app[bot]@users.noreply.github.com>
  • Loading branch information
stringhandler and aviator-app[bot] committed Jul 13, 2022
1 parent e8d4a00 commit 75a8f59
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions dan_layer/core/src/models/view_id.rs
Expand Up @@ -33,15 +33,19 @@ impl ViewId {
pub fn current_leader(&self, committee_size: usize) -> usize {
#[allow(clippy::cast_possible_truncation)]
let view_id = self.0 as usize;
view_id % committee_size
if committee_size == 0 {
0
} else {
view_id % committee_size
}
}

pub fn is_genesis(&self) -> bool {
self.0 == 0
}

pub fn next(&self) -> ViewId {
ViewId(self.0 + 1)
ViewId(self.0.wrapping_add(1))
}

pub fn as_u64(&self) -> u64 {
Expand Down

0 comments on commit 75a8f59

Please sign in to comment.