Skip to content

Commit

Permalink
switch to IndexSet for committee
Browse files Browse the repository at this point in the history
  • Loading branch information
dknopik committed Jan 10, 2025
1 parent 8312017 commit 507a9f7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions anchor/common/qbft/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = { workspace = true }
[dependencies]
derive_more = { workspace = true }
tracing = { workspace = true }
indexmap = "2.7.0"

[dev-dependencies]
tracing-subscriber = { workspace = true }
11 changes: 6 additions & 5 deletions anchor/common/qbft/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::error::ConfigBuilderError;
use crate::types::{DefaultLeaderFunction, InstanceHeight, LeaderFunction, OperatorId, Round};
use indexmap::IndexSet;
use std::fmt::Debug;
use std::time::Duration;

Expand All @@ -11,7 +12,7 @@ where
pub operator_id: OperatorId,
pub instance_height: InstanceHeight,
pub round: Round,
pub committee_members: Vec<OperatorId>,
pub committee_members: IndexSet<OperatorId>,
pub quorum_size: usize,
pub round_time: Duration,
pub max_rounds: usize,
Expand All @@ -25,7 +26,7 @@ impl<F: Clone + LeaderFunction> Config<F> {
self.operator_id
}

pub fn commmittee_members(&self) -> &[OperatorId] {
pub fn commmittee_members(&self) -> &IndexSet<OperatorId> {
&self.committee_members
}

Expand Down Expand Up @@ -66,7 +67,7 @@ pub struct ConfigBuilder<F: LeaderFunction + Clone> {
operator_id: Option<OperatorId>,
instance_height: Option<InstanceHeight>,
round: Round,
committee_members: Vec<OperatorId>,
committee_members: IndexSet<OperatorId>,
quorum_size: Option<usize>,
round_time: Duration,
max_rounds: usize,
Expand All @@ -78,7 +79,7 @@ impl Default for ConfigBuilder<DefaultLeaderFunction> {
ConfigBuilder {
operator_id: None,
instance_height: None,
committee_members: vec![],
committee_members: IndexSet::new(),
quorum_size: None,
round: Round::default(),
round_time: Duration::new(2, 0),
Expand All @@ -99,7 +100,7 @@ impl<F: LeaderFunction + Clone> ConfigBuilder<F> {
self
}

pub fn committee_members(&mut self, committee_members: Vec<OperatorId>) -> &mut Self {
pub fn committee_members(&mut self, committee_members: IndexSet<OperatorId>) -> &mut Self {
self.committee_members = committee_members;
self
}
Expand Down
9 changes: 6 additions & 3 deletions anchor/common/qbft/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use crate::validation::ValidatedData;
use crate::Data;
use derive_more::{Deref, From};
use indexmap::IndexSet;
use std::cmp::Eq;
use std::fmt::Debug;
use std::hash::Hash;
Expand All @@ -15,7 +16,7 @@ pub trait LeaderFunction {
operator_id: &OperatorId,
round: Round,
instance_height: InstanceHeight,
committee: &[OperatorId],
committee: &IndexSet<OperatorId>,
) -> bool;
}

Expand All @@ -28,11 +29,13 @@ impl LeaderFunction for DefaultLeaderFunction {
operator_id: &OperatorId,
round: Round,
instance_height: InstanceHeight,
committee: &[OperatorId],
committee: &IndexSet<OperatorId>,
) -> bool {
*operator_id
== *committee
.get(((round.get() - Round::default().get()) + *instance_height) % committee.len())
.get_index(
((round.get() - Round::default().get()) + *instance_height) % committee.len(),
)
.expect("slice bounds kept by modulo length")
}
}
Expand Down

0 comments on commit 507a9f7

Please sign in to comment.