minbft 1.0.3

Efficient Byzantine Fault-Tolerance in the partially synchronous timing model
Documentation
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
//! Defines a message of type [NewView].
//! A [NewView] is broadcast by the new [View] when enough [ViewChange]s have
//! been collected.
//! For further explanation, see the documentation in [crate::MinBft] or the
//! paper "Efficient Byzantine Fault-Tolerance" by Veronese et al.

use std::collections::HashSet;

use anyhow::Result;
use blake2::digest::Update;
use serde::{Deserialize, Serialize};
use shared_ids::ReplicaId;
use std::fmt::Debug;
use tracing::{error, trace};
use usig::{Counter, Usig};

use crate::{error::InnerError, Config, RequestPayload, View};

use super::{
    signed::{UsigSignable, UsigSigned},
    view_change::ViewChange,
};

/// The content of a message of type [NewView].
/// The content consists of the origin of the message, the next [View], and the
/// [NewViewCertificate].
#[derive(Serialize, Deserialize, Clone, Debug)]
pub(crate) struct NewViewContent<P, Sig> {
    /// The replica which the [NewView] originates from.
    pub(crate) origin: ReplicaId,
    /// The next [View] to which the replica should changed to.
    pub(crate) next_view: View,
    /// The certificate of the [NewView] containing relevant messages for the
    /// transition to the next [View].
    pub(crate) certificate: NewViewCertificate<P, Sig>,
}

impl<P, Sig> AsRef<ReplicaId> for NewViewContent<P, Sig> {
    /// Referencing [NewViewContent] returns a reference to the origin in the
    /// [NewViewContent].
    fn as_ref(&self) -> &ReplicaId {
        &self.origin
    }
}

impl<P: RequestPayload, Sig: Serialize + Counter + Debug> NewViewContent<P, Sig> {
    /// Validates the [NewViewContent].
    /// A [NewViewContent] is valid if its origin is the next [View].
    pub(crate) fn validate(&self, config: &Config) -> Result<(), InnerError> {
        // Assure that the origin is the expected next View.
        if !config.is_primary(self.next_view, self.origin) {
            error!(
                "Failed validating NewView (origin: {:?}, next view: {:?}): 
            Origin does not correspond to set next view.",
                self.origin, self.next_view
            );
            return Err(InnerError::NewViewContentUnexpectedNextView {
                receiver: config.id,
                origin_actual: self.origin,
                origin_expected: ReplicaId::from_u64(self.next_view.0 % config.n),
            });
        }
        Ok(())
    }
}

/// The message of type [NewView].
/// [NewView]s consist of their content and must be signed by a USIG.
/// Such a message is broadcast by the next [View] in response to having
/// collected a sufficient amount (`t + 1`) of [ViewChange]s
/// (for further explanation, see [crate::Config], [crate::peer_message_processor].
pub(crate) type NewView<P, Sig> = UsigSigned<NewViewContent<P, Sig>, Sig>;

impl<P: Serialize, Sig: Serialize> UsigSignable for NewViewContent<P, Sig> {
    /// Hashes the content of a message of type [NewView].
    /// Required for signing and verifying a message of type [NewView].
    fn hash_content<H: Update>(&self, hasher: &mut H) {
        let encoded = bincode::serialize(self).unwrap();
        hasher.update(&encoded);
    }
}

