From 152283ec9cdfb58969e15f2026c238f041133cdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20Dimitroff=20H=C3=B3di?= Date: Mon, 25 Mar 2024 13:04:00 -0300 Subject: [PATCH] Changed weight type to u64 and several clippy corrections --- node/actors/bft/src/leader/replica_commit.rs | 7 ++----- node/actors/bft/src/testonly/ut_harness.rs | 8 ++++---- node/libs/roles/src/proto/validator.proto | 2 +- .../roles/src/validator/messages/consensus.rs | 19 +++++++++---------- .../src/validator/messages/leader_commit.rs | 6 +++--- .../src/validator/messages/leader_prepare.rs | 6 +++--- node/libs/roles/src/validator/testonly.rs | 4 ++-- node/libs/roles/src/validator/tests.rs | 4 ++-- 8 files changed, 26 insertions(+), 30 deletions(-) diff --git a/node/actors/bft/src/leader/replica_commit.rs b/node/actors/bft/src/leader/replica_commit.rs index 3e152d01..d22ec8a6 100644 --- a/node/actors/bft/src/leader/replica_commit.rs +++ b/node/actors/bft/src/leader/replica_commit.rs @@ -134,9 +134,7 @@ impl StateMachine { // We check validators weight from current messages, stored by proposal let weight_before = self.config.genesis().validators.weight_from_msgs( cache_entry - .clone() - .iter() - .map(|(_, m)| m) + .values() .filter(|m| m.msg.proposal == message.proposal) .collect::>() .as_slice(), @@ -148,8 +146,7 @@ impl StateMachine { // Now we check if we have enough weight to continue. let weight = self.config.genesis().validators.weight_from_msgs( cache_entry - .iter() - .map(|(_, m)| m) + .values() .filter(|m| m.msg.proposal == message.proposal) .collect::>() .as_slice(), diff --git a/node/actors/bft/src/testonly/ut_harness.rs b/node/actors/bft/src/testonly/ut_harness.rs index 9b26535e..f3c7be12 100644 --- a/node/actors/bft/src/testonly/ut_harness.rs +++ b/node/actors/bft/src/testonly/ut_harness.rs @@ -224,7 +224,7 @@ impl UTHarness { ctx: &ctx::Ctx, msg: ReplicaPrepare, ) -> Signed { - let expected_validator_weight = ValidatorCommittee::MAX_WEIGHT / self.keys.len(); + let expected_validator_weight = ValidatorCommittee::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(); @@ -232,7 +232,7 @@ impl UTHarness { for (i, msg) in msgs.into_iter().enumerate() { let res = self.process_replica_prepare(ctx, msg).await; match ( - (i + 1) * expected_validator_weight < want_threshold, + (i + 1) as u64 * expected_validator_weight < want_threshold, first_match, ) { (true, _) => assert!(res.unwrap().is_none()), @@ -260,7 +260,7 @@ impl UTHarness { ctx: &ctx::Ctx, msg: ReplicaCommit, ) -> Signed { - let expected_validator_weight = ValidatorCommittee::MAX_WEIGHT / self.keys.len(); + let expected_validator_weight = ValidatorCommittee::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() { @@ -268,7 +268,7 @@ impl UTHarness { .leader .process_replica_commit(ctx, key.sign_msg(msg.clone())); match ( - (i + 1) * expected_validator_weight < want_threshold, + (i + 1) as u64 * expected_validator_weight < want_threshold, first_match, ) { (true, _) => res.unwrap(), diff --git a/node/libs/roles/src/proto/validator.proto b/node/libs/roles/src/proto/validator.proto index 66050009..29de5b7b 100644 --- a/node/libs/roles/src/proto/validator.proto +++ b/node/libs/roles/src/proto/validator.proto @@ -169,6 +169,6 @@ message AggregateSignature { message WeightedValidator { optional PublicKey key = 1; // required - optional uint32 weight = 2; // required + optional uint64 weight = 2; // required } \ No newline at end of file diff --git a/node/libs/roles/src/validator/messages/consensus.rs b/node/libs/roles/src/validator/messages/consensus.rs index 8223ca34..19b8859d 100644 --- a/node/libs/roles/src/validator/messages/consensus.rs +++ b/node/libs/roles/src/validator/messages/consensus.rs @@ -1,7 +1,6 @@ //! Messages related to the consensus protocol. use super::{BlockNumber, LeaderCommit, LeaderPrepare, Msg, ReplicaCommit, ReplicaPrepare}; use crate::validator; -use anyhow::Context; use bit_vec::BitVec; use std::{ collections::{BTreeMap, BTreeSet}, @@ -79,17 +78,17 @@ impl Default for Fork { pub struct ValidatorCommittee { vec: Vec, indexes: BTreeMap, - weights: Vec, + weights: Vec, } impl ValidatorCommittee { /// Maximum validator weight /// 100.00% - pub const MAX_WEIGHT: usize = 10000; + pub const MAX_WEIGHT: u64 = 10000; /// Required weight to verify signatures. /// Currently 80.00% - const THRESHOLD: usize = 8000; + const THRESHOLD: u64 = 8000; /// Creates a new ValidatorCommittee from a list of validator public keys. pub fn new(validators: impl IntoIterator) -> anyhow::Result { @@ -102,7 +101,7 @@ impl ValidatorCommittee { ); weights.insert( validator.key, - validator.weight.try_into().context("weight")?, + validator.weight, ); } anyhow::ensure!( @@ -130,7 +129,7 @@ impl ValidatorCommittee { pub fn weighted_validators_iter(&self) -> impl Iterator + '_ { zip(&self.vec, &self.weights).map(|(key, weight)| WeightedValidator { key: key.clone(), - weight: *weight as u32, + weight: *weight, }) } @@ -162,7 +161,7 @@ impl ValidatorCommittee { } /// Signature threshold for this validator committee. - pub fn threshold(&self) -> usize { + pub fn threshold(&self) -> u64 { Self::THRESHOLD } @@ -172,7 +171,7 @@ impl ValidatorCommittee { } /// Compute the sum of signers weights. - pub fn weight_from_signers(&self, signers: Signers) -> usize { + pub fn weight_from_signers(&self, signers: Signers) -> u64 { self.weights .iter() .enumerate() @@ -182,7 +181,7 @@ impl ValidatorCommittee { } /// Compute the sum of signers weights. - pub fn weight_from_msgs>(&self, signed: &[&validator::Signed]) -> usize { + pub fn weight_from_msgs>(&self, signed: &[&validator::Signed]) -> u64 { signed .iter() .map(|s| { @@ -431,5 +430,5 @@ pub struct WeightedValidator { /// Validator key pub key: validator::PublicKey, /// Validator weight inside the ValidatorCommittee. - pub weight: u32, + pub weight: u64, } diff --git a/node/libs/roles/src/validator/messages/leader_commit.rs b/node/libs/roles/src/validator/messages/leader_commit.rs index cfc2924b..91f7d40d 100644 --- a/node/libs/roles/src/validator/messages/leader_commit.rs +++ b/node/libs/roles/src/validator/messages/leader_commit.rs @@ -45,9 +45,9 @@ pub enum CommitQCVerifyError { #[error("Signers have not reached wanted weight: got {got}, want {want}")] WeightNotReached { /// Got weight. - got: usize, + got: u64, /// Want weight. - want: usize, + want: u64, }, /// Bad signature. #[error("bad signature: {0:#}")] @@ -100,7 +100,7 @@ impl CommitQC { return Err(Error::BadSignersSet); } - // Verify the signer's weights is enough. + // Verify the signers' weight is enough. let weight = genesis.validators.weight_from_signers(self.signers.clone()); let threshold = genesis.validators.threshold(); if weight < threshold { diff --git a/node/libs/roles/src/validator/messages/leader_prepare.rs b/node/libs/roles/src/validator/messages/leader_prepare.rs index 488340ff..af722dc8 100644 --- a/node/libs/roles/src/validator/messages/leader_prepare.rs +++ b/node/libs/roles/src/validator/messages/leader_prepare.rs @@ -34,9 +34,9 @@ pub enum PrepareQCVerifyError { #[error("Signers have not reached wanted weight: got {got}, want {want}")] WeightNotReached { /// Got weight. - got: usize, + got: u64, /// Want weight. - want: usize, + want: u64, }, /// Bad signature. #[error("bad signature: {0:#}")] @@ -127,7 +127,7 @@ impl PrepareQC { sum |= signers; } - // Verify the signer's weights is enough. + // Verify the signers' weight is enough. let weight = genesis.validators.weight_from_signers(sum.clone()); let threshold = genesis.validators.threshold(); if weight < threshold { diff --git a/node/libs/roles/src/validator/testonly.rs b/node/libs/roles/src/validator/testonly.rs index b2ce90a3..0934de53 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 u32; + let weight = ValidatorCommittee::MAX_WEIGHT / validators as u64; let genesis = Genesis { validators: ValidatorCommittee::new(keys.iter().map(|k| WeightedValidator { key: k.public(), @@ -303,7 +303,7 @@ impl Distribution for Standard { let count = rng.gen_range(1..11); let public_keys = (0..count).map(|_| WeightedValidator { key: rng.gen(), - weight: ValidatorCommittee::MAX_WEIGHT as u32, + weight: ValidatorCommittee::MAX_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 a78315c2..fcb2a33f 100644 --- a/node/libs/roles/src/validator/tests.rs +++ b/node/libs/roles/src/validator/tests.rs @@ -217,7 +217,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 * ValidatorCommittee::MAX_WEIGHT / 6; + let expected_weight = i as u64 * ValidatorCommittee::MAX_WEIGHT / 6; if expected_weight >= setup1.genesis.validators.threshold() { qc.verify(&setup1.genesis).unwrap(); } else { @@ -263,7 +263,7 @@ fn test_prepare_qc() { &setup1.genesis, ); } - let expected_weight = n * ValidatorCommittee::MAX_WEIGHT / 6; + let expected_weight = n as u64 * ValidatorCommittee::MAX_WEIGHT / 6; if expected_weight >= setup1.genesis.validators.threshold() { qc.verify(&setup1.genesis).unwrap(); } else {