diem_types/
reward_distribution_event.rs

1// Copyright 2021 Conflux Foundation. All rights reserved.
2// Conflux is free software and distributed under GNU General Public License.
3// See http://www.gnu.org/licenses/
4
5use std::collections::BTreeMap;
6
7use serde::{Deserialize, Serialize};
8
9use cfx_types::H256;
10
11use crate::term_state::{
12    bonus_vote_points, leader_points, COMMITTEE_POINTS, ELECTION_POINTS,
13};
14
15#[derive(Clone, Serialize, Deserialize, Default, Debug, Eq, PartialEq)]
16pub struct VoteCount {
17    // The number of rounds that the node becomes the leader.
18    pub leader_count: u32,
19    // The total number of votes that the node includes as a leader.
20    pub included_vote_count: u64,
21    // Total vote
22    pub total_votes: u64,
23    // The total number of votes that the node signs in the committed QCs
24    // within the term.
25    pub vote_count: u64,
26}
27
28impl VoteCount {
29    pub fn reward_points(&self, view: u64) -> u64 {
30        if self.vote_count == 0 {
31            return 0;
32        }
33        self.leader_count as u64 * leader_points(view)
34            + self.included_vote_count * bonus_vote_points(view)
35            + self.total_votes * COMMITTEE_POINTS
36    }
37}
38
39#[derive(Clone, Serialize, Deserialize, Default, Debug, Eq, PartialEq)]
40pub struct RewardDistributionEventV2 {
41    pub candidates: BTreeMap<H256, u64>,
42    pub elected: BTreeMap<H256, VoteCount>,
43    pub view: u64,
44}
45
46impl RewardDistributionEventV2 {
47    pub fn rewards(&self) -> impl Iterator<Item = (&H256, u64)> {
48        let view = self.view;
49        let committee_rewards = self
50            .elected
51            .iter()
52            .map(move |(id, vote_count)| (id, vote_count.reward_points(view)));
53        let participate_rewards = self
54            .candidates
55            .iter()
56            .map(|(id, cnt)| (id, ELECTION_POINTS * cnt));
57        committee_rewards.chain(participate_rewards)
58    }
59}
60
61#[derive(Clone, Serialize, Deserialize, Default, Debug, Eq, PartialEq)]
62pub struct RewardDistributionEventV1 {
63    pub candidates: BTreeMap<H256, u64>,
64    pub elected: BTreeMap<H256, VoteCount>,
65}
66
67impl From<RewardDistributionEventV1> for RewardDistributionEventV2 {
68    fn from(value: RewardDistributionEventV1) -> Self {
69        Self {
70            candidates: value.candidates,
71            elected: value.elected,
72            view: 0,
73        }
74    }
75}