impl<P: RequestPayload, Sig: Serialize + Counter + Debug> NewView<P, Sig> {
    /// Validates the [NewView].
    /// See below for the different steps regarding the validation.
    ///
    /// # Arguments
    ///
    /// * `config` - The config of the replica.
    /// * `usig` - The USIG signature that should be a valid one for the
    ///
    /// # Return Value
    ///
    /// [Ok] if the the validation succeeds, otherwise [InnerError].
    /// [NewView].
    pub(crate) fn validate(
        &self,
        config: &Config,
        usig: &mut impl Usig<Signature = Sig>,
    ) -> Result<(), InnerError> {
        trace!(
            "Validating NewView (origin: {:?}, next view: {:?}) ...",
            self.origin,
            self.next_view
        );
        // The origin of the message of type NewView must be the replica that
        // corresponds to the set next View.
        if !config.is_primary(self.next_view, self.origin) {
            error!(
                "Failed validating NewView (origin: {:?}, next view: {:?}): 
            The origin of the NewView does not correspond to the set next view.",
                self.origin, self.next_view
            );
        };

        // Assure that the signature is correct.
        trace!(
            "Verifying signature of NewView (origin: {:?}, next view: {:?}) ...",
            self.origin,
            self.next_view
        );
        self.verify(usig).map_or_else(
            |usig_error| {
                error!(
                    "Failed validating NewView (origin: {:?}, next view: {:?}): 
                Signature of NewView is invalid. For further information see output.",
                    self.origin, self.next_view
                );
                Err(InnerError::parse_usig_error(
                    usig_error,
                    config.id,
                    "NewView",
                    self.origin,
                ))
            },
            |v| {
                trace!(
                    "Successfully verified signature of NewView (origin: {:?}, 
                        next view: {:?}).",
                    self.origin,
                    self.next_view
                );
                trace!(
                    "Successfully validated NewView (origin: {:?}, next view: {:?}).",
                    self.origin,
                    self.next_view
                );
                Ok(v)
            },
        )?;

        self.data.validate(config).map(|v| {
            trace!(
                "Successfully validated NewView (origin: {:?}, next view: {:?}).",
                self.origin,
                self.next_view
            );
            v
        })
    }
}

/// The certificate of the [NewView].
/// Must contain at least `t + 1` valid messages of type [ViewChange].
#[derive(Serialize, Deserialize, Clone, Debug)]
pub(crate) struct NewViewCertificate<P, Sig> {
    /// The collection of messages of type [ViewChange] that together form the
    /// [NewViewCertificate].
    pub(crate) view_changes: Vec<ViewChange<P, Sig>>,
}

impl<P: RequestPayload, Sig: Serialize + Counter + Debug> NewViewCertificate<P, Sig> {
    /// Validates the [NewViewCertificate].
    /// See below for the different steps regarding the validation.
    ///
    /// # Arguments
    ///
    /// * `config` - The config of the replica.
    /// * `usig` - The USIG signature that should be a valid one for the
    /// [NewView].
    ///
    pub(crate) fn validate(
        &self,
        config: &Config,
        usig: &mut impl Usig<Signature = Sig>,
    ) -> Result<(), InnerError> {
        trace!("Validating NewViewCertificate ...");
        // Assure that there are at least t + 1 ViewChanges.
        if (self.view_changes.len() as u64) <= config.t {
            error!(
                "Failed validating NewViewCertificate: NewViewCertificate 
            does not contain sufficient ViewChanges (contains: {:?}, requires: {:?}). 
            For further information see output.",
                self.view_changes.len(),
                config.t + 1
            );
            return Err(InnerError::NewViewCheckpointCertNotSufficientMsgs {
                receiver: config.id,
            });
        }

        // Assure that all messages of type ViewChange have the same next View set.
        for view_change in &self.view_changes {
            if self.view_changes.first().unwrap().next_view != view_change.next_view {
                error!(
                    "Failed validating NewViewCertificate: Not all ViewChanges 
                contained in the NewViewCertificate have all the same next view. 
                For further information see output."
                );
                return Err(InnerError::NewViewCheckpointCertNotAllSameNextView {
                    receiver: config.id,
                });
            }
        }

        // Assure that all messages of type ViewChange originate from different replicas.
        let mut origins = HashSet::new();
        for msg in &self.view_changes {
            if !origins.insert(msg.origin) {
                error!(
                    "Failed validating NewViewCertificate: Not all ViewChanges 
                contained in the NewViewCertificate originate from different 
                replicas. For further information see output."
                );
                return Err(InnerError::NewViewCheckpointCertNotAllDifferentOrigin {
                    receiver: config.id,
                });
            }
        }

        // Assure that the messages of type ViewChange are valid.
        for m in &self.view_changes {
            match m.validate(config, usig) {
                Ok(_) => {}
                Err(e) => return Err(e),
            }
        }
        Ok(())
    }
}

#[cfg(test)]
pub(crate) mod test {
    use std::{collections::HashMap, num::NonZeroU64};

