Skip to content

Commit

Permalink
fix: saturating subtraction for weights
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaMasych committed Oct 17, 2024
1 parent e1d6fa7 commit 217a2d3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/party.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ impl<V: Value, VS: ValueSelector<V>> Party<V, VS> {
self.cfg.party_weights[routing.sender as usize] as u128;

let self_weight = self.cfg.party_weights[self.id as usize] as u128;
if self.messages_1b_weight >= self.cfg.threshold - self_weight {
if self.messages_1b_weight >= self.cfg.threshold.saturating_sub(self_weight) {
self.status = PartyStatus::Passed1b;
}
}
Expand Down Expand Up @@ -576,7 +576,9 @@ impl<V: Value, VS: ValueSelector<V>> Party<V, VS> {
);

let self_weight = self.cfg.party_weights[self.id as usize] as u128;
if self.messages_2av_state.get_weight() >= self.cfg.threshold - self_weight {
if self.messages_2av_state.get_weight()
>= self.cfg.threshold.saturating_sub(self_weight)
{
self.status = PartyStatus::Passed2av;
}
}
Expand Down Expand Up @@ -609,7 +611,9 @@ impl<V: Value, VS: ValueSelector<V>> Party<V, VS> {
);

let self_weight = self.cfg.party_weights[self.id as usize] as u128;
if self.messages_2b_state.get_weight() >= self.cfg.threshold - self_weight {
if self.messages_2b_state.get_weight()
>= self.cfg.threshold.saturating_sub(self_weight)
{
self.status = PartyStatus::Passed2b;
}
}
Expand Down
16 changes: 15 additions & 1 deletion tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ async fn test_ballot_many_parties() {
analyze_ballot(results);
}

#[ignore = "failing for now"]
#[tokio::test]
async fn test_ballot_max_weight() {
let weights = vec![u64::MAX, 1];
Expand All @@ -301,3 +300,18 @@ async fn test_ballot_max_weight() {

analyze_ballot(results);
}

#[tokio::test]
async fn test_ballot_weights_underflow() {
let weights = vec![100, 1, 2, 3, 4];
let threshold = BPConConfig::compute_bft_threshold(weights.clone());
let cfg = BPConConfig::with_default_timeouts(weights, threshold);

let (parties, receivers, senders) = create_parties(cfg);
let ballot_tasks = launch_parties(parties);
let p2p_task = propagate_p2p(receivers, senders);
let results = await_results(ballot_tasks).await;
p2p_task.abort();

analyze_ballot(results);
}

0 comments on commit 217a2d3

Please sign in to comment.