Skip to content

Commit

Permalink
consensus: add a (failing) verify round-trip test
Browse files Browse the repository at this point in the history
This test doesn't work, because the futures are not used correctly.

Part of ZcashFoundation#428.
  • Loading branch information
teor2345 committed Jun 12, 2020
1 parent ab781cb commit 431bee4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions zebra-consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
58 changes: 55 additions & 3 deletions zebra-consensus/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -131,10 +131,17 @@ mod tests {
.init();
}

/// Initialise and return an unwrapped `BlockVerifier`.
fn init_block_verifier<ZSF>(state_service: ZS<ZSF>) -> BlockVerifier<ZSF>
where
ZSF: Future<Output = Result<zebra_state::Response, ZSE>> + Send + 'static,
{
BlockVerifier::<ZSF> { state_service }
}

#[tokio::test]
#[spandoc::spandoc]
async fn verify() -> Result<(), Report> {
install_tracing();

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();
Expand All @@ -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(())
}
}

0 comments on commit 431bee4

Please sign in to comment.