Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
implement header queue
Browse files Browse the repository at this point in the history
  • Loading branch information
rphmeier committed Sep 16, 2016
1 parent 30952a5 commit 417f371
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
35 changes: 35 additions & 0 deletions ethcore/src/verification/queue/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use error::Error;
use util::{HeapSizeOf, H256};

pub use self::blocks::Blocks;
pub use self::headers::Headers;

/// Something which can produce a hash and a parent hash.
pub trait HasHash {
Expand Down Expand Up @@ -144,4 +145,38 @@ pub mod blocks {
self.header.parent_hash().clone()
}
}
}

/// Verification for headers.
pub mod headers {
use super::{Kind, HasHash};

use engines::Engine;
use error::Error;
use header::Header;
use verification::verify_header_params;

use util::hash::H256;

impl HasHash for Header {
fn hash(&self) -> H256 { self.hash() }
fn parent_hash(&self) -> H256 { self.parent_hash().clone() }
}

/// A mode for verifying headers.
pub struct Headers;

impl Kind for Headers {
type Input = Header;
type Unverified = Header;
type Verified = Header;

fn create(input: Self::Input, engine: &Engine) -> Result<Self::Unverified, Error> {
verify_header_params(&input, engine).map(|_| input)
}

fn verify(unverified: Self::Unverified, engine: &Engine) -> Result<Self::Verified, Error> {
engine.verify_block_unordered(&unverified, None).map(|_| unverified)
}
}
}
3 changes: 3 additions & 0 deletions ethcore/src/verification/queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const MIN_QUEUE_LIMIT: usize = 512;
/// Type alias for block queue convenience.
pub type BlockQueue = VerificationQueue<self::kind::Blocks>;

/// Type alias for header queue convenience.
pub type HeaderQueue = VerificationQueue<self::kind::Headers>;

/// Verification queue configuration
#[derive(Debug, PartialEq, Clone)]
pub struct Config {
Expand Down
6 changes: 3 additions & 3 deletions ethcore/src/verification/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ impl HeapSizeOf for PreverifiedBlock {

/// Phase 1 quick block verification. Only does checks that are cheap. Operates on a single block
pub fn verify_block_basic(header: &Header, bytes: &[u8], engine: &Engine) -> Result<(), Error> {
try!(verify_header(&header, engine));
try!(verify_header_params(&header, engine));
try!(verify_block_integrity(bytes, &header.transactions_root(), &header.uncles_hash()));
try!(engine.verify_block_basic(&header, Some(bytes)));
for u in try!(UntrustedRlp::new(bytes).at(2)).iter().map(|rlp| rlp.as_val::<Header>()) {
let u = try!(u);
try!(verify_header(&u, engine));
try!(verify_header_params(&u, engine));
try!(engine.verify_block_basic(&u, None));
}
// Verify transactions.
Expand Down Expand Up @@ -187,7 +187,7 @@ pub fn verify_block_final(expected: &Header, got: &Header) -> Result<(), Error>
}

/// Check basic header parameters.
fn verify_header(header: &Header, engine: &Engine) -> Result<(), Error> {
pub fn verify_header_params(header: &Header, engine: &Engine) -> Result<(), Error> {
if header.number() >= From::from(BlockNumber::max_value()) {
return Err(From::from(BlockError::RidiculousNumber(OutOfBounds { max: Some(From::from(BlockNumber::max_value())), min: None, found: header.number() })))
}
Expand Down

0 comments on commit 417f371

Please sign in to comment.