2020-07-20 18:34:23 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2020-09-21 11:54:06 -07:00
|
|
|
use displaydoc::Display;
|
|
|
|
use futures::{FutureExt, TryFutureExt};
|
2020-07-20 18:34:23 -07:00
|
|
|
use std::{
|
|
|
|
future::Future,
|
|
|
|
pin::Pin,
|
|
|
|
sync::Arc,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
2020-09-21 11:54:06 -07:00
|
|
|
use thiserror::Error;
|
2020-09-09 20:16:11 -07:00
|
|
|
use tower::{buffer::Buffer, util::BoxService, Service, ServiceExt};
|
2020-09-11 12:53:19 -07:00
|
|
|
use tracing::instrument;
|
2020-07-20 18:34:23 -07:00
|
|
|
|
2020-09-09 18:53:40 -07:00
|
|
|
use zebra_chain::{
|
|
|
|
block::{self, Block},
|
2021-03-12 10:09:39 -08:00
|
|
|
parameters::{Network, NetworkUpgrade::Canopy},
|
2020-09-09 18:53:40 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
use zebra_state as zs;
|
2020-07-20 18:34:23 -07:00
|
|
|
|
2020-09-09 20:16:11 -07:00
|
|
|
use crate::{
|
|
|
|
block::BlockVerifier,
|
2021-02-19 16:43:38 -08:00
|
|
|
block::VerifyBlockError,
|
|
|
|
checkpoint::{CheckpointList, CheckpointVerifier, VerifyCheckpointError},
|
2020-09-09 20:16:11 -07:00
|
|
|
BoxError, Config,
|
|
|
|
};
|
|
|
|
|
2021-02-19 16:43:38 -08:00
|
|
|
/// The bound for the chain verifier's buffer.
|
2021-02-08 14:13:55 -08:00
|
|
|
///
|
|
|
|
/// We choose the verifier buffer bound based on the maximum number of
|
|
|
|
/// concurrent verifier users, to avoid contention:
|
|
|
|
/// - the `ChainSync` component
|
|
|
|
/// - the `Inbound` service
|
|
|
|
/// - a miner component, which we might add in future, and
|
|
|
|
/// - 1 extra slot to avoid contention.
|
|
|
|
///
|
|
|
|
/// We deliberately add extra slots, because they only cost a small amount of
|
|
|
|
/// memory, but missing slots can significantly slow down Zebra.
|
|
|
|
const VERIFIER_BUFFER_BOUND: usize = 4;
|
|
|
|
|
2020-09-09 20:16:11 -07:00
|
|
|
/// The chain verifier routes requests to either the checkpoint verifier or the
|
|
|
|
/// block verifier, depending on the maximum checkpoint height.
|
|
|
|
struct ChainVerifier<S>
|
2020-09-02 21:23:57 -07:00
|
|
|
where
|
2020-09-09 20:16:11 -07:00
|
|
|
S: Service<zs::Request, Response = zs::Response, Error = BoxError> + Send + Clone + 'static,
|
2020-09-02 21:23:57 -07:00
|
|
|
S::Future: Send + 'static,
|
|
|
|
{
|
2021-02-19 16:43:38 -08:00
|
|
|
block: BlockVerifier<S>,
|
|
|
|
checkpoint: CheckpointVerifier<S>,
|
2020-09-09 20:16:11 -07:00
|
|
|
max_checkpoint_height: block::Height,
|
2020-07-20 18:34:23 -07:00
|
|
|
}
|
|
|
|
|
2020-09-21 11:54:06 -07:00
|
|
|
#[derive(Debug, Display, Error)]
|
|
|
|
pub enum VerifyChainError {
|
|
|
|
/// block could not be checkpointed
|
2021-02-19 16:43:38 -08:00
|
|
|
Checkpoint(#[source] VerifyCheckpointError),
|
2020-09-21 11:54:06 -07:00
|
|
|
/// block could not be verified
|
2021-02-19 16:43:38 -08:00
|
|
|
Block(#[source] VerifyBlockError),
|
2020-09-21 11:54:06 -07:00
|
|
|
}
|
|
|
|
|
2020-09-09 20:16:11 -07:00
|
|
|
impl<S> Service<Arc<Block>> for ChainVerifier<S>
|
2020-07-20 18:34:23 -07:00
|
|
|
where
|
2020-09-09 20:16:11 -07:00
|
|
|
S: Service<zs::Request, Response = zs::Response, Error = BoxError> + Send + Clone + 'static,
|
2020-07-20 18:34:23 -07:00
|
|
|
S::Future: Send + 'static,
|
|
|
|
{
|
2020-08-15 23:20:01 -07:00
|
|
|
type Response = block::Hash;
|
2020-09-21 11:54:06 -07:00
|
|
|
type Error = VerifyChainError;
|
2020-07-20 18:34:23 -07:00
|
|
|
type Future =
|
|
|
|
Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;
|
|
|
|
|
2021-02-19 16:43:38 -08:00
|
|
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2021-02-08 14:13:55 -08:00
|
|
|
// Correctness:
|
|
|
|
//
|
2021-02-19 16:43:38 -08:00
|
|
|
// We acquire checkpoint readiness before block readiness, to avoid an unlikely
|
|
|
|
// hang during the checkpoint to block verifier transition. If the checkpoint and
|
|
|
|
// block verifiers are contending for the same buffer/batch, we want the checkpoint
|
|
|
|
// verifier to win, so that checkpoint verification completes, and block verification
|
|
|
|
// can start. (Buffers and batches have multiple slots, so this contention is unlikely.)
|
|
|
|
use futures::ready;
|
|
|
|
// The chain verifier holds one slot in each verifier, for each concurrent task.
|
|
|
|
// Therefore, any shared buffers or batches polled by these verifiers should double
|
|
|
|
// their bounds. (For example, the state service buffer.)
|
|
|
|
ready!(self
|
|
|
|
.checkpoint
|
|
|
|
.poll_ready(cx)
|
|
|
|
.map_err(VerifyChainError::Checkpoint))?;
|
|
|
|
ready!(self.block.poll_ready(cx).map_err(VerifyChainError::Block))?;
|
2021-02-08 14:13:55 -08:00
|
|
|
Poll::Ready(Ok(()))
|
2020-07-20 18:34:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, block: Arc<Block>) -> Self::Future {
|
2020-10-21 21:06:21 -07:00
|
|
|
match block.coinbase_height() {
|
|
|
|
Some(height) if height <= self.max_checkpoint_height => self
|
2021-02-19 16:43:38 -08:00
|
|
|
.checkpoint
|
|
|
|
.call(block)
|
2020-10-21 21:06:21 -07:00
|
|
|
.map_err(VerifyChainError::Checkpoint)
|
|
|
|
.boxed(),
|
|
|
|
// This also covers blocks with no height, which the block verifier
|
|
|
|
// will reject immediately.
|
|
|
|
_ => self
|
2021-02-19 16:43:38 -08:00
|
|
|
.block
|
|
|
|
.call(block)
|
2020-10-21 21:06:21 -07:00
|
|
|
.map_err(VerifyChainError::Block)
|
|
|
|
.boxed(),
|
2020-07-20 18:34:23 -07:00
|
|
|
}
|
2020-07-29 03:51:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 11:43:17 -08:00
|
|
|
/// Initialize a block verification service.
|
|
|
|
///
|
|
|
|
/// The consensus configuration is specified by `config`, and the Zcash network
|
|
|
|
/// to verify blocks for is specified by `network`.
|
|
|
|
///
|
|
|
|
/// The block verification service asynchronously performs semantic verification
|
|
|
|
/// checks. Blocks that pass semantic verification are submitted to the supplied
|
|
|
|
/// `state_service` for contextual verification before being committed to the chain.
|
2020-07-23 18:47:48 -07:00
|
|
|
///
|
2020-09-09 20:16:11 -07:00
|
|
|
/// This function should only be called once for a particular state service.
|
2020-11-12 11:43:17 -08:00
|
|
|
///
|
|
|
|
/// Dropped requests are cancelled on a best-effort basis, but may continue to be processed.
|
2020-09-11 12:53:19 -07:00
|
|
|
#[instrument(skip(state_service))]
|
2020-07-23 18:47:48 -07:00
|
|
|
pub async fn init<S>(
|
2020-08-19 19:28:21 -07:00
|
|
|
config: Config,
|
2020-07-20 19:24:58 -07:00
|
|
|
network: Network,
|
2021-02-19 16:43:38 -08:00
|
|
|
mut state_service: S,
|
2020-09-21 11:54:06 -07:00
|
|
|
) -> Buffer<BoxService<Arc<Block>, block::Hash, VerifyChainError>, Arc<Block>>
|
2020-07-20 19:24:58 -07:00
|
|
|
where
|
2020-09-09 20:16:11 -07:00
|
|
|
S: Service<zs::Request, Response = zs::Response, Error = BoxError> + Send + Clone + 'static,
|
2020-07-20 19:24:58 -07:00
|
|
|
S::Future: Send + 'static,
|
|
|
|
{
|
2020-09-09 20:16:11 -07:00
|
|
|
let list = CheckpointList::new(network);
|
|
|
|
|
|
|
|
let max_checkpoint_height = if config.checkpoint_sync {
|
|
|
|
list.max_height()
|
|
|
|
} else {
|
2021-03-12 10:09:39 -08:00
|
|
|
list.min_height_in_range(Canopy.activation_height(network).unwrap()..)
|
|
|
|
.expect("hardcoded checkpoint list extends past canopy activation")
|
2020-09-09 20:16:11 -07:00
|
|
|
};
|
|
|
|
|
2020-09-10 09:49:13 -07:00
|
|
|
let tip = match state_service
|
2021-02-19 16:43:38 -08:00
|
|
|
.ready_and()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.call(zs::Request::Tip)
|
2020-09-09 18:53:40 -07:00
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
{
|
2020-09-09 20:16:11 -07:00
|
|
|
zs::Response::Tip(tip) => tip,
|
|
|
|
_ => unreachable!("wrong response to Request::Tip"),
|
2020-09-09 18:53:40 -07:00
|
|
|
};
|
2020-09-11 12:53:19 -07:00
|
|
|
tracing::info!(?tip, ?max_checkpoint_height, "initializing chain verifier");
|
2020-07-20 19:24:58 -07:00
|
|
|
|
2021-02-19 16:43:38 -08:00
|
|
|
let block = BlockVerifier::new(network, state_service.clone());
|
2021-03-10 18:07:37 -08:00
|
|
|
let checkpoint = CheckpointVerifier::from_checkpoint_list(list, network, tip, state_service);
|
2020-07-20 18:34:23 -07:00
|
|
|
|
|
|
|
Buffer::new(
|
2020-09-09 20:16:11 -07:00
|
|
|
BoxService::new(ChainVerifier {
|
2021-02-19 16:43:38 -08:00
|
|
|
block,
|
|
|
|
checkpoint,
|
2020-09-09 20:16:11 -07:00
|
|
|
max_checkpoint_height,
|
|
|
|
}),
|
2021-02-08 14:13:55 -08:00
|
|
|
VERIFIER_BUFFER_BOUND,
|
2020-07-20 18:34:23 -07:00
|
|
|
)
|
|
|
|
}
|