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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
use std::collections::HashMap;

use bit_vec::BitVec;
use derive_more::Display;
use prime_tools::get_primes_less_than_x;

use crate::error::ConsensusError;
use crate::types::{Address, Node};
use crate::utils::rand_proposer::get_random_proposer_index;
use crate::ConsensusResult;

/// Authority manage is an extensional data structure of authority list which means
/// `Vec<Node>`. It transforms the information in `Node` struct into a more suitable data structure
/// according to its usage scene. The vote weight need look up by address frequently, therefore,
/// address with vote weight saved in a `HashMap`.
#[derive(Clone, Debug, Display, PartialEq, Eq)]
#[display(fmt = "Authority List {:?}", address)]
pub struct AuthorityManage {
    address: Vec<Address>,
    propose_weights: Vec<u64>,
    vote_weight_map: HashMap<Address, u32>,
    propose_weight_sum: u64,
    vote_weight_sum: u64,
}

impl AuthorityManage {
    /// Create a new height authority manage.
    pub fn new() -> Self {
        AuthorityManage {
            address: Vec::new(),
            propose_weights: Vec::new(),
            vote_weight_map: HashMap::new(),
            propose_weight_sum: 0u64,
            vote_weight_sum: 0u64,
        }
    }

    /// Update the height authority manage by a new authority list.
    pub fn update(&mut self, authority_list: &mut [Node]) {
        self.flush();
        authority_list.sort();

        for node in authority_list.iter_mut() {
            let propose_weight = u64::from(node.propose_weight);
            let vote_weight = node.vote_weight;

            self.address.push(node.address.clone());
            self.propose_weights.push(propose_weight);
            self.vote_weight_map
                .insert(node.address.clone(), vote_weight);
            self.propose_weight_sum += propose_weight;
            self.vote_weight_sum += u64::from(vote_weight);
        }
    }

    /// Get a vote weight of the node.
    pub fn get_vote_weight(&self, addr: &Address) -> ConsensusResult<&u32> {
        self.vote_weight_map
            .get(addr)
            .ok_or(ConsensusError::InvalidAddress)
    }

    /// Get the proposer address by a given seed.
    pub fn get_proposer(&self, height: u64, round: u64) -> ConsensusResult<Address> {
        let index = if cfg!(feature = "random_leader") {
            get_random_proposer_index(
                height + round,
                &self.propose_weights,
                self.propose_weight_sum,
            )
        } else {
            rotation_leader_index(height, round, self.address.len())
        };

        if let Some(addr) = self.address.get(index) {
            return Ok(addr.to_owned());
        }
        Err(ConsensusError::Other(
            "The address list mismatch propose weight list".to_string(),
        ))
    }

    /// Calculate whether the sum of vote weights from bitmap is above 2/3.
    pub fn is_above_threshold(&self, bitmap: &[u8]) -> ConsensusResult<bool> {
        let bitmap = BitVec::from_bytes(bitmap);
        let mut acc = 0u64;

        for node in bitmap.iter().zip(self.address.iter()) {
            if node.0 {
                if let Some(weight) = self.vote_weight_map.get(node.1) {
                    acc += u64::from(*weight);
                } else {
                    return Err(ConsensusError::Other(format!(
                        "Lose {:?} vote weight",
                        node.1.clone()
                    )));
                }
            }
        }

        Ok(acc * 3 > self.vote_weight_sum * 2)
    }

    pub fn get_voters(&self, bitmap: &[u8]) -> ConsensusResult<Vec<Address>> {
        let bitmap = BitVec::from_bytes(bitmap);
        let voters = bitmap
            .iter()
            .zip(self.address.iter())
            .filter(|node| node.0)
            .map(|node| node.1.clone())
            .collect::<Vec<_>>();
        Ok(voters)
    }

    /// If the given address is in the current authority list.
    pub fn contains(&self, address: &Address) -> bool {
        self.address.contains(address)
    }

    /// Get the sum of the vote weights in the current height.
    pub fn get_vote_weight_sum(&self) -> u64 {
        self.vote_weight_sum
    }

    /// Clear the HeightAuthorityManage, removing all values.
    pub fn flush(&mut self) {
        self.address.clear();
        self.propose_weights.clear();
        self.vote_weight_map.clear();
        self.propose_weight_sum = 0;
        self.vote_weight_sum = 0;
    }

