From af21038bb90f1268320e59e06d9a077a4c11569a Mon Sep 17 00:00:00 2001 From: arkpar Date: Sat, 9 Jan 2016 19:10:05 +0100 Subject: [PATCH 1/4] More block verifications --- src/block.rs | 2 +- src/engine.rs | 15 ++++++-- src/ethereum/ethash.rs | 87 ++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + src/verification.rs | 72 ++++++++++++++++++++++++++++------ 5 files changed, 161 insertions(+), 16 deletions(-) diff --git a/src/block.rs b/src/block.rs index 3c42e4e27..f98934988 100644 --- a/src/block.rs +++ b/src/block.rs @@ -162,4 +162,4 @@ fn open_block() { let mut db = OverlayDB::new_temp(); engine.spec().ensure_db_good(&mut db); let b = OpenBlock::new(engine.deref(), db, &genesis_header, vec![genesis_header.hash()]); -} \ No newline at end of file +} diff --git a/src/engine.rs b/src/engine.rs index 7fee085e9..7d86aa0a1 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1,6 +1,7 @@ use common::*; use block::Block; use spec::Spec; +use verification::VerificationError; /// A consensus mechanism for the chain. Generally either proof-of-work or proof-of-stake-based. /// Provides hooks into each of the major parts of block import. @@ -25,7 +26,7 @@ pub trait Engine { fn evm_schedule(&self, env_info: &EnvInfo) -> EvmSchedule; /// Some intrinsic operation parameters; by default they take their value from the `spec()`'s `engine_params`. - fn maximum_extra_data_size(&self, _env_info: &EnvInfo) -> usize { decode(&self.spec().engine_params.get("maximumExtraDataSize").unwrap()) } + fn maximum_extra_data_size(&self) -> usize { decode(&self.spec().engine_params.get("maximumExtraDataSize").unwrap()) } fn account_start_nonce(&self) -> U256 { decode(&self.spec().engine_params.get("accountStartNonce").unwrap()) } /// Block transformation functions, before and after the transactions. @@ -36,12 +37,12 @@ pub trait Engine { /// `parent` (the parent header) and `block` (the header's full block) may be provided for additional /// checks. Returns either a null `Ok` or a general error detailing the problem with import. // TODO: consider including State in the params. - fn verify_block(&self, _header: &Header, _parent: Option<&Header>, _block: Option<&[u8]>) -> Result<(), EthcoreError> { Ok(()) } + fn verify_block(&self, _mode: VerificationMode, _header: &Header, _parent: Option<&Header>, _block: Option<&[u8]>) -> Result<(), VerificationError> { Ok(()) } /// Additional verification for transactions in blocks. // TODO: Add flags for which bits of the transaction to check. // TODO: consider including State in the params. - fn verify_transaction(&self, _t: &Transaction, _header: &Header) -> Result<(), EthcoreError> { Ok(()) } + fn verify_transaction(&self, _t: &Transaction, _header: &Header) -> Result<(), VerificationError> { Ok(()) } /// Don't forget to call Super::populateFromParent when subclassing & overriding. // TODO: consider including State in the params. @@ -55,3 +56,11 @@ pub trait Engine { // TODO: sealing stuff - though might want to leave this for later. } + +#[derive(Debug, PartialEq, Eq)] +pub enum VerificationMode { + /// Do a quick and basic verification if possible. + Quick, + /// Do a full verification. + Full +} diff --git a/src/ethereum/ethash.rs b/src/ethereum/ethash.rs index 433a03681..8e26e8f86 100644 --- a/src/ethereum/ethash.rs +++ b/src/ethereum/ethash.rs @@ -2,6 +2,7 @@ use common::*; use block::*; use spec::*; use engine::*; +use verification::*; /// Engine using Ethash proof-of-work consensus algorithm, suitable for Ethereum /// mainnet chains in the Olympic, Frontier and Homestead eras. @@ -25,6 +26,91 @@ impl Engine for Ethash { let a = block.header().author.clone(); block.state_mut().add_balance(&a, &decode(&self.spec().engine_params.get("block_reward").unwrap())); } + + fn verify_block(&self, mode: VerificationMode, header: &Header, parent: Option<&Header>, block: Option<&[u8]>) -> Result<(), VerificationError> { + if mode == VerificationMode::Quick { + let min_difficulty = decode(self.spec().engine_params.get("minimumDifficulty").unwrap()); + if header.difficulty < min_difficulty { + return Err(VerificationError::block( + BlockVerificationError::InvalidDifficulty { required: min_difficulty, got: header.difficulty }, + block.map(|b| b.to_vec()))); + } + let min_gas_limit = decode(self.spec().engine_params.get("minGasLimit").unwrap()); + if header.gas_limit < min_gas_limit { + return Err(VerificationError::block( + BlockVerificationError::InvalidGasLimit { min: min_gas_limit, max: From::from(0), got: header.gas_limit }, + block.map(|b| b.to_vec()))); + } + let len: U256 = From::from(header.extra_data.len()); + let maximum_extra_data_size: U256 = From::from(self.maximum_extra_data_size()); + if header.number != From::from(0) && len > maximum_extra_data_size { + return Err(VerificationError::block( + BlockVerificationError::ExtraDataTooBig { required: maximum_extra_data_size, got: len }, + block.map(|b| b.to_vec()))); + } + match parent { + Some(p) => { + // Check difficulty is correct given the two timestamps. + let expected_difficulty = self.calculate_difficuty(header, p); + if header.difficulty != expected_difficulty { + return Err(VerificationError::block( + BlockVerificationError::InvalidDifficulty { required: expected_difficulty, got: header.difficulty }, + block.map(|b| b.to_vec()))); + } + let gas_limit_divisor = decode(self.spec().engine_params.get("gasLimitBoundDivisor").unwrap()); + let min_gas = p.gas_limit - p.gas_limit / gas_limit_divisor; + let max_gas = p.gas_limit + p.gas_limit / gas_limit_divisor; + if header.gas_limit <= min_gas || header.gas_limit >= max_gas { + return Err(VerificationError::block( + BlockVerificationError::InvalidGasLimit { min: min_gas_limit, max: max_gas, got: header.gas_limit }, + block.map(|b| b.to_vec()))); + } + }, + None => () + } + // TODO: Verify seal + } + Ok(()) + } + + fn verify_transaction(&self, _t: &Transaction, _header: &Header) -> Result<(), VerificationError> { Ok(()) } +} + +impl Ethash { + fn calculate_difficuty(&self, header: &Header, parent: &Header) -> U256 { + const EXP_DIFF_PERIOD: u64 = 100000; + if header.number == From::from(0) { + panic!("Can't calculate genesis block difficulty"); + } + + let min_difficulty = decode(self.spec().engine_params.get("minimumDifficulty").unwrap()); + let difficulty_bound_divisor = decode(self.spec().engine_params.get("difficultyBoundDivisor").unwrap()); + let duration_limit = decode(self.spec().engine_params.get("durationLimit").unwrap()); + let frontier_limit = decode(self.spec().engine_params.get("frontierCompatibilityModeLimit").unwrap()); + let mut target = if header.number < frontier_limit { + if header.timestamp >= parent.timestamp + duration_limit { + parent.difficulty - (parent.difficulty / difficulty_bound_divisor) + } + else { + parent.difficulty + (parent.difficulty / difficulty_bound_divisor) + } + } + else { + let diff_inc = (header.timestamp - parent.timestamp) / From::from(10); + if diff_inc <= From::from(1) { + parent.difficulty + parent.difficulty / From::from(2048) * (U256::from(1) - diff_inc) + } + else { + parent.difficulty - parent.difficulty / From::from(2048) * max(diff_inc - From::from(1), From::from(99)) + } + }; + target = max(min_difficulty, target); + let period = ((parent.number + From::from(1)).as_u64() / EXP_DIFF_PERIOD) as usize; + if period > 1 { + target = max(min_difficulty, target + (U256::from(1) << (period - 2))); + } + target + } } // TODO: test for on_close_block. @@ -44,3 +130,4 @@ fn playpen() { let b = OpenBlock::new(engine.deref(), db, &genesis_header, vec![genesis_header.hash()]); // let c = b.close(); } + diff --git a/src/lib.rs b/src/lib.rs index 2154a7c14..b1c6b0a31 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -102,4 +102,5 @@ pub mod extras; pub mod client; pub mod sync; pub mod block; +pub mod verification; pub mod ethereum; diff --git a/src/verification.rs b/src/verification.rs index 95951da53..f4aa4dcb6 100644 --- a/src/verification.rs +++ b/src/verification.rs @@ -1,10 +1,35 @@ -use util::uint::*; -use util::hash::*; -use util::rlp::*; -use util::sha3::Hashable; -use util::triehash::ordered_trie_root; +use util::*; use header::Header; use client::BlockNumber; +use engine::{Engine, VerificationMode}; +use views::BlockView; + +#[derive(Debug)] +pub struct VerificationError { + pub block: Option, + pub error: VerificationErrorOption, +} + +impl VerificationError { + pub fn block(error: BlockVerificationError, block: Option) -> VerificationError { + VerificationError { + block: block, + error: VerificationErrorOption::Block(error), + } + } + pub fn transaction(error: TransactionVerificationError, block: Option) -> VerificationError { + VerificationError { + block: block, + error: VerificationErrorOption::Transaction(error), + } + } +} + +#[derive(Debug)] +pub enum VerificationErrorOption { + Transaction(TransactionVerificationError), + Block(BlockVerificationError), +} #[derive(Debug)] pub enum TransactionVerificationError { @@ -18,7 +43,6 @@ pub enum TransactionVerificationError { used: U256, limit: U256 }, - ExtraDataTooBig, InvalidSignature, InvalidTransactionFormat, } @@ -30,8 +54,12 @@ pub enum BlockVerificationError { limit: U256, }, InvalidBlockFormat, + ExtraDataTooBig { + required: U256, + got: U256, + }, InvalidUnclesHash { - expected: H256, + required: H256, got: H256, }, TooManyUncles, @@ -42,11 +70,18 @@ pub enum BlockVerificationError { InvalidStateRoot, InvalidGasUsed, InvalidTransactionsRoot { - expected: H256, + required: H256, got: H256, }, - InvalidDifficulty, - InvalidGasLimit, + InvalidDifficulty { + required: U256, + got: U256, + }, + InvalidGasLimit { + min: U256, + max: U256, + got: U256, + }, InvalidReceiptsStateRoot, InvalidTimestamp, InvalidLogBloom, @@ -93,17 +128,30 @@ pub fn verify_block_integrity(block: &[u8], transactions_root: &H256, uncles_has let expected_root = ordered_trie_root(tx.iter().map(|r| r.as_raw().to_vec()).collect()); //TODO: get rid of vectors here if &expected_root != transactions_root { return Err(BlockVerificationError::InvalidTransactionsRoot { - expected: expected_root.clone(), + required: expected_root.clone(), got: transactions_root.clone(), }); } let expected_uncles = block.at(2).as_raw().sha3(); if &expected_uncles != uncles_hash { return Err(BlockVerificationError::InvalidUnclesHash { - expected: expected_uncles.clone(), + required: expected_uncles.clone(), got: uncles_hash.clone(), }); } Ok(()) } +pub fn verify_block_basic(bytes: &[u8], parent: &Header, engine: &mut Engine) -> Result<(), BlockVerificationError> { + let block = BlockView::new(bytes); + let header = block.header(); + try!(verify_header(&header)); + try!(verify_parent(&header, parent)); + try!(verify_block_integrity(bytes, &header.transactions_root, &header.uncles_hash)); + + Ok(()) +} + +pub fn verify_block_unordered(block: &[u8]) -> Result<(), BlockVerificationError> { + Ok(()) +} From e6572e34faf380b1af85f76301aa4868693a2624 Mon Sep 17 00:00:00 2001 From: arkpar Date: Sun, 10 Jan 2016 21:30:22 +0100 Subject: [PATCH 2/4] Documentation --- src/block.rs | 2 +- src/verification.rs | 94 ++++++++++++++++++++++++++------------------- 2 files changed, 56 insertions(+), 40 deletions(-) diff --git a/src/block.rs b/src/block.rs index 7b5262168..dce3803f5 100644 --- a/src/block.rs +++ b/src/block.rs @@ -119,7 +119,7 @@ impl<'engine> OpenBlock<'engine> { /// that the header itself is actually valid. pub fn push_uncle(&mut self, valid_uncle_header: Header) -> Result<(), BlockError> { if self.block.uncles.len() >= self.engine.maximum_uncle_count() { - return Err(BlockError::TooManyUncles); + return Err(BlockError::TooManyUncles(OutOfBounds{min: 0, max: self.engine.maximum_uncle_count(), found: self.block.uncles.len()})); } // TODO: check number // TODO: check not a direct ancestor (use last_hashes for that) diff --git a/src/verification.rs b/src/verification.rs index ae156cbfa..2b843134b 100644 --- a/src/verification.rs +++ b/src/verification.rs @@ -1,45 +1,16 @@ +/// Block and transaction verification functions +/// +/// Block verification is done in 3 steps +/// 1. Quick verification upon adding to the block queue +/// 2. Signatures verification done in the queue. +/// 3. Final verification against the blockchain done before enactment. + use common::*; use client::BlockNumber; use engine::Engine; use blockchain::BlockChain; -fn verify_header(header: &Header) -> Result<(), Error> { - if header.number > From::from(BlockNumber::max_value()) { - return Err(From::from(BlockError::InvalidNumber(OutOfBounds { max: From::from(BlockNumber::max_value()), min: From::from(0), found: header.number }))) - } - if header.gas_used > header.gas_limit { - return Err(From::from(BlockError::TooMuchGasUsed(OutOfBounds { max: header.gas_limit, min: From::from(0), found: header.gas_used }))); - } - Ok(()) -} - -fn verify_parent(header: &Header, parent: &Header) -> Result<(), Error> { - if !header.parent_hash.is_zero() && parent.hash() != header.parent_hash { - return Err(From::from(BlockError::InvalidParentHash(Mismatch { expected: parent.hash(), found: header.parent_hash.clone() }))) - } - if header.timestamp <= parent.timestamp { - return Err(From::from(BlockError::InvalidTimestamp(OutOfBounds { max: BAD_U256, min: parent.timestamp + From::from(1), found: header.timestamp }))) - } - if header.number <= parent.number { - return Err(From::from(BlockError::InvalidNumber(OutOfBounds { max: From::from(BlockNumber::max_value()), min: parent.number + From::from(1), found: header.number }))); - } - Ok(()) -} - -fn verify_block_integrity(block: &[u8], transactions_root: &H256, uncles_hash: &H256) -> Result<(), Error> { - let block = Rlp::new(block); - let tx = block.at(1); - let expected_root = &ordered_trie_root(tx.iter().map(|r| r.as_raw().to_vec()).collect()); //TODO: get rid of vectors here - if expected_root != transactions_root { - return Err(From::from(BlockError::InvalidTransactionsRoot(Mismatch { expected: expected_root.clone(), found: transactions_root.clone() }))) - } - let expected_uncles = &block.at(2).as_raw().sha3(); - if expected_uncles != uncles_hash { - return Err(From::from(BlockError::InvalidUnclesHash(Mismatch { expected: expected_uncles.clone(), found: uncles_hash.clone() }))) - } - Ok(()) -} - +/// Phase 1 quick block verification. Only does checks that are cheap. Operates on a single block pub fn verify_block_basic(bytes: &[u8], engine: &mut Engine) -> Result<(), Error> { let block = BlockView::new(bytes); let header = block.header(); @@ -53,6 +24,9 @@ pub fn verify_block_basic(bytes: &[u8], engine: &mut Engine) -> Result<(), Error Ok(()) } +/// Phase 2 verification. Perform costly checks such as transaction signatures and block nonce for ethash. +/// Still operates on a individual block +/// TODO: return cached transactions, header hash. pub fn verify_block_unordered(bytes: &[u8], engine: &mut Engine) -> Result<(), Error> { let block = BlockView::new(bytes); let header = block.header(); @@ -63,6 +37,7 @@ pub fn verify_block_unordered(bytes: &[u8], engine: &mut Engine) -> Result<(), E Ok(()) } +/// Phase 3 verification. Check block information against parents and uncles. pub fn verify_block_final(bytes: &[u8], engine: &mut Engine, bc: &BlockChain) -> Result<(), Error> { let block = BlockView::new(bytes); let header = block.header(); @@ -72,8 +47,8 @@ pub fn verify_block_final(bytes: &[u8], engine: &mut Engine, bc: &BlockChain) -> let num_uncles = Rlp::new(bytes).at(2).item_count(); if num_uncles != 0 { - if num_uncles > 2 { - return Err(From::from(BlockError::TooManyUncles(OutOfBounds { min: 0, max: 2, found: num_uncles }))); + if num_uncles > engine.maximum_uncle_count() { + return Err(From::from(BlockError::TooManyUncles(OutOfBounds { min: 0, max: engine.maximum_uncle_count(), found: num_uncles }))); } let mut excluded = HashSet::new(); @@ -137,3 +112,44 @@ pub fn verify_block_final(bytes: &[u8], engine: &mut Engine, bc: &BlockChain) -> } Ok(()) } + +/// Check basic header parameters. +fn verify_header(header: &Header) -> Result<(), Error> { + if header.number > From::from(BlockNumber::max_value()) { + return Err(From::from(BlockError::InvalidNumber(OutOfBounds { max: From::from(BlockNumber::max_value()), min: From::from(0), found: header.number }))) + } + if header.gas_used > header.gas_limit { + return Err(From::from(BlockError::TooMuchGasUsed(OutOfBounds { max: header.gas_limit, min: From::from(0), found: header.gas_used }))); + } + Ok(()) +} + +/// Check header parameters agains parent header. +fn verify_parent(header: &Header, parent: &Header) -> Result<(), Error> { + if !header.parent_hash.is_zero() && parent.hash() != header.parent_hash { + return Err(From::from(BlockError::InvalidParentHash(Mismatch { expected: parent.hash(), found: header.parent_hash.clone() }))) + } + if header.timestamp <= parent.timestamp { + return Err(From::from(BlockError::InvalidTimestamp(OutOfBounds { max: BAD_U256, min: parent.timestamp + From::from(1), found: header.timestamp }))) + } + if header.number <= parent.number { + return Err(From::from(BlockError::InvalidNumber(OutOfBounds { max: From::from(BlockNumber::max_value()), min: parent.number + From::from(1), found: header.number }))); + } + Ok(()) +} + +/// Verify block data against header: transactions root and uncles hash. +fn verify_block_integrity(block: &[u8], transactions_root: &H256, uncles_hash: &H256) -> Result<(), Error> { + let block = Rlp::new(block); + let tx = block.at(1); + let expected_root = &ordered_trie_root(tx.iter().map(|r| r.as_raw().to_vec()).collect()); //TODO: get rid of vectors here + if expected_root != transactions_root { + return Err(From::from(BlockError::InvalidTransactionsRoot(Mismatch { expected: expected_root.clone(), found: transactions_root.clone() }))) + } + let expected_uncles = &block.at(2).as_raw().sha3(); + if expected_uncles != uncles_hash { + return Err(From::from(BlockError::InvalidUnclesHash(Mismatch { expected: expected_uncles.clone(), found: uncles_hash.clone() }))) + } + Ok(()) +} + From 0221d47544960348d5a2cfc67d45e40da339171e Mon Sep 17 00:00:00 2001 From: arkpar Date: Mon, 11 Jan 2016 13:42:32 +0100 Subject: [PATCH 3/4] Verification integrated into client/queue --- src/client.rs | 53 +++++++++++++++++++-------------------------- src/error.rs | 11 +++++++++- src/lib.rs | 1 + src/queue.rs | 36 +++++++++++++++++------------- src/sync/tests.rs | 12 +++------- src/verification.rs | 6 ++--- 6 files changed, 60 insertions(+), 59 deletions(-) diff --git a/src/client.rs b/src/client.rs index b914dba19..018f99b6f 100644 --- a/src/client.rs +++ b/src/client.rs @@ -5,6 +5,7 @@ use error::*; use header::BlockNumber; use spec::Spec; use engine::Engine; +use queue::BlockQueue; /// General block status pub enum BlockStatus { @@ -18,9 +19,6 @@ pub enum BlockStatus { Unknown, } -/// Result of import block operation. -pub type ImportResult = Result<(), ImportError>; - /// Information about the blockchain gthered together. pub struct BlockChainInfo { /// Blockchain difficulty. @@ -95,28 +93,30 @@ pub trait BlockChainClient : Sync { /// Blockchain database client backed by a persistent database. Owns and manages a blockchain and a block queue. pub struct Client { - chain: Arc, + chain: Arc>, _engine: Arc>, + queue: BlockQueue, } impl Client { pub fn new(spec: Spec, path: &Path) -> Result { - let chain = Arc::new(BlockChain::new(&spec.genesis_block(), path)); + let chain = Arc::new(RwLock::new(BlockChain::new(&spec.genesis_block(), path))); let engine = Arc::new(try!(spec.to_engine())); Ok(Client { chain: chain.clone(), - _engine: engine, + _engine: engine.clone(), + queue: BlockQueue::new(chain.clone(), engine.clone()), }) } } impl BlockChainClient for Client { fn block_header(&self, hash: &H256) -> Option { - self.chain.block(hash).map(|bytes| BlockView::new(&bytes).rlp().at(0).as_raw().to_vec()) + self.chain.read().unwrap().block(hash).map(|bytes| BlockView::new(&bytes).rlp().at(0).as_raw().to_vec()) } fn block_body(&self, hash: &H256) -> Option { - self.chain.block(hash).map(|bytes| { + self.chain.read().unwrap().block(hash).map(|bytes| { let rlp = Rlp::new(&bytes); let mut body = RlpStream::new(); body.append_raw(rlp.at(1).as_raw(), 1); @@ -126,34 +126,34 @@ impl BlockChainClient for Client { } fn block(&self, hash: &H256) -> Option { - self.chain.block(hash) + self.chain.read().unwrap().block(hash) } fn block_status(&self, hash: &H256) -> BlockStatus { - if self.chain.is_known(&hash) { BlockStatus::InChain } else { BlockStatus::Unknown } + if self.chain.read().unwrap().is_known(&hash) { BlockStatus::InChain } else { BlockStatus::Unknown } } fn block_header_at(&self, n: BlockNumber) -> Option { - self.chain.block_hash(n).and_then(|h| self.block_header(&h)) + self.chain.read().unwrap().block_hash(n).and_then(|h| self.block_header(&h)) } fn block_body_at(&self, n: BlockNumber) -> Option { - self.chain.block_hash(n).and_then(|h| self.block_body(&h)) + self.chain.read().unwrap().block_hash(n).and_then(|h| self.block_body(&h)) } fn block_at(&self, n: BlockNumber) -> Option { - self.chain.block_hash(n).and_then(|h| self.block(&h)) + self.chain.read().unwrap().block_hash(n).and_then(|h| self.block(&h)) } fn block_status_at(&self, n: BlockNumber) -> BlockStatus { - match self.chain.block_hash(n) { + match self.chain.read().unwrap().block_hash(n) { Some(h) => self.block_status(&h), None => BlockStatus::Unknown } } fn tree_route(&self, from: &H256, to: &H256) -> Option { - self.chain.tree_route(from.clone(), to.clone()) + self.chain.read().unwrap().tree_route(from.clone(), to.clone()) } fn state_data(&self, _hash: &H256) -> Option { @@ -165,17 +165,7 @@ impl BlockChainClient for Client { } fn import_block(&mut self, bytes: &[u8]) -> ImportResult { - //TODO: verify block - { - let block = BlockView::new(bytes); - let header = block.header_view(); - let hash = header.sha3(); - if self.chain.is_known(&hash) { - return Err(ImportError::AlreadyInChain); - } - } - self.chain.insert_block(bytes); - Ok(()) + self.queue.import_block(bytes) } fn queue_status(&self) -> BlockQueueStatus { @@ -188,12 +178,13 @@ impl BlockChainClient for Client { } fn chain_info(&self) -> BlockChainInfo { + let chain = self.chain.read().unwrap(); BlockChainInfo { - total_difficulty: self.chain.best_block_total_difficulty(), - pending_total_difficulty: self.chain.best_block_total_difficulty(), - genesis_hash: self.chain.genesis_hash(), - best_block_hash: self.chain.best_block_hash(), - best_block_number: From::from(self.chain.best_block_number()) + total_difficulty: chain.best_block_total_difficulty(), + pending_total_difficulty: chain.best_block_total_difficulty(), + genesis_hash: chain.genesis_hash(), + best_block_hash: chain.best_block_hash(), + best_block_number: From::from(chain.best_block_number()) } } } diff --git a/src/error.rs b/src/error.rs index 99a64ce7a..c18782502 100644 --- a/src/error.rs +++ b/src/error.rs @@ -45,11 +45,20 @@ pub enum BlockError { #[derive(Debug)] pub enum ImportError { - Bad(BlockError), + Bad(Error), AlreadyInChain, AlreadyQueued, } +impl From for ImportError { + fn from(err: Error) -> ImportError { + ImportError::Bad(err) + } +} + +/// Result of import block operation. +pub type ImportResult = Result<(), ImportError>; + #[derive(Debug)] /// General error type which should be capable of representing all errors in ethcore. pub enum Error { diff --git a/src/lib.rs b/src/lib.rs index f090ba83a..6aab2587a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -106,4 +106,5 @@ pub mod client; pub mod sync; pub mod block; pub mod verification; +pub mod queue; pub mod ethereum; diff --git a/src/queue.rs b/src/queue.rs index ea212aaf1..721960259 100644 --- a/src/queue.rs +++ b/src/queue.rs @@ -1,16 +1,24 @@ -use std::sync::Arc; use util::*; use blockchain::BlockChain; -use client::{QueueStatus, ImportResult}; use views::{BlockView}; +use verification::*; +use error::*; +use engine::Engine; /// A queue of blocks. Sits between network or other I/O and the BlockChain. /// Sorts them ready for blockchain insertion. -pub struct BlockQueue; +pub struct BlockQueue { + bc: Arc>, + engine: Arc>, +} impl BlockQueue { /// Creates a new queue instance. - pub fn new() -> BlockQueue { + pub fn new(bc: Arc>, engine: Arc>) -> BlockQueue { + BlockQueue { + bc: bc, + engine: engine, + } } /// Clear the queue and stop verification activity. @@ -18,18 +26,16 @@ impl BlockQueue { } /// Add a block to the queue. - pub fn import_block(&mut self, bytes: &[u8], bc: &mut BlockChain) -> ImportResult { - //TODO: verify block - { - let block = BlockView::new(bytes); - let header = block.header_view(); - let hash = header.sha3(); - if self.chain.is_known(&hash) { - return ImportResult::Bad; - } + pub fn import_block(&mut self, bytes: &[u8]) -> ImportResult { + let header = BlockView::new(bytes).header(); + if self.bc.read().unwrap().is_known(&header.hash()) { + return Err(ImportError::AlreadyInChain); } - bc.insert_block(bytes); - ImportResult::Queued(QueueStatus::Known) + try!(verify_block_basic(bytes, self.engine.deref().deref())); + try!(verify_block_unordered(bytes, self.engine.deref().deref())); + try!(verify_block_final(bytes, self.engine.deref().deref(), self.bc.read().unwrap().deref())); + self.bc.write().unwrap().insert_block(bytes); + Ok(()) } } diff --git a/src/sync/tests.rs b/src/sync/tests.rs index 6a045fa1e..bc0e171d2 100644 --- a/src/sync/tests.rs +++ b/src/sync/tests.rs @@ -1,13 +1,7 @@ -use std::collections::{HashMap, VecDeque}; -use util::bytes::Bytes; -use util::hash::{H256, FixedHash}; -use util::uint::{U256}; -use util::sha3::Hashable; -use util::rlp::{self, Rlp, RlpStream, View, Stream}; -use util::network::{PeerId, PacketId}; -use util::error::UtilError; -use client::{BlockChainClient, BlockStatus, TreeRoute, BlockQueueStatus, BlockChainInfo, ImportResult}; +use util::*; +use client::{BlockChainClient, BlockStatus, TreeRoute, BlockQueueStatus, BlockChainInfo}; use header::{Header as BlockHeader, BlockNumber}; +use error::*; use sync::io::SyncIo; use sync::chain::ChainSync; diff --git a/src/verification.rs b/src/verification.rs index e27536187..1d34ddff4 100644 --- a/src/verification.rs +++ b/src/verification.rs @@ -10,7 +10,7 @@ use engine::Engine; use blockchain::BlockChain; /// Phase 1 quick block verification. Only does checks that are cheap. Operates on a single block -pub fn verify_block_basic(bytes: &[u8], engine: &mut Engine) -> Result<(), Error> { +pub fn verify_block_basic(bytes: &[u8], engine: &Engine) -> Result<(), Error> { let block = BlockView::new(bytes); let header = block.header(); try!(verify_header(&header)); @@ -26,7 +26,7 @@ pub fn verify_block_basic(bytes: &[u8], engine: &mut Engine) -> Result<(), Error /// Phase 2 verification. Perform costly checks such as transaction signatures and block nonce for ethash. /// Still operates on a individual block /// TODO: return cached transactions, header hash. -pub fn verify_block_unordered(bytes: &[u8], engine: &mut Engine) -> Result<(), Error> { +pub fn verify_block_unordered(bytes: &[u8], engine: &Engine) -> Result<(), Error> { let block = BlockView::new(bytes); let header = block.header(); try!(engine.verify_block_unordered(&header, Some(bytes))); @@ -37,7 +37,7 @@ pub fn verify_block_unordered(bytes: &[u8], engine: &mut Engine) -> Result<(), E } /// Phase 3 verification. Check block information against parents and uncles. -pub fn verify_block_final(bytes: &[u8], engine: &mut Engine, bc: &BlockChain) -> Result<(), Error> { +pub fn verify_block_final(bytes: &[u8], engine: &Engine, bc: &BlockChain) -> Result<(), Error> { let block = BlockView::new(bytes); let header = block.header(); let parent = try!(bc.block_header(&header.parent_hash).ok_or::(From::from(BlockError::UnknownParent(header.parent_hash.clone())))); From 3185301c97893537fb8acc9099933117f2b91501 Mon Sep 17 00:00:00 2001 From: arkpar Date: Mon, 11 Jan 2016 13:51:58 +0100 Subject: [PATCH 4/4] Updated docs --- src/engine.rs | 13 +++++++++---- src/verification.rs | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 65f3a4b9f..aed9fe2f3 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -33,12 +33,17 @@ pub trait Engine : Sync + Send { fn on_new_block(&self, _block: &mut Block) {} fn on_close_block(&self, _block: &mut Block) {} - /// Verify that `header` is valid. - /// `parent` (the parent header) and `block` (the header's full block) may be provided for additional - /// checks. Returns either a null `Ok` or a general error detailing the problem with import. - // TODO: consider including State in the params. + // TODO: consider including State in the params for verification functions. + /// Phase 1 quick block verification. Only does checks that are cheap. `block` (the header's full block) + /// may be provided for additional checks. Returns either a null `Ok` or a general error detailing the problem with import. fn verify_block_basic(&self, _header: &Header, _block: Option<&[u8]>) -> Result<(), Error> { Ok(()) } + + /// Phase 2 verification. Perform costly checks such as transaction signatures. `block` (the header's full block) + /// may be provided for additional checks. Returns either a null `Ok` or a general error detailing the problem with import. fn verify_block_unordered(&self, _header: &Header, _block: Option<&[u8]>) -> Result<(), Error> { Ok(()) } + + /// Phase 3 verification. Check block information against parent and uncles. `block` (the header's full block) + /// may be provided for additional checks. Returns either a null `Ok` or a general error detailing the problem with import. fn verify_block_final(&self, _header: &Header, _parent: &Header, _block: Option<&[u8]>) -> Result<(), Error> { Ok(()) } /// Additional verification for transactions in blocks. diff --git a/src/verification.rs b/src/verification.rs index 1d34ddff4..be885162a 100644 --- a/src/verification.rs +++ b/src/verification.rs @@ -36,7 +36,7 @@ pub fn verify_block_unordered(bytes: &[u8], engine: &Engine) -> Result<(), Error Ok(()) } -/// Phase 3 verification. Check block information against parents and uncles. +/// Phase 3 verification. Check block information against parent and uncles. pub fn verify_block_final(bytes: &[u8], engine: &Engine, bc: &BlockChain) -> Result<(), Error> { let block = BlockView::new(bytes); let header = block.header();