Skip to content

Commit

Permalink
consensus: refactor create_committee function to reflect specs
Browse files Browse the repository at this point in the history
  • Loading branch information
herr-seppia committed Feb 9, 2024
1 parent 62e7ff6 commit d27860e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 29 deletions.
4 changes: 2 additions & 2 deletions consensus/src/user/committee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Committee {
/// It executes deterministic sortition algorithm.
pub fn new(provisioners: &Provisioners, cfg: &sortition::Config) -> Self {
// Generate committee using deterministic sortition.
let res = provisioners.create_committee(cfg);
let extracted = provisioners.create_committee(cfg);

let quorum = (cfg.committee_size() as f64
* config::CONSENSUS_QUORUM_THRESHOLD)
Expand All @@ -51,7 +51,7 @@ impl Committee {
excluded: cfg.exclusion().copied(),
};

for member_key in res {
for member_key in extracted {
*committee.members.entry(member_key).or_insert(0) += 1;
}

Expand Down
37 changes: 15 additions & 22 deletions consensus/src/user/provisioners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,51 +118,44 @@ impl Provisioners {
&self,
cfg: &sortition::Config,
) -> Vec<PublicKey> {
let mut committee: Vec<PublicKey> = vec![];
let committee_size = cfg.committee_size();
let mut extracted: Vec<PublicKey> = Vec::with_capacity(committee_size);

let mut comm = CommitteeGenerator::from_provisioners(
self,
cfg.round(),
cfg.exclusion(),
);

let mut total_amount_stake =
BigInt::from(comm.calc_total_eligible_weight());
let mut total_weight = comm.total_weight().into();

let mut counter: u32 = 0;
loop {
if total_amount_stake.eq(&BigInt::from(0))
|| committee.len() == cfg.committee_size()
{
break;
}
while extracted.len() != committee_size {
let counter = extracted.len() as u32;

// 1. Compute n ← H(seed ∣∣ round ∣∣ step ∣∣ counter)
// 1. Compute n ← H(seed ∣∣ step ∣∣ counter)
let hash = sortition::create_sortition_hash(cfg, counter);
counter += 1;

// 2. Compute d ← n mod s
let score =
sortition::generate_sortition_score(hash, &total_amount_stake);
sortition::generate_sortition_score(hash, &total_weight);

// NB: The public key can be extracted multiple times per committee.
match comm.extract_and_subtract_member(score) {
Some((pk, value)) => {
Some((pk, subtracted_stake)) => {
// append the public key to the committee set.
committee.push(pk);
extracted.push(pk);

let subtracted_stake = value;
if total_amount_stake > subtracted_stake {
total_amount_stake -= subtracted_stake;
if total_weight > subtracted_stake {
total_weight -= subtracted_stake;
} else {
total_amount_stake = BigInt::from(0);
break;
}
}
None => panic!("invalid score"),
}
}

committee
extracted
}

pub fn get_generator(
Expand Down Expand Up @@ -228,8 +221,8 @@ impl<'a> CommitteeGenerator<'a> {
}
}

/// Sums up the total weight of all **eligible** stakes
fn calc_total_eligible_weight(&self) -> u64 {
/// Sums up the total weight of all stakes
fn total_weight(&self) -> u64 {
self.members.values().map(|m| m.value()).sum()
}

Expand Down
6 changes: 1 addition & 5 deletions consensus/src/user/sortition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn create_sortition_hash(cfg: &Config, counter: u32) -> [u8; 32] {
reader.as_slice().try_into().expect("Wrong length")
}

// Generate a score from the given hash and total stake weight
/// Generate a score from the given hash and total stake weight
pub fn generate_sortition_score(
hash: [u8; 32],
total_weight: &BigInt,
Expand All @@ -91,10 +91,6 @@ pub fn generate_sortition_score(
num % total_weight
}

// The set of active stakes consists of tuples ,
// where is the amount staked and is the BLS public key corresponding to the
// stake.

#[cfg(test)]
mod tests {

Expand Down

0 comments on commit d27860e

Please sign in to comment.