aleph-bft 0.45.4

AlephBFT is an asynchronous and Byzantine fault tolerant consensus protocol aimed at ordering arbitrary messages (transactions). It has been designed to continuously operate even in the harshest conditions: with no bounds on message-delivery delays and in the presence of malicious actors. This makes it an excellent fit for blockchain-related applications.
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
use crate::{
    units::{ControlHash, FullUnit, HashFor, Unit, UnitCoord, UnitWithParents, WrappedUnit},
    Hasher, NodeMap, SessionId,
};
use aleph_bft_rmc::NodeCount;
use std::collections::HashMap;

mod dag;
mod parents;

use aleph_bft_types::{Data, MultiKeychain, NodeIndex, OrderedUnit, Round, Signed};
use dag::Dag;
use parents::Reconstruction as ParentReconstruction;

/// A unit with its parents represented explicitly.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ReconstructedUnit<U: Unit> {
    unit: U,
    parents: NodeMap<(HashFor<U>, Round)>,
}

impl<U: Unit> ReconstructedUnit<U> {
    /// Returns a reconstructed unit if the parents agree with the hash, errors out otherwise.
    pub fn with_parents(unit: U, parents: NodeMap<(HashFor<U>, Round)>) -> Result<Self, U> {
        match unit.control_hash().combined_hash()
            == ControlHash::<U::Hasher>::create_control_hash(&parents)
        {
            true => Ok(ReconstructedUnit { unit, parents }),
            false => Err(unit),
        }
    }

    /// Reconstructs empty parents for a round 0 unit.
    /// Assumes obviously incorrect units with wrong control hashes have been rejected earlier.
    /// Will panic if called for any other kind of unit.
    pub fn initial(unit: U) -> Self {
        let n_members = unit.control_hash().n_members();
        assert!(unit.round() == 0, "Only the zeroth unit can be initial.");
        ReconstructedUnit {
            unit,
            parents: NodeMap::with_size(n_members),
        }
    }
}

impl<U: Unit> Unit for ReconstructedUnit<U> {
    type Hasher = U::Hasher;

    fn hash(&self) -> HashFor<U> {
        self.unit.hash()
    }

    fn coord(&self) -> UnitCoord {
        self.unit.coord()
    }

    fn control_hash(&self) -> &ControlHash<Self::Hasher> {
        self.unit.control_hash()
    }

    fn session_id(&self) -> SessionId {
        self.unit.session_id()
    }
}

impl<U: Unit> WrappedUnit<U::Hasher> for ReconstructedUnit<U> {
    type Wrapped = U;

    fn unpack(self) -> U {
        self.unit
    }
}

impl<U: Unit> UnitWithParents for ReconstructedUnit<U> {
    fn parents(&self) -> impl Iterator<Item = &HashFor<U>> {
        self.parents.values().map(|(hash, _)| hash)
    }

    fn direct_parents(&self) -> impl Iterator<Item = &HashFor<Self>> {
        self.parents
            .values()
            .filter_map(|(hash, parent_round)| match self.unit.coord().round() {
                // round 0 units cannot have non-empty parents
                0 => None,

                unit_round => {
                    if unit_round - 1 == *parent_round {
                        Some(hash)
                    } else {
                        None
                    }
                }
            })
    }

    fn parent_for(&self, index: NodeIndex) -> Option<&HashFor<Self>> {
        self.parents.get(index).map(|(hash, _)| hash)
    }

    fn node_count(&self) -> NodeCount {
        self.parents.size()
    }
}

impl<D: Data, H: Hasher, K: MultiKeychain> From<ReconstructedUnit<Signed<FullUnit<H, D>, K>>>
    for Option<D>
{
    fn from(value: ReconstructedUnit<Signed<FullUnit<H, D>, K>>) -> Self {
        value.unpack().into_signable().into()
    }
}

impl<D: Data, H: Hasher, K: MultiKeychain> From<ReconstructedUnit<Signed<FullUnit<H, D>, K>>>
    for OrderedUnit<D, H>
{
    fn from(unit: ReconstructedUnit<Signed<FullUnit<H, D>, K>>) -> Self {
        let parents = unit.parents().cloned().collect();
        let unit = unit.unpack();
        let creator = unit.creator();
        let round = unit.round();
        let hash = unit.hash();
        let data = unit.into_signable().data().clone();
        OrderedUnit {
            parents,
            creator,
            round,
            hash,
            data,
        }
    }
}

