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
use anyhow::{anyhow, Result};
use rand::{distributions::Uniform, prelude::SliceRandom, rngs::ThreadRng, Rng};
use serde::{Deserialize, Serialize};
use shared_ids::{ClientId, RequestId};
use std::{
    collections::HashMap,
    num::NonZeroU64,
    time::{Duration, Instant},
};

use shared_ids::ReplicaId;
use usig::{noop::UsigNoOp, Usig};

use crate::{
    output::TimeoutRequest,
    peer_message::{req_view_change::ReqViewChange, usig_message::checkpoint::CheckpointHash},
    timeout::StopClass,
    Config, Error, MinBft, Output, PeerMessage, RequestPayload, ValidatedPeerMessage, View,
};

use super::{Prepare, PrepareContent, TimeoutType};

mod multi;
mod normal;
mod viewchange;

/// Defines a dummy payload for sending client-requests.
///
/// It only contains the ID of the request, and if it is a valid or invalid request.
#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
pub(super) struct DummyPayload(pub(super) u64, pub(super) bool);

impl RequestPayload for DummyPayload {
    /// Returns the ID of the Request.
    fn id(&self) -> RequestId {
        RequestId::from_u64(self.0)
    }

    /// Returns Ok(()) if it is a valid request, else Err.
    fn verify(&self, _id: ClientId) -> Result<()> {
        self.1
            .then_some(())
            .ok_or_else(|| anyhow!("invalid request"))
    }
}

type MinBftSetup = (
    (
        MinBft<DummyPayload, UsigNoOp>,
        Output<DummyPayload, UsigNoOp>,
    ),
    TimeoutHandler,
);

type SetupSet = (
    HashMap<ReplicaId, MinBft<DummyPayload, UsigNoOp>>,
    HashMap<ReplicaId, TimeoutHandler>,
);

/// Creates a minimal setup for a MinBft with the given configuration parameters.
/// n is the amount of Replicas.
/// t is the amount of faulty Replicas.
/// id is the Replica id.
/// checkpoint_period is the period for a checkpoint generation.
fn minimal_setup(n: u64, t: u64, id: ReplicaId, checkpoint_period: u64) -> MinBftSetup {
    let checkpoint_period = NonZeroU64::new(checkpoint_period).unwrap();
    (
        MinBft::new(
            UsigNoOp::default(),
            Config {
                n: n.try_into().unwrap(),
                t,
                id,
                max_batch_size: Some(1.try_into().expect("> 0")),
                batch_timeout: Duration::from_secs(1),
                initial_timeout_duration: Duration::from_secs(1),
                checkpoint_period,
            },
        )
        .unwrap(),
        TimeoutHandler::default(),
    )
}

/// Creates a minimal setup for a MinBft with the given configuration parameters
/// and with max_batch_size set to None.
/// n is the amount of Replicas.
/// t is the amount of faulty Replicas.
/// id is the Replica id.
fn minimal_setup_batching(n: u64, t: u64, id: ReplicaId, checkpoint_period: u64) -> MinBftSetup {
    let checkpoint_period = NonZeroU64::new(checkpoint_period).unwrap();
    (
        MinBft::new(
            UsigNoOp::default(),
            Config {
                n: n.try_into().unwrap(),
                t,
                id,
                max_batch_size: None,
                batch_timeout: Duration::from_secs(1),
                initial_timeout_duration: Duration::from_secs(1),
                checkpoint_period,
            },
        )
        .unwrap(),
        TimeoutHandler::default(),
    )
}

/// Contains the information needed to store timeouts in the [TimeoutHandler].
#[derive(Debug, Clone, Copy)]
struct TimeoutEntry {
    /// The type of the timeout.
    timeout_type: TimeoutType,
    /// The deadline for when the timeout times out (and needs to be handled).
    timeout_deadline: Instant,
    /// The identifying class of a timeout for when a request for stopping it is received.
    stop_class: StopClass,
}

/// Handles timeout requests and timeouts.
/// See functions below for a better understanding.
#[derive(Debug, Clone, Default)]
pub(crate) struct TimeoutHandler(HashMap<TimeoutType, TimeoutEntry>);

