Skip to content

Commit

Permalink
consensus: refactor generate_committee
Browse files Browse the repository at this point in the history
- fix a bug of not extracting the next generator due to skipping
  the already done Proposal step
- extract next generator on Validation/Ratification step instead
  of Proposal
- fix 'iteration < CONSENSUS_MAX_ITER' wrong check (last iteration
  is actually CONSENSUS_MAX_ITER-1)
  • Loading branch information
fed-franz committed Sep 5, 2024
1 parent 785829e commit 485c820
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions consensus/src/iteration_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,35 @@ impl IterationCtx {
let iteration = self.iter;
let step = step_name.to_step(iteration);

// Check if this committee has been already generated
// Check if we already generated the committee.
// This will be usually the case for all Proposal steps after
// iteration 0
if self.committees.get_committee(step).is_some() {
return;
}

// For Validation and Ratification steps we need the next-iteration
// generator for the exclusion list. So we extract, it if necessary.
//
// This is not necessary in the last iteration, so we skip it
if step_name != StepName::Proposal && iteration < CONSENSUS_MAX_ITER - 1
{
let prop = StepName::Proposal;
let next_prop_step = prop.to_step(iteration + 1);

// Check if this committee has been already generated.
// This will be typically the case when executing the Ratification
// step after the Validation one
if self.committees.get_committee(next_prop_step).is_none() {
let mut next_cfg =
self.get_sortition_config(seed, prop, vec![]);
next_cfg.step = next_prop_step;

let next_generator = Committee::new(provisioners, &next_cfg);
self.committees.insert(next_prop_step, next_generator);
}
}

// Fill up exclusion list
//
// We exclude the generators for the current iteration and the next one
Expand All @@ -175,7 +199,7 @@ impl IterationCtx {
exclusion_list.push(cur_generator);

// Exclude generator for next iteration
if iteration < CONSENSUS_MAX_ITER {
if iteration < CONSENSUS_MAX_ITER - 1 {
let next_generator =
self.get_generator(iteration + 1).expect(
"Next Proposal committee to be already generated",
Expand All @@ -197,20 +221,6 @@ impl IterationCtx {
&self.get_sortition_config(seed, step_name, exclusion),
);

if let StepName::Proposal = step_name {
if iteration < CONSENSUS_MAX_ITER {
let mut cfg_next_iteration =
self.get_sortition_config(seed, step_name, vec![]);
cfg_next_iteration.step =
StepName::Proposal.to_step(iteration + 1);

let next_iteration_generator =
Committee::new(provisioners, &cfg_next_iteration);
self.committees
.insert(cfg_next_iteration.step, next_iteration_generator);
}
}

debug!(
event = "committee_generated",
members = format!("{}", &step_committee)
Expand Down

0 comments on commit 485c820

Please sign in to comment.