aleph_bft_types/dataio.rs
1use async_trait::async_trait;
2
3use crate::{Data, Hasher, NodeIndex, Round};
4
5/// The source of data items that consensus should order.
6///
7/// AlephBFT internally calls [`DataProvider::get_data`] whenever a new unit is created and data
8/// needs to be placed inside.
9///
10/// We refer to the documentation
11/// https://cardinal-cryptography.github.io/AlephBFT/aleph_bft_api.html for a discussion and
12/// examples of how this trait can be implemented.
13#[async_trait]
14pub trait DataProvider: Sync + Send + 'static {
15 /// Type of data returned by this provider.
16 type Output: Data;
17 /// Outputs a new data item to be ordered.
18 async fn get_data(&mut self) -> Option<Self::Output>;
19}
20
21/// The source of finalization of the units that consensus produces.
22///
23/// The [`FinalizationHandler::data_finalized`] method is called whenever a piece of data input
24/// to the algorithm using [`DataProvider::get_data`] has been finalized, in order of finalization.
25pub trait FinalizationHandler<D: Data>: Sync + Send + 'static {
26 /// Data, provided by [DataProvider::get_data], has been finalized.
27 /// The calls to this function follow the order of finalization.
28 fn data_finalized(&mut self, data: D);
29}
30
31/// Represents state of the main internal data structure of AlephBFT (i.e. direct acyclic graph) used for
32/// achieving consensus.
33///
34/// Instances of this type are returned indirectly by [`member::run_session`] method using the
35/// [`UnitFinalizationHandler`] trait. This way it allows to reconstruct the DAG's structure used by AlephBFT,
36/// which can be then used for example for the purpose of node's performance evaluation.
37pub struct OrderedUnit<D: Data, H: Hasher> {
38 pub data: Option<D>,
39 pub parents: Vec<H::Hash>,
40 pub hash: H::Hash,
41 pub creator: NodeIndex,
42 pub round: Round,
43}
44
45/// The source of finalization of the units that consensus produces.
46///
47/// The [`UnitFinalizationHandler::batch_finalized`] method is called whenever a batch of units
48/// has been finalized, in order of finalization.
49pub trait UnitFinalizationHandler: Sync + Send + 'static {
50 type Data: Data;
51 type Hasher: Hasher;
52
53 /// A batch of units, that contains data provided by [DataProvider::get_data], has been finalized.
54 /// The calls to this function follow the order of finalization.
55 fn batch_finalized(&mut self, batch: Vec<OrderedUnit<Self::Data, Self::Hasher>>);
56}