Skip to content

Commit

Permalink
Merge pull request #1166 from dusk-network/refactor_committee
Browse files Browse the repository at this point in the history
Refactor committee
  • Loading branch information
herr-seppia authored Dec 4, 2023
2 parents 1f59b9f + f83af8d commit 02ae1e9
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 226 deletions.
2 changes: 1 addition & 1 deletion consensus/example/consensus_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn run_main_loop(
// Load provisioners keys from external consensus keys.
// The loaded keys should be the same as the ones from Genesis State.
let keys = load_provisioners_keys(provisioners_num);
let mut provisioners = Provisioners::new();
let mut provisioners = Provisioners::default();

for (_, (_, pk)) in keys.iter().enumerate() {
provisioners.add_member_with_value(pk.clone(), 1000 * DUSK * 10);
Expand Down
2 changes: 1 addition & 1 deletion consensus/example/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn generate_keys(n: u64) -> Vec<(SecretKey, PublicKey)> {
fn generate_provisioners_from_keys(
keys: Vec<(SecretKey, PublicKey)>,
) -> Provisioners {
let mut p = Provisioners::new();
let mut p = Provisioners::default();

for (pos, (_, pk)) in keys.into_iter().enumerate() {
p.add_member_with_value(pk, 1000 * (pos as u64) * DUSK);
Expand Down
8 changes: 3 additions & 5 deletions consensus/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ mod tests {

// Create provisioners
// Also populate a vector of headers
let mut p = Provisioners::new();
let mut p = Provisioners::default();
let mut input = vec![];

for sk in sks {
Expand All @@ -248,14 +248,12 @@ mod tests {
input.push((signature, header));
}

p.update_eligibility_flag(round);

// Execute sortition with specific config
let cfg = Config::new(Seed::from([4u8; 48]), round, step, 10);
let c = Committee::new(
node_data::bls::PublicKey::new(PublicKey::default()),
&mut p,
cfg,
&p,
&cfg,
);

let target_quorum = 7;
Expand Down
1 change: 0 additions & 1 deletion consensus/src/commons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use tokio::task::JoinSet;
use tracing::{debug, error};

#[derive(Clone, Default, Debug)]
#[allow(unused)]
pub struct RoundUpdate {
// Current round number of the ongoing consensus
pub round: u64,
Expand Down
5 changes: 1 addition & 4 deletions consensus/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,10 @@ impl<T: Operations + 'static, D: Database + 'static> Consensus<T, D> {
pub async fn spin(
&mut self,
ru: RoundUpdate,
mut provisioners: Provisioners,
provisioners: Provisioners,
cancel_rx: oneshot::Receiver<i32>,
) -> Result<Block, ConsensusError> {
let round = ru.round;
// Enable/Disable all members stakes depending on the current round. If
// a stake is not eligible for this round, it's disabled.
provisioners.update_eligibility_flag(round);

// Agreement loop Executes agreement loop in a separate tokio::task to
// collect (aggr)Agreement messages.
Expand Down
1 change: 0 additions & 1 deletion consensus/src/firststep/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use std::sync::Arc;
use tokio::sync::Mutex;
use tracing::debug;

#[allow(unused)]
pub struct Reduction<T, DB: Database> {
timeout_millis: u64,
handler: Arc<Mutex<handler::Reduction<DB>>>,
Expand Down
2 changes: 1 addition & 1 deletion consensus/src/phase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<T: Operations + 'static, D: Database + 'static> Phase<T, D> {
let step_committee = Committee::new(
ctx.round_update.pubkey_bls.clone(),
ctx.provisioners,
ctx.get_sortition_config(size),
&ctx.get_sortition_config(size),
);

debug!(
Expand Down
1 change: 0 additions & 1 deletion consensus/src/secondstep/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use node_data::message::{Message, Payload, Topics};
use std::sync::Arc;
use tokio::sync::Mutex;

#[allow(unused)]
pub struct Reduction<T, DB> {
handler: Arc<Mutex<handler::Reduction>>,
candidate: Option<Block>,
Expand Down
31 changes: 16 additions & 15 deletions consensus/src/user/committee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ use std::collections::{BTreeMap, HashMap, HashSet};
use std::fmt;
use std::mem;

#[allow(unused)]
#[derive(Default, Debug, Clone)]
pub struct Committee {
members: BTreeMap<PublicKey, usize>,
this_member_key: PublicKey,
cfg: sortition::Config,
quorum: usize,
nil_quorum: usize,
}

impl Committee {
Expand All @@ -28,7 +28,6 @@ impl Committee {
}
}

#[allow(unused)]
impl Committee {
/// Generates a new committee from the given provisioners state and
/// sortition config.
Expand All @@ -41,19 +40,24 @@ impl Committee {
/// method.
pub fn new(
pubkey_bls: PublicKey,
provisioners: &mut Provisioners,
cfg: sortition::Config,
provisioners: &Provisioners,
cfg: &sortition::Config,
) -> Self {
provisioners.update_eligibility_flag(cfg.round);
// Generate committee using deterministic sortition.
let res = provisioners.create_committee(&cfg);
let res = provisioners.create_committee(cfg);

let quorum = (cfg.committee_size as f64
* config::CONSENSUS_QUORUM_THRESHOLD)
.ceil() as usize;
let nil_quorum = quorum;

// Turn the raw vector into a hashmap where we map a pubkey to its
// occurrences.
let mut committee = Self {
members: BTreeMap::new(),
this_member_key: pubkey_bls,
cfg,
nil_quorum,
quorum,
};

for member_key in res {
Expand Down Expand Up @@ -94,22 +98,19 @@ impl Committee {

/// Returns target quorum for the generated committee.
pub fn quorum(&self) -> usize {
(self.cfg.committee_size as f64 * config::CONSENSUS_QUORUM_THRESHOLD)
.ceil() as usize
self.quorum
}

/// Returns target NIL quorum for the generated committee.
pub fn nil_quorum(&self) -> usize {
(self.cfg.committee_size as f64 * config::CONSENSUS_NILQUORUM_THRESHOLD)
.ceil() as usize
self.nil_quorum
}

pub fn bits(&self, voters: &Cluster<PublicKey>) -> u64 {
let mut bits: u64 = 0;

debug_assert!(self.members.len() <= mem::size_of_val(&bits) * 8);

let mut pos = 0;
for (pk, _) in voters.iter() {
for (pos, (member_pk, _)) in self.members.iter().enumerate() {
if member_pk.eq(pk) {
Expand Down Expand Up @@ -251,8 +252,8 @@ impl CommitteeSet {
.or_insert_with_key(|config| {
Committee::new(
self.this_member_key.clone(),
&mut self.provisioners,
config.clone(),
&self.provisioners,
config,
)
})
}
Expand Down
Loading

0 comments on commit 02ae1e9

Please sign in to comment.