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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
//use crate::certificate::{verify_certificate, HasPublicKeys, SignatureRaw};
use crate::certificate::{UpdateProposal, UpdateProposalId, UpdateVote, UpdateVoterId};
use crate::date::BlockDate;
use crate::setting::{ActiveSlotsCoeffError, Settings};
use imhamt::Hamt;
use std::collections::{hash_map::DefaultHasher, HashMap};

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UpdateState {
    pub(crate) proposals: Hamt<DefaultHasher, UpdateProposalId, UpdateProposalState>,
}

impl UpdateState {
    pub fn new() -> Self {
        UpdateState {
            proposals: Hamt::new(),
        }
    }

    pub fn apply_proposal(
        mut self,
        proposal_id: UpdateProposalId,
        proposal: UpdateProposal,
        settings: &Settings,
        cur_date: BlockDate,
    ) -> Result<Self, Error> {
        let proposer_id = proposal.proposer_id();

        // Only proposal.changes() validation without mutating of the 'settings' variable
        settings.try_apply(proposal.changes())?;

        if !settings.bft_leaders.contains(proposer_id) {
            return Err(Error::BadProposer(proposal_id, proposer_id.clone()));
        }

        self.proposals = self
            .proposals
            .insert(
                proposal_id,
                UpdateProposalState {
                    proposal: proposal.clone(),
                    proposal_date: cur_date,
                    votes: Hamt::new(),
                },
            )
            .map_err(|_| Error::DuplicateProposal(proposal_id))?;
        Ok(self)
    }

    pub fn apply_vote(mut self, vote: &UpdateVote, settings: &Settings) -> Result<Self, Error> {
        if !settings.bft_leaders.contains(vote.voter_id()) {
            return Err(Error::BadVoter(
                *vote.proposal_id(),
                vote.voter_id().clone(),
            ));
        }

        let new = self.proposals.update(vote.proposal_id(), |proposal| {
            let mut proposal_new = proposal.clone();
            proposal_new.votes = proposal.votes.insert(vote.voter_id().clone(), ())?;
            Ok::<_, imhamt::InsertError>(Some(proposal_new))
        });

        match new {
            Err(imhamt::UpdateError::KeyNotFound) => {
                Err(Error::VoteForMissingProposal(*vote.proposal_id()))
            }
            Err(imhamt::UpdateError::ValueCallbackError(_)) => Err(Error::DuplicateVote(
                *vote.proposal_id(),
                vote.voter_id().clone(),
            )),
            Ok(new) => {
                self.proposals = new;
                Ok(self)
            }
        }
    }

