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

Commit

Permalink
Merge pull request #72 from gavofyork/verification
Browse files Browse the repository at this point in the history
Block Verification (no tests yet)
  • Loading branch information
arkpar committed Jan 11, 2016
2 parents 79e122c + 3185301 commit 4353518
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl<'x, 'y> OpenBlock<'x, 'y> {
/// 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)
Expand Down
53 changes: 22 additions & 31 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use error::*;
use header::BlockNumber;
use spec::Spec;
use engine::Engine;
use queue::BlockQueue;

/// General block status
pub enum BlockStatus {
Expand All @@ -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.
Expand Down Expand Up @@ -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<BlockChain>,
chain: Arc<RwLock<BlockChain>>,
_engine: Arc<Box<Engine>>,
queue: BlockQueue,
}

impl Client {
pub fn new(spec: Spec, path: &Path) -> Result<Client, Error> {
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<Bytes> {
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<Bytes> {
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);
Expand All @@ -126,34 +126,34 @@ impl BlockChainClient for Client {
}

fn block(&self, hash: &H256) -> Option<Bytes> {
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<Bytes> {
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<Bytes> {
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<Bytes> {
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<TreeRoute> {
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<Bytes> {
Expand All @@ -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 {
Expand All @@ -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())
}
}
}
17 changes: 12 additions & 5 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,18 @@ 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.
fn verify_block(&self, _header: &Header, _parent: Option<&Header>, _block: Option<&[u8]>) -> Result<(), Error> { Ok(()) }
// 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.
// TODO: Add flags for which bits of the transaction to check.
Expand Down
33 changes: 31 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! General error types for use in ethcore.

use util::*;
use header::BlockNumber;

#[derive(Debug)]
pub struct Mismatch<T: fmt::Debug> {
Expand All @@ -17,19 +18,47 @@ pub struct OutOfBounds<T: fmt::Debug> {

#[derive(Debug)]
pub enum BlockError {
TooManyUncles,
TooManyUncles(OutOfBounds<usize>),
UncleWrongGeneration,
ExtraDataOutOfBounds(OutOfBounds<usize>),
InvalidSealArity(Mismatch<usize>),
TooMuchGasUsed(OutOfBounds<U256>),
InvalidUnclesHash(Mismatch<H256>),
UncleTooOld(OutOfBounds<BlockNumber>),
UncleIsBrother(OutOfBounds<BlockNumber>),
UncleInChain(H256),
UncleParentNotInChain(H256),
InvalidStateRoot,
InvalidGasUsed,
InvalidTransactionsRoot(Mismatch<H256>),
InvalidDifficulty(Mismatch<U256>),
InvalidGasLimit(OutOfBounds<U256>),
InvalidReceiptsStateRoot,
InvalidTimestamp(OutOfBounds<u64>),
InvalidLogBloom,
InvalidBlockNonce,
InvalidParentHash(Mismatch<H256>),
InvalidNumber(OutOfBounds<BlockNumber>),
UnknownParent(H256),
UnknownUncleParent(H256),
}

#[derive(Debug)]
pub enum ImportError {
Bad(BlockError),
Bad(Error),
AlreadyInChain,
AlreadyQueued,
}

impl From<Error> 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 {
Expand Down
79 changes: 79 additions & 0 deletions src/ethereum/ethash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,83 @@ impl Engine for Ethash {
fields.state.add_balance(u.author(), &(reward * U256::from((8 + u.number() - current_number) / 8)));
}
}


fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> Result<(), Error> {
let min_difficulty = decode(self.spec().engine_params.get("minimumDifficulty").unwrap());
if header.difficulty < min_difficulty {
return Err(From::from(BlockError::InvalidDifficulty(Mismatch { expected: min_difficulty, found: header.difficulty })))
}
let min_gas_limit = decode(self.spec().engine_params.get("minGasLimit").unwrap());
if header.gas_limit < min_gas_limit {
return Err(From::from(BlockError::InvalidGasLimit(OutOfBounds { min: min_gas_limit, max: From::from(0), found: header.gas_limit })));
}
let maximum_extra_data_size = self.maximum_extra_data_size();
if header.number != 0 && header.extra_data.len() > maximum_extra_data_size {
return Err(From::from(BlockError::ExtraDataOutOfBounds(OutOfBounds { min: 0, max: maximum_extra_data_size, found: header.extra_data.len() })));
}
// TODO: Verify seal (quick)
Ok(())
}

fn verify_block_unordered(&self, _header: &Header, _block: Option<&[u8]>) -> Result<(), Error> {
// TODO: Verify seal (full)
Ok(())
}

fn verify_block_final(&self, header: &Header, parent: &Header, _block: Option<&[u8]>) -> Result<(), Error> {
// Check difficulty is correct given the two timestamps.
let expected_difficulty = self.calculate_difficuty(header, parent);
if header.difficulty != expected_difficulty {
return Err(From::from(BlockError::InvalidDifficulty(Mismatch { expected: expected_difficulty, found: header.difficulty })))
}
let gas_limit_divisor = decode(self.spec().engine_params.get("gasLimitBoundDivisor").unwrap());
let min_gas = parent.gas_limit - parent.gas_limit / gas_limit_divisor;
let max_gas = parent.gas_limit + parent.gas_limit / gas_limit_divisor;
if header.gas_limit <= min_gas || header.gas_limit >= max_gas {
return Err(From::from(BlockError::InvalidGasLimit(OutOfBounds { min: min_gas, max: max_gas, found: header.gas_limit })));
}
Ok(())
}

fn verify_transaction(&self, _t: &Transaction, _header: &Header) -> Result<(), Error> { Ok(()) }
}

impl Ethash {
fn calculate_difficuty(&self, header: &Header, parent: &Header) -> U256 {
const EXP_DIFF_PERIOD: u64 = 100000;
if header.number == 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: u64 = 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) / 10;
if diff_inc <= 1 {
parent.difficulty + parent.difficulty / From::from(2048) * From::from(1 - diff_inc)
}
else {
parent.difficulty - parent.difficulty / From::from(2048) * From::from(max(diff_inc - 1, 99))
}
};
target = max(min_difficulty, target);
let period = ((parent.number + 1) / EXP_DIFF_PERIOD) as usize;
if period > 1 {
target = max(min_difficulty, target + (U256::from(1) << (period - 2)));
}
target
}
}

#[test]
Expand All @@ -57,3 +134,5 @@ fn on_close_block() {
let b = b.close();
assert_eq!(b.state().balance(&Address::zero()), U256::from_str("4563918244f40000").unwrap());
}

// TODO: difficulty test
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,6 @@ pub mod extras;
pub mod client;
pub mod sync;
pub mod block;
pub mod verification;
pub mod queue;
pub mod ethereum;
36 changes: 21 additions & 15 deletions src/queue.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
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<RwLock<BlockChain>>,
engine: Arc<Box<Engine>>,
}

impl BlockQueue {
/// Creates a new queue instance.
pub fn new() -> BlockQueue {
pub fn new(bc: Arc<RwLock<BlockChain>>, engine: Arc<Box<Engine>>) -> BlockQueue {
BlockQueue {
bc: bc,
engine: engine,
}
}

/// Clear the queue and stop verification activity.
pub fn clear(&mut self) {
}

/// 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(())
}
}

12 changes: 3 additions & 9 deletions src/sync/tests.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Loading

0 comments on commit 4353518

Please sign in to comment.