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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
use crate::{
    block::BlockDate,
    certificate::CertificateSlice,
    ledger::governance::{Governance, ParametersGovernanceAction, TreasuryGovernanceAction},
    tokens::identifier::TokenIdentifier,
    transaction::{
        Payload, PayloadAuthData, PayloadData, PayloadSlice, SingleAccountBindingSignature,
        TransactionBindingAuthData,
    },
    vote,
};
use chain_core::{
    packer::Codec,
    property::{Deserialize, DeserializeFromSlice, ReadError, Serialize, WriteError},
};
use chain_crypto::{digest::DigestOf, Blake2b256, Verification};
use chain_vote::MemberPublicKey;
use std::ops::Deref;
use typed_bytes::{ByteArray, ByteBuilder};

/// abstract tag type to represent an external document, whatever it may be
pub struct ExternalProposalDocument;

/// the identifier of the external proposal is the Blake2b 256 bits of the
/// external proposal document hash.
///
pub type ExternalProposalId = DigestOf<Blake2b256, ExternalProposalDocument>;

/// the vote plan identifier on the blockchain
pub type VotePlanId = DigestOf<Blake2b256, VotePlan>;

/// a vote plan for the voting system
///
/// A vote plan defines what is being voted, for how long and how long
/// is the committee supposed to reveal the results.
///
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VotePlan {
    /// the vote start validity
    vote_start: BlockDate,
    /// the duration within which it is possible to vote for one of the proposals
    /// of this voting plan.
    vote_end: BlockDate,
    /// the committee duration is the time allocated to the committee to open
    /// the ballots and publish the results on chain
    committee_end: BlockDate,
    /// the proposals to vote for
    proposals: Proposals,
    /// vote payload type
    payload_type: vote::PayloadType,
    /// encrypting votes public keys
    committee_public_keys: Vec<chain_vote::MemberPublicKey>,
    /// voting token used for weigthing the votes for any proposal in this voteplan
    voting_token: TokenIdentifier,
}

#[derive(Debug, Clone)]
pub struct VotePlanProof {
    pub id: vote::CommitteeId,
    pub signature: SingleAccountBindingSignature,
}

/// this is the action that will result of the vote
///
///
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VoteAction {
    /// the action if off chain or not relevant to the blockchain
    OffChain,
    /// control the treasury
    Treasury { action: TreasuryGovernanceAction },
    /// control the parameters
    Parameters { action: ParametersGovernanceAction },
}

/// a collection of proposals
///
/// there may not be more than 255 proposal
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Proposals {
    proposals: Vec<Proposal>,
}

/// a proposal with the associated external proposal identifier
/// which leads to retrieving data from outside of the blockchain
/// with its unique identifier and the funding plan required
/// for the proposal to be operated.
///
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Proposal {
    external_id: ExternalProposalId,
    options: vote::Options,
    action: VoteAction,
}

#[must_use = "Adding a proposal may fail"]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PushProposal {
    Success,
    Full { proposal: Proposal },
}

impl Proposal {
    pub fn new(
        external_id: ExternalProposalId,
        options: vote::Options,
        action: VoteAction,
    ) -> Self {
        Self {
            external_id,
            options,
            action,
        }
    }

    pub fn check_governance(&self, governance: &Governance) -> bool {
        let criteria = match self.action() {
            VoteAction::OffChain => {
                // OffChain passes acceptance as it does not require governance
                return true;
            }
            VoteAction::Parameters { action } => governance
                .parameters
                .acceptance_criteria_for(action.to_type()),
            VoteAction::Treasury { action } => governance
                .treasury
                .acceptance_criteria_for(action.to_type()),
        };

        criteria.options == self.options
    }

    pub fn external_id(&self) -> &ExternalProposalId {
        &self.external_id
    }

    pub fn options(&self) -> &vote::Options {
        &self.options
    }

    pub fn action(&self) -> &VoteAction {
        &self.action
    }

    fn serialize_in(&self, bb: ByteBuilder<VotePlan>) -> ByteBuilder<VotePlan> {
        bb.bytes(self.external_id.as_ref())
            .u8(self.options.as_byte())
            .sub(|bb| self.action.serialize_in(bb))
    }
}

impl VoteAction {
    fn serialize_in(&self, bb: ByteBuilder<VotePlan>) -> ByteBuilder<VotePlan> {
        match self {
            Self::OffChain => bb.u8(0),
            Self::Treasury { action } => bb.u8(1).sub(|bb| action.serialize_in(bb)),
            Self::Parameters { action } => bb.u8(2).sub(|bb| action.serialize_in(bb)),
        }
    }
}

impl Proposals {
    /// the maximum number of proposals to push in `Proposals`
    pub const MAX_LEN: usize = 255;

    pub fn new() -> Proposals {
        Self {
            proposals: Vec::with_capacity(12),
        }
    }

