diff --git a/src/block.rs b/src/block.rs index 963124be0..f8e8ef16f 100644 --- a/src/block.rs +++ b/src/block.rs @@ -349,38 +349,49 @@ pub fn enact_and_seal(block_bytes: &[u8], engine: &Engine, db: JournalDB, parent Ok(try!(try!(enact_bytes(block_bytes, engine, db, parent, last_hashes)).seal(header.seal()))) } -#[test] -fn open_block() { - use spec::*; - let engine = Spec::new_test().to_engine().unwrap(); - let genesis_header = engine.spec().genesis_header(); - let mut db = JournalDB::new_temp(); - engine.spec().ensure_db_good(&mut db); - let last_hashes = vec![genesis_header.hash()]; - let b = OpenBlock::new(engine.deref(), db, &genesis_header, &last_hashes, Address::zero(), vec![]); - let b = b.close(); - let _ = b.seal(vec![]); -} +#[cfg(test)] +mod tests { + use tests::helpers::*; + use super::*; + use common::*; + use engine::*; -#[test] -fn enact_block() { - use spec::*; - let engine = Spec::new_test().to_engine().unwrap(); - let genesis_header = engine.spec().genesis_header(); + #[test] + fn open_block() { + use spec::*; + let engine = Spec::new_test().to_engine().unwrap(); + let genesis_header = engine.spec().genesis_header(); + let mut db_result = get_temp_journal_db(); + let db = db_result.reference_mut(); + engine.spec().ensure_db_good(db); + let last_hashes = vec![genesis_header.hash()]; + let b = OpenBlock::new(engine.deref(), db.clone(), &genesis_header, &last_hashes, Address::zero(), vec![]); + let b = b.close(); + let _ = b.seal(vec![]); + } - let mut db = JournalDB::new_temp(); - engine.spec().ensure_db_good(&mut db); - let b = OpenBlock::new(engine.deref(), db, &genesis_header, &vec![genesis_header.hash()], Address::zero(), vec![]).close().seal(vec![]).unwrap(); - let orig_bytes = b.rlp_bytes(); - let orig_db = b.drain(); + #[test] + fn enact_block() { + use spec::*; + let engine = Spec::new_test().to_engine().unwrap(); + let genesis_header = engine.spec().genesis_header(); - let mut db = JournalDB::new_temp(); - engine.spec().ensure_db_good(&mut db); - let e = enact_and_seal(&orig_bytes, engine.deref(), db, &genesis_header, &vec![genesis_header.hash()]).unwrap(); + let mut db_result = get_temp_journal_db(); + let db = db_result.reference_mut(); + engine.spec().ensure_db_good(db); + let b = OpenBlock::new(engine.deref(), db.clone(), &genesis_header, &vec![genesis_header.hash()], Address::zero(), vec![]).close().seal(vec![]).unwrap(); + let orig_bytes = b.rlp_bytes(); + let orig_db = b.drain(); - assert_eq!(e.rlp_bytes(), orig_bytes); - - let db = e.drain(); - assert_eq!(orig_db.keys(), db.keys()); - assert!(orig_db.keys().iter().filter(|k| orig_db.get(k.0) != db.get(k.0)).next() == None); -} + let mut db_result = get_temp_journal_db(); + let db = db_result.reference_mut(); + engine.spec().ensure_db_good(db); + let e = enact_and_seal(&orig_bytes, engine.deref(), db.clone(), &genesis_header, &vec![genesis_header.hash()]).unwrap(); + + assert_eq!(e.rlp_bytes(), orig_bytes); + + let db = e.drain(); + assert_eq!(orig_db.keys(), db.keys()); + assert!(orig_db.keys().iter().filter(|k| orig_db.get(k.0) != db.get(k.0)).next() == None); + } +} \ No newline at end of file diff --git a/src/ethereum/ethash.rs b/src/ethereum/ethash.rs index f28ea31c6..ce40c5f42 100644 --- a/src/ethereum/ethash.rs +++ b/src/ethereum/ethash.rs @@ -7,6 +7,8 @@ use spec::*; use engine::*; use evm::Schedule; use evm::Factory; +#[cfg(test)] +use tests::helpers::*; /// Engine using Ethash proof-of-work consensus algorithm, suitable for Ethereum /// mainnet chains in the Olympic, Frontier and Homestead eras. @@ -227,10 +229,11 @@ fn on_close_block() { use super::*; let engine = new_morden().to_engine().unwrap(); let genesis_header = engine.spec().genesis_header(); - let mut db = JournalDB::new_temp(); - engine.spec().ensure_db_good(&mut db); + let mut db_result = get_temp_journal_db(); + let mut db = db_result.reference_mut(); + engine.spec().ensure_db_good(db); let last_hashes = vec![genesis_header.hash()]; - let b = OpenBlock::new(engine.deref(), db, &genesis_header, &last_hashes, Address::zero(), vec![]); + let b = OpenBlock::new(engine.deref(), db.clone(), &genesis_header, &last_hashes, Address::zero(), vec![]); let b = b.close(); assert_eq!(b.state().balance(&Address::zero()), U256::from_str("4563918244f40000").unwrap()); } @@ -240,10 +243,11 @@ fn on_close_block_with_uncle() { use super::*; let engine = new_morden().to_engine().unwrap(); let genesis_header = engine.spec().genesis_header(); - let mut db = JournalDB::new_temp(); - engine.spec().ensure_db_good(&mut db); + let mut db_result = get_temp_journal_db(); + let mut db = db_result.reference_mut(); + engine.spec().ensure_db_good(db); let last_hashes = vec![genesis_header.hash()]; - let mut b = OpenBlock::new(engine.deref(), db, &genesis_header, &last_hashes, Address::zero(), vec![]); + let mut b = OpenBlock::new(engine.deref(), db.clone(), &genesis_header, &last_hashes, Address::zero(), vec![]); let mut uncle = Header::new(); let uncle_author = address_from_hex("ef2d6d194084c2de36e0dabfce45d046b37d1106"); uncle.author = uncle_author.clone(); diff --git a/src/ethereum/mod.rs b/src/ethereum/mod.rs index 66bb7f356..5dd5a8da2 100644 --- a/src/ethereum/mod.rs +++ b/src/ethereum/mod.rs @@ -37,13 +37,15 @@ mod tests { use state::*; use engine::*; use super::*; + use tests::helpers::*; #[test] fn ensure_db_good() { let engine = new_morden().to_engine().unwrap(); let genesis_header = engine.spec().genesis_header(); - let mut db = JournalDB::new_temp(); - engine.spec().ensure_db_good(&mut db); + let mut db_result = get_temp_journal_db(); + let mut db = db_result.reference_mut(); + engine.spec().ensure_db_good(db); let s = State::from_existing(db.clone(), genesis_header.state_root.clone(), engine.account_start_nonce()); assert_eq!(s.balance(&address_from_hex("0000000000000000000000000000000000000001")), U256::from(1u64)); assert_eq!(s.balance(&address_from_hex("0000000000000000000000000000000000000002")), U256::from(1u64)); diff --git a/src/executive.rs b/src/executive.rs index b113363fd..5e1689830 100644 --- a/src/executive.rs +++ b/src/executive.rs @@ -341,12 +341,12 @@ impl<'a> Executive<'a> { mod tests { use super::*; use common::*; - use state::*; use ethereum; use engine::*; use spec::*; use evm::{Schedule, Factory, VMType}; use substate::*; + use tests::helpers::*; struct TestEngine { factory: Factory, @@ -395,7 +395,8 @@ mod tests { params.gas = U256::from(100_000); params.code = Some("3331600055".from_hex().unwrap()); params.value = ActionValue::Transfer(U256::from(0x7)); - let mut state = State::new_temp(); + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); state.add_balance(&sender, &U256::from(0x100u64)); let info = EnvInfo::new(); let engine = TestEngine::new(0, factory); @@ -453,7 +454,8 @@ mod tests { params.gas = U256::from(100_000); params.code = Some(code.clone()); params.value = ActionValue::Transfer(U256::from(100)); - let mut state = State::new_temp(); + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); state.add_balance(&sender, &U256::from(100)); let info = EnvInfo::new(); let engine = TestEngine::new(0, factory); @@ -506,7 +508,8 @@ mod tests { params.gas = U256::from(100_000); params.code = Some(code.clone()); params.value = ActionValue::Transfer(U256::from(100)); - let mut state = State::new_temp(); + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); state.add_balance(&sender, &U256::from(100)); let info = EnvInfo::new(); let engine = TestEngine::new(0, factory); @@ -557,7 +560,8 @@ mod tests { params.gas = U256::from(100_000); params.code = Some(code.clone()); params.value = ActionValue::Transfer(U256::from(100)); - let mut state = State::new_temp(); + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); state.add_balance(&sender, &U256::from(100)); let info = EnvInfo::new(); let engine = TestEngine::new(1024, factory); @@ -613,7 +617,8 @@ mod tests { params.code = Some(code_a.clone()); params.value = ActionValue::Transfer(U256::from(100_000)); - let mut state = State::new_temp(); + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); state.init_code(&address_a, code_a.clone()); state.init_code(&address_b, code_b.clone()); state.add_balance(&sender, &U256::from(100_000)); @@ -659,7 +664,8 @@ mod tests { params.address = address.clone(); params.gas = U256::from(100_000); params.code = Some(code.clone()); - let mut state = State::new_temp(); + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); state.init_code(&address, code.clone()); let info = EnvInfo::new(); let engine = TestEngine::new(0, factory); @@ -684,8 +690,9 @@ mod tests { let sender = t.sender().unwrap(); let contract = contract_address(&sender, &U256::zero()); - let mut state = State::new_temp(); - state.add_balance(&sender, &U256::from(18)); + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); + state.add_balance(&sender, &U256::from(18)); let mut info = EnvInfo::new(); info.gas_limit = U256::from(100_000); let engine = TestEngine::new(0, factory); @@ -711,7 +718,8 @@ mod tests { fn test_transact_invalid_sender(factory: Factory) { let t = Transaction::new_create(U256::from(17), "3331600055".from_hex().unwrap(), U256::from(100_000), U256::zero(), U256::zero()); - let mut state = State::new_temp(); + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); let mut info = EnvInfo::new(); info.gas_limit = U256::from(100_000); let engine = TestEngine::new(0, factory); @@ -734,8 +742,9 @@ mod tests { t.sign(&keypair.secret()); let sender = t.sender().unwrap(); - let mut state = State::new_temp(); - state.add_balance(&sender, &U256::from(17)); + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); + state.add_balance(&sender, &U256::from(17)); let mut info = EnvInfo::new(); info.gas_limit = U256::from(100_000); let engine = TestEngine::new(0, factory); @@ -759,8 +768,9 @@ mod tests { t.sign(&keypair.secret()); let sender = t.sender().unwrap(); - let mut state = State::new_temp(); - state.add_balance(&sender, &U256::from(17)); + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); + state.add_balance(&sender, &U256::from(17)); let mut info = EnvInfo::new(); info.gas_used = U256::from(20_000); info.gas_limit = U256::from(100_000); @@ -785,8 +795,9 @@ mod tests { t.sign(&keypair.secret()); let sender = t.sender().unwrap(); - let mut state = State::new_temp(); - state.add_balance(&sender, &U256::from(100_017)); + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); + state.add_balance(&sender, &U256::from(100_017)); let mut info = EnvInfo::new(); info.gas_limit = U256::from(100_000); let engine = TestEngine::new(0, factory); @@ -818,7 +829,8 @@ mod tests { params.gas = U256::from(0x0186a0); params.code = Some(code.clone()); params.value = ActionValue::Transfer(U256::from_str("0de0b6b3a7640000").unwrap()); - let mut state = State::new_temp(); + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); state.add_balance(&sender, &U256::from_str("152d02c7e14af6800000").unwrap()); let info = EnvInfo::new(); let engine = TestEngine::new(0, factory); diff --git a/src/state.rs b/src/state.rs index 7310f63a7..c1b7e71c6 100644 --- a/src/state.rs +++ b/src/state.rs @@ -50,11 +50,6 @@ impl State { } } - /// Create temporary state object - pub fn new_temp() -> State { - Self::new(JournalDB::new_temp(), U256::from(0u8)) - } - /// Destroy the current object and return root and database. pub fn drop(self) -> (H256, JournalDB) { (self.root, self.db) @@ -285,158 +280,169 @@ use util::trie::*; use util::rlp::*; use util::uint::*; use account::*; +use tests::helpers::*; #[test] fn code_from_database() { let a = Address::zero(); - let (r, db) = { - let mut s = State::new_temp(); - s.require_or_from(&a, false, ||Account::new_contract(U256::from(42u32)), |_|{}); - s.init_code(&a, vec![1, 2, 3]); - assert_eq!(s.code(&a), Some([1u8, 2, 3].to_vec())); - s.commit(); - assert_eq!(s.code(&a), Some([1u8, 2, 3].to_vec())); - s.drop() + let temp = RandomTempPath::new(); + let (root, db) = { + let mut state = get_temp_state_in(temp.as_path()); + state.require_or_from(&a, false, ||Account::new_contract(U256::from(42u32)), |_|{}); + state.init_code(&a, vec![1, 2, 3]); + assert_eq!(state.code(&a), Some([1u8, 2, 3].to_vec())); + state.commit(); + assert_eq!(state.code(&a), Some([1u8, 2, 3].to_vec())); + state.drop() }; - let s = State::from_existing(db, r, U256::from(0u8)); - assert_eq!(s.code(&a), Some([1u8, 2, 3].to_vec())); + let state = State::from_existing(db, root, U256::from(0u8)); + assert_eq!(state.code(&a), Some([1u8, 2, 3].to_vec())); } #[test] fn storage_at_from_database() { let a = Address::zero(); - let (r, db) = { - let mut s = State::new_temp(); - s.set_storage(&a, H256::from(&U256::from(01u64)), H256::from(&U256::from(69u64))); - s.commit(); - s.drop() + let temp = RandomTempPath::new(); + let (root, db) = { + let mut state = get_temp_state_in(temp.as_path()); + state.set_storage(&a, H256::from(&U256::from(01u64)), H256::from(&U256::from(69u64))); + state.commit(); + state.drop() }; - let s = State::from_existing(db, r, U256::from(0u8)); + let s = State::from_existing(db, root, U256::from(0u8)); assert_eq!(s.storage_at(&a, &H256::from(&U256::from(01u64))), H256::from(&U256::from(69u64))); } #[test] fn get_from_database() { let a = Address::zero(); - let (r, db) = { - let mut s = State::new_temp(); - s.inc_nonce(&a); - s.add_balance(&a, &U256::from(69u64)); - s.commit(); - assert_eq!(s.balance(&a), U256::from(69u64)); - s.drop() + let temp = RandomTempPath::new(); + let (root, db) = { + let mut state = get_temp_state_in(temp.as_path()); + state.inc_nonce(&a); + state.add_balance(&a, &U256::from(69u64)); + state.commit(); + assert_eq!(state.balance(&a), U256::from(69u64)); + state.drop() }; - let s = State::from_existing(db, r, U256::from(0u8)); - assert_eq!(s.balance(&a), U256::from(69u64)); - assert_eq!(s.nonce(&a), U256::from(1u64)); + let state = State::from_existing(db, root, U256::from(0u8)); + assert_eq!(state.balance(&a), U256::from(69u64)); + assert_eq!(state.nonce(&a), U256::from(1u64)); } #[test] fn remove() { let a = Address::zero(); - let mut s = State::new_temp(); - assert_eq!(s.exists(&a), false); - s.inc_nonce(&a); - assert_eq!(s.exists(&a), true); - assert_eq!(s.nonce(&a), U256::from(1u64)); - s.kill_account(&a); - assert_eq!(s.exists(&a), false); - assert_eq!(s.nonce(&a), U256::from(0u64)); + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); + assert_eq!(state.exists(&a), false); + state.inc_nonce(&a); + assert_eq!(state.exists(&a), true); + assert_eq!(state.nonce(&a), U256::from(1u64)); + state.kill_account(&a); + assert_eq!(state.exists(&a), false); + assert_eq!(state.nonce(&a), U256::from(0u64)); } #[test] fn remove_from_database() { let a = Address::zero(); - let (r, db) = { - let mut s = State::new_temp(); - s.inc_nonce(&a); - s.commit(); - assert_eq!(s.exists(&a), true); - assert_eq!(s.nonce(&a), U256::from(1u64)); - s.drop() + let temp = RandomTempPath::new(); + { + let mut state = get_temp_state_in(temp.as_path()); + state.inc_nonce(&a); + state.commit(); + assert_eq!(state.exists(&a), true); + assert_eq!(state.nonce(&a), U256::from(1u64)); + state.drop() }; - let (r, db) = { - let mut s = State::from_existing(db, r, U256::from(0u8)); - assert_eq!(s.exists(&a), true); - assert_eq!(s.nonce(&a), U256::from(1u64)); - s.kill_account(&a); - s.commit(); - assert_eq!(s.exists(&a), false); - assert_eq!(s.nonce(&a), U256::from(0u64)); - s.drop() + let (root, db) = { + let mut state = get_temp_state_in(temp.as_path()); + assert_eq!(state.exists(&a), true); + assert_eq!(state.nonce(&a), U256::from(1u64)); + state.kill_account(&a); + state.commit(); + assert_eq!(state.exists(&a), false); + assert_eq!(state.nonce(&a), U256::from(0u64)); + state.drop() }; - let s = State::from_existing(db, r, U256::from(0u8)); - assert_eq!(s.exists(&a), false); - assert_eq!(s.nonce(&a), U256::from(0u64)); + let state = State::from_existing(db, root, U256::from(0u8)); + assert_eq!(state.exists(&a), false); + assert_eq!(state.nonce(&a), U256::from(0u64)); } #[test] fn alter_balance() { - let mut s = State::new_temp(); + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); let a = Address::zero(); let b = address_from_u64(1u64); - s.add_balance(&a, &U256::from(69u64)); - assert_eq!(s.balance(&a), U256::from(69u64)); - s.commit(); - assert_eq!(s.balance(&a), U256::from(69u64)); - s.sub_balance(&a, &U256::from(42u64)); - assert_eq!(s.balance(&a), U256::from(27u64)); - s.commit(); - assert_eq!(s.balance(&a), U256::from(27u64)); - s.transfer_balance(&a, &b, &U256::from(18u64)); - assert_eq!(s.balance(&a), U256::from(9u64)); - assert_eq!(s.balance(&b), U256::from(18u64)); - s.commit(); - assert_eq!(s.balance(&a), U256::from(9u64)); - assert_eq!(s.balance(&b), U256::from(18u64)); + state.add_balance(&a, &U256::from(69u64)); + assert_eq!(state.balance(&a), U256::from(69u64)); + state.commit(); + assert_eq!(state.balance(&a), U256::from(69u64)); + state.sub_balance(&a, &U256::from(42u64)); + assert_eq!(state.balance(&a), U256::from(27u64)); + state.commit(); + assert_eq!(state.balance(&a), U256::from(27u64)); + state.transfer_balance(&a, &b, &U256::from(18u64)); + assert_eq!(state.balance(&a), U256::from(9u64)); + assert_eq!(state.balance(&b), U256::from(18u64)); + state.commit(); + assert_eq!(state.balance(&a), U256::from(9u64)); + assert_eq!(state.balance(&b), U256::from(18u64)); } #[test] fn alter_nonce() { - let mut s = State::new_temp(); + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); let a = Address::zero(); - s.inc_nonce(&a); - assert_eq!(s.nonce(&a), U256::from(1u64)); - s.inc_nonce(&a); - assert_eq!(s.nonce(&a), U256::from(2u64)); - s.commit(); - assert_eq!(s.nonce(&a), U256::from(2u64)); - s.inc_nonce(&a); - assert_eq!(s.nonce(&a), U256::from(3u64)); - s.commit(); - assert_eq!(s.nonce(&a), U256::from(3u64)); + state.inc_nonce(&a); + assert_eq!(state.nonce(&a), U256::from(1u64)); + state.inc_nonce(&a); + assert_eq!(state.nonce(&a), U256::from(2u64)); + state.commit(); + assert_eq!(state.nonce(&a), U256::from(2u64)); + state.inc_nonce(&a); + assert_eq!(state.nonce(&a), U256::from(3u64)); + state.commit(); + assert_eq!(state.nonce(&a), U256::from(3u64)); } #[test] fn balance_nonce() { - let mut s = State::new_temp(); + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); let a = Address::zero(); - assert_eq!(s.balance(&a), U256::from(0u64)); - assert_eq!(s.nonce(&a), U256::from(0u64)); - s.commit(); - assert_eq!(s.balance(&a), U256::from(0u64)); - assert_eq!(s.nonce(&a), U256::from(0u64)); + assert_eq!(state.balance(&a), U256::from(0u64)); + assert_eq!(state.nonce(&a), U256::from(0u64)); + state.commit(); + assert_eq!(state.balance(&a), U256::from(0u64)); + assert_eq!(state.nonce(&a), U256::from(0u64)); } #[test] fn ensure_cached() { - let mut s = State::new_temp(); + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); let a = Address::zero(); - s.require(&a, false); - s.commit(); - assert_eq!(s.root().hex(), "0ce23f3c809de377b008a4a3ee94a0834aac8bec1f86e28ffe4fdb5a15b0c785"); + state.require(&a, false); + state.commit(); + assert_eq!(state.root().hex(), "0ce23f3c809de377b008a4a3ee94a0834aac8bec1f86e28ffe4fdb5a15b0c785"); } #[test] fn create_empty() { - let mut s = State::new_temp(); - s.commit(); - assert_eq!(s.root().hex(), "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"); + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); + state.commit(); + assert_eq!(state.root().hex(), "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"); } } diff --git a/src/substate.rs b/src/substate.rs index d2aca983e..e14ac4d20 100644 --- a/src/substate.rs +++ b/src/substate.rs @@ -37,11 +37,15 @@ impl Substate { #[cfg(test)] mod tests { use super::*; + use util::hash::*; #[test] fn accrue() { let mut sub_state = Substate::new(); - let sub_state_2 = Substate::new(); + sub_state.contracts_created.push(address_from_u64(1u64)); + let mut sub_state_2 = Substate::new(); + sub_state_2.contracts_created.push(address_from_u64(2u64)); sub_state.accrue(sub_state_2); + assert_eq!(sub_state.contracts_created.len(), 2); } } \ No newline at end of file diff --git a/src/tests/executive.rs b/src/tests/executive.rs index 1df1b7eec..f097a17ad 100644 --- a/src/tests/executive.rs +++ b/src/tests/executive.rs @@ -8,6 +8,7 @@ use evm::{Schedule, Ext, Factory, VMType, ContractCreateResult, MessageCallResul use ethereum; use externalities::*; use substate::*; +use tests::helpers::*; struct TestEngine { vm_factory: Factory, @@ -174,7 +175,8 @@ fn do_json_test_for(vm: &VMType, json_data: &[u8]) -> Vec { }; // test env - let mut state = State::new_temp(); + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); test.find("pre").map(|pre| for (addr, s) in pre.as_object().unwrap() { let address = Address::from(addr.as_ref()); diff --git a/src/tests/helpers.rs b/src/tests/helpers.rs index de73224fd..d2423f95c 100644 --- a/src/tests/helpers.rs +++ b/src/tests/helpers.rs @@ -5,7 +5,8 @@ use std::path::PathBuf; use spec::*; use std::fs::{remove_dir_all}; use blockchain::{BlockChain}; - +use state::*; +use rocksdb::*; pub struct RandomTempPath { path: PathBuf @@ -23,6 +24,10 @@ impl RandomTempPath { pub fn as_path(&self) -> &PathBuf { &self.path } + + pub fn as_str(&self) -> &str { + self.path.to_str().unwrap() + } } impl Drop for RandomTempPath { @@ -43,6 +48,10 @@ impl GuardedTempResult { pub fn reference(&self) -> &T { &self.result } + + pub fn reference_mut(&mut self) -> &mut T { + &mut self.result + } } pub fn get_test_spec() -> Spec { @@ -189,6 +198,35 @@ pub fn generate_dummy_empty_blockchain() -> GuardedTempResult { } } +pub fn get_temp_journal_db() -> GuardedTempResult { + let temp = RandomTempPath::new(); + let db = DB::open_default(temp.as_str()).unwrap(); + let journal_db = JournalDB::new(db); + GuardedTempResult { + temp: temp, + result: journal_db + } +} + +pub fn get_temp_state() -> GuardedTempResult { + let temp = RandomTempPath::new(); + let journal_db = get_temp_journal_db_in(temp.as_path()); + GuardedTempResult { + temp: temp, + result: State::new(journal_db, U256::from(0u8)) + } +} + +pub fn get_temp_journal_db_in(path: &Path) -> JournalDB { + let db = DB::open_default(path.to_str().unwrap()).unwrap(); + JournalDB::new(db) +} + +pub fn get_temp_state_in(path: &Path) -> State { + let journal_db = get_temp_journal_db_in(path); + State::new(journal_db, U256::from(0u8)) +} + pub fn get_good_dummy_block() -> Bytes { let mut block_header = Header::new(); let test_spec = get_test_spec(); diff --git a/src/tests/state.rs b/src/tests/state.rs index 325a8b646..1dbe27c9c 100644 --- a/src/tests/state.rs +++ b/src/tests/state.rs @@ -1,8 +1,8 @@ use super::test_common::*; -use state::*; use pod_state::*; use state_diff::*; use ethereum; +use tests::helpers::*; fn do_json_test(json_data: &[u8]) -> Vec { let json = Json::from_str(::std::str::from_utf8(json_data).unwrap()).expect("Json is invalid"); @@ -39,14 +39,15 @@ fn do_json_test(json_data: &[u8]) -> Vec { println!("!!! {}: Trie root mismatch (got: {}, expect: {}):", name, calc_post, post_state_root); println!("!!! Post:\n{}", post); } else { - let mut s = State::new_temp(); - s.populate_from(pre); - s.commit(); - let res = s.apply(&env, engine.deref(), &t); + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); + state.populate_from(pre); + state.commit(); + let res = state.apply(&env, engine.deref(), &t); - if fail_unless(s.root() == &post_state_root) { - println!("!!! {}: State mismatch (got: {}, expect: {}):", name, s.root(), post_state_root); - let our_post = s.to_pod(); + if fail_unless(state.root() == &post_state_root) { + println!("!!! {}: State mismatch (got: {}, expect: {}):", name, state.root(), post_state_root); + let our_post = state.to_pod(); println!("Got:\n{}", our_post); println!("Expect:\n{}", post); println!("Diff ---expect -> +++got:\n{}", StateDiff::diff_pod(&post, &our_post)); diff --git a/util/src/journaldb.rs b/util/src/journaldb.rs index ba31efafb..858455072 100644 --- a/util/src/journaldb.rs +++ b/util/src/journaldb.rs @@ -19,7 +19,7 @@ pub struct JournalDB { forward: OverlayDB, backing: Arc, inserts: Vec, - removes: Vec, + removes: Vec } impl JournalDB { @@ -45,6 +45,7 @@ impl JournalDB { } /// Create a new instance with an anonymous temporary database. + #[cfg(test)] pub fn new_temp() -> JournalDB { let mut dir = env::temp_dir(); dir.push(H32::random().hex()); @@ -137,6 +138,7 @@ mod tests { use common::*; use super::*; use hashdb::*; + use tests::helpers::*; #[test] fn long_history() { @@ -221,4 +223,11 @@ mod tests { assert!(!jdb.exists(&baz)); assert!(!jdb.exists(&bar)); } + + #[test] + fn old_commits_applied() { + let mut dir = env::temp_dir(); + dir.push(H32::random().hex()); + + } }