fedimint_server/consensus/aleph_bft/
spawner.rs

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
use fedimint_core::task::TaskGroup;
use fedimint_logging::LOG_CONSENSUS;
use tracing::warn;

#[derive(Clone)]
pub struct Spawner {
    task_group: TaskGroup,
}

impl Spawner {
    pub fn new(task_group: TaskGroup) -> Self {
        Self { task_group }
    }
}

impl aleph_bft::SpawnHandle for Spawner {
    fn spawn(&self, name: &str, task: impl futures::Future<Output = ()> + Send + 'static) {
        self.task_group.spawn(name, |_| task);
    }

    fn spawn_essential(
        &self,
        name: &str,
        task: impl futures::Future<Output = ()> + Send + 'static,
    ) -> aleph_bft::TaskHandle {
        let (sender, receiver) = futures::channel::oneshot::channel();

        self.task_group.spawn(name, |_| async {
            task.await;

            if sender.send(()).is_err() {
                warn!(target: LOG_CONSENSUS, "Unable to send essential spawned task completion. Are we shutting down?");
            }
        });

        Box::pin(async { receiver.await.map_err(|_| ()) })
    }
}