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:
parent
a722cf33f7
commit
a998346f4c
|
@ -28,6 +28,7 @@ use tower::{buffer::Buffer, Service, ServiceExt};
|
||||||
|
|
||||||
use zebra_chain::block::{Block, BlockHeaderHash};
|
use zebra_chain::block::{Block, BlockHeaderHash};
|
||||||
use zebra_chain::types::BlockHeight;
|
use zebra_chain::types::BlockHeight;
|
||||||
|
use zebra_chain::Network;
|
||||||
|
|
||||||
struct ChainVerifier<BV, S> {
|
struct ChainVerifier<BV, S> {
|
||||||
/// The underlying `BlockVerifier`, possibly wrapped in other services.
|
/// 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
|
/// Return a chain verification service, using the provided verifier and state
|
||||||
/// services.
|
/// services.
|
||||||
///
|
///
|
||||||
|
@ -141,8 +179,9 @@ where
|
||||||
//
|
//
|
||||||
// Only used by tests and other modules
|
// Only used by tests and other modules
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn init<BV, S>(
|
pub fn init_from_verifiers<BV, S>(
|
||||||
block_verifier: BV,
|
block_verifier: BV,
|
||||||
|
// We use an explcit type, so callers can't accidentally swap the verifiers
|
||||||
checkpoint_verifier: CheckpointVerifier,
|
checkpoint_verifier: CheckpointVerifier,
|
||||||
state_service: S,
|
state_service: S,
|
||||||
) -> impl Service<
|
) -> impl Service<
|
||||||
|
|
|
@ -64,7 +64,8 @@ fn verifiers_from_checkpoint_list(
|
||||||
let block_verifier = crate::block::init(state_service.clone());
|
let block_verifier = crate::block::init(state_service.clone());
|
||||||
let checkpoint_verifier =
|
let checkpoint_verifier =
|
||||||
crate::checkpoint::CheckpointVerifier::from_checkpoint_list(checkpoint_list);
|
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)
|
(chain_verifier, state_service)
|
||||||
}
|
}
|
||||||
|
@ -153,7 +154,9 @@ async fn verify_checkpoint_test() -> Result<(), Report> {
|
||||||
verify_checkpoint().await
|
verify_checkpoint().await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Test that checkpoint verifies work
|
/// Test that checkpoint verifies work.
|
||||||
|
///
|
||||||
|
/// Also tests the `chain::init` function.
|
||||||
#[spandoc::spandoc]
|
#[spandoc::spandoc]
|
||||||
async fn verify_checkpoint() -> Result<(), Report> {
|
async fn verify_checkpoint() -> Result<(), Report> {
|
||||||
zebra_test::init();
|
zebra_test::init();
|
||||||
|
@ -162,7 +165,9 @@ async fn verify_checkpoint() -> Result<(), Report> {
|
||||||
Arc::<Block>::zcash_deserialize(&zebra_test::vectors::BLOCK_MAINNET_GENESIS_BYTES[..])?;
|
Arc::<Block>::zcash_deserialize(&zebra_test::vectors::BLOCK_MAINNET_GENESIS_BYTES[..])?;
|
||||||
let hash: BlockHeaderHash = block.as_ref().into();
|
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
|
/// SPANDOC: Make sure the verifier service is ready
|
||||||
let ready_verifier_service = chain_verifier.ready_and().await.map_err(|e| eyre!(e))?;
|
let ready_verifier_service = chain_verifier.ready_and().await.map_err(|e| eyre!(e))?;
|
||||||
|
|
Loading…
Reference in New Issue