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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
use std::collections::HashSet;

use crate::{
    client_request::RequestBatch,
    peer_message::usig_message::UsigMessageV,
    peer_message::{
        usig_message::new_view::NewView, usig_message::new_view::NewViewCertificate,
        usig_message::new_view::NewViewContent,
    },
    ViewState,
};
use rstest::rstest;

use rand::thread_rng;

use super::*;

/// Tests for different setups if after a successfull view-change, the new View receives messages.
/// Furthermore, it tests if the new View handles right away a client request previously not handled by the previous View.
#[rstest]
fn view_change_new_view_receives_msgs(
    #[values(3, 4, 5, 6, 7, 8, 9, 10)] n: u64,
    #[values(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)] checkpoint_period: u64,
) {
    let mut rng = thread_rng();
    for t in 1..n / 2 {
        let (mut minbfts, mut timeout_handlers) = setup_set(n, t, checkpoint_period);

        // remove primary, view-change is expected to be forced when a timeout is triggered
        minbfts.remove(&ReplicaId::from_u64(0));

        // send client message
        // the primary got deleted, therefore it is expected that the message is not handled
        let mut collected_output = try_client_request(
            &mut minbfts,
            ClientId::from_u64(0),
            DummyPayload(0, true),
            &mut rng,
        );

        // output should be empty
        for o in collected_output.responses.values() {
            assert_eq!(o.len(), 0);
        }

        // force timeout in order for view-change to take place
        for (r, _) in minbfts.iter() {
            timeout_handlers
                .get_mut(r)
                .unwrap()
                .handle_timeout_requests(
                    collected_output.timeout_requests.get(r).unwrap().to_owned(),
                );
        }
        collected_output = force_timeout(&mut minbfts, &mut timeout_handlers, &mut rng);

        // (ClientId::from_u64(0), DummyPayload(0, true)) should have been handled by the new View now
        for responses in collected_output.responses.values() {
            assert_eq!(responses.len(), 1);
            assert_eq!(
                *responses.get(0).unwrap(),
                (ClientId::from_u64(0), DummyPayload(0, true))
            );
        }

        // no errors should have been collected
        for errors in collected_output.errors.values() {
            assert!(errors.is_empty());
        }

        // the view-change should have taken place
        // the new View should be Replica(1)
        let new_view = ReplicaId::from_u64(1);
        match &minbfts.get(&new_view).unwrap().view_state {
            ViewState::InView(in_view) => {
                assert!(minbfts
                    .get(&new_view)
                    .unwrap()
                    .config
                    .me_primary(in_view.view));
            }
            ViewState::ChangeInProgress(_) => {
                panic!("view-change is still in progress, possibly stuck")
            }
        }

        // receive and handle new client message
        let collected_output = try_client_request(
            &mut minbfts,
            ClientId::from_u64(0),
            DummyPayload(1, true),
            &mut rng,
        );

        // (ClientId::from_u64(0), DummyPayload(1, true)) should have been handled by the new View now
        for o in collected_output.responses.values() {
            assert_eq!(o.len(), 1);
            assert_eq!(
                *o.get(0).unwrap(),
                (ClientId::from_u64(0), DummyPayload(1, true))
            );
        }
        // no errors should have been collected
        for errors in collected_output.errors.values() {
            assert!(errors.is_empty());
        }
    }
}

/// Tests for different setups if NewView messages sent by
/// Replicas that are not the new View are ignored.
#[rstest]
fn validation_other_replicas_cannot_send_new_view(
    #[values(3, 4, 5, 6, 7, 8, 9, 10)] n: u64,
    #[values(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)] checkpoint_period: u64,
) {
    let t = n / 2 - 1;
    let (mut minbfts, _timeout_handlers) = setup_set(n, t, checkpoint_period);
    // check if NewView messages sent by Replicas that are not the new View are ignored
    for index_other_non_primary in 1..t {
        let rep_id = ReplicaId::from_u64(index_other_non_primary);
        let minbft_origin = minbfts.get_mut(&rep_id).unwrap();
        let id_origin = minbft_origin.config.id;
        let next_view = View(index_other_non_primary + 1);

        let msg: NewView<DummyPayload, _> = NewView::sign(
            NewViewContent {
                origin: minbft_origin.config.id,
                next_view,
                certificate: NewViewCertificate {
                    view_changes: vec![],
                },
            },
            &mut minbft_origin.usig,
        )
        .unwrap();

        minbfts
            .get_mut(&rep_id)
            .unwrap()
            .sent_usig_msgs
            .push(UsigMessageV::NewView(msg.clone()));

        let new_view_msg = ValidatedPeerMessage::from(msg).into();

        let output = minbfts
            .get_mut(&ReplicaId::from_u64(0))
            .unwrap()
            .handle_peer_message(id_origin, new_view_msg);

        assert_eq!(output.errors.len(), 1);
    }
}

