2016-01-10 14:05:39 +01:00
|
|
|
use common::*;
|
2015-12-20 13:16:12 +01:00
|
|
|
use engine::Engine;
|
2016-01-11 17:37:22 +01:00
|
|
|
use executive::Executive;
|
2015-12-19 22:15:22 +01:00
|
|
|
|
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.
|
2016-01-13 01:19:05 +01:00
|
|
|
#[derive(Clone)]
|
2015-12-13 21:36:17 +01:00
|
|
|
pub struct State {
|
|
|
|
db: OverlayDB,
|
|
|
|
root: H256,
|
2015-12-19 22:15:22 +01:00
|
|
|
cache: RefCell<HashMap<Address, Option<Account>>>,
|
2015-12-13 21:36:17 +01:00
|
|
|
|
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
|
|
|
|
pub fn new(mut db: OverlayDB, account_start_nonce: U256) -> State {
|
|
|
|
let mut root = H256::new();
|
|
|
|
{
|
|
|
|
// init trie and reset root too null
|
2016-01-09 18:20:31 +01:00
|
|
|
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()),
|
|
|
|
account_start_nonce: account_start_nonce,
|
2015-12-13 21:36:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates new state with existing state root
|
2016-01-09 18:20:31 +01:00
|
|
|
pub fn from_existing(db: OverlayDB, root: H256, account_start_nonce: U256) -> State {
|
2015-12-13 21:36:17 +01:00
|
|
|
{
|
|
|
|
// trie should panic! if root does not exist
|
2016-01-09 18:20:31 +01:00
|
|
|
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()),
|
|
|
|
account_start_nonce: account_start_nonce,
|
2015-12-13 21:36:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create temporary state object
|
|
|
|
pub fn new_temp() -> State {
|
|
|
|
Self::new(OverlayDB::new_temp(), U256::from(0u8))
|
|
|
|
}
|
|
|
|
|
2015-12-16 20:02:28 +01:00
|
|
|
/// Destroy the current object and return root and database.
|
2015-12-19 19:03:42 +01:00
|
|
|
pub fn drop(self) -> (H256, OverlayDB) {
|
|
|
|
(self.root, self.db)
|
2015-12-16 20:02:28 +01:00
|
|
|
}
|
|
|
|
|
2015-12-19 22:15:22 +01:00
|
|
|
/// 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
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
self.require_or_from(contract, false, || Account::new_contract(U256::from(0u8)), |r| r.reset_code());
|
|
|
|
}
|
|
|
|
|
2016-01-09 22:28:31 +01:00
|
|
|
/// Remove an existing account.
|
|
|
|
pub fn kill_account(&mut self, account: &Address) {
|
|
|
|
self.cache.borrow_mut().insert(account.clone(), 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)
|
|
|
|
}
|
|
|
|
|
2015-12-16 18:20:23 +01:00
|
|
|
/// Get the balance of account `a`.
|
2015-12-19 22:15:22 +01:00
|
|
|
pub fn balance(&self, a: &Address) -> U256 {
|
2015-12-16 18:20:23 +01:00
|
|
|
self.get(a, false).as_ref().map(|account| account.balance().clone()).unwrap_or(U256::from(0u8))
|
|
|
|
}
|
|
|
|
|
2015-12-19 22:15:22 +01:00
|
|
|
/// Get the nonce of account `a`.
|
|
|
|
pub fn nonce(&self, a: &Address) -> U256 {
|
|
|
|
self.get(a, false).as_ref().map(|account| account.nonce().clone()).unwrap_or(U256::from(0u8))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mutate storage of account `a` so that it is `value` for `key`.
|
|
|
|
pub fn storage_at(&self, a: &Address, key: &H256) -> H256 {
|
|
|
|
self.get(a, false).as_ref().map(|a|a.storage_at(&self.db, key)).unwrap_or(H256::new())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mutate storage of account `a` so that it is `value` for `key`.
|
|
|
|
pub fn code(&self, a: &Address) -> Option<Vec<u8>> {
|
|
|
|
self.get(a, true).as_ref().map(|a|a.code().map(|x|x.to_vec())).unwrap_or(None)
|
|
|
|
}
|
|
|
|
|
2015-12-16 18:20:23 +01:00
|
|
|
/// Add `incr` to the balance of account `a`.
|
|
|
|
pub fn add_balance(&mut self, a: &Address, incr: &U256) {
|
|
|
|
self.require(a, false).add_balance(incr)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Subtract `decr` from the balance of account `a`.
|
|
|
|
pub fn sub_balance(&mut self, a: &Address, decr: &U256) {
|
|
|
|
self.require(a, false).sub_balance(decr)
|
|
|
|
}
|
|
|
|
|
2015-12-19 22:38:25 +01:00
|
|
|
/// 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);
|
|
|
|
}
|
|
|
|
|
2015-12-16 18:20:23 +01:00
|
|
|
/// Increment the nonce of account `a` by 1.
|
|
|
|
pub fn inc_nonce(&mut self, a: &Address) {
|
|
|
|
self.require(a, false).inc_nonce()
|
|
|
|
}
|
|
|
|
|
2015-12-19 19:00:19 +01:00
|
|
|
/// 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) {
|
|
|
|
self.require(a, false).set_storage(key, value);
|
2015-12-19 19:00:19 +01:00
|
|
|
}
|
|
|
|
|
2016-01-09 12:30:41 +01:00
|
|
|
/// 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);
|
2015-12-19 19:00:19 +01:00
|
|
|
}
|
|
|
|
|
2015-12-19 22:38:25 +01:00
|
|
|
/// Execute a given transaction.
|
|
|
|
/// This will change the state accordingly.
|
2016-01-11 17:01:42 +01:00
|
|
|
pub fn apply(&mut self, env_info: &EnvInfo, engine: &Engine, t: &Transaction) -> ApplyResult {
|
2016-01-11 17:37:22 +01:00
|
|
|
let e = try!(Executive::new(self, env_info, engine).transact(t));
|
|
|
|
self.commit();
|
|
|
|
Ok(Receipt::new(self.root().clone(), e.gas_used, e.logs))
|
2015-12-20 13:16:12 +01:00
|
|
|
}
|
2015-12-19 22:38:25 +01:00
|
|
|
|
|
|
|
/// Convert into a JSON representation.
|
|
|
|
pub fn as_json(&self) -> String {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2016-01-09 18:20:31 +01:00
|
|
|
/// Commit accounts to SecTrieDBMut. This is similar to cpp-ethereum's dev::eth::commit.
|
2015-12-16 16:39:49 +01:00
|
|
|
/// `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
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-01-09 18:20:31 +01:00
|
|
|
let mut trie = SecTrieDBMut::from_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-19 22:15:22 +01:00
|
|
|
self.root = Self::commit_into(&mut self.db, r, self.cache.borrow_mut().deref_mut());
|
2015-12-13 21:36:17 +01:00
|
|
|
}
|
2015-12-16 16:39:49 +01:00
|
|
|
|
2016-01-13 01:19:05 +01:00
|
|
|
/// Populate the state from `accounts`.
|
2016-01-13 12:14:11 +01:00
|
|
|
pub fn populate_from(&mut self, accounts: BTreeMap<Address, PodAccount>) {
|
2016-01-13 01:19:05 +01:00
|
|
|
for (add, acc) in accounts.into_iter() {
|
|
|
|
self.cache.borrow_mut().insert(add, Some(Account::from_pod(acc)));
|
|
|
|
}
|
2015-12-19 22:38:25 +01:00
|
|
|
}
|
|
|
|
|
2016-01-13 12:14:11 +01:00
|
|
|
/// Populate a PodAccount map from this state.
|
|
|
|
pub fn to_hashmap_pod(&self) -> HashMap<Address, PodAccount> {
|
|
|
|
// TODO: handle database rather than just the cache.
|
|
|
|
self.cache.borrow().iter().fold(HashMap::new(), |mut m, (add, opt)| {
|
|
|
|
if let &Some(ref acc) = opt {
|
|
|
|
m.insert(add.clone(), PodAccount::from_account(acc));
|
|
|
|
}
|
|
|
|
m
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Populate a PodAccount map from this state.
|
|
|
|
pub fn to_pod_map(&self) -> BTreeMap<Address, PodAccount> {
|
|
|
|
// TODO: handle database rather than just the cache.
|
|
|
|
self.cache.borrow().iter().fold(BTreeMap::new(), |mut m, (add, opt)| {
|
|
|
|
if let &Some(ref acc) = opt {
|
|
|
|
m.insert(add.clone(), PodAccount::from_account(acc));
|
|
|
|
}
|
|
|
|
m
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-12-19 19:00:19 +01:00
|
|
|
/// Pull account `a` in our cache from the trie DB and return it.
|
|
|
|
/// `require_code` requires that the code be cached, too.
|
2015-12-19 22:15:22 +01:00
|
|
|
fn get(&self, a: &Address, require_code: bool) -> Ref<Option<Account>> {
|
2015-12-19 22:22:19 +01:00
|
|
|
self.cache.borrow_mut().entry(a.clone()).or_insert_with(||
|
2016-01-09 18:20:31 +01:00
|
|
|
SecTrieDB::new(&self.db, &self.root).get(&a).map(|rlp| Account::from_rlp(rlp)));
|
2015-12-19 19:00:19 +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() {
|
|
|
|
account.cache_code(&self.db);
|
2015-12-16 18:20:23 +01:00
|
|
|
}
|
2015-12-19 19:00:19 +01:00
|
|
|
}
|
2015-12-19 22:15:22 +01:00
|
|
|
Ref::map(self.cache.borrow(), |m| m.get(a).unwrap())
|
2015-12-16 18:20:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Pull account `a` in our cache from the trie DB. `require_code` requires that the code be cached, too.
|
2015-12-19 22:15:22 +01:00
|
|
|
fn require(&self, a: &Address, require_code: bool) -> RefMut<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), |_|{})
|
2015-12-19 19:00:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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`.
|
|
|
|
fn require_or_from<F: FnOnce() -> Account, G: FnOnce(&mut Account)>(&self, a: &Address, require_code: bool, default: F, not_default: G) -> RefMut<Account> {
|
2015-12-19 22:22:19 +01:00
|
|
|
self.cache.borrow_mut().entry(a.clone()).or_insert_with(||
|
2016-01-09 18:20:31 +01:00
|
|
|
SecTrieDB::new(&self.db, &self.root).get(&a).map(|rlp| Account::from_rlp(rlp)));
|
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());
|
2015-12-16 16:39:49 +01:00
|
|
|
}
|
|
|
|
|
2015-12-19 22:15:22 +01:00
|
|
|
let b = self.cache.borrow_mut();
|
|
|
|
RefMut::map(b, |m| m.get_mut(a).unwrap().as_mut().map(|account| {
|
2015-12-16 18:20:23 +01:00
|
|
|
if require_code {
|
2015-12-19 22:15:22 +01:00
|
|
|
account.cache_code(&self.db);
|
2015-12-16 16:39:49 +01:00
|
|
|
}
|
2015-12-16 18:20:23 +01:00
|
|
|
account
|
2015-12-19 22:15:22 +01:00
|
|
|
}).unwrap())
|
2015-12-16 16:39:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-13 01:19:05 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 16:39:49 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use util::hash::*;
|
|
|
|
use util::trie::*;
|
|
|
|
use util::rlp::*;
|
2015-12-16 18:20:23 +01:00
|
|
|
use util::uint::*;
|
2015-12-19 19:00:19 +01:00
|
|
|
use account::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn code_from_database() {
|
2016-01-09 22:28:31 +01:00
|
|
|
let a = Address::zero();
|
2015-12-19 19:00:19 +01:00
|
|
|
let (r, db) = {
|
|
|
|
let mut s = State::new_temp();
|
2016-01-09 14:19:35 +01:00
|
|
|
s.require_or_from(&a, false, ||Account::new_contract(U256::from(42u32)), |_|{});
|
2016-01-09 12:30:41 +01:00
|
|
|
s.init_code(&a, vec![1, 2, 3]);
|
2015-12-19 22:15:22 +01:00
|
|
|
assert_eq!(s.code(&a), Some([1u8, 2, 3].to_vec()));
|
2015-12-19 19:00:19 +01:00
|
|
|
s.commit();
|
2015-12-19 22:15:22 +01:00
|
|
|
assert_eq!(s.code(&a), Some([1u8, 2, 3].to_vec()));
|
2015-12-19 19:03:42 +01:00
|
|
|
s.drop()
|
2015-12-19 19:00:19 +01:00
|
|
|
};
|
|
|
|
|
2016-01-08 22:04:21 +01:00
|
|
|
let s = State::from_existing(db, r, U256::from(0u8));
|
2015-12-19 22:15:22 +01:00
|
|
|
assert_eq!(s.code(&a), Some([1u8, 2, 3].to_vec()));
|
2015-12-19 19:00:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn storage_at_from_database() {
|
2016-01-09 22:28:31 +01:00
|
|
|
let a = Address::zero();
|
2015-12-19 19:00:19 +01:00
|
|
|
let (r, db) = {
|
|
|
|
let mut s = State::new_temp();
|
|
|
|
s.set_storage(&a, H256::from(&U256::from(01u64)), H256::from(&U256::from(69u64)));
|
|
|
|
s.commit();
|
2015-12-19 19:03:42 +01:00
|
|
|
s.drop()
|
2015-12-19 19:00:19 +01:00
|
|
|
};
|
|
|
|
|
2016-01-08 22:04:21 +01:00
|
|
|
let s = State::from_existing(db, r, U256::from(0u8));
|
2015-12-19 19:00:19 +01:00
|
|
|
assert_eq!(s.storage_at(&a, &H256::from(&U256::from(01u64))), H256::from(&U256::from(69u64)));
|
|
|
|
}
|
2015-12-16 16:39:49 +01:00
|
|
|
|
|
|
|
#[test]
|
2015-12-16 20:11:37 +01:00
|
|
|
fn get_from_database() {
|
2016-01-09 22:28:31 +01:00
|
|
|
let a = Address::zero();
|
2015-12-16 20:02:28 +01:00
|
|
|
let (r, db) = {
|
|
|
|
let mut s = State::new_temp();
|
|
|
|
s.inc_nonce(&a);
|
|
|
|
s.add_balance(&a, &U256::from(69u64));
|
|
|
|
s.commit();
|
|
|
|
assert_eq!(s.balance(&a), U256::from(69u64));
|
2015-12-19 19:03:42 +01:00
|
|
|
s.drop()
|
2015-12-16 20:02:28 +01:00
|
|
|
};
|
|
|
|
|
2016-01-08 22:04:21 +01:00
|
|
|
let s = State::from_existing(db, r, U256::from(0u8));
|
2015-12-16 20:02:28 +01:00
|
|
|
assert_eq!(s.balance(&a), U256::from(69u64));
|
|
|
|
assert_eq!(s.nonce(&a), U256::from(1u64));
|
2015-12-16 18:20:23 +01:00
|
|
|
}
|
|
|
|
|
2016-01-09 22:28:31 +01:00
|
|
|
#[test]
|
|
|
|
fn remove() {
|
|
|
|
let a = Address::zero();
|
|
|
|
let mut s = State::new_temp();
|
2016-01-12 12:34:14 +01:00
|
|
|
assert_eq!(s.exists(&a), false);
|
2016-01-09 22:28:31 +01:00
|
|
|
s.inc_nonce(&a);
|
2016-01-12 12:34:14 +01:00
|
|
|
assert_eq!(s.exists(&a), true);
|
2016-01-09 22:28:31 +01:00
|
|
|
assert_eq!(s.nonce(&a), U256::from(1u64));
|
|
|
|
s.kill_account(&a);
|
2016-01-12 12:34:14 +01:00
|
|
|
assert_eq!(s.exists(&a), false);
|
2016-01-09 22:28:31 +01:00
|
|
|
assert_eq!(s.nonce(&a), U256::from(0u64));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn remove_from_database() {
|
|
|
|
let a = Address::zero();
|
|
|
|
let (r, db) = {
|
|
|
|
let mut s = State::new_temp();
|
|
|
|
s.inc_nonce(&a);
|
|
|
|
s.commit();
|
2016-01-12 12:34:14 +01:00
|
|
|
assert_eq!(s.exists(&a), true);
|
2016-01-09 22:28:31 +01:00
|
|
|
assert_eq!(s.nonce(&a), U256::from(1u64));
|
|
|
|
s.drop()
|
|
|
|
};
|
|
|
|
|
|
|
|
let (r, db) = {
|
|
|
|
let mut s = State::from_existing(db, r, U256::from(0u8));
|
2016-01-12 12:34:14 +01:00
|
|
|
assert_eq!(s.exists(&a), true);
|
2016-01-09 22:28:31 +01:00
|
|
|
assert_eq!(s.nonce(&a), U256::from(1u64));
|
|
|
|
s.kill_account(&a);
|
|
|
|
s.commit();
|
2016-01-12 12:34:14 +01:00
|
|
|
assert_eq!(s.exists(&a), false);
|
2016-01-09 22:28:31 +01:00
|
|
|
assert_eq!(s.nonce(&a), U256::from(0u64));
|
|
|
|
s.drop()
|
|
|
|
};
|
|
|
|
|
|
|
|
let s = State::from_existing(db, r, U256::from(0u8));
|
2016-01-12 12:34:14 +01:00
|
|
|
assert_eq!(s.exists(&a), false);
|
2016-01-09 22:28:31 +01:00
|
|
|
assert_eq!(s.nonce(&a), U256::from(0u64));
|
|
|
|
}
|
|
|
|
|
2015-12-16 18:20:23 +01:00
|
|
|
#[test]
|
2015-12-16 18:28:04 +01:00
|
|
|
fn alter_balance() {
|
2015-12-16 18:20:23 +01:00
|
|
|
let mut s = State::new_temp();
|
2016-01-09 22:28:31 +01:00
|
|
|
let a = Address::zero();
|
|
|
|
let b = address_from_u64(1u64);
|
2015-12-16 18:20:23 +01:00
|
|
|
s.add_balance(&a, &U256::from(69u64));
|
|
|
|
assert_eq!(s.balance(&a), U256::from(69u64));
|
|
|
|
s.commit();
|
|
|
|
assert_eq!(s.balance(&a), U256::from(69u64));
|
2015-12-16 18:28:04 +01:00
|
|
|
s.sub_balance(&a, &U256::from(42u64));
|
|
|
|
assert_eq!(s.balance(&a), U256::from(27u64));
|
|
|
|
s.commit();
|
|
|
|
assert_eq!(s.balance(&a), U256::from(27u64));
|
2015-12-19 22:38:25 +01:00
|
|
|
s.transfer_balance(&a, &b, &U256::from(18u64));
|
|
|
|
assert_eq!(s.balance(&a), U256::from(9u64));
|
|
|
|
assert_eq!(s.balance(&b), U256::from(18u64));
|
|
|
|
s.commit();
|
|
|
|
assert_eq!(s.balance(&a), U256::from(9u64));
|
|
|
|
assert_eq!(s.balance(&b), U256::from(18u64));
|
2015-12-16 18:28:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn alter_nonce() {
|
|
|
|
let mut s = State::new_temp();
|
2016-01-09 22:28:31 +01:00
|
|
|
let a = Address::zero();
|
2015-12-16 18:28:04 +01:00
|
|
|
s.inc_nonce(&a);
|
|
|
|
assert_eq!(s.nonce(&a), U256::from(1u64));
|
|
|
|
s.inc_nonce(&a);
|
|
|
|
assert_eq!(s.nonce(&a), U256::from(2u64));
|
|
|
|
s.commit();
|
|
|
|
assert_eq!(s.nonce(&a), U256::from(2u64));
|
|
|
|
s.inc_nonce(&a);
|
|
|
|
assert_eq!(s.nonce(&a), U256::from(3u64));
|
|
|
|
s.commit();
|
|
|
|
assert_eq!(s.nonce(&a), U256::from(3u64));
|
2015-12-16 18:20:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-12-16 18:28:04 +01:00
|
|
|
fn balance_nonce() {
|
2015-12-16 18:20:23 +01:00
|
|
|
let mut s = State::new_temp();
|
2016-01-09 22:28:31 +01:00
|
|
|
let a = Address::zero();
|
2015-12-16 18:20:23 +01:00
|
|
|
assert_eq!(s.balance(&a), U256::from(0u64));
|
2015-12-16 18:28:04 +01:00
|
|
|
assert_eq!(s.nonce(&a), U256::from(0u64));
|
2015-12-16 18:20:23 +01:00
|
|
|
s.commit();
|
|
|
|
assert_eq!(s.balance(&a), U256::from(0u64));
|
2015-12-16 18:28:04 +01:00
|
|
|
assert_eq!(s.nonce(&a), U256::from(0u64));
|
2015-12-13 21:36:17 +01:00
|
|
|
}
|
2015-12-16 16:39:49 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ensure_cached() {
|
|
|
|
let mut s = State::new_temp();
|
2016-01-09 22:28:31 +01:00
|
|
|
let a = Address::zero();
|
2015-12-16 18:20:23 +01:00
|
|
|
s.require(&a, false);
|
2015-12-16 16:39:49 +01:00
|
|
|
s.commit();
|
2016-01-09 18:20:31 +01:00
|
|
|
assert_eq!(s.root().hex(), "0ce23f3c809de377b008a4a3ee94a0834aac8bec1f86e28ffe4fdb5a15b0c785");
|
2015-12-16 16:39:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_empty() {
|
|
|
|
let mut s = State::new_temp();
|
|
|
|
s.commit();
|
|
|
|
assert_eq!(s.root().hex(), "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421");
|
|
|
|
}
|
|
|
|
|
2016-01-07 21:29:36 +01:00
|
|
|
}
|