@@ -74,13 +74,20 @@ struct BlockChanges {
74
74
/// For canonical clones cache changes are accumulated and applied
75
75
/// on commit.
76
76
/// 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.
77
84
pub struct StateDB {
78
85
/// Backing database.
79
86
db : Box < JournalDB > ,
80
87
/// Shared canonical state cache.
81
88
account_cache : Arc < Mutex < AccountCache > > ,
82
- /// Local cache buffer .
83
- cache_buffer : Vec < CacheQueueItem > ,
89
+ /// Local dirty cache .
90
+ local_cache : Vec < CacheQueueItem > ,
84
91
/// Shared account bloom. Does not handle chain reorganizations.
85
92
account_bloom : Arc < Mutex < Bloom > > ,
86
93
/// Hash of the block on top of which this instance was created or
@@ -129,7 +136,7 @@ impl StateDB {
129
136
accounts : LruCache :: new ( STATE_CACHE_ITEMS ) ,
130
137
modifications : VecDeque :: new ( ) ,
131
138
} ) ) ,
132
- cache_buffer : Vec :: new ( ) ,
139
+ local_cache : Vec :: new ( ) ,
133
140
account_bloom : Arc :: new ( Mutex :: new ( bloom) ) ,
134
141
parent_hash : None ,
135
142
commit_hash : None ,
@@ -176,18 +183,18 @@ impl StateDB {
176
183
Ok ( records)
177
184
}
178
185
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.
185
192
pub fn sync_cache ( & mut self , enacted : & [ H256 ] , retracted : & [ H256 ] , is_best : bool ) {
186
193
trace ! ( "sync_cache id = (#{:?}, {:?}), parent={:?}, best={}" , self . commit_number, self . commit_hash, self . parent_hash, is_best) ;
187
194
let mut cache = self . account_cache . lock ( ) ;
188
195
let mut cache = & mut * cache;
189
196
190
- // Clean changes from re-enacted and retracted blocks.
197
+ // Purge changes from re-enacted and retracted blocks.
191
198
// Filter out commiting block if any.
192
199
let mut clear = false ;
193
200
for block in enacted. iter ( ) . filter ( |h| self . commit_hash . as_ref ( ) . map_or ( true , |p| * h != p) ) {
@@ -228,16 +235,16 @@ impl StateDB {
228
235
cache. modifications . clear ( ) ;
229
236
}
230
237
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
232
239
// blocks are ordered by number and only one block with a given number is marked as canonical
233
240
// (contributed to canonical state cache)
234
241
if let ( Some ( ref number) , Some ( ref hash) , Some ( ref parent) ) = ( self . commit_number , self . commit_hash , self . parent_hash ) {
235
242
if cache. modifications . len ( ) == STATE_CACHE_BLOCKS {
236
243
cache. modifications . pop_back ( ) ;
237
244
}
238
245
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 ( ..) {
241
248
if account. modified {
242
249
modifications. insert ( account. address . clone ( ) ) ;
243
250
}
@@ -287,7 +294,7 @@ impl StateDB {
287
294
StateDB {
288
295
db : self . db . boxed_clone ( ) ,
289
296
account_cache : self . account_cache . clone ( ) ,
290
- cache_buffer : Vec :: new ( ) ,
297
+ local_cache : Vec :: new ( ) ,
291
298
account_bloom : self . account_bloom . clone ( ) ,
292
299
parent_hash : None ,
293
300
commit_hash : None ,
@@ -300,7 +307,7 @@ impl StateDB {
300
307
StateDB {
301
308
db : self . db . boxed_clone ( ) ,
302
309
account_cache : self . account_cache . clone ( ) ,
303
- cache_buffer : Vec :: new ( ) ,
310
+ local_cache : Vec :: new ( ) ,
304
311
account_bloom : self . account_bloom . clone ( ) ,
305
312
parent_hash : Some ( parent. clone ( ) ) ,
306
313
commit_hash : None ,
@@ -323,10 +330,12 @@ impl StateDB {
323
330
& * self . db
324
331
}
325
332
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`).
328
337
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 {
330
339
address : addr,
331
340
account : data,
332
341
modified : modified,
0 commit comments