From 6a5702f27c64fc2cfbe46989df32df10b12fd707 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Mon, 24 Apr 2017 13:14:50 +0200 Subject: [PATCH] address grumbles --- ethcore/src/client/client.rs | 2 +- ethcore/src/engines/epoch_verifier.rs | 2 +- ethcore/src/engines/null_engine.rs | 2 +- ethcore/src/ethereum/ethash.rs | 6 +++++- ethcore/src/snapshot/consensus/mod.rs | 25 ++++++++++++++----------- ethcore/src/snapshot/tests/blocks.rs | 9 +++++---- 6 files changed, 27 insertions(+), 19 deletions(-) diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index 866742230..cc8520cb7 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -414,7 +414,7 @@ impl Client { // Final Verification if let Err(e) = self.verifier.verify_block_final(header, locked_block.block().header()) { - warn!(target: "client", "Stage 4 block verification failed for #{} ({})\nError: {:?}", header.number(), header.hash(), e); + warn!(target: "client", "Stage 5 block verification failed for #{} ({})\nError: {:?}", header.number(), header.hash(), e); return Err(()); } diff --git a/ethcore/src/engines/epoch_verifier.rs b/ethcore/src/engines/epoch_verifier.rs index 62740fbd4..0d9c87e53 100644 --- a/ethcore/src/engines/epoch_verifier.rs +++ b/ethcore/src/engines/epoch_verifier.rs @@ -19,7 +19,7 @@ use error::Error; use header::Header; -/// Verifier for all blocks within an epoch without accessing +/// Verifier for all blocks within an epoch with self-contained state. /// /// See docs on `Engine` relating to proving functions for more details. pub trait EpochVerifier: Sync { diff --git a/ethcore/src/engines/null_engine.rs b/ethcore/src/engines/null_engine.rs index 09bd07607..3bdef480c 100644 --- a/ethcore/src/engines/null_engine.rs +++ b/ethcore/src/engines/null_engine.rs @@ -62,6 +62,6 @@ impl Engine for NullEngine { } fn snapshot_components(&self) -> Option> { - Some(Box::new(::snapshot::PowSnapshot)) + Some(Box::new(::snapshot::PowSnapshot(10000))) } } diff --git a/ethcore/src/ethereum/ethash.rs b/ethcore/src/ethereum/ethash.rs index c7174e197..de53227de 100644 --- a/ethcore/src/ethereum/ethash.rs +++ b/ethcore/src/ethereum/ethash.rs @@ -32,6 +32,10 @@ use rlp::{self, UntrustedRlp}; /// Parity tries to round block.gas_limit to multiple of this constant pub const PARITY_GAS_LIMIT_DETERMINANT: U256 = U256([37, 0, 0, 0]); +/// Number of blocks in an ethash snapshot. +// make dependent on difficulty incrment divisor? +const SNAPSHOT_BLOCKS: u64 = 30000; + /// Ethash params. #[derive(Debug, PartialEq)] pub struct EthashParams { @@ -401,7 +405,7 @@ impl Engine for Arc { } fn snapshot_components(&self) -> Option> { - Some(Box::new(::snapshot::PowSnapshot)) + Some(Box::new(::snapshot::PowSnapshot(SNAPSHOT_BLOCKS))) } } diff --git a/ethcore/src/snapshot/consensus/mod.rs b/ethcore/src/snapshot/consensus/mod.rs index 2e6b6b736..4d853ca27 100644 --- a/ethcore/src/snapshot/consensus/mod.rs +++ b/ethcore/src/snapshot/consensus/mod.rs @@ -36,9 +36,6 @@ use rlp::{RlpStream, UntrustedRlp}; /// A sink for produced chunks. pub type ChunkSink<'a> = FnMut(&[u8]) -> io::Result<()> + 'a; -// How many blocks to include in a snapshot, starting from the head of the chain. -const SNAPSHOT_BLOCKS: u64 = 30000; - /// Components necessary for snapshot creation and restoration. pub trait SnapshotComponents: Send { /// Create secondary snapshot chunks; these corroborate the state data @@ -92,7 +89,11 @@ pub trait Rebuilder: Send { /// Snapshot creation and restoration for PoW chains. /// This includes blocks from the head of the chain as a /// loose assurance that the chain is valid. -pub struct PowSnapshot; +/// +/// The field is the number of blocks from the head of the chain +/// to include in the snapshot. +#[derive(Clone, Copy, PartialEq)] +pub struct PowSnapshot(pub u64); impl SnapshotComponents for PowSnapshot { fn chunk_all( @@ -108,7 +109,7 @@ impl SnapshotComponents for PowSnapshot { current_hash: block_at, writer: chunk_sink, preferred_size: preferred_size, - }.chunk_all() + }.chunk_all(self.0) } fn rebuilder( @@ -117,7 +118,7 @@ impl SnapshotComponents for PowSnapshot { db: Arc, manifest: &ManifestData, ) -> Result, ::error::Error> { - PowRebuilder::new(chain, db, manifest).map(|r| Box::new(r) as Box<_>) + PowRebuilder::new(chain, db, manifest, self.0).map(|r| Box::new(r) as Box<_>) } } @@ -134,13 +135,13 @@ struct PowWorker<'a> { impl<'a> PowWorker<'a> { // Repeatedly fill the buffers and writes out chunks, moving backwards from starting block hash. // Loops until we reach the first desired block, and writes out the remainder. - fn chunk_all(&mut self) -> Result<(), Error> { + fn chunk_all(&mut self, snapshot_blocks: u64) -> Result<(), Error> { let mut loaded_size = 0; let mut last = self.current_hash; let genesis_hash = self.chain.genesis_hash(); - for _ in 0..SNAPSHOT_BLOCKS { + for _ in 0..snapshot_blocks { if self.current_hash == genesis_hash { break } let (block, receipts) = self.chain.block(&self.current_hash) @@ -229,11 +230,12 @@ pub struct PowRebuilder { best_hash: H256, best_root: H256, fed_blocks: u64, + snapshot_blocks: u64, } impl PowRebuilder { /// Create a new PowRebuilder. - fn new(chain: BlockChain, db: Arc, manifest: &ManifestData) -> Result { + fn new(chain: BlockChain, db: Arc, manifest: &ManifestData, snapshot_blocks: u64) -> Result { Ok(PowRebuilder { chain: chain, db: db, @@ -243,6 +245,7 @@ impl PowRebuilder { best_hash: manifest.block_hash, best_root: manifest.state_root, fed_blocks: 0, + snapshot_blocks: snapshot_blocks, }) } } @@ -263,8 +266,8 @@ impl Rebuilder for PowRebuilder { trace!(target: "snapshot", "restoring block chunk with {} blocks.", item_count - 3); - if self.fed_blocks + num_blocks > SNAPSHOT_BLOCKS { - return Err(Error::TooManyBlocks(SNAPSHOT_BLOCKS, self.fed_blocks).into()) + if self.fed_blocks + num_blocks > self.snapshot_blocks { + return Err(Error::TooManyBlocks(self.snapshot_blocks, self.fed_blocks).into()) } // todo: assert here that these values are consistent with chunks being in order. diff --git a/ethcore/src/snapshot/tests/blocks.rs b/ethcore/src/snapshot/tests/blocks.rs index fae9ae75e..2769644e8 100644 --- a/ethcore/src/snapshot/tests/blocks.rs +++ b/ethcore/src/snapshot/tests/blocks.rs @@ -30,11 +30,12 @@ use util::kvdb::{self, KeyValueDB, DBTransaction}; use std::sync::Arc; use std::sync::atomic::AtomicBool; +const SNAPSHOT_MODE: ::snapshot::PowSnapshot = ::snapshot::PowSnapshot(30000); + fn chunk_and_restore(amount: u64) { let mut canon_chain = ChainGenerator::default(); let mut finalizer = BlockFinalizer::default(); let genesis = canon_chain.generate(&mut finalizer).unwrap(); - let components = ::snapshot::PowSnapshot; let engine = Arc::new(::engines::NullEngine::default()); let new_path = RandomTempPath::create_dir(); @@ -59,7 +60,7 @@ fn chunk_and_restore(amount: u64) { // snapshot it. let writer = Mutex::new(PackedWriter::new(&snapshot_path).unwrap()); let block_hashes = chunk_secondary( - Box::new(::snapshot::PowSnapshot), + Box::new(SNAPSHOT_MODE), &bc, best_hash, &writer, @@ -80,7 +81,7 @@ fn chunk_and_restore(amount: u64) { // restore it. let new_db = Arc::new(kvdb::in_memory(::db::NUM_COLUMNS.unwrap_or(0))); let new_chain = BlockChain::new(Default::default(), &genesis, new_db.clone()); - let mut rebuilder = components.rebuilder(new_chain, new_db.clone(), &manifest).unwrap(); + let mut rebuilder = SNAPSHOT_MODE.rebuilder(new_chain, new_db.clone(), &manifest).unwrap(); let reader = PackedReader::new(&snapshot_path).unwrap().unwrap(); let flag = AtomicBool::new(true); @@ -138,7 +139,7 @@ fn checks_flag() { block_hash: H256::default(), }; - let mut rebuilder = ::snapshot::PowSnapshot.rebuilder(chain, db.clone(), &manifest).unwrap(); + let mut rebuilder = SNAPSHOT_MODE.rebuilder(chain, db.clone(), &manifest).unwrap(); match rebuilder.feed(&chunk, engine.as_ref(), &AtomicBool::new(false)) { Err(Error::Snapshot(SnapshotError::RestorationAborted)) => {}