Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.

Commit ecf098e

Browse files
arkpargavofyork
authored andcommitted
Track dirty accounts in the state (#2461)
* State to track dirty accounts * Removed clone_for_snapshot * Renaming stuff * Documentation and other minor fixes * Replaced MaybeAccount with Option
1 parent 33abb47 commit ecf098e

File tree

2 files changed

+135
-132
lines changed

2 files changed

+135
-132
lines changed

ethcore/src/state/account.rs

+13-48
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@
1616

1717
//! Single account in the system.
1818
19-
use std::collections::hash_map::Entry;
2019
use util::*;
2120
use pod_account::*;
2221
use rlp::*;
2322
use lru_cache::LruCache;
2423

2524
use std::cell::{RefCell, Cell};
2625

27-
const STORAGE_CACHE_ITEMS: usize = 4096;
26+
const STORAGE_CACHE_ITEMS: usize = 8192;
2827

2928
/// Single account in the system.
29+
/// Keeps track of changes to the code and storage.
30+
/// The changes are applied in `commit_storage` and `commit_code`
3031
pub struct Account {
3132
// Balance of the account.
3233
balance: U256,
@@ -46,8 +47,6 @@ pub struct Account {
4647
code_size: Option<usize>,
4748
// Code cache of the account.
4849
code_cache: Arc<Bytes>,
49-
// Account is new or has been modified.
50-
filth: Filth,
5150
// Account code new or has been modified.
5251
code_filth: Filth,
5352
// Cached address hash.
@@ -67,7 +66,6 @@ impl Account {
6766
code_hash: code.sha3(),
6867
code_size: Some(code.len()),
6968
code_cache: Arc::new(code),
70-
filth: Filth::Dirty,
7169
code_filth: Filth::Dirty,
7270
address_hash: Cell::new(None),
7371
}
@@ -89,7 +87,6 @@ impl Account {
8987
code_filth: Filth::Dirty,
9088
code_size: Some(pod.code.as_ref().map_or(0, |c| c.len())),
9189
code_cache: Arc::new(pod.code.map_or_else(|| { warn!("POD account with unknown code is being created! Assuming no code."); vec![] }, |c| c)),
92-
filth: Filth::Dirty,
9390
address_hash: Cell::new(None),
9491
}
9592
}
@@ -105,7 +102,6 @@ impl Account {
105102
code_hash: SHA3_EMPTY,
106103
code_cache: Arc::new(vec![]),
107104
code_size: Some(0),
108-
filth: Filth::Dirty,
109105
code_filth: Filth::Clean,
110106
address_hash: Cell::new(None),
111107
}
@@ -123,7 +119,6 @@ impl Account {
123119
code_hash: r.val_at(3),
124120
code_cache: Arc::new(vec![]),
125121
code_size: None,
126-
filth: Filth::Clean,
127122
code_filth: Filth::Clean,
128123
address_hash: Cell::new(None),
129124
}
@@ -141,7 +136,6 @@ impl Account {
141136
code_hash: SHA3_EMPTY,
142137
code_cache: Arc::new(vec![]),
143138
code_size: None,
144-
filth: Filth::Dirty,
145139
code_filth: Filth::Clean,
146140
address_hash: Cell::new(None),
147141
}
@@ -153,7 +147,6 @@ impl Account {
153147
self.code_hash = code.sha3();
154148
self.code_cache = Arc::new(code);
155149
self.code_size = Some(self.code_cache.len());
156-
self.filth = Filth::Dirty;
157150
self.code_filth = Filth::Dirty;
158151
}
159152

@@ -164,17 +157,7 @@ impl Account {
164157

165158
/// Set (and cache) the contents of the trie's storage at `key` to `value`.
166159
pub fn set_storage(&mut self, key: H256, value: H256) {
167-
match self.storage_changes.entry(key) {
168-
Entry::Occupied(ref mut entry) if entry.get() != &value => {
169-
entry.insert(value);
170-
self.filth = Filth::Dirty;
171-
},
172-
Entry::Vacant(entry) => {
173-
entry.insert(value);
174-
self.filth = Filth::Dirty;
175-
},
176-
_ => {},
177-
}
160+
self.storage_changes.insert(key, value);
178161
}
179162

180163
/// Get (and cache) the contents of the trie's storage at `key`.
@@ -263,17 +246,6 @@ impl Account {
263246
!self.code_cache.is_empty() || (self.code_cache.is_empty() && self.code_hash == SHA3_EMPTY)
264247
}
265248

266-
/// Is this a new or modified account?
267-
pub fn is_dirty(&self) -> bool {
268-
self.filth == Filth::Dirty || self.code_filth == Filth::Dirty || !self.storage_is_clean()
269-
}
270-
271-
/// Mark account as clean.
272-
pub fn set_clean(&mut self) {
273-
assert!(self.storage_is_clean());
274-
self.filth = Filth::Clean
275-
}
276-
277249
/// Provide a database to get `code_hash`. Should not be called if it is a contract without code.
278250
pub fn cache_code(&mut self, db: &HashDB) -> bool {
279251
// TODO: fill out self.code_cache;
@@ -326,25 +298,18 @@ impl Account {
326298
/// Increment the nonce of the account by one.
327299
pub fn inc_nonce(&mut self) {
328300
self.nonce = self.nonce + U256::from(1u8);
329-
self.filth = Filth::Dirty;
330301
}
331302

332-
/// Increment the nonce of the account by one.
303+
/// Increase account balance.
333304
pub fn add_balance(&mut self, x: &U256) {
334-
if !x.is_zero() {
335-
self.balance = self.balance + *x;
336-
self.filth = Filth::Dirty;
337-
}
305+
self.balance = self.balance + *x;
338306
}
339307

340-
/// Increment the nonce of the account by one.
308+
/// Decrease account balance.
341309
/// Panics if balance is less than `x`
342310
pub fn sub_balance(&mut self, x: &U256) {
343-
if !x.is_zero() {
344-
assert!(self.balance >= *x);
345-
self.balance = self.balance - *x;
346-
self.filth = Filth::Dirty;
347-
}
311+
assert!(self.balance >= *x);
312+
self.balance = self.balance - *x;
348313
}
349314

350315
/// Commit the `storage_changes` to the backing DB and update `storage_root`.
@@ -406,7 +371,6 @@ impl Account {
406371
code_hash: self.code_hash.clone(),
407372
code_size: self.code_size.clone(),
408373
code_cache: self.code_cache.clone(),
409-
filth: self.filth,
410374
code_filth: self.code_filth,
411375
address_hash: self.address_hash.clone(),
412376
}
@@ -427,10 +391,10 @@ impl Account {
427391
account
428392
}
429393

430-
/// Replace self with the data from other account merging storage cache
394+
/// Replace self with the data from other account merging storage cache.
395+
/// Basic account data and all modifications are overwritten
396+
/// with new values.
431397
pub fn merge_with(&mut self, other: Account) {
432-
assert!(self.storage_is_clean());
433-
assert!(other.storage_is_clean());
434398
self.balance = other.balance;
435399
self.nonce = other.nonce;
436400
self.storage_root = other.storage_root;
@@ -443,6 +407,7 @@ impl Account {
443407
for (k, v) in other.storage_cache.into_inner().into_iter() {
444408
cache.insert(k.clone() , v.clone()); //TODO: cloning should not be required here
445409
}
410+
self.storage_changes = other.storage_changes;
446411
}
447412
}
448413

0 commit comments

Comments
 (0)