Skip to main content

zebra_consensus/
primitives.rs

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