impl TimeoutHandler {
    /// Handles a timeout request.
    /// Sets a timeout if the timeout request itself is a start request and
    /// if there is not already a timeout of the same type set.
    /// Stops a set timeout if the timeout request it self is a stop request and
    /// if the type and the stop class of the timeout in the request is the same as the set timeout.
    fn handle_timeout_request(&mut self, timeout_request: TimeoutRequest) {
        if let TimeoutRequest::Start(timeout) = timeout_request {
            if self.0.contains_key(&timeout.timeout_type) {
                return;
            }
            let new_entry = TimeoutEntry {
                timeout_type: timeout.timeout_type,
                timeout_deadline: Instant::now() + timeout.duration,
                stop_class: timeout.stop_class,
            };
            self.0.insert(new_entry.timeout_type, new_entry);
        }
        if let TimeoutRequest::Stop(timeout) = timeout_request {
            if !self.0.contains_key(&timeout.timeout_type) {
                return;
            }
            let current_timeout = self.0.get(&timeout.timeout_type).unwrap();
            if current_timeout.stop_class == timeout.stop_class {
                self.0.remove(&timeout.timeout_type);
            }
        }
    }

    /// Handles a collection of timeout requests.
    fn handle_timeout_requests(&mut self, timeout_requests: Vec<TimeoutRequest>) {
        for timeout_request in timeout_requests {
            self.handle_timeout_request(timeout_request);
        }
    }

    /// Retrieves set timeouts in the order of their deadline (from most to least urgent).
    fn retrieve_timeouts_ordered(&mut self) -> Vec<TimeoutType> {
        let mut timeouts: Vec<TimeoutEntry> = self.0.drain().map(|(_, e)| e).collect();
        timeouts.sort_by(|x, y| x.timeout_deadline.cmp(&y.timeout_deadline));
        timeouts.iter().map(|e| e.timeout_type).collect()
    }
}

/// Test if a Replica receives a hello message from itself.
#[test]
fn hello() {
    let id = ReplicaId::from_u64(0);
    let ((mut minbft, output), _) = minimal_setup(1, 0, id, 2);

    assert_eq!(output.responses.len(), 0);
    assert_eq!(output.errors.len(), 0);
    assert_eq!(output.timeout_requests.len(), 0);
    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 message = iter
        .next()
        .unwrap()
        .validate(id, &minbft.config, &mut minbft.usig)
        .unwrap();
    assert!(matches!(message, ValidatedPeerMessage::Hello(_)));
}

/// Returns a random [ReplicaId].
///
/// # Arguments
///
/// * `n` - The amount of peers that communicate with each other.
pub(crate) fn get_random_replica_id(n: NonZeroU64, rng: &mut ThreadRng) -> ReplicaId {
    let id: u64 = rng.gen_range(0..n.into());
    ReplicaId::from_u64(id)
}

/// Returns a random [View] smaller than the one provided.
///
/// # Arguments
///
/// * `max_view` - The [View] that should be bigger than the one returned.
pub(crate) fn get_random_view_with_max(max_view: View) -> View {
    let mut rng = rand::thread_rng();
    let view_nr: u64 = rng.gen_range(0..max_view.0);
    View(view_nr)
}

/// Returns a [Config] with default values.
///
/// # Arguments
///
/// * `n` - The total number of replicas.
/// * `t` - The maximum number of faulty replicas.
/// * `id` - The ID of the replica to which this [Config] belongs to.
pub(crate) fn create_config_default(n: NonZeroU64, t: u64, id: ReplicaId) -> Config {
    Config {
        n,
        t,
        id,
        batch_timeout: Duration::from_secs(2),
        max_batch_size: None,
        initial_timeout_duration: Duration::from_secs(2),
        checkpoint_period: NonZeroU64::new(2).unwrap(),
    }
}

pub(crate) fn create_default_configs_for_replicas(
    n: NonZeroU64,
    t: u64,
) -> HashMap<ReplicaId, Config> {
    let mut configs = HashMap::new();
    for i in 0..n.get() {
        let rep_id = ReplicaId::from_u64(i);
        let config = create_config_default(n, t, rep_id);
        configs.insert(rep_id, config);
    }
    configs
}