    #[inline]
    pub fn full(&self) -> bool {
        self.len() >= Self::MAX_LEN
    }

    /// attempt to add a new proposal in the proposal collection
    ///
    pub fn push(&mut self, proposal: Proposal) -> PushProposal {
        if self.full() {
            PushProposal::Full { proposal }
        } else {
            self.proposals.push(proposal);
            PushProposal::Success
        }
    }

    pub fn proposals(&self) -> &Vec<Proposal> {
        &self.proposals
    }
}

impl VotePlan {
    pub fn new(
        vote_start: BlockDate,
        vote_end: BlockDate,
        committee_end: BlockDate,
        proposals: Proposals,
        payload_type: vote::PayloadType,
        committee_public_keys: Vec<chain_vote::MemberPublicKey>,
        voting_token: TokenIdentifier,
    ) -> Self {
        Self {
            vote_start,
            vote_end,
            committee_end,
            proposals,
            payload_type,
            committee_public_keys,
            voting_token,
        }
    }

    pub fn check_governance(&self, governance: &Governance) -> bool {
        self.proposals()
            .iter()
            .all(|proposal| proposal.check_governance(governance))
    }

    pub fn is_governance(&self) -> bool {
        self.proposals()
            .iter()
            .any(|proposal| match proposal.action() {
                VoteAction::OffChain => false,
                VoteAction::Parameters { .. } | VoteAction::Treasury { .. } => true,
            })
    }

    pub fn vote_start(&self) -> BlockDate {
        self.vote_start
    }

    pub fn vote_end(&self) -> BlockDate {
        self.vote_end
    }

    pub fn committee_start(&self) -> BlockDate {
        self.vote_end
    }

    pub fn committee_end(&self) -> BlockDate {
        self.committee_end
    }

    /// access the proposals associated to this voting plan
    pub fn proposals(&self) -> &Proposals {
        &self.proposals
    }

    pub fn proposals_mut(&mut self) -> &mut Proposals {
        &mut self.proposals
    }

    pub fn payload_type(&self) -> vote::PayloadType {
        self.payload_type
    }

    pub fn committee_public_keys(&self) -> &[chain_vote::MemberPublicKey] {
        &self.committee_public_keys
    }

    pub fn voting_token(&self) -> &TokenIdentifier {
        &self.voting_token
    }

    #[inline]
    pub fn vote_started(&self, date: BlockDate) -> bool {
        self.vote_start <= date
    }

    #[inline]
    pub fn vote_finished(&self, date: BlockDate) -> bool {
        self.vote_end <= date
    }

    /// tells if it is possible to vote at the given date
    ///
    /// `[vote_start..vote_end[`: from the start date (included) to
    /// the end (not included).
    #[inline]
    pub fn can_vote(&self, date: BlockDate) -> bool {
        self.vote_started(date) && !self.vote_finished(date)
    }

    #[inline]
    pub fn committee_started(&self, date: BlockDate) -> bool {
        self.vote_end <= date
    }

    #[inline]
    pub fn committee_finished(&self, date: BlockDate) -> bool {
        self.committee_end <= date
    }

    /// tells if it is possible to do the committee operations at the given date
    ///
    /// `[vote_end..committee_end[` from the end of the vote date (included) to the
    /// end of the committee (not included).
    ///
    #[inline]
    pub fn committee_time(&self, date: BlockDate) -> bool {
        self.committee_started(date) && !self.committee_finished(date)
    }

    pub fn serialize_in(&self, bb: ByteBuilder<Self>) -> ByteBuilder<Self> {
        bb.u32(self.vote_start.epoch)
            .u32(self.vote_start.slot_id)
            .u32(self.vote_end.epoch)
            .u32(self.vote_end.slot_id)
            .u32(self.committee_end.epoch)
            .u32(self.committee_end.slot_id)
            .u8(self.payload_type as u8)
            .iter8(&mut self.proposals.iter(), |bb, proposal| {
                proposal.serialize_in(bb)
            })
            .iter8(self.committee_public_keys.iter(), |bb, key| {
                bb.bytes(key.to_bytes().as_ref())
            })
            .bytes(self.voting_token.bytes().as_ref())
    }

    pub fn serialize(&self) -> ByteArray<Self> {
        self.serialize_in(ByteBuilder::new()).finalize()
    }

    /// build the identifier of the vote plan
    ///
    /// this is not a very efficient function so it is better not
    /// to call it in tight loop
    pub fn to_id(&self) -> VotePlanId {
        let ba = self.serialize();
        DigestOf::digest_byteslice(&ba.as_byteslice())
    }
}

impl VotePlanProof {
    pub fn serialize_in(&self, bb: ByteBuilder<Self>) -> ByteBuilder<Self> {
        bb.bytes(self.id.as_ref()).bytes(self.signature.as_ref())
    }

