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

Commit aab0a59

Browse files
committed
Renamed and documented a few methods
1 parent abbf0ef commit aab0a59

File tree

3 files changed

+58
-51
lines changed

3 files changed

+58
-51
lines changed

ethcore/src/client/client.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,8 @@ impl Client {
442442
.collect();
443443

444444
//let traces = From::from(block.traces().clone().unwrap_or_else(Vec::new));
445-
let mut batch = DBTransaction::new(&self.db.read());
446445

446+
let mut batch = DBTransaction::new(&self.db.read());
447447
// CHECK! I *think* this is fine, even if the state_root is equal to another
448448
// already-imported block of the same number.
449449
// TODO: Prove it with a test.
@@ -458,8 +458,8 @@ impl Client {
458458
enacted: route.enacted.clone(),
459459
retracted: route.retracted.len()
460460
});
461-
let is_canon = route.omitted.len() == 0;
462-
state.update_cache(&route.enacted, &route.retracted, is_canon);
461+
let is_canon = route.enacted.last().map_or(false, |h| h == hash);
462+
state.sync_cache(&route.enacted, &route.retracted, is_canon);
463463
// Final commit to the DB
464464
self.db.read().write_buffered(batch);
465465
chain.commit();

ethcore/src/state/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ impl State {
267267

268268
/// Destroy the current object and return root and database.
269269
pub fn drop(mut self) -> (H256, StateDB) {
270-
self.commit_cache();
270+
self.update_shared_cache();
271271
(self.root, self.db)
272272
}
273273

@@ -482,11 +482,12 @@ impl State {
482482
Ok(())
483483
}
484484

485-
fn commit_cache(&mut self) {
485+
/// Merge local cache into shared canonical state cache.
486+
fn update_shared_cache(&mut self) {
486487
let mut addresses = self.cache.borrow_mut();
487488
trace!("Committing cache {:?} entries", addresses.len());
488489
for (address, a) in addresses.drain().filter(|&(_, ref a)| !a.is_dirty()) {
489-
self.db.cache_account(address, a.account, a.state == AccountState::Commited);
490+
self.db.add_to_account_cache(address, a.account, a.state == AccountState::Commited);
490491
}
491492
}
492493

ethcore/src/state_db.rs

+51-45
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,37 @@ pub const DEFAULT_ACCOUNT_PRESET: usize = 1000000;
3535

3636
pub const ACCOUNT_BLOOM_HASHCOUNT_KEY: &'static [u8] = b"account_hash_count";
3737

38+
/// Shared canonical state cache.
3839
struct AccountCache {
3940
/// DB Account cache. `None` indicates that account is known to be missing.
4041
accounts: LruCache<Address, Option<Account>>,
41-
/// Accounts changed in recent blocks. Ordered by block number.
42+
/// Accounts changed in recently committed blocks. Ordered by block number.
4243
modifications: VecDeque<BlockChanges>,
4344
}
4445

46+
/// Pending account cache item.
4547
struct CacheQueueItem {
48+
/// Account address.
4649
address: Address,
50+
/// Acccount data or `None` if account does not exist.
4751
account: Option<Account>,
52+
/// Indicates that the account was modified before being
53+
/// added to the cache.
4854
modified: bool,
4955
}
5056

5157
#[derive(Debug)]
52-
// Accumulates a list of accounts changed in a block.
58+
/// Accumulates a list of accounts changed in a block.
5359
struct BlockChanges {
60+
/// Block number.
5461
number: BlockNumber,
62+
/// Block hash.
5563
hash: H256,
64+
/// Parent block hash.
5665
parent: H256,
66+
/// A set of modified account addresses.
5767
accounts: HashSet<Address>,
68+
/// Block is part of the canonical chain.
5869
is_canon: bool,
5970
}
6071

@@ -65,19 +76,20 @@ struct BlockChanges {
6576
/// on commit.
6677
/// For non-canonical clones cache is cleared on commit.
6778
pub struct StateDB {
68-
// Backing database.
79+
/// Backing database.
6980
db: Box<JournalDB>,
70-
// Shared canonical state cache.
81+
/// Shared canonical state cache.
7182
account_cache: Arc<Mutex<AccountCache>>,
72-
// Local pending cache changes.
73-
cache_overlay: Vec<CacheQueueItem>,
74-
// Shared account bloom. Does not handle chain reorganizations.
83+
/// Local pending cache changes.
84+
pending_cache: Vec<CacheQueueItem>,
85+
/// Shared account bloom. Does not handle chain reorganizations.
7586
account_bloom: Arc<Mutex<Bloom>>,
76-
// Hash of the block on top of which this instance was created
87+
/// Hash of the block on top of which this instance was created or
88+
/// `None` if cache is disabled
7789
parent_hash: Option<H256>,
78-
// Hash of the committing block
90+
/// Hash of the committing block or `None` if not committed yet.
7991
commit_hash: Option<H256>,
80-
// Number of the committing block
92+
/// Number of the committing block or `None` if not committed yet.
8193
commit_number: Option<BlockNumber>,
8294
}
8395

@@ -118,7 +130,7 @@ impl StateDB {
118130
accounts: LruCache::new(STATE_CACHE_ITEMS),
119131
modifications: VecDeque::new(),
120132
})),
121-
cache_overlay: Vec::new(),
133+
pending_cache: Vec::new(),
122134
account_bloom: Arc::new(Mutex::new(bloom)),
123135
parent_hash: None,
124136
commit_hash: None,
@@ -165,11 +177,14 @@ impl StateDB {
165177
Ok(records)
166178
}
167179

168-
/// Update canonical cache. This should be called after the block has been commited and the
169-
/// blockchain route has ben calculated.
170-
/// `retracted` is the list of the retracted block hashes.
171-
pub fn update_cache(&mut self, enacted: &[H256], retracted: &[H256], is_best: bool) {
172-
trace!("commit id = (#{:?}, {:?}), parent={:?}, best={}", self.commit_number, self.commit_hash, self.parent_hash, is_best);
180+
/// Apply pending cache changes and synchronize canonical
181+
/// state cache with the best block state.
182+
/// This function updates the cache by removing entries that are
183+
/// invalidated by chain reorganization. `update_cache` should be
184+
/// called after the block has been commited and the blockchain
185+
/// route has ben calculated.
186+
pub fn sync_cache(&mut self, enacted: &[H256], retracted: &[H256], is_best: bool) {
187+
trace!("sync_cache id = (#{:?}, {:?}), parent={:?}, best={}", self.commit_number, self.commit_hash, self.parent_hash, is_best);
173188
let mut cache = self.account_cache.lock();
174189
let mut cache = &mut *cache;
175190

@@ -178,10 +193,10 @@ impl StateDB {
178193
for block in enacted.iter().filter(|h| self.commit_hash.as_ref().map_or(false, |p| *h != p)) {
179194
clear = clear || {
180195
if let Some(ref mut m) = cache.modifications.iter_mut().find(|ref m| &m.hash == block) {
181-
trace!("reverting enacted block {:?}", block);
196+
trace!("Reverting enacted block {:?}", block);
182197
m.is_canon = true;
183198
for a in &m.accounts {
184-
trace!("reverting enacted address {:?}", a);
199+
trace!("Reverting enacted address {:?}", a);
185200
cache.accounts.remove(a);
186201
}
187202
false
@@ -194,10 +209,10 @@ impl StateDB {
194209
for block in retracted {
195210
clear = clear || {
196211
if let Some(ref mut m) = cache.modifications.iter_mut().find(|ref m| &m.hash == block) {
197-
trace!("retracting block {:?}", block);
212+
trace!("Retracting block {:?}", block);
198213
m.is_canon = false;
199214
for a in &m.accounts {
200-
trace!("retracted address {:?}", a);
215+
trace!("Retracted address {:?}", a);
201216
cache.accounts.remove(a);
202217
}
203218
false
@@ -208,7 +223,7 @@ impl StateDB {
208223
}
209224
if clear {
210225
// We don't know anything about the block; clear everything
211-
trace!("wiping cache");
226+
trace!("Wiping cache");
212227
cache.accounts.clear();
213228
cache.modifications.clear();
214229
}
@@ -221,8 +236,8 @@ impl StateDB {
221236
cache.modifications.pop_back();
222237
}
223238
let mut modifications = HashSet::new();
224-
trace!("committing {} cache entries", self.cache_overlay.len());
225-
for account in self.cache_overlay.drain(..) {
239+
trace!("committing {} cache entries", self.pending_cache.len());
240+
for account in self.pending_cache.drain(..) {
226241
if account.modified {
227242
modifications.insert(account.address.clone());
228243
}
@@ -272,7 +287,7 @@ impl StateDB {
272287
StateDB {
273288
db: self.db.boxed_clone(),
274289
account_cache: self.account_cache.clone(),
275-
cache_overlay: Vec::new(),
290+
pending_cache: Vec::new(),
276291
account_bloom: self.account_bloom.clone(),
277292
parent_hash: None,
278293
commit_hash: None,
@@ -285,7 +300,7 @@ impl StateDB {
285300
StateDB {
286301
db: self.db.boxed_clone(),
287302
account_cache: self.account_cache.clone(),
288-
cache_overlay: Vec::new(),
303+
pending_cache: Vec::new(),
289304
account_bloom: self.account_bloom.clone(),
290305
parent_hash: Some(parent.clone()),
291306
commit_hash: None,
@@ -308,26 +323,18 @@ impl StateDB {
308323
&*self.db
309324
}
310325

311-
/// Enqueue cache change.
312-
pub fn cache_account(&mut self, addr: Address, data: Option<Account>, modified: bool) {
313-
self.cache_overlay.push(CacheQueueItem {
326+
/// Add pending cache change.
327+
/// The change is queued to be applied in `commit`.
328+
pub fn add_to_account_cache(&mut self, addr: Address, data: Option<Account>, modified: bool) {
329+
self.pending_cache.push(CacheQueueItem {
314330
address: addr,
315331
account: data,
316332
modified: modified,
317333
})
318334
}
319335

320-
/// Clear the cache.
321-
pub fn clear_cache(&mut self) {
322-
trace!("Clearing cache");
323-
self.cache_overlay.clear();
324-
let mut cache = self.account_cache.lock();
325-
cache.accounts.clear();
326-
}
327-
328336
/// Get basic copy of the cached account. Does not include storage.
329-
/// Returns 'None' if the state is non-canonical and cache is disabled
330-
/// or if the account is not cached.
337+
/// Returns 'None' if cache is disabled or if the account is not cached.
331338
pub fn get_cached_account(&self, addr: &Address) -> Option<Option<Account>> {
332339
let mut cache = self.account_cache.lock();
333340
if !Self::is_allowed(addr, &self.parent_hash, &cache.modifications) {
@@ -337,8 +344,7 @@ impl StateDB {
337344
}
338345

339346
/// Get value from a cached account.
340-
/// Returns 'None' if the state is non-canonical and cache is disabled
341-
/// or if the account is not cached.
347+
/// Returns 'None' if cache is disabled or if the account is not cached.
342348
pub fn get_cached<F, U>(&self, a: &Address, f: F) -> Option<U>
343349
where F: FnOnce(Option<&mut Account>) -> U {
344350
let mut cache = self.account_cache.lock();
@@ -348,12 +354,12 @@ impl StateDB {
348354
cache.accounts.get_mut(a).map(|c| f(c.as_mut()))
349355
}
350356

351-
// Check if the account can be returned from cache by matching current block parent hash against canonical
352-
// state and filtering out account modified in later blocks.
357+
/// Check if the account can be returned from cache by matching current block parent hash against canonical
358+
/// state and filtering out account modified in later blocks.
353359
fn is_allowed(addr: &Address, parent_hash: &Option<H256>, modifications: &VecDeque<BlockChanges>) -> bool {
354360
let mut parent = match *parent_hash {
355361
None => {
356-
trace!("cache lookup skipped for {:?}: no parent hash", addr);
362+
trace!("Cache lookup skipped for {:?}: no parent hash", addr);
357363
return false;
358364
}
359365
Some(ref parent) => parent,
@@ -375,11 +381,11 @@ impl StateDB {
375381
parent = &m.parent;
376382
}
377383
if m.accounts.contains(addr) {
378-
trace!("cache lookup skipped for {:?}: modified in a later block", addr);
384+
trace!("Cache lookup skipped for {:?}: modified in a later block", addr);
379385
return false;
380386
}
381387
}
382-
trace!("cache lookup skipped for {:?}: parent hash is unknown", addr);
388+
trace!("Cache lookup skipped for {:?}: parent hash is unknown", addr);
383389
return false;
384390
}
385391
}

0 commit comments

Comments
 (0)