Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(code): Experiment with 1/5 faulty threshold instead of 1/3 #581

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 54 additions & 31 deletions code/crates/common/src/threshold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,42 @@ pub struct ThresholdParams {
pub honest: ThresholdParam,
}

impl Default for ThresholdParams {
fn default() -> Self {
Self {
quorum: ThresholdParam::TWO_F_PLUS_ONE,
honest: ThresholdParam::F_PLUS_ONE,
}
}
impl ThresholdParams {
/// One third of the total weight may be faulty (f = 1/3)
pub const ONE_THIRD: ThresholdParams = one_third::THRESHOLD_PARAMS;

/// One fifth of the total weight may be faulty (f = 1/5)
pub const ONE_FIFTH: ThresholdParams = one_fifth::THRESHOLD_PARAMS;
}

mod one_third {
use super::{ThresholdParam, ThresholdParams};

pub const THRESHOLD_PARAMS: ThresholdParams = ThresholdParams {
quorum: QUORUM,
honest: HONEST,
};

/// More than one third of the total weight (f + 1)
pub const HONEST: ThresholdParam = ThresholdParam::new(1, 3);

/// More than two thirds of the total weight (2f + 1)
pub const QUORUM: ThresholdParam = ThresholdParam::new(2, 3);
}

mod one_fifth {
use super::{ThresholdParam, ThresholdParams};

pub const THRESHOLD_PARAMS: ThresholdParams = ThresholdParams {
quorum: QUORUM,
honest: HONEST,
};

/// More than one fifth of the total weight (f + 1)
pub const HONEST: ThresholdParam = ThresholdParam::new(1, 5);

/// More than two fifths of the total weight (2f + 1)
pub const QUORUM: ThresholdParam = ThresholdParam::new(2, 5);
}

/// Represents the different quorum thresholds.
Expand All @@ -50,12 +79,6 @@ pub struct ThresholdParam {
}

impl ThresholdParam {
/// 2f+1, ie. more than two thirds of the total weight
pub const TWO_F_PLUS_ONE: Self = Self::new(2, 3);

/// f+1, ie. more than one third of the total weight
pub const F_PLUS_ONE: Self = Self::new(1, 3);

/// Create a new threshold parameter with the given numerator and denominator.
pub const fn new(numerator: u64, denominator: u64) -> Self {
Self {
Expand Down Expand Up @@ -87,21 +110,21 @@ impl ThresholdParam {
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn threshold_param_is_met() {
assert!(ThresholdParam::TWO_F_PLUS_ONE.is_met(7, 10));
assert!(!ThresholdParam::TWO_F_PLUS_ONE.is_met(6, 10));
assert!(ThresholdParam::F_PLUS_ONE.is_met(4, 10));
assert!(!ThresholdParam::F_PLUS_ONE.is_met(3, 10));
}

#[test]
#[should_panic(expected = "attempt to multiply with overflow")]
fn threshold_param_is_met_overflow() {
assert!(!ThresholdParam::TWO_F_PLUS_ONE.is_met(1, u64::MAX));
}
}
// #[cfg(test)]
// mod tests {
// use super::*;
//
// #[test]
// fn threshold_param_is_met() {
// assert!(ThresholdParam::TWO_THIRDS.is_met(7, 10));
// assert!(!ThresholdParam::TWO_THIRDS.is_met(6, 10));
// assert!(ThresholdParam::ONE_THIRD.is_met(4, 10));
// assert!(!ThresholdParam::ONE_THIRD.is_met(3, 10));
// }
//
// #[test]
// #[should_panic(expected = "attempt to multiply with overflow")]
// fn threshold_param_is_met_overflow() {
// assert!(!ThresholdParam::TWO_THIRDS.is_met(1, u64::MAX));
// }
// }
4 changes: 2 additions & 2 deletions code/crates/starknet/host/src/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use malachite_actors::gossip_mempool::{GossipMempool, GossipMempoolRef};
use malachite_actors::host::HostRef;
use malachite_actors::node::{Node, NodeRef};
use malachite_blocksync as blocksync;
use malachite_common::CommitCertificate;
use malachite_common::{CommitCertificate, ThresholdParams};
use malachite_config::{
BlockSyncConfig, Config as NodeConfig, MempoolConfig, PubSubProtocol, TestConfig,
TransportProtocol,
Expand Down Expand Up @@ -157,7 +157,7 @@ async fn spawn_consensus_actor(
start_height,
initial_validator_set,
address,
threshold_params: Default::default(),
threshold_params: ThresholdParams::ONE_FIFTH,
value_payload,
};

Expand Down
25 changes: 13 additions & 12 deletions code/crates/test/tests/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use malachite_test::utils::validators::make_validators;
use malachite_test::{
Address, Height, Proposal, Signature, TestContext, ValidatorSet, Value, ValueId, Vote,
};
use malachite_vote::ThresholdParams;

pub struct TestStep {
desc: &'static str,
Expand Down Expand Up @@ -90,7 +91,7 @@ fn driver_steps_proposer() {
let sel = Arc::new(FixedProposer::new(my_addr));
let vs = ValidatorSet::new(vec![v1, v2.clone(), v3.clone()]);

let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, Default::default());
let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, ThresholdParams::ONE_THIRD);

let proposal = new_signed_proposal(Height::new(1), Round::new(0), value, Round::Nil, my_addr);

Expand Down Expand Up @@ -288,7 +289,7 @@ fn driver_steps_proposer_timeout_get_value() {
let sel = Arc::new(FixedProposer::new(my_addr));
let vs = ValidatorSet::new(vec![v1, v2.clone(), v3.clone()]);

let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, Default::default());
let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, ThresholdParams::ONE_THIRD);

let steps = vec![
TestStep {
Expand Down Expand Up @@ -346,7 +347,7 @@ fn driver_steps_not_proposer_valid() {
let sel = Arc::new(FixedProposer::new(v1.address));
let vs = ValidatorSet::new(vec![v1.clone(), v2.clone(), v3.clone()]);

let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, Default::default());
let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, ThresholdParams::ONE_THIRD);

let proposal =
new_signed_proposal(Height::new(1), Round::new(0), value, Round::Nil, v1.address);
Expand Down Expand Up @@ -534,7 +535,7 @@ fn driver_steps_not_proposer_invalid() {
let sel = Arc::new(FixedProposer::new(v1.address));
let vs = ValidatorSet::new(vec![v1.clone(), v2.clone(), v3.clone()]);

let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, Default::default());
let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, ThresholdParams::ONE_THIRD);

let proposal =
new_signed_proposal(Height::new(1), Round::new(0), value, Round::Nil, v1.address);
Expand Down Expand Up @@ -656,7 +657,7 @@ fn driver_steps_not_proposer_other_height() {
let sel = Arc::new(FixedProposer::new(v1.address));
let vs = ValidatorSet::new(vec![v1.clone(), v2.clone()]);

let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, Default::default());
let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, ThresholdParams::ONE_THIRD);

// Proposal is for another height
let proposal =
Expand Down Expand Up @@ -715,7 +716,7 @@ fn driver_steps_not_proposer_other_round() {
let sel = Arc::new(FixedProposer::new(v1.address));
let vs = ValidatorSet::new(vec![v1.clone(), v2.clone()]);

let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, Default::default());
let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, ThresholdParams::ONE_THIRD);

// Proposal is for another round
let proposal =
Expand Down Expand Up @@ -765,7 +766,7 @@ fn driver_steps_not_proposer_timeout_multiple_rounds() {
let sel = Arc::new(FixedProposer::new(v1.address));
let vs = ValidatorSet::new(vec![v1.clone(), v2.clone(), v3.clone()]);

let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, Default::default());
let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, ThresholdParams::ONE_THIRD);

let steps = vec![
// Start round 0, we, v3, are not the proposer
Expand Down Expand Up @@ -944,7 +945,7 @@ fn driver_steps_no_value_to_propose() {
// We are the proposer
let vs = ValidatorSet::new(vec![v1.clone(), v2.clone(), v3.clone()]);

let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, Default::default());
let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, ThresholdParams::ONE_THIRD);

let outputs = driver
.process(Input::NewRound(Height::new(1), Round::new(0), v1.address))
Expand Down Expand Up @@ -975,7 +976,7 @@ fn driver_steps_proposer_not_found() {
// Proposer is v1, which is not in the validator set
let vs = ValidatorSet::new(vec![v2.clone(), v3.clone()]);

let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, Default::default());
let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, ThresholdParams::ONE_THIRD);

