2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-09-27 16:50:24 +02:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
//! Definition of valid items for the verification queue.
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
use engines::EthEngine;
|
2016-09-27 16:50:24 +02:00
|
|
|
use error::Error;
|
|
|
|
|
2017-08-30 16:04:47 +02:00
|
|
|
use heapsize::HeapSizeOf;
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::{H256, U256};
|
2016-09-27 16:50:24 +02:00
|
|
|
|
|
|
|
pub use self::blocks::Blocks;
|
|
|
|
pub use self::headers::Headers;
|
|
|
|
|
|
|
|
/// Something which can produce a hash and a parent hash.
|
2016-12-23 18:43:40 +01:00
|
|
|
pub trait BlockLike {
|
2016-09-27 16:50:24 +02:00
|
|
|
/// Get the hash of this item.
|
|
|
|
fn hash(&self) -> H256;
|
|
|
|
|
|
|
|
/// Get the hash of this item's parent.
|
|
|
|
fn parent_hash(&self) -> H256;
|
2016-12-23 18:43:40 +01:00
|
|
|
|
|
|
|
/// Get the difficulty of this item.
|
|
|
|
fn difficulty(&self) -> U256;
|
2016-09-27 16:50:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Defines transitions between stages of verification.
|
|
|
|
///
|
|
|
|
/// It starts with a fallible transformation from an "input" into the unverified item.
|
|
|
|
/// This consists of quick, simply done checks as well as extracting particular data.
|
|
|
|
///
|
|
|
|
/// Then, there is a `verify` function which performs more expensive checks and
|
|
|
|
/// produces the verified output.
|
|
|
|
///
|
|
|
|
/// For correctness, the hashes produced by each stage of the pipeline should be
|
|
|
|
/// consistent.
|
|
|
|
pub trait Kind: 'static + Sized + Send + Sync {
|
|
|
|
/// The first stage: completely unverified.
|
2016-12-23 18:43:40 +01:00
|
|
|
type Input: Sized + Send + BlockLike + HeapSizeOf;
|
2016-09-27 16:50:24 +02:00
|
|
|
|
|
|
|
/// The second stage: partially verified.
|
2016-12-23 18:43:40 +01:00
|
|
|
type Unverified: Sized + Send + BlockLike + HeapSizeOf;
|
2016-09-27 16:50:24 +02:00
|
|
|
|
|
|
|
/// The third stage: completely verified.
|
2016-12-23 18:43:40 +01:00
|
|
|
type Verified: Sized + Send + BlockLike + HeapSizeOf;
|
2016-09-27 16:50:24 +02:00
|
|
|
|
|
|
|
/// Attempt to create the `Unverified` item from the input.
|
2017-09-26 14:19:08 +02:00
|
|
|
fn create(input: Self::Input, engine: &EthEngine) -> Result<Self::Unverified, Error>;
|
2016-09-27 16:50:24 +02:00
|
|
|
|
|
|
|
/// Attempt to verify the `Unverified` item using the given engine.
|
2017-09-26 14:19:08 +02:00
|
|
|
fn verify(unverified: Self::Unverified, engine: &EthEngine, check_seal: bool) -> Result<Self::Verified, Error>;
|
2016-09-27 16:50:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The blocks verification module.
|
|
|
|
pub mod blocks {
|
2016-12-23 18:43:40 +01:00
|
|
|
use super::{Kind, BlockLike};
|
2016-09-27 16:50:24 +02:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
use engines::EthEngine;
|
2016-09-27 16:50:24 +02:00
|
|
|
use error::Error;
|
|
|
|
use header::Header;
|
|
|
|
use verification::{PreverifiedBlock, verify_block_basic, verify_block_unordered};
|
|
|
|
|
2017-08-30 16:04:47 +02:00
|
|
|
use heapsize::HeapSizeOf;
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::{H256, U256};
|
2017-09-06 20:47:45 +02:00
|
|
|
use bytes::Bytes;
|
2016-09-27 16:50:24 +02:00
|
|
|
|
|
|
|
/// A mode for verifying blocks.
|
|
|
|
pub struct Blocks;
|
|
|
|
|
|
|
|
impl Kind for Blocks {
|
|
|
|
type Input = Unverified;
|
|
|
|
type Unverified = Unverified;
|
|
|
|
type Verified = PreverifiedBlock;
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
fn create(input: Self::Input, engine: &EthEngine) -> Result<Self::Unverified, Error> {
|
2016-09-27 16:50:24 +02:00
|
|
|
match verify_block_basic(&input.header, &input.bytes, engine) {
|
|
|
|
Ok(()) => Ok(input),
|
|
|
|
Err(e) => {
|
|
|
|
warn!(target: "client", "Stage 1 block verification failed for {}: {:?}", input.hash(), e);
|
|
|
|
Err(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
fn verify(un: Self::Unverified, engine: &EthEngine, check_seal: bool) -> Result<Self::Verified, Error> {
|
2016-09-27 16:50:24 +02:00
|
|
|
let hash = un.hash();
|
2016-10-24 15:09:13 +02:00
|
|
|
match verify_block_unordered(un.header, un.bytes, engine, check_seal) {
|
2016-09-27 16:50:24 +02:00
|
|
|
Ok(verified) => Ok(verified),
|
|
|
|
Err(e) => {
|
|
|
|
warn!(target: "client", "Stage 2 block verification failed for {}: {:?}", hash, e);
|
|
|
|
Err(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An unverified block.
|
|
|
|
pub struct Unverified {
|
|
|
|
header: Header,
|
|
|
|
bytes: Bytes,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Unverified {
|
|
|
|
/// Create an `Unverified` from raw bytes.
|
|
|
|
pub fn new(bytes: Bytes) -> Self {
|
|
|
|
use views::BlockView;
|
|
|
|
|
|
|
|
let header = BlockView::new(&bytes).header();
|
|
|
|
Unverified {
|
|
|
|
header: header,
|
|
|
|
bytes: bytes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HeapSizeOf for Unverified {
|
|
|
|
fn heap_size_of_children(&self) -> usize {
|
|
|
|
self.header.heap_size_of_children() + self.bytes.heap_size_of_children()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-23 18:43:40 +01:00
|
|
|
impl BlockLike for Unverified {
|
2016-09-27 16:50:24 +02:00
|
|
|
fn hash(&self) -> H256 {
|
|
|
|
self.header.hash()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parent_hash(&self) -> H256 {
|
|
|
|
self.header.parent_hash().clone()
|
|
|
|
}
|
2016-12-23 18:43:40 +01:00
|
|
|
|
|
|
|
fn difficulty(&self) -> U256 {
|
|
|
|
self.header.difficulty().clone()
|
|
|
|
}
|
2016-09-27 16:50:24 +02:00
|
|
|
}
|
|
|
|
|
2016-12-23 18:43:40 +01:00
|
|
|
impl BlockLike for PreverifiedBlock {
|
2016-09-27 16:50:24 +02:00
|
|
|
fn hash(&self) -> H256 {
|
|
|
|
self.header.hash()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parent_hash(&self) -> H256 {
|
|
|
|
self.header.parent_hash().clone()
|
|
|
|
}
|
2016-12-23 18:43:40 +01:00
|
|
|
|
|
|
|
fn difficulty(&self) -> U256 {
|
|
|
|
self.header.difficulty().clone()
|
|
|
|
}
|
2016-09-27 16:50:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Verification for headers.
|
|
|
|
pub mod headers {
|
2016-12-23 18:43:40 +01:00
|
|
|
use super::{Kind, BlockLike};
|
2016-09-27 16:50:24 +02:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
use engines::EthEngine;
|
2016-09-27 16:50:24 +02:00
|
|
|
use error::Error;
|
|
|
|
use header::Header;
|
|
|
|
use verification::verify_header_params;
|
|
|
|
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::{H256, U256};
|
2016-09-27 16:50:24 +02:00
|
|
|
|
2016-12-23 18:43:40 +01:00
|
|
|
impl BlockLike for Header {
|
2016-09-27 16:50:24 +02:00
|
|
|
fn hash(&self) -> H256 { self.hash() }
|
|
|
|
fn parent_hash(&self) -> H256 { self.parent_hash().clone() }
|
2016-12-23 18:43:40 +01:00
|
|
|
fn difficulty(&self) -> U256 { self.difficulty().clone() }
|
2016-09-27 16:50:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A mode for verifying headers.
|
|
|
|
pub struct Headers;
|
|
|
|
|
|
|
|
impl Kind for Headers {
|
|
|
|
type Input = Header;
|
|
|
|
type Unverified = Header;
|
|
|
|
type Verified = Header;
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
fn create(input: Self::Input, engine: &EthEngine) -> Result<Self::Unverified, Error> {
|
2017-02-22 18:24:22 +01:00
|
|
|
verify_header_params(&input, engine, true).map(|_| input)
|
2016-09-27 16:50:24 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
fn verify(unverified: Self::Unverified, engine: &EthEngine, check_seal: bool) -> Result<Self::Verified, Error> {
|
2016-10-24 15:09:13 +02:00
|
|
|
match check_seal {
|
2017-09-26 14:19:08 +02:00
|
|
|
true => engine.verify_block_unordered(&unverified,).map(|_| unverified),
|
2016-10-24 15:09:13 +02:00
|
|
|
false => Ok(unverified),
|
|
|
|
}
|
2016-09-27 16:50:24 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-02 18:45:36 +02:00
|
|
|
}
|