consensus_core/
stake_aggregator.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::{collections::BTreeSet, marker::PhantomData};

use consensus_config::{AuthorityIndex, Committee, Stake};

pub(crate) trait CommitteeThreshold {
    fn is_threshold(committee: &Committee, amount: Stake) -> bool;
    fn threshold(committee: &Committee) -> Stake;
}

#[derive(Default)]
pub(crate) struct QuorumThreshold;

#[cfg(test)]
#[derive(Default)]
pub(crate) struct ValidityThreshold;

impl CommitteeThreshold for QuorumThreshold {
    fn is_threshold(committee: &Committee, amount: Stake) -> bool {
        committee.reached_quorum(amount)
    }
    fn threshold(committee: &Committee) -> Stake {
        committee.quorum_threshold()
    }
}

#[cfg(test)]
impl CommitteeThreshold for ValidityThreshold {
    fn is_threshold(committee: &Committee, amount: Stake) -> bool {
        committee.reached_validity(amount)
    }
    fn threshold(committee: &Committee) -> Stake {
        committee.validity_threshold()
    }
}

#[derive(Default)]
pub(crate) struct StakeAggregator<T> {
    votes: BTreeSet<AuthorityIndex>,
    stake: Stake,
    _phantom: PhantomData<T>,
}

impl<T: CommitteeThreshold> StakeAggregator<T> {
    pub(crate) fn new() -> Self {
        Self {
            votes: Default::default(),
            stake: 0,
            _phantom: Default::default(),
        }
    }

    /// Adds a vote for the specified authority index to the aggregator. It is guaranteed to count
    /// the vote only once for an authority. The method returns true when the required threshold has
    /// been reached.
    pub(crate) fn add(&mut self, vote: AuthorityIndex, committee: &Committee) -> bool {
        if self.votes.insert(vote) {
            self.stake += committee.stake(vote);
        }
        T::is_threshold(committee, self.stake)
    }

    /// Adds a vote for the specified authority index to the aggregator. It is guaranteed to count
    /// the vote only once for an authority.
    /// The method returns true when the vote comes from a new authority and is counted.
    pub(crate) fn add_unique(&mut self, vote: AuthorityIndex, committee: &Committee) -> bool {
        if self.votes.insert(vote) {
            self.stake += committee.stake(vote);
            return true;
        }
        false
    }

    pub(crate) fn stake(&self) -> Stake {
        self.stake
    }

    pub(crate) fn reached_threshold(&self, committee: &Committee) -> bool {
        T::is_threshold(committee, self.stake)
    }

    pub(crate) fn threshold(&self, committee: &Committee) -> Stake {
        T::threshold(committee)
    }

    pub(crate) fn clear(&mut self) {
        self.votes.clear();
        self.stake = 0;
    }
}

#[cfg(test)]
mod tests {
    use consensus_config::{local_committee_and_keys, AuthorityIndex};

    use super::*;

    #[test]
    fn test_aggregator_quorum_threshold() {
        let committee = local_committee_and_keys(0, vec![1, 1, 1, 1]).0;
        let mut aggregator = StakeAggregator::<QuorumThreshold>::new();

        assert!(!aggregator.add(AuthorityIndex::new_for_test(0), &committee));
        assert!(!aggregator.add(AuthorityIndex::new_for_test(1), &committee));
        assert!(aggregator.add(AuthorityIndex::new_for_test(2), &committee));
        assert!(aggregator.add(AuthorityIndex::new_for_test(3), &committee));
    }

    #[test]
    fn test_add_unique_quorum_threshold() {
        let committee = local_committee_and_keys(0, vec![1, 1, 1, 1]).0;
        let mut aggregator = StakeAggregator::<QuorumThreshold>::new();

        assert!(aggregator.add_unique(AuthorityIndex::new_for_test(0), &committee));
        assert!(!aggregator.reached_threshold(&committee));

        assert!(aggregator.add_unique(AuthorityIndex::new_for_test(1), &committee));
        assert!(!aggregator.reached_threshold(&committee));

        assert!(!aggregator.add_unique(AuthorityIndex::new_for_test(1), &committee));
        assert!(!aggregator.reached_threshold(&committee));

        assert!(aggregator.add_unique(AuthorityIndex::new_for_test(2), &committee));
        assert!(aggregator.reached_threshold(&committee));

        assert!(aggregator.add_unique(AuthorityIndex::new_for_test(3), &committee));
        assert!(aggregator.reached_threshold(&committee));
    }

    #[test]
    fn test_aggregator_validity_threshold() {
        let committee = local_committee_and_keys(0, vec![1, 1, 1, 1]).0;
        let mut aggregator = StakeAggregator::<ValidityThreshold>::new();

        assert!(!aggregator.add(AuthorityIndex::new_for_test(0), &committee));
        assert!(aggregator.add(AuthorityIndex::new_for_test(1), &committee));
    }

    #[test]
    fn test_aggregator_clear() {
        let committee = local_committee_and_keys(0, vec![1, 1, 1, 1]).0;
        let mut aggregator = StakeAggregator::<ValidityThreshold>::new();

        assert!(!aggregator.add(AuthorityIndex::new_for_test(0), &committee));
        assert!(aggregator.add(AuthorityIndex::new_for_test(1), &committee));

        // clear the aggregator
        aggregator.clear();

        // now add them again
        assert!(!aggregator.add(AuthorityIndex::new_for_test(0), &committee));
        assert!(aggregator.add(AuthorityIndex::new_for_test(1), &committee));
    }
}