/// What we need to request to reconstruct units.
#[derive(Eq, PartialEq, Debug, Clone)]
pub enum Request<H: Hasher> {
    /// We need a unit at this coordinate.
    Coord(UnitCoord),
    /// We need the explicit list of parents for the unit identified by the hash.
    /// This should only happen in the presence of forks, when optimistic reconstruction failed.
    ParentsOf(H::Hash),
}

/// The result of a reconstruction attempt. Might contain multiple reconstructed units,
/// as well as requests for some data that is needed for further reconstruction.
#[derive(Debug, PartialEq, Eq)]
pub struct ReconstructionResult<U: Unit> {
    /// All the units that got reconstructed.
    pub units: Vec<ReconstructedUnit<U>>,
    /// Any requests that now should be made.
    pub requests: Vec<Request<U::Hasher>>,
}

impl<U: Unit> ReconstructionResult<U> {
    fn new(units: Vec<ReconstructedUnit<U>>, requests: Vec<Request<U::Hasher>>) -> Self {
        ReconstructionResult { units, requests }
    }

    fn empty() -> Self {
        ReconstructionResult::new(Vec::new(), Vec::new())
    }

    fn reconstructed(unit: ReconstructedUnit<U>) -> Self {
        ReconstructionResult {
            units: vec![unit],
            requests: Vec::new(),
        }
    }

    fn request(request: Request<U::Hasher>) -> Self {
        ReconstructionResult {
            units: Vec::new(),
            requests: vec![request],
        }
    }

    fn add_unit(&mut self, unit: ReconstructedUnit<U>) {
        self.units.push(unit);
    }

    fn add_request(&mut self, request: Request<U::Hasher>) {
        self.requests.push(request);
    }

    fn accumulate(&mut self, other: ReconstructionResult<U>) {
        let ReconstructionResult {
            mut units,
            mut requests,
        } = other;
        self.units.append(&mut units);
        self.requests.append(&mut requests);
    }
}

/// The reconstruction of the structure of the Dag.
/// When passed units containing control hashes, and responses to requests it produces,
/// it eventually outputs versions with explicit parents in an order conforming to the Dag order.
pub struct Reconstruction<U: Unit> {
    parents: ParentReconstruction<U>,
    dag: Dag<ReconstructedUnit<U>>,
}

impl<U: Unit> Reconstruction<U> {
    /// Create a new reconstruction.
    pub fn new() -> Self {
        let parents = ParentReconstruction::new();
        let dag = Dag::new();
        Reconstruction { parents, dag }
    }

    fn handle_parents_reconstruction_result(
        &mut self,
        reconstruction_result: ReconstructionResult<U>,
    ) -> ReconstructionResult<U> {
        let ReconstructionResult { units, requests } = reconstruction_result;
        let units = units
            .into_iter()
            .flat_map(|unit| self.dag.add_unit(unit))
            .collect();
        ReconstructionResult::new(units, requests)
    }

    /// Add a unit to the reconstruction.
    pub fn add_unit(&mut self, unit: U) -> ReconstructionResult<U> {
        let parent_reconstruction_result = self.parents.add_unit(unit);
        self.handle_parents_reconstruction_result(parent_reconstruction_result)
    }

    /// Add an explicit list of parents to the reconstruction.
    pub fn add_parents(
        &mut self,
        unit: HashFor<U>,
        parents: HashMap<UnitCoord, HashFor<U>>,
    ) -> ReconstructionResult<U> {
        let parent_reconstruction_result = self.parents.add_parents(unit, parents);
        self.handle_parents_reconstruction_result(parent_reconstruction_result)
    }
}

#[cfg(test)]
mod test {
    use crate::{
        dag::reconstruction::{ReconstructedUnit, Reconstruction, ReconstructionResult, Request},
        units::{random_full_parent_units_up_to, Unit, UnitCoord, UnitWithParents},
        NodeCount, NodeIndex,
    };
    use aleph_bft_types::{NodeMap, Round};
    use rand::Rng;
    use std::collections::HashMap;

