Skip to content

Commit

Permalink
Changed weight type to u64 and several clippy corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
ElFantasma committed Mar 25, 2024
1 parent 77a3483 commit 152283e
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 30 deletions.
7 changes: 2 additions & 5 deletions node/actors/bft/src/leader/replica_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>()
.as_slice(),
Expand All @@ -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::<Vec<_>>()
.as_slice(),
Expand Down
8 changes: 4 additions & 4 deletions node/actors/bft/src/testonly/ut_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,15 @@ impl UTHarness {
ctx: &ctx::Ctx,
msg: ReplicaPrepare,
) -> Signed<LeaderPrepare> {
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();
let mut first_match = true;
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()),
Expand Down Expand Up @@ -260,15 +260,15 @@ impl UTHarness {
ctx: &ctx::Ctx,
msg: ReplicaCommit,
) -> Signed<LeaderCommit> {
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() {
let res = self
.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(),
Expand Down
2 changes: 1 addition & 1 deletion node/libs/roles/src/proto/validator.proto
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,6 @@ message AggregateSignature {

message WeightedValidator {
optional PublicKey key = 1; // required
optional uint32 weight = 2; // required
optional uint64 weight = 2; // required

}
19 changes: 9 additions & 10 deletions node/libs/roles/src/validator/messages/consensus.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -79,17 +78,17 @@ impl Default for Fork {
pub struct ValidatorCommittee {
vec: Vec<validator::PublicKey>,
indexes: BTreeMap<validator::PublicKey, usize>,
weights: Vec<usize>,
weights: Vec<u64>,
}

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<Item = WeightedValidator>) -> anyhow::Result<Self> {
Expand All @@ -102,7 +101,7 @@ impl ValidatorCommittee {
);
weights.insert(
validator.key,
validator.weight.try_into().context("weight")?,
validator.weight,
);
}
anyhow::ensure!(
Expand Down Expand Up @@ -130,7 +129,7 @@ impl ValidatorCommittee {
pub fn weighted_validators_iter(&self) -> impl Iterator<Item = WeightedValidator> + '_ {
zip(&self.vec, &self.weights).map(|(key, weight)| WeightedValidator {
key: key.clone(),
weight: *weight as u32,
weight: *weight,
})
}

Expand Down Expand Up @@ -162,7 +161,7 @@ impl ValidatorCommittee {
}

/// Signature threshold for this validator committee.
pub fn threshold(&self) -> usize {
pub fn threshold(&self) -> u64 {
Self::THRESHOLD
}

Expand All @@ -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()
Expand All @@ -182,7 +181,7 @@ impl ValidatorCommittee {
}

/// Compute the sum of signers weights.
pub fn weight_from_msgs<T: Variant<Msg>>(&self, signed: &[&validator::Signed<T>]) -> usize {
pub fn weight_from_msgs<T: Variant<Msg>>(&self, signed: &[&validator::Signed<T>]) -> u64 {
signed
.iter()
.map(|s| {
Expand Down Expand Up @@ -431,5 +430,5 @@ pub struct WeightedValidator {
/// Validator key
pub key: validator::PublicKey,
/// Validator weight inside the ValidatorCommittee.
pub weight: u32,
pub weight: u64,
}
6 changes: 3 additions & 3 deletions node/libs/roles/src/validator/messages/leader_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:#}")]
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions node/libs/roles/src/validator/messages/leader_prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:#}")]
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 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 u32;
let weight = ValidatorCommittee::MAX_WEIGHT / validators as u64;
let genesis = Genesis {
validators: ValidatorCommittee::new(keys.iter().map(|k| WeightedValidator {
key: k.public(),
Expand Down Expand Up @@ -303,7 +303,7 @@ impl Distribution<ValidatorCommittee> 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()
}
Expand Down
4 changes: 2 additions & 2 deletions node/libs/roles/src/validator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 152283e

Please sign in to comment.