From 15bc64aed9a888296fa547fd3148ac8833194d95 Mon Sep 17 00:00:00 2001 From: teor Date: Thu, 11 Jun 2020 22:14:07 +1000 Subject: [PATCH] consensus: add a (failing) verify round-trip test This test doesn't work, because the futures are not used correctly. Part of #428. --- Cargo.lock | 1 + zebra-consensus/Cargo.toml | 1 + zebra-consensus/src/verify.rs | 60 ++++++++++++++++++++++++++++++++--- 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 172abe64..04f01958 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2128,6 +2128,7 @@ dependencies = [ "color-eyre", "eyre", "futures-util", + "spandoc", "tokio", "tower", "tracing", diff --git a/zebra-consensus/Cargo.toml b/zebra-consensus/Cargo.toml index 81f0eeec..60253f43 100644 --- a/zebra-consensus/Cargo.toml +++ b/zebra-consensus/Cargo.toml @@ -17,6 +17,7 @@ tower = "0.3.1" zebra-test-vectors = { path = "../zebra-test-vectors/" } color-eyre = "0.3.4" eyre = "0.4.2" +spandoc = { git = "https://github.com/yaahc/spandoc.git" } tokio = { version = "0.2.21", features = ["full"] } tracing = "0.1.15" tracing-error = "0.1.2" diff --git a/zebra-consensus/src/verify.rs b/zebra-consensus/src/verify.rs index 5232f625..bb1818d7 100644 --- a/zebra-consensus/src/verify.rs +++ b/zebra-consensus/src/verify.rs @@ -110,7 +110,7 @@ where mod tests { use super::*; use color_eyre::Report; - use eyre::{ensure, eyre}; + use eyre::{bail, ensure, eyre}; use tower::{util::ServiceExt, Service}; use zebra_chain::serialization::ZcashDeserialize; @@ -131,10 +131,17 @@ mod tests { .init(); } - #[tokio::test] - async fn verify() -> Result<(), Report> { - install_tracing(); + /// Initialise and return an unwrapped `BlockVerifier`. + fn init_block_verifier(state_service: ZS) -> BlockVerifier + where + ZSF: Future> + Send + 'static, + { + BlockVerifier:: { state_service } + } + #[tokio::test] + #[spandoc::spandoc] + async fn verify() -> Result<(), Report> { let block = Block::zcash_deserialize(&zebra_test_vectors::BLOCK_MAINNET_415000_BYTES[..])?; // TODO(teor): why does rustc say that _hash is unused? let _hash: BlockHeaderHash = (&block).into(); @@ -158,4 +165,49 @@ mod tests { Ok(()) } + + #[tokio::test] + #[spandoc::spandoc] + async fn round_trip() -> Result<(), Report> { + install_tracing(); + + let block = Block::zcash_deserialize(&zebra_test_vectors::BLOCK_MAINNET_GENESIS_BYTES[..])?; + // TODO(teor): why does rustc say that _hash is unused? + let _hash: BlockHeaderHash = (&block).into(); + + let state_service = Box::new(zebra_state::in_memory::init()); + let mut block_verifier = init_block_verifier(state_service); + + let verify_response = block_verifier + .ready_and() + .await + .map_err(|e| eyre!(e))? + .call(block.clone()) + .await + .map_err(|e| eyre!(e))?; + + ensure!( + matches!(verify_response, _hash), + "unexpected response kind: {:?}", + verify_response + ); + + let state_response = block_verifier + .state_service + .ready_and() + .await + .map_err(|e| eyre!(e))? + .call(zebra_state::Request::GetBlock { hash: _hash }) + .await + .map_err(|e| eyre!(e))?; + + match state_response { + zebra_state::Response::Block { + block: returned_block, + } => assert_eq!(&block, returned_block.as_ref()), + _ => bail!("unexpected response kind: {:?}", state_response), + } + + Ok(()) + } }