let output = driver.process(Input::NewRound(Height::new(1), Round::new(0), v1.address));
assert_eq!(output, Err(Error::ProposerNotFound(v1.address)));
Expand All @@ -996,7 +997,7 @@ fn driver_steps_validator_not_found() {
// We omit v2 from the validator set
let vs = ValidatorSet::new(vec![v1.clone(), v3.clone()]);

let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, Default::default());
let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, ThresholdParams::ONE_THIRD);

// Start new height
driver
Expand Down Expand Up @@ -1029,7 +1030,7 @@ fn driver_steps_skip_round_skip_threshold() {
let height = Height::new(1);

let vs = ValidatorSet::new(vec![v1.clone(), v2.clone(), v3.clone()]);
let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, Default::default());
let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, ThresholdParams::ONE_THIRD);

let steps = vec![
// Start round 0, we, v3, are not the proposer
Expand Down Expand Up @@ -1131,7 +1132,7 @@ fn driver_steps_skip_round_quorum_threshold() {
let ctx = TestContext::new(my_sk.clone());

let vs = ValidatorSet::new(vec![v1.clone(), v2.clone(), v3.clone()]);
let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, Default::default());
let mut driver = Driver::new(ctx, height, vs.clone(), my_addr, ThresholdParams::ONE_THIRD);

let steps = vec![
// Start round 0, we, v3, are not the proposer
Expand Down
Loading
Loading