Configurable path for chain db. Lowered test cuckoo size.

This commit is contained in:
Ignotus Peverell 2016-11-29 18:45:39 -08:00
parent d11c85dae1
commit 08f2f38098
No known key found for this signature in database
GPG key ID: 99CD25F39F8F8211
5 changed files with 22 additions and 25 deletions

View file

@ -35,5 +35,5 @@ pub mod types;
// Re-export the base interface // Re-export the base interface
pub use types::{ChainStore, State, Tip}; pub use types::{ChainStore, Tip};
pub use pipe::{NONE, process_block}; pub use pipe::{NONE, process_block};

View file

@ -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)); let prev = try!(ctx.store.get_block_header(&header.previous).map_err(&Error::StoreErr));
if header.timestamp <= prev.timestamp { 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); return Err(Error::InvalidBlockTime);
} }
if header.timestamp > time::now() + time::Duration::seconds(12*(consensus::BLOCK_TIME_SEC as i64)) { if header.timestamp >
// refuse blocks too far in future, constant of 12 comes from bitcoin (2h worth of blocks) time::now() + time::Duration::seconds(12 * (consensus::BLOCK_TIME_SEC as i64)) {
// TODO add warning in p2p code if local time is too different from peers // 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); return Err(Error::InvalidBlockTime);
} }
let (diff_target, cuckoo_sz) = consensus::next_target(header.timestamp.to_timespec().sec, let (diff_target, cuckoo_sz) = consensus::next_target(header.timestamp.to_timespec().sec,
prev.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 ctx.opts.intersects(EASY_POW) {
if !pow::verify20(b) { if !pow::verify_size(b, 15) {
return Err(Error::InvalidPow); return Err(Error::InvalidPow);
} }
} else if !pow::verify_size(b, cuckoo_sz as u32) { } else if !pow::verify_size(b, cuckoo_sz as u32) {

View file

@ -21,7 +21,7 @@ use core::core::hash::Hash;
use core::core::{Block, BlockHeader}; use core::core::{Block, BlockHeader};
use grin_store; use grin_store;
const STORE_PATH: &'static str = ".grin/chain"; const STORE_SUBPATH: &'static str = "chain";
const SEP: u8 = ':' as u8; const SEP: u8 = ':' as u8;
@ -37,8 +37,9 @@ pub struct ChainKVStore {
} }
impl ChainKVStore { impl ChainKVStore {
pub fn new() -> Result<ChainKVStore, Error> { pub fn new(root_path: String) -> Result<ChainKVStore, Error> {
let db = try!(grin_store::Store::open(STORE_PATH).map_err(to_store_err)); let db = try!(grin_store::Store::open(format!("{}/{}", root_path, STORE_SUBPATH).as_str())
.map_err(to_store_err));
Ok(ChainKVStore { db: db }) Ok(ChainKVStore { db: db })
} }
} }
@ -48,10 +49,10 @@ impl ChainStore for ChainKVStore {
option_to_not_found(self.db.get_ser(&vec![HEAD_PREFIX])) option_to_not_found(self.db.get_ser(&vec![HEAD_PREFIX]))
} }
fn head_header(&self) -> Result<BlockHeader, Error> { fn head_header(&self) -> Result<BlockHeader, Error> {
let head: Tip = try!(option_to_not_found(self.db.get_ser(&vec![HEAD_PREFIX]))); 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> { fn save_block(&self, b: &Block) -> Result<(), Error> {
try!(self.db try!(self.db

View file

@ -131,20 +131,14 @@ pub enum Error {
StorageErr(String), StorageErr(String),
} }
#[derive(Debug, Clone)]
pub struct State {
pub head: Tip,
pub forks: Vec<Tip>,
}
/// Trait the chain pipeline requires an implementor for in order to process /// Trait the chain pipeline requires an implementor for in order to process
/// blocks. /// blocks.
pub trait ChainStore { pub trait ChainStore: Send {
/// Get the tip that's also the head of the chain /// Get the tip that's also the head of the chain
fn head(&self) -> Result<Tip, Error>; fn head(&self) -> Result<Tip, Error>;
/// Block header for the chain head /// Block header for the chain head
fn head_header(&self) -> Result<BlockHeader, Error>; fn head_header(&self) -> Result<BlockHeader, Error>;
/// Gets a block header by hash /// Gets a block header by hash
fn get_block_header(&self, h: &Hash) -> Result<BlockHeader, Error>; fn get_block_header(&self, h: &Hash) -> Result<BlockHeader, Error>;
@ -158,4 +152,3 @@ pub trait ChainStore {
/// Save the provided tip without setting it as head /// Save the provided tip without setting it as head
fn save_tip(&self, t: &Tip) -> Result<(), Error>; fn save_tip(&self, t: &Tip) -> Result<(), Error>;
} }

View file

@ -28,7 +28,7 @@ use grin_core::consensus;
fn mine_empty_chain() { fn mine_empty_chain() {
let curve = secp::Secp256k1::with_caps(secp::ContextFlag::Commit); let curve = secp::Secp256k1::with_caps(secp::ContextFlag::Commit);
let mut rng = OsRng::new().unwrap(); 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 // save a genesis block
let gen = grin_core::genesis::genesis(); 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(); let mut b = core::Block::new(prev.header, vec![], reward_key).unwrap();
println!("=> {} {:?}", b.header.height, b.verify(&curve)); 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.pow = proof;
b.header.nonce = nonce; b.header.nonce = nonce;
grin_chain::pipe::process_block(&b, &store, grin_chain::pipe::EASY_POW).unwrap(); grin_chain::pipe::process_block(&b, &store, grin_chain::pipe::EASY_POW).unwrap();