Skip to content

Commit

Permalink
Simplified weights representation
Browse files Browse the repository at this point in the history
  • Loading branch information
ElFantasma committed Mar 15, 2024
1 parent 0707c46 commit 416517a
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions node/libs/roles/src/validator/messages/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use bit_vec::BitVec;
use std::{
collections::{BTreeMap, BTreeSet},
fmt,
iter::zip,
};
use zksync_consensus_crypto::{keccak256::Keccak256, ByteFmt, Text, TextFmt};
use zksync_consensus_utils::enum_util::{BadVariantError, Variant};
Expand Down Expand Up @@ -83,7 +84,7 @@ impl Default for Fork {
pub struct ValidatorCommittee {
vec: Vec<validator::PublicKey>,
indexes: BTreeMap<validator::PublicKey, usize>,
weights: BTreeMap<validator::PublicKey, usize>,
weights: Vec<usize>,
}

impl ValidatorCommittee {
Expand All @@ -107,8 +108,13 @@ impl ValidatorCommittee {
);
Ok(Self {
vec: set.iter().cloned().collect(),
indexes: set.into_iter().enumerate().map(|(i, pk)| (pk, i)).collect(),
weights,
indexes: set
.clone()
.into_iter()
.enumerate()
.map(|(i, pk)| (pk, i))
.collect(),
weights: set.iter().map(|pk| weights[pk]).collect(),
})
}

Expand All @@ -119,7 +125,7 @@ impl ValidatorCommittee {

/// Iterates over validators.
pub fn weighted_validators_iter(&self) -> impl Iterator<Item = WeightedValidator> + '_ {
self.weights.iter().map(|(key, weight)| WeightedValidator {
zip(&self.vec, &self.weights).map(|(key, weight)| WeightedValidator {
key: key.clone(),
weight: *weight as u32,
})
Expand Down Expand Up @@ -164,10 +170,12 @@ impl ValidatorCommittee {

/// Compute the sum of signers weights.
pub fn weight(&self, signers: Signers) -> usize {
self.iter()
self.weights
.iter()
.enumerate()
.filter(|(i, _)| signers.0[*i])
.fold(0, |acc, (_, pk)| acc + self.weights[pk])
.map(|(_, weight)| weight)
.sum()
}
}

Expand Down

0 comments on commit 416517a

Please sign in to comment.