openethereum/src/state.rs

150 lines
3.8 KiB
Rust
Raw Normal View History

2015-12-13 21:36:17 +01:00
use std::collections::HashMap;
use util::hash::*;
use util::hashdb::*;
use util::overlaydb::*;
use util::trie::*;
use util::rlp::*;
use util::uint::*;
use account::Account;
2015-12-13 23:12:22 +01:00
/// Representation of the entire state of all accounts in the system.
2015-12-13 21:36:17 +01:00
pub struct State {
db: OverlayDB,
root: H256,
2015-12-13 23:12:22 +01:00
cache: HashMap<Address, Option<Account>>,
2015-12-13 21:36:17 +01:00
_account_start_nonce: U256,
}
impl State {
/// Creates new state with empty state root
pub fn new(mut db: OverlayDB, account_start_nonce: U256) -> State {
let mut root = H256::new();
{
// init trie and reset root too null
let _ = TrieDB::new(&mut db, &mut root);
}
State {
db: db,
root: root,
2015-12-13 23:12:22 +01:00
cache: HashMap::new(),
2015-12-13 21:36:17 +01:00
_account_start_nonce: account_start_nonce,
}
}
/// Creates new state with existing state root
pub fn new_existing(mut db: OverlayDB, mut root: H256, account_start_nonce: U256) -> State {
{
// trie should panic! if root does not exist
let _ = TrieDB::new_existing(&mut db, &mut root);
}
State {
db: db,
root: root,
2015-12-13 23:12:22 +01:00
cache: HashMap::new(),
2015-12-13 21:36:17 +01:00
_account_start_nonce: account_start_nonce,
}
}
/// Create temporary state object
pub fn new_temp() -> State {
Self::new(OverlayDB::new_temp(), U256::from(0u8))
}
/// Return reference to root
pub fn root(&self) -> &H256 {
&self.root
}
2015-12-13 23:12:22 +01:00
/// Expose the underlying database; good to use for calling `state.db().commit()`.
pub fn db(&mut self) -> &mut OverlayDB {
&mut self.db
2015-12-13 21:36:17 +01:00
}
/// Commit accounts to TrieDB. This is similar to cpp-ethereum's dev::eth::commit.
/// `accounts` is mutable because we may need to commit the code or storage and record that.
2015-12-13 23:12:22 +01:00
pub fn commit_into(db: &mut HashDB, mut root: H256, accounts: &mut HashMap<Address, Option<Account>>) -> H256 {
2015-12-13 21:36:17 +01:00
// first, commit the sub trees.
2015-12-13 23:12:22 +01:00
// TODO: is this necessary or can we dispense with the `ref mut a` for just `a`?
for (_, ref mut a) in accounts.iter_mut() {
match a {
&mut&mut Some(ref mut account) => {
account.commit_storage(db);
account.commit_code(db);
}
&mut&mut None => {}
}
2015-12-13 21:36:17 +01:00
}
{
let mut trie = TrieDB::new_existing(db, &mut root);
2015-12-13 23:12:22 +01:00
for (address, ref a) in accounts.iter() {
match a {
&&Some(ref account) => trie.insert(address, &account.rlp()),
&&None => trie.remove(address),
}
2015-12-13 21:36:17 +01:00
}
}
root
}
2015-12-13 23:12:22 +01:00
/// Commits our cached account changes into the trie.
pub fn commit(&mut self) {
2015-12-13 21:49:40 +01:00
let r = self.root.clone(); // would prefer not to do this, really.
2015-12-13 23:12:22 +01:00
self.root = Self::commit_into(&mut self.db, r, &mut self.cache);
2015-12-13 21:36:17 +01:00
}
/// Pull account `a` in our cache from the trie DB. `require_code` requires that the code be cached, too.
/// `force_create` creates a new, empty basic account if there is not currently an active account.
fn ensure_cached(&mut self, a: &Address, require_code: bool, force_create: bool) {
if self.cache.get(a).is_none() {
// load from trie.
self.cache.insert(a.clone(), TrieDB::new(&mut self.db, &mut self.root).at(&a).map(|rlp| Account::from_rlp(rlp)));
}
if self.cache.get(a).unwrap().is_none() {
if !force_create { return; }
// create a new account
self.cache.insert(a.clone(), Some(Account::new_basic(U256::from(0u8))));
}
if require_code {
if let &mut Some(ref mut account) = self.cache.get_mut(a).unwrap() {
account.ensure_cached(&self.db);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use util::hash::*;
use util::trie::*;
use util::rlp::*;
use std::str::FromStr;
#[test]
fn playpen() {
2015-12-13 21:36:17 +01:00
}
#[test]
fn ensure_cached() {
let mut s = State::new_temp();
let a = Address::from_str("0000000000000000000000000000000000000000").unwrap();
s.ensure_cached(&a, false, true);
s.commit();
assert_eq!(s.root().hex(), "ec68b85fa2e0526dc0e821a5b33135459114f19173ce0479f5c09b21cc25b9a4");
}
#[test]
fn create_empty() {
let mut s = State::new_temp();
s.commit();
assert_eq!(s.root().hex(), "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421");
}
}