Skip to content

Commit

Permalink
Prefer None over panic in VoterSet construction.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman S. Borschel committed Nov 25, 2019
1 parent b28a693 commit 8be57e4
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/voter_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,9 @@ impl<Id: Hash + Eq> VoterSet<Id> {
/// understood to be partial weights and are accumulated. As a result, the
/// order in which the iterator produces the weights is irrelevant.
///
/// Returns `None` if the iterator produced no non-zero weights, i.e.
/// the voter set would be empty.
///
/// # Panics
///
/// If the total voter weight exceeds `u64::MAX`.
///
/// Returns `None` if the iterator does not yield a valid voter set, which is
/// the case if it either produced no non-zero weights or, i.e. the voter set
/// would be empty, or if the total voter weight exceeds `u64::MAX`.
pub fn new<I>(weights: I) -> Option<Self>
where
Id: Ord + Clone,
Expand All @@ -71,7 +67,7 @@ impl<Id: Hash + Eq> VoterSet<Id> {
// Prevent construction of inconsistent voter sets by checking
// for weight overflow (not just in debug mode). The protocol
// should never run with such voter sets.
total_weight = total_weight.checked_add(weight).expect("Voter weight overflow");
total_weight = total_weight.checked_add(weight)?;
match voters.entry(id) {
Entry::Vacant(e) => {
e.insert(VoterInfo {
Expand Down

0 comments on commit 8be57e4

Please sign in to comment.