pub(crate) fn create_attested_usigs_for_replicas(
    n: NonZeroU64,
    not_to_attest_with_rest: Vec<ReplicaId>,
) -> HashMap<ReplicaId, UsigNoOp> {
    let mut usigs = HashMap::new();
    for i in 0..n.get() {
        let rep_id = ReplicaId::from_u64(i);
        let usig = UsigNoOp::default();
        usigs.insert(rep_id, usig);
    }

    let mut usigs_tuple = Vec::new();
    for (rep_id, usig) in usigs.iter_mut() {
        if not_to_attest_with_rest.contains(rep_id) {
            continue;
        }
        usigs_tuple.push((*rep_id, usig));
    }

    add_attestations(&mut usigs_tuple);

    usigs
}

/// Adds each [UsigNoOp] to each [UsigNoOp] as a remote party.
///
/// # Arguments
///
/// * `usigs` - The [UsigNoOp]s that shall be added as a remote party to
///             each other.
pub(crate) fn add_attestations(usigs: &mut Vec<(ReplicaId, &mut UsigNoOp)>) {
    for i in 0..usigs.len() {
        for j in 0..usigs.len() {
            let peer_id = usigs[j].0;
            usigs[i].1.add_remote_party(peer_id, ());
        }
    }
}

/// Creates a random state hash for a [Checkpoint].
pub(crate) fn create_random_state_hash() -> CheckpointHash {
    let mut rng = rand::thread_rng();
    let range = Uniform::<u8>::new(0, 255);

    let vals: Vec<u8> = (0..64).map(|_| rng.sample(range)).collect();
    vals.try_into().unwrap()
}

pub(crate) fn get_two_different_indexes(max_range: usize, rng: &mut ThreadRng) -> (usize, usize) {
    assert!(max_range > 1);
    let index_1 = rng.gen_range(0..max_range);
    let mut index_2 = rng.gen_range(0..max_range);
    while index_2 == index_1 {
        index_2 = rng.gen_range(0..max_range);
    }
    (index_1, index_2)
}

pub(crate) fn get_shuffled_remaining_replicas(
    n: NonZeroU64,
    excluded_replica: Option<ReplicaId>,
    rng: &mut ThreadRng,
) -> Vec<ReplicaId> {
    let mut remaining_replica_ids = Vec::new();
    for i in 0..n.get() {
        let replica_id = ReplicaId::from_u64(i);
        if excluded_replica.is_none() || replica_id != excluded_replica.unwrap() {
            remaining_replica_ids.push(replica_id);
        }
    }
    remaining_replica_ids.shuffle(rng);
    remaining_replica_ids
}

/// Returns a random valid backup [ReplicaId].
///
/// # Arguments
///
/// * `n` - The amount of peers that communicate with each other.
/// * `primary_id` - The [ReplicaId] of the current primary. The generated
///                  backup [ReplicaId] should differ from it.
pub(crate) fn get_random_included_replica_id(
    n: NonZeroU64,
    excluded_rep_id: ReplicaId,
    rng: &mut ThreadRng,
) -> ReplicaId {
    let remaining_replica_ids = get_shuffled_remaining_replicas(n, Some(excluded_rep_id), rng);
    let random_index = rng.gen_range(0..remaining_replica_ids.len() as u64) as usize;
    remaining_replica_ids[random_index]
}

pub(crate) fn get_random_included_index(
    excluded_max_index: usize,
    excluded_index: Option<usize>,
    rng: &mut ThreadRng,
) -> usize {
    let mut random_index = rng.gen_range(0..excluded_max_index);
    if let Some(excluded_index) = excluded_index {
        while random_index == excluded_index {
            random_index = rng.gen_range(0..excluded_max_index);
        }
    }
    random_index
}

pub(crate) fn create_random_valid_req_vc_next_dir_subsequent(
    n: NonZeroU64,
    rng: &mut ThreadRng,
) -> ReqViewChange {
    let rand_factor_0 = get_random_included_index(n.get() as usize * 10, None, rng);

    let prev_view_nr = rng.gen_range(0..=rand_factor_0 as u64 * n.get());
    let next_view_nr = prev_view_nr + 1;

    let prev_view = View(prev_view_nr);
    let next_view = View(next_view_nr);

    ReqViewChange {
        prev_view,
        next_view,
    }
}