    pub fn verify(&self, verify_data: &TransactionBindingAuthData<'_>) -> Verification {
        let pk = self.id.public_key();
        self.signature.verify_slice(&pk, verify_data)
    }
}

/* Auth/Payload ************************************************************* */

impl Payload for VotePlan {
    const HAS_DATA: bool = true;
    const HAS_AUTH: bool = true;
    type Auth = VotePlanProof;

    fn payload_data(&self) -> PayloadData<Self> {
        PayloadData(
            self.serialize_in(ByteBuilder::new())
                .finalize_as_vec()
                .into(),
            std::marker::PhantomData,
        )
    }

    fn payload_auth_data(auth: &Self::Auth) -> PayloadAuthData<Self> {
        PayloadAuthData(
            auth.serialize_in(ByteBuilder::new())
                .finalize_as_vec()
                .into(),
            std::marker::PhantomData,
        )
    }

    fn payload_to_certificate_slice(p: PayloadSlice<'_, Self>) -> Option<CertificateSlice<'_>> {
        Some(CertificateSlice::from(p))
    }
}

/* Deref ******************************************************************** */

impl Deref for Proposals {
    type Target = <Vec<Proposal> as Deref>::Target;
    fn deref(&self) -> &Self::Target {
        self.proposals.deref()
    }
}

/* Ser/De ******************************************************************* */

impl Serialize for VotePlan {
    fn serialized_size(&self) -> usize {
        self.serialize().as_slice().len()
    }

