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.