State::exists, docs and tests.

This commit is contained in:
Gav Wood 2016-01-12 12:34:14 +01:00
parent cfb7d9a7c3
commit c6ec6e4aef

View File

@ -76,6 +76,11 @@ impl State {
self.cache.borrow_mut().insert(account.clone(), None);
}
/// Determine whether an account exists.
pub fn exists(&self, a: &Address) -> bool {
self.cache.borrow().get(&a).unwrap_or(&None).is_some() || SecTrieDB::new(&self.db, &self.root).contains(&a)
}
/// Get the balance of account `a`.
pub fn balance(&self, a: &Address) -> U256 {
self.get(a, false).as_ref().map(|account| account.balance().clone()).unwrap_or(U256::from(0u8))
@ -287,9 +292,12 @@ fn get_from_database() {
fn remove() {
let a = Address::zero();
let mut s = State::new_temp();
assert_eq!(s.exists(&a), false);
s.inc_nonce(&a);
assert_eq!(s.exists(&a), true);
assert_eq!(s.nonce(&a), U256::from(1u64));
s.kill_account(&a);
assert_eq!(s.exists(&a), false);
assert_eq!(s.nonce(&a), U256::from(0u64));
}
@ -300,20 +308,24 @@ fn remove_from_database() {
let mut s = State::new_temp();
s.inc_nonce(&a);
s.commit();
assert_eq!(s.exists(&a), true);
assert_eq!(s.nonce(&a), U256::from(1u64));
s.drop()
};
let (r, db) = {
let mut s = State::from_existing(db, r, U256::from(0u8));
assert_eq!(s.exists(&a), true);
assert_eq!(s.nonce(&a), U256::from(1u64));
s.kill_account(&a);
s.commit();
assert_eq!(s.exists(&a), false);
assert_eq!(s.nonce(&a), U256::from(0u64));
s.drop()
};
let s = State::from_existing(db, r, U256::from(0u8));
assert_eq!(s.exists(&a), false);
assert_eq!(s.nonce(&a), U256::from(0u64));
}