From 08f2f380981ab2b949b86ad00812b08aba9dcdf2 Mon Sep 17 00:00:00 2001 From: Ignotus Peverell Date: Tue, 29 Nov 2016 18:45:39 -0800 Subject: [PATCH] Configurable path for chain db. Lowered test cuckoo size. --- chain/src/lib.rs | 2 +- chain/src/pipe.rs | 15 +++++++++------ chain/src/store.rs | 13 +++++++------ chain/src/types.rs | 13 +++---------- chain/tests/mine_simple_chain.rs | 4 ++-- 5 files changed, 22 insertions(+), 25 deletions(-) diff --git a/chain/src/lib.rs b/chain/src/lib.rs index 28f5b9602..6b17e9293 100644 --- a/chain/src/lib.rs +++ b/chain/src/lib.rs @@ -35,5 +35,5 @@ pub mod types; // Re-export the base interface -pub use types::{ChainStore, State, Tip}; +pub use types::{ChainStore, Tip}; pub use pipe::{NONE, process_block}; diff --git a/chain/src/pipe.rs b/chain/src/pipe.rs index 30677cfba..897c73225 100644 --- a/chain/src/pipe.rs +++ b/chain/src/pipe.rs @@ -102,14 +102,17 @@ fn validate_header(b: &Block, ctx: &mut BlockContext) -> Result<(), Error> { let prev = try!(ctx.store.get_block_header(&header.previous).map_err(&Error::StoreErr)); if header.timestamp <= prev.timestamp { - // prevent time warp attacks and some timestamp manipulations by forcing strict time progression + // prevent time warp attacks and some timestamp manipulations by forcing strict + // time progression return Err(Error::InvalidBlockTime); } - if header.timestamp > time::now() + time::Duration::seconds(12*(consensus::BLOCK_TIME_SEC as i64)) { - // refuse blocks too far in future, constant of 12 comes from bitcoin (2h worth of blocks) - // TODO add warning in p2p code if local time is too different from peers + if header.timestamp > + time::now() + time::Duration::seconds(12 * (consensus::BLOCK_TIME_SEC as i64)) { + // refuse blocks too far in future, constant of 12 comes from bitcoin (2h worth + // of blocks) + // TODO add warning in p2p code if local time is too different from peers return Err(Error::InvalidBlockTime); - } + } let (diff_target, cuckoo_sz) = consensus::next_target(header.timestamp.to_timespec().sec, prev.timestamp.to_timespec().sec, @@ -120,7 +123,7 @@ fn validate_header(b: &Block, ctx: &mut BlockContext) -> Result<(), Error> { } if ctx.opts.intersects(EASY_POW) { - if !pow::verify20(b) { + if !pow::verify_size(b, 15) { return Err(Error::InvalidPow); } } else if !pow::verify_size(b, cuckoo_sz as u32) { diff --git a/chain/src/store.rs b/chain/src/store.rs index 7866f8634..a3c5415d8 100644 --- a/chain/src/store.rs +++ b/chain/src/store.rs @@ -21,7 +21,7 @@ use core::core::hash::Hash; use core::core::{Block, BlockHeader}; use grin_store; -const STORE_PATH: &'static str = ".grin/chain"; +const STORE_SUBPATH: &'static str = "chain"; const SEP: u8 = ':' as u8; @@ -37,8 +37,9 @@ pub struct ChainKVStore { } impl ChainKVStore { - pub fn new() -> Result { - let db = try!(grin_store::Store::open(STORE_PATH).map_err(to_store_err)); + pub fn new(root_path: String) -> Result { + let db = try!(grin_store::Store::open(format!("{}/{}", root_path, STORE_SUBPATH).as_str()) + .map_err(to_store_err)); Ok(ChainKVStore { db: db }) } } @@ -48,10 +49,10 @@ impl ChainStore for ChainKVStore { option_to_not_found(self.db.get_ser(&vec![HEAD_PREFIX])) } - fn head_header(&self) -> Result { + fn head_header(&self) -> Result { let head: Tip = try!(option_to_not_found(self.db.get_ser(&vec![HEAD_PREFIX]))); - self.get_block_header(&head.last_block_h) - } + self.get_block_header(&head.last_block_h) + } fn save_block(&self, b: &Block) -> Result<(), Error> { try!(self.db diff --git a/chain/src/types.rs b/chain/src/types.rs index 62048088e..dc499d788 100644 --- a/chain/src/types.rs +++ b/chain/src/types.rs @@ -131,20 +131,14 @@ pub enum Error { StorageErr(String), } -#[derive(Debug, Clone)] -pub struct State { - pub head: Tip, - pub forks: Vec, -} - /// Trait the chain pipeline requires an implementor for in order to process /// blocks. -pub trait ChainStore { +pub trait ChainStore: Send { /// Get the tip that's also the head of the chain fn head(&self) -> Result; - /// Block header for the chain head - fn head_header(&self) -> Result; + /// Block header for the chain head + fn head_header(&self) -> Result; /// Gets a block header by hash fn get_block_header(&self, h: &Hash) -> Result; @@ -158,4 +152,3 @@ pub trait ChainStore { /// Save the provided tip without setting it as head fn save_tip(&self, t: &Tip) -> Result<(), Error>; } - diff --git a/chain/tests/mine_simple_chain.rs b/chain/tests/mine_simple_chain.rs index 102d6cac5..8fb0c6101 100644 --- a/chain/tests/mine_simple_chain.rs +++ b/chain/tests/mine_simple_chain.rs @@ -28,7 +28,7 @@ use grin_core::consensus; fn mine_empty_chain() { let curve = secp::Secp256k1::with_caps(secp::ContextFlag::Commit); let mut rng = OsRng::new().unwrap(); - let store = grin_chain::store::ChainKVStore::new().unwrap(); + let store = grin_chain::store::ChainKVStore::new(".grin".to_string()).unwrap(); // save a genesis block let gen = grin_core::genesis::genesis(); @@ -47,7 +47,7 @@ fn mine_empty_chain() { let mut b = core::Block::new(prev.header, vec![], reward_key).unwrap(); println!("=> {} {:?}", b.header.height, b.verify(&curve)); - let (proof, nonce) = pow::pow20(&b, consensus::MAX_TARGET).unwrap(); + let (proof, nonce) = pow::pow_size(&b, consensus::MAX_TARGET, 15).unwrap(); b.header.pow = proof; b.header.nonce = nonce; grin_chain::pipe::process_block(&b, &store, grin_chain::pipe::EASY_POW).unwrap();