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; ignore closure after the session consumer exits.
29        let _ = self.sender.try_send(OrderedUnit {
30            // Skipping a finalized unit would make us compute a different session
31            // outcome than our peers, so we must not swallow an invalid index here.
32            // It is unreachable regardless: a unit is only finalized after its
33            // signature was verified against our broadcast public key set, which
34            // requires its creator to be one of our peers.
35            creator: super::to_peer_id(creator)
36                .expect("Finalized units were verified against the broadcast public key set"),
37            round,
38            data,
39        });
40    }
41}