    pub fn process_proposals(
        mut self,
        mut settings: Settings,
        prev_date: BlockDate,
        new_date: BlockDate,
    ) -> (Self, Settings) {
        let mut expired_ids = vec![];

        assert!(prev_date < new_date);

        let mut proposals: Vec<(&UpdateProposalId, &UpdateProposalState)> =
            self.proposals.iter().collect();

        // sort proposals by the date
        proposals.sort_by(|(a_id, a_state), (b_id, b_state)| {
            match a_state.proposal_date.cmp(&b_state.proposal_date) {
                std::cmp::Ordering::Equal => a_id.cmp(b_id),
                res => res,
            }
        });

        // If we entered a new epoch, then delete expired update
        // proposals and apply accepted update proposals.
        if prev_date.epoch < new_date.epoch {
            for (proposal_id, proposal_state) in proposals {
                // If a majority of BFT leaders voted for the
                // proposal, then apply it.
                if proposal_state.votes.size() > settings.bft_leaders.len() / 2 {
                    // WARNING: be careful, if settings update with the new proposals will depend on the order proposal application,
                    // assumption that all proposals should be valid at this point can be violated
                    settings = settings
                        .try_apply(proposal_state.proposal.changes())
                        .expect("proposal should be valid");
                    expired_ids.push(*proposal_id);
                } else if proposal_state.proposal_date.epoch + settings.proposal_expiration
                    < new_date.epoch
                {
                    expired_ids.push(*proposal_id);
                }
            }

            for proposal_id in expired_ids {
                self.proposals = self
                    .proposals
                    .remove(&proposal_id)
                    .expect("proposal does not exist");
            }
        }

        (self, settings)
    }
    pub fn proposals(&self) -> HashMap<UpdateProposalId, UpdateProposalState> {
        self.proposals
            .iter()
            .map(|(id, state)| (*id, state.clone()))
            .collect()
    }
}

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

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UpdateProposalState {
    pub proposal: UpdateProposal,
    pub proposal_date: BlockDate,
    pub votes: Hamt<DefaultHasher, UpdateVoterId, ()>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Error {
    /*
    InvalidCurrentBlockId(Hash, Hash),
    UpdateIsInvalid,
     */
    BadProposalSignature(UpdateProposalId, UpdateVoterId),
    BadProposer(UpdateProposalId, UpdateVoterId),
    DuplicateProposal(UpdateProposalId),
    VoteForMissingProposal(UpdateProposalId),
    BadVoteSignature(UpdateProposalId, UpdateVoterId),
    BadVoter(UpdateProposalId, UpdateVoterId),
    DuplicateVote(UpdateProposalId, UpdateVoterId),
    ReadOnlySetting,
    BadBftSlotsRatio(crate::milli::Milli),
    BadConsensusGenesisPraosActiveSlotsCoeff(ActiveSlotsCoeffError),
}
impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            /*
            Error::InvalidCurrentBlockId(current_one, update_one) => {
                write!(f, "Cannot apply Setting Update. Update needs to be applied to from block {:?} but received {:?}", update_one, current_one)
            }
            Error::UpdateIsInvalid => write!(
                f,
                "Update does not apply to current state"
            ),
             */
            Error::BadProposalSignature(proposal_id, proposer_id) => write!(
                f,
                "Proposal {} from {:?} has an incorrect signature",
                proposal_id, proposer_id
            ),
            Error::BadProposer(proposal_id, proposer_id) => write!(
                f,
                "Proposer {:?} for proposal {} is not a BFT leader",
                proposer_id, proposal_id
            ),
            Error::DuplicateProposal(proposal_id) => {
                write!(f, "Received a duplicate proposal {}", proposal_id)
            }
            Error::VoteForMissingProposal(proposal_id) => write!(
                f,
                "Received a vote for a non-existent proposal {}",
                proposal_id
            ),
            Error::BadVoteSignature(proposal_id, voter_id) => write!(
                f,
                "Vote from {:?} for proposal {} has an incorrect signature",
                voter_id, proposal_id
            ),
            Error::BadVoter(proposal_id, voter_id) => write!(
                f,
                "Voter {:?} for proposal {} is not a BFT leader",
                voter_id, proposal_id
            ),
            Error::DuplicateVote(proposal_id, voter_id) => write!(
                f,
                "Received a duplicate vote from {:?} for proposal {}",
                voter_id, proposal_id
            ),
            Error::ReadOnlySetting => write!(
                f,
                "Received a proposal to modify a chain parameter that can only be set in block 0"
            ),
            Error::BadBftSlotsRatio(m) => {
                write!(f, "Cannot set BFT slots ratio to invalid value {}", m)
            }
            Error::BadConsensusGenesisPraosActiveSlotsCoeff(err) => write!(
                f,
                "Cannot set consensus genesis praos active slots coefficient: {}",
                err
            ),
        }
    }
}

impl std::error::Error for Error {}

impl From<ActiveSlotsCoeffError> for Error {
    fn from(err: ActiveSlotsCoeffError) -> Self {
        Error::BadConsensusGenesisPraosActiveSlotsCoeff(err)
    }
}

#[cfg(any(test, feature = "property-test-api"))]
mod tests {
    use super::*;
    use crate::certificate::UpdateProposal;
    #[cfg(test)]
    use crate::milli::Milli;
    #[cfg(test)]
    use crate::testing::serialization::serialization_bijection;
    #[cfg(test)]
    use crate::{
        config::ConfigParam,
        fragment::config::ConfigParams,
        testing::{data::LeaderPair, TestGen},
    };
    #[cfg(test)]
    use chain_addr::Discrimination;
    #[cfg(test)]
    use quickcheck::TestResult;
    use quickcheck::{Arbitrary, Gen};
    use quickcheck_macros::quickcheck;
    use std::iter;

    impl Arbitrary for UpdateProposalState {
        fn arbitrary<G: Gen>(g: &mut G) -> Self {
            let size = usize::arbitrary(g);
            Self {
                proposal: UpdateProposal::arbitrary(g),
                proposal_date: BlockDate::arbitrary(g),
                votes: iter::from_fn(|| Some((UpdateVoterId::arbitrary(g), ())))
                    .take(size)
                    .collect(),
            }
        }
    }

