Skip to content

Commit

Permalink
Fixing check for weight before adding new signature in replica commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ElFantasma committed Mar 22, 2024
1 parent 5ff923d commit 532a9aa
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
24 changes: 16 additions & 8 deletions node/actors/bft/src/leader/replica_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,15 @@ impl StateMachine {
.entry(message.view.number)
.or_default();

// We check validators weight from current messages
// TODO: this is wrong, we have to calculate previous weights by proposal
let previous_weight = self
.config
.genesis()
.validators
.weight_from_msgs(&cache_entry.values().collect());
// We check validators weight from current messages, stored by prFoposal
let mut by_proposal_before: HashMap<_, Vec<_>> = HashMap::new();
let entry_before = cache_entry.clone();
for msg in entry_before.values() {
by_proposal_before
.entry(msg.msg.proposal)
.or_default()
.push(msg);
}

// We store the message in our cache.
cache_entry.insert(author.clone(), signed_message.clone());
Expand All @@ -149,13 +151,19 @@ impl StateMachine {
by_proposal.entry(msg.msg.proposal).or_default().push(msg);
}
let threshold = self.config.genesis().validators.threshold();
let Some((_, replica_messages)) = by_proposal
let Some((proposal, _replica_messages)) = by_proposal
.into_iter()
.find(|(_, v)| self.config.genesis().validators.weight_from_msgs(v) >= threshold)
else {
return Ok(());
};

// Check that previous weight did not reach threshold
let previous_weight = self
.config
.genesis()
.validators
.weight_from_msgs(&by_proposal_before.entry(proposal).or_default());
// to ensure this is the first time the threshold has been reached
debug_assert!(previous_weight < threshold);

Expand Down
14 changes: 6 additions & 8 deletions node/actors/bft/src/leader/replica_prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,19 @@ impl StateMachine {
.or_default();

// We check validators weight from current messages
let previous_weight = self
let msgs_before: Vec<_> = entry.values().collect();
let weight_before = self
.config
.genesis()
.validators
.weight_from_msgs(&entry.values().collect());
.weight_from_msgs(&msgs_before);

// We store the message in our cache.
entry.insert(author.clone(), signed_message);

// Now we check if we have enough weight to continue.
let weight = self
.config
.genesis()
.validators
.weight_from_msgs(&entry.values().collect());
let msgs: Vec<_> = entry.values().collect();
let weight = self.config.genesis().validators.weight_from_msgs(&msgs);
let threshold = self.config.genesis().validators.threshold();
if weight < threshold {
return Ok(());
Expand All @@ -170,7 +168,7 @@ impl StateMachine {

// Check that previous weight did not reach threshold
// to ensure this is the first time the threshold has been reached
debug_assert!(previous_weight < threshold);
debug_assert!(weight_before < threshold);

// ----------- Update the state machine --------------

Expand Down
2 changes: 1 addition & 1 deletion node/libs/roles/src/validator/messages/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl ValidatorCommittee {
}

/// Compute the sum of signers weights.
pub fn weight_from_msgs<T: Variant<Msg>>(&self, signed: &Vec<&validator::Signed<T>>) -> usize {
pub fn weight_from_msgs<T: Variant<Msg>>(&self, signed: &[&validator::Signed<T>]) -> usize {
signed
.iter()
.map(|s| {
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 @@ -200,7 +200,7 @@ fn test_commit_qc() {
let ctx = ctx::test_root(&ctx::RealClock);
let rng = &mut ctx.rng();

// This will ceate equally weighted validators
// This will create equally weighted validators
let setup1 = Setup::new(rng, 6);
let setup2 = Setup::new(rng, 6);
let genesis3 = Genesis {
Expand Down Expand Up @@ -239,7 +239,7 @@ fn test_prepare_qc() {
let ctx = ctx::test_root(&ctx::RealClock);
let rng = &mut ctx.rng();

// This will ceate equally weighted validators
// This will create equally weighted validators
let setup1 = Setup::new(rng, 6);
let setup2 = Setup::new(rng, 6);
let genesis3 = Genesis {
Expand Down

0 comments on commit 532a9aa

Please sign in to comment.