/// Tests for different setups if the new View is and behaves as expected
/// when forcing a second view-change after the first one succeeded.
#[rstest]
fn view_change_two_view_changes(
    #[values(6, 7, 8, 9, 10)] n: u64,
    #[values(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)] checkpoint_period: u64,
) {
    let mut rng = thread_rng();
    for t in 1..(n - 1) / 2 {
        let (mut minbfts, mut timeout_handlers) = setup_set(n, t, checkpoint_period);

        let collected_output = try_client_request(
            &mut minbfts,
            ClientId::from_u64(0),
            DummyPayload(0, true),
            &mut rng,
        );

        for responses in collected_output.responses.values() {
            assert_eq!(responses.len(), 1);
            assert_eq!(
                *responses.get(0).unwrap(),
                (ClientId::from_u64(0), DummyPayload(0, true))
            );
        }
        // no errors should have been collected
        for errors in collected_output.errors.values() {
            assert!(errors.is_empty());
        }

        let collected_output = try_client_request(
            &mut minbfts,
            ClientId::from_u64(0),
            DummyPayload(1, true),
            &mut rng,
        );

        // DummyPayload(1, true) should have been handled
        for responses in collected_output.responses.values() {
            assert_eq!(responses.len(), 1);
            assert_eq!(
                *responses.get(0).unwrap(),
                (ClientId::from_u64(0), DummyPayload(1, true))
            );
        }
        // no errors should have been collected
        for errors in collected_output.errors.values() {
            assert!(errors.is_empty());
        }

        // the View should still be Replica(0)
        let id_primary = ReplicaId::from_u64(0);
        for minbft in minbfts.values() {
            match &minbft.view_state {
                ViewState::InView(in_view) => {
                    assert!(minbft
                        .config
                        .is_primary(in_view.view, minbfts.get(&id_primary).unwrap().config.id));
                }
                ViewState::ChangeInProgress(_) => panic!(
                    "view-change is still in progress for {:?}, possibly stuck",
                    minbft.config.id
                ),
            }
        }

        minbfts.remove(&id_primary);

        let mut collected_output = try_client_request(
            &mut minbfts,
            ClientId::from_u64(0),
            DummyPayload(2, true),
            &mut rng,
        );

        for responses in collected_output.responses.values() {
            assert_eq!(responses.len(), 0);
        }

        for (r, _) in minbfts.iter() {
            timeout_handlers
                .get_mut(r)
                .unwrap()
                .handle_timeout_requests(
                    collected_output.timeout_requests.get(r).unwrap().to_owned(),
                );
        }

        for (r, _) in minbfts.iter() {
            timeout_handlers
                .get_mut(r)
                .unwrap()
                .handle_timeout_requests(
                    collected_output.timeout_requests.get(r).unwrap().to_owned(),
                );
        }
        collected_output = force_timeout(&mut minbfts, &mut timeout_handlers, &mut rng);

        for (r, _) in minbfts.iter() {
            timeout_handlers
                .get_mut(r)
                .unwrap()
                .handle_timeout_requests(
                    collected_output.timeout_requests.get(r).unwrap().to_owned(),
                );
        }

        collected_output = force_timeout(&mut minbfts, &mut timeout_handlers, &mut rng);

        // The View should be Replica(1), and the counter of the latest accepted
        // Prepare should be synchronized.
        let mut counters_last_accepted_prep = HashSet::new();

        let primary_id = ReplicaId::from_u64(1);
        for minbft in minbfts.values() {
            match &minbft.view_state {
                ViewState::InView(in_view) => {
                    assert!(minbft
                        .config
                        .is_primary(in_view.view, minbfts.get(&primary_id).unwrap().config.id));
                    assert!(minbft.counter_last_accepted_prep.is_some());
                    counters_last_accepted_prep.insert(minbft.counter_last_accepted_prep.unwrap());
                }
                ViewState::ChangeInProgress(_) => panic!(
                    "view-change is still in progress for {:?}, possibly stuck",
                    minbft.config.id
                ),
            }
        }
        assert_eq!(counters_last_accepted_prep.len(), 1);

        // DummyPayload(2, true) should have been handled by the new View now
        for responses in collected_output.responses.values() {
            assert_eq!(responses.len(), 1);
            assert_eq!(
                *responses.get(0).unwrap(),
                (ClientId::from_u64(0), DummyPayload(2, true))
            );
        }
        // no errors should have been collected
        for errors in collected_output.errors.values() {
            assert!(errors.is_empty());
        }

        minbfts.remove(&primary_id);

        let mut collected_output = try_client_request(
            &mut minbfts,
            ClientId::from_u64(0),
            DummyPayload(3, true),
            &mut rng,
        );

        for responses in collected_output.responses.values() {
            assert_eq!(responses.len(), 0);
        }

        // force handling timeout, view-change is expected to be forced as well
        for (r, _) in minbfts.iter() {
            timeout_handlers
                .get_mut(r)
                .unwrap()
                .handle_timeout_requests(
                    collected_output.timeout_requests.get(r).unwrap().to_owned(),
                );
        }
        collected_output = force_timeout(&mut minbfts, &mut timeout_handlers, &mut rng);

        // the View should be Replica(2), and the counters of the last accepted
        // Prepare should be synced.
        let mut counters_last_accepted_prep = HashSet::new();

        let primary_id = ReplicaId::from_u64(2);
        for minbft in minbfts.values() {
            match &minbft.view_state {
                ViewState::InView(in_view) => {
                    assert!(minbft
                        .config
                        .is_primary(in_view.view, minbfts.get(&primary_id).unwrap().config.id));
                    assert!(minbft.counter_last_accepted_prep.is_some());
                    counters_last_accepted_prep.insert(minbft.counter_last_accepted_prep.unwrap());
                }
                ViewState::ChangeInProgress(_) => panic!(
                    "view-change is still in progress for {:?}, possibly stuck",
                    minbft.config.id
                ),
            }
        }
        assert_eq!(counters_last_accepted_prep.len(), 1);

        // DummyPayload(3, true) should have been handled by the new View now
        for (_, responses) in collected_output.responses.iter() {
            assert_eq!(responses.len(), 1);
            assert_eq!(
                *responses.get(0).unwrap(),
                (ClientId::from_u64(0), DummyPayload(3, true))
            );
        }
        // no errors should have been collected
        for errors in collected_output.errors.values() {
            assert!(errors.is_empty());
        }
    }
}