    use rand::{rngs::ThreadRng, thread_rng, Rng};
    use rstest::rstest;
    use usig::{noop::UsigNoOp, ReplicaId};

    use crate::{
        peer_message::usig_message::view_change::test::{
            create_message_log, create_view_change, setup_view_change_tests,
        },
        tests::{
            create_attested_usigs_for_replicas, create_default_configs_for_replicas,
            get_random_included_index, get_random_included_replica_id, get_random_replica_id,
            get_random_view_with_max, get_shuffled_remaining_replicas, get_two_different_indexes,
            DummyPayload,
        },
        Config, View,
    };

    use super::{NewView, NewViewCertificate, NewViewContent};

    /// Tests if the validation of a valid NewViewCertificate succeeds.
    ///
    /// # Arguments
    ///
    /// * `n` - The number of replicas.
    #[rstest]
    fn validate_valid_new_view_cert(#[values(3, 4, 5, 6, 7, 8, 9, 10)] n: u64) {
        let mut view_changes = Vec::new();

        let mut vc_setup = setup_view_change_tests(n);

        let shuffled_replicas =
            get_shuffled_remaining_replicas(vc_setup.n_parsed, None, &mut vc_setup.rng);

        for shuffled_replica in &shuffled_replicas {
            let amount_messages: u64 = vc_setup.rng.gen_range(5..10);

            let message_log = create_message_log(
                *shuffled_replica,
                amount_messages,
                None,
                &mut vc_setup.rng,
                &vc_setup.configs,
                &mut vc_setup.usigs,
            );

            let usig_origin = vc_setup.usigs.get_mut(shuffled_replica).unwrap();

            let view_change = create_view_change(
                *shuffled_replica,
                vc_setup.next_view,
                None,
                message_log,
                usig_origin,
            );

            view_changes.push(view_change);
        }

        let new_view_cert = NewViewCertificate { view_changes };

        for shuffled_replica in &shuffled_replicas {
            let config = vc_setup.configs.get(shuffled_replica).unwrap();
            let usig = vc_setup.usigs.get_mut(shuffled_replica).unwrap();
            assert!(new_view_cert.validate(config, usig).is_ok());
        }
    }

    /// Tests if the validation of an invalid NewViewCertificate fails.
    /// The NewViewCertificate contains an unsufficient amount of messages.
    ///
    /// # Arguments
    ///
    /// * `n` - The number of replicas.
    #[rstest]
    fn validate_invalid_new_view_cert_unsuff_msgs(#[values(3, 4, 5, 6, 7, 8, 9, 10)] n: u64) {
        let mut vc_setup = setup_view_change_tests(n);

        for amount_view_change_msgs in 0..vc_setup.t {
            let shuffled_replicas =
                get_shuffled_remaining_replicas(vc_setup.n_parsed, None, &mut vc_setup.rng);
            let shuffled_rep_set = shuffled_replicas
                .iter()
                .take(amount_view_change_msgs as usize);

            let mut view_changes = Vec::new();

            for shuffled_replica in shuffled_rep_set {
                let message_log = create_message_log(
                    *shuffled_replica,
                    vc_setup.amount_messages,
                    None,
                    &mut vc_setup.rng,
                    &vc_setup.configs,
                    &mut vc_setup.usigs,
                );

                let usig_origin = vc_setup.usigs.get_mut(shuffled_replica).unwrap();

                let view_change = create_view_change(
                    *shuffled_replica,
                    vc_setup.next_view,
                    None,
                    message_log,
                    usig_origin,
                );

                view_changes.push(view_change);
            }

            let new_view_cert = NewViewCertificate { view_changes };

            for shuffled_replica in &shuffled_replicas {
                let config = vc_setup.configs.get(shuffled_replica).unwrap();
                let usig = vc_setup.usigs.get_mut(shuffled_replica).unwrap();
                assert!(new_view_cert.validate(config, usig).is_err());
            }
        }
    }