    #[test]
    fn reconstructs_initial_units() {
        let mut reconstruction = Reconstruction::new();
        for unit in &random_full_parent_units_up_to(0, NodeCount(4), 43)[0] {
            let ReconstructionResult {
                mut units,
                requests,
            } = reconstruction.add_unit(unit.clone());
            assert!(requests.is_empty());
            assert_eq!(units.len(), 1);
            let reconstructed_unit = units.pop().expect("just checked its there");
            assert_eq!(reconstructed_unit, ReconstructedUnit::initial(unit.clone()));
            assert_eq!(reconstructed_unit.parents().count(), 0);
        }
    }

    #[test]
    fn reconstructs_units_coming_in_order() {
        let mut reconstruction = Reconstruction::new();
        let dag = random_full_parent_units_up_to(7, NodeCount(4), 43);
        for units in &dag {
            for unit in units {
                let round = unit.round();
                let ReconstructionResult {
                    mut units,
                    requests,
                } = reconstruction.add_unit(unit.clone());
                assert!(requests.is_empty());
                assert_eq!(units.len(), 1);
                let reconstructed_unit = units.pop().expect("just checked its there");
                match round {
                    0 => {
                        assert_eq!(reconstructed_unit, ReconstructedUnit::initial(unit.clone()));
                        assert_eq!(reconstructed_unit.parents().count(), 0);
                    }
                    round => {
                        assert_eq!(reconstructed_unit.parents().count(), 4);
                        let parents = dag
                            .get((round - 1) as usize)
                            .expect("the parents are there");
                        for (parent, reconstructed_parent) in
                            parents.iter().zip(reconstructed_unit.parents())
                        {
                            assert_eq!(&parent.hash(), reconstructed_parent);
                        }
                    }
                }
            }
        }
    }

    #[test]
    fn requests_all_parents() {
        let mut reconstruction = Reconstruction::new();
        let dag = random_full_parent_units_up_to(1, NodeCount(4), 43);
        let unit = dag
            .get(1)
            .expect("just created")
            .last()
            .expect("we have a unit");
        let ReconstructionResult { units, requests } = reconstruction.add_unit(unit.clone());
        assert!(units.is_empty());
        assert_eq!(requests.len(), 4);
    }

    #[test]
    fn requests_single_parent() {
        let mut reconstruction = Reconstruction::new();
        let dag = random_full_parent_units_up_to(1, NodeCount(4), 43);
        for unit in dag.first().expect("just created").iter().skip(1) {
            reconstruction.add_unit(unit.clone());
        }
        let unit = dag
            .get(1)
            .expect("just created")
            .last()
            .expect("we have a unit");
        let ReconstructionResult { units, requests } = reconstruction.add_unit(unit.clone());
        assert!(units.is_empty());
        assert_eq!(requests.len(), 1);
        assert_eq!(
            requests.last().expect("just checked"),
            &Request::Coord(UnitCoord::new(0, NodeIndex(0)))
        );
    }

    #[test]
    fn reconstructs_units_coming_in_reverse_order() {
        let mut reconstruction = Reconstruction::new();
        let mut dag = random_full_parent_units_up_to(7, NodeCount(4), 43);
        dag.reverse();
        for units in dag.iter().take(7) {
            for unit in units {
                let ReconstructionResult { units, requests } =
                    reconstruction.add_unit(unit.clone());
                assert!(units.is_empty());
                assert_eq!(requests.len(), 4);
            }
        }
        for unit in dag[7].iter().take(3) {
            let ReconstructionResult { units, requests } = reconstruction.add_unit(unit.clone());
            assert!(requests.is_empty());
            assert_eq!(units.len(), 1);
        }
        let ReconstructionResult { units, requests } = reconstruction.add_unit(dag[7][3].clone());
        assert!(requests.is_empty());
        assert_eq!(units.len(), 4 * 8 - 3);
    }

