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
use crate::ledger::token_distribution::TokenDistribution;
use crate::{
    account,
    certificate::{TallyProof, VoteAction, VoteCast, VotePlan, VotePlanId, VoteTally},
    date::BlockDate,
    ledger::governance::Governance,
    vote::{CommitteeId, PayloadType, VoteError, VotePlanManager},
};
use imhamt::{Hamt, InsertError, UpdateError};
use std::collections::{hash_map::DefaultHasher, HashSet};
use thiserror::Error;

#[derive(Clone, PartialEq, Eq)]
pub struct VotePlanLedger {
    pub(crate) plans: Hamt<DefaultHasher, VotePlanId, VotePlanManager>,
}

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum VotePlanLedgerError {
    #[error("cannot insert the vote plan {id}")]
    VotePlanInsertionError {
        id: VotePlanId,
        #[source]
        reason: InsertError,
    },

    #[error("cannot update the vote plan {id}")]
    VoteError {
        id: VotePlanId,
        #[source]
        reason: UpdateError<VoteError>,
    },

    #[error("Vote plan is set to finish in the passed ({vote_end}), current date {current_date}")]
    VotePlanVoteEndPassed {
        current_date: BlockDate,
        vote_end: BlockDate,
    },

    #[error("Vote plan already started ({vote_start}), current date {current_date}")]
    VotePlanVoteStartStartedAlready {
        current_date: BlockDate,
        vote_start: BlockDate,
    },

    #[error("Private vote plan must contain at least one committee member key")]
    VotePlanMissingCommitteeMemberKey,
}

impl VotePlanLedger {
    pub fn new() -> Self {
        Self { plans: Hamt::new() }
    }

    /// attempt to apply the vote to the appropriate Vote Proposal
    ///
    /// # errors
    ///
    /// can fail if:
    ///
    /// * the vote plan id does not exist;
    /// * the proposal's index does not exist;
    /// * it is no longer possible to vote (the date to vote expired)
    ///
    pub fn apply_vote(
        &self,
        block_date: BlockDate,
        identifier: account::Identifier,
        vote: VoteCast,
        token_distribution: TokenDistribution<()>,
    ) -> Result<Self, VotePlanLedgerError> {
        let id = vote.vote_plan().clone();

        let r = self.plans.update(&id, move |v| {
            v.vote(block_date, identifier, vote, token_distribution)
                .map(Some)
        });

        match r {
            Err(reason) => Err(VotePlanLedgerError::VoteError { reason, id }),
            Ok(plans) => Ok(Self { plans }),
        }
    }

    /// add the vote plan in a new `VotePlanLedger`
    ///
    /// the given `VotePlanLedger` is not modified and instead a new `VotePlanLedger` is
    /// returned. They share read-only memory.
    ///
    /// # errors if
    ///
    /// * the vote_plan is set to finished votes in the past
    /// * the vote_plan has already started
    ///
    #[must_use = "This function does not modify the object, the result contains the resulted new version of the vote plan ledger"]
    pub fn add_vote_plan(
        &self,
        current_date: BlockDate,
        vote_plan: VotePlan,
        committee: HashSet<CommitteeId>,
    ) -> Result<Self, VotePlanLedgerError> {
        if current_date > vote_plan.vote_end() {
            return Err(VotePlanLedgerError::VotePlanVoteEndPassed {
                current_date,
                vote_end: vote_plan.vote_end(),
            });
        }

        if current_date > vote_plan.vote_start() {
            return Err(VotePlanLedgerError::VotePlanVoteStartStartedAlready {
                current_date,
                vote_start: vote_plan.vote_start(),
            });
        }

        if let PayloadType::Private = vote_plan.payload_type() {
            if vote_plan.committee_public_keys().is_empty() {
                return Err(VotePlanLedgerError::VotePlanMissingCommitteeMemberKey);
            }
        }

        let id = vote_plan.to_id();
        let manager = VotePlanManager::new(vote_plan, committee);

        match self.plans.insert(id.clone(), manager) {
            Err(reason) => Err(VotePlanLedgerError::VotePlanInsertionError { id, reason }),
            Ok(plans) => Ok(Self { plans }),
        }
    }

    /// apply the committee result for the associated vote plan
    ///
    /// # Errors
    ///
    /// This function may fail:
    ///
    /// * if the Committee time has elapsed
    ///
    pub fn apply_committee_result<F>(
        &self,
        block_date: BlockDate,
        governance: &Governance,
        tally: &VoteTally,
        sig: TallyProof,
        token_distribution: TokenDistribution<()>,
        f: F,
    ) -> Result<Self, VotePlanLedgerError>
    where
        F: FnMut(&VoteAction),
    {
        let id = tally.id().clone();

        let committee_id = match sig {
            TallyProof::Public { id, .. } => id,
            TallyProof::Private { id, .. } => id,
        };
        let r = self.plans.update(&id, move |v| match sig {
            TallyProof::Public { .. } => v
                .public_tally(block_date, governance, committee_id, token_distribution, f)
                .map(Some),
            TallyProof::Private { .. } => {
                let shares = tally.tally_decrypted().unwrap();
                v.private_tally(
                    block_date,
                    shares,
                    governance,
                    committee_id,
                    token_distribution,
                    f,
                )
                .map(Some)
            }
        });

        match r {
            Err(reason) => Err(VotePlanLedgerError::VoteError { reason, id }),
            Ok(plans) => Ok(Self { plans }),
        }
    }
}

impl Default for VotePlanLedger {
    fn default() -> Self {
        Self::new()
    }
}