    /// Tests if the validation of an invalid NewViewCertificate fails.
    /// The NewViewCertificate consists of messages that do not share the same
    /// next View.
    ///
    /// # Arguments
    ///
    /// * `n` - The number of replicas.
    #[rstest]
    fn validate_invalid_new_view_cert_not_all_same_next_view(
        #[values(3, 4, 5, 6, 7, 8, 9, 10)] n: u64,
    ) {
        let mut view_changes = Vec::new();

        let n_parsed = NonZeroU64::new(n).unwrap();
        let mut rng = thread_rng();
        let shuffled_replicas = get_shuffled_remaining_replicas(n_parsed, None, &mut rng);

        let t = n / 2;
        let next_view_1 = get_random_view_with_max(View(2 * n + 1));
        let mut next_view_2 = get_random_view_with_max(View(2 * n + 1));
        while next_view_1 == next_view_2 {
            next_view_2 = get_random_view_with_max(View(2 * n + 1));
        }
        let random_rep = shuffled_replicas.first().unwrap();

        let shuffled_set = shuffled_replicas.iter().take(t as usize + 1);

        let configs = create_default_configs_for_replicas(n_parsed, t);
        let mut usigs = create_attested_usigs_for_replicas(n_parsed, Vec::new());

        for shuffled_replica in shuffled_set {
            let amount_messages: u64 = rng.gen_range(5..10);

            let message_log = create_message_log(
                *shuffled_replica,
                amount_messages,
                None,
                &mut rng,
                &configs,
                &mut usigs,
            );

            let usig_origin = usigs.get_mut(shuffled_replica).unwrap();

            let next_view = if *shuffled_replica == *random_rep {
                next_view_2
            } else {
                next_view_1
            };

            let view_change =
                create_view_change(*shuffled_replica, next_view, None, message_log, usig_origin);

            view_changes.push(view_change);
        }

        let new_view_cert = NewViewCertificate { view_changes };

        for shuffled_replica in &shuffled_replicas {
            let config = configs.get(shuffled_replica).unwrap();
            let usig = usigs.get_mut(shuffled_replica).unwrap();
            assert!(new_view_cert.validate(config, usig).is_err());
        }
    }

    /// Tests if the validation of an invalid NewViewCertificate fails.
    /// The NewViewCertificate consists of messages that do not all originate
    /// from different replicas.
    ///
    /// # Arguments
    ///
    /// * `n` - The number of replicas.
    #[rstest]
    fn validate_invalid_new_view_cert_not_all_diff_origin(
        #[values(3, 4, 5, 6, 7, 8, 9, 10)] n: u64,
    ) {
        let mut view_changes = Vec::new();

        let mut vc_setup = setup_view_change_tests(n);

        let shuffled_replicas =
            get_shuffled_remaining_replicas(vc_setup.n_parsed, None, &mut vc_setup.rng);

        let (index_to_replace_origin, index_origin_to_set_to) =
            get_two_different_indexes(vc_setup.t as usize + 1, &mut vc_setup.rng);
        let (rep_id_to_replace_origin, rep_id_origin_to_set_to) = (
            shuffled_replicas[index_to_replace_origin],
            shuffled_replicas[index_origin_to_set_to],
        );

        let shuffled_set = shuffled_replicas.iter().take(vc_setup.t as usize + 1);

        for shuffled_replica in shuffled_set {
            let amount_messages: u64 = vc_setup.rng.gen_range(5..10);

            let origin = if *shuffled_replica == rep_id_to_replace_origin {
                rep_id_origin_to_set_to
            } else {
                *shuffled_replica
            };

            let message_log = create_message_log(
                origin,
                amount_messages,
                None,
                &mut vc_setup.rng,
                &vc_setup.configs,
                &mut vc_setup.usigs,
            );

            let usig_origin = vc_setup.usigs.get_mut(&origin).unwrap();

            let view_change =
                create_view_change(origin, vc_setup.next_view, None, message_log, usig_origin);

            view_changes.push(view_change);
        }

        let new_view_cert = NewViewCertificate { view_changes };

        for shuffled_replica in &shuffled_replicas {
            let config = vc_setup.configs.get(shuffled_replica).unwrap();
            let usig = vc_setup.usigs.get_mut(shuffled_replica).unwrap();
            assert!(new_view_cert.validate(config, usig).is_err());
        }
    }

