test storage to db

This commit is contained in:
NikVolf 2016-10-24 17:08:25 +03:00
parent 527cadc880
commit 7ebd4c947a
5 changed files with 91 additions and 77 deletions

View File

@ -13,3 +13,6 @@ chain = { path = "../chain" }
serialization = { path = "../serialization" }
parking_lot = "0.3"
test-data = { path = "../test-data" }
[features]
dev = []

View File

@ -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;

78
db/src/test_storage.rs Normal file
View File

@ -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<H256, chain::Block>,
heights: HashMap<usize, H256>,
}
impl TestStorage {
fn resolve_hash(&self, block_ref: BlockRef) -> Option<H256> {
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<H256> {
self.heights.get(&(number as usize)).cloned()
}
fn block_header_bytes(&self, block_ref: BlockRef) -> Option<Bytes> {
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<H256> {
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<Bytes> {
self.transaction(hash).map(|tx| serialization::serialize(&tx))
}
fn transaction(&self, hash: &H256) -> Option<chain::Transaction> {
self.blocks.iter().flat_map(|(_, b)| b.transactions())
.find(|ref tx| tx.hash() == *hash)
.cloned()
}
fn block_transactions(&self, block_ref: BlockRef) -> Vec<chain::Transaction> {
self.block(block_ref)
.map(|b| b.transactions().iter().cloned().collect())
.unwrap_or(Vec::new())
}
fn block(&self, block_ref: BlockRef) -> Option<chain::Block> {
self.resolve_hash(block_ref)
.and_then(|ref h| self.blocks.get(h))
.cloned()
}
fn insert_block(&self, _block: &chain::Block) -> Result<(), Error> {
Ok(())
}
}

View File

@ -4,7 +4,6 @@ version = "0.1.0"
authors = ["Nikolay Volf <nikvolf@gmail.com>"]
[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"]

View File

@ -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<H256, chain::Block>,
heights: HashMap<usize, H256>,
}
impl TestStorage {
fn resolve_hash(&self, block_ref: BlockRef) -> Option<H256> {
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<H256> {
self.heights.get(&(number as usize)).cloned()
}
fn block_header_bytes(&self, block_ref: BlockRef) -> Option<Bytes> {
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<H256> {
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<Bytes> {
self.transaction(hash).map(|tx| serialization::serialize(&tx))
}
fn transaction(&self, hash: &H256) -> Option<chain::Transaction> {
self.blocks.iter().flat_map(|(_, b)| b.transactions())
.find(|ref tx| tx.hash() == *hash)
.cloned()
}
fn block_transactions(&self, block_ref: BlockRef) -> Vec<chain::Transaction> {
self.block(block_ref)
.map(|b| b.transactions().iter().cloned().collect())
.unwrap_or(Vec::new())
}
fn block(&self, block_ref: BlockRef) -> Option<chain::Block> {
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()]);