/// Tests for different setups if the new View accepts client requests that the previous view received
/// and had not yet accepted before a view-change took place.
#[rstest]
fn view_change_request_accepted_prev_view(
    #[values(4, 5, 6, 7, 8, 9, 10)] n: u64,
    #[values(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)] checkpoint_period: u64,
) {
    let mut rng = thread_rng();
    for t in 1..n / 2 {
        let (mut minbfts, mut timeout_handlers) = setup_set(n, t, checkpoint_period);

        for (i, client_request_i) in (0..t).step_by(2).enumerate() {
            let collected_output = try_client_request(
                &mut minbfts,
                ClientId::from_u64(0),
                DummyPayload(client_request_i, true),
                &mut rng,
            );
            for responses in collected_output.responses.values() {
                assert_eq!(responses.len(), 1);
            }
            // no errors should have been collected
            for errors in collected_output.errors.values() {
                assert!(errors.is_empty());
            }

            // primary is deleted, a client message should therefore be ignored
            let deleted_primary = ReplicaId::from_u64(i.try_into().unwrap());
            minbfts.remove(&deleted_primary);
            let mut collected_output = try_client_request(
                &mut minbfts,
                ClientId::from_u64(0),
                DummyPayload(client_request_i + 1, true),
                &mut rng,
            );

            // output is expected to remain unchanged
            for responses in collected_output.responses.values() {
                assert_eq!(responses.len(), 0);
            }

            // force handling timeout, view-change is expected to be forced as well
            for (r, _) in minbfts.iter() {
                timeout_handlers
                    .get_mut(r)
                    .unwrap()
                    .handle_timeout_requests(
                        collected_output.timeout_requests.get(r).unwrap().to_owned(),
                    );
            }
            collected_output = force_timeout(&mut minbfts, &mut timeout_handlers, &mut rng);

            for (r, _) in minbfts.iter() {
                timeout_handlers
                    .get_mut(r)
                    .unwrap()
                    .handle_timeout_requests(
                        collected_output.timeout_requests.get(r).unwrap().to_owned(),
                    );
            }
            collected_output = force_timeout(&mut minbfts, &mut timeout_handlers, &mut rng);

            // DummyPayload(client_request_i + 1, true) should have been handled by the new View now
            for responses in collected_output.responses.values() {
                assert_eq!(responses.len(), 1);
                assert_eq!(
                    *responses.get(0).unwrap(),
                    (
                        ClientId::from_u64(0),
                        DummyPayload(client_request_i + 1, true)
                    )
                );
                // no errors should have been collected
                for errors in collected_output.errors.values() {
                    assert!(errors.is_empty());
                }
            }

            // view-change should have taken place
            let primary_id = ReplicaId::from_u64((i + 1).try_into().unwrap());
            let mut counters_last_accepted_prep = HashSet::new();
            for minbft in minbfts.values() {
                match &minbft.view_state {
                    ViewState::InView(in_view) => {
                        assert!(minbft
                            .config
                            .is_primary(in_view.view, minbfts.get(&primary_id).unwrap().config.id));
                        assert!(minbft.counter_last_accepted_prep.is_some());
                        counters_last_accepted_prep
                            .insert(minbft.counter_last_accepted_prep.unwrap());
                    }
                    ViewState::ChangeInProgress(_) => panic!(
                        "view-change is still in progress for {:?}, possibly stuck",
                        minbft.config.id
                    ),
                }
            }
            assert_eq!(counters_last_accepted_prep.len(), 1);

            // request that was already accepted by previous view should not be accepted by the new view
            for (_, minbft) in minbfts.iter_mut() {
                assert!(minbft
                    .handle_client_message(
                        ClientId::from_u64(0),
                        DummyPayload(client_request_i, true),
                    )
                    .responses
                    .is_empty());
            }
        }
    }
}

/// Test if after performing a view-change, only the new View may send a Prepare message.
/// In other words, test if no Replica is able to impersonate the current View.
#[rstest]
fn view_change_primary_impersonation(
    #[values(4, 5, 6, 7)] n: u64,
    #[values(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)] checkpoint_period: u64,
) {
    let mut rng = thread_rng();
    for t in 1..n / 2 {
        let (mut minbfts, mut timeout_handlers) = setup_set(n, t, checkpoint_period);

        let collected_output = try_client_request(
            &mut minbfts,
            ClientId::from_u64(0),
            DummyPayload(0, true),
            &mut rng,
        );
        for responses in collected_output.responses.values() {
            assert_eq!(responses.len(), 1);
            assert_eq!(
                *responses.get(0).unwrap(),
                (ClientId::from_u64(0), DummyPayload(0, true))
            );
        }
        // no errors should have been collected
        for errors in collected_output.errors.values() {
            assert!(errors.is_empty());
        }

        // primary is deleted, a client message should therefore be ignored
        let primary_id = ReplicaId::from_u64(0);
        let deleted_primary = minbfts.remove(&primary_id).unwrap();
        let mut collected_output = try_client_request(
            &mut minbfts,
            ClientId::from_u64(0),
            DummyPayload(1, true),
            &mut rng,
        );

        // output is expected to remain unchanged
        for responses in collected_output.responses.values() {
            assert_eq!(responses.len(), 0);
        }

        // force handling timeout, view-change is expected to be forced as well
        for (r, _) in minbfts.iter() {
            timeout_handlers
                .get_mut(r)
                .unwrap()
                .handle_timeout_requests(
                    collected_output.timeout_requests.get(r).unwrap().to_owned(),
                );
        }
        collected_output = force_timeout(&mut minbfts, &mut timeout_handlers, &mut rng);

        // view-change should have taken place
        // new View should be Replica(1)
        let mut counters_last_accepted_prep = HashSet::new();
        let primary_id = ReplicaId::from_u64(1);
        for minbft in minbfts.values() {
            match &minbft.view_state {
                ViewState::InView(in_view) => {
                    assert!(minbft
                        .config
                        .is_primary(in_view.view, minbfts.get(&primary_id).unwrap().config.id));
                    assert!(minbft.counter_last_accepted_prep.is_some());
                    counters_last_accepted_prep.insert(minbft.counter_last_accepted_prep.unwrap());
                }
                ViewState::ChangeInProgress(_) => panic!(
                    "view-change is still in progress for {:?}, possibly stuck",
                    minbft.config.id
                ),
            }
        }
        assert_eq!(counters_last_accepted_prep.len(), 1);

        // DummyPayload(1, true) should have been handled by the new View now
        for responses in collected_output.responses.values() {
            assert_eq!(responses.len(), 1);
            assert_eq!(
                *responses.get(0).unwrap(),
                (ClientId::from_u64(0), DummyPayload(1, true))
            );
        }
        // no errors should have been collected
        for errors in collected_output.errors.values() {
            assert!(errors.is_empty());
        }

        assert_eq!(
            minbfts.get(&primary_id).unwrap().config.me(),
            ReplicaId::from_u64(deleted_primary.config.id.as_u64() + 1)
        );

        for i in 2..n - 1 {
            let rep_id = ReplicaId::from_u64(i);
            let minbft_origin = minbfts.get_mut(&rep_id).unwrap();
            let id_origin = minbft_origin.config.id;

            let msg = Prepare::sign(
                PrepareContent {
                    view: View(deleted_primary.config.id.as_u64() + 1),
                    origin: id_origin,
                    request_batch: RequestBatch::new(Box::new([])),
                },
                &mut minbft_origin.usig,
            )
            .unwrap();

            let prepare = ValidatedPeerMessage::from(msg).into();

            let output = minbfts
                .get_mut(&primary_id)
                .unwrap()
                .handle_peer_message(id_origin, prepare);

            assert_eq!(output.errors.len(), 1);
        }
    }
}