    /// Tests if the validation of an invalid NewViewCertificate fails.
    /// The NewViewCertificate consists of invalid ViewChange messages.
    ///
    /// # Arguments
    ///
    /// * `n` - The number of replicas.
    #[rstest]
    fn validate_invalid_new_view_cert_invalid_vchange_msgs(
        #[values(3, 4, 5, 6, 7, 8, 9, 10)] n: u64,
    ) {
        use crate::peer_message::usig_message::view_change::test::{
            create_invalid_vchange_cert_empty_log, create_invalid_vchange_cert_log_first_not_cp,
            create_invalid_vchange_counter_greater_0_empty_msg_log_no_cert,
            create_invalid_vchange_msg_log_first_missing, create_invalid_vchange_msg_log_hole,
            create_invalid_vchange_msg_log_last_missing, ViewChangeCreation,
        };

        let fns_create_invalid_vchange: Vec<ViewChangeCreation> = vec![
            create_invalid_vchange_cert_empty_log,
            create_invalid_vchange_cert_log_first_not_cp,
            create_invalid_vchange_counter_greater_0_empty_msg_log_no_cert,
            create_invalid_vchange_msg_log_hole,
            create_invalid_vchange_msg_log_first_missing,
            create_invalid_vchange_msg_log_last_missing,
        ];

        for fn_create_invalid_vchange in fns_create_invalid_vchange {
            let mut view_changes = Vec::new();

            let mut vc_setup = setup_view_change_tests(n);

            let shuffled_replicas =
                get_shuffled_remaining_replicas(vc_setup.n_parsed, None, &mut vc_setup.rng);

            let shuffled_set = shuffled_replicas.iter().take(vc_setup.t as usize + 1);

            let random_rep =
                get_random_included_index(vc_setup.t as usize + 1, None, &mut vc_setup.rng);

            for (index, shuffled_rep) in shuffled_set.enumerate() {
                let amount_messages: u64 = vc_setup.rng.gen_range(5..10);

                let message_log = create_message_log(
                    *shuffled_rep,
                    amount_messages,
                    None,
                    &mut vc_setup.rng,
                    &vc_setup.configs,
                    &mut vc_setup.usigs,
                );

                let usig_origin = vc_setup.usigs.get_mut(shuffled_rep).unwrap();

                let view_change = if index == random_rep {
                    fn_create_invalid_vchange(n, Some(&mut vc_setup)).0
                } else {
                    create_view_change(
                        *shuffled_rep,
                        vc_setup.next_view,
                        None,
                        message_log,
                        usig_origin,
                    )
                };

                view_changes.push(view_change);
            }

            let new_view_cert = NewViewCertificate { view_changes };

            for shuffled_replica in &shuffled_replicas {
                let config = vc_setup.configs.get(shuffled_replica).unwrap();
                let usig = vc_setup.usigs.get_mut(shuffled_replica).unwrap();
                assert!(new_view_cert.validate(config, usig).is_err());
            }
        }
    }

    /// Defines the setup state for [NewView] messages.
    pub(crate) struct NewViewSetup {
        /// The parsed number of replicas.
        pub(crate) n_parsed: NonZeroU64,
        /// The origin of the NewView.
        pub(crate) origin: ReplicaId,
        /// The next View of the NewView.
        pub(crate) next_view: View,
        /// The random number generator.
        pub(crate) rng: ThreadRng,
        /// The configs of the replicas.
        pub(crate) configs: HashMap<ReplicaId, Config>,
        /// The USIGs of the replicas.
        pub(crate) usigs: HashMap<ReplicaId, UsigNoOp>,
    }

    /// Sets up the [NewView] tests.
    ///
    /// # Arguments
    ///
    /// * `n` - The number of replicas.
    ///
    /// # Return Value
    ///
    /// Returns the created setup for [NewView] tests.
    pub(crate) fn setup_new_view_tests(n: u64) -> NewViewSetup {
        let n_parsed = NonZeroU64::new(n).unwrap();
        let mut rng = thread_rng();
        let origin = get_random_replica_id(n_parsed, &mut rng);

        let t = n / 2;

        let rand_factor = get_random_included_index(n as usize * 10, None, &mut rng);

        let next_view = View(origin.as_u64() + rand_factor as u64 * n);

        let configs = create_default_configs_for_replicas(n_parsed, t);
        let usigs = create_attested_usigs_for_replicas(n_parsed, Vec::new());

        NewViewSetup {
            n_parsed,
            origin,
            next_view,
            rng,
            configs,
            usigs,
        }
    }