pub(crate) fn create_random_valid_req_vc_next_jump(
    n: NonZeroU64,
    rng: &mut ThreadRng,
) -> ReqViewChange {
    let rand_factor_0 = get_random_included_index(n.get() as usize * 10, None, rng);
    let rand_summand = rng.gen_range(1..=n.get() * 10);

    let prev_view_nr = rng.gen_range(0..=rand_factor_0 as u64 * n.get());
    let next_view_nr = prev_view_nr + rand_summand;

    let prev_view = View(prev_view_nr);
    let next_view = View(next_view_nr);

    ReqViewChange {
        prev_view,
        next_view,
    }
}

/// Sets up n [MinBft]s configured with the given parameters.
/// Moreover, it returns the [TimeoutHandler]s of the [MinBft]s.
pub(crate) fn setup_set(n: u64, t: u64, checkpoint_period: u64) -> SetupSet {
    let mut minbfts = HashMap::new();
    let mut timeout_handlers = HashMap::new();

    let mut all_broadcasts = Vec::new();

    let mut hello_done_count = 0;

    for i in 0..n {
        let replica = ReplicaId::from_u64(i);
        let (
            (
                minbft,
                Output {
                    broadcasts,
                    responses,
                    timeout_requests,
                    errors,
                    ready_for_client_requests,
                    primary: _,
                    view_info: _,
                    round: _,
                },
            ),
            timeout_handler,
        ) = minimal_setup(n, t, replica, checkpoint_period);
        assert_eq!(responses.len(), 0);
        assert_eq!(errors.len(), 0);
        assert_eq!(timeout_requests.len(), 0);
        if ready_for_client_requests {
            hello_done_count += 1;
        }
        // hello should only be done when n = 1
        // otherwise, replicas still should have to attest themselves.
        assert!(!ready_for_client_requests || n == 1);
        all_broadcasts.push((replica, broadcasts));
        minbfts.insert(replica, minbft);
        timeout_handlers.insert(replica, timeout_handler);
    }

    for (id, broadcasts) in all_broadcasts.into_iter() {
        for broadcast in Vec::from(broadcasts).into_iter() {
            // remove once https://github.com/rust-lang/rust/issues/59878 is fixed
            for (_, minbft) in minbfts.iter_mut().filter(|(i, _)| **i != id) {
                let Output {
                    broadcasts,
                    responses,
                    timeout_requests,
                    errors,
                    ready_for_client_requests,
                    primary: _,
                    view_info: _,
                    round: _,
                } = minbft.handle_peer_message(id, broadcast.clone());
                assert_eq!(broadcasts.len(), 0);
                assert_eq!(responses.len(), 0);
                assert_eq!(errors.len(), 0);
                assert_eq!(timeout_requests.len(), 0);
                if ready_for_client_requests {
                    hello_done_count += 1;
                }
            }
        }
    }

    assert_eq!(hello_done_count, n);

    (minbfts, timeout_handlers)
}

/// Defines a NotValidadedPeerMessage for testing (UsigNoOp as Usig).
type PeerMessageTest =
    PeerMessage<<UsigNoOp as Usig>::Attestation, DummyPayload, <UsigNoOp as Usig>::Signature>;

/// Contains the collected [Output] (responses, errors, timeout requests) of the [MinBft]s.
#[derive(Default, Debug)]
struct CollectedOutput {
    responses: HashMap<ReplicaId, Vec<(ClientId, DummyPayload)>>,
    errors: HashMap<ReplicaId, Vec<Error>>,
    timeout_requests: HashMap<ReplicaId, Vec<TimeoutRequest>>,
}

impl CollectedOutput {
    /// Returns the relevant timeouts to handle.
    fn timeouts_to_handle(
        &self,
        timeout_handlers: &mut HashMap<ReplicaId, TimeoutHandler>,
        rng: &mut ThreadRng,
    ) -> HashMap<ReplicaId, Vec<TimeoutType>> {
        let mut timeouts_to_handle = HashMap::new();

        let mut replica_ids: Vec<ReplicaId> = self.timeout_requests.keys().cloned().collect();
        replica_ids.shuffle(rng);

        for rep_id in &replica_ids {
            let timeout_requests = self.timeout_requests.get(rep_id).unwrap();
            let timeout_handler = timeout_handlers.get_mut(rep_id).unwrap();
            timeout_handler.handle_timeout_requests(timeout_requests.to_vec());
            timeouts_to_handle.insert(*rep_id, timeout_handler.retrieve_timeouts_ordered());
        }
        timeouts_to_handle
    }
}

