diff --git a/code/crates/common/src/threshold.rs b/code/crates/common/src/threshold.rs index 563b3c96c..4bd625077 100644 --- a/code/crates/common/src/threshold.rs +++ b/code/crates/common/src/threshold.rs @@ -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. @@ -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 { @@ -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)); +// } +// } diff --git a/code/crates/starknet/host/src/spawn.rs b/code/crates/starknet/host/src/spawn.rs index f1e5ad988..d8e0e851b 100644 --- a/code/crates/starknet/host/src/spawn.rs +++ b/code/crates/starknet/host/src/spawn.rs @@ -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, @@ -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, }; diff --git a/code/crates/test/tests/driver.rs b/code/crates/test/tests/driver.rs index 3e029b82b..714001706 100644 --- a/code/crates/test/tests/driver.rs +++ b/code/crates/test/tests/driver.rs @@ -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, @@ -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); @@ -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 { @@ -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); @@ -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); @@ -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 = @@ -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 = @@ -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 @@ -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)) @@ -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))); @@ -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 @@ -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 @@ -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 diff --git a/code/crates/test/tests/driver_extra.rs b/code/crates/test/tests/driver_extra.rs index f28b82443..c17b4bf6d 100644 --- a/code/crates/test/tests/driver_extra.rs +++ b/code/crates/test/tests/driver_extra.rs @@ -5,6 +5,7 @@ use malachite_round::state::State; use malachite_test::utils::driver::*; use malachite_test::utils::validators::make_validators; use malachite_test::{Height, Proposal, TestContext, ValidatorSet, Value}; +use malachite_vote::ThresholdParams; // The following tests are performed: // - L49 with commits from current rounds, no locked value, no valid value: @@ -70,7 +71,7 @@ fn driver_steps_decide_current_with_no_locked_no_valid() { let proposal = Proposal::new(Height::new(1), Round::new(0), value, Round::Nil, v1.address); - let mut driver = Driver::new(ctx, height, vs, my_addr, Default::default()); + let mut driver = Driver::new(ctx, height, vs, my_addr, ThresholdParams::ONE_THIRD); let steps = vec![ TestStep { @@ -144,7 +145,7 @@ fn driver_steps_decide_previous_with_no_locked_no_valid() { 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, my_addr, Default::default()); + let mut driver = Driver::new(ctx, height, vs, my_addr, ThresholdParams::ONE_THIRD); let proposal = Proposal::new(Height::new(1), Round::new(0), value, Round::Nil, v1.address); @@ -255,7 +256,7 @@ fn driver_steps_decide_previous_with_locked_and_valid() { 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, my_addr, Default::default()); + let mut driver = Driver::new(ctx, height, vs, my_addr, ThresholdParams::ONE_THIRD); let proposal = Proposal::new(Height::new(1), Round::new(0), value, Round::Nil, v1.address); @@ -387,7 +388,7 @@ fn driver_steps_polka_previous_with_locked() { 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, my_addr, Default::default()); + let mut driver = Driver::new(ctx, height, vs, my_addr, ThresholdParams::ONE_THIRD); let steps = vec![ TestStep { @@ -517,7 +518,7 @@ fn driver_steps_polka_previous_invalid_proposal() { 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, my_addr, Default::default()); + let mut driver = Driver::new(ctx, height, vs, my_addr, ThresholdParams::ONE_THIRD); let steps = vec![ TestStep { @@ -613,7 +614,7 @@ fn driver_steps_polka_previous_new_proposal() { 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, my_addr, Default::default()); + let mut driver = Driver::new(ctx, height, vs, my_addr, ThresholdParams::ONE_THIRD); let steps = vec![ TestStep { @@ -741,7 +742,7 @@ fn driver_steps_polka_previous_with_no_locked() { 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, my_addr, Default::default()); + let mut driver = Driver::new(ctx, height, vs, my_addr, ThresholdParams::ONE_THIRD); let steps = vec![ TestStep { @@ -866,7 +867,7 @@ fn driver_steps_polka_nil_and_timeout_propose() { 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, my_addr, Default::default()); + let mut driver = Driver::new(ctx, height, vs, my_addr, ThresholdParams::ONE_THIRD); let steps = vec![ TestStep { @@ -929,7 +930,7 @@ fn driver_steps_polka_value_then_proposal() { 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, my_addr, Default::default()); + let mut driver = Driver::new(ctx, height, vs, my_addr, ThresholdParams::ONE_THIRD); let steps = vec![ TestStep { @@ -1001,7 +1002,7 @@ fn driver_steps_polka_any_then_proposal_other() { 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, my_addr, Default::default()); + let mut driver = Driver::new(ctx, height, vs, my_addr, ThresholdParams::ONE_THIRD); let steps = vec![ TestStep { @@ -1060,7 +1061,7 @@ fn driver_equivocate_vote() { v1.address, ); - let mut driver = Driver::new(ctx, height, vs, my_addr, Default::default()); + let mut driver = Driver::new(ctx, height, vs, my_addr, ThresholdParams::ONE_THIRD); let steps = vec![ TestStep { @@ -1129,7 +1130,7 @@ fn driver_equivocate_proposal() { v1.address, ); - let mut driver = Driver::new(ctx, height, vs, my_addr, Default::default()); + let mut driver = Driver::new(ctx, height, vs, my_addr, ThresholdParams::ONE_THIRD); let steps = vec![ TestStep { @@ -1200,7 +1201,7 @@ fn driver_conflicting_proposal_unreachable() { 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, my_addr, Default::default()); + let mut driver = Driver::new(ctx, height, vs, my_addr, ThresholdParams::ONE_THIRD); let steps = vec![ TestStep { @@ -1252,7 +1253,7 @@ fn driver_step_change_mux_with_proposal() { 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, my_addr, Default::default()); + let mut driver = Driver::new(ctx, height, vs, my_addr, ThresholdParams::ONE_THIRD); let steps = vec![ TestStep { @@ -1349,7 +1350,7 @@ fn driver_step_change_mux_with_proposal_and_polka() { let proposal = Proposal::new(Height::new(1), Round::new(1), value, Round::Nil, v1.address); - let mut driver = Driver::new(ctx, height, vs, my_addr, Default::default()); + let mut driver = Driver::new(ctx, height, vs, my_addr, ThresholdParams::ONE_THIRD); let steps = vec![ TestStep { @@ -1420,7 +1421,7 @@ fn driver_step_change_mux_with_proposal_and_commit_quorum() { let proposal = Proposal::new(Height::new(1), Round::new(1), value, Round::Nil, v1.address); - let mut driver = Driver::new(ctx, height, vs, my_addr, Default::default()); + let mut driver = Driver::new(ctx, height, vs, my_addr, ThresholdParams::ONE_THIRD); let steps = vec![ TestStep { @@ -1486,7 +1487,7 @@ fn proposal_mux_with_polka() { let proposal = Proposal::new(Height::new(1), Round::new(1), value, Round::Nil, v1.address); - let mut driver = Driver::new(ctx, height, vs, my_addr, Default::default()); + let mut driver = Driver::new(ctx, height, vs, my_addr, ThresholdParams::ONE_THIRD); let steps = vec![ TestStep { @@ -1554,7 +1555,7 @@ fn proposal_mux_with_commit_quorum() { let proposal = Proposal::new(Height::new(1), Round::new(1), value, Round::Nil, v1.address); - let mut driver = Driver::new(ctx, height, vs, my_addr, Default::default()); + let mut driver = Driver::new(ctx, height, vs, my_addr, ThresholdParams::ONE_THIRD); let steps = vec![ TestStep { diff --git a/code/crates/test/tests/vote_count.rs b/code/crates/test/tests/vote_count.rs index 08d305027..5567a8f3a 100644 --- a/code/crates/test/tests/vote_count.rs +++ b/code/crates/test/tests/vote_count.rs @@ -8,7 +8,7 @@ use malachite_vote::{Threshold, ThresholdParam}; #[test] fn vote_count_nil() { let t = 4; - let q = ThresholdParam::TWO_F_PLUS_ONE; + let q = ThresholdParam::new(2, 3); let h = Height::new(1); let r = Round::new(0); @@ -85,7 +85,7 @@ fn vote_count_nil() { #[test] fn vote_count_value() { let t = 4; - let q = ThresholdParam::TWO_F_PLUS_ONE; + let q = ThresholdParam::new(2, 3); let h = Height::new(1); let r = Round::new(0); diff --git a/code/crates/test/tests/vote_keeper.rs b/code/crates/test/tests/vote_keeper.rs index a92f9825c..55fbb2d0b 100644 --- a/code/crates/test/tests/vote_keeper.rs +++ b/code/crates/test/tests/vote_keeper.rs @@ -4,6 +4,7 @@ use malachite_vote::keeper::{Output, VoteKeeper}; use malachite_test::{ Address, Height, PrivateKey, Signature, TestContext, Validator, ValidatorSet, ValueId, Vote, }; +use malachite_vote::ThresholdParams; fn setup(vp: [u64; N]) -> ([Address; N], VoteKeeper) { let mut addrs = [Address::new([0; 20]); N]; @@ -13,7 +14,7 @@ fn setup(vp: [u64; N]) -> ([Address; N], VoteKeeper addrs[i] = Address::from_public_key(&pk.public_key()); vals.push(Validator::new(pk.public_key(), vp[i])); } - let keeper = VoteKeeper::new(ValidatorSet::new(vals), Default::default()); + let keeper = VoteKeeper::new(ValidatorSet::new(vals), ThresholdParams::ONE_THIRD); (addrs, keeper) }