/// Tests for different setups if when deleting the last message of the message log results in an error.
#[rstest]
fn view_change_manipulate_message_log_pop(
    #[values(3, 4, 5, 6, 7, 8, 9, 10)] n: u64,
    #[values(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)] checkpoint_period: u64,
) {
    let mut rng = thread_rng();
    for t in 1..n / 2 {
        let (mut minbfts, mut timeout_handlers) = setup_set(n, t, checkpoint_period);

        // client message is received and handled
        let collected_output = try_client_request(
            &mut minbfts,
            ClientId::from_u64(0),
            DummyPayload(0, true),
            &mut rng,
        );
        for o in collected_output.responses.values() {
            assert_eq!(o.len(), 1);
            assert_eq!(
                *o.get(0).unwrap(),
                (ClientId::from_u64(0), DummyPayload(0, true))
            );
        }
        // no errors should have been collected
        for errors in collected_output.errors.values() {
            assert!(errors.is_empty());
        }

        // primary is deleted, a client message should therefore be ignored
        let primary_id = ReplicaId::from_u64(0);
        minbfts.remove(&primary_id);
        let mut collected_output = try_client_request(
            &mut minbfts,
            ClientId::from_u64(0),
            DummyPayload(1, true),
            &mut rng,
        );

        // output is expected to remain unchanged
        for responses in collected_output.responses.values() {
            assert_eq!(responses.len(), 0);
        }

        let minbft = minbfts.get_mut(&ReplicaId::from_u64(2)).unwrap();
        minbft.sent_usig_msgs.pop().unwrap();

        // force handling timeout, view-change is expected to be forced as well
        for (r, _) in minbfts.iter() {
            timeout_handlers
                .get_mut(r)
                .unwrap()
                .handle_timeout_requests(
                    collected_output.timeout_requests.get(r).unwrap().to_owned(),
                );
        }
        collected_output = force_timeout(&mut minbfts, &mut timeout_handlers, &mut rng);
        assert_eq!(collected_output.errors.len(), minbfts.len());
    }
}

/// Tests for different setups if when deleting the first message of the message log results in an error.
#[rstest]
fn view_change_manipulate_message_log_pop_front(
    #[values(3, 4, 5, 6, 7, 8, 9, 10)] n: u64,
    #[values(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)] checkpoint_period: u64,
) {
    let mut rng = thread_rng();
    for t in 1..n / 2 {
        let (mut minbfts, mut timeout_handlers) = setup_set(n, t, checkpoint_period);

        // client message is received and handled
        let collected_output = try_client_request(
            &mut minbfts,
            ClientId::from_u64(0),
            DummyPayload(0, true),
            &mut rng,
        );
        for responses in collected_output.responses.values() {
            assert_eq!(responses.len(), 1);
            assert_eq!(
                *responses.get(0).unwrap(),
                (ClientId::from_u64(0), DummyPayload(0, true))
            );
        }
        // no errors should have been collected
        for errors in collected_output.errors.values() {
            assert!(errors.is_empty());
        }

        // primary is deleted, a client message should therefore be ignored
        let primary_id = ReplicaId::from_u64(0);
        minbfts.remove(&primary_id);
        let collected_output = try_client_request(
            &mut minbfts,
            ClientId::from_u64(0),
            DummyPayload(1, true),
            &mut rng,
        );

        // output is expected to remain unchanged
        for o in collected_output.responses.values() {
            assert_eq!(o.len(), 0);
        }

        let minbft = minbfts.get_mut(&ReplicaId::from_u64(2)).unwrap();
        minbft.sent_usig_msgs.pop().unwrap();

        // force handling timeout, view-change is expected to be forced as well
        let timeouts_to_handle =
            collected_output.timeouts_to_handle(&mut timeout_handlers, &mut rng);
        force_timeout_expect_error(&mut minbfts, &timeouts_to_handle, &mut rng);
        assert_eq!(collected_output.errors.len(), minbfts.len());
    }
}

