test storage to db
This commit is contained in:
parent
527cadc880
commit
7ebd4c947a
|
@ -13,3 +13,6 @@ chain = { path = "../chain" }
|
||||||
serialization = { path = "../serialization" }
|
serialization = { path = "../serialization" }
|
||||||
parking_lot = "0.3"
|
parking_lot = "0.3"
|
||||||
test-data = { path = "../test-data" }
|
test-data = { path = "../test-data" }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
dev = []
|
||||||
|
|
|
@ -15,6 +15,8 @@ extern crate test_data;
|
||||||
|
|
||||||
mod kvdb;
|
mod kvdb;
|
||||||
mod storage;
|
mod storage;
|
||||||
|
#[cfg(feature="dev")]
|
||||||
|
mod test_storage;
|
||||||
|
|
||||||
pub enum BlockRef {
|
pub enum BlockRef {
|
||||||
Number(u64),
|
Number(u64),
|
||||||
|
@ -23,3 +25,6 @@ pub enum BlockRef {
|
||||||
|
|
||||||
pub use storage::{Storage, Store, Error};
|
pub use storage::{Storage, Store, Error};
|
||||||
pub use kvdb::Database;
|
pub use kvdb::Database;
|
||||||
|
|
||||||
|
#[cfg(feature="dev")]
|
||||||
|
pub use test_storage::TestStorage;
|
||||||
|
|
|
@ -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(())
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,7 +4,6 @@ version = "0.1.0"
|
||||||
authors = ["Nikolay Volf <nikvolf@gmail.com>"]
|
authors = ["Nikolay Volf <nikvolf@gmail.com>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
db = { path = "../db" }
|
|
||||||
ethcore-devtools = { path = "../devtools" }
|
ethcore-devtools = { path = "../devtools" }
|
||||||
primitives = { path = "../primitives" }
|
primitives = { path = "../primitives" }
|
||||||
chain = { path = "../chain" }
|
chain = { path = "../chain" }
|
||||||
|
@ -15,3 +14,7 @@ test-data = { path = "../test-data" }
|
||||||
byteorder = "0.5"
|
byteorder = "0.5"
|
||||||
time = "0.1"
|
time = "0.1"
|
||||||
script = { path = "../script" }
|
script = { path = "../script" }
|
||||||
|
|
||||||
|
[dependencies.db]
|
||||||
|
path = "../db"
|
||||||
|
features = ["dev"]
|
||||||
|
|
|
@ -106,85 +106,10 @@ mod tests {
|
||||||
|
|
||||||
use super::ChainVerifier;
|
use super::ChainVerifier;
|
||||||
use super::super::{Verify, Chain};
|
use super::super::{Verify, Chain};
|
||||||
use primitives::hash::H256;
|
use db::TestStorage;
|
||||||
use chain;
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use db::{self, BlockRef, Store};
|
|
||||||
use serialization;
|
|
||||||
use serialization::bytes::Bytes;
|
|
||||||
use test_data;
|
use test_data;
|
||||||
use std::sync::Arc;
|
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]
|
#[test]
|
||||||
fn verify_orphan() {
|
fn verify_orphan() {
|
||||||
let storage = TestStorage::with_blocks(&vec![test_data::genesis()]);
|
let storage = TestStorage::with_blocks(&vec![test_data::genesis()]);
|
||||||
|
|
Loading…
Reference in New Issue