    #[cfg(test)]
    fn apply_update_proposal(
        update_state: UpdateState,
        proposal_id: UpdateProposalId,
        config_param: ConfigParam,
        proposer: &LeaderPair,
        settings: &Settings,
        block_date: BlockDate,
    ) -> Result<UpdateState, Error> {
        let update_proposal = UpdateProposal::new(ConfigParams(vec![config_param]), proposer.id());
        update_state.apply_proposal(proposal_id, update_proposal, settings, block_date)
    }

    #[cfg(test)]
    fn apply_update_vote(
        update_state: UpdateState,
        proposal_id: UpdateProposalId,
        proposer: &LeaderPair,
        settings: &Settings,
    ) -> Result<UpdateState, Error> {
        let signed_update_vote = UpdateVote::new(proposal_id, proposer.id());
        update_state.apply_vote(&signed_update_vote, settings)
    }

    quickcheck! {
        fn update_proposal_serialize_deserialize_bijection(update_proposal: UpdateProposal) -> TestResult {
            serialization_bijection(update_proposal)
        }
    }

    #[test]
    fn apply_proposal_with_unknown_proposer_should_return_error() {
        // data
        let unknown_leader = TestGen::leader_pair();
        let block_date = BlockDate::first();
        let proposal_id = TestGen::hash();
        let config_param = ConfigParam::SlotsPerEpoch(100);
        //setup
        let update_state = UpdateState::new();
        let settings = Settings::new();

        assert_eq!(
            apply_update_proposal(
                update_state,
                proposal_id,
                config_param,
                &unknown_leader,
                &settings,
                block_date,
            ),
            Err(Error::BadProposer(proposal_id, unknown_leader.id()))
        );
    }

    #[test]
    fn apply_duplicated_proposal_should_return_error() {
        // data
        let proposal_id = TestGen::hash();
        let block_date = BlockDate::first();
        let config_param = ConfigParam::SlotsPerEpoch(100);
        //setup
        let mut update_state = UpdateState::new();

        let leaders = TestGen::leaders_pairs()
            .take(5)
            .collect::<Vec<LeaderPair>>();
        let proposer = &leaders[0];
        let settings = TestGen::settings(leaders.clone());

        update_state = apply_update_proposal(
            update_state,
            proposal_id,
            config_param.clone(),
            proposer,
            &settings,
            block_date,
        )
        .expect("failed while applying first proposal");

        assert_eq!(
            apply_update_proposal(
                update_state,
                proposal_id,
                config_param,
                proposer,
                &settings,
                block_date
            ),
            Err(Error::DuplicateProposal(proposal_id))
        );
    }

    #[test]
    fn test_add_vote_for_non_existing_proposal_should_return_error() {
        let mut update_state = UpdateState::new();
        let proposal_id = TestGen::hash();
        let unknown_proposal_id = TestGen::hash();
        let block_date = BlockDate::first();
        let config_param = ConfigParam::SlotsPerEpoch(100);
        let leaders = TestGen::leaders_pairs()
            .take(5)
            .collect::<Vec<LeaderPair>>();
        let proposer = &leaders[0];
        let settings = TestGen::settings(leaders.clone());

        // Apply proposal
        update_state = apply_update_proposal(
            update_state,
            proposal_id,
            config_param,
            proposer,
            &settings,
            block_date,
        )
        .expect("failed while applying first proposal");

        // Apply vote for unknown proposal
        assert_eq!(
            apply_update_vote(update_state, unknown_proposal_id, proposer, &settings),
            Err(Error::VoteForMissingProposal(unknown_proposal_id))
        );
    }

    #[test]
    fn test_add_duplicated_vote_should_return_error() {
        let mut update_state = UpdateState::new();
        let proposal_id = TestGen::hash();
        let block_date = BlockDate::first();
        let config_param = ConfigParam::SlotsPerEpoch(100);

        let leaders = TestGen::leaders_pairs()
            .take(5)
            .collect::<Vec<LeaderPair>>();
        let proposer = &leaders[0];
        let settings = TestGen::settings(leaders.clone());

        update_state = apply_update_proposal(
            update_state,
            proposal_id,
            config_param,
            proposer,
            &settings,
            block_date,
        )
        .expect("failed while applying proposal");

        // Apply vote
        update_state = apply_update_vote(update_state, proposal_id, proposer, &settings)
            .expect("failed while applying first vote");

        // Apply duplicated vote
        assert_eq!(
            apply_update_vote(update_state, proposal_id, proposer, &settings),
            Err(Error::DuplicateVote(proposal_id, proposer.id()))
        );
    }

