diff --git a/consensus-engine/src/overlay/flat_overlay.rs b/consensus-engine/src/overlay/flat_overlay.rs index 83d7c10ff..dbadb8cb2 100644 --- a/consensus-engine/src/overlay/flat_overlay.rs +++ b/consensus-engine/src/overlay/flat_overlay.rs @@ -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}; @@ -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(), } } diff --git a/consensus-engine/src/overlay/mod.rs b/consensus-engine/src/overlay/mod.rs index 1a5bcade9..91d3daa52 100644 --- a/consensus-engine/src/overlay/mod.rs +++ b/consensus-engine/src/overlay/mod.rs @@ -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], diff --git a/consensus-engine/src/overlay/threshold.rs b/consensus-engine/src/overlay/threshold.rs index 2a7d719bd..395649a00 100644 --- a/consensus-engine/src/overlay/threshold.rs +++ b/consensus-engine/src/overlay/threshold.rs @@ -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 { - Fraction::new( - LEADER_SUPER_MAJORITY_THRESHOLD_NUM, - LEADER_SUPER_MAJORITY_THRESHOLD_DEN, - ) +pub(crate) fn default_super_majority_threshold() -> GenericFraction { + Fraction::new(SUPER_MAJORITY_THRESHOLD_NUM, SUPER_MAJORITY_THRESHOLD_DEN) } -pub fn apply_threshold(size: usize, threshold: GenericFraction) -> usize { +pub(crate) fn apply_threshold(size: usize, threshold: GenericFraction) -> usize { // `threshold` is a tuple of (num, den) where `num/den` is the super majority threshold (Fraction::from(size) * threshold) .ceil() diff --git a/consensus-engine/src/overlay/tree_overlay/overlay.rs b/consensus-engine/src/overlay/tree_overlay/overlay.rs index 1e6a3c734..8155238ac 100644 --- a/consensus-engine/src/overlay/tree_overlay/overlay.rs +++ b/consensus-engine/src/overlay/tree_overlay/overlay.rs @@ -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}; @@ -19,7 +19,7 @@ pub struct TreeOverlaySettings { /// Defaults to 2/3 #[serde(with = "deser_fraction")] #[serde(skip_serializing_if = "Option::is_none")] - pub leader_super_majority_threshold: Option, + pub super_majority_threshold: Option, } #[derive(Debug, Clone)] @@ -30,7 +30,7 @@ pub struct TreeOverlay { pub(super) carnot_tree: Tree, pub(super) leader: L, pub(super) committee_membership: M, - pub(super) leader_threshold: Fraction, + pub(super) threshold: Fraction, } impl Overlay for TreeOverlay @@ -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); @@ -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), } } @@ -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") } @@ -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(&self, f: F) -> Result @@ -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) }) @@ -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), }) } @@ -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]); @@ -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(); @@ -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(); @@ -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 @@ -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); @@ -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); @@ -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!( diff --git a/simulations/src/bin/app/overlay_node.rs b/simulations/src/bin/app/overlay_node.rs index bffed3038..dbde33a97 100644 --- a/simulations/src/bin/app/overlay_node.rs +++ b/simulations/src/bin/app/overlay_node.rs @@ -57,7 +57,7 @@ pub fn to_overlay_node( 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::>::new( diff --git a/tests/src/nodes/nomos.rs b/tests/src/nodes/nomos.rs index 4adcaee3c..950afb134 100644 --- a/tests/src/nodes/nomos.rs +++ b/tests/src/nodes/nomos.rs @@ -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, },