/// Tests for different setups if when deleting a message somewhere in the middle
/// (not first nor last) of the message log results in an error.
#[rstest]
fn view_change_manipulate_message_log_hole_middle(
    #[values(3, 4, 5, 6, 7, 8, 9, 10)] n: u64,
    #[values(4, 5, 6, 7, 8, 9, 10)] checkpoint_period: u64,
) {
    let mut rng = thread_rng();
    for t in 1..n / 2 {
        let (mut minbfts, mut timeout_handlers) = setup_set(n, t, checkpoint_period);

        let collected_output = try_client_request(
            &mut minbfts,
            ClientId::from_u64(0),
            DummyPayload(0, true),
            &mut rng,
        );

        for responses in collected_output.responses.values() {
            assert_eq!(responses.len(), 1);
            assert_eq!(
                *responses.get(0).unwrap(),
                (ClientId::from_u64(0), DummyPayload(0, true))
            );
        }
        // no errors should have been collected
        for errors in collected_output.errors.values() {
            assert!(errors.is_empty());
        }

        let collected_output = try_client_request(
            &mut minbfts,
            ClientId::from_u64(0),
            DummyPayload(1, true),
            &mut rng,
        );

        // DummyPayload(1, true) should have been handled
        for o in collected_output.responses.values() {
            assert_eq!(o.len(), 1);
            assert_eq!(
                *o.get(0).unwrap(),
                (ClientId::from_u64(0), DummyPayload(1, true))
            );
        }
        // no errors should have been collected
        for errors in collected_output.errors.values() {
            assert!(errors.is_empty());
        }

        let collected_output = try_client_request(
            &mut minbfts,
            ClientId::from_u64(0),
            DummyPayload(2, true),
            &mut rng,
        );

        // DummyPayload(2, true) should have been handled
        for o in collected_output.responses.values() {
            assert_eq!(o.len(), 1);
            assert_eq!(
                *o.get(0).unwrap(),
                (ClientId::from_u64(0), DummyPayload(2, true))
            );
        }
        // no errors should have been collected
        for errors in collected_output.errors.values() {
            assert!(errors.is_empty());
        }

        // primary is deleted, a client message should therefore be ignored
        let primary_id = ReplicaId::from_u64(0);
        minbfts.remove(&primary_id);
        let collected_output = try_client_request(
            &mut minbfts,
            ClientId::from_u64(0),
            DummyPayload(3, true),
            &mut rng,
        );

        // output is expected to remain unchanged
        for o in collected_output.responses.values() {
            assert_eq!(o.len(), 0);
        }

        let minbft = minbfts.get_mut(&ReplicaId::from_u64(2)).unwrap();
        minbft.sent_usig_msgs.remove(1);

        // force handling timeout, view-change is expected to be forced as well
        let timeouts_to_handle =
            collected_output.timeouts_to_handle(&mut timeout_handlers, &mut rng);
        force_timeout_expect_error(&mut minbfts, &timeouts_to_handle, &mut rng);
        assert_eq!(collected_output.errors.len(), minbfts.len());
    }
}

/// Tests for different setups if every Replica generates a checkpoint when expected
/// (= when enough batches have been accepted so that the checkpoint period is met).
/// Validates if the checkpoint certificates are valid.
/// Implicitly, it tests if the order of the checkpoints generated is as expected.
#[rstest]
fn checkpoint_is_generated_after_period_set(
    #[values(3, 4, 5, 6, 7, 8, 9, 10)] n: u64,
    #[values(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 21, 42)] checkpoint_period: u64,
) {
    let mut rng = thread_rng();
    for t in 1..n / 2 {
        let (mut minbfts, _timeout_handlers) = setup_set(n, t, checkpoint_period);

        for minbft in minbfts.values() {
            assert_eq!(
                minbft.config.checkpoint_period,
                NonZeroU64::new(checkpoint_period).unwrap()
            )
        }

        // checkpoint cert does not contain any messages at the beginning
        for minbft in minbfts.values() {
            assert!(minbft.last_checkpoint_cert.is_none());
        }

        for client_request_i in 1..(checkpoint_period * 2 + 1) {
            // client message is received and handled
            let collected_output = try_client_request(
                &mut minbfts,
                ClientId::from_u64(0),
                DummyPayload(client_request_i, true),
                &mut rng,
            );

            for responses in collected_output.responses.values() {
                assert_eq!(responses.len(), 1);
                assert_eq!(
                    *responses.get(0).unwrap(),
                    (ClientId::from_u64(0), DummyPayload(client_request_i, true))
                );
            }
            // no errors should have been collected
            for errors in collected_output.errors.values() {
                assert!(errors.is_empty());
            }

            let primary_id = ReplicaId::from_u64(0);
            let primary = minbfts.get(&primary_id).unwrap();
            let last_hash_expected = primary.request_processor.checkpoint_generator.last_hash;
            let amount_accepted_batches_expected = primary
                .request_processor
                .checkpoint_generator
                .total_amount_accepted_batches;
            for minbft in minbfts.values_mut() {
                if minbft.config.id != ReplicaId::from_u64(0) {
                    // Check if all others agree on the expected last hash and amount of accepted batches.
                    assert_eq!(
                        minbft.request_processor.checkpoint_generator.last_hash,
                        last_hash_expected
                    );
                    assert_eq!(
                        minbft
                            .request_processor
                            .checkpoint_generator
                            .total_amount_accepted_batches,
                        amount_accepted_batches_expected
                    );
                }
                // If a CheckpointCertificate was generated, see if everyone agrees on
                // the expected last hash and amount of accept batches.
                if amount_accepted_batches_expected % checkpoint_period == 0 {
                    validate_checkpoint_cert(minbft);
                    assert_eq!(
                        minbft
                            .last_checkpoint_cert
                            .clone()
                            .unwrap()
                            .my_checkpoint
                            .state_hash,
                        last_hash_expected
                    );
                    assert_eq!(
                        minbft
                            .last_checkpoint_cert
                            .clone()
                            .unwrap()
                            .my_checkpoint
                            .total_amount_accepted_batches,
                        amount_accepted_batches_expected
                    );
                }
            }
        }
    }
}

