diff --git a/zebra-consensus/src/verify.rs b/zebra-consensus/src/verify.rs index 7a230d766f1..551ed022504 100644 --- a/zebra-consensus/src/verify.rs +++ b/zebra-consensus/src/verify.rs @@ -21,12 +21,27 @@ use zebra_chain::block::{Block, BlockHeaderHash}; mod script; mod transaction; +/// The trait constraints that we expect from `zebra_state::ZebraState` errors. +type ZSE = Box; +/// The trait constraints that we expect from the `zebra_state::ZebraState` service. +/// `ZSF` is the `Future` type for `zebra_state::ZebraState`. This type is generic, +/// because `tower::Service` function calls require a `Sized` future type. +type ZS = Box< + dyn Service + + Send + + 'static, +>; + /// Block verification service. /// /// After verification, blocks and their associated transactions are added to /// `zebra_state::ZebraState`. -#[derive(Default)] -struct BlockVerifier {} +struct BlockVerifier +where + ZSF: Future> + Send + 'static, +{ + state_service: ZS, +} /// The result type for the BlockVerifier Service. type Response = BlockHeaderHash; @@ -36,21 +51,20 @@ type Response = BlockHeaderHash; type Error = Box; /// The BlockVerifier service implementation. -impl Service for BlockVerifier { +impl Service for BlockVerifier +where + ZSF: Future> + Send + 'static, +{ type Response = Response; type Error = Error; type Future = Pin> + Send + 'static>>; fn poll_ready(&mut self, context: &mut Context<'_>) -> Poll> { - // TODO(teor): is this a shared state? - let mut state_service = zebra_state::in_memory::init(); - state_service.poll_ready(context) + self.state_service.poll_ready(context) } fn call(&mut self, block: Block) -> Self::Future { - let mut state_service = zebra_state::in_memory::init(); - let header_hash: BlockHeaderHash = (&block).into(); // Ignore errors for now. @@ -58,7 +72,7 @@ impl Service for BlockVerifier { // TODO(teor): // - handle chain reorgs, adjust state_service "unique block height" conditions // - handle block validation errors (including errors in the block's transactions) - let _ = state_service.call(zebra_state::Request::AddBlock { + let _: ZSF = self.state_service.call(zebra_state::Request::AddBlock { block: block.into(), }); @@ -67,13 +81,18 @@ impl Service for BlockVerifier { } /// Initialise the BlockVerifier service. -pub fn init() -> impl Service< +pub fn init( + state_service: ZS, +) -> impl Service< Block, Response = Response, Error = Error, Future = impl Future>, > + Send + Clone - + 'static { - Buffer::new(BlockVerifier::default(), 1) + + 'static +where + ZSF: Future> + Send + 'static, +{ + Buffer::new(BlockVerifier:: { state_service }, 1) }