Skip to content

Commit

Permalink
feat: changed logic for leader election
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaMasych committed Aug 24, 2024
1 parent 0d0906a commit bd7341e
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/party.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::message::{
};
use crate::{Value, ValueSelector};
use rkyv::{AlignedVec, Deserialize, Infallible};
use std::cmp::PartialEq;
use std::cmp::{Ordering, PartialEq};
use std::collections::hash_map::DefaultHasher;
use std::collections::hash_map::Entry::Vacant;
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -241,14 +241,26 @@ impl<V: Value, VS: ValueSelector<V>> Party<V, VS> {
return Err(BallotError::LeaderElection("Zero weight sum".into()));
}

let mut cumulative_weight = 0;
for (i, &weight) in self.cfg.party_weights.iter().enumerate() {
cumulative_weight += weight;
if self.hash_to_range(seed, cumulative_weight) < weight {
return Ok(i as u64);
// Generate a random number in the range [0, total_weight)
let random_value = self.hash_to_range(seed, total_weight);

// Use binary search to find the corresponding participant
let mut cumulative_weights = vec![0; self.cfg.party_weights.len()];
cumulative_weights[0] = self.cfg.party_weights[0];

for i in 1..self.cfg.party_weights.len() {
cumulative_weights[i] = cumulative_weights[i - 1] + self.cfg.party_weights[i];
}

match cumulative_weights.binary_search_by(|&weight| {
if random_value < weight {
Ordering::Greater
} else {
Ordering::Less
}
}) {
Ok(index) | Err(index) => Ok(index as u64),
}
Err(BallotError::LeaderElection("Election failed".into()))
}

/// Compute seed for randomized leader election.
Expand Down

0 comments on commit bd7341e

Please sign in to comment.