/// Tests if all Replicas change to each expected view in a total of 2n - 1 view-changes
/// when receiving t + 1 ViewChange messages each time.
#[rstest]
fn view_change_multi_view_changes(#[values(4)] n: u64, #[values(1)] checkpoint_period: u64) {
    let mut rng = thread_rng();
    for t in 1..n / 2 {
        let (mut minbfts, mut timeout_handlers) = setup_set(n, t, checkpoint_period);
        let mut prev_view = View(0);
        let mut next_view = View(1);
        let mut view_change_number = 0;
        while view_change_number < 2 * n + 1 {
            let mut all_broadcasts = Vec::new();
            // to collect the output of each Replica generated by handling client requests
            let mut collected_output = CollectedOutput::default();

            let expected_view = View(prev_view.0);
            let mut replica_number = 0;
            let mut id = (prev_view.0 % n) as usize;
            while replica_number < minbfts.len() {
                let rep_id = ReplicaId::from_u64(id.try_into().unwrap());
                match &minbfts.get(&rep_id).unwrap().view_state {
                    ViewState::InView(in_view) => {
                        assert_eq!(in_view.view.0, expected_view.0);
                        let Output {
                            broadcasts: _,
                            responses: _,
                            timeout_requests,
                            errors: _,
                            ready_for_client_requests: _,
                            primary: _,
                            view_info: _,
                            round: _,
                        } = minbfts.get_mut(&rep_id).unwrap().handle_client_message(
                            ClientId::from_u64(0),
                            DummyPayload(view_change_number, true),
                        );

                        collected_output
                            .timeout_requests
                            .entry(minbfts.get(&rep_id).unwrap().config.id)
                            .or_default()
                            .append(&mut Vec::from(timeout_requests));

                        let timeout_types =
                            collected_output.timeouts_to_handle(&mut timeout_handlers, &mut rng);

                        let replica_timeout_types =
                            timeout_types.get(&minbfts.get(&rep_id).unwrap().config.id);

                        if replica_timeout_types.is_none() {
                            replica_number += 1;
                            id = (id + 1) % n as usize;
                            continue;
                        }

                        for timeout_type in replica_timeout_types.unwrap() {
                            let Output {
                                broadcasts,
                                responses,
                                timeout_requests,
                                errors,
                                ready_for_client_requests,
                                primary: _,
                                view_info: _,
                                round: _,
                            } = minbfts
                                .get_mut(&rep_id)
                                .unwrap()
                                .handle_timeout(*timeout_type);

                            assert!(ready_for_client_requests);
                            collected_output
                                .responses
                                .entry(minbfts.get(&rep_id).unwrap().config.id)
                                .or_default()
                                .append(&mut Vec::from(responses));
                            collected_output
                                .errors
                                .entry(minbfts.get(&rep_id).unwrap().config.id)
                                .or_default()
                                .append(&mut Vec::from(errors));
                            collected_output
                                .timeout_requests
                                .entry(minbfts.get(&rep_id).unwrap().config.id)
                                .or_default()
                                .append(&mut Vec::from(timeout_requests));
                            if !broadcasts.is_empty() {
                                all_broadcasts
                                    .push((minbfts.get(&rep_id).unwrap().config.id, broadcasts));
                            }
                        }
                    }
                    ViewState::ChangeInProgress(_) => {
                        panic!("before handle_client_message(): view-change is still in progress, possibly stuck")
                    }
                }
                replica_number += 1;
                id = (id + 1) % n as usize;
            }

            handle_broadcasts(
                &mut minbfts,
                all_broadcasts,
                &mut collected_output,
                &mut rng,
            );

            for (r, _) in minbfts.iter() {
                timeout_handlers
                    .get_mut(r)
                    .unwrap()
                    .handle_timeout_requests(
                        collected_output.timeout_requests.get(r).unwrap().to_owned(),
                    );
            }
            collected_output = force_timeout(&mut minbfts, &mut timeout_handlers, &mut rng);

            // view-change should have taken place

            let mut counters_last_accepted_prep = HashSet::new();

            for minbft in minbfts.values() {
                let expected_view = View(next_view.0);
                match &minbft.view_state {
                    ViewState::InView(in_view) => {
                        assert_eq!(
                            in_view.view.0, expected_view.0,
                            "for {:?} view is {:?} rather than the expected view {:?}",
                            minbft.config.id, in_view.view, expected_view
                        );
                        assert!(minbft.counter_last_accepted_prep.is_some());
                        counters_last_accepted_prep
                            .insert(minbft.counter_last_accepted_prep.unwrap());
                    }
                    ViewState::ChangeInProgress(_) => {
                        panic!("{:?}: after handle_broadcasts_new(): view-change is still in progress, possibly stuck", minbft.config.id);
                    }
                }
            }
            assert_eq!(counters_last_accepted_prep.len(), 1);

            for (id, responses) in collected_output.responses.iter() {
                assert_eq!(responses.len(), 1, "failed for ReplicaId {:?}", id);
                assert_eq!(
                    *responses.get(0).unwrap(),
                    (
                        ClientId::from_u64(0),
                        DummyPayload(view_change_number, true)
                    ),
                    "failed for ReplicaId {:?}",
                    id
                );
            }

            prev_view = prev_view + 1;
            next_view = next_view + 1;
            view_change_number += 1;
        }
    }
}