    fn serialize<W: std::io::Write>(&self, codec: &mut Codec<W>) -> Result<(), WriteError> {
        codec.put_bytes(self.serialize().as_slice())
    }
}

impl DeserializeFromSlice for VotePlanProof {
    fn deserialize_from_slice(codec: &mut Codec<&[u8]>) -> Result<Self, ReadError> {
        let id = vote::CommitteeId::deserialize_from_slice(codec)?;
        let signature = SingleAccountBindingSignature::deserialize_from_slice(codec)?;
        Ok(Self { id, signature })
    }
}

impl Deserialize for VoteAction {
    fn deserialize<R: std::io::Read>(codec: &mut Codec<R>) -> Result<Self, ReadError> {
        match codec.get_u8()? {
            0 => Ok(Self::OffChain),
            1 => {
                TreasuryGovernanceAction::deserialize(codec).map(|action| Self::Treasury { action })
            }
            2 => ParametersGovernanceAction::deserialize(codec)
                .map(|action| Self::Parameters { action }),
            t => Err(ReadError::UnknownTag(t as u32)),
        }
    }
}

impl DeserializeFromSlice for VotePlan {
    fn deserialize_from_slice(codec: &mut Codec<&[u8]>) -> Result<Self, ReadError> {
        let vote_start = BlockDate {
            epoch: codec.get_be_u32()?,
            slot_id: codec.get_be_u32()?,
        };
        let vote_end = BlockDate {
            epoch: codec.get_be_u32()?,
            slot_id: codec.get_be_u32()?,
        };
        let committee_end = BlockDate {
            epoch: codec.get_be_u32()?,
            slot_id: codec.get_be_u32()?,
        };

        let payload_type = codec
            .get_u8()?
            .try_into()
            .map_err(|e: vote::TryFromIntError| ReadError::StructureInvalid(e.to_string()))?;

        let proposal_size = codec.get_u8()? as usize;
        let mut proposals = Proposals {
            proposals: Vec::with_capacity(proposal_size),
        };
        for _ in 0..proposal_size {
            let external_id = <[u8; 32]>::deserialize(codec)?.into();
            let options = codec.get_u8().and_then(|num_choices| {
                vote::Options::new_length(num_choices)
                    .map_err(|e| ReadError::StructureInvalid(e.to_string()))
            })?;
            let action = VoteAction::deserialize(codec)?;

            let proposal = Proposal {
                external_id,
                options,
                action,
            };

            proposals.proposals.push(proposal);
        }

        let member_keys_len = codec.get_u8()?;
        let mut committee_public_keys = Vec::new();
        for _ in 0..member_keys_len {
            let key_buf = codec.get_slice(MemberPublicKey::BYTES_LEN)?;
            committee_public_keys.push(MemberPublicKey::from_bytes(key_buf).ok_or_else(|| {
                ReadError::StructureInvalid("invalid public key format".to_string())
            })?);
        }

        let voting_token = TokenIdentifier::deserialize(codec)?;

        Ok(Self {
            vote_start,
            vote_end,
            committee_end,
            proposals,
            payload_type,
            committee_public_keys,
            voting_token,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::block::BlockDate;
    use crate::testing::VoteTestGen;
    use crate::tokens::name::{TokenName, TOKEN_NAME_MAX_SIZE};
    use crate::tokens::policy_hash::{PolicyHash, POLICY_HASH_SIZE};
    use chain_core::property::BlockDate as BlockDateProp;
    use quickcheck_macros::quickcheck;

    #[quickcheck]
    fn serialize_deserialize(vote_plan: VotePlan) -> bool {
        let serialized = vote_plan.serialize();

        let result = VotePlan::deserialize_from_slice(&mut Codec::new(serialized.as_ref()));

        let decoded = result.expect("can decode encoded vote plan");

        decoded == vote_plan
    }

    #[test]
    pub fn proposals_are_full() {
        let mut proposals = VoteTestGen::proposals(Proposals::MAX_LEN);
        assert!(proposals.full());
        let proposal = VoteTestGen::proposal();
        let expected = PushProposal::Full {
            proposal: proposal.clone(),
        };
        assert_eq!(expected, proposals.push(proposal));
    }

    #[test]
    pub fn vote_and_committee_dates_are_left_inclusive() {
        let vote_start = BlockDate::first();
        let vote_end = vote_start.next_epoch();
        let committee_finished = vote_end.next_epoch();
        let vote_plan = VotePlan::new(
            vote_start,
            vote_end,
            committee_finished,
            VoteTestGen::proposals(1),
            vote::PayloadType::Public,
            Vec::new(),
            TokenIdentifier {
                policy_hash: PolicyHash::from([0u8; POLICY_HASH_SIZE]),
                token_name: TokenName::try_from(vec![0u8; TOKEN_NAME_MAX_SIZE]).unwrap(),
            },
        );

        assert!(vote_plan.vote_started(vote_start));
        assert!(vote_plan.vote_finished(vote_end));
        assert!(vote_plan.committee_started(vote_end));
        assert!(vote_plan.committee_finished(committee_finished));
    }

    #[test]
    pub fn correct_vote_plan_timeline() {
        let vote_start = BlockDate::from_epoch_slot_id(1, 0);
        let vote_end = vote_start.next_epoch();
        let committee_finished = vote_end.next_epoch();
        let vote_plan = VotePlan::new(
            vote_start,
            vote_end,
            committee_finished,
            VoteTestGen::proposals(1),
            vote::PayloadType::Public,
            Vec::new(),
            TokenIdentifier {
                policy_hash: PolicyHash::from([0u8; POLICY_HASH_SIZE]),
                token_name: TokenName::try_from(vec![0u8; TOKEN_NAME_MAX_SIZE]).unwrap(),
            },
        );

        let before_voting = BlockDate::from_epoch_slot_id(0, 10);
        let voting_date = BlockDate::from_epoch_slot_id(1, 10);
        let committee_time = BlockDate::from_epoch_slot_id(2, 10);
        let after_committee_time = BlockDate::from_epoch_slot_id(3, 10);

        assert!(!vote_plan.can_vote(before_voting));
        assert!(!vote_plan.committee_time(before_voting));

        assert!(vote_plan.can_vote(voting_date));
        assert!(!vote_plan.committee_time(voting_date));

        assert!(!vote_plan.can_vote(committee_time));
        assert!(vote_plan.committee_time(committee_time));

        assert!(!vote_plan.can_vote(after_committee_time));
        assert!(!vote_plan.committee_time(after_committee_time));
    }

    #[test]
    pub fn inverted_vote_plan_timeline() {
        let vote_start = BlockDate::from_epoch_slot_id(1, 0);
        let vote_end = vote_start.next_epoch();
        let committee_finished = vote_end.next_epoch();
        let vote_plan = VotePlan::new(
            committee_finished,
            vote_end,
            vote_start,
            VoteTestGen::proposals(1),
            vote::PayloadType::Public,
            Vec::new(),
            TokenIdentifier {
                policy_hash: PolicyHash::from([0u8; POLICY_HASH_SIZE]),
                token_name: TokenName::try_from(vec![0u8; TOKEN_NAME_MAX_SIZE]).unwrap(),
            },
        );

        let before_voting = BlockDate::from_epoch_slot_id(0, 10);
        let voting_date = BlockDate::from_epoch_slot_id(1, 10);
        let committee_time = BlockDate::from_epoch_slot_id(2, 10);
        let after_committee_time = BlockDate::from_epoch_slot_id(3, 10);

        assert!(!vote_plan.can_vote(before_voting));
        assert!(!vote_plan.committee_time(before_voting));

        assert!(!vote_plan.can_vote(voting_date));
        assert!(!vote_plan.committee_time(voting_date));

        assert!(!vote_plan.can_vote(committee_time));
        assert!(!vote_plan.committee_time(committee_time));

        assert!(!vote_plan.can_vote(after_committee_time));
        assert!(!vote_plan.committee_time(after_committee_time));
    }
}