    #[test]
    fn test_add_vote_from_unknown_voter_should_return_error() {
        let mut update_state = UpdateState::new();
        let proposal_id = TestGen::hash();
        let unknown_leader = TestGen::leader_pair();
        let block_date = BlockDate::first();
        let config_param = ConfigParam::SlotsPerEpoch(100);

        let leaders = TestGen::leaders_pairs()
            .take(5)
            .collect::<Vec<LeaderPair>>();
        let proposer = &leaders[0];
        let settings = TestGen::settings(leaders.clone());

        update_state = apply_update_proposal(
            update_state,
            proposal_id,
            config_param,
            proposer,
            &settings,
            block_date,
        )
        .expect("failed while applying proposal");

        // Apply vote for unknown leader
        assert_eq!(
            apply_update_vote(update_state, proposal_id, &unknown_leader, &settings),
            Err(Error::BadVoter(proposal_id, unknown_leader.id()))
        );
    }

    #[test]
    fn apply_for_readonly_setting_should_return_error() {
        let update_state = UpdateState::new();
        let proposal_id = TestGen::hash();
        let proposer = TestGen::leader_pair();
        let block_date = BlockDate::first();
        let readonly_setting = ConfigParam::Discrimination(Discrimination::Test);

        let settings = TestGen::settings(vec![proposer.clone()]);

        assert_eq!(
            apply_update_proposal(
                update_state,
                proposal_id,
                readonly_setting,
                &proposer,
                &settings,
                block_date,
            ),
            Err(Error::ReadOnlySetting)
        );
    }

    #[test]
    fn apply_for_invalid_active_slot_coeff_should_return_error() {
        let update_state = UpdateState::new();
        let proposal_id = TestGen::hash();
        let proposer = TestGen::leader_pair();
        let block_date = BlockDate::first();
        let overflow_milli = Milli::from_millis(Milli::ONE.to_millis() + 1);
        let invalid_active_slot_coeff =
            ConfigParam::ConsensusGenesisPraosActiveSlotsCoeff(overflow_milli);

        let settings = TestGen::settings(vec![proposer.clone()]);

        assert_eq!(
            apply_update_proposal(
                update_state,
                proposal_id,
                invalid_active_slot_coeff,
                &proposer,
                &settings,
                block_date,
            ),
            Err(Error::BadConsensusGenesisPraosActiveSlotsCoeff(
                ActiveSlotsCoeffError::InvalidValue(overflow_milli)
            ))
        );
    }

    #[test]
    pub fn process_proposal_is_by_id_ordered() {
        let mut update_state = UpdateState::new();
        let first_proposal_id = TestGen::hash();
        let second_proposal_id = TestGen::hash();
        let first_proposer = TestGen::leader_pair();
        let second_proposer = TestGen::leader_pair();
        let block_date = BlockDate::first();
        let first_update = ConfigParam::SlotsPerEpoch(100);
        let second_update = ConfigParam::SlotsPerEpoch(200);

        let settings = TestGen::settings(vec![first_proposer.clone(), second_proposer.clone()]);

        // Apply proposal
        update_state = apply_update_proposal(
            update_state,
            first_proposal_id,
            first_update,
            &first_proposer,
            &settings,
            block_date,
        )
        .expect("failed while applying proposal");

        // Apply vote
        update_state =
            apply_update_vote(update_state, first_proposal_id, &first_proposer, &settings)
                .expect("failed while applying vote");

        // Apply vote
        update_state =
            apply_update_vote(update_state, first_proposal_id, &second_proposer, &settings)
                .expect("failed while applying vote");

        // Apply proposal
        update_state = apply_update_proposal(
            update_state,
            second_proposal_id,
            second_update,
            &second_proposer,
            &settings,
            block_date,
        )
        .expect("failed while applying proposal");

        // Apply vote
        update_state =
            apply_update_vote(update_state, second_proposal_id, &first_proposer, &settings)
                .expect("failed while applying vote");

        // Apply vote
        update_state = apply_update_vote(
            update_state,
            second_proposal_id,
            &second_proposer,
            &settings,
        )
        .expect("failed while applying vote");

        let (update_state, settings) =
            update_state.process_proposals(settings, block_date, block_date.next_epoch());

        match first_proposal_id.cmp(&second_proposal_id) {
            std::cmp::Ordering::Less => assert_eq!(settings.slots_per_epoch, 200),
            std::cmp::Ordering::Greater => assert_eq!(settings.slots_per_epoch, 100),
            _ => {}
        }

        assert_eq!(update_state.proposals.size(), 0);
    }

