zebra_consensus/
primitives.rs

1//! Asynchronous verification of cryptographic primitives.
2
3use tokio::sync::oneshot::error::RecvError;
4
5use crate::BoxError;
6
7pub mod ed25519;
8pub mod groth16;
9pub mod halo2;
10pub mod redjubjub;
11pub mod redpallas;
12pub mod sapling;
13
14/// The maximum batch size for any of the batch verifiers.
15const MAX_BATCH_SIZE: usize = 64;
16
17/// The maximum latency bound for any of the batch verifiers.
18const MAX_BATCH_LATENCY: std::time::Duration = std::time::Duration::from_millis(100);
19
20/// Fires off a task into the Rayon threadpool, awaits the result through a oneshot channel,
21/// then converts the error to a [`BoxError`].
22pub async fn spawn_fifo_and_convert<
23    E: 'static + std::error::Error + Into<BoxError> + Sync + Send,
24    F: 'static + FnOnce() -> Result<(), E> + Send,
25>(
26    f: F,
27) -> Result<(), BoxError> {
28    spawn_fifo(f)
29        .await
30        .map_err(|_| {
31            "threadpool unexpectedly dropped response channel sender. Is Zebra shutting down?"
32        })?
33        .map_err(BoxError::from)
34}
35
36/// Fires off a task into the Rayon threadpool and awaits the result through a oneshot channel.
37pub async fn spawn_fifo<T: 'static + Send, F: 'static + FnOnce() -> T + Send>(
38    f: F,
39) -> Result<T, RecvError> {
40    // Rayon doesn't have a spawn function that returns a value,
41    // so we use a oneshot channel instead.
42    let (rsp_tx, rsp_rx) = tokio::sync::oneshot::channel();
43
44    rayon::spawn_fifo(move || {
45        let _ = rsp_tx.send(f());
46    });
47
48    rsp_rx.await
49}