/// Handle messages to be broadcast.
fn handle_broadcasts(
    minbfts: &mut HashMap<ReplicaId, MinBft<DummyPayload, UsigNoOp>>,
    broadcasts_with_origin: Vec<(ReplicaId, Box<[PeerMessageTest]>)>,
    collected_output: &mut CollectedOutput,
    rng: &mut ThreadRng,
) {
    // to collect possibly new added messages to broadcast
    // see below (1)
    let mut all_broadcasts = Vec::new();
    for (from, messages_to_broadcast) in broadcasts_with_origin {
        for message_to_broadcast in Vec::from(messages_to_broadcast).into_iter() {
            // remove once https://github.com/rust-lang/rust/issues/59878 is fixed

            // all other Replicas other than the origin handle the message
            let mut replica_ids: Vec<ReplicaId> =
                minbfts.keys().filter(|id| **id != from).cloned().collect();
            replica_ids.shuffle(rng);

            for rep_id in &replica_ids {
                let minbft = minbfts.get_mut(rep_id).unwrap();

                let Output {
                    broadcasts,
                    responses,
                    timeout_requests: timeouts,
                    errors,
                    ready_for_client_requests,
                    primary: _,
                    view_info: _,
                    round: _,
                } = minbft.handle_peer_message(from, message_to_broadcast.clone());
                assert!(ready_for_client_requests);
                // collect the responses of the Replica
                collected_output
                    .responses
                    .entry(*rep_id)
                    .or_default()
                    .append(&mut Vec::from(responses));
                // collect the errors of the Replica
                collected_output
                    .errors
                    .entry(*rep_id)
                    .or_default()
                    .append(&mut Vec::from(errors));
                // collect the timeouts of the Replica
                collected_output
                    .timeout_requests
                    .entry(*rep_id)
                    .or_default()
                    .append(&mut Vec::from(timeouts));
                // (1) if the handling of the peer message triggered the creation of new messages that need to be broadcasted,
                // push these new messages to the Vec of all broadcasts to be sent
                if !broadcasts.is_empty() {
                    all_broadcasts.push((*rep_id, broadcasts));
                }
            }
        }
    }
    // handle the possibly new added messages to broadcast
    // see above (1)
    if !all_broadcasts.is_empty() {
        handle_broadcasts(minbfts, all_broadcasts, collected_output, rng);
    }
}

/// Try to send a client request.
fn try_client_request(
    minbfts: &mut HashMap<ReplicaId, MinBft<DummyPayload, UsigNoOp>>,
    client_id: ClientId,
    payload: DummyPayload,
    rng: &mut ThreadRng,
) -> CollectedOutput {
    // to collect the output of each Replica generated by handling the client request
    let mut collected_output = CollectedOutput::default();

    // to collect all messages to be broadcasted generated by handling the client message
    let mut all_broadcasts = Vec::new();

    // each Replica handles the client message in a randomized order
    let mut replica_ids: Vec<ReplicaId> = minbfts.keys().cloned().collect();
    replica_ids.shuffle(rng);

    for rep_id in &replica_ids {
        let minbft = minbfts.get_mut(rep_id).unwrap();
        let Output {
            broadcasts,
            responses,
            timeout_requests: timeouts,
            errors,
            ready_for_client_requests,
            primary: _,
            view_info: _,
            round: _,
        } = minbft.handle_client_message(client_id, payload);
        assert!(ready_for_client_requests);
        // collect the responses of the Replica
        collected_output
            .responses
            .entry(*rep_id)
            .or_default()
            .append(&mut Vec::from(responses));
        // collect the errors of the Replica
        collected_output
            .errors
            .entry(*rep_id)
            .or_default()
            .append(&mut Vec::from(errors));
        // collect the timeouts of the Replica
        collected_output
            .timeout_requests
            .entry(*rep_id)
            .or_default()
            .append(&mut Vec::from(timeouts));
        // (1) If the handling of the client message triggered the creation of new messages that need to be broadcasted,
        // push these new messages to the Vec of all broadcasts to be sent.
        if !broadcasts.is_empty() {
            all_broadcasts.push((*rep_id, broadcasts));
        }
    }

    // handle the new messages to be broadcasted
    // see above (1)
    handle_broadcasts(minbfts, all_broadcasts, &mut collected_output, rng);

    collected_output
}

