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.)
This commit is contained in:
teor 2020-07-21 12:24:58 +10:00
parent a722cf33f7
commit a998346f4c
2 changed files with 48 additions and 4 deletions

View File

@ -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<BV, S> {
/// 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<S>(
network: Network,
state_service: S,
) -> impl Service<
Arc<Block>,
Response = BlockHeaderHash,
Error = Error,
Future = impl Future<Output = Result<BlockHeaderHash, Error>>,
> + Send
+ Clone
+ 'static
where
S: Service<zebra_state::Request, Response = zebra_state::Response, Error = Error>
+ 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<BV, S>(
pub fn init_from_verifiers<BV, S>(
block_verifier: BV,
// We use an explcit type, so callers can't accidentally swap the verifiers
checkpoint_verifier: CheckpointVerifier,
state_service: S,
) -> impl Service<

View File

@ -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::<Block>::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))?;