Skip to main content

fedimint_server/consensus/aleph_bft/
finalization_handler.rs

1use aleph_bft::{NodeIndex, Round};
2use fedimint_core::PeerId;
3
4use super::data_provider::UnitData;
5
6pub struct OrderedUnit {
7    pub creator: PeerId,
8    pub round: Round,
9    pub data: Option<UnitData>,
10}
11
12pub struct FinalizationHandler {
13    sender: async_channel::Sender<OrderedUnit>,
14}
15
16impl FinalizationHandler {
17    pub fn new(sender: async_channel::Sender<OrderedUnit>) -> Self {
18        Self { sender }
19    }
20}
21
22impl aleph_bft::FinalizationHandler<UnitData> for FinalizationHandler {
23    fn data_finalized(&mut self, _data: UnitData) {
24        unreachable!("This method is not called")
25    }
26
27    fn unit_finalized(&mut self, creator: NodeIndex, round: Round, data: Option<UnitData>) {
28        // the channel is unbounded
29        self.sender
30            .try_send(OrderedUnit {
31                // Skipping a finalized unit would make us compute a different session
32                // outcome than our peers, so we must not swallow an invalid index here.
33                // It is unreachable regardless: a unit is only finalized after its
34                // signature was verified against our broadcast public key set, which
35                // requires its creator to be one of our peers.
36                creator: super::to_peer_id(creator)
37                    .expect("Finalized units were verified against the broadcast public key set"),
38                round,
39                data,
40            })
41            .ok();
42    }
43}