Skip to content

Commit

Permalink
use configurable threshold for both standard threshold and leader thr…
Browse files Browse the repository at this point in the history
…eshold
  • Loading branch information
youngjoon-lee committed Sep 21, 2023
1 parent 60b92b5 commit 6a3822d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 30 deletions.
4 changes: 2 additions & 2 deletions consensus-engine/src/overlay/flat_overlay.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::threshold::{apply_threshold, default_leader_super_majority_threshold, deser_fraction};
use super::threshold::{apply_threshold, default_super_majority_threshold, deser_fraction};
use super::LeaderSelection;
use crate::overlay::CommitteeMembership;
use crate::{NodeId, Overlay};
Expand Down Expand Up @@ -35,7 +35,7 @@ where
nodes,
leader,
leader_threshold: leader_super_majority_threshold
.unwrap_or_else(default_leader_super_majority_threshold),
.unwrap_or_else(default_super_majority_threshold),
_committee_membership: Default::default(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion consensus-engine/src/overlay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ mod tests {
number_of_committees: 1,
leader: RoundRobin::new(),
committee_membership: FisherYatesShuffle::new(ENTROPY),
leader_super_majority_threshold: None,
super_majority_threshold: None,
});
let branch_overlay = BranchOverlay::new(BranchOverlaySettings {
current_leader: nodes[0],
Expand Down
13 changes: 5 additions & 8 deletions consensus-engine/src/overlay/threshold.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use fraction::{Fraction, GenericFraction, ToPrimitive};

const LEADER_SUPER_MAJORITY_THRESHOLD_NUM: u64 = 2;
const LEADER_SUPER_MAJORITY_THRESHOLD_DEN: u64 = 3;
const SUPER_MAJORITY_THRESHOLD_NUM: u64 = 2;
const SUPER_MAJORITY_THRESHOLD_DEN: u64 = 3;

pub fn default_leader_super_majority_threshold() -> GenericFraction<u64> {
Fraction::new(
LEADER_SUPER_MAJORITY_THRESHOLD_NUM,
LEADER_SUPER_MAJORITY_THRESHOLD_DEN,
)
pub(crate) fn default_super_majority_threshold() -> GenericFraction<u64> {
Fraction::new(SUPER_MAJORITY_THRESHOLD_NUM, SUPER_MAJORITY_THRESHOLD_DEN)
}

pub fn apply_threshold(size: usize, threshold: GenericFraction<u64>) -> usize {
pub(crate) fn apply_threshold(size: usize, threshold: GenericFraction<u64>) -> usize {
// `threshold` is a tuple of (num, den) where `num/den` is the super majority threshold
(Fraction::from(size) * threshold)
.ceil()
Expand Down
33 changes: 16 additions & 17 deletions consensus-engine/src/overlay/tree_overlay/overlay.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::tree::Tree;
use crate::overlay::threshold::{
apply_threshold, default_leader_super_majority_threshold, deser_fraction,
apply_threshold, default_super_majority_threshold, deser_fraction,
};
use crate::overlay::CommitteeMembership;
use crate::{overlay::LeaderSelection, Committee, NodeId, Overlay};
Expand All @@ -19,7 +19,7 @@ pub struct TreeOverlaySettings<L: LeaderSelection, M: CommitteeMembership> {
/// Defaults to 2/3
#[serde(with = "deser_fraction")]
#[serde(skip_serializing_if = "Option::is_none")]
pub leader_super_majority_threshold: Option<Fraction>,
pub super_majority_threshold: Option<Fraction>,
}

#[derive(Debug, Clone)]
Expand All @@ -30,7 +30,7 @@ pub struct TreeOverlay<L, M> {
pub(super) carnot_tree: Tree,
pub(super) leader: L,
pub(super) committee_membership: M,
pub(super) leader_threshold: Fraction,
pub(super) threshold: Fraction,
}

impl<L, M> Overlay for TreeOverlay<L, M>
Expand All @@ -50,7 +50,7 @@ where
number_of_committees,
leader,
committee_membership,
leader_super_majority_threshold,
super_majority_threshold,
} = settings;

committee_membership.reshape_committees(&mut nodes);
Expand All @@ -63,8 +63,7 @@ where
carnot_tree,
leader,
committee_membership,
leader_threshold: leader_super_majority_threshold
.unwrap_or_else(default_leader_super_majority_threshold),
threshold: super_majority_threshold.unwrap_or_else(default_super_majority_threshold),
}
}

Expand Down Expand Up @@ -149,7 +148,7 @@ where
}
self.carnot_tree
.committee_by_member_id(&id)
.map(|c| (c.len() * 2 / 3) + 1)
.map(|c| apply_threshold(c.len(), self.threshold))
.expect("node is not part of any committee")
}

