2020-07-09 23:51:01 -07:00
|
|
|
//! Tests for redjubjub signature verification
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
use std::time::Duration;
|
|
|
|
|
2020-07-16 12:20:06 -07:00
|
|
|
use color_eyre::eyre::{eyre, Result};
|
2020-07-09 23:51:01 -07:00
|
|
|
use futures::stream::{FuturesUnordered, StreamExt};
|
|
|
|
use tower::ServiceExt;
|
|
|
|
use tower_batch::Batch;
|
|
|
|
|
|
|
|
async fn sign_and_verify<V>(mut verifier: V, n: usize) -> Result<(), V::Error>
|
|
|
|
where
|
|
|
|
V: Service<Item, Response = ()>,
|
|
|
|
{
|
|
|
|
let rng = thread_rng();
|
|
|
|
let mut results = FuturesUnordered::new();
|
|
|
|
for i in 0..n {
|
|
|
|
let span = tracing::trace_span!("sig", i);
|
|
|
|
let msg = b"BatchVerifyTest";
|
|
|
|
|
|
|
|
match i % 2 {
|
|
|
|
0 => {
|
|
|
|
let sk = SigningKey::<SpendAuth>::new(rng);
|
|
|
|
let vk = VerificationKey::from(&sk);
|
|
|
|
let sig = sk.sign(rng, &msg[..]);
|
|
|
|
verifier.ready_and().await?;
|
|
|
|
results.push(span.in_scope(|| verifier.call((vk.into(), sig, msg).into())))
|
|
|
|
}
|
|
|
|
1 => {
|
|
|
|
let sk = SigningKey::<Binding>::new(rng);
|
|
|
|
let vk = VerificationKey::from(&sk);
|
|
|
|
let sig = sk.sign(rng, &msg[..]);
|
|
|
|
verifier.ready_and().await?;
|
|
|
|
results.push(span.in_scope(|| verifier.call((vk.into(), sig, msg).into())))
|
|
|
|
}
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while let Some(result) = results.next().await {
|
|
|
|
result?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[spandoc::spandoc]
|
|
|
|
async fn batch_flushes_on_max_items() -> Result<()> {
|
|
|
|
use tokio::time::timeout;
|
|
|
|
|
|
|
|
// Use a very long max_latency and a short timeout to check that
|
|
|
|
// flushing is happening based on hitting max_items.
|
|
|
|
let verifier = Batch::new(Verifier::new(), 10, Duration::from_secs(1000));
|
2020-07-16 12:20:06 -07:00
|
|
|
timeout(Duration::from_secs(5), sign_and_verify(verifier, 100))
|
2020-07-22 18:01:31 -07:00
|
|
|
.await?
|
2020-07-16 12:20:06 -07:00
|
|
|
.map_err(|e| eyre!(e))?;
|
|
|
|
|
|
|
|
Ok(())
|
2020-07-09 23:51:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[spandoc::spandoc]
|
|
|
|
async fn batch_flushes_on_max_latency() -> Result<()> {
|
|
|
|
use tokio::time::timeout;
|
|
|
|
|
|
|
|
// Use a very high max_items and a short timeout to check that
|
|
|
|
// flushing is happening based on hitting max_latency.
|
|
|
|
let verifier = Batch::new(Verifier::new(), 100, Duration::from_millis(500));
|
2020-07-16 12:20:06 -07:00
|
|
|
timeout(Duration::from_secs(5), sign_and_verify(verifier, 10))
|
2020-07-22 18:01:31 -07:00
|
|
|
.await?
|
2020-07-16 12:20:06 -07:00
|
|
|
.map_err(|e| eyre!(e))?;
|
|
|
|
|
|
|
|
Ok(())
|
2020-07-09 23:51:01 -07:00
|
|
|
}
|