/// Tests if a backup does not broadcast two Commits when the primary sends its Prepare twice.
#[test]
fn send_prepare_twice_to_backup() {
    let (mut minbfts, _timeout_handlers) = setup_set(5, 2, 2);

    // send client-request to primary
    let primary_id = ReplicaId::from_u64(0);
    let output = minbfts
        .get_mut(&primary_id)
        .unwrap()
        .handle_client_message(ClientId::from_u64(23), DummyPayload(56, true));

    // should broadcast Prepare
    assert_eq!(output.broadcasts.len(), 1);
    let broadcasts = Vec::from(output.broadcasts); // remove once https://github.com/rust-lang/rust/issues/59878 is fixed
    let mut iter = broadcasts.into_iter();
    let prepare = iter.next().unwrap();

    // send client-request to backup once
    let backup_id = ReplicaId::from_u64(1);
    let output = minbfts
        .get_mut(&backup_id)
        .unwrap()
        .handle_peer_message(ReplicaId::from_u64(0), prepare.clone());

    // backup should broadcast Commit
    assert_eq!(output.broadcasts.len(), 1);

    // send client-request to backup twice
    let output = minbfts
        .get_mut(&backup_id)
        .unwrap()
        .handle_peer_message(ReplicaId::from_u64(0), prepare);

    // backup should not broadcast Commit
    assert_eq!(output.broadcasts.len(), 0);
}

/// Tests if performing two view-changes subsequently changes to the respective view and
/// if a pending client-request is accepted in the end.
#[test]
fn two_view_changes_subsequently() {
    let mut rng = thread_rng();

    let (mut minbfts, mut timeout_handlers) = setup_set(5, 1, 2);

    // Remove first two replicas that would be the first two views.
    let primary_id = ReplicaId::from_u64(0);
    minbfts.remove(&primary_id);
    let primary_id = ReplicaId::from_u64(1);
    minbfts.remove(&primary_id);

    // Send a client-request.
    let mut collected_output = try_client_request(
        &mut minbfts,
        ClientId::from_u64(0),
        DummyPayload(0, true),
        &mut rng,
    );

    for responses in collected_output.responses.values() {
        assert_eq!(responses.len(), 0);
    }

    for (r, _) in minbfts.iter() {
        timeout_handlers
            .get_mut(r)
            .unwrap()
            .handle_timeout_requests(collected_output.timeout_requests.get(r).unwrap().to_owned());
    }
    collected_output = force_timeout(&mut minbfts, &mut timeout_handlers, &mut rng);
    for (r, _) in minbfts.iter() {
        timeout_handlers
            .get_mut(r)
            .unwrap()
            .handle_timeout_requests(collected_output.timeout_requests.get(r).unwrap().to_owned());
    }
    collected_output = force_timeout(&mut minbfts, &mut timeout_handlers, &mut rng);

    // the View should be Replica(2)
    let mut counters_last_accepted_prep = HashSet::new();
    let primary_id = ReplicaId::from_u64(2);
    for minbft in minbfts.values() {
        match &minbft.view_state {
            ViewState::InView(in_view) => {
                assert!(minbft
                    .config
                    .is_primary(in_view.view, minbfts.get(&primary_id).unwrap().config.id));
                assert!(minbft.counter_last_accepted_prep.is_some());
                counters_last_accepted_prep.insert(minbft.counter_last_accepted_prep.unwrap());
            }
            ViewState::ChangeInProgress(_) => panic!(
                "view-change is still in progress for {:?}, possibly stuck",
                minbft.config.id
            ),
        }
    }
    assert_eq!(counters_last_accepted_prep.len(), 1);

    // DummyPayload(0, true) should have been handled by the new View now
    for responses in collected_output.responses.values() {
        assert_eq!(responses.len(), 1);
        assert_eq!(
            *responses.get(0).unwrap(),
            (ClientId::from_u64(0), DummyPayload(0, true))
        );
    }
    // no errors should have been collected
    for errors in collected_output.errors.values() {
        assert!(errors.is_empty());
    }
}

/// Tests if performing two view-changes subsequently changes to the respective view and
/// if a pending client-request is accepted in the end.
// #[traced_test]
#[rstest]
fn multi_view_changes_subsequently(
    #[values(5, 6, 7, 8, 9, 11, 21, 31)] n: u64,
    #[values(1, 2, 3)] checkpoint_period: u64,
) {
    let mut rng = thread_rng();
    let mut f = 1;
    let t = (n - 1) / 2;
    while f <= t {
        // At least two view-changes can be forced.
        let (mut minbfts, mut timeout_handlers) = setup_set(n, t, checkpoint_period);
        // Remove first n - (2 * t + 1) replicas to force the same amount of view-changes.
        let mut amount_replicas_removed = 0;
        while amount_replicas_removed < f {
            minbfts.remove(&ReplicaId::from_u64(amount_replicas_removed));
            amount_replicas_removed += 1;
        }
        // Send a client-request.
        let mut collected_output = try_client_request(
            &mut minbfts,
            ClientId::from_u64(0),
            DummyPayload(0, true),
            &mut rng,
        );

        for responses in collected_output.responses.values() {
            assert_eq!(responses.len(), 0);
        }

        let mut i = 0;
        while i < f {
            for (r, _) in minbfts.iter() {
                timeout_handlers
                    .get_mut(r)
                    .unwrap()
                    .handle_timeout_requests(
                        collected_output.timeout_requests.get(r).unwrap().to_owned(),
                    );
            }
            collected_output = force_timeout(&mut minbfts, &mut timeout_handlers, &mut rng);
            i += 1;
        }

        // the View should be Replica with ID (amount_replicas_removed)
        let mut counters_last_accepted_prep = HashSet::new();
        let primary_id = ReplicaId::from_u64(amount_replicas_removed);
        for minbft in minbfts.values() {
            match &minbft.view_state {
                ViewState::InView(in_view) => {
                    assert!(minbft
                        .config
                        .is_primary(in_view.view, primary_id));
                    assert!(minbft.counter_last_accepted_prep.is_some());
                    counters_last_accepted_prep.insert(minbft.counter_last_accepted_prep.unwrap());
                }
                ViewState::ChangeInProgress(_) => panic!(
                    "view-change is still in progress for {:?}, possibly stuck (primary should be {:?}",
                    minbft.config.id,
                    primary_id
                ),
            }
        }
        assert_eq!(counters_last_accepted_prep.len(), 1);

        // DummyPayload(0, true) should have been handled by the new View now
        for responses in collected_output.responses.values() {
            assert_eq!(responses.len(), 1);
            assert_eq!(
                *responses.get(0).unwrap(),
                (ClientId::from_u64(0), DummyPayload(0, true))
            );
        }
        // no errors should have been collected
        for errors in collected_output.errors.values() {
            assert!(errors.is_empty());
        }
        f += 1;
    }
}

