Use in-memory database in tests

This commit is contained in:
arkpar 2017-04-06 19:26:17 +02:00
parent 1e6674f804
commit 314af4cdae
19 changed files with 130 additions and 276 deletions

View File

@ -667,8 +667,7 @@ mod tests {
use spec::*; use spec::*;
let spec = Spec::new_test(); let spec = Spec::new_test();
let genesis_header = spec.genesis_header(); let genesis_header = spec.genesis_header();
let mut db_result = get_temp_state_db(); let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
let db = spec.ensure_db_good(db_result.take(), &Default::default()).unwrap();
let last_hashes = Arc::new(vec![genesis_header.hash()]); let last_hashes = Arc::new(vec![genesis_header.hash()]);
let b = OpenBlock::new(&*spec.engine, Default::default(), false, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![]).unwrap(); let b = OpenBlock::new(&*spec.engine, Default::default(), false, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![]).unwrap();
let b = b.close_and_lock(); let b = b.close_and_lock();
@ -682,16 +681,14 @@ mod tests {
let engine = &*spec.engine; let engine = &*spec.engine;
let genesis_header = spec.genesis_header(); let genesis_header = spec.genesis_header();
let mut db_result = get_temp_state_db(); let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
let db = spec.ensure_db_good(db_result.take(), &Default::default()).unwrap();
let last_hashes = Arc::new(vec![genesis_header.hash()]); let last_hashes = Arc::new(vec![genesis_header.hash()]);
let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes.clone(), Address::zero(), (3141562.into(), 31415620.into()), vec![]).unwrap() let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes.clone(), Address::zero(), (3141562.into(), 31415620.into()), vec![]).unwrap()
.close_and_lock().seal(engine, vec![]).unwrap(); .close_and_lock().seal(engine, vec![]).unwrap();
let orig_bytes = b.rlp_bytes(); let orig_bytes = b.rlp_bytes();
let orig_db = b.drain(); let orig_db = b.drain();
let mut db_result = get_temp_state_db(); let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
let db = spec.ensure_db_good(db_result.take(), &Default::default()).unwrap();
let e = enact_and_seal(&orig_bytes, engine, false, db, &genesis_header, last_hashes, Default::default()).unwrap(); let e = enact_and_seal(&orig_bytes, engine, false, db, &genesis_header, last_hashes, Default::default()).unwrap();
assert_eq!(e.rlp_bytes(), orig_bytes); assert_eq!(e.rlp_bytes(), orig_bytes);
@ -708,8 +705,7 @@ mod tests {
let engine = &*spec.engine; let engine = &*spec.engine;
let genesis_header = spec.genesis_header(); let genesis_header = spec.genesis_header();
let mut db_result = get_temp_state_db(); let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
let db = spec.ensure_db_good(db_result.take(), &Default::default()).unwrap();
let last_hashes = Arc::new(vec![genesis_header.hash()]); let last_hashes = Arc::new(vec![genesis_header.hash()]);
let mut open_block = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes.clone(), Address::zero(), (3141562.into(), 31415620.into()), vec![]).unwrap(); let mut open_block = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes.clone(), Address::zero(), (3141562.into(), 31415620.into()), vec![]).unwrap();
let mut uncle1_header = Header::new(); let mut uncle1_header = Header::new();
@ -723,8 +719,7 @@ mod tests {
let orig_bytes = b.rlp_bytes(); let orig_bytes = b.rlp_bytes();
let orig_db = b.drain(); let orig_db = b.drain();
let mut db_result = get_temp_state_db(); let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
let db = spec.ensure_db_good(db_result.take(), &Default::default()).unwrap();
let e = enact_and_seal(&orig_bytes, engine, false, db, &genesis_header, last_hashes, Default::default()).unwrap(); let e = enact_and_seal(&orig_bytes, engine, false, db, &genesis_header, last_hashes, Default::default()).unwrap();
let bytes = e.rlp_bytes(); let bytes = e.rlp_bytes();

View File

@ -1791,15 +1791,13 @@ mod tests {
#[test] #[test]
fn can_contain_arbitrary_block_sequence() { fn can_contain_arbitrary_block_sequence() {
let bc_result = generate_dummy_blockchain(50); let bc = generate_dummy_blockchain(50);
let bc = bc_result.reference();
assert_eq!(bc.best_block_number(), 49); assert_eq!(bc.best_block_number(), 49);
} }
#[test] #[test]
fn can_collect_garbage() { fn can_collect_garbage() {
let bc_result = generate_dummy_blockchain(3000); let bc = generate_dummy_blockchain(3000);
let bc = bc_result.reference();
assert_eq!(bc.best_block_number(), 2999); assert_eq!(bc.best_block_number(), 2999);
let best_hash = bc.best_block_hash(); let best_hash = bc.best_block_hash();
@ -1818,15 +1816,13 @@ mod tests {
#[test] #[test]
fn can_contain_arbitrary_block_sequence_with_extra() { fn can_contain_arbitrary_block_sequence_with_extra() {
let bc_result = generate_dummy_blockchain_with_extra(25); let bc = generate_dummy_blockchain_with_extra(25);
let bc = bc_result.reference();
assert_eq!(bc.best_block_number(), 24); assert_eq!(bc.best_block_number(), 24);
} }
#[test] #[test]
fn can_contain_only_genesis_block() { fn can_contain_only_genesis_block() {
let bc_result = generate_dummy_empty_blockchain(); let bc = generate_dummy_empty_blockchain();
let bc = bc_result.reference();
assert_eq!(bc.best_block_number(), 0); assert_eq!(bc.best_block_number(), 0);
} }

View File

@ -1717,7 +1717,7 @@ mod tests {
// Separate thread uncommited transaction // Separate thread uncommited transaction
let go = Arc::new(AtomicBool::new(false)); let go = Arc::new(AtomicBool::new(false));
let go_thread = go.clone(); let go_thread = go.clone();
let another_client = client.reference().clone(); let another_client = client.clone();
thread::spawn(move || { thread::spawn(move || {
let mut batch = DBTransaction::new(); let mut batch = DBTransaction::new();
another_client.chain.read().insert_block(&mut batch, &new_block, Vec::new()); another_client.chain.read().insert_block(&mut batch, &new_block, Vec::new());

View File

@ -454,8 +454,8 @@ mod tests {
let spec = Spec::new_test_round(); let spec = Spec::new_test_round();
let engine = &*spec.engine; let engine = &*spec.engine;
let genesis_header = spec.genesis_header(); let genesis_header = spec.genesis_header();
let db1 = spec.ensure_db_good(get_temp_state_db().take(), &Default::default()).unwrap(); let db1 = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
let db2 = spec.ensure_db_good(get_temp_state_db().take(), &Default::default()).unwrap(); let db2 = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
let last_hashes = Arc::new(vec![genesis_header.hash()]); let last_hashes = Arc::new(vec![genesis_header.hash()]);
let b1 = OpenBlock::new(engine, Default::default(), false, db1, &genesis_header, last_hashes.clone(), addr1, (3141562.into(), 31415620.into()), vec![]).unwrap(); let b1 = OpenBlock::new(engine, Default::default(), false, db1, &genesis_header, last_hashes.clone(), addr1, (3141562.into(), 31415620.into()), vec![]).unwrap();
let b1 = b1.close_and_lock(); let b1 = b1.close_and_lock();

View File

@ -252,8 +252,7 @@ mod tests {
let engine = &*spec.engine; let engine = &*spec.engine;
engine.set_signer(Arc::new(tap), addr, "".into()); engine.set_signer(Arc::new(tap), addr, "".into());
let genesis_header = spec.genesis_header(); let genesis_header = spec.genesis_header();
let mut db_result = get_temp_state_db(); let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
let db = spec.ensure_db_good(db_result.take(), &Default::default()).unwrap();
let last_hashes = Arc::new(vec![genesis_header.hash()]); let last_hashes = Arc::new(vec![genesis_header.hash()]);
let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, addr, (3141562.into(), 31415620.into()), vec![]).unwrap(); let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, addr, (3141562.into(), 31415620.into()), vec![]).unwrap();
let b = b.close_and_lock(); let b = b.close_and_lock();

View File

@ -82,8 +82,7 @@ mod tests {
fn instant_can_seal() { fn instant_can_seal() {
let spec = Spec::new_instant(); let spec = Spec::new_instant();
let engine = &*spec.engine; let engine = &*spec.engine;
let mut db_result = get_temp_state_db(); let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
let db = spec.ensure_db_good(db_result.take(), &Default::default()).unwrap();
let genesis_header = spec.genesis_header(); let genesis_header = spec.genesis_header();
let last_hashes = Arc::new(vec![genesis_header.hash()]); let last_hashes = Arc::new(vec![genesis_header.hash()]);
let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, Address::default(), (3141562.into(), 31415620.into()), vec![]).unwrap(); let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, Address::default(), (3141562.into(), 31415620.into()), vec![]).unwrap();

View File

@ -676,8 +676,8 @@ mod tests {
} }
fn propose_default(spec: &Spec, proposer: Address) -> (ClosedBlock, Vec<Bytes>) { fn propose_default(spec: &Spec, proposer: Address) -> (ClosedBlock, Vec<Bytes>) {
let mut db_result = get_temp_state_db(); let db = get_temp_state_db();
let db = spec.ensure_db_good(db_result.take(), &Default::default()).unwrap(); let db = spec.ensure_db_good(db, &Default::default()).unwrap();
let genesis_header = spec.genesis_header(); let genesis_header = spec.genesis_header();
let last_hashes = Arc::new(vec![genesis_header.hash()]); let last_hashes = Arc::new(vec![genesis_header.hash()]);
let b = OpenBlock::new(spec.engine.as_ref(), Default::default(), false, db.boxed_clone(), &genesis_header, last_hashes, proposer, (3141562.into(), 31415620.into()), vec![]).unwrap(); let b = OpenBlock::new(spec.engine.as_ref(), Default::default(), false, db.boxed_clone(), &genesis_header, last_hashes, proposer, (3141562.into(), 31415620.into()), vec![]).unwrap();

View File

@ -524,8 +524,7 @@ mod tests {
let spec = new_morden(); let spec = new_morden();
let engine = &*spec.engine; let engine = &*spec.engine;
let genesis_header = spec.genesis_header(); let genesis_header = spec.genesis_header();
let mut db_result = get_temp_state_db(); let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
let db = spec.ensure_db_good(db_result.take(), &Default::default()).unwrap();
let last_hashes = Arc::new(vec![genesis_header.hash()]); let last_hashes = Arc::new(vec![genesis_header.hash()]);
let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![]).unwrap(); let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![]).unwrap();
let b = b.close(); let b = b.close();
@ -537,8 +536,7 @@ mod tests {
let spec = new_morden(); let spec = new_morden();
let engine = &*spec.engine; let engine = &*spec.engine;
let genesis_header = spec.genesis_header(); let genesis_header = spec.genesis_header();
let mut db_result = get_temp_state_db(); let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
let db = spec.ensure_db_good(db_result.take(), &Default::default()).unwrap();
let last_hashes = Arc::new(vec![genesis_header.hash()]); let last_hashes = Arc::new(vec![genesis_header.hash()]);
let mut b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![]).unwrap(); let mut b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![]).unwrap();
let mut uncle = Header::new(); let mut uncle = Header::new();

View File

@ -94,8 +94,7 @@ mod tests {
let spec = new_morden(); let spec = new_morden();
let engine = &spec.engine; let engine = &spec.engine;
let genesis_header = spec.genesis_header(); let genesis_header = spec.genesis_header();
let mut db_result = get_temp_state_db(); let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
let db = spec.ensure_db_good(db_result.take(), &Default::default()).unwrap();
let s = State::from_existing(db, genesis_header.state_root().clone(), engine.account_start_nonce(), Default::default()).unwrap(); let s = State::from_existing(db, genesis_header.state_root().clone(), engine.account_start_nonce(), Default::default()).unwrap();
assert_eq!(s.balance(&"0000000000000000000000000000000000000001".into()).unwrap(), 1u64.into()); assert_eq!(s.balance(&"0000000000000000000000000000000000000001".into()).unwrap(), 1u64.into());
assert_eq!(s.balance(&"0000000000000000000000000000000000000002".into()).unwrap(), 1u64.into()); assert_eq!(s.balance(&"0000000000000000000000000000000000000002".into()).unwrap(), 1u64.into());

View File

@ -554,8 +554,7 @@ mod tests {
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(Arc::new("3331600055".from_hex().unwrap())); params.code = Some(Arc::new("3331600055".from_hex().unwrap()));
params.value = ActionValue::Transfer(U256::from(0x7)); params.value = ActionValue::Transfer(U256::from(0x7));
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
state.add_balance(&sender, &U256::from(0x100u64), CleanupMode::NoEmpty).unwrap(); state.add_balance(&sender, &U256::from(0x100u64), CleanupMode::NoEmpty).unwrap();
let info = EnvInfo::default(); let info = EnvInfo::default();
let engine = TestEngine::new(0); let engine = TestEngine::new(0);
@ -613,8 +612,7 @@ mod tests {
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(Arc::new(code)); params.code = Some(Arc::new(code));
params.value = ActionValue::Transfer(U256::from(100)); params.value = ActionValue::Transfer(U256::from(100));
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
state.add_balance(&sender, &U256::from(100), CleanupMode::NoEmpty).unwrap(); state.add_balance(&sender, &U256::from(100), CleanupMode::NoEmpty).unwrap();
let info = EnvInfo::default(); let info = EnvInfo::default();
let engine = TestEngine::new(0); let engine = TestEngine::new(0);
@ -672,8 +670,7 @@ mod tests {
params.code = Some(Arc::new(code)); params.code = Some(Arc::new(code));
params.value = ActionValue::Transfer(U256::from(100)); params.value = ActionValue::Transfer(U256::from(100));
params.call_type = CallType::Call; params.call_type = CallType::Call;
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
state.add_balance(&sender, &U256::from(100), CleanupMode::NoEmpty).unwrap(); state.add_balance(&sender, &U256::from(100), CleanupMode::NoEmpty).unwrap();
let info = EnvInfo::default(); let info = EnvInfo::default();
let engine = TestEngine::new(5); let engine = TestEngine::new(5);
@ -783,8 +780,7 @@ mod tests {
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(Arc::new(code)); params.code = Some(Arc::new(code));
params.value = ActionValue::Transfer(100.into()); params.value = ActionValue::Transfer(100.into());
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
state.add_balance(&sender, &U256::from(100), CleanupMode::NoEmpty).unwrap(); state.add_balance(&sender, &U256::from(100), CleanupMode::NoEmpty).unwrap();
let info = EnvInfo::default(); let info = EnvInfo::default();
let engine = TestEngine::new(5); let engine = TestEngine::new(5);
@ -871,8 +867,7 @@ mod tests {
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(Arc::new(code)); params.code = Some(Arc::new(code));
params.value = ActionValue::Transfer(U256::from(100)); params.value = ActionValue::Transfer(U256::from(100));
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
state.add_balance(&sender, &U256::from(100), CleanupMode::NoEmpty).unwrap(); state.add_balance(&sender, &U256::from(100), CleanupMode::NoEmpty).unwrap();
let info = EnvInfo::default(); let info = EnvInfo::default();
let engine = TestEngine::new(0); let engine = TestEngine::new(0);
@ -923,8 +918,7 @@ mod tests {
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(Arc::new(code)); params.code = Some(Arc::new(code));
params.value = ActionValue::Transfer(U256::from(100)); params.value = ActionValue::Transfer(U256::from(100));
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
state.add_balance(&sender, &U256::from(100), CleanupMode::NoEmpty).unwrap(); state.add_balance(&sender, &U256::from(100), CleanupMode::NoEmpty).unwrap();
let info = EnvInfo::default(); let info = EnvInfo::default();
let engine = TestEngine::new(1024); let engine = TestEngine::new(1024);
@ -981,8 +975,7 @@ mod tests {
params.code = Some(Arc::new(code_a.clone())); params.code = Some(Arc::new(code_a.clone()));
params.value = ActionValue::Transfer(U256::from(100_000)); params.value = ActionValue::Transfer(U256::from(100_000));
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
state.init_code(&address_a, code_a.clone()).unwrap(); state.init_code(&address_a, code_a.clone()).unwrap();
state.init_code(&address_b, code_b.clone()).unwrap(); state.init_code(&address_b, code_b.clone()).unwrap();
state.add_balance(&sender, &U256::from(100_000), CleanupMode::NoEmpty).unwrap(); state.add_balance(&sender, &U256::from(100_000), CleanupMode::NoEmpty).unwrap();
@ -1029,8 +1022,7 @@ mod tests {
params.address = address.clone(); params.address = address.clone();
params.gas = U256::from(100_000); params.gas = U256::from(100_000);
params.code = Some(Arc::new(code.clone())); params.code = Some(Arc::new(code.clone()));
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
state.init_code(&address, code).unwrap(); state.init_code(&address, code).unwrap();
let info = EnvInfo::default(); let info = EnvInfo::default();
let engine = TestEngine::new(0); let engine = TestEngine::new(0);
@ -1062,8 +1054,7 @@ mod tests {
let sender = t.sender(); let sender = t.sender();
let contract = contract_address(&sender, &U256::zero()); let contract = contract_address(&sender, &U256::zero());
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
state.add_balance(&sender, &U256::from(18), CleanupMode::NoEmpty).unwrap(); state.add_balance(&sender, &U256::from(18), CleanupMode::NoEmpty).unwrap();
let mut info = EnvInfo::default(); let mut info = EnvInfo::default();
info.gas_limit = U256::from(100_000); info.gas_limit = U256::from(100_000);
@ -1100,8 +1091,7 @@ mod tests {
}.sign(keypair.secret(), None); }.sign(keypair.secret(), None);
let sender = t.sender(); let sender = t.sender();
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
state.add_balance(&sender, &U256::from(17), CleanupMode::NoEmpty).unwrap(); state.add_balance(&sender, &U256::from(17), CleanupMode::NoEmpty).unwrap();
let mut info = EnvInfo::default(); let mut info = EnvInfo::default();
info.gas_limit = U256::from(100_000); info.gas_limit = U256::from(100_000);
@ -1133,8 +1123,7 @@ mod tests {
}.sign(keypair.secret(), None); }.sign(keypair.secret(), None);
let sender = t.sender(); let sender = t.sender();
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
state.add_balance(&sender, &U256::from(17), CleanupMode::NoEmpty).unwrap(); state.add_balance(&sender, &U256::from(17), CleanupMode::NoEmpty).unwrap();
let mut info = EnvInfo::default(); let mut info = EnvInfo::default();
info.gas_used = U256::from(20_000); info.gas_used = U256::from(20_000);
@ -1168,8 +1157,7 @@ mod tests {
}.sign(keypair.secret(), None); }.sign(keypair.secret(), None);
let sender = t.sender(); let sender = t.sender();
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
state.add_balance(&sender, &U256::from(100_017), CleanupMode::NoEmpty).unwrap(); state.add_balance(&sender, &U256::from(100_017), CleanupMode::NoEmpty).unwrap();
let mut info = EnvInfo::default(); let mut info = EnvInfo::default();
info.gas_limit = U256::from(100_000); info.gas_limit = U256::from(100_000);
@ -1203,8 +1191,7 @@ mod tests {
params.gas = U256::from(0x0186a0); params.gas = U256::from(0x0186a0);
params.code = Some(Arc::new(code)); params.code = Some(Arc::new(code));
params.value = ActionValue::Transfer(U256::from_str("0de0b6b3a7640000").unwrap()); params.value = ActionValue::Transfer(U256::from_str("0de0b6b3a7640000").unwrap());
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
state.add_balance(&sender, &U256::from_str("152d02c7e14af6800000").unwrap(), CleanupMode::NoEmpty).unwrap(); state.add_balance(&sender, &U256::from_str("152d02c7e14af6800000").unwrap(), CleanupMode::NoEmpty).unwrap();
let info = EnvInfo::default(); let info = EnvInfo::default();
let engine = TestEngine::new(0); let engine = TestEngine::new(0);

View File

@ -346,7 +346,6 @@ mod tests {
use evm::Ext; use evm::Ext;
use state::{State, Substate}; use state::{State, Substate};
use tests::helpers::*; use tests::helpers::*;
use devtools::GuardedTempResult;
use super::*; use super::*;
use trace::{NoopTracer, NoopVMTracer}; use trace::{NoopTracer, NoopVMTracer};
use types::executed::CallType; use types::executed::CallType;
@ -373,7 +372,7 @@ mod tests {
} }
struct TestSetup { struct TestSetup {
state: GuardedTempResult<State<::state_db::StateDB>>, state: State<::state_db::StateDB>,
engine: Arc<Engine>, engine: Arc<Engine>,
sub_state: Substate, sub_state: Substate,
env_info: EnvInfo env_info: EnvInfo
@ -399,7 +398,7 @@ mod tests {
#[test] #[test]
fn can_be_created() { fn can_be_created() {
let mut setup = TestSetup::new(); let mut setup = TestSetup::new();
let state = setup.state.reference_mut(); let state = &mut setup.state;
let mut tracer = NoopTracer; let mut tracer = NoopTracer;
let mut vm_tracer = NoopVMTracer; let mut vm_tracer = NoopVMTracer;
@ -412,7 +411,7 @@ mod tests {
#[test] #[test]
fn can_return_block_hash_no_env() { fn can_return_block_hash_no_env() {
let mut setup = TestSetup::new(); let mut setup = TestSetup::new();
let state = setup.state.reference_mut(); let state = &mut setup.state;
let mut tracer = NoopTracer; let mut tracer = NoopTracer;
let mut vm_tracer = NoopVMTracer; let mut vm_tracer = NoopVMTracer;
@ -437,7 +436,7 @@ mod tests {
last_hashes.push(test_hash.clone()); last_hashes.push(test_hash.clone());
env_info.last_hashes = Arc::new(last_hashes); env_info.last_hashes = Arc::new(last_hashes);
} }
let state = setup.state.reference_mut(); let state = &mut setup.state;
let mut tracer = NoopTracer; let mut tracer = NoopTracer;
let mut vm_tracer = NoopVMTracer; let mut vm_tracer = NoopVMTracer;
@ -453,7 +452,7 @@ mod tests {
#[should_panic] #[should_panic]
fn can_call_fail_empty() { fn can_call_fail_empty() {
let mut setup = TestSetup::new(); let mut setup = TestSetup::new();
let state = setup.state.reference_mut(); let state = &mut setup.state;
let mut tracer = NoopTracer; let mut tracer = NoopTracer;
let mut vm_tracer = NoopVMTracer; let mut vm_tracer = NoopVMTracer;
@ -481,7 +480,7 @@ mod tests {
let log_topics = vec![H256::from("af0fa234a6af46afa23faf23bcbc1c1cb4bcb7bcbe7e7e7ee3ee2edddddddddd")]; let log_topics = vec![H256::from("af0fa234a6af46afa23faf23bcbc1c1cb4bcb7bcbe7e7e7ee3ee2edddddddddd")];
let mut setup = TestSetup::new(); let mut setup = TestSetup::new();
let state = setup.state.reference_mut(); let state = &mut setup.state;
let mut tracer = NoopTracer; let mut tracer = NoopTracer;
let mut vm_tracer = NoopVMTracer; let mut vm_tracer = NoopVMTracer;
@ -499,7 +498,7 @@ mod tests {
let refund_account = &Address::new(); let refund_account = &Address::new();
let mut setup = TestSetup::new(); let mut setup = TestSetup::new();
let state = setup.state.reference_mut(); let state = &mut setup.state;
let mut tracer = NoopTracer; let mut tracer = NoopTracer;
let mut vm_tracer = NoopVMTracer; let mut vm_tracer = NoopVMTracer;

View File

@ -215,8 +215,7 @@ fn do_json_test_for(vm_type: &VMType, json_data: &[u8]) -> Vec<String> {
} }
let out_of_gas = vm.out_of_gas(); let out_of_gas = vm.out_of_gas();
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
state.populate_from(From::from(vm.pre_state.clone())); state.populate_from(From::from(vm.pre_state.clone()));
let info = From::from(vm.env); let info = From::from(vm.env);
let engine = TestEngine::new(1); let engine = TestEngine::new(1);

View File

@ -56,7 +56,7 @@ pub fn json_chain_test(json_data: &[u8]) -> Vec<String> {
let post_root: H256 = state.hash.into(); let post_root: H256 = state.hash.into();
let transaction = multitransaction.select(&state.indexes).into(); let transaction = multitransaction.select(&state.indexes).into();
let mut state = get_temp_mem_state(); let mut state = get_temp_state();
state.populate_from(pre.clone()); state.populate_from(pre.clone());
state.commit().expect(&format!("State test {} failed due to internal error.", name)); state.commit().expect(&format!("State test {} failed due to internal error.", name));
let _res = state.apply(&env, &**engine, &transaction, false); let _res = state.apply(&env, &**engine, &transaction, false);

View File

@ -1413,19 +1413,18 @@ mod tests {
fn internal_seals_without_work() { fn internal_seals_without_work() {
let miner = Miner::with_spec(&Spec::new_instant()); let miner = Miner::with_spec(&Spec::new_instant());
let c = generate_dummy_client(2); let client = generate_dummy_client(2);
let client = c.reference().as_ref();
assert_eq!(miner.import_external_transactions(client, vec![transaction().into()]).pop().unwrap().unwrap(), TransactionImportResult::Current); assert_eq!(miner.import_external_transactions(&*client, vec![transaction().into()]).pop().unwrap().unwrap(), TransactionImportResult::Current);
miner.update_sealing(client); miner.update_sealing(&*client);
client.flush_queue(); client.flush_queue();
assert!(miner.pending_block().is_none()); assert!(miner.pending_block().is_none());
assert_eq!(client.chain_info().best_block_number, 3 as BlockNumber); assert_eq!(client.chain_info().best_block_number, 3 as BlockNumber);
assert_eq!(miner.import_own_transaction(client, PendingTransaction::new(transaction().into(), None)).unwrap(), TransactionImportResult::Current); assert_eq!(miner.import_own_transaction(&*client, PendingTransaction::new(transaction().into(), None)).unwrap(), TransactionImportResult::Current);
miner.update_sealing(client); miner.update_sealing(&*client);
client.flush_queue(); client.flush_queue();
assert!(miner.pending_block().is_none()); assert!(miner.pending_block().is_none());
assert_eq!(client.chain_info().best_block_number, 4 as BlockNumber); assert_eq!(client.chain_info().best_block_number, 4 as BlockNumber);

View File

@ -392,8 +392,7 @@ mod tests {
#[test] #[test]
fn genesis_constructor() { fn genesis_constructor() {
let spec = Spec::new_test_constructor(); let spec = Spec::new_test_constructor();
let mut db_result = get_temp_state_db(); let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
let db = spec.ensure_db_good(db_result.take(), &Default::default()).unwrap();
let state = State::from_existing(db.boxed_clone(), spec.state_root(), spec.engine.account_start_nonce(), Default::default()).unwrap(); let state = State::from_existing(db.boxed_clone(), spec.state_root(), spec.engine.account_start_nonce(), Default::default()).unwrap();
let expected = H256::from_str("0000000000000000000000000000000000000000000000000000000000000001").unwrap(); let expected = H256::from_str("0000000000000000000000000000000000000000000000000000000000000001").unwrap();
assert_eq!(state.storage_at(&Address::from_str("0000000000000000000000000000000000000005").unwrap(), &H256::zero()).unwrap(), expected); assert_eq!(state.storage_at(&Address::from_str("0000000000000000000000000000000000000005").unwrap(), &H256::zero()).unwrap(), expected);

View File

@ -939,7 +939,6 @@ mod tests {
use ethkey::Secret; use ethkey::Secret;
use util::{U256, H256, Address, Hashable}; use util::{U256, H256, Address, Hashable};
use tests::helpers::*; use tests::helpers::*;
use devtools::*;
use env_info::EnvInfo; use env_info::EnvInfo;
use spec::*; use spec::*;
use transaction::*; use transaction::*;
@ -955,8 +954,7 @@ mod tests {
fn should_apply_create_transaction() { fn should_apply_create_transaction() {
init_log(); init_log();
let temp = RandomTempPath::new(); let mut state = get_temp_state();
let mut state = get_temp_state_in(temp.as_path());
let mut info = EnvInfo::default(); let mut info = EnvInfo::default();
info.gas_limit = 1_000_000.into(); info.gas_limit = 1_000_000.into();
@ -998,9 +996,8 @@ mod tests {
let a = Address::zero(); let a = Address::zero();
let temp = RandomTempPath::new();
let mut state = { let mut state = {
let mut state = get_temp_state_in(temp.as_path()); let mut state = get_temp_state();
assert_eq!(state.exists(&a).unwrap(), false); assert_eq!(state.exists(&a).unwrap(), false);
state.inc_nonce(&a).unwrap(); state.inc_nonce(&a).unwrap();
state.commit().unwrap(); state.commit().unwrap();
@ -1015,8 +1012,7 @@ mod tests {
fn should_trace_failed_create_transaction() { fn should_trace_failed_create_transaction() {
init_log(); init_log();
let temp = RandomTempPath::new(); let mut state = get_temp_state();
let mut state = get_temp_state_in(temp.as_path());
let mut info = EnvInfo::default(); let mut info = EnvInfo::default();
info.gas_limit = 1_000_000.into(); info.gas_limit = 1_000_000.into();
@ -1052,8 +1048,7 @@ mod tests {
fn should_trace_call_transaction() { fn should_trace_call_transaction() {
init_log(); init_log();
let temp = RandomTempPath::new(); let mut state = get_temp_state();
let mut state = get_temp_state_in(temp.as_path());
let mut info = EnvInfo::default(); let mut info = EnvInfo::default();
info.gas_limit = 1_000_000.into(); info.gas_limit = 1_000_000.into();
@ -1095,8 +1090,7 @@ mod tests {
fn should_trace_basic_call_transaction() { fn should_trace_basic_call_transaction() {
init_log(); init_log();
let temp = RandomTempPath::new(); let mut state = get_temp_state();
let mut state = get_temp_state_in(temp.as_path());
let mut info = EnvInfo::default(); let mut info = EnvInfo::default();
info.gas_limit = 1_000_000.into(); info.gas_limit = 1_000_000.into();
@ -1137,8 +1131,7 @@ mod tests {
fn should_trace_call_transaction_to_builtin() { fn should_trace_call_transaction_to_builtin() {
init_log(); init_log();
let temp = RandomTempPath::new(); let mut state = get_temp_state();
let mut state = get_temp_state_in(temp.as_path());
let mut info = EnvInfo::default(); let mut info = EnvInfo::default();
info.gas_limit = 1_000_000.into(); info.gas_limit = 1_000_000.into();
@ -1179,8 +1172,7 @@ mod tests {
fn should_not_trace_subcall_transaction_to_builtin() { fn should_not_trace_subcall_transaction_to_builtin() {
init_log(); init_log();
let temp = RandomTempPath::new(); let mut state = get_temp_state();
let mut state = get_temp_state_in(temp.as_path());
let mut info = EnvInfo::default(); let mut info = EnvInfo::default();
info.gas_limit = 1_000_000.into(); info.gas_limit = 1_000_000.into();
@ -1222,8 +1214,7 @@ mod tests {
fn should_not_trace_callcode() { fn should_not_trace_callcode() {
init_log(); init_log();
let temp = RandomTempPath::new(); let mut state = get_temp_state();
let mut state = get_temp_state_in(temp.as_path());
let mut info = EnvInfo::default(); let mut info = EnvInfo::default();
info.gas_limit = 1_000_000.into(); info.gas_limit = 1_000_000.into();
@ -1281,8 +1272,7 @@ mod tests {
fn should_not_trace_delegatecall() { fn should_not_trace_delegatecall() {
init_log(); init_log();
let temp = RandomTempPath::new(); let mut state = get_temp_state();
let mut state = get_temp_state_in(temp.as_path());
let mut info = EnvInfo::default(); let mut info = EnvInfo::default();
info.gas_limit = 1_000_000.into(); info.gas_limit = 1_000_000.into();
@ -1343,8 +1333,7 @@ mod tests {
fn should_trace_failed_call_transaction() { fn should_trace_failed_call_transaction() {
init_log(); init_log();
let temp = RandomTempPath::new(); let mut state = get_temp_state();
let mut state = get_temp_state_in(temp.as_path());
let mut info = EnvInfo::default(); let mut info = EnvInfo::default();
info.gas_limit = 1_000_000.into(); info.gas_limit = 1_000_000.into();
@ -1383,8 +1372,7 @@ mod tests {
fn should_trace_call_with_subcall_transaction() { fn should_trace_call_with_subcall_transaction() {
init_log(); init_log();
let temp = RandomTempPath::new(); let mut state = get_temp_state();
let mut state = get_temp_state_in(temp.as_path());
let mut info = EnvInfo::default(); let mut info = EnvInfo::default();
info.gas_limit = 1_000_000.into(); info.gas_limit = 1_000_000.into();
@ -1443,8 +1431,7 @@ mod tests {
fn should_trace_call_with_basic_subcall_transaction() { fn should_trace_call_with_basic_subcall_transaction() {
init_log(); init_log();
let temp = RandomTempPath::new(); let mut state = get_temp_state();
let mut state = get_temp_state_in(temp.as_path());
let mut info = EnvInfo::default(); let mut info = EnvInfo::default();
info.gas_limit = 1_000_000.into(); info.gas_limit = 1_000_000.into();
@ -1498,8 +1485,7 @@ mod tests {
fn should_not_trace_call_with_invalid_basic_subcall_transaction() { fn should_not_trace_call_with_invalid_basic_subcall_transaction() {
init_log(); init_log();
let temp = RandomTempPath::new(); let mut state = get_temp_state();
let mut state = get_temp_state_in(temp.as_path());
let mut info = EnvInfo::default(); let mut info = EnvInfo::default();
info.gas_limit = 1_000_000.into(); info.gas_limit = 1_000_000.into();
@ -1541,8 +1527,7 @@ mod tests {
fn should_trace_failed_subcall_transaction() { fn should_trace_failed_subcall_transaction() {
init_log(); init_log();
let temp = RandomTempPath::new(); let mut state = get_temp_state();
let mut state = get_temp_state_in(temp.as_path());
let mut info = EnvInfo::default(); let mut info = EnvInfo::default();
info.gas_limit = 1_000_000.into(); info.gas_limit = 1_000_000.into();
@ -1597,8 +1582,7 @@ mod tests {
fn should_trace_call_with_subcall_with_subcall_transaction() { fn should_trace_call_with_subcall_with_subcall_transaction() {
init_log(); init_log();
let temp = RandomTempPath::new(); let mut state = get_temp_state();
let mut state = get_temp_state_in(temp.as_path());
let mut info = EnvInfo::default(); let mut info = EnvInfo::default();
info.gas_limit = 1_000_000.into(); info.gas_limit = 1_000_000.into();
@ -1672,8 +1656,7 @@ mod tests {
fn should_trace_failed_subcall_with_subcall_transaction() { fn should_trace_failed_subcall_with_subcall_transaction() {
init_log(); init_log();
let temp = RandomTempPath::new(); let mut state = get_temp_state();
let mut state = get_temp_state_in(temp.as_path());
let mut info = EnvInfo::default(); let mut info = EnvInfo::default();
info.gas_limit = 1_000_000.into(); info.gas_limit = 1_000_000.into();
@ -1745,8 +1728,7 @@ mod tests {
fn should_trace_suicide() { fn should_trace_suicide() {
init_log(); init_log();
let temp = RandomTempPath::new(); let mut state = get_temp_state();
let mut state = get_temp_state_in(temp.as_path());
let mut info = EnvInfo::default(); let mut info = EnvInfo::default();
info.gas_limit = 1_000_000.into(); info.gas_limit = 1_000_000.into();
@ -1797,9 +1779,8 @@ mod tests {
#[test] #[test]
fn code_from_database() { fn code_from_database() {
let a = Address::zero(); let a = Address::zero();
let temp = RandomTempPath::new();
let (root, db) = { let (root, db) = {
let mut state = get_temp_state_in(temp.as_path()); let mut state = get_temp_state();
state.require_or_from(&a, false, ||Account::new_contract(42.into(), 0.into()), |_|{}).unwrap(); state.require_or_from(&a, false, ||Account::new_contract(42.into(), 0.into()), |_|{}).unwrap();
state.init_code(&a, vec![1, 2, 3]).unwrap(); state.init_code(&a, vec![1, 2, 3]).unwrap();
assert_eq!(state.code(&a).unwrap(), Some(Arc::new([1u8, 2, 3].to_vec()))); assert_eq!(state.code(&a).unwrap(), Some(Arc::new([1u8, 2, 3].to_vec())));
@ -1815,9 +1796,8 @@ mod tests {
#[test] #[test]
fn storage_at_from_database() { fn storage_at_from_database() {
let a = Address::zero(); let a = Address::zero();
let temp = RandomTempPath::new();
let (root, db) = { let (root, db) = {
let mut state = get_temp_state_in(temp.as_path()); let mut state = get_temp_state();
state.set_storage(&a, H256::from(&U256::from(1u64)), H256::from(&U256::from(69u64))).unwrap(); state.set_storage(&a, H256::from(&U256::from(1u64)), H256::from(&U256::from(69u64))).unwrap();
state.commit().unwrap(); state.commit().unwrap();
state.drop() state.drop()
@ -1830,9 +1810,8 @@ mod tests {
#[test] #[test]
fn get_from_database() { fn get_from_database() {
let a = Address::zero(); let a = Address::zero();
let temp = RandomTempPath::new();
let (root, db) = { let (root, db) = {
let mut state = get_temp_state_in(temp.as_path()); let mut state = get_temp_state();
state.inc_nonce(&a).unwrap(); state.inc_nonce(&a).unwrap();
state.add_balance(&a, &U256::from(69u64), CleanupMode::NoEmpty).unwrap(); state.add_balance(&a, &U256::from(69u64), CleanupMode::NoEmpty).unwrap();
state.commit().unwrap(); state.commit().unwrap();
@ -1848,8 +1827,7 @@ mod tests {
#[test] #[test]
fn remove() { fn remove() {
let a = Address::zero(); let a = Address::zero();
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
assert_eq!(state.exists(&a).unwrap(), false); assert_eq!(state.exists(&a).unwrap(), false);
assert_eq!(state.exists_and_not_null(&a).unwrap(), false); assert_eq!(state.exists_and_not_null(&a).unwrap(), false);
state.inc_nonce(&a).unwrap(); state.inc_nonce(&a).unwrap();
@ -1865,8 +1843,7 @@ mod tests {
#[test] #[test]
fn empty_account_is_not_created() { fn empty_account_is_not_created() {
let a = Address::zero(); let a = Address::zero();
let path = RandomTempPath::new(); let db = get_temp_state_db();
let db = get_temp_state_db_in(path.as_path());
let (root, db) = { let (root, db) = {
let mut state = State::new(db, U256::from(0), Default::default()); let mut state = State::new(db, U256::from(0), Default::default());
state.add_balance(&a, &U256::default(), CleanupMode::NoEmpty).unwrap(); // create an empty account state.add_balance(&a, &U256::default(), CleanupMode::NoEmpty).unwrap(); // create an empty account
@ -1881,8 +1858,7 @@ mod tests {
#[test] #[test]
fn empty_account_exists_when_creation_forced() { fn empty_account_exists_when_creation_forced() {
let a = Address::zero(); let a = Address::zero();
let path = RandomTempPath::new(); let db = get_temp_state_db();
let db = get_temp_state_db_in(path.as_path());
let (root, db) = { let (root, db) = {
let mut state = State::new(db, U256::from(0), Default::default()); let mut state = State::new(db, U256::from(0), Default::default());
state.add_balance(&a, &U256::default(), CleanupMode::ForceCreate).unwrap(); // create an empty account state.add_balance(&a, &U256::default(), CleanupMode::ForceCreate).unwrap(); // create an empty account
@ -1897,9 +1873,8 @@ mod tests {
#[test] #[test]
fn remove_from_database() { fn remove_from_database() {
let a = Address::zero(); let a = Address::zero();
let temp = RandomTempPath::new();
let (root, db) = { let (root, db) = {
let mut state = get_temp_state_in(temp.as_path()); let mut state = get_temp_state();
state.inc_nonce(&a).unwrap(); state.inc_nonce(&a).unwrap();
state.commit().unwrap(); state.commit().unwrap();
assert_eq!(state.exists(&a).unwrap(), true); assert_eq!(state.exists(&a).unwrap(), true);
@ -1925,8 +1900,7 @@ mod tests {
#[test] #[test]
fn alter_balance() { fn alter_balance() {
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
let a = Address::zero(); let a = Address::zero();
let b = 1u64.into(); let b = 1u64.into();
state.add_balance(&a, &U256::from(69u64), CleanupMode::NoEmpty).unwrap(); state.add_balance(&a, &U256::from(69u64), CleanupMode::NoEmpty).unwrap();
@ -1947,8 +1921,7 @@ mod tests {
#[test] #[test]
fn alter_nonce() { fn alter_nonce() {
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
let a = Address::zero(); let a = Address::zero();
state.inc_nonce(&a).unwrap(); state.inc_nonce(&a).unwrap();
assert_eq!(state.nonce(&a).unwrap(), U256::from(1u64)); assert_eq!(state.nonce(&a).unwrap(), U256::from(1u64));
@ -1964,8 +1937,7 @@ mod tests {
#[test] #[test]
fn balance_nonce() { fn balance_nonce() {
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
let a = Address::zero(); let a = Address::zero();
assert_eq!(state.balance(&a).unwrap(), U256::from(0u64)); assert_eq!(state.balance(&a).unwrap(), U256::from(0u64));
assert_eq!(state.nonce(&a).unwrap(), U256::from(0u64)); assert_eq!(state.nonce(&a).unwrap(), U256::from(0u64));
@ -1976,8 +1948,7 @@ mod tests {
#[test] #[test]
fn ensure_cached() { fn ensure_cached() {
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
let a = Address::zero(); let a = Address::zero();
state.require(&a, false).unwrap(); state.require(&a, false).unwrap();
state.commit().unwrap(); state.commit().unwrap();
@ -1986,8 +1957,7 @@ mod tests {
#[test] #[test]
fn checkpoint_basic() { fn checkpoint_basic() {
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
let a = Address::zero(); let a = Address::zero();
state.checkpoint(); state.checkpoint();
state.add_balance(&a, &U256::from(69u64), CleanupMode::NoEmpty).unwrap(); state.add_balance(&a, &U256::from(69u64), CleanupMode::NoEmpty).unwrap();
@ -2003,8 +1973,7 @@ mod tests {
#[test] #[test]
fn checkpoint_nested() { fn checkpoint_nested() {
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
let a = Address::zero(); let a = Address::zero();
state.checkpoint(); state.checkpoint();
state.checkpoint(); state.checkpoint();
@ -2018,16 +1987,14 @@ mod tests {
#[test] #[test]
fn create_empty() { fn create_empty() {
let mut state_result = get_temp_state(); let mut state = get_temp_state();
let mut state = state_result.reference_mut();
state.commit().unwrap(); state.commit().unwrap();
assert_eq!(state.root().hex(), "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"); assert_eq!(state.root().hex(), "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421");
} }
#[test] #[test]
fn should_not_panic_on_state_diff_with_storage() { fn should_not_panic_on_state_diff_with_storage() {
let state = get_temp_state(); let mut state = get_temp_state();
let mut state = state.reference().clone();
let a: Address = 0xa.into(); let a: Address = 0xa.into();
state.init_code(&a, b"abcdefg".to_vec()).unwrap();; state.init_code(&a, b"abcdefg".to_vec()).unwrap();;

View File

@ -463,8 +463,7 @@ mod tests {
fn state_db_smoke() { fn state_db_smoke() {
init_log(); init_log();
let mut state_db_result = get_temp_state_db(); let state_db = get_temp_state_db();
let state_db = state_db_result.take();
let root_parent = H256::random(); let root_parent = H256::random();
let address = Address::random(); let address = Address::random();
let h0 = H256::random(); let h0 = H256::random();

View File

@ -72,8 +72,7 @@ fn should_return_registrar() {
#[test] #[test]
fn returns_state_root_basic() { fn returns_state_root_basic() {
let client_result = generate_dummy_client(6); let client = generate_dummy_client(6);
let client = client_result.reference();
let test_spec = get_test_spec(); let test_spec = get_test_spec();
let genesis_header = test_spec.genesis_header(); let genesis_header = test_spec.genesis_header();
@ -125,8 +124,7 @@ fn query_none_block() {
#[test] #[test]
fn query_bad_block() { fn query_bad_block() {
let client_result = get_test_client_with_blocks(vec![get_bad_state_dummy_block()]); let client = get_test_client_with_blocks(vec![get_bad_state_dummy_block()]);
let client = client_result.reference();
let bad_block: Option<_> = client.block_header(BlockId::Number(1)); let bad_block: Option<_> = client.block_header(BlockId::Number(1));
assert!(bad_block.is_none()); assert!(bad_block.is_none());
@ -135,8 +133,7 @@ fn query_bad_block() {
#[test] #[test]
fn returns_chain_info() { fn returns_chain_info() {
let dummy_block = get_good_dummy_block(); let dummy_block = get_good_dummy_block();
let client_result = get_test_client_with_blocks(vec![dummy_block.clone()]); let client = get_test_client_with_blocks(vec![dummy_block.clone()]);
let client = client_result.reference();
let block = BlockView::new(&dummy_block); let block = BlockView::new(&dummy_block);
let info = client.chain_info(); let info = client.chain_info();
assert_eq!(info.best_block_hash, block.header().hash()); assert_eq!(info.best_block_hash, block.header().hash());
@ -145,8 +142,7 @@ fn returns_chain_info() {
#[test] #[test]
fn returns_logs() { fn returns_logs() {
let dummy_block = get_good_dummy_block(); let dummy_block = get_good_dummy_block();
let client_result = get_test_client_with_blocks(vec![dummy_block.clone()]); let client = get_test_client_with_blocks(vec![dummy_block.clone()]);
let client = client_result.reference();
let logs = client.logs(Filter { let logs = client.logs(Filter {
from_block: BlockId::Earliest, from_block: BlockId::Earliest,
to_block: BlockId::Latest, to_block: BlockId::Latest,
@ -160,8 +156,7 @@ fn returns_logs() {
#[test] #[test]
fn returns_logs_with_limit() { fn returns_logs_with_limit() {
let dummy_block = get_good_dummy_block(); let dummy_block = get_good_dummy_block();
let client_result = get_test_client_with_blocks(vec![dummy_block.clone()]); let client = get_test_client_with_blocks(vec![dummy_block.clone()]);
let client = client_result.reference();
let logs = client.logs(Filter { let logs = client.logs(Filter {
from_block: BlockId::Earliest, from_block: BlockId::Earliest,
to_block: BlockId::Latest, to_block: BlockId::Latest,
@ -175,8 +170,7 @@ fn returns_logs_with_limit() {
#[test] #[test]
fn returns_block_body() { fn returns_block_body() {
let dummy_block = get_good_dummy_block(); let dummy_block = get_good_dummy_block();
let client_result = get_test_client_with_blocks(vec![dummy_block.clone()]); let client = get_test_client_with_blocks(vec![dummy_block.clone()]);
let client = client_result.reference();
let block = BlockView::new(&dummy_block); let block = BlockView::new(&dummy_block);
let body = client.block_body(BlockId::Hash(block.header().hash())).unwrap(); let body = client.block_body(BlockId::Hash(block.header().hash())).unwrap();
let body = body.rlp(); let body = body.rlp();
@ -187,8 +181,7 @@ fn returns_block_body() {
#[test] #[test]
fn imports_block_sequence() { fn imports_block_sequence() {
let client_result = generate_dummy_client(6); let client = generate_dummy_client(6);
let client = client_result.reference();
let block = client.block_header(BlockId::Number(5)).unwrap(); let block = client.block_header(BlockId::Number(5)).unwrap();
assert!(!block.into_inner().is_empty()); assert!(!block.into_inner().is_empty());
@ -196,8 +189,7 @@ fn imports_block_sequence() {
#[test] #[test]
fn can_collect_garbage() { fn can_collect_garbage() {
let client_result = generate_dummy_client(100); let client = generate_dummy_client(100);
let client = client_result.reference();
client.tick(); client.tick();
assert!(client.blockchain_cache_info().blocks < 100 * 1024); assert!(client.blockchain_cache_info().blocks < 100 * 1024);
} }
@ -205,19 +197,16 @@ fn can_collect_garbage() {
#[test] #[test]
fn can_generate_gas_price_median() { fn can_generate_gas_price_median() {
let client_result = generate_dummy_client_with_data(3, 1, slice_into![1, 2, 3]); let client = generate_dummy_client_with_data(3, 1, slice_into![1, 2, 3]);
let client = client_result.reference();
assert_eq!(Some(&U256::from(2)), client.gas_price_corpus(3).median()); assert_eq!(Some(&U256::from(2)), client.gas_price_corpus(3).median());
let client_result = generate_dummy_client_with_data(4, 1, slice_into![1, 4, 3, 2]); let client = generate_dummy_client_with_data(4, 1, slice_into![1, 4, 3, 2]);
let client = client_result.reference();
assert_eq!(Some(&U256::from(3)), client.gas_price_corpus(3).median()); assert_eq!(Some(&U256::from(3)), client.gas_price_corpus(3).median());
} }
#[test] #[test]
fn can_generate_gas_price_histogram() { fn can_generate_gas_price_histogram() {
let client_result = generate_dummy_client_with_data(20, 1, slice_into![6354,8593,6065,4842,7845,7002,689,4958,4250,6098,5804,4320,643,8895,2296,8589,7145,2000,2512,1408]); let client = generate_dummy_client_with_data(20, 1, slice_into![6354,8593,6065,4842,7845,7002,689,4958,4250,6098,5804,4320,643,8895,2296,8589,7145,2000,2512,1408]);
let client = client_result.reference();
let hist = client.gas_price_corpus(20).histogram(5).unwrap(); let hist = client.gas_price_corpus(20).histogram(5).unwrap();
let correct_hist = ::stats::Histogram { bucket_bounds: vec_into![643, 2294, 3945, 5596, 7247, 8898], counts: vec![4,2,4,6,4] }; let correct_hist = ::stats::Histogram { bucket_bounds: vec_into![643, 2294, 3945, 5596, 7247, 8898], counts: vec![4,2,4,6,4] };
@ -226,32 +215,29 @@ fn can_generate_gas_price_histogram() {
#[test] #[test]
fn empty_gas_price_histogram() { fn empty_gas_price_histogram() {
let client_result = generate_dummy_client_with_data(20, 0, slice_into![]); let client = generate_dummy_client_with_data(20, 0, slice_into![]);
let client = client_result.reference();
assert!(client.gas_price_corpus(20).histogram(5).is_none()); assert!(client.gas_price_corpus(20).histogram(5).is_none());
} }
#[test] #[test]
fn corpus_is_sorted() { fn corpus_is_sorted() {
let client_result = generate_dummy_client_with_data(2, 1, slice_into![U256::from_str("11426908979").unwrap(), U256::from_str("50426908979").unwrap()]); let client = generate_dummy_client_with_data(2, 1, slice_into![U256::from_str("11426908979").unwrap(), U256::from_str("50426908979").unwrap()]);
let client = client_result.reference();
let corpus = client.gas_price_corpus(20); let corpus = client.gas_price_corpus(20);
assert!(corpus[0] < corpus[1]); assert!(corpus[0] < corpus[1]);
} }
#[test] #[test]
fn can_handle_long_fork() { fn can_handle_long_fork() {
let client_result = generate_dummy_client(1200); let client = generate_dummy_client(1200);
let client = client_result.reference();
for _ in 0..20 { for _ in 0..20 {
client.import_verified_blocks(); client.import_verified_blocks();
} }
assert_eq!(1200, client.chain_info().best_block_number); assert_eq!(1200, client.chain_info().best_block_number);
push_blocks_to_client(client, 45, 1201, 800); push_blocks_to_client(&client, 45, 1201, 800);
push_blocks_to_client(client, 49, 1201, 800); push_blocks_to_client(&client, 49, 1201, 800);
push_blocks_to_client(client, 53, 1201, 600); push_blocks_to_client(&client, 53, 1201, 600);
for _ in 0..400 { for _ in 0..400 {
client.import_verified_blocks(); client.import_verified_blocks();
@ -262,8 +248,7 @@ fn can_handle_long_fork() {
#[test] #[test]
fn can_mine() { fn can_mine() {
let dummy_blocks = get_good_dummy_block_seq(2); let dummy_blocks = get_good_dummy_block_seq(2);
let client_result = get_test_client_with_blocks(vec![dummy_blocks[0].clone()]); let client = get_test_client_with_blocks(vec![dummy_blocks[0].clone()]);
let client = client_result.reference();
let b = client.prepare_open_block(Address::default(), (3141562.into(), 31415620.into()), vec![]).close(); let b = client.prepare_open_block(Address::default(), (3141562.into(), 31415620.into()), vec![]).close();
@ -329,14 +314,13 @@ fn does_not_propagate_delayed_transactions() {
value: 0.into(), value: 0.into(),
data: Vec::new(), data: Vec::new(),
}.sign(secret, None), None); }.sign(secret, None), None);
let client_result = generate_dummy_client(1); let client = generate_dummy_client(1);
let client = client_result.reference();
client.miner().import_own_transaction(&**client, tx0).unwrap(); client.miner().import_own_transaction(&*client, tx0).unwrap();
client.miner().import_own_transaction(&**client, tx1).unwrap(); client.miner().import_own_transaction(&*client, tx1).unwrap();
assert_eq!(0, client.ready_transactions().len()); assert_eq!(0, client.ready_transactions().len());
assert_eq!(2, client.miner().pending_transactions().len()); assert_eq!(2, client.miner().pending_transactions().len());
push_blocks_to_client(client, 53, 2, 2); push_blocks_to_client(&client, 53, 2, 2);
client.flush_queue(); client.flush_queue();
assert_eq!(2, client.ready_transactions().len()); assert_eq!(2, client.ready_transactions().len());
assert_eq!(2, client.miner().pending_transactions().len()); assert_eq!(2, client.miner().pending_transactions().len());
@ -346,8 +330,7 @@ fn does_not_propagate_delayed_transactions() {
fn transaction_proof() { fn transaction_proof() {
use ::client::ProvingBlockChainClient; use ::client::ProvingBlockChainClient;
let client_result = generate_dummy_client(0); let client = generate_dummy_client(0);
let client = client_result.reference();
let address = Address::random(); let address = Address::random();
let test_spec = Spec::new_test(); let test_spec = Spec::new_test();
for _ in 0..20 { for _ in 0..20 {

View File

@ -30,7 +30,6 @@ use engines::Engine;
use env_info::EnvInfo; use env_info::EnvInfo;
use ethereum; use ethereum;
use ethereum::ethash::EthashParams; use ethereum::ethash::EthashParams;
use devtools::*;
use miner::Miner; use miner::Miner;
use header::Header; use header::Header;
use transaction::{Action, Transaction, SignedTransaction}; use transaction::{Action, Transaction, SignedTransaction};
@ -133,28 +132,26 @@ pub fn create_test_block_with_data(header: &Header, transactions: &[SignedTransa
rlp.out() rlp.out()
} }
pub fn generate_dummy_client(block_number: u32) -> GuardedTempResult<Arc<Client>> { pub fn generate_dummy_client(block_number: u32) -> Arc<Client> {
generate_dummy_client_with_spec_and_data(Spec::new_test, block_number, 0, &[]) generate_dummy_client_with_spec_and_data(Spec::new_test, block_number, 0, &[])
} }
pub fn generate_dummy_client_with_data(block_number: u32, txs_per_block: usize, tx_gas_prices: &[U256]) -> GuardedTempResult<Arc<Client>> { pub fn generate_dummy_client_with_data(block_number: u32, txs_per_block: usize, tx_gas_prices: &[U256]) -> Arc<Client> {
generate_dummy_client_with_spec_and_data(Spec::new_null, block_number, txs_per_block, tx_gas_prices) generate_dummy_client_with_spec_and_data(Spec::new_null, block_number, txs_per_block, tx_gas_prices)
} }
pub fn generate_dummy_client_with_spec_and_data<F>(get_test_spec: F, block_number: u32, txs_per_block: usize, tx_gas_prices: &[U256]) -> GuardedTempResult<Arc<Client>> where F: Fn()->Spec { pub fn generate_dummy_client_with_spec_and_data<F>(get_test_spec: F, block_number: u32, txs_per_block: usize, tx_gas_prices: &[U256]) -> Arc<Client> where F: Fn()->Spec {
generate_dummy_client_with_spec_accounts_and_data(get_test_spec, None, block_number, txs_per_block, tx_gas_prices) generate_dummy_client_with_spec_accounts_and_data(get_test_spec, None, block_number, txs_per_block, tx_gas_prices)
} }
pub fn generate_dummy_client_with_spec_and_accounts<F>(get_test_spec: F, accounts: Option<Arc<AccountProvider>>) -> GuardedTempResult<Arc<Client>> where F: Fn()->Spec { pub fn generate_dummy_client_with_spec_and_accounts<F>(get_test_spec: F, accounts: Option<Arc<AccountProvider>>) -> Arc<Client> where F: Fn()->Spec {
generate_dummy_client_with_spec_accounts_and_data(get_test_spec, accounts, 0, 0, &[]) generate_dummy_client_with_spec_accounts_and_data(get_test_spec, accounts, 0, 0, &[])
} }
pub fn generate_dummy_client_with_spec_accounts_and_data<F>(get_test_spec: F, accounts: Option<Arc<AccountProvider>>, block_number: u32, txs_per_block: usize, tx_gas_prices: &[U256]) -> GuardedTempResult<Arc<Client>> where F: Fn()->Spec { pub fn generate_dummy_client_with_spec_accounts_and_data<F>(get_test_spec: F, accounts: Option<Arc<AccountProvider>>, block_number: u32, txs_per_block: usize, tx_gas_prices: &[U256]) -> Arc<Client> where F: Fn()->Spec {
let dir = RandomTempPath::new();
let test_spec = get_test_spec(); let test_spec = get_test_spec();
let db_config = DatabaseConfig::with_columns(::db::NUM_COLUMNS); let client_db = new_db();
let client_db = Arc::new(Database::open(&db_config, dir.as_path().to_str().unwrap()).unwrap());
let client = Client::new( let client = Client::new(
ClientConfig::default(), ClientConfig::default(),
@ -165,8 +162,7 @@ pub fn generate_dummy_client_with_spec_accounts_and_data<F>(get_test_spec: F, ac
).unwrap(); ).unwrap();
let test_engine = &*test_spec.engine; let test_engine = &*test_spec.engine;
let mut db_result = get_temp_state_db(); let mut db = test_spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
let mut db = test_spec.ensure_db_good(db_result.take(), &Default::default()).unwrap();
let genesis_header = test_spec.genesis_header(); let genesis_header = test_spec.genesis_header();
let mut rolling_timestamp = 40; let mut rolling_timestamp = 40;
@ -220,11 +216,7 @@ pub fn generate_dummy_client_with_spec_accounts_and_data<F>(get_test_spec: F, ac
} }
client.flush_queue(); client.flush_queue();
client.import_verified_blocks(); client.import_verified_blocks();
client
GuardedTempResult::<Arc<Client>> {
_temp: dir,
result: Some(client)
}
} }
pub fn push_blocks_to_client(client: &Arc<Client>, timestamp_salt: u64, starting_number: usize, block_number: usize) { pub fn push_blocks_to_client(client: &Arc<Client>, timestamp_salt: u64, starting_number: usize, block_number: usize) {
@ -256,11 +248,9 @@ pub fn push_blocks_to_client(client: &Arc<Client>, timestamp_salt: u64, starting
} }
} }
pub fn get_test_client_with_blocks(blocks: Vec<Bytes>) -> GuardedTempResult<Arc<Client>> { pub fn get_test_client_with_blocks(blocks: Vec<Bytes>) -> Arc<Client> {
let dir = RandomTempPath::new();
let test_spec = get_test_spec(); let test_spec = get_test_spec();
let db_config = DatabaseConfig::with_columns(::db::NUM_COLUMNS); let client_db = new_db();
let client_db = Arc::new(Database::open(&db_config, dir.as_path().to_str().unwrap()).unwrap());
let client = Client::new( let client = Client::new(
ClientConfig::default(), ClientConfig::default(),
@ -277,23 +267,15 @@ pub fn get_test_client_with_blocks(blocks: Vec<Bytes>) -> GuardedTempResult<Arc<
} }
client.flush_queue(); client.flush_queue();
client.import_verified_blocks(); client.import_verified_blocks();
client
GuardedTempResult::<Arc<Client>> {
_temp: dir,
result: Some(client)
}
} }
fn new_db(path: &str) -> Arc<Database> { fn new_db() -> Arc<KeyValueDB> {
Arc::new( Arc::new(::util::kvdb::in_memory(::db::NUM_COLUMNS.unwrap_or(0)))
Database::open(&DatabaseConfig::with_columns(::db::NUM_COLUMNS), path)
.expect("Opening database for tests should always work.")
)
} }
pub fn generate_dummy_blockchain(block_number: u32) -> GuardedTempResult<BlockChain> { pub fn generate_dummy_blockchain(block_number: u32) -> BlockChain {
let temp = RandomTempPath::new(); let db = new_db();
let db = new_db(temp.as_str());
let bc = BlockChain::new(BlockChainConfig::default(), &create_unverifiable_block(0, H256::zero()), db.clone()); let bc = BlockChain::new(BlockChainConfig::default(), &create_unverifiable_block(0, H256::zero()), db.clone());
let mut batch = db.transaction(); let mut batch = db.transaction();
@ -302,16 +284,11 @@ pub fn generate_dummy_blockchain(block_number: u32) -> GuardedTempResult<BlockCh
bc.commit(); bc.commit();
} }
db.write(batch).unwrap(); db.write(batch).unwrap();
bc
GuardedTempResult::<BlockChain> {
_temp: temp,
result: Some(bc)
}
} }
pub fn generate_dummy_blockchain_with_extra(block_number: u32) -> GuardedTempResult<BlockChain> { pub fn generate_dummy_blockchain_with_extra(block_number: u32) -> BlockChain {
let temp = RandomTempPath::new(); let db = new_db();
let db = new_db(temp.as_str());
let bc = BlockChain::new(BlockChainConfig::default(), &create_unverifiable_block(0, H256::zero()), db.clone()); let bc = BlockChain::new(BlockChainConfig::default(), &create_unverifiable_block(0, H256::zero()), db.clone());
@ -321,69 +298,29 @@ pub fn generate_dummy_blockchain_with_extra(block_number: u32) -> GuardedTempRes
bc.commit(); bc.commit();
} }
db.write(batch).unwrap(); db.write(batch).unwrap();
bc
GuardedTempResult::<BlockChain> {
_temp: temp,
result: Some(bc)
}
} }
pub fn generate_dummy_empty_blockchain() -> GuardedTempResult<BlockChain> { pub fn generate_dummy_empty_blockchain() -> BlockChain {
let temp = RandomTempPath::new(); let db = new_db();
let db = new_db(temp.as_str());
let bc = BlockChain::new(BlockChainConfig::default(), &create_unverifiable_block(0, H256::zero()), db.clone()); let bc = BlockChain::new(BlockChainConfig::default(), &create_unverifiable_block(0, H256::zero()), db.clone());
bc
GuardedTempResult::<BlockChain> {
_temp: temp,
result: Some(bc)
}
} }
pub fn get_temp_state_db() -> GuardedTempResult<StateDB> { pub fn get_temp_state() -> State<::state_db::StateDB> {
let temp = RandomTempPath::new(); let journal_db = get_temp_state_db();
let journal_db = get_temp_state_db_in(temp.as_path());
GuardedTempResult {
_temp: temp,
result: Some(journal_db)
}
}
pub fn get_temp_state() -> GuardedTempResult<State<::state_db::StateDB>> {
let temp = RandomTempPath::new();
let journal_db = get_temp_state_db_in(temp.as_path());
GuardedTempResult {
_temp: temp,
result: Some(State::new(journal_db, U256::from(0), Default::default())),
}
}
pub fn get_temp_mem_state() -> State<::state_db::StateDB> {
let journal_db = get_temp_mem_state_db();
State::new(journal_db, U256::from(0), Default::default()) State::new(journal_db, U256::from(0), Default::default())
} }
pub fn get_temp_state_db_in(path: &Path) -> StateDB { pub fn get_temp_state_db() -> StateDB {
let db = new_db(path.to_str().expect("Only valid utf8 paths for tests.")); let db = new_db();
let journal_db = journaldb::new(db.clone(), journaldb::Algorithm::EarlyMerge, ::db::COL_STATE);
StateDB::new(journal_db, 5 * 1024 * 1024)
}
pub fn get_temp_mem_state_db() -> StateDB {
let db = Arc::new(::util::kvdb::in_memory(::db::NUM_COLUMNS.unwrap_or(0)));
let journal_db = journaldb::new(db, journaldb::Algorithm::EarlyMerge, ::db::COL_STATE); let journal_db = journaldb::new(db, journaldb::Algorithm::EarlyMerge, ::db::COL_STATE);
StateDB::new(journal_db, 5 * 1024 * 1024) StateDB::new(journal_db, 5 * 1024 * 1024)
} }
pub fn get_temp_state_in(path: &Path) -> State<::state_db::StateDB> {
let journal_db = get_temp_state_db_in(path);
State::new(journal_db, U256::from(0), Default::default())
}
pub fn get_good_dummy_block_seq(count: usize) -> Vec<Bytes> { pub fn get_good_dummy_block_seq(count: usize) -> Vec<Bytes> {
let test_spec = get_test_spec(); let test_spec = get_test_spec();
get_good_dummy_block_fork_seq(1, count, &test_spec.genesis_header().hash()) get_good_dummy_block_fork_seq(1, count, &test_spec.genesis_header().hash())
} }
pub fn get_good_dummy_block_fork_seq(start_number: usize, count: usize, parent_hash: &H256) -> Vec<Bytes> { pub fn get_good_dummy_block_fork_seq(start_number: usize, count: usize, parent_hash: &H256) -> Vec<Bytes> {
@ -405,7 +342,6 @@ pub fn get_good_dummy_block_fork_seq(start_number: usize, count: usize, parent_h
rolling_timestamp = rolling_timestamp + 10; rolling_timestamp = rolling_timestamp + 10;
r.push(create_test_block(&block_header)); r.push(create_test_block(&block_header));
} }
r r
} }