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

#[derive(Clone)]
pub struct Spawner;

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

impl Default for Spawner {
    fn default() -> Self {
        Self::new()
    }
}

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

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

        fedimint_core::runtime::spawn(name, async move {
            task.await;
            if let Err(_err) = res_tx.send(()) {
                warn!(target: LOG_CONSENSUS, "Unable to send essential spawned task completion. Are we shutting down?");
            }
        });

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