basic processing

This commit is contained in:
NikVolf 2016-10-18 16:53:59 +03:00
parent 77fe3bf887
commit e50402fb14
5 changed files with 71 additions and 2 deletions

7
Cargo.lock generated
View File

@ -199,6 +199,11 @@ name = "libc"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "linked-hash-map"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "log"
version = "0.3.6"
@ -504,6 +509,7 @@ dependencies = [
"chain 0.1.0",
"db 0.1.0",
"ethcore-devtools 1.3.0",
"linked-hash-map 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"primitives 0.1.0",
"serialization 0.1.0",
@ -558,6 +564,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "49247ec2a285bb3dcb23cbd9c35193c025e7251bfce77c1d5da97e6362dffe7f"
"checksum lazycell 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ce12306c4739d86ee97c23139f3a34ddf0387bbf181bc7929d287025a8c3ef6b"
"checksum libc 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)" = "408014cace30ee0f767b1c4517980646a573ec61a57957aeeabcac8ac0a02e8d"
"checksum linked-hash-map 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6d262045c5b87c0861b3f004610afd0e2c851e2908d08b6c870cbb9d5f494ecd"
"checksum log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ab83497bf8bf4ed2a74259c1c802351fcd67a65baa86394b6ba73c36f4838054"
"checksum mio 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2dadd39d4b47343e10513ac2a731c979517a4761224ecb6bbd243602300c9537"
"checksum miow 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d5bfc6782530ac8ace97af10a540054a37126b63b0702ddaaa243b73b5745b9a"

View File

@ -1,5 +1,6 @@
use std::{fmt, ops, cmp, str};
use hex::{ToHex, FromHex, FromHexError};
use std::hash::{Hash, Hasher};
macro_rules! impl_hash {
($name: ident, $size: expr) => {
@ -104,6 +105,15 @@ macro_rules! impl_hash {
}
}
impl Hash for $name {
fn hash<H>(&self, state: &mut H) where H: Hasher {
state.write(&self.0);
state.finish();
}
}
impl Eq for $name { }
impl $name {
pub fn reversed(&self) -> Self {
let mut result = self.clone();

View File

@ -10,3 +10,4 @@ primitives = { path = "../primitives" }
chain = { path = "../chain" }
serialization = { path = "../serialization" }
parking_lot = "0.3"
linked-hash-map = "0.3"

View File

@ -5,12 +5,16 @@ extern crate primitives;
extern crate chain;
extern crate serialization;
extern crate parking_lot;
extern crate linked_hash_map;
#[cfg(test)]
extern crate ethcore_devtools as devtools;
mod queue;
use primitives::hash::H256;
#[derive(Debug)]
/// All possible verification errors
pub enum Error {
/// has an equal duplicate in the chain
@ -29,6 +33,7 @@ pub enum Error {
Difficulty
}
#[derive(Debug)]
/// Possible transactions verification errors
pub enum TransactionError {
/// Not found corresponding output for transaction input
@ -54,5 +59,5 @@ pub type VerificationResult = Result<Chain, Error>;
/// Interface for block verification
pub trait Verify {
fn verify(block: &chain::Block) -> Result<VerificationResult, Error>;
fn verify(&self, block: &chain::Block) -> VerificationResult;
}

View File

@ -1,5 +1,51 @@
//! Blocks verification queue
struct Queue {
use chain::{Block, BlockHeader};
use primitives::hash::H256;
use super::{Chain, Verify};
use linked_hash_map::LinkedHashMap;
use parking_lot::RwLock;
use std::collections::HashSet;
pub struct VerifiedBlock {
chain: Chain,
block: Block,
}
impl VerifiedBlock {
fn new(chain: Chain, block: Block) -> Self {
VerifiedBlock { chain: chain, block: block }
}
}
pub struct Queue {
verifier: Box<Verify>,
items: RwLock<LinkedHashMap<H256, Block>>,
verified: RwLock<LinkedHashMap<H256, VerifiedBlock>>,
invalid: RwLock<HashSet<H256>>,
}
impl Queue {
pub fn process(&self) {
let (hash, block) = {
let mut items = self.items.write();
match items.pop_front() {
Some((hash, block)) => (hash, block),
/// nothing to verify
None => { return; },
}
};
match self.verifier.verify(&block) {
Ok(chain) => {
let mut verified = self.verified.write();
verified.insert(hash, VerifiedBlock::new(chain, block));
},
Err(e) => {
println!("Verification failed: {:?}", e);
let mut invalid = self.invalid.write();
invalid.insert(hash);
}
}
}
}