    /// Get the length of the current authority list.
    pub fn len(&self) -> usize {
        self.address.len()
    }

    pub fn get_address_ref(&self) -> &Vec<Address> {
        &self.address
    }
}

/// Give the validators list and bitmap, returns the activated validators, the authority list MUST
/// be sorted
pub fn extract_voters(
    authority_list: &mut [Node],
    address_bitmap: &bytes::Bytes,
) -> ConsensusResult<Vec<Address>> {
    authority_list.sort();
    let bitmap = BitVec::from_bytes(address_bitmap);
    let voters: Vec<Address> = bitmap
        .iter()
        .zip(authority_list.iter())
        .filter(|pair| pair.0) //the bitmap must hit
        .map(|pair| pair.1.address.clone()) //get the corresponding address
        .collect::<Vec<_>>();
    Ok(voters)
}

/// Get the leader address of the height and the round, the authority list MUST be sorted.
pub fn get_leader(height: u64, round: u64, mut authority_list: Vec<Node>) -> Address {
    authority_list.sort();
    let mut weight_sum = 0;
    let mut propose_weights = Vec::new();
    for node in authority_list.iter() {
        weight_sum += node.propose_weight;
        propose_weights.push(node.propose_weight as u64);
    }

    let index = if cfg!(feature = "random_leader") {
        get_random_proposer_index(height + round, &propose_weights, weight_sum as u64)
    } else {
        rotation_leader_index(height, round, authority_list.len())
    };

    authority_list[index].address.clone()
}

fn rotation_leader_index(height: u64, round: u64, authority_len: usize) -> usize {
    let len = authority_len as u32;
    let prime_num = *get_primes_less_than_x(len).last().unwrap_or(&1) as u64;
    let res = (height * prime_num + round) % (len as u64);
    res as usize
}

#[cfg(test)]
mod test {
    use bit_vec::BitVec;
    use bytes::Bytes;
    use rand::random;

    use crate::error::ConsensusError;
    use crate::extract_voters;
    use crate::types::{Address, Node};
    use crate::utils::auth_manage::AuthorityManage;

    fn gen_address() -> Address {
        Address::from((0..32).map(|_| random::<u8>()).collect::<Vec<_>>())
    }

    fn gen_auth_list(len: usize) -> Vec<Node> {
        if len == 0 {
            return vec![];
        }

        let mut authority_list = Vec::new();
        for _ in 0..len {
            authority_list.push(gen_node(gen_address(), random::<u32>(), random::<u32>()));
        }
        authority_list
    }

    fn gen_node(addr: Address, propose_weight: u32, vote_weight: u32) -> Node {
        let mut node = Node::new(addr);
        node.set_propose_weight(propose_weight);
        node.set_vote_weight(vote_weight);
        node
    }

    fn gen_bitmap(len: usize, nbits: Vec<usize>) -> BitVec {
        let mut bv = BitVec::from_elem(len, false);
        for n in nbits.into_iter() {
            bv.set(n, true);
        }
        bv
    }

    #[test]
    fn test_vote_weight() {
        let mut authority_list = gen_auth_list(0);
        let mut authority_manage = AuthorityManage::new();
        authority_manage.update(&mut authority_list);

        for node in authority_list.iter() {
            assert_eq!(
                authority_manage.get_vote_weight(&node.address),
                Err(ConsensusError::InvalidAddress)
            );
        }

        let mut auth_len = random::<u8>();
        while auth_len == 0 {
            auth_len = random::<u8>();
        }
        authority_manage.update(&mut gen_auth_list(auth_len as usize));

        for node in authority_list.iter() {
            assert_eq!(
                *authority_manage.get_vote_weight(&node.address).unwrap(),
                node.propose_weight
            );
        }
    }

    #[test]
    fn test_update() {
        let mut authority_list = gen_auth_list(random::<u8>() as usize);
        let mut auth_manage = AuthorityManage::new();
        auth_manage.update(&mut authority_list);
        assert_eq!(
            auth_manage.address,
            authority_list
                .into_iter()
                .map(|node| node.address)
                .collect::<Vec<Address>>()
        );
    }

    #[test]
    fn test_vote_threshold() {
        let mut authority_list = vec![
            gen_node(gen_address(), 1u32, 1u32),
            gen_node(gen_address(), 1u32, 1u32),
            gen_node(gen_address(), 1u32, 1u32),
            gen_node(gen_address(), 1u32, 1u32),
        ];
        authority_list.sort();
        let mut authority = AuthorityManage::new();
        authority.update(&mut authority_list);

        for i in 0..4 {
            let bit_map = gen_bitmap(4, vec![i]);
            let res = authority.is_above_threshold(Bytes::from(bit_map.to_bytes()).as_ref());
            assert!(!res.unwrap())
        }

        let tmp = vec![vec![1, 2], vec![1, 3], vec![2, 3], vec![0, 1], vec![0, 2]];
        for i in tmp.into_iter() {
            let bit_map = gen_bitmap(4, i);
            let res = authority.is_above_threshold(Bytes::from(bit_map.to_bytes()).as_ref());
            assert!(!res.unwrap())
        }

        let tmp = vec![vec![0, 1, 2], vec![0, 1, 3], vec![1, 2, 3]];
        for i in tmp.into_iter() {
            let bit_map = gen_bitmap(4, i);
            let res = authority.is_above_threshold(Bytes::from(bit_map.to_bytes()).as_ref());
            assert!(res.unwrap())
        }

        let bit_map = gen_bitmap(4, vec![0, 1, 2, 3]);
        let res = authority.is_above_threshold(Bytes::from(bit_map.to_bytes()).as_ref());
        assert!(res.unwrap())
    }

    #[test]
    fn test_bitmap() {
        let len = random::<u8>() as usize;
        let bitmap = (0..len).map(|_| random::<bool>()).collect::<Vec<_>>();
        let mut bv = BitVec::from_elem(len, false);
        for (index, is_vote) in bitmap.iter().enumerate() {
            if *is_vote {
                bv.set(index, true);
            }
        }

        let tmp = Bytes::from(bv.to_bytes());
        let output = BitVec::from_bytes(tmp.as_ref());

        for item in output.iter().zip(bitmap.iter()) {
            assert_eq!(item.0, *item.1);
        }
    }

    #[test]
    fn test_get_voters() {
        let auth_list = (0..4).map(|_| gen_address()).collect::<Vec<_>>();
        let bitmap = BitVec::from_bytes(&[0b1010_0000]);
        let voters = bitmap
            .iter()
            .zip(auth_list.iter())
            .filter(|node| node.0)
            .map(|node| node.1.clone())
            .collect::<Vec<_>>();

        assert_eq!(voters.len(), 2);
        assert_eq!(voters[0], auth_list[0]);
        assert_eq!(voters[1], auth_list[2]);
    }

    #[test]
    fn test_poll_leader() {
        let mut authority_list = vec![
            gen_node(gen_address(), 1u32, 1u32),
            gen_node(gen_address(), 1u32, 1u32),
            gen_node(gen_address(), 1u32, 1u32),
            gen_node(gen_address(), 1u32, 1u32),
        ];
        authority_list.sort();
        let mut authority = AuthorityManage::new();
        authority.update(&mut authority_list);

        assert_eq!(
            authority.get_proposer(1, 0).unwrap(),
            authority_list[3].address
        );
        assert_eq!(
            authority.get_proposer(1, 1).unwrap(),
            authority_list[0].address
        );
        assert_eq!(
            authority.get_proposer(2, 0).unwrap(),
            authority_list[2].address
        );
        assert_eq!(
            authority.get_proposer(2, 2).unwrap(),
            authority_list[0].address
        );
        assert_eq!(
            authority.get_proposer(3, 0).unwrap(),
            authority_list[1].address
        );
        assert_eq!(
            authority.get_proposer(3, 1).unwrap(),
            authority_list[2].address
        );
    }

    #[test]
    fn test_extract_voters() {
        let mut auth_list = gen_auth_list(10);
        let mut origin_auth_list = auth_list.clone();
        origin_auth_list.sort();
        let bit_map = gen_bitmap(10, vec![0, 1, 2]);
        let res = extract_voters(&mut auth_list, &Bytes::from(bit_map.to_bytes())).unwrap();

        assert_eq!(res.len(), 3);

        for (index, address) in res.iter().enumerate() {
            assert_eq!(
                origin_auth_list.get(index).unwrap().address,
                address.clone()
            );
        }
    }
}