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