/// Test if a replica that was previously the primary and later becomes the primary again
/// still receives and accepts client requests after a full view change cycle.
#[rstest]
fn replica_becomes_primary_after_view_change_cycle(
    #[values(5, 6, 7, 8, 9, 11, 21, 31)] n: u64,
    #[values(1, 2, 3)] checkpoint_period: u64,
) {
    let mut rng = thread_rng();
    let (mut minbfts, mut timeout_handlers) = setup_set(n, 1, checkpoint_period);

    let mut current_req_id = 0;
    let mut current_view = 0;

    while current_view < n {
        let primary_id = ReplicaId::from_u64(current_view % n);
        let current_primary = minbfts.remove(&primary_id).unwrap();

        // send client message
        // the primary got deleted, therefore it is expected that the message is not handled
        let mut collected_output = try_client_request(
            &mut minbfts,
            ClientId::from_u64(0),
            DummyPayload(current_req_id, true),
            &mut rng,
        );
        // output should be empty
        for o in collected_output.responses.values() {
            assert_eq!(o.len(), 0);
        }
        minbfts.insert(primary_id, current_primary);

        // force timeout in order for view-change to take place
        for (r, _) in minbfts.iter() {
            if *r == primary_id {
                continue;
            }
            timeout_handlers
                .get_mut(r)
                .unwrap()
                .handle_timeout_requests(
                    collected_output.timeout_requests.get(r).unwrap().to_owned(),
                );
        }
        collected_output = force_timeout(&mut minbfts, &mut timeout_handlers, &mut rng);

        for (r, _) in minbfts.iter() {
            timeout_handlers
                .get_mut(r)
                .unwrap()
                .handle_timeout_requests(
                    collected_output.timeout_requests.get(r).unwrap().to_owned(),
                );
        }
        collected_output = force_timeout(&mut minbfts, &mut timeout_handlers, &mut rng);

        // client request should have been handled by the new View now
        for responses in collected_output.responses.values() {
            assert_eq!(responses.len(), 1);
            assert_eq!(
                *responses.get(0).unwrap(),
                (ClientId::from_u64(0), DummyPayload(current_req_id, true))
            );
        }

        // no errors should have been collected
        for errors in collected_output.errors.values() {
            assert!(errors.is_empty());
        }
        // the view-change should have taken place
        let mut counters_last_accepted_prep = HashSet::new();
        let primary_id = ReplicaId::from_u64((current_view + 1) % n);
        for minbft in minbfts.values() {
            match &minbft.view_state {
                ViewState::InView(in_view) => {
                    assert!(minbft.config.is_primary(in_view.view, primary_id));
                    assert!(minbft.counter_last_accepted_prep.is_some());
                    counters_last_accepted_prep.insert(minbft.counter_last_accepted_prep.unwrap());
                }
                ViewState::ChangeInProgress(in_progress) => {
                    panic!(
                        "view-change is still in progress, possibly stuck (from {:?}, to {:?}",
                        in_progress.prev_view, in_progress.next_view
                    );
                }
            }
        }
        assert_eq!(counters_last_accepted_prep.len(), 1);

        current_view += 1;
        current_req_id += 1;
    }
    // receive and handle new client message
    let collected_output = try_client_request(
        &mut minbfts,
        ClientId::from_u64(0),
        DummyPayload(current_req_id, true),
        &mut rng,
    );

    // (ClientId::from_u64(0), DummyPayload(1, true)) should have been handled by the new View now
    for o in collected_output.responses.values() {
        assert_eq!(o.len(), 1);
        assert_eq!(
            *o.get(0).unwrap(),
            (ClientId::from_u64(0), DummyPayload(current_req_id, true))
        );
    }
    // no errors should have been collected
    for errors in collected_output.errors.values() {
        assert!(errors.is_empty());
    }
}

/// Validates the checkpoint certificate of the provided Replica.
fn validate_checkpoint_cert(minbft: &mut MinBft<DummyPayload, UsigNoOp>) {
    assert!(minbft.last_checkpoint_cert.is_some());
    // assure checkpoint cert contains at least t + 1 checkpoint messages,
    // in which 1 is own message
    assert!(
        (minbft
            .last_checkpoint_cert
            .clone()
            .unwrap()
            .other_checkpoints
            .len() as u64)
            >= minbft.config.t
    );

    // assure all checkpoint messages have the state hash of own message
    for other in &minbft
        .last_checkpoint_cert
        .clone()
        .unwrap()
        .other_checkpoints
    {
        assert!(
            minbft
                .last_checkpoint_cert
                .clone()
                .unwrap()
                .my_checkpoint
                .state_hash
                == other.state_hash
        );
    }

    // assure all checkpoint messages are from different replicas
    let mut origins = HashSet::new();
    origins.insert(
        minbft
            .last_checkpoint_cert
            .clone()
            .unwrap()
            .my_checkpoint
            .origin,
    );
    for msg in &minbft
        .last_checkpoint_cert
        .as_ref()
        .unwrap()
        .other_checkpoints
    {
        assert!(origins.insert(msg.origin));
    }

    // assure checkpoint messages are valid
    minbft
        .last_checkpoint_cert
        .clone()
        .unwrap()
        .my_checkpoint
        .validate(&minbft.config, &mut minbft.usig)
        .unwrap();
    for msg in &minbft
        .last_checkpoint_cert
        .clone()
        .unwrap()
        .other_checkpoints
    {
        assert!(msg.validate(&minbft.config, &mut minbft.usig).is_ok());
    }
}