Skip to content

Commit

Permalink
Removed unnecessary constants
Browse files Browse the repository at this point in the history
  • Loading branch information
ElFantasma committed Mar 25, 2024
1 parent 152283e commit 94e4ef4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
8 changes: 5 additions & 3 deletions node/actors/bft/src/testonly/ut_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -224,7 +224,8 @@ impl UTHarness {
ctx: &ctx::Ctx,
msg: ReplicaPrepare,
) -> Signed<LeaderPrepare> {
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();
Expand Down Expand Up @@ -260,7 +261,8 @@ impl UTHarness {
ctx: &ctx::Ctx,
msg: ReplicaCommit,
) -> Signed<LeaderCommit> {
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() {
Expand Down
14 changes: 6 additions & 8 deletions node/libs/roles/src/validator/messages/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(),
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions node/libs/roles/src/validator/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SecretKey> = (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(),
Expand Down Expand Up @@ -301,9 +301,10 @@ impl Distribution<Signers> for Standard {
impl Distribution<ValidatorCommittee> for Standard {
fn sample<R: Rng + ?Sized>(&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()
}
Expand Down
5 changes: 3 additions & 2 deletions node/libs/roles/src/validator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,15 @@ 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();
let mut qc = CommitQC::new(make_replica_commit(rng, view, &setup1), &setup1.genesis);
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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 94e4ef4

Please sign in to comment.