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

Commit 0f80b7b

Browse files
committed
More renames and updated documentation
1 parent 455279d commit 0f80b7b

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

ethcore/src/state/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ impl AccountEntry {
164164
/// use that.
165165
/// ****************************************************************************
166166
///
167-
/// Upon destruction all the local cache data merged into the global cache.
168-
/// The merge might be rejected if current state is non-canonical.
167+
/// Upon destruction all the local cache data propagated into the global cache.
168+
/// Propagated items might be rejected if current state is non-canonical.
169169
///
170170
/// State snapshotting.
171171
///
@@ -318,7 +318,7 @@ impl State {
318318

319319
/// Destroy the current object and return root and database.
320320
pub fn drop(mut self) -> (H256, StateDB) {
321-
self.update_shared_cache();
321+
self.propagate_to_global_cache();
322322
(self.root, self.db)
323323
}
324324

@@ -533,8 +533,8 @@ impl State {
533533
Ok(())
534534
}
535535

536-
/// Merge local cache into shared canonical state cache.
537-
fn update_shared_cache(&mut self) {
536+
/// Propagate local cache into shared canonical state cache.
537+
fn propagate_to_global_cache(&mut self) {
538538
let mut addresses = self.cache.borrow_mut();
539539
trace!("Committing cache {:?} entries", addresses.len());
540540
for (address, a) in addresses.drain().filter(|&(_, ref a)| a.state == AccountState::Committed || a.state == AccountState::CleanFresh) {

ethcore/src/state_db.rs

+27-18
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,20 @@ struct BlockChanges {
7474
/// For canonical clones cache changes are accumulated and applied
7575
/// on commit.
7676
/// For non-canonical clones cache is cleared on commit.
77+
///
78+
/// Global cache propagation.
79+
/// After a `State` object has been committed to the trie it
80+
/// propagates its local cache into the `StateDB` local cache
81+
/// using `add_to_account_cache` function.
82+
/// Then, after the block has been added to the chain the local cache in the
83+
/// `StateDB` is propagated into the global cache.
7784
pub struct StateDB {
7885
/// Backing database.
7986
db: Box<JournalDB>,
8087
/// Shared canonical state cache.
8188
account_cache: Arc<Mutex<AccountCache>>,
82-
/// Local cache buffer.
83-
cache_buffer: Vec<CacheQueueItem>,
89+
/// Local dirty cache.
90+
local_cache: Vec<CacheQueueItem>,
8491
/// Shared account bloom. Does not handle chain reorganizations.
8592
account_bloom: Arc<Mutex<Bloom>>,
8693
/// Hash of the block on top of which this instance was created or
@@ -129,7 +136,7 @@ impl StateDB {
129136
accounts: LruCache::new(STATE_CACHE_ITEMS),
130137
modifications: VecDeque::new(),
131138
})),
132-
cache_buffer: Vec::new(),
139+
local_cache: Vec::new(),
133140
account_bloom: Arc::new(Mutex::new(bloom)),
134141
parent_hash: None,
135142
commit_hash: None,
@@ -176,18 +183,18 @@ impl StateDB {
176183
Ok(records)
177184
}
178185

179-
/// Apply buffered cache changes and synchronize canonical
180-
/// state cache with the best block state.
181-
/// This function updates the cache by removing entries that are
182-
/// invalidated by chain reorganization. `sync_cache` should be
183-
/// called after the block has been commited and the blockchain
184-
/// route has ben calculated.
186+
/// Propagate local cache into the global cache and synchonize
187+
/// the global cache with the best block state.
188+
/// This function updates the global cache by removing entries
189+
/// that are invalidated by chain reorganization. `sync_cache`
190+
/// should be called after the block has been committed and the
191+
/// blockchain route has ben calculated.
185192
pub fn sync_cache(&mut self, enacted: &[H256], retracted: &[H256], is_best: bool) {
186193
trace!("sync_cache id = (#{:?}, {:?}), parent={:?}, best={}", self.commit_number, self.commit_hash, self.parent_hash, is_best);
187194
let mut cache = self.account_cache.lock();
188195
let mut cache = &mut *cache;
189196

190-
// Clean changes from re-enacted and retracted blocks.
197+
// Purge changes from re-enacted and retracted blocks.
191198
// Filter out commiting block if any.
192199
let mut clear = false;
193200
for block in enacted.iter().filter(|h| self.commit_hash.as_ref().map_or(true, |p| *h != p)) {
@@ -228,16 +235,16 @@ impl StateDB {
228235
cache.modifications.clear();
229236
}
230237

231-
// Apply cache changes only if committing on top of the latest canonical state
238+
// Propagate cache only if committing on top of the latest canonical state
232239
// blocks are ordered by number and only one block with a given number is marked as canonical
233240
// (contributed to canonical state cache)
234241
if let (Some(ref number), Some(ref hash), Some(ref parent)) = (self.commit_number, self.commit_hash, self.parent_hash) {
235242
if cache.modifications.len() == STATE_CACHE_BLOCKS {
236243
cache.modifications.pop_back();
237244
}
238245
let mut modifications = HashSet::new();
239-
trace!("committing {} cache entries", self.cache_buffer.len());
240-
for account in self.cache_buffer.drain(..) {
246+
trace!("committing {} cache entries", self.local_cache.len());
247+
for account in self.local_cache.drain(..) {
241248
if account.modified {
242249
modifications.insert(account.address.clone());
243250
}
@@ -287,7 +294,7 @@ impl StateDB {
287294
StateDB {
288295
db: self.db.boxed_clone(),
289296
account_cache: self.account_cache.clone(),
290-
cache_buffer: Vec::new(),
297+
local_cache: Vec::new(),
291298
account_bloom: self.account_bloom.clone(),
292299
parent_hash: None,
293300
commit_hash: None,
@@ -300,7 +307,7 @@ impl StateDB {
300307
StateDB {
301308
db: self.db.boxed_clone(),
302309
account_cache: self.account_cache.clone(),
303-
cache_buffer: Vec::new(),
310+
local_cache: Vec::new(),
304311
account_bloom: self.account_bloom.clone(),
305312
parent_hash: Some(parent.clone()),
306313
commit_hash: None,
@@ -323,10 +330,12 @@ impl StateDB {
323330
&*self.db
324331
}
325332

326-
/// Add pending cache change.
327-
/// The change is queued to be applied in `commit`.
333+
/// Add a local cache entry.
334+
/// The entry will be propagated to the global cache in `sync_cache`.
335+
/// `modified` indicates that the entry was changed since being read from disk or global cache.
336+
/// `data` can be set to an existing (`Some`), or non-existing account (`None`).
328337
pub fn add_to_account_cache(&mut self, addr: Address, data: Option<Account>, modified: bool) {
329-
self.cache_buffer.push(CacheQueueItem {
338+
self.local_cache.push(CacheQueueItem {
330339
address: addr,
331340
account: data,
332341
modified: modified,

0 commit comments

Comments
 (0)