From 7c715aeec3449b61d32479898a878613024f3f44 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Tue, 3 Jan 2017 16:32:50 +0100 Subject: [PATCH 1/3] basic account type --- ethcore/src/state/account.rs | 67 +++++++++++++++++++++++++++++------- ethcore/src/state/mod.rs | 2 +- 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/ethcore/src/state/account.rs b/ethcore/src/state/account.rs index 49cebd550..9d6b58d0e 100644 --- a/ethcore/src/state/account.rs +++ b/ethcore/src/state/account.rs @@ -25,6 +25,41 @@ use std::cell::{RefCell, Cell}; const STORAGE_CACHE_ITEMS: usize = 8192; +/// Basic account type. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct BasicAccount { + /// Nonce of the account. + pub nonce: U256, + /// Balance of the account. + pub balance: U256, + /// Storage root of the account. + pub storage_root: H256, + /// Code hash of the account. + pub code_hash: H256, +} + +impl Encodable for BasicAccount { + fn rlp_append(&self, s: &mut RlpStream) { + s.begin_list(4) + .append(&self.nonce) + .append(&self.balance) + .append(&self.storage_root) + .append(&self.code_hash); + } +} + +impl Decodable for BasicAccount { + fn decode(decoder: &D) -> Result where D: Decoder { + let rlp = decoder.as_rlp(); + Ok(BasicAccount { + nonce: rlp.val_at(0)?, + balance: rlp.val_at(1)?, + storage_root: rlp.val_at(2)?, + code_hash: rlp.val_at(3)?, + }) + } +} + /// Single account in the system. /// Keeps track of changes to the code and storage. /// The changes are applied in `commit_storage` and `commit_code` @@ -53,6 +88,23 @@ pub struct Account { address_hash: Cell>, } +impl From for Account { + fn from(basic: BasicAccount) -> Self { + Account { + balance: basic.balance, + nonce: basic.nonce, + storage_root: basic.storage_root, + storage_cache: Self::empty_storage_cache(), + storage_changes: HashMap::new(), + code_hash: basic.code_hash, + code_size: None, + code_cache: Arc::new(vec![]), + code_filth: Filth::Clean, + address_hash: Cell::new(None), + } + } +} + impl Account { #[cfg(test)] /// General constructor. @@ -109,19 +161,8 @@ impl Account { /// Create a new account from RLP. pub fn from_rlp(rlp: &[u8]) -> Account { - let r: Rlp = Rlp::new(rlp); - Account { - nonce: r.val_at(0), - balance: r.val_at(1), - storage_root: r.val_at(2), - storage_cache: Self::empty_storage_cache(), - storage_changes: HashMap::new(), - code_hash: r.val_at(3), - code_cache: Arc::new(vec![]), - code_size: None, - code_filth: Filth::Clean, - address_hash: Cell::new(None), - } + let basic: BasicAccount = ::rlp::decode(rlp); + basic.into() } /// Create a new contract account. diff --git a/ethcore/src/state/mod.rs b/ethcore/src/state/mod.rs index c9730c1c3..1dcc37732 100644 --- a/ethcore/src/state/mod.rs +++ b/ethcore/src/state/mod.rs @@ -37,7 +37,7 @@ use util::trie::recorder::{Recorder, BasicRecorder as TrieRecorder}; mod account; mod substate; -pub use self::account::Account; +pub use self::account::{BasicAccount, Account}; pub use self::substate::Substate; /// Used to return information about an `State::apply` operation. From eb2b1ad5daf93453e82092761a7c60da4ae6a465 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Tue, 3 Jan 2017 16:43:22 +0100 Subject: [PATCH 2/3] move basic_account to types module --- ethcore/src/state/account.rs | 36 +------------------ ethcore/src/state/mod.rs | 2 +- ethcore/src/types/basic_account.rs | 55 ++++++++++++++++++++++++++++++ ethcore/src/types/mod.rs.in | 1 + 4 files changed, 58 insertions(+), 36 deletions(-) create mode 100644 ethcore/src/types/basic_account.rs diff --git a/ethcore/src/state/account.rs b/ethcore/src/state/account.rs index 9d6b58d0e..63e8ff9de 100644 --- a/ethcore/src/state/account.rs +++ b/ethcore/src/state/account.rs @@ -20,46 +20,12 @@ use util::*; use pod_account::*; use rlp::*; use lru_cache::LruCache; +use basic_account::BasicAccount; use std::cell::{RefCell, Cell}; const STORAGE_CACHE_ITEMS: usize = 8192; -/// Basic account type. -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct BasicAccount { - /// Nonce of the account. - pub nonce: U256, - /// Balance of the account. - pub balance: U256, - /// Storage root of the account. - pub storage_root: H256, - /// Code hash of the account. - pub code_hash: H256, -} - -impl Encodable for BasicAccount { - fn rlp_append(&self, s: &mut RlpStream) { - s.begin_list(4) - .append(&self.nonce) - .append(&self.balance) - .append(&self.storage_root) - .append(&self.code_hash); - } -} - -impl Decodable for BasicAccount { - fn decode(decoder: &D) -> Result where D: Decoder { - let rlp = decoder.as_rlp(); - Ok(BasicAccount { - nonce: rlp.val_at(0)?, - balance: rlp.val_at(1)?, - storage_root: rlp.val_at(2)?, - code_hash: rlp.val_at(3)?, - }) - } -} - /// Single account in the system. /// Keeps track of changes to the code and storage. /// The changes are applied in `commit_storage` and `commit_code` diff --git a/ethcore/src/state/mod.rs b/ethcore/src/state/mod.rs index 1dcc37732..c9730c1c3 100644 --- a/ethcore/src/state/mod.rs +++ b/ethcore/src/state/mod.rs @@ -37,7 +37,7 @@ use util::trie::recorder::{Recorder, BasicRecorder as TrieRecorder}; mod account; mod substate; -pub use self::account::{BasicAccount, Account}; +pub use self::account::Account; pub use self::substate::Substate; /// Used to return information about an `State::apply` operation. diff --git a/ethcore/src/types/basic_account.rs b/ethcore/src/types/basic_account.rs new file mode 100644 index 000000000..18adbc944 --- /dev/null +++ b/ethcore/src/types/basic_account.rs @@ -0,0 +1,55 @@ +// Copyright 2015, 2016 Parity Technologies (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +//! Basic account type -- the decoded RLP from the state trie. + +use rlp::*; +use util::{U256, H256}; + +/// Basic account type. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct BasicAccount { + /// Nonce of the account. + pub nonce: U256, + /// Balance of the account. + pub balance: U256, + /// Storage root of the account. + pub storage_root: H256, + /// Code hash of the account. + pub code_hash: H256, +} + +impl Encodable for BasicAccount { + fn rlp_append(&self, s: &mut RlpStream) { + s.begin_list(4) + .append(&self.nonce) + .append(&self.balance) + .append(&self.storage_root) + .append(&self.code_hash); + } +} + +impl Decodable for BasicAccount { + fn decode(decoder: &D) -> Result where D: Decoder { + let rlp = decoder.as_rlp(); + Ok(BasicAccount { + nonce: rlp.val_at(0)?, + balance: rlp.val_at(1)?, + storage_root: rlp.val_at(2)?, + code_hash: rlp.val_at(3)?, + }) + } +} diff --git a/ethcore/src/types/mod.rs.in b/ethcore/src/types/mod.rs.in index 567c6fff9..12081d1fb 100644 --- a/ethcore/src/types/mod.rs.in +++ b/ethcore/src/types/mod.rs.in @@ -37,3 +37,4 @@ pub mod mode; pub mod pruning_info; pub mod security_level; pub mod encoded; +pub mod basic_account; From 5d2cf22ef46bd376a171d0ee08417042e80e56d7 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Tue, 3 Jan 2017 17:05:27 +0100 Subject: [PATCH 3/3] use basic_account in snapshot --- ethcore/src/snapshot/account.rs | 293 +++++++++++--------------- ethcore/src/snapshot/mod.rs | 11 +- ethcore/src/snapshot/tests/helpers.rs | 8 +- ethcore/src/snapshot/tests/state.rs | 16 +- 4 files changed, 142 insertions(+), 186 deletions(-) diff --git a/ethcore/src/snapshot/account.rs b/ethcore/src/snapshot/account.rs index 0dfb72955..9d9aa0b9e 100644 --- a/ethcore/src/snapshot/account.rs +++ b/ethcore/src/snapshot/account.rs @@ -17,16 +17,17 @@ //! Account state encoding and decoding use account_db::{AccountDB, AccountDBMut}; +use basic_account::BasicAccount; use snapshot::Error; use util::{U256, FixedHash, H256, Bytes, HashDB, SHA3_EMPTY, SHA3_NULL_RLP}; use util::trie::{TrieDB, Trie}; -use rlp::{Rlp, RlpStream, Stream, UntrustedRlp, View}; +use rlp::{RlpStream, Stream, UntrustedRlp, View}; use std::collections::HashSet; // An empty account -- these are replaced with RLP null data for a space optimization. -const ACC_EMPTY: Account = Account { +const ACC_EMPTY: BasicAccount = BasicAccount { nonce: U256([0, 0, 0, 0]), balance: U256([0, 0, 0, 0]), storage_root: SHA3_NULL_RLP, @@ -59,165 +60,121 @@ impl CodeState { } } -// An alternate account structure from ::account::Account. -#[derive(PartialEq, Clone, Debug)] -pub struct Account { - nonce: U256, - balance: U256, - storage_root: H256, - code_hash: H256, +// walk the account's storage trie, returning an RLP item containing the +// account properties and the storage. +pub fn to_fat_rlp(acc: &BasicAccount, acct_db: &AccountDB, used_code: &mut HashSet) -> Result { + if acc == &ACC_EMPTY { + return Ok(::rlp::NULL_RLP.to_vec()); + } + + let db = TrieDB::new(acct_db, &acc.storage_root)?; + + let mut pairs = Vec::new(); + + for item in db.iter()? { + let (k, v) = item?; + pairs.push((k, v)); + } + + let mut stream = RlpStream::new_list(pairs.len()); + + for (k, v) in pairs { + stream.begin_list(2).append(&k).append(&&*v); + } + + let pairs_rlp = stream.out(); + + let mut account_stream = RlpStream::new_list(5); + account_stream.append(&acc.nonce) + .append(&acc.balance); + + // [has_code, code_hash]. + if acc.code_hash == SHA3_EMPTY { + account_stream.append(&CodeState::Empty.raw()).append_empty_data(); + } else if used_code.contains(&acc.code_hash) { + account_stream.append(&CodeState::Hash.raw()).append(&acc.code_hash); + } else { + match acct_db.get(&acc.code_hash) { + Some(c) => { + used_code.insert(acc.code_hash.clone()); + account_stream.append(&CodeState::Inline.raw()).append(&&*c); + } + None => { + warn!("code lookup failed during snapshot"); + account_stream.append(&false).append_empty_data(); + } + } + } + + account_stream.append_raw(&pairs_rlp, 1); + + Ok(account_stream.out()) } -impl Account { - // decode the account from rlp. - pub fn from_thin_rlp(rlp: &[u8]) -> Self { - let r: Rlp = Rlp::new(rlp); +// decode a fat rlp, and rebuild the storage trie as we go. +// returns the account structure along with its newly recovered code, +// if it exists. +pub fn from_fat_rlp( + acct_db: &mut AccountDBMut, + rlp: UntrustedRlp, +) -> Result<(BasicAccount, Option), Error> { + use util::{TrieDBMut, TrieMut}; - Account { - nonce: r.val_at(0), - balance: r.val_at(1), - storage_root: r.val_at(2), - code_hash: r.val_at(3), + // check for special case of empty account. + if rlp.is_empty() { + return Ok((ACC_EMPTY, None)); + } + + let nonce = rlp.val_at(0)?; + let balance = rlp.val_at(1)?; + let code_state: CodeState = { + let raw: u8 = rlp.val_at(2)?; + CodeState::from(raw)? + }; + + // load the code if it exists. + let (code_hash, new_code) = match code_state { + CodeState::Empty => (SHA3_EMPTY, None), + CodeState::Inline => { + let code: Bytes = rlp.val_at(3)?; + let code_hash = acct_db.insert(&code); + + (code_hash, Some(code)) + } + CodeState::Hash => { + let code_hash = rlp.val_at(3)?; + + (code_hash, None) + } + }; + + let mut storage_root = H256::zero(); + + { + let mut storage_trie = TrieDBMut::new(acct_db, &mut storage_root); + let pairs = rlp.at(4)?; + for pair_rlp in pairs.iter() { + let k: Bytes = pair_rlp.val_at(0)?; + let v: Bytes = pair_rlp.val_at(1)?; + + storage_trie.insert(&k, &v)?; } } - // encode the account to a standard rlp. - pub fn to_thin_rlp(&self) -> Bytes { - let mut stream = RlpStream::new_list(4); - stream - .append(&self.nonce) - .append(&self.balance) - .append(&self.storage_root) - .append(&self.code_hash); + let acc = BasicAccount { + nonce: nonce, + balance: balance, + storage_root: storage_root, + code_hash: code_hash, + }; - stream.out() - } - - // walk the account's storage trie, returning an RLP item containing the - // account properties and the storage. - pub fn to_fat_rlp(&self, acct_db: &AccountDB, used_code: &mut HashSet) -> Result { - if self == &ACC_EMPTY { - return Ok(::rlp::NULL_RLP.to_vec()); - } - - let db = TrieDB::new(acct_db, &self.storage_root)?; - - let mut pairs = Vec::new(); - - for item in db.iter()? { - let (k, v) = item?; - pairs.push((k, v)); - } - - let mut stream = RlpStream::new_list(pairs.len()); - - for (k, v) in pairs { - stream.begin_list(2).append(&k).append(&&*v); - } - - let pairs_rlp = stream.out(); - - let mut account_stream = RlpStream::new_list(5); - account_stream.append(&self.nonce) - .append(&self.balance); - - // [has_code, code_hash]. - if self.code_hash == SHA3_EMPTY { - account_stream.append(&CodeState::Empty.raw()).append_empty_data(); - } else if used_code.contains(&self.code_hash) { - account_stream.append(&CodeState::Hash.raw()).append(&self.code_hash); - } else { - match acct_db.get(&self.code_hash) { - Some(c) => { - used_code.insert(self.code_hash.clone()); - account_stream.append(&CodeState::Inline.raw()).append(&&*c); - } - None => { - warn!("code lookup failed during snapshot"); - account_stream.append(&false).append_empty_data(); - } - } - } - - account_stream.append_raw(&pairs_rlp, 1); - - Ok(account_stream.out()) - } - - // decode a fat rlp, and rebuild the storage trie as we go. - // returns the account structure along with its newly recovered code, - // if it exists. - pub fn from_fat_rlp( - acct_db: &mut AccountDBMut, - rlp: UntrustedRlp, - ) -> Result<(Self, Option), Error> { - use util::{TrieDBMut, TrieMut}; - - // check for special case of empty account. - if rlp.is_empty() { - return Ok((ACC_EMPTY, None)); - } - - let nonce = rlp.val_at(0)?; - let balance = rlp.val_at(1)?; - let code_state: CodeState = { - let raw: u8 = rlp.val_at(2)?; - CodeState::from(raw)? - }; - - // load the code if it exists. - let (code_hash, new_code) = match code_state { - CodeState::Empty => (SHA3_EMPTY, None), - CodeState::Inline => { - let code: Bytes = rlp.val_at(3)?; - let code_hash = acct_db.insert(&code); - - (code_hash, Some(code)) - } - CodeState::Hash => { - let code_hash = rlp.val_at(3)?; - - (code_hash, None) - } - }; - - let mut storage_root = H256::zero(); - - { - let mut storage_trie = TrieDBMut::new(acct_db, &mut storage_root); - let pairs = rlp.at(4)?; - for pair_rlp in pairs.iter() { - let k: Bytes = pair_rlp.val_at(0)?; - let v: Bytes = pair_rlp.val_at(1)?; - - storage_trie.insert(&k, &v)?; - } - } - - let acc = Account { - nonce: nonce, - balance: balance, - storage_root: storage_root, - code_hash: code_hash, - }; - - Ok((acc, new_code)) - } - - /// Get the account's code hash. - pub fn code_hash(&self) -> &H256 { - &self.code_hash - } - - #[cfg(test)] - pub fn storage_root_mut(&mut self) -> &mut H256 { - &mut self.storage_root - } + Ok((acc, new_code)) } #[cfg(test)] mod tests { use account_db::{AccountDB, AccountDBMut}; + use basic_account::BasicAccount; use tests::helpers::get_temp_state_db; use snapshot::tests::helpers::fill_storage; @@ -227,26 +184,26 @@ mod tests { use std::collections::HashSet; - use super::{ACC_EMPTY, Account}; + use super::{ACC_EMPTY, to_fat_rlp, from_fat_rlp}; #[test] fn encoding_basic() { let mut db = get_temp_state_db(); let addr = Address::random(); - let account = Account { + let account = BasicAccount { nonce: 50.into(), balance: 123456789.into(), storage_root: SHA3_NULL_RLP, code_hash: SHA3_EMPTY, }; - let thin_rlp = account.to_thin_rlp(); - assert_eq!(Account::from_thin_rlp(&thin_rlp), account); + let thin_rlp = ::rlp::encode(&account); + assert_eq!(::rlp::decode::(&thin_rlp), account); - let fat_rlp = account.to_fat_rlp(&AccountDB::new(db.as_hashdb(), &addr), &mut Default::default()).unwrap(); + let fat_rlp = to_fat_rlp(&account, &AccountDB::new(db.as_hashdb(), &addr), &mut Default::default()).unwrap(); let fat_rlp = UntrustedRlp::new(&fat_rlp); - assert_eq!(Account::from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &addr), fat_rlp).unwrap().0, account); + assert_eq!(from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &addr), fat_rlp).unwrap().0, account); } #[test] @@ -258,7 +215,7 @@ mod tests { let acct_db = AccountDBMut::new(db.as_hashdb_mut(), &addr); let mut root = SHA3_NULL_RLP; fill_storage(acct_db, &mut root, &mut H256::zero()); - Account { + BasicAccount { nonce: 25.into(), balance: 987654321.into(), storage_root: root, @@ -266,12 +223,12 @@ mod tests { } }; - let thin_rlp = account.to_thin_rlp(); - assert_eq!(Account::from_thin_rlp(&thin_rlp), account); + let thin_rlp = ::rlp::encode(&account); + assert_eq!(::rlp::decode::(&thin_rlp), account); - let fat_rlp = account.to_fat_rlp(&AccountDB::new(db.as_hashdb(), &addr), &mut Default::default()).unwrap(); + let fat_rlp = to_fat_rlp(&account, &AccountDB::new(db.as_hashdb(), &addr), &mut Default::default()).unwrap(); let fat_rlp = UntrustedRlp::new(&fat_rlp); - assert_eq!(Account::from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &addr), fat_rlp).unwrap().0, account); + assert_eq!(from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &addr), fat_rlp).unwrap().0, account); } #[test] @@ -291,14 +248,14 @@ mod tests { acct_db.emplace(code_hash.clone(), DBValue::from_slice(b"this is definitely code")); } - let account1 = Account { + let account1 = BasicAccount { nonce: 50.into(), balance: 123456789.into(), storage_root: SHA3_NULL_RLP, code_hash: code_hash, }; - let account2 = Account { + let account2 = BasicAccount { nonce: 400.into(), balance: 98765432123456789usize.into(), storage_root: SHA3_NULL_RLP, @@ -307,18 +264,18 @@ mod tests { let mut used_code = HashSet::new(); - let fat_rlp1 = account1.to_fat_rlp(&AccountDB::new(db.as_hashdb(), &addr1), &mut used_code).unwrap(); - let fat_rlp2 = account2.to_fat_rlp(&AccountDB::new(db.as_hashdb(), &addr2), &mut used_code).unwrap(); + let fat_rlp1 = to_fat_rlp(&account1, &AccountDB::new(db.as_hashdb(), &addr1), &mut used_code).unwrap(); + let fat_rlp2 = to_fat_rlp(&account2, &AccountDB::new(db.as_hashdb(), &addr2), &mut used_code).unwrap(); assert_eq!(used_code.len(), 1); let fat_rlp1 = UntrustedRlp::new(&fat_rlp1); let fat_rlp2 = UntrustedRlp::new(&fat_rlp2); - let (acc, maybe_code) = Account::from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &addr2), fat_rlp2).unwrap(); + let (acc, maybe_code) = from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &addr2), fat_rlp2).unwrap(); assert!(maybe_code.is_none()); assert_eq!(acc, account2); - let (acc, maybe_code) = Account::from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &addr1), fat_rlp1).unwrap(); + let (acc, maybe_code) = from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &addr1), fat_rlp1).unwrap(); assert_eq!(maybe_code, Some(b"this is definitely code".to_vec())); assert_eq!(acc, account1); } @@ -328,7 +285,7 @@ mod tests { let mut db = get_temp_state_db(); let mut used_code = HashSet::new(); - assert_eq!(ACC_EMPTY.to_fat_rlp(&AccountDB::new(db.as_hashdb(), &Address::default()), &mut used_code).unwrap(), ::rlp::NULL_RLP.to_vec()); - assert_eq!(Account::from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &Address::default()), UntrustedRlp::new(&::rlp::NULL_RLP)).unwrap(), (ACC_EMPTY, None)); + assert_eq!(to_fat_rlp(&ACC_EMPTY, &AccountDB::new(db.as_hashdb(), &Address::default()), &mut used_code).unwrap(), ::rlp::NULL_RLP.to_vec()); + assert_eq!(from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &Address::default()), UntrustedRlp::new(&::rlp::NULL_RLP)).unwrap(), (ACC_EMPTY, None)); } } diff --git a/ethcore/src/snapshot/mod.rs b/ethcore/src/snapshot/mod.rs index 56fd09200..add5029b2 100644 --- a/ethcore/src/snapshot/mod.rs +++ b/ethcore/src/snapshot/mod.rs @@ -40,7 +40,6 @@ use util::sha3::SHA3_NULL_RLP; use rlp::{RlpStream, Stream, UntrustedRlp, View}; use bloom_journal::Bloom; -use self::account::Account; use self::block::AbridgedBlock; use self::io::SnapshotWriter; @@ -368,12 +367,12 @@ pub fn chunk_state<'a>(db: &HashDB, root: &H256, writer: &Mutex status.new_code.push((code_hash, code, hash)), @@ -534,7 +533,7 @@ fn rebuild_accounts( } } - acc.to_thin_rlp() + ::rlp::encode(&acc).to_vec() }; *out = (hash, thin_rlp); diff --git a/ethcore/src/snapshot/tests/helpers.rs b/ethcore/src/snapshot/tests/helpers.rs index 164f99121..8d7d1bb97 100644 --- a/ethcore/src/snapshot/tests/helpers.rs +++ b/ethcore/src/snapshot/tests/helpers.rs @@ -17,9 +17,9 @@ //! Snapshot test helpers. These are used to build blockchains and state tries //! which can be queried before and after a full snapshot/restore cycle. +use basic_account::BasicAccount; use account_db::AccountDBMut; use rand::Rng; -use snapshot::account::Account; use util::DBValue; use util::hash::{FixedHash, H256}; @@ -64,10 +64,10 @@ impl StateProducer { // sweep once to alter storage tries. for &mut (ref mut address_hash, ref mut account_data) in &mut accounts_to_modify { - let mut account = Account::from_thin_rlp(&*account_data); + let mut account: BasicAccount = ::rlp::decode(&*account_data); let acct_db = AccountDBMut::from_hash(db, *address_hash); - fill_storage(acct_db, account.storage_root_mut(), &mut self.storage_seed); - *account_data = DBValue::from_vec(account.to_thin_rlp()); + fill_storage(acct_db, &mut account.storage_root, &mut self.storage_seed); + *account_data = DBValue::from_vec(::rlp::encode(&account).to_vec()); } // sweep again to alter account trie. diff --git a/ethcore/src/snapshot/tests/state.rs b/ethcore/src/snapshot/tests/state.rs index 380e9fb0d..5fcc5a52c 100644 --- a/ethcore/src/snapshot/tests/state.rs +++ b/ethcore/src/snapshot/tests/state.rs @@ -16,8 +16,9 @@ //! State snapshotting tests. +use basic_account::BasicAccount; +use snapshot::account; use snapshot::{chunk_state, Error as SnapshotError, Progress, StateRebuilder}; -use snapshot::account::Account; use snapshot::io::{PackedReader, PackedWriter, SnapshotReader, SnapshotWriter}; use super::helpers::{compare_dbs, StateProducer}; @@ -113,22 +114,21 @@ fn get_code_from_prev_chunk() { // first one will have code inlined, // second will just have its hash. let thin_rlp = acc_stream.out(); - let acc1 = Account::from_thin_rlp(&thin_rlp); - let acc2 = Account::from_thin_rlp(&thin_rlp); + let acc: BasicAccount = ::rlp::decode(&thin_rlp); - let mut make_chunk = |acc: Account, hash| { + let mut make_chunk = |acc, hash| { let mut db = MemoryDB::new(); AccountDBMut::from_hash(&mut db, hash).insert(&code[..]); - let fat_rlp = acc.to_fat_rlp(&AccountDB::from_hash(&db, hash), &mut used_code).unwrap(); + let fat_rlp = account::to_fat_rlp(&acc, &AccountDB::from_hash(&db, hash), &mut used_code).unwrap(); let mut stream = RlpStream::new_list(1); stream.begin_list(2).append(&hash).append_raw(&fat_rlp, 1); stream.out() }; - let chunk1 = make_chunk(acc1, h1); - let chunk2 = make_chunk(acc2, h2); + let chunk1 = make_chunk(acc.clone(), h1); + let chunk2 = make_chunk(acc, h2); let db_path = RandomTempPath::create_dir(); let db_cfg = DatabaseConfig::with_columns(::db::NUM_COLUMNS); @@ -190,4 +190,4 @@ fn checks_flag() { } } } -} \ No newline at end of file +}