From a998346f4cba7837f7a849646e0a91abd0834996 Mon Sep 17 00:00:00 2001 From: teor Date: Tue, 21 Jul 2020 12:24:58 +1000 Subject: [PATCH] refactor: Split out a simpler chain::init function This new chain::init function will let us hide the BlockVerifier and CheckpointVerifier from the zebra-consensus public interface. (If needed.) --- zebra-consensus/src/chain.rs | 41 +++++++++++++++++++++++++++++- zebra-consensus/src/chain/tests.rs | 11 +++++--- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/zebra-consensus/src/chain.rs b/zebra-consensus/src/chain.rs index d8f0b194..c3b3657d 100644 --- a/zebra-consensus/src/chain.rs +++ b/zebra-consensus/src/chain.rs @@ -28,6 +28,7 @@ use tower::{buffer::Buffer, Service, ServiceExt}; use zebra_chain::block::{Block, BlockHeaderHash}; use zebra_chain::types::BlockHeight; +use zebra_chain::Network; struct ChainVerifier { /// The underlying `BlockVerifier`, possibly wrapped in other services. @@ -123,6 +124,43 @@ where } } +/// Return a chain verification service, using `network` and the provided state +/// service. The network is used to create a block verifier and checkpoint +/// verifier. +/// +/// This function should only be called once for a particular state service. If +/// you need shared block or checkpoint verfiers, create them yourself, and pass +/// them to `init_from_verifiers`. +// +// TODO: revise this interface when we generate our own blocks, or validate +// mempool transactions. +// +// Only used by tests and other modules +#[allow(dead_code)] +pub fn init( + network: Network, + state_service: S, +) -> impl Service< + Arc, + Response = BlockHeaderHash, + Error = Error, + Future = impl Future>, +> + Send + + Clone + + 'static +where + S: Service + + Send + + Clone + + 'static, + S::Future: Send + 'static, +{ + let block_verifier = crate::block::init(state_service.clone()); + let checkpoint_verifier = CheckpointVerifier::new(network); + + init_from_verifiers(block_verifier, checkpoint_verifier, state_service) +} + /// Return a chain verification service, using the provided verifier and state /// services. /// @@ -141,8 +179,9 @@ where // // Only used by tests and other modules #[allow(dead_code)] -pub fn init( +pub fn init_from_verifiers( block_verifier: BV, + // We use an explcit type, so callers can't accidentally swap the verifiers checkpoint_verifier: CheckpointVerifier, state_service: S, ) -> impl Service< diff --git a/zebra-consensus/src/chain/tests.rs b/zebra-consensus/src/chain/tests.rs index d7d286fc..86e94792 100644 --- a/zebra-consensus/src/chain/tests.rs +++ b/zebra-consensus/src/chain/tests.rs @@ -64,7 +64,8 @@ fn verifiers_from_checkpoint_list( let block_verifier = crate::block::init(state_service.clone()); let checkpoint_verifier = crate::checkpoint::CheckpointVerifier::from_checkpoint_list(checkpoint_list); - let chain_verifier = super::init(block_verifier, checkpoint_verifier, state_service.clone()); + let chain_verifier = + super::init_from_verifiers(block_verifier, checkpoint_verifier, state_service.clone()); (chain_verifier, state_service) } @@ -153,7 +154,9 @@ async fn verify_checkpoint_test() -> Result<(), Report> { verify_checkpoint().await } -/// Test that checkpoint verifies work +/// Test that checkpoint verifies work. +/// +/// Also tests the `chain::init` function. #[spandoc::spandoc] async fn verify_checkpoint() -> Result<(), Report> { zebra_test::init(); @@ -162,7 +165,9 @@ async fn verify_checkpoint() -> Result<(), Report> { Arc::::zcash_deserialize(&zebra_test::vectors::BLOCK_MAINNET_GENESIS_BYTES[..])?; let hash: BlockHeaderHash = block.as_ref().into(); - let (mut chain_verifier, _) = verifiers_from_network(Mainnet); + // Test that the chain::init function works. Most of the other tests use + // init_from_verifiers. + let mut chain_verifier = super::init(Mainnet, zebra_state::in_memory::init()); /// SPANDOC: Make sure the verifier service is ready let ready_verifier_service = chain_verifier.ready_and().await.map_err(|e| eyre!(e))?;