diff --git a/db/Cargo.toml b/db/Cargo.toml index 9962bc54..800fdc32 100644 --- a/db/Cargo.toml +++ b/db/Cargo.toml @@ -13,3 +13,6 @@ chain = { path = "../chain" } serialization = { path = "../serialization" } parking_lot = "0.3" test-data = { path = "../test-data" } + +[features] +dev = [] diff --git a/db/src/lib.rs b/db/src/lib.rs index dc826523..8b892a20 100644 --- a/db/src/lib.rs +++ b/db/src/lib.rs @@ -15,6 +15,8 @@ extern crate test_data; mod kvdb; mod storage; +#[cfg(feature="dev")] +mod test_storage; pub enum BlockRef { Number(u64), @@ -23,3 +25,6 @@ pub enum BlockRef { pub use storage::{Storage, Store, Error}; pub use kvdb::Database; + +#[cfg(feature="dev")] +pub use test_storage::TestStorage; diff --git a/db/src/test_storage.rs b/db/src/test_storage.rs new file mode 100644 index 00000000..b62c27bd --- /dev/null +++ b/db/src/test_storage.rs @@ -0,0 +1,78 @@ +//! Test storage + +use super::{BlockRef, Store, Error}; +use chain; +use primitives::hash::H256; +use serialization; +use chain::bytes::Bytes; +use std::collections::HashMap; + +#[derive(Default)] +pub struct TestStorage { + blocks: HashMap, + heights: HashMap, +} + +impl TestStorage { + fn resolve_hash(&self, block_ref: BlockRef) -> Option { + match block_ref { + BlockRef::Number(n) => self.block_hash(n), + BlockRef::Hash(h) => Some(h), + } + } + + pub fn with_blocks(blocks: &[chain::Block]) -> Self { + let mut storage = TestStorage::default(); + for (idx, block) in blocks.iter().enumerate() { + let hash = block.hash(); + storage.blocks.insert(hash.clone(), block.clone()); + storage.heights.insert(idx, hash); + } + storage + } +} + +impl Store for TestStorage { + fn block_hash(&self, number: u64) -> Option { + self.heights.get(&(number as usize)).cloned() + } + + fn block_header_bytes(&self, block_ref: BlockRef) -> Option { + self.resolve_hash(block_ref) + .and_then(|ref h| self.blocks.get(h)) + .map(|ref block| serialization::serialize(block.header())) + } + + fn block_transaction_hashes(&self, block_ref: BlockRef) -> Vec { + self.resolve_hash(block_ref) + .and_then(|ref h| self.blocks.get(h)) + .map(|ref block| block.transactions().iter().map(|tx| tx.hash()).collect()) + .unwrap_or(Vec::new()) + } + + fn transaction_bytes(&self, hash: &H256) -> Option { + self.transaction(hash).map(|tx| serialization::serialize(&tx)) + } + + fn transaction(&self, hash: &H256) -> Option { + self.blocks.iter().flat_map(|(_, b)| b.transactions()) + .find(|ref tx| tx.hash() == *hash) + .cloned() + } + + fn block_transactions(&self, block_ref: BlockRef) -> Vec { + self.block(block_ref) + .map(|b| b.transactions().iter().cloned().collect()) + .unwrap_or(Vec::new()) + } + + fn block(&self, block_ref: BlockRef) -> Option { + self.resolve_hash(block_ref) + .and_then(|ref h| self.blocks.get(h)) + .cloned() + } + + fn insert_block(&self, _block: &chain::Block) -> Result<(), Error> { + Ok(()) + } +} diff --git a/verification/Cargo.toml b/verification/Cargo.toml index 363785b7..e98c7782 100644 --- a/verification/Cargo.toml +++ b/verification/Cargo.toml @@ -4,7 +4,6 @@ version = "0.1.0" authors = ["Nikolay Volf "] [dependencies] -db = { path = "../db" } ethcore-devtools = { path = "../devtools" } primitives = { path = "../primitives" } chain = { path = "../chain" } @@ -15,3 +14,7 @@ test-data = { path = "../test-data" } byteorder = "0.5" time = "0.1" script = { path = "../script" } + +[dependencies.db] +path = "../db" +features = ["dev"] diff --git a/verification/src/chain_verifier.rs b/verification/src/chain_verifier.rs index cb6367fb..7e927252 100644 --- a/verification/src/chain_verifier.rs +++ b/verification/src/chain_verifier.rs @@ -106,85 +106,10 @@ mod tests { use super::ChainVerifier; use super::super::{Verify, Chain}; - use primitives::hash::H256; - use chain; - use std::collections::HashMap; - use db::{self, BlockRef, Store}; - use serialization; - use serialization::bytes::Bytes; + use db::TestStorage; use test_data; use std::sync::Arc; - #[derive(Default)] - struct TestStorage { - blocks: HashMap, - heights: HashMap, - } - - impl TestStorage { - fn resolve_hash(&self, block_ref: BlockRef) -> Option { - match block_ref { - BlockRef::Number(n) => self.block_hash(n), - BlockRef::Hash(h) => Some(h), - } - } - - fn with_blocks(blocks: &[chain::Block]) -> Self { - let mut storage = TestStorage::default(); - for (idx, block) in blocks.iter().enumerate() { - let hash = block.hash(); - storage.blocks.insert(hash.clone(), block.clone()); - storage.heights.insert(idx, hash); - } - storage - } - } - - impl Store for TestStorage { - fn block_hash(&self, number: u64) -> Option { - self.heights.get(&(number as usize)).cloned() - } - - fn block_header_bytes(&self, block_ref: BlockRef) -> Option { - self.resolve_hash(block_ref) - .and_then(|ref h| self.blocks.get(h)) - .map(|ref block| serialization::serialize(block.header())) - } - - fn block_transaction_hashes(&self, block_ref: BlockRef) -> Vec { - self.resolve_hash(block_ref) - .and_then(|ref h| self.blocks.get(h)) - .map(|ref block| block.transactions().iter().map(|tx| tx.hash()).collect()) - .unwrap_or(Vec::new()) - } - - fn transaction_bytes(&self, hash: &H256) -> Option { - self.transaction(hash).map(|tx| serialization::serialize(&tx)) - } - - fn transaction(&self, hash: &H256) -> Option { - self.blocks.iter().flat_map(|(_, b)| b.transactions()) - .find(|ref tx| tx.hash() == *hash) - .cloned() - } - - fn block_transactions(&self, block_ref: BlockRef) -> Vec { - self.block(block_ref) - .map(|b| b.transactions().iter().cloned().collect()) - .unwrap_or(Vec::new()) - } - - fn block(&self, block_ref: BlockRef) -> Option { - self.resolve_hash(block_ref) - .and_then(|ref h| self.blocks.get(h)) - .cloned() - } - - fn insert_block(&self, _block: &chain::Block) -> Result<(), db::Error> { - Ok(()) - } - } - #[test] fn verify_orphan() { let storage = TestStorage::with_blocks(&vec![test_data::genesis()]);