From 31cf20ffa2a195c17cf7d4ed4d6a881b6f1ac6cd Mon Sep 17 00:00:00 2001 From: arkpar Date: Fri, 5 Feb 2016 12:58:18 +0100 Subject: [PATCH] New account_db key scheme; snapshot tests --- ethcore/src/account_db.rs | 5 ++++- ethcore/src/state.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/ethcore/src/account_db.rs b/ethcore/src/account_db.rs index 7a07778f0..e7f1b2bad 100644 --- a/ethcore/src/account_db.rs +++ b/ethcore/src/account_db.rs @@ -13,7 +13,10 @@ pub struct AccountDB<'db> { #[inline] fn combine_key<'a>(address: &'a H256, key: &'a H256) -> H256 { - address ^ key + let mut addr_hash = address.sha3(); + // preserve 96 bits of original key for db lookup + addr_hash[0..12].clone_from_slice(&[0u8; 12]); + &addr_hash ^ key } impl<'db> AccountDB<'db> { diff --git a/ethcore/src/state.rs b/ethcore/src/state.rs index 4db171d77..947326e56 100644 --- a/ethcore/src/state.rs +++ b/ethcore/src/state.rs @@ -482,6 +482,38 @@ fn ensure_cached() { assert_eq!(state.root().hex(), "0ce23f3c809de377b008a4a3ee94a0834aac8bec1f86e28ffe4fdb5a15b0c785"); } +#[test] +fn snapshot_basic() { + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); + let a = Address::zero(); + state.snapshot(); + state.add_balance(&a, &U256::from(69u64)); + assert_eq!(state.balance(&a), U256::from(69u64)); + state.clear_snapshot(); + assert_eq!(state.balance(&a), U256::from(69u64)); + state.snapshot(); + state.add_balance(&a, &U256::from(1u64)); + assert_eq!(state.balance(&a), U256::from(70u64)); + state.revert_snapshot(); + assert_eq!(state.balance(&a), U256::from(69u64)); +} + +#[test] +fn snapshot_nested() { + let mut state_result = get_temp_state(); + let mut state = state_result.reference_mut(); + let a = Address::zero(); + state.snapshot(); + state.snapshot(); + state.add_balance(&a, &U256::from(69u64)); + assert_eq!(state.balance(&a), U256::from(69u64)); + state.clear_snapshot(); + assert_eq!(state.balance(&a), U256::from(69u64)); + state.revert_snapshot(); + assert_eq!(state.balance(&a), U256::from(0)); +} + #[test] fn create_empty() { let mut state_result = get_temp_state();