diff --git a/devtools/src/lib.rs b/devtools/src/lib.rs index e37d5c528..831e4315e 100644 --- a/devtools/src/lib.rs +++ b/devtools/src/lib.rs @@ -19,9 +19,9 @@ extern crate rand; -pub mod random_path; -pub mod test_socket; -pub mod stop_guard; +mod random_path; +mod test_socket; +mod stop_guard; pub use random_path::*; pub use test_socket::*; diff --git a/devtools/src/random_path.rs b/devtools/src/random_path.rs index 09bee7e45..7c1fd19ea 100644 --- a/devtools/src/random_path.rs +++ b/devtools/src/random_path.rs @@ -74,6 +74,25 @@ impl Drop for RandomTempPath { } } +pub struct GuardedTempResult { + pub result: Option, + pub _temp: RandomTempPath +} + +impl GuardedTempResult { + pub fn reference(&self) -> &T { + self.result.as_ref().unwrap() + } + + pub fn reference_mut(&mut self) -> &mut T { + self.result.as_mut().unwrap() + } + + pub fn take(&mut self) -> T { + self.result.take().unwrap() + } +} + #[test] fn creates_dir() { let temp = RandomTempPath::create_dir(); diff --git a/ethcore/src/client/test_client.rs b/ethcore/src/client/test_client.rs index 309934a98..f7387e6ab 100644 --- a/ethcore/src/client/test_client.rs +++ b/ethcore/src/client/test_client.rs @@ -18,6 +18,7 @@ use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrder}; use util::*; +use devtools::*; use transaction::{Transaction, LocalizedTransaction, SignedTransaction, Action}; use blockchain::TreeRoute; use client::{BlockChainClient, MiningBlockChainClient, BlockChainInfo, BlockStatus, BlockID, @@ -29,7 +30,7 @@ use log_entry::LocalizedLogEntry; use receipt::{Receipt, LocalizedReceipt}; use blockchain::extras::BlockReceipts; use error::{ImportResult}; -use evm::Factory as EvmFactory; +use evm::{Factory as EvmFactory, VMType}; use miner::{Miner, MinerService}; use spec::Spec; @@ -67,6 +68,10 @@ pub struct TestBlockChainClient { pub queue_size: AtomicUsize, /// Miner pub miner: Arc, + /// Spec + pub spec: Spec, + /// VM Factory + pub vm_factory: EvmFactory, } #[derive(Clone)] @@ -106,6 +111,8 @@ impl TestBlockChainClient { receipts: RwLock::new(HashMap::new()), queue_size: AtomicUsize::new(0), miner: Arc::new(Miner::with_spec(Spec::new_test())), + spec: Spec::new_test(), + vm_factory: EvmFactory::new(VMType::Interpreter), }; client.add_blocks(1, EachBlockWith::Nothing); // add genesis block client.genesis_hash = client.last_hash.unwrapped_read().clone(); @@ -239,17 +246,43 @@ impl TestBlockChainClient { } } +pub fn get_temp_journal_db() -> GuardedTempResult> { + let temp = RandomTempPath::new(); + let journal_db = journaldb::new(temp.as_str(), journaldb::Algorithm::EarlyMerge, DatabaseConfig::default()); + GuardedTempResult { + _temp: temp, + result: Some(journal_db) + } +} + impl MiningBlockChainClient for TestBlockChainClient { fn prepare_open_block(&self, _author: Address, _gas_range_target: (U256, U256), _extra_data: Bytes) -> OpenBlock { - unimplemented!(); + let engine = &self.spec.engine; + let genesis_header = self.spec.genesis_header(); + let mut db_result = get_temp_journal_db(); + let mut db = db_result.take(); + self.spec.ensure_db_good(db.as_hashdb_mut()); + let last_hashes = vec![genesis_header.hash()]; + OpenBlock::new( + engine.deref(), + self.vm_factory(), + Default::default(), + false, + db, + &genesis_header, + last_hashes, + Address::zero(), + (3141562.into(), 31415620.into()), + vec![] + ).expect("Opening block for tests will not fail.") } fn vm_factory(&self) -> &EvmFactory { - unimplemented!(); + &self.vm_factory } fn import_sealed_block(&self, _block: SealedBlock) -> ImportResult { - unimplemented!(); + Ok(H256::default()) } } diff --git a/ethcore/src/externalities.rs b/ethcore/src/externalities.rs index 5eccc717b..8a44363fa 100644 --- a/ethcore/src/externalities.rs +++ b/ethcore/src/externalities.rs @@ -298,6 +298,7 @@ mod tests { use evm::{Ext}; use substate::*; use tests::helpers::*; + use devtools::GuardedTempResult; use super::*; use trace::{NoopTracer, NoopVMTracer}; diff --git a/ethcore/src/lib.rs b/ethcore/src/lib.rs index f2269501c..a2df34ca5 100644 --- a/ethcore/src/lib.rs +++ b/ethcore/src/lib.rs @@ -98,7 +98,8 @@ pub extern crate ethstore; extern crate semver; extern crate ethcore_ipc_nano as nanoipc; -#[cfg(test)] extern crate ethcore_devtools as devtools; +extern crate ethcore_devtools as devtools; + #[cfg(feature = "jit" )] extern crate evmjit; pub mod account_provider; diff --git a/ethcore/src/miner/miner.rs b/ethcore/src/miner/miner.rs index a423592b3..a2935c4ea 100644 --- a/ethcore/src/miner/miner.rs +++ b/ethcore/src/miner/miner.rs @@ -756,8 +756,6 @@ mod tests { use block::*; use spec::Spec; - // TODO [ToDr] To uncomment` when TestBlockChainClient can actually return a ClosedBlock. - #[ignore] #[test] fn should_prepare_block_to_seal() { // given @@ -769,7 +767,6 @@ mod tests { assert!(sealing_work.is_some(), "Expected closed block"); } - #[ignore] #[test] fn should_still_work_after_a_couple_of_blocks() { // given diff --git a/ethcore/src/tests/helpers.rs b/ethcore/src/tests/helpers.rs index b2e7deb51..883e0aa14 100644 --- a/ethcore/src/tests/helpers.rs +++ b/ethcore/src/tests/helpers.rs @@ -32,26 +32,6 @@ pub enum ChainEra { Homestead, } -#[cfg(test)] -pub struct GuardedTempResult { - result: Option, - _temp: RandomTempPath -} - -impl GuardedTempResult { - pub fn reference(&self) -> &T { - self.result.as_ref().unwrap() - } - - pub fn reference_mut(&mut self) -> &mut T { - self.result.as_mut().unwrap() - } - - pub fn take(&mut self) -> T { - self.result.take().unwrap() - } -} - pub struct TestEngine { engine: Box, max_depth: usize