Skip to main content

scp_kernel/
ballotutil.rs

1use crate::*;
2use std::cmp::Ordering;
3use std::collections::HashMap;
4
5#[derive(Debug, Clone, PartialEq)]
6pub struct Interval {
7    pub lo: thruput_scp::SCPCount,
8    pub hi: thruput_scp::SCPCount,
9}
10
11pub fn compare_ballots(
12    left: &thruput_scp::SCPBallot,
13    right: &thruput_scp::SCPBallot,
14) -> Ordering {
15    match left.counter.cmp(&right.counter) {
16        Ordering::Equal => left.value.cmp(&right.value),
17        other => other,
18    }
19}
20
21pub fn compare_opt_ballots(
22    left: &Option<thruput_scp::SCPBallot>,
23    right: &Option<thruput_scp::SCPBallot>,
24) -> Ordering {
25    match (left, right) {
26        (Some(b1), Some(b2)) => compare_ballots(b1, b2),
27        (Some(_), None) => Ordering::Greater,
28        (None, Some(_)) => Ordering::Less,
29        (None, None) => Ordering::Equal,
30    }
31}
32
33pub fn are_ballots_compatible(
34    left: &thruput_scp::SCPBallot,
35    right: &thruput_scp::SCPBallot,
36) -> bool {
37    left.value == right.value
38}
39
40pub fn are_ballots_less_and_incompatible(
41    left: &thruput_scp::SCPBallot,
42    right: &thruput_scp::SCPBallot,
43) -> bool {
44    (compare_ballots(left, right) != Ordering::Greater)
45        && !are_ballots_compatible(left, right)
46}
47
48pub fn are_ballots_less_and_compatible(
49    left: &thruput_scp::SCPBallot,
50    right: &thruput_scp::SCPBallot,
51) -> bool {
52    (compare_ballots(left, right) != Ordering::Greater)
53        && are_ballots_compatible(left, right)
54}
55
56pub fn has_v_blocking_subset_strictly_ahead_of<'a, C: Consensus, D: Driver>(
57    local: &LocalProto<'a, C, D>,
58    envelopes: &HashMap<thruput_scp::SCPNodeID, thruput_scp::SCPEnvelope>,
59    n: thruput_scp::SCPCount,
60) -> bool {
61    LocalProto::<C, D>::is_vblocking_envelopes(
62        local.get_qset(),
63        envelopes,
64        |st: &thruput_scp::SCPStatement| -> bool {
65            statement_ballot_counter(st) > n
66        },
67    )
68}
69
70pub fn statement_ballot_counter(
71    st: &thruput_scp::SCPStatement,
72) -> thruput_scp::SCPCount {
73    match &st.pledges {
74        thruput_scp::SCPStatement_pledges::SCP_ST_PREPARE { prepare } => {
75            prepare.ballot.counter
76        }
77        thruput_scp::SCPStatement_pledges::SCP_ST_CONFIRM { confirm } => {
78            confirm.ballot.counter
79        }
80        thruput_scp::SCPStatement_pledges::SCP_ST_EXTERNALIZE { .. } => {
81            u32::MAX
82        }
83        _ => panic!("bug"),
84    }
85}