Expand All @@ -171,7 +170,7 @@ where
// });
// let root_size = self.root_committee().len();
// let committee_size = root_size + children_size;
apply_threshold(self.root_committee().len(), self.leader_threshold)
apply_threshold(self.root_committee().len(), self.threshold)
}

fn update_leader_selection<F, E>(&self, f: F) -> Result<Self, E>
Expand All @@ -198,7 +197,7 @@ where
number_of_committees: self.number_of_committees,
leader: self.leader.clone(),
committee_membership,
leader_super_majority_threshold: Some(self.leader_threshold),
super_majority_threshold: Some(self.threshold),
};
Self::new(settings)
})
Expand All @@ -217,7 +216,7 @@ where
number_of_committees: self.number_of_committees,
leader,
committee_membership,
leader_super_majority_threshold: Some(self.leader_threshold),
super_majority_threshold: Some(self.threshold),
})
}

Expand Down Expand Up @@ -248,7 +247,7 @@ mod tests {
number_of_committees: 3,
leader: RoundRobin::new(),
committee_membership: FisherYatesShuffle::new(ENTROPY),
leader_super_majority_threshold: None,
super_majority_threshold: None,
});

assert_eq!(*overlay.leader(), nodes[0]);
Expand All @@ -263,7 +262,7 @@ mod tests {
number_of_committees: 3,
leader: RoundRobin::new(),
committee_membership: FisherYatesShuffle::new(ENTROPY),
leader_super_majority_threshold: None,
super_majority_threshold: None,
});

let leader = overlay.next_leader();
Expand All @@ -281,7 +280,7 @@ mod tests {
number_of_committees: 3,
leader: RoundRobin::new(),
committee_membership: FisherYatesShuffle::new(ENTROPY),
leader_super_majority_threshold: None,
super_majority_threshold: None,
});

let mut expected_root = Committee::new();
Expand All @@ -300,7 +299,7 @@ mod tests {
number_of_committees: 3,
leader: RoundRobin::new(),
committee_membership: FisherYatesShuffle::new(ENTROPY),
leader_super_majority_threshold: None,
super_majority_threshold: None,
});

let mut leaf_committees = overlay
Expand Down Expand Up @@ -331,7 +330,7 @@ mod tests {
number_of_committees: 3,
leader: RoundRobin::new(),
committee_membership: FisherYatesShuffle::new(ENTROPY),
leader_super_majority_threshold: None,
super_majority_threshold: None,
});

assert_eq!(overlay.super_majority_threshold(overlay.nodes[8]), 0);
Expand All @@ -346,7 +345,7 @@ mod tests {
number_of_committees: 3,
leader: RoundRobin::new(),
committee_membership: FisherYatesShuffle::new(ENTROPY),
leader_super_majority_threshold: None,
super_majority_threshold: None,
});

assert_eq!(overlay.super_majority_threshold(overlay.nodes[0]), 3);
Expand All @@ -361,7 +360,7 @@ mod tests {
number_of_committees: 3,
leader: RoundRobin::new(),
committee_membership: FisherYatesShuffle::new(ENTROPY),
leader_super_majority_threshold: None,
super_majority_threshold: None,
});

assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion simulations/src/bin/app/overlay_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn to_overlay_node<R: Rng>(
number_of_committees: tree_settings.number_of_committees,
leader: RoundRobin::new(),
committee_membership: RandomBeaconState::initial_sad_from_entropy([0; 32]),
leader_super_majority_threshold: None,
super_majority_threshold: None,
};
Box::new(
CarnotNode::<TreeOverlay<RoundRobin, RandomBeaconState>>::new(
Expand Down
2 changes: 1 addition & 1 deletion tests/src/nodes/nomos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ fn create_node_config(
// By setting the leader_threshold to 1 we ensure that all nodes come
// online before progressing. This is only necessary until we add a way
// to recover poast blocks from other nodes.
leader_super_majority_threshold: Some(threshold),
super_majority_threshold: Some(threshold),
},
timeout,
},
Expand Down

0 comments on commit 6a3822d

Please sign in to comment.