    /// Tests if the validation of a valid NewView succeeds.
    ///
    /// # Arguments
    ///
    /// * `n` - The number of replicas.
    #[rstest]
    fn validate_valid_new_view(#[values(3, 4, 5, 6, 7, 8, 9, 10)] n: u64) {
        let mut nv_setup = setup_new_view_tests(n);

        let certificate: NewViewCertificate<DummyPayload, usig::noop::Signature> =
            NewViewCertificate {
                view_changes: Vec::new(),
            };

        let usig_origin = nv_setup.usigs.get_mut(&nv_setup.origin).unwrap();

        let new_view = NewView::sign(
            NewViewContent {
                origin: nv_setup.origin,
                next_view: nv_setup.next_view,
                certificate,
            },
            usig_origin,
        )
        .unwrap();

        for i in 0..n {
            let rep_id = ReplicaId::from_u64(i);
            let config = nv_setup.configs.get(&rep_id).unwrap();
            let usig = nv_setup.usigs.get_mut(&rep_id).unwrap();
            assert!((new_view.validate(config, usig)).is_ok());
        }
    }

    /// Tests if the validation of an invalid NewView fails.
    /// The NewView contains an invalid origin.
    ///
    /// # Arguments
    ///
    /// * `n` - The number of replicas.
    #[rstest]
    fn validate_invalid_new_view_origin(#[values(3, 4, 5, 6, 7, 8, 9, 10)] n: u64) {
        let mut nv_setup = setup_new_view_tests(n);

        let certificate: NewViewCertificate<DummyPayload, usig::noop::Signature> =
            NewViewCertificate {
                view_changes: Vec::new(),
            };

        let usig_origin = nv_setup.usigs.get_mut(&nv_setup.origin).unwrap();

        let rand_rep =
            get_random_included_replica_id(nv_setup.n_parsed, nv_setup.origin, &mut nv_setup.rng);
        let rand_factor = get_random_included_index(n as usize * 10, None, &mut nv_setup.rng);

        let next_view = View(rand_rep.as_u64() + rand_factor as u64 * n);

        let new_view = NewView::sign(
            NewViewContent {
                origin: nv_setup.origin,
                next_view,
                certificate,
            },
            usig_origin,
        )
        .unwrap();

        for i in 0..n {
            let rep_id = ReplicaId::from_u64(i);
            let config = nv_setup.configs.get(&rep_id).unwrap();
            let usig = nv_setup.usigs.get_mut(&rep_id).unwrap();
            assert!((new_view.validate(config, usig)).is_err());
        }
    }

    /// Tests if the validation of an invalid NewView fails.
    /// The NewView contains an invalid signature.
    ///
    /// # Arguments
    ///
    /// * `n` - The number of replicas.
    #[rstest]
    fn validate_invalid_new_view_signature(#[values(3, 4, 5, 6, 7, 8, 9, 10)] n: u64) {
        let n_parsed = NonZeroU64::new(n).unwrap();
        let mut rng = thread_rng();
        let origin = get_random_replica_id(n_parsed, &mut rng);

        let t = n / 2;

        let rand_factor = get_random_included_index(n as usize * 10, None, &mut rng);

        let next_view = View(origin.as_u64() + rand_factor as u64 * n);

        let configs = create_default_configs_for_replicas(n_parsed, t);
        let mut usigs = create_attested_usigs_for_replicas(n_parsed, vec![origin]);

        let certificate: NewViewCertificate<DummyPayload, usig::noop::Signature> =
            NewViewCertificate {
                view_changes: Vec::new(),
            };

        let usig_origin = usigs.get_mut(&origin).unwrap();

        let new_view = NewView::sign(
            NewViewContent {
                origin,
                next_view,
                certificate,
            },
            usig_origin,
        )
        .unwrap();

        for i in 0..n {
            let rep_id = ReplicaId::from_u64(i);
            let config = configs.get(&rep_id).unwrap();
            let usig = usigs.get_mut(&rep_id).unwrap();
            assert!((new_view.validate(config, usig)).is_err());
        }
    }
}