From 17435099f192766d9d0e0b811b43cef785836987 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Tue, 15 Dec 2015 13:09:50 +0100 Subject: [PATCH 1/2] Additional tests. --- src/account.rs | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/account.rs b/src/account.rs index 400273fab..346da23c7 100644 --- a/src/account.rs +++ b/src/account.rs @@ -89,12 +89,15 @@ impl Account { /// return the balance associated with this account. pub fn balance(&self) -> &U256 { &self.balance } + /// return the nonce associated with this account. pub fn nonce(&self) -> &U256 { &self.nonce } + /// return the code hash associated with this account. pub fn code_hash(&self) -> H256 { self.code_hash.clone().unwrap_or(SHA3_EMPTY) } + /// returns the account's code. If `None` then the code cache isn't available - /// get someone who knows to call `note_code`. pub fn code(&self) -> Option<&[u8]> { @@ -119,8 +122,10 @@ impl Account { /// return the storage root associated with this account. pub fn base_root(&self) -> &H256 { &self.storage_root } + /// return the storage root associated with this account or None if it has been altered via the overlay. pub fn storage_root(&self) -> Option<&H256> { if self.storage_overlay.is_empty() {Some(&self.storage_root)} else {None} } + /// rturn the storage overlay. pub fn storage_overlay(&self) -> &HashMap { &self.storage_overlay } @@ -133,7 +138,6 @@ impl Account { /// Increment the nonce of the account by one. pub fn sub_balance(&mut self, x: &U256) { self.balance = self.balance - *x; } - /// Commit the `storage_overlay` to the backing DB and update `storage_root`. pub fn commit_storage(&mut self, db: &mut HashDB) { let mut t = TrieDB::new(db, &mut self.storage_root); @@ -163,10 +167,43 @@ impl Account { } } +#[cfg(test)] +mod tests { + +use super::*; +use std::collections::HashMap; +use util::hash::*; +use util::bytes::*; +use util::trie::*; +use util::rlp::*; +use util::uint::*; +use util::overlaydb::*; + #[test] fn playpen() { } +#[test] +fn commit_storage() { + let mut a = Account::new_contract(U256::from(69u8)); + let mut db = OverlayDB::new_temp(); + //a.set_code(vec![0x55, 0x44, 0xffu8]); + a.set_storage(From::from(&U256::from(0x00u64)), From::from(&U256::from(0x1234u64))); + a.commit_storage(&mut db); + assert_eq!(a.storage_root().unwrap().hex(), "3541f181d6dad5c504371884684d08c29a8bad04926f8ceddf5e279dbc3cc769"); +} + +#[test] +fn rlpio() { + let a = Account::new(U256::from(69u8), U256::from(0u8), HashMap::new(), Bytes::new()); + assert_eq!(a.rlp().to_hex(), "f8448045a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"); + let b = Account::from_rlp(&a.rlp()); + assert_eq!(a.balance(), b.balance()); + assert_eq!(a.nonce(), b.nonce()); + assert_eq!(a.code_hash(), b.code_hash()); + assert_eq!(a.storage_root(), b.storage_root()); +} + #[test] fn new_account() { let a = Account::new(U256::from(69u8), U256::from(0u8), HashMap::new(), Bytes::new()); @@ -182,3 +219,5 @@ fn create_account() { let a = Account::new(U256::from(69u8), U256::from(0u8), HashMap::new(), Bytes::new()); assert_eq!(a.rlp().to_hex(), "f8448045a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"); } + +} \ No newline at end of file From 7fb17a39ce8367319db9071916e5a6e9b8a89ceb Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Tue, 15 Dec 2015 13:17:59 +0100 Subject: [PATCH 2/2] Additional tests for Account set_storage and set_code. --- src/account.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/account.rs b/src/account.rs index 346da23c7..f9e4d9097 100644 --- a/src/account.rs +++ b/src/account.rs @@ -187,16 +187,25 @@ fn playpen() { fn commit_storage() { let mut a = Account::new_contract(U256::from(69u8)); let mut db = OverlayDB::new_temp(); - //a.set_code(vec![0x55, 0x44, 0xffu8]); a.set_storage(From::from(&U256::from(0x00u64)), From::from(&U256::from(0x1234u64))); + assert_eq!(a.storage_root(), None); a.commit_storage(&mut db); assert_eq!(a.storage_root().unwrap().hex(), "3541f181d6dad5c504371884684d08c29a8bad04926f8ceddf5e279dbc3cc769"); } +#[test] +fn commit_code() { + let mut a = Account::new_contract(U256::from(69u8)); + let mut db = OverlayDB::new_temp(); + a.set_code(vec![0x55, 0x44, 0xffu8]); + assert_eq!(a.code_hash(), SHA3_EMPTY); + a.commit_code(&mut db); + assert_eq!(a.code_hash().hex(), "af231e631776a517ca23125370d542873eca1fb4d613ed9b5d5335a46ae5b7eb"); +} + #[test] fn rlpio() { let a = Account::new(U256::from(69u8), U256::from(0u8), HashMap::new(), Bytes::new()); - assert_eq!(a.rlp().to_hex(), "f8448045a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"); let b = Account::from_rlp(&a.rlp()); assert_eq!(a.balance(), b.balance()); assert_eq!(a.nonce(), b.nonce());