openethereum/ethcore/src/state.rs

542 lines
17 KiB
Rust
Raw Normal View History

2016-02-05 13:40:41 +01:00
// Copyright 2015, 2016 Ethcore (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 <http://www.gnu.org/licenses/>.
2016-01-10 14:05:39 +01:00
use common::*;
use engine::Engine;
2016-01-11 17:37:22 +01:00
use executive::Executive;
2016-02-05 01:49:06 +01:00
use account_db::*;
#[cfg(test)]
#[cfg(feature = "json-tests")]
use pod_account::*;
#[cfg(test)]
#[cfg(feature = "json-tests")]
use pod_state::PodState;
2016-01-25 23:37:49 +01:00
//use state_diff::*; // TODO: uncomment once to_pod() works correctly.
2015-12-19 22:15:22 +01:00
2016-02-03 13:20:32 +01:00
/// Result type for the execution ("application") of a transaction.
2016-01-11 20:47:19 +01:00
pub type ApplyResult = Result<Receipt, Error>;
2015-12-13 21:36:17 +01:00
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 {
2016-01-18 13:54:46 +01:00
db: JournalDB,
2015-12-13 21:36:17 +01:00
root: H256,
2015-12-19 22:15:22 +01:00
cache: RefCell<HashMap<Address, Option<Account>>>,
2016-02-03 19:34:51 +01:00
snapshots: RefCell<Vec<HashMap<Address, Option<Option<Account>>>>>,
2015-12-19 22:15:22 +01:00
account_start_nonce: U256,
2015-12-13 21:36:17 +01:00
}
impl State {
/// Creates new state with empty state root
#[cfg(test)]
2016-01-18 13:54:46 +01:00
pub fn new(mut db: JournalDB, account_start_nonce: U256) -> State {
2015-12-13 21:36:17 +01:00
let mut root = H256::new();
{
// init trie and reset root too null
let _ = SecTrieDBMut::new(&mut db, &mut root);
2015-12-13 21:36:17 +01:00
}
State {
db: db,
root: root,
2015-12-19 22:15:22 +01:00
cache: RefCell::new(HashMap::new()),
2016-02-03 19:34:51 +01:00
snapshots: RefCell::new(Vec::new()),
2015-12-19 22:15:22 +01:00
account_start_nonce: account_start_nonce,
2015-12-13 21:36:17 +01:00
}
}
/// Creates new state with existing state root
2016-01-18 13:54:46 +01:00
pub fn from_existing(db: JournalDB, root: H256, account_start_nonce: U256) -> State {
2015-12-13 21:36:17 +01:00
{
// trie should panic! if root does not exist
let _ = SecTrieDB::new(&db, &root);
2015-12-13 21:36:17 +01:00
}
State {
db: db,
root: root,
2015-12-19 22:15:22 +01:00
cache: RefCell::new(HashMap::new()),
2016-02-03 19:34:51 +01:00
snapshots: RefCell::new(Vec::new()),
2015-12-19 22:15:22 +01:00
account_start_nonce: account_start_nonce,
2015-12-13 21:36:17 +01:00
}
}
2016-02-03 19:34:51 +01:00
/// Create a recoverable snaphot of this state
pub fn snapshot(&mut self) {
self.snapshots.borrow_mut().push(HashMap::new());
}
2016-02-05 02:08:17 +01:00
/// Merge last snapshot with previous
2016-02-03 19:34:51 +01:00
pub fn clear_snapshot(&mut self) {
// merge with previous snapshot
let last = self.snapshots.borrow_mut().pop();
if let Some(mut snapshot) = last {
if let Some(ref mut prev) = self.snapshots.borrow_mut().last_mut() {
for (k, v) in snapshot.drain() {
prev.entry(k).or_insert(v);
}
}
}
}
2016-02-05 02:08:17 +01:00
/// Revert to snapshot
2016-02-03 19:34:51 +01:00
pub fn revert_snapshot(&mut self) {
if let Some(mut snapshot) = self.snapshots.borrow_mut().pop() {
for (k, v) in snapshot.drain() {
match v {
Some(v) => {
self.cache.borrow_mut().insert(k, v);
},
None => {
self.cache.borrow_mut().remove(&k);
}
}
}
}
}
fn insert_cache(&self, address: &Address, account: Option<Account>) {
if let Some(ref mut snapshot) = self.snapshots.borrow_mut().last_mut() {
if !snapshot.contains_key(&address) {
snapshot.insert(address.clone(), self.cache.borrow_mut().insert(address.clone(), account));
return;
}
}
self.cache.borrow_mut().insert(address.clone(), account);
}
fn note_cache(&self, address: &Address) {
if let Some(ref mut snapshot) = self.snapshots.borrow_mut().last_mut() {
if !snapshot.contains_key(&address) {
snapshot.insert(address.clone(), self.cache.borrow().get(address).cloned());
}
}
}
/// Destroy the current object and return root and database.
2016-01-18 13:54:46 +01:00
pub fn drop(self) -> (H256, JournalDB) {
2015-12-19 19:03:42 +01:00
(self.root, self.db)
}
2015-12-19 22:15:22 +01:00
/// Return reference to root
pub fn root(&self) -> &H256 {
&self.root
}
2016-01-09 14:19:35 +01:00
/// Create a new contract at address `contract`. If there is already an account at the address
/// it will have its code reset, ready for `init_code()`.
pub fn new_contract(&mut self, contract: &Address, balance: U256) {
2016-02-03 19:34:51 +01:00
self.insert_cache(&contract, Some(Account::new_contract(balance)));
2016-01-09 14:19:35 +01:00
}
/// Remove an existing account.
pub fn kill_account(&mut self, account: &Address) {
2016-02-03 19:34:51 +01:00
self.insert_cache(account, None);
}
2016-01-12 12:34:14 +01:00
/// 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`.
2015-12-19 22:15:22 +01:00
pub fn balance(&self, a: &Address) -> U256 {
2016-02-14 12:54:27 +01:00
self.get(a, false).as_ref().map_or(U256::zero(), |account| *account.balance())
}
2015-12-19 22:15:22 +01:00
/// Get the nonce of account `a`.
pub fn nonce(&self, a: &Address) -> U256 {
2016-02-14 12:54:27 +01:00
self.get(a, false).as_ref().map_or(U256::zero(), |account| *account.nonce())
2015-12-19 22:15:22 +01:00
}
2016-02-05 13:06:40 +01:00
/// Mutate storage of account `address` so that it is `value` for `key`.
2016-02-04 02:40:35 +01:00
pub fn storage_at(&self, address: &Address, key: &H256) -> H256 {
2016-02-19 15:18:20 +01:00
self.get(address, false).as_ref().map_or(H256::new(), |a|a.storage_at(&AccountDB::new(&self.db, address), key))
2015-12-19 22:15:22 +01:00
}
/// Mutate storage of account `a` so that it is `value` for `key`.
2016-01-16 17:17:43 +01:00
pub fn code(&self, a: &Address) -> Option<Bytes> {
2016-01-17 15:56:09 +01:00
self.get(a, true).as_ref().map_or(None, |a|a.code().map(|x|x.to_vec()))
2015-12-19 22:15:22 +01:00
}
/// Add `incr` to the balance of account `a`.
pub fn add_balance(&mut self, a: &Address, incr: &U256) {
2016-01-16 01:48:38 +01:00
let old = self.balance(a);
2016-01-15 22:55:04 +01:00
self.require(a, false).add_balance(incr);
2016-01-16 01:48:38 +01:00
trace!("state: add_balance({}, {}): {} -> {}\n", a, incr, old, self.balance(a));
}
/// Subtract `decr` from the balance of account `a`.
pub fn sub_balance(&mut self, a: &Address, decr: &U256) {
2016-01-16 01:48:38 +01:00
let old = self.balance(a);
2016-01-15 22:55:04 +01:00
self.require(a, false).sub_balance(decr);
2016-01-16 01:48:38 +01:00
trace!("state: sub_balance({}, {}): {} -> {}\n", a, decr, old, self.balance(a));
}
/// Subtracts `by` from the balance of `from` and adds it to that of `to`.
pub fn transfer_balance(&mut self, from: &Address, to: &Address, by: &U256) {
self.sub_balance(from, by);
self.add_balance(to, by);
}
/// Increment the nonce of account `a` by 1.
pub fn inc_nonce(&mut self, a: &Address) {
self.require(a, false).inc_nonce()
}
/// Mutate storage of account `a` so that it is `value` for `key`.
2015-12-19 22:15:22 +01:00
pub fn set_storage(&mut self, a: &Address, key: H256, value: H256) {
2016-01-14 23:52:26 +01:00
self.require(a, false).set_storage(key, value)
}
/// Initialise the code of account `a` so that it is `value` for `key`.
/// NOTE: Account should have been created with `new_contract`.
pub fn init_code(&mut self, a: &Address, code: Bytes) {
2016-01-09 14:19:35 +01:00
self.require_or_from(a, true, || Account::new_contract(U256::from(0u8)), |_|{}).init_code(code);
}
/// Execute a given transaction.
/// This will change the state accordingly.
2016-02-04 23:48:29 +01:00
pub fn apply(&mut self, env_info: &EnvInfo, engine: &Engine, t: &SignedTransaction) -> ApplyResult {
2016-01-25 23:24:51 +01:00
// let old = self.to_pod();
2016-01-15 18:56:28 +01:00
2016-01-11 17:37:22 +01:00
let e = try!(Executive::new(self, env_info, engine).transact(t));
2016-01-15 18:56:28 +01:00
2016-01-25 23:37:49 +01:00
// TODO uncomment once to_pod() works correctly.
2016-01-25 23:24:51 +01:00
// trace!("Applied transaction. Diff:\n{}\n", StateDiff::diff_pod(&old, &self.to_pod()));
2016-01-11 17:37:22 +01:00
self.commit();
2016-01-15 02:52:37 +01:00
let receipt = Receipt::new(self.root().clone(), e.cumulative_gas_used, e.logs);
2016-01-25 23:24:51 +01:00
// trace!("Transaction receipt: {:?}", receipt);
2016-01-15 02:52:37 +01:00
Ok(receipt)
}
/// Commit accounts to SecTrieDBMut. 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.
2016-02-19 12:19:43 +01:00
#[cfg_attr(feature="dev", allow(match_ref_pats))]
2016-01-15 04:02:06 +01:00
pub fn commit_into(db: &mut HashDB, root: &mut H256, accounts: &mut HashMap<Address, Option<Account>>) {
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`?
2016-02-04 02:40:35 +01:00
for (address, ref mut a) in accounts.iter_mut() {
2015-12-13 23:12:22 +01:00
match a {
&mut&mut Some(ref mut account) => {
2016-02-04 02:40:35 +01:00
let mut account_db = AccountDBMut::new(db, address);
account.commit_storage(&mut account_db);
account.commit_code(&mut account_db);
2015-12-13 23:12:22 +01:00
}
&mut&mut None => {}
}
2015-12-13 21:36:17 +01:00
}
{
2016-01-15 04:02:06 +01:00
let mut trie = SecTrieDBMut::from_existing(db, root);
2015-12-13 23:12:22 +01:00
for (address, ref a) in accounts.iter() {
2016-01-17 15:56:09 +01:00
match **a {
Some(ref account) => trie.insert(address, &account.rlp()),
None => trie.remove(address),
2015-12-13 23:12:22 +01:00
}
2015-12-13 21:36:17 +01:00
}
}
}
2015-12-13 23:12:22 +01:00
/// Commits our cached account changes into the trie.
pub fn commit(&mut self) {
2016-02-03 19:34:51 +01:00
assert!(self.snapshots.borrow().is_empty());
2016-01-15 04:02:06 +01:00
Self::commit_into(&mut self.db, &mut self.root, self.cache.borrow_mut().deref_mut());
2015-12-13 21:36:17 +01:00
}
#[cfg(test)]
#[cfg(feature = "json-tests")]
/// Populate the state from `accounts`.
2016-01-14 12:27:35 +01:00
pub fn populate_from(&mut self, accounts: PodState) {
2016-02-03 19:34:51 +01:00
assert!(self.snapshots.borrow().is_empty());
2016-01-14 12:27:35 +01:00
for (add, acc) in accounts.drain().into_iter() {
self.cache.borrow_mut().insert(add, Some(Account::from_pod(acc)));
}
}
#[cfg(test)]
#[cfg(feature = "json-tests")]
/// Populate a PodAccount map from this state.
2016-01-14 12:27:35 +01:00
pub fn to_pod(&self) -> PodState {
2016-02-03 19:34:51 +01:00
assert!(self.snapshots.borrow().is_empty());
// TODO: handle database rather than just the cache.
2016-01-25 18:56:36 +01:00
PodState::from(self.cache.borrow().iter().fold(BTreeMap::new(), |mut m, (add, opt)| {
2016-01-17 15:56:09 +01:00
if let Some(ref acc) = *opt {
m.insert(add.clone(), PodAccount::from_account(acc));
}
m
2016-01-14 12:27:35 +01:00
}))
}
/// Pull account `a` in our cache from the trie DB and return it.
/// `require_code` requires that the code be cached, too.
2016-02-19 00:06:06 +01:00
fn get<'a>(&'a self, a: &Address, require_code: bool) -> &'a Option<Account> {
2016-02-03 19:34:51 +01:00
let have_key = self.cache.borrow().contains_key(a);
if !have_key {
2016-02-05 13:06:40 +01:00
self.insert_cache(a, SecTrieDB::new(&self.db, &self.root).get(&a).map(Account::from_rlp))
2016-02-03 19:34:51 +01:00
}
if require_code {
2015-12-19 22:15:22 +01:00
if let Some(ref mut account) = self.cache.borrow_mut().get_mut(a).unwrap().as_mut() {
2016-02-04 02:40:35 +01:00
account.cache_code(&AccountDB::new(&self.db, a));
}
}
2016-02-19 00:06:06 +01:00
unsafe { ::std::mem::transmute(self.cache.borrow().get(a).unwrap()) }
}
/// Pull account `a` in our cache from the trie DB. `require_code` requires that the code be cached, too.
2016-02-19 00:06:06 +01:00
fn require<'a>(&'a self, a: &Address, require_code: bool) -> &'a mut Account {
2016-01-09 14:19:35 +01:00
self.require_or_from(a, require_code, || Account::new_basic(U256::from(0u8), self.account_start_nonce), |_|{})
}
/// Pull account `a` in our cache from the trie DB. `require_code` requires that the code be cached, too.
2016-01-09 14:19:35 +01:00
/// If it doesn't exist, make account equal the evaluation of `default`.
2016-02-19 00:06:06 +01:00
fn require_or_from<'a, F: FnOnce() -> Account, G: FnOnce(&mut Account)>(&self, a: &Address, require_code: bool, default: F, not_default: G) -> &'a mut Account {
2016-02-03 19:34:51 +01:00
let have_key = self.cache.borrow().contains_key(a);
if !have_key {
2016-02-05 13:06:40 +01:00
self.insert_cache(a, SecTrieDB::new(&self.db, &self.root).get(&a).map(Account::from_rlp))
2016-02-03 19:34:51 +01:00
} else {
self.note_cache(a);
}
2016-01-09 14:19:35 +01:00
let preexists = self.cache.borrow().get(a).unwrap().is_none();
if preexists {
2015-12-19 22:15:22 +01:00
self.cache.borrow_mut().insert(a.clone(), Some(default()));
2016-01-09 14:19:35 +01:00
} else {
not_default(self.cache.borrow_mut().get_mut(a).unwrap().as_mut().unwrap());
}
2016-02-19 00:06:06 +01:00
unsafe { ::std::mem::transmute(self.cache.borrow_mut().get_mut(a).unwrap().as_mut().map(|account| {
if require_code {
2016-02-04 02:40:35 +01:00
account.cache_code(&AccountDB::new(&self.db, a));
}
account
2016-02-19 00:06:06 +01:00
}).unwrap()) }
}
}
impl fmt::Debug for State {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.cache.borrow())
2016-01-11 14:47:50 +01:00
}
}
#[cfg(test)]
mod tests {
use super::*;
use util::hash::*;
use util::trie::*;
use util::rlp::*;
use util::uint::*;
use account::*;
use tests::helpers::*;
2016-02-19 15:18:20 +01:00
use devtools::*;
#[test]
fn code_from_database() {
let a = Address::zero();
let temp = RandomTempPath::new();
let (root, db) = {
let mut state = get_temp_state_in(temp.as_path());
state.require_or_from(&a, false, ||Account::new_contract(U256::from(42u32)), |_|{});
state.init_code(&a, vec![1, 2, 3]);
assert_eq!(state.code(&a), Some([1u8, 2, 3].to_vec()));
state.commit();
assert_eq!(state.code(&a), Some([1u8, 2, 3].to_vec()));
state.drop()
};
let state = State::from_existing(db, root, U256::from(0u8));
assert_eq!(state.code(&a), Some([1u8, 2, 3].to_vec()));
}
#[test]
fn storage_at_from_database() {
let a = Address::zero();
let temp = RandomTempPath::new();
let (root, db) = {
let mut state = get_temp_state_in(temp.as_path());
state.set_storage(&a, H256::from(&U256::from(01u64)), H256::from(&U256::from(69u64)));
state.commit();
state.drop()
};
let s = State::from_existing(db, root, U256::from(0u8));
assert_eq!(s.storage_at(&a, &H256::from(&U256::from(01u64))), H256::from(&U256::from(69u64)));
}
#[test]
2015-12-16 20:11:37 +01:00
fn get_from_database() {
let a = Address::zero();
let temp = RandomTempPath::new();
let (root, db) = {
let mut state = get_temp_state_in(temp.as_path());
state.inc_nonce(&a);
state.add_balance(&a, &U256::from(69u64));
state.commit();
assert_eq!(state.balance(&a), U256::from(69u64));
state.drop()
};
let state = State::from_existing(db, root, U256::from(0u8));
assert_eq!(state.balance(&a), U256::from(69u64));
assert_eq!(state.nonce(&a), U256::from(1u64));
}
#[test]
fn remove() {
let a = Address::zero();
let mut state_result = get_temp_state();
let mut state = state_result.reference_mut();
assert_eq!(state.exists(&a), false);
state.inc_nonce(&a);
assert_eq!(state.exists(&a), true);
assert_eq!(state.nonce(&a), U256::from(1u64));
state.kill_account(&a);
assert_eq!(state.exists(&a), false);
assert_eq!(state.nonce(&a), U256::from(0u64));
}
#[test]
fn remove_from_database() {
let a = Address::zero();
let temp = RandomTempPath::new();
let (root, db) = {
let mut state = get_temp_state_in(temp.as_path());
state.inc_nonce(&a);
state.commit();
assert_eq!(state.exists(&a), true);
assert_eq!(state.nonce(&a), U256::from(1u64));
state.drop()
};
let (root, db) = {
let mut state = State::from_existing(db, root, U256::from(0u8));
assert_eq!(state.exists(&a), true);
assert_eq!(state.nonce(&a), U256::from(1u64));
state.kill_account(&a);
state.commit();
assert_eq!(state.exists(&a), false);
assert_eq!(state.nonce(&a), U256::from(0u64));
state.drop()
};
let state = State::from_existing(db, root, U256::from(0u8));
assert_eq!(state.exists(&a), false);
assert_eq!(state.nonce(&a), U256::from(0u64));
}
#[test]
2015-12-16 18:28:04 +01:00
fn alter_balance() {
let mut state_result = get_temp_state();
let mut state = state_result.reference_mut();
let a = Address::zero();
let b = address_from_u64(1u64);
state.add_balance(&a, &U256::from(69u64));
assert_eq!(state.balance(&a), U256::from(69u64));
state.commit();
assert_eq!(state.balance(&a), U256::from(69u64));
state.sub_balance(&a, &U256::from(42u64));
assert_eq!(state.balance(&a), U256::from(27u64));
state.commit();
assert_eq!(state.balance(&a), U256::from(27u64));
state.transfer_balance(&a, &b, &U256::from(18u64));
assert_eq!(state.balance(&a), U256::from(9u64));
assert_eq!(state.balance(&b), U256::from(18u64));
state.commit();
assert_eq!(state.balance(&a), U256::from(9u64));
assert_eq!(state.balance(&b), U256::from(18u64));
2015-12-16 18:28:04 +01:00
}
#[test]
fn alter_nonce() {
let mut state_result = get_temp_state();
let mut state = state_result.reference_mut();
let a = Address::zero();
state.inc_nonce(&a);
assert_eq!(state.nonce(&a), U256::from(1u64));
state.inc_nonce(&a);
assert_eq!(state.nonce(&a), U256::from(2u64));
state.commit();
assert_eq!(state.nonce(&a), U256::from(2u64));
state.inc_nonce(&a);
assert_eq!(state.nonce(&a), U256::from(3u64));
state.commit();
assert_eq!(state.nonce(&a), U256::from(3u64));
}
#[test]
2015-12-16 18:28:04 +01:00
fn balance_nonce() {
let mut state_result = get_temp_state();
let mut state = state_result.reference_mut();
let a = Address::zero();
assert_eq!(state.balance(&a), U256::from(0u64));
assert_eq!(state.nonce(&a), U256::from(0u64));
state.commit();
assert_eq!(state.balance(&a), U256::from(0u64));
assert_eq!(state.nonce(&a), U256::from(0u64));
2015-12-13 21:36:17 +01:00
}
#[test]
fn ensure_cached() {
let mut state_result = get_temp_state();
let mut state = state_result.reference_mut();
let a = Address::zero();
state.require(&a, false);
state.commit();
assert_eq!(state.root().hex(), "0ce23f3c809de377b008a4a3ee94a0834aac8bec1f86e28ffe4fdb5a15b0c785");
}
#[test]
fn snapshot_basic() {
let mut state_result = get_temp_state();
let mut state = state_result.reference_mut();
let a = Address::zero();
state.snapshot();
state.add_balance(&a, &U256::from(69u64));
assert_eq!(state.balance(&a), U256::from(69u64));
state.clear_snapshot();
assert_eq!(state.balance(&a), U256::from(69u64));
state.snapshot();
state.add_balance(&a, &U256::from(1u64));
assert_eq!(state.balance(&a), U256::from(70u64));
state.revert_snapshot();
assert_eq!(state.balance(&a), U256::from(69u64));
}
#[test]
fn snapshot_nested() {
let mut state_result = get_temp_state();
let mut state = state_result.reference_mut();
let a = Address::zero();
state.snapshot();
state.snapshot();
state.add_balance(&a, &U256::from(69u64));
assert_eq!(state.balance(&a), U256::from(69u64));
state.clear_snapshot();
assert_eq!(state.balance(&a), U256::from(69u64));
state.revert_snapshot();
assert_eq!(state.balance(&a), U256::from(0));
}
#[test]
fn create_empty() {
let mut state_result = get_temp_state();
let mut state = state_result.reference_mut();
state.commit();
assert_eq!(state.root().hex(), "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421");
}
2016-01-07 21:29:36 +01:00
}