/// Forces the provided [MinBft]s to handle the given timeouts.
fn force_timeout(
    minbfts: &mut HashMap<ReplicaId, MinBft<DummyPayload, UsigNoOp>>,
    timeout_handler: &mut HashMap<ReplicaId, TimeoutHandler>,
    rng: &mut ThreadRng,
) -> CollectedOutput {
    // to collect the output of each Replica generated by handling the client request
    let mut collected_output = CollectedOutput::default();
    // client message is received and handled
    let mut all_broadcasts = Vec::new();

    let mut replica_ids: Vec<ReplicaId> = minbfts.keys().cloned().collect();
    replica_ids.shuffle(rng);

    for rep_id in &replica_ids {
        let minbft = minbfts.get_mut(rep_id).unwrap();
        for timeout_to_handle in timeout_handler
            .get_mut(rep_id)
            .unwrap()
            .retrieve_timeouts_ordered()
        {
            let timeout_type = timeout_to_handle.to_owned();
            let Output {
                broadcasts,
                responses,
                timeout_requests: timeouts,
                errors,
                ready_for_client_requests,
                primary: _,
                view_info: _,
                round: _,
            } = minbft.handle_timeout(timeout_type);
            // collect the responses of the Replica
            assert!(ready_for_client_requests);
            collected_output
                .responses
                .entry(*rep_id)
                .or_default()
                .append(&mut Vec::from(responses));
            // collect the errors of the Replica
            collected_output
                .errors
                .entry(*rep_id)
                .or_default()
                .append(&mut Vec::from(errors));
            // collect the timeouts of the Replica
            collected_output
                .timeout_requests
                .entry(*rep_id)
                .or_default()
                .append(&mut Vec::from(timeouts));
            if !broadcasts.is_empty() {
                all_broadcasts.push((*rep_id, broadcasts));
            }
        }
    }

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

/// Forces the provided [MinBft]s to handle the given timeouts.
/// An error is expected (for testing purposes).
fn force_timeout_expect_error(
    minbfts: &mut HashMap<ReplicaId, MinBft<DummyPayload, UsigNoOp>>,
    timeouts: &HashMap<ReplicaId, Vec<TimeoutType>>,
    rng: &mut ThreadRng,
) {
    // to collect the output of each Replica generated by handling the client request
    let mut collected_output = CollectedOutput::default();

    // client message is received and handled
    let mut all_broadcasts = Vec::new();

    let mut replica_ids: Vec<ReplicaId> = minbfts.keys().cloned().collect();
    replica_ids.shuffle(rng);

    for rep_id in &replica_ids {
        let minbft = minbfts.get_mut(rep_id).unwrap();
        if let Some(timeouts_to_handle) = timeouts.get(rep_id) {
            for timeout_to_handle in timeouts_to_handle {
                let timeout_type = timeout_to_handle.to_owned();
                let Output {
                    broadcasts,
                    responses,
                    timeout_requests: timeouts,
                    errors,
                    ready_for_client_requests,
                    primary: _,
                    view_info: _,
                    round: _,
                } = minbft.handle_timeout(timeout_type);

                assert!(ready_for_client_requests);
                collected_output
                    .responses
                    .entry(minbft.config.id)
                    .or_default()
                    .append(&mut Vec::from(responses));
                // collect the errors of the Replica
                collected_output
                    .errors
                    .entry(minbft.config.id)
                    .or_default()
                    .append(&mut Vec::from(errors));
                // collect the timeouts of the Replica
                collected_output
                    .timeout_requests
                    .entry(minbft.config.id)
                    .or_default()
                    .append(&mut Vec::from(timeouts));
                if !broadcasts.is_empty() {
                    all_broadcasts.push((minbft.config.id, broadcasts));
                }
            }
        }
    }

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

/// Create a random different number from another.
///
/// # Arguments
///
/// * `original_number` - The original number from which the generated one
/// should differ.
/// * `rng` - The random number generator to be used.
///
/// # Return Value
///
/// * `The created different number.`
pub(crate) fn create_rand_number_diff(original: u64, rng: &mut ThreadRng) -> u64 {
    let mut other = rng.gen();
    while other == original {
        other = rng.gen();
    }
    other
}