diff --git a/node/actors/bft/src/testonly/ut_harness.rs b/node/actors/bft/src/testonly/ut_harness.rs index f3c7be12..93a3b681 100644 --- a/node/actors/bft/src/testonly/ut_harness.rs +++ b/node/actors/bft/src/testonly/ut_harness.rs @@ -12,7 +12,7 @@ use zksync_concurrency::ctx; use zksync_consensus_network as network; use zksync_consensus_roles::validator::{ self, CommitQC, LeaderCommit, LeaderPrepare, Phase, PrepareQC, ReplicaCommit, ReplicaPrepare, - SecretKey, Signed, ValidatorCommittee, ViewNumber, + SecretKey, Signed, ViewNumber, }; use zksync_consensus_storage::{ testonly::{in_memory, new_store}, @@ -224,7 +224,8 @@ impl UTHarness { ctx: &ctx::Ctx, msg: ReplicaPrepare, ) -> Signed { - let expected_validator_weight = ValidatorCommittee::MAX_WEIGHT / self.keys.len() as u64; + let expected_validator_weight = + self.genesis().validators.max_weight() / self.keys.len() as u64; let want_threshold = self.genesis().validators.threshold(); let mut leader_prepare = None; let msgs: Vec<_> = self.keys.iter().map(|k| k.sign_msg(msg.clone())).collect(); @@ -260,7 +261,8 @@ impl UTHarness { ctx: &ctx::Ctx, msg: ReplicaCommit, ) -> Signed { - let expected_validator_weight = ValidatorCommittee::MAX_WEIGHT / self.keys.len() as u64; + let expected_validator_weight = + self.genesis().validators.max_weight() / self.keys.len() as u64; let want_threshold = self.genesis().validators.threshold(); let mut first_match = true; for (i, key) in self.keys.iter().enumerate() { diff --git a/node/libs/roles/src/validator/messages/consensus.rs b/node/libs/roles/src/validator/messages/consensus.rs index 19b8859d..f56c3f81 100644 --- a/node/libs/roles/src/validator/messages/consensus.rs +++ b/node/libs/roles/src/validator/messages/consensus.rs @@ -82,10 +82,6 @@ pub struct ValidatorCommittee { } impl ValidatorCommittee { - /// Maximum validator weight - /// 100.00% - pub const MAX_WEIGHT: u64 = 10000; - /// Required weight to verify signatures. /// Currently 80.00% const THRESHOLD: u64 = 8000; @@ -99,10 +95,7 @@ impl ValidatorCommittee { set.insert(validator.key.clone()), "Duplicate validator in ValidatorCommittee" ); - weights.insert( - validator.key, - validator.weight, - ); + weights.insert(validator.key, validator.weight); } anyhow::ensure!( !set.is_empty(), @@ -191,6 +184,11 @@ impl ValidatorCommittee { }) .sum() } + + /// Sum of all validators' weight in the committee + pub fn max_weight(&self) -> u64 { + self.weights.iter().sum() + } } /// Calculate the consensus threshold, the minimum number of votes for any consensus action to be valid, diff --git a/node/libs/roles/src/validator/testonly.rs b/node/libs/roles/src/validator/testonly.rs index 0934de53..1e5c69b8 100644 --- a/node/libs/roles/src/validator/testonly.rs +++ b/node/libs/roles/src/validator/testonly.rs @@ -23,7 +23,7 @@ impl Setup { /// New `Setup` with a given `fork`. pub fn new_with_fork(rng: &mut impl Rng, validators: usize, fork: Fork) -> Self { let keys: Vec = (0..validators).map(|_| rng.gen()).collect(); - let weight = ValidatorCommittee::MAX_WEIGHT / validators as u64; + let weight = 10000 / validators as u64; let genesis = Genesis { validators: ValidatorCommittee::new(keys.iter().map(|k| WeightedValidator { key: k.public(), @@ -301,9 +301,10 @@ impl Distribution for Standard { impl Distribution for Standard { fn sample(&self, rng: &mut R) -> ValidatorCommittee { let count = rng.gen_range(1..11); + let weight = 10000 / count; let public_keys = (0..count).map(|_| WeightedValidator { key: rng.gen(), - weight: ValidatorCommittee::MAX_WEIGHT, + weight, }); ValidatorCommittee::new(public_keys).unwrap() } diff --git a/node/libs/roles/src/validator/tests.rs b/node/libs/roles/src/validator/tests.rs index fcb2a33f..da9c9204 100644 --- a/node/libs/roles/src/validator/tests.rs +++ b/node/libs/roles/src/validator/tests.rs @@ -210,6 +210,7 @@ fn test_commit_qc() { .unwrap(), fork: setup1.genesis.fork.clone(), }; + let validator_weight = setup1.genesis.validators.max_weight(); for i in 0..setup1.keys.len() + 1 { let view = rng.gen(); @@ -217,7 +218,7 @@ fn test_commit_qc() { for key in &setup1.keys[0..i] { qc.add(&key.sign_msg(qc.message.clone()), &setup1.genesis); } - let expected_weight = i as u64 * ValidatorCommittee::MAX_WEIGHT / 6; + let expected_weight = i as u64 * validator_weight / 6; if expected_weight >= setup1.genesis.validators.threshold() { qc.verify(&setup1.genesis).unwrap(); } else { @@ -263,7 +264,7 @@ fn test_prepare_qc() { &setup1.genesis, ); } - let expected_weight = n as u64 * ValidatorCommittee::MAX_WEIGHT / 6; + let expected_weight = n as u64 * setup1.genesis.validators.max_weight() / 6; if expected_weight >= setup1.genesis.validators.threshold() { qc.verify(&setup1.genesis).unwrap(); } else {