2020-07-20 18:31:01 -07:00
|
|
|
//! Block verification for Zebra.
|
2020-07-09 23:51:01 -07:00
|
|
|
//!
|
|
|
|
//! Verification occurs in multiple stages:
|
|
|
|
//! - getting blocks (disk- or network-bound)
|
|
|
|
//! - context-free verification of signatures, proofs, and scripts (CPU-bound)
|
2020-07-23 20:17:39 -07:00
|
|
|
//! - context-dependent verification of the chain state (depends on previous blocks)
|
2020-07-09 23:51:01 -07:00
|
|
|
//!
|
|
|
|
//! Verification is provided via a `tower::Service`, to support backpressure and batch
|
|
|
|
//! verification.
|
|
|
|
|
|
|
|
use std::{
|
|
|
|
future::Future,
|
|
|
|
pin::Pin,
|
|
|
|
sync::Arc,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
2020-09-09 18:53:40 -07:00
|
|
|
|
|
|
|
use chrono::Utc;
|
|
|
|
use futures_util::FutureExt;
|
2020-09-21 11:54:06 -07:00
|
|
|
use thiserror::Error;
|
2020-09-09 20:16:11 -07:00
|
|
|
use tower::{Service, ServiceExt};
|
2020-07-09 23:51:01 -07:00
|
|
|
|
2020-09-21 11:54:06 -07:00
|
|
|
use zebra_chain::{
|
|
|
|
block::{self, Block},
|
|
|
|
work::equihash,
|
|
|
|
};
|
2020-09-09 18:53:40 -07:00
|
|
|
use zebra_state as zs;
|
|
|
|
|
2020-09-21 11:54:06 -07:00
|
|
|
use crate::error::*;
|
2020-09-09 18:53:40 -07:00
|
|
|
use crate::BoxError;
|
|
|
|
|
|
|
|
mod check;
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
2020-07-09 23:51:01 -07:00
|
|
|
|
2020-07-30 17:21:20 -07:00
|
|
|
/// A service that verifies blocks.
|
|
|
|
#[derive(Debug)]
|
2020-09-09 20:16:11 -07:00
|
|
|
pub struct BlockVerifier<S>
|
2020-07-23 18:47:48 -07:00
|
|
|
where
|
2020-09-09 18:53:40 -07:00
|
|
|
S: Service<zs::Request, Response = zs::Response, Error = BoxError> + Send + Clone + 'static,
|
2020-07-23 18:47:48 -07:00
|
|
|
S::Future: Send + 'static,
|
|
|
|
{
|
2020-09-02 21:23:57 -07:00
|
|
|
/// The underlying state service, possibly wrapped in other services.
|
2020-07-09 23:51:01 -07:00
|
|
|
state_service: S,
|
|
|
|
}
|
|
|
|
|
2020-09-21 11:54:06 -07:00
|
|
|
#[non_exhaustive]
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum VerifyBlockError {
|
|
|
|
#[error("unable to verify depth for block {hash} from chain state during block verification")]
|
|
|
|
Depth { source: BoxError, hash: block::Hash },
|
|
|
|
#[error(transparent)]
|
|
|
|
Block {
|
|
|
|
#[from]
|
|
|
|
source: BlockError,
|
|
|
|
},
|
|
|
|
#[error(transparent)]
|
|
|
|
Equihash {
|
|
|
|
#[from]
|
|
|
|
source: equihash::Error,
|
|
|
|
},
|
|
|
|
#[error(transparent)]
|
|
|
|
Time(BoxError),
|
|
|
|
#[error("unable to commit block after semantic verification")]
|
|
|
|
Commit(#[source] BoxError),
|
|
|
|
}
|
|
|
|
|
2020-09-09 20:16:11 -07:00
|
|
|
impl<S> BlockVerifier<S>
|
|
|
|
where
|
|
|
|
S: Service<zs::Request, Response = zs::Response, Error = BoxError> + Send + Clone + 'static,
|
|
|
|
S::Future: Send + 'static,
|
|
|
|
{
|
|
|
|
pub fn new(state_service: S) -> Self {
|
|
|
|
Self { state_service }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 23:51:01 -07:00
|
|
|
impl<S> Service<Arc<Block>> for BlockVerifier<S>
|
|
|
|
where
|
2020-09-09 18:53:40 -07:00
|
|
|
S: Service<zs::Request, Response = zs::Response, Error = BoxError> + Send + Clone + 'static,
|
2020-07-09 23:51:01 -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 = VerifyBlockError;
|
2020-07-09 23:51:01 -07:00
|
|
|
type Future =
|
|
|
|
Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2020-07-20 18:31:01 -07:00
|
|
|
// We use the state for contextual verification, and we expect those
|
|
|
|
// queries to be fast. So we don't need to call
|
|
|
|
// `state_service.poll_ready()` here.
|
2020-07-09 23:51:01 -07:00
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, block: Arc<Block>) -> Self::Future {
|
2020-09-02 21:23:57 -07:00
|
|
|
let mut state_service = self.state_service.clone();
|
2020-07-09 23:51:01 -07:00
|
|
|
|
2020-07-23 20:17:39 -07:00
|
|
|
// TODO(jlusby): Error = Report, handle errors from state_service.
|
2020-07-09 23:51:01 -07:00
|
|
|
async move {
|
2020-08-05 22:26:26 -07:00
|
|
|
let hash = block.hash();
|
|
|
|
|
2020-09-09 18:53:40 -07:00
|
|
|
// Check that this block is actually a new block.
|
2020-09-21 11:54:06 -07:00
|
|
|
match state_service
|
|
|
|
.ready_and()
|
|
|
|
.await
|
|
|
|
.map_err(|source| VerifyBlockError::Depth { source, hash })?
|
|
|
|
.call(zs::Request::Depth(hash))
|
|
|
|
.await
|
|
|
|
.map_err(|source| VerifyBlockError::Depth { source, hash })?
|
|
|
|
{
|
2020-09-09 18:53:40 -07:00
|
|
|
zs::Response::Depth(Some(depth)) => {
|
2020-09-21 11:54:06 -07:00
|
|
|
return Err(BlockError::AlreadyInChain(hash, depth).into())
|
|
|
|
}
|
|
|
|
zs::Response::Depth(None) => {}
|
2020-09-09 18:53:40 -07:00
|
|
|
_ => unreachable!("wrong response to Request::Depth"),
|
|
|
|
}
|
|
|
|
|
2020-08-05 22:26:26 -07:00
|
|
|
// These checks only apply to generated blocks. We check the block
|
|
|
|
// height for parsed blocks when we deserialize them.
|
|
|
|
let height = block
|
|
|
|
.coinbase_height()
|
2020-09-21 11:54:06 -07:00
|
|
|
.ok_or_else(|| BlockError::MissingHeight(hash))?;
|
2020-08-16 11:42:02 -07:00
|
|
|
if height > block::Height::MAX {
|
2020-09-21 11:54:06 -07:00
|
|
|
Err(BlockError::MaxHeight(height, hash, block::Height::MAX))?;
|
2020-08-05 22:26:26 -07:00
|
|
|
}
|
|
|
|
|
2020-08-03 00:37:08 -07:00
|
|
|
// Do the difficulty checks first, to raise the threshold for
|
|
|
|
// attacks that use any other fields.
|
2020-09-02 19:50:16 -07:00
|
|
|
let difficulty_threshold = block
|
|
|
|
.header
|
|
|
|
.difficulty_threshold
|
|
|
|
.to_expanded()
|
2020-09-21 11:54:06 -07:00
|
|
|
.ok_or_else(|| BlockError::InvalidDifficulty(height, hash))?;
|
2020-08-03 00:37:08 -07:00
|
|
|
if hash > difficulty_threshold {
|
2020-09-21 11:54:06 -07:00
|
|
|
Err(BlockError::DifficultyFilter(
|
|
|
|
height,
|
|
|
|
hash,
|
|
|
|
difficulty_threshold,
|
|
|
|
))?;
|
2020-08-03 00:37:08 -07:00
|
|
|
}
|
2020-09-03 18:56:09 -07:00
|
|
|
check::is_equihash_solution_valid(&block.header)?;
|
2020-08-03 00:37:08 -07:00
|
|
|
|
2020-07-09 23:51:01 -07:00
|
|
|
// Since errors cause an early exit, try to do the
|
|
|
|
// quick checks first.
|
|
|
|
|
2020-08-03 00:37:08 -07:00
|
|
|
// Field validity and structure checks
|
2020-07-09 23:51:01 -07:00
|
|
|
let now = Utc::now();
|
2020-09-21 11:54:06 -07:00
|
|
|
check::is_time_valid_at(&block.header, now).map_err(VerifyBlockError::Time)?;
|
2020-08-18 10:47:57 -07:00
|
|
|
check::is_coinbase_first(&block)?;
|
2020-07-09 23:51:01 -07:00
|
|
|
|
2020-09-02 19:50:16 -07:00
|
|
|
// TODO: context-free header verification: merkle root
|
2020-08-03 00:37:08 -07:00
|
|
|
|
2020-09-02 19:50:16 -07:00
|
|
|
tracing::trace!("verified block");
|
2020-09-21 11:54:06 -07:00
|
|
|
metrics::gauge!("block.verified.block.height", height.0 as _);
|
2020-09-03 15:06:21 -07:00
|
|
|
metrics::counter!("block.verified.block.count", 1);
|
|
|
|
|
2020-09-09 18:53:40 -07:00
|
|
|
// Finally, submit the block for contextual verification.
|
2020-09-10 10:34:59 -07:00
|
|
|
match state_service
|
|
|
|
.ready_and()
|
2020-09-21 11:54:06 -07:00
|
|
|
.await
|
|
|
|
.map_err(VerifyBlockError::Commit)?
|
2020-09-10 10:34:59 -07:00
|
|
|
.call(zs::Request::CommitBlock { block })
|
2020-09-21 11:54:06 -07:00
|
|
|
.await
|
|
|
|
.map_err(VerifyBlockError::Commit)?
|
2020-09-10 10:34:59 -07:00
|
|
|
{
|
2020-09-09 18:53:40 -07:00
|
|
|
zs::Response::Committed(committed_hash) => {
|
2020-09-10 10:34:59 -07:00
|
|
|
assert_eq!(committed_hash, hash, "state must commit correct hash");
|
2020-09-02 21:23:57 -07:00
|
|
|
Ok(hash)
|
|
|
|
}
|
2020-09-10 10:34:59 -07:00
|
|
|
_ => unreachable!("wrong response for CommitBlock"),
|
2020-09-02 21:23:57 -07:00
|
|
|
}
|
2020-07-09 23:51:01 -07:00
|
|
|
}
|
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
}
|