    #[test]
    pub fn process_proposal_is_by_time_ordered() {
        let mut update_state = UpdateState::new();
        let first_proposal_id = TestGen::hash();
        let second_proposal_id = TestGen::hash();
        let first_proposer = TestGen::leader_pair();
        let second_proposer = TestGen::leader_pair();
        let first_proposal_block_date = BlockDate::first();
        let second_proposal_block_date = BlockDate {
            slot_id: first_proposal_block_date.slot_id + 1,
            ..first_proposal_block_date
        };
        let first_update = ConfigParam::SlotsPerEpoch(100);
        let second_update = ConfigParam::SlotsPerEpoch(200);

        let settings = TestGen::settings(vec![first_proposer.clone(), second_proposer.clone()]);

        // Apply proposal
        update_state = apply_update_proposal(
            update_state,
            first_proposal_id,
            first_update,
            &first_proposer,
            &settings,
            first_proposal_block_date,
        )
        .expect("failed while applying proposal");

        // Apply vote
        update_state =
            apply_update_vote(update_state, first_proposal_id, &first_proposer, &settings)
                .expect("failed while applying vote");

        // Apply vote
        update_state =
            apply_update_vote(update_state, first_proposal_id, &second_proposer, &settings)
                .expect("failed while applying vote");

        // Apply proposal
        update_state = apply_update_proposal(
            update_state,
            second_proposal_id,
            second_update,
            &second_proposer,
            &settings,
            second_proposal_block_date,
        )
        .expect("failed while applying proposal");

        // Apply vote
        update_state =
            apply_update_vote(update_state, second_proposal_id, &first_proposer, &settings)
                .expect("failed while applying vote");

        // Apply vote
        update_state = apply_update_vote(
            update_state,
            second_proposal_id,
            &second_proposer,
            &settings,
        )
        .expect("failed while applying vote");

        let (update_state, settings) = update_state.process_proposals(
            settings,
            first_proposal_block_date,
            first_proposal_block_date.next_epoch(),
        );

        assert_eq!(settings.slots_per_epoch, 200);

        assert_eq!(update_state.proposals.size(), 0);
    }

    #[cfg(test)]
    #[derive(Debug, Copy, Clone)]
    struct ExpiryBlockDate {
        block_date: BlockDate,
        proposal_expiration: u32,
    }

    #[cfg(test)]
    impl ExpiryBlockDate {
        pub fn block_date(&self) -> BlockDate {
            self.block_date
        }

        pub fn proposal_expiration(&self) -> u32 {
            self.proposal_expiration
        }

        pub fn get_last_epoch(&self) -> u32 {
            self.block_date().epoch + self.proposal_expiration() + 1
        }
    }

    #[cfg(test)]
    impl Arbitrary for ExpiryBlockDate {
        fn arbitrary<G: Gen>(gen: &mut G) -> Self {
            let mut block_date = BlockDate::arbitrary(gen);
            block_date.epoch %= 10;
            let proposal_expiration = u32::arbitrary(gen) % 10;
            ExpiryBlockDate {
                block_date,
                proposal_expiration,
            }
        }
    }

    #[cfg(test)]
    #[quickcheck]
    fn rejected_proposals_are_removed_after_expiration_period(
        expiry_block_data: ExpiryBlockDate,
    ) -> TestResult {
        let proposal_date = expiry_block_data.block_date();
        let proposal_expiration = expiry_block_data.proposal_expiration();

        let mut update_state = UpdateState::new();
        let proposal_id = TestGen::hash();
        let proposer = TestGen::leader_pair();
        let update = ConfigParam::SlotsPerEpoch(100);

        let mut settings = TestGen::settings(vec![proposer.clone()]);
        settings.proposal_expiration = proposal_expiration;

        // Apply proposal
        update_state = apply_update_proposal(
            update_state,
            proposal_id,
            update,
            &proposer,
            &settings,
            proposal_date,
        )
        .expect("failed while applying proposal");

        let mut current_block_date = BlockDate::first();

        // Traverse through epoch and check if proposal is still in queue
        // if proposal expiration period is not exceeded after that
        // proposal should be removed from proposal collection
        for _i in 0..expiry_block_data.get_last_epoch() {
            let (update_state, _settings) = update_state.clone().process_proposals(
                settings.clone(),
                current_block_date,
                current_block_date.next_epoch(),
            );

            if proposal_date.epoch + proposal_expiration <= current_block_date.epoch {
                assert_eq!(update_state.proposals.size(), 0);
            } else {
                assert_eq!(update_state.proposals.size(), 1);
            }
            current_block_date = current_block_date.next_epoch()
        }

        TestResult::passed()
    }
}