diff --git a/.gitmodules b/.gitmodules index d134318b4..95ac098ec 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,4 +1,4 @@ [submodule "ethcore/res/ethereum/tests"] path = ethcore/res/ethereum/tests url = git@github.com:ethereum/tests - branch = develop + branch = develop \ No newline at end of file diff --git a/ethcore/src/block.rs b/ethcore/src/block.rs index 963124be0..f8e8ef16f 100644 --- a/ethcore/src/block.rs +++ b/ethcore/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/ethcore/src/env_info.rs b/ethcore/src/env_info.rs index 76d33cd5a..8b037036e 100644 --- a/ethcore/src/env_info.rs +++ b/ethcore/src/env_info.rs @@ -59,3 +59,42 @@ impl FromJson for EnvInfo { } } } + +#[cfg(test)] +mod tests { + extern crate rustc_serialize; + + use super::*; + use rustc_serialize::*; + use util::from_json::FromJson; + use util::hash::*; + use std::str::FromStr; + + #[test] + fn it_serializes_form_json() { + let env_info = EnvInfo::from_json(&json::Json::from_str( +r#" + { + "currentCoinbase": "0x000000f00000000f000000000000f00000000f00", + "currentNumber": 1112339, + "currentDifficulty": 50000, + "currentGasLimit" : 40000, + "currentTimestamp" : 1100 + } +"# + ).unwrap()); + + assert_eq!(env_info.number, 1112339); + assert_eq!(env_info.author, Address::from_str("000000f00000000f000000000000f00000000f00").unwrap()); + assert_eq!(env_info.gas_limit, x!(40000)); + assert_eq!(env_info.difficulty, x!(50000)); + assert_eq!(env_info.gas_used, x!(0)); + } + + #[test] + fn it_can_be_created_as_default() { + let default_env_info = EnvInfo::default(); + + assert_eq!(default_env_info.difficulty, x!(0)); + } +} \ No newline at end of file diff --git a/ethcore/src/ethereum/ethash.rs b/ethcore/src/ethereum/ethash.rs index f28ea31c6..ce40c5f42 100644 --- a/ethcore/src/ethereum/ethash.rs +++ b/ethcore/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/ethcore/src/ethereum/mod.rs b/ethcore/src/ethereum/mod.rs index caab556de..c32fc40c6 100644 --- a/ethcore/src/ethereum/mod.rs +++ b/ethcore/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/ethcore/src/executive.rs b/ethcore/src/executive.rs index 039a6007e..d824ccc2c 100644 --- a/ethcore/src/executive.rs +++ b/ethcore/src/executive.rs @@ -342,12 +342,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, @@ -396,7 +396,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); @@ -454,7 +455,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); @@ -507,7 +509,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); @@ -558,7 +561,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); @@ -614,7 +618,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)); @@ -660,7 +665,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); @@ -685,8 +691,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); @@ -712,7 +719,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); @@ -735,8 +743,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); @@ -760,8 +769,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); @@ -786,8 +796,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); @@ -819,7 +830,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/ethcore/src/pod_state.rs b/ethcore/src/pod_state.rs index 15ae6a8ae..f37a327f8 100644 --- a/ethcore/src/pod_state.rs +++ b/ethcore/src/pod_state.rs @@ -53,3 +53,31 @@ impl fmt::Display for PodState { Ok(()) } } + +#[cfg(test)] +mod tests { + extern crate rustc_serialize; + + use super::*; + use rustc_serialize::*; + use util::from_json::FromJson; + use util::hash::*; + + #[test] + fn it_serializes_form_json() { + let pod_state = PodState::from_json(&json::Json::from_str( +r#" + { + "0000000000000000000000000000000000000000": { + "balance": "1000", + "nonce": "100", + "storage": {}, + "code" : [] + } + } +"# + ).unwrap()); + + assert!(pod_state.get().get(&ZERO_ADDRESS).is_some()); + } +} diff --git a/ethcore/src/receipt.rs b/ethcore/src/receipt.rs index b3379c148..b12cbab23 100644 --- a/ethcore/src/receipt.rs +++ b/ethcore/src/receipt.rs @@ -37,6 +37,7 @@ impl Encodable for Receipt { } } + #[test] fn test_basic() { let expected = FromHex::from_hex("f90162a02f697d671e9ae4ee24a43c4b0d7e15f1cb4ba6de1561120d43b9a4e8c4a8a6ee83040caeb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000f838f794dcf421d093428b096ca501a7cd1a740855a7976fc0a00000000000000000000000000000000000000000000000000000000000000000").unwrap(); @@ -50,4 +51,4 @@ fn test_basic() { )] ); assert_eq!(&encode(&r)[..], &expected[..]); -} \ No newline at end of file +} diff --git a/ethcore/src/service.rs b/ethcore/src/service.rs index a56ed7f44..f24758abb 100644 --- a/ethcore/src/service.rs +++ b/ethcore/src/service.rs @@ -56,7 +56,6 @@ impl ClientService { /// Get client interface pub fn client(&self) -> Arc { self.client.clone() - } /// Get network service component @@ -98,3 +97,16 @@ impl IoHandler for ClientIoHandler { } } +#[cfg(test)] +mod tests { + use super::*; + use tests::helpers::*; + use util::network::*; + + #[test] + fn it_can_be_started() { + let spec = get_test_spec(); + let service = ClientService::start(spec, NetworkConfiguration::new()); + assert!(service.is_ok()); + } +} \ No newline at end of file diff --git a/ethcore/src/state.rs b/ethcore/src/state.rs index 9b44d7194..58cdf85a2 100644 --- a/ethcore/src/state.rs +++ b/ethcore/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 (root, db) = { + 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 = State::from_existing(db, root, U256::from(0u8)); + 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/ethcore/src/substate.rs b/ethcore/src/substate.rs index 5c4cde60c..73ba267cd 100644 --- a/ethcore/src/substate.rs +++ b/ethcore/src/substate.rs @@ -33,3 +33,28 @@ impl Substate { self.contracts_created.extend(s.contracts_created.into_iter()); } } + +#[cfg(test)] +mod tests { + use super::*; + use common::*; + + #[test] + fn accrue() { + let mut sub_state = Substate::new(); + sub_state.contracts_created.push(address_from_u64(1u64)); + sub_state.logs.push(LogEntry::new(address_from_u64(1u64), vec![], vec![])); + sub_state.sstore_clears_count = x!(5); + sub_state.suicides.insert(address_from_u64(10u64)); + + let mut sub_state_2 = Substate::new(); + sub_state_2.contracts_created.push(address_from_u64(2u64)); + sub_state_2.logs.push(LogEntry::new(address_from_u64(1u64), vec![], vec![])); + sub_state_2.sstore_clears_count = x!(7); + + sub_state.accrue(sub_state_2); + assert_eq!(sub_state.contracts_created.len(), 2); + assert_eq!(sub_state.sstore_clears_count, x!(12)); + assert_eq!(sub_state.suicides.len(), 1); + } +} \ No newline at end of file diff --git a/ethcore/src/tests/executive.rs b/ethcore/src/tests/executive.rs index ab5cdaf98..4a56007ad 100644 --- a/ethcore/src/tests/executive.rs +++ b/ethcore/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/ethcore/src/tests/helpers.rs b/ethcore/src/tests/helpers.rs index 7f24b6c6f..1923e9164 100644 --- a/ethcore/src/tests/helpers.rs +++ b/ethcore/src/tests/helpers.rs @@ -5,6 +5,8 @@ use std::path::PathBuf; use spec::*; use std::fs::{remove_dir_all}; use blockchain::{BlockChain}; +use state::*; +use rocksdb::*; pub enum ChainEra { Frontier, @@ -27,6 +29,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 { @@ -47,6 +53,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 { @@ -193,6 +203,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/ethcore/src/tests/state.rs b/ethcore/src/tests/state.rs index 947782e8f..34b24de3b 100644 --- a/ethcore/src/tests/state.rs +++ b/ethcore/src/tests/state.rs @@ -1,6 +1,5 @@ use super::test_common::*; use super::helpers::*; -use state::*; use pod_state::*; use state_diff::*; use ethereum; @@ -50,14 +49,15 @@ pub fn json_chain_test(json_data: &[u8], era: ChainEra) -> 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..e30b8947c 100644 --- a/util/src/journaldb.rs +++ b/util/src/journaldb.rs @@ -1,11 +1,12 @@ //! Disk-backed HashDB implementation. -use std::env; use common::*; use rlp::*; use hashdb::*; use overlaydb::*; use rocksdb::{DB, Writable}; +#[cfg(test)] +use std::env; #[derive(Clone)] /// Implementation of the HashDB trait for a disk-backed database with a memory overlay @@ -45,6 +46,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());