    #[test]
    fn handles_bad_hash() {
        let node_count = NodeCount(7);
        let mut reconstruction = Reconstruction::new();
        let dag = random_full_parent_units_up_to(0, node_count, 43);
        for unit in dag.first().expect("just created") {
            reconstruction.add_unit(unit.clone());
        }
        let other_dag = random_full_parent_units_up_to(1, node_count, 43);
        let unit = other_dag
            .get(1)
            .expect("just created")
            .last()
            .expect("we have a unit");
        let unit_hash = unit.hash();
        let ReconstructionResult { units, requests } = reconstruction.add_unit(unit.clone());
        assert!(units.is_empty());
        assert_eq!(requests.len(), 1);
        assert_eq!(
            requests.last().expect("just checked"),
            &Request::ParentsOf(unit_hash),
        );
        let parent_hashes: HashMap<_, _> = other_dag
            .first()
            .expect("other dag has initial units")
            .iter()
            .map(|unit| (unit.coord(), unit.hash()))
            .collect();
        let ReconstructionResult { units, requests } =
            reconstruction.add_parents(unit_hash, parent_hashes.clone());
        assert!(requests.is_empty());
        assert!(units.is_empty());
        let mut all_reconstructed = Vec::new();
        for other_initial in &other_dag[0] {
            let ReconstructionResult {
                mut units,
                requests,
            } = reconstruction.add_unit(other_initial.clone());
            assert!(requests.is_empty());
            all_reconstructed.append(&mut units);
        }
        // some of the initial units may randomly be identical,
        // so all we can say that the last reconstructed unit should be the one we want
        assert!(!all_reconstructed.is_empty());
        assert_eq!(
            all_reconstructed.pop().expect("just checked").hash(),
            unit_hash
        )
    }
    #[test]
    fn given_wrong_rounds_with_matching_hashes_when_calling_with_parents_then_err_is_returned() {
        const MAX_ROUND: Round = 7;

        let mut rng = rand::thread_rng();
        let node_count = NodeCount(7);
        let mut reconstruction = Reconstruction::new();

        let dag = random_full_parent_units_up_to(MAX_ROUND, node_count, 43);
        for units in &dag {
            for unit in units {
                let round = unit.round();
                let ReconstructionResult { units, requests } =
                    reconstruction.add_unit(unit.clone());
                assert!(requests.is_empty());
                assert_eq!(units.len(), 1);
                match round {
                    0 => {
                        let mut parents_map: NodeMap<(_, _)> = NodeMap::with_size(node_count);
                        assert!(
                            ReconstructedUnit::with_parents(unit.clone(), parents_map.clone())
                                .is_ok(),
                            "Initial units should not have parents!"
                        );

                        let random_parent_index = rng.gen::<u64>() % node_count.0 as u64;
                        parents_map.insert(
                            NodeIndex(random_parent_index as usize),
                            (unit.hash(), 2 as Round),
                        );
                        assert_eq!(
                            ReconstructedUnit::with_parents(unit.clone(), parents_map),
                            Err(unit.clone()),
                            "Initial unit reconstructed with a non-empty parent!"
                        );
                    }
                    round => {
                        let mut parents_map: NodeMap<(_, _)> = NodeMap::with_size(node_count);
                        assert_eq!(
                            ReconstructedUnit::with_parents(unit.clone(), parents_map.clone()),
                            Err(unit.clone()),
                            "Non-initial rounds should have parents!"
                        );

                        let random_parent_index = rng.gen::<u64>() % node_count.0 as u64;
                        parents_map.insert(
                            NodeIndex(random_parent_index as usize),
                            (unit.hash(), round as Round),
                        );
                        assert_eq!(
                            ReconstructedUnit::with_parents(unit.clone(), parents_map.clone()),
                            Err(unit.clone()),
                            "Unit reconstructed with missing parents and wrong parent rounds!"
                        );

                        let this_unit_control_hash = unit.control_hash();
                        let mut parents: NodeMap<(_, _)> =
                            NodeMap::with_size(this_unit_control_hash.n_members());
                        for (node_index, &(hash, round)) in units[0].parents.iter() {
                            parents.insert(node_index, (hash, round));
                        }
                        assert!(
                            ReconstructedUnit::with_parents(unit.clone(), parents.clone()).is_ok(),
                            "Reconstructed unit control hash does not match unit's control hash!"
                        );
                        let random_parent_index = rng.gen::<u64>() % node_count.0 as u64;
                        let random_parent_index = NodeIndex(random_parent_index as usize);
                        let &(parent_hash, _) = parents.get(random_parent_index).unwrap();
                        let wrong_round = match round {
                            1 => MAX_ROUND,
                            _ => 0,
                        };
                        parents_map.insert(random_parent_index, (parent_hash, wrong_round));
                        assert_eq!(
                            ReconstructedUnit::with_parents(unit.clone(), parents_map.clone()),
                            Err(unit.clone()),
                            "Unit reconstructed with one parent having wrong round!"
                        );
                    }
                }
            }
        }
    }
}