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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::user::sortition;
use crate::user::stake::Stake;
use crate::util::pubkey::ConsensusPublicKey;

use num_bigint::BigInt;
use std::collections::BTreeMap;

pub const DUSK: u64 = 100_000_000;

#[derive(Clone, Debug)]
#[allow(unused)]
pub struct Member {
    /// Vector of pairs (stake and eligibility flag)
    stakes: Vec<(Stake, bool)>,
    pubkey_bls: ConsensusPublicKey,
}

impl Member {
    pub fn new(pubkey_bls: ConsensusPublicKey) -> Self {
        Self {
            stakes: vec![],
            pubkey_bls,
        }
    }

    pub fn public_key(&self) -> &ConsensusPublicKey {
        &self.pubkey_bls
    }

    // AddStake appends a stake to the stake set with eligible_flag=false.
    pub fn add_stake(&mut self, stake: Stake) {
        self.stakes.push((stake, false));
    }

    pub fn update_eligibility_flag(&mut self, round: u64) {
        for (stake, eligible) in self.stakes.iter_mut() {
            *eligible = stake.eligible_since <= round;
        }
    }

    pub fn subtract_from_stake(&mut self, value: u64) -> u64 {
        for (stake, _) in self.stakes.iter_mut() {
            let stake_val = stake.intermediate_value;
            if stake_val > 0 {
                if stake_val < value {
                    stake.intermediate_value = 0;
                    return stake_val;
                }
                stake.intermediate_value -= value;
                return value;
            }
        }

        0
    }

    pub fn restore_intermediate_value(&mut self) {
        for stake in self.stakes.iter_mut() {
            stake.0.restore_intermediate_value();
        }
    }

    fn get_total_eligible_stake(&self) -> BigInt {
        let mut total: u64 = 0;
        for (stake, eligible) in self.stakes.iter() {
            if *eligible {
                total += stake.intermediate_value;
            }
        }

        BigInt::from(total)
    }
}

#[derive(Clone, Default, Debug)]
pub struct Provisioners {
    members: BTreeMap<ConsensusPublicKey, Member>,
}

impl Provisioners {
    pub fn new() -> Self {
        Self {
            members: BTreeMap::new(),
        }
    }

    /// Adds a provisioner with stake.
    ///
    /// It appends the stake if the given provisioner already exists.
    pub fn add_member_with_stake(
        &mut self,
        pubkey_bls: ConsensusPublicKey,
        stake: Stake,
    ) {
        self.members
            .entry(pubkey_bls)
            .or_insert_with_key(|key| Member::new(key.clone()))
            .add_stake(stake);
    }

    /// Adds a new member with reward=0 and elibile_since=0.
    ///
    /// Useful for implementing unit tests.
    pub fn add_member_with_value(
        &mut self,
        pubkey_bls: ConsensusPublicKey,
        value: u64,
    ) {
        self.add_member_with_stake(pubkey_bls, Stake::new(value, 0, 0));
    }

    /// Turns on/off elibility flag of stakes for a given round.
    pub fn update_eligibility_flag(&mut self, round: u64) {
        self.members
            .values_mut()
            .for_each(|m| m.update_eligibility_flag(round));
    }

    /// Returns number of provisioners that owns at least one eligibile stake.
    pub fn get_eligible_size(&self, max_size: usize) -> usize {
        self.members
            .iter()
            .filter(|(_, m)| m.stakes.iter().any(|(_, elegible)| *elegible))
            .take(max_size)
            .count()
    }

    /// Returns a member of Provisioner list by public key.
    pub fn get_member(&self, key: &ConsensusPublicKey) -> Option<&Member> {
        self.members.get(key)
    }

    /// Runs the deterministic sortition algorithm which determines the committee members for a given round, step and seed.
    ///
    /// Returns a vector of provisioners public keys.
    pub fn create_committee(
        &mut self,
        cfg: &sortition::Config,
    ) -> Vec<ConsensusPublicKey> {
        let mut committee: Vec<ConsensusPublicKey> = vec![];
        let committee_size = self.get_eligible_size(cfg.max_committee_size);

        // Restore intermediate value of all stakes.
        for (_, member) in self.members.iter_mut() {
            member.restore_intermediate_value();
        }

        let mut total_amount_stake =
            BigInt::from(self.calc_total_eligible_weight());

        let mut counter: u32 = 0;
        loop {
            if total_amount_stake.eq(&BigInt::from(0))
                || committee.len() == committee_size
            {
                break;
            }

            // 1. Compute n ← H(seed ∣∣ round ∣∣ step ∣∣ counter)
            let hash = sortition::create_sortition_hash(cfg, counter);
            counter += 1;

            // 2. Compute d ← n mod s
            let score =
                sortition::generate_sortition_score(hash, &total_amount_stake);

            // NB: The public key can be extracted multiple times per committee.
            match self.extract_and_subtract_member(score) {
                Some((pk, value)) => {
                    // append the public key to the committee set.
                    committee.push(pk.clone());

                    let subtracted_stake = value;
                    if total_amount_stake > subtracted_stake {
                        total_amount_stake -= subtracted_stake;
                    } else {
                        total_amount_stake = BigInt::from(0);
                    }
                }
                None => panic!("invalid score"),
            }
        }

        committee
    }

    /// Sums up the total weight of all **eligible** stakes
    fn calc_total_eligible_weight(&self) -> u64 {
        self.members
            .values()
            .flat_map(|m| &m.stakes)
            .filter_map(|(stake, eligible)| {
                eligible.then(|| stake.intermediate_value)
            })
            .sum()
    }

    fn extract_and_subtract_member(
        &mut self,
        mut score: BigInt,
    ) -> Option<(ConsensusPublicKey, BigInt)> {
        if self.members.is_empty() {
            return None;
        }

        loop {
            for member in self.members.values_mut() {
                let total_stake = member.get_total_eligible_stake();
                if total_stake >= score {
                    // Subtract 1 DUSK from the value extracted and rebalance accordingly.
                    let subtracted_stake =
                        BigInt::from(member.subtract_from_stake(DUSK));

                    return Some((
                        member.public_key().clone(),
                        subtracted_stake,
                    ));
                }

                score -= total_stake;
            }
        }
    }
}