16
16
17
17
//! Single account in the system.
18
18
19
- use std:: collections:: hash_map:: Entry ;
20
19
use util:: * ;
21
20
use pod_account:: * ;
22
21
use rlp:: * ;
23
22
use lru_cache:: LruCache ;
24
23
25
24
use std:: cell:: { RefCell , Cell } ;
26
25
27
- const STORAGE_CACHE_ITEMS : usize = 4096 ;
26
+ const STORAGE_CACHE_ITEMS : usize = 8192 ;
28
27
29
28
/// 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`
30
31
pub struct Account {
31
32
// Balance of the account.
32
33
balance : U256 ,
@@ -46,8 +47,6 @@ pub struct Account {
46
47
code_size : Option < usize > ,
47
48
// Code cache of the account.
48
49
code_cache : Arc < Bytes > ,
49
- // Account is new or has been modified.
50
- filth : Filth ,
51
50
// Account code new or has been modified.
52
51
code_filth : Filth ,
53
52
// Cached address hash.
@@ -67,7 +66,6 @@ impl Account {
67
66
code_hash : code. sha3 ( ) ,
68
67
code_size : Some ( code. len ( ) ) ,
69
68
code_cache : Arc :: new ( code) ,
70
- filth : Filth :: Dirty ,
71
69
code_filth : Filth :: Dirty ,
72
70
address_hash : Cell :: new ( None ) ,
73
71
}
@@ -89,7 +87,6 @@ impl Account {
89
87
code_filth : Filth :: Dirty ,
90
88
code_size : Some ( pod. code . as_ref ( ) . map_or ( 0 , |c| c. len ( ) ) ) ,
91
89
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 ,
93
90
address_hash : Cell :: new ( None ) ,
94
91
}
95
92
}
@@ -105,7 +102,6 @@ impl Account {
105
102
code_hash : SHA3_EMPTY ,
106
103
code_cache : Arc :: new ( vec ! [ ] ) ,
107
104
code_size : Some ( 0 ) ,
108
- filth : Filth :: Dirty ,
109
105
code_filth : Filth :: Clean ,
110
106
address_hash : Cell :: new ( None ) ,
111
107
}
@@ -123,7 +119,6 @@ impl Account {
123
119
code_hash : r. val_at ( 3 ) ,
124
120
code_cache : Arc :: new ( vec ! [ ] ) ,
125
121
code_size : None ,
126
- filth : Filth :: Clean ,
127
122
code_filth : Filth :: Clean ,
128
123
address_hash : Cell :: new ( None ) ,
129
124
}
@@ -141,7 +136,6 @@ impl Account {
141
136
code_hash : SHA3_EMPTY ,
142
137
code_cache : Arc :: new ( vec ! [ ] ) ,
143
138
code_size : None ,
144
- filth : Filth :: Dirty ,
145
139
code_filth : Filth :: Clean ,
146
140
address_hash : Cell :: new ( None ) ,
147
141
}
@@ -153,7 +147,6 @@ impl Account {
153
147
self . code_hash = code. sha3 ( ) ;
154
148
self . code_cache = Arc :: new ( code) ;
155
149
self . code_size = Some ( self . code_cache . len ( ) ) ;
156
- self . filth = Filth :: Dirty ;
157
150
self . code_filth = Filth :: Dirty ;
158
151
}
159
152
@@ -164,17 +157,7 @@ impl Account {
164
157
165
158
/// Set (and cache) the contents of the trie's storage at `key` to `value`.
166
159
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) ;
178
161
}
179
162
180
163
/// Get (and cache) the contents of the trie's storage at `key`.
@@ -263,17 +246,6 @@ impl Account {
263
246
!self . code_cache . is_empty ( ) || ( self . code_cache . is_empty ( ) && self . code_hash == SHA3_EMPTY )
264
247
}
265
248
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
-
277
249
/// Provide a database to get `code_hash`. Should not be called if it is a contract without code.
278
250
pub fn cache_code ( & mut self , db : & HashDB ) -> bool {
279
251
// TODO: fill out self.code_cache;
@@ -326,25 +298,18 @@ impl Account {
326
298
/// Increment the nonce of the account by one.
327
299
pub fn inc_nonce ( & mut self ) {
328
300
self . nonce = self . nonce + U256 :: from ( 1u8 ) ;
329
- self . filth = Filth :: Dirty ;
330
301
}
331
302
332
- /// Increment the nonce of the account by one .
303
+ /// Increase account balance .
333
304
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;
338
306
}
339
307
340
- /// Increment the nonce of the account by one .
308
+ /// Decrease account balance .
341
309
/// Panics if balance is less than `x`
342
310
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;
348
313
}
349
314
350
315
/// Commit the `storage_changes` to the backing DB and update `storage_root`.
@@ -406,7 +371,6 @@ impl Account {
406
371
code_hash : self . code_hash . clone ( ) ,
407
372
code_size : self . code_size . clone ( ) ,
408
373
code_cache : self . code_cache . clone ( ) ,
409
- filth : self . filth ,
410
374
code_filth : self . code_filth ,
411
375
address_hash : self . address_hash . clone ( ) ,
412
376
}
@@ -427,10 +391,10 @@ impl Account {
427
391
account
428
392
}
429
393
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.
431
397
pub fn merge_with ( & mut self , other : Account ) {
432
- assert ! ( self . storage_is_clean( ) ) ;
433
- assert ! ( other. storage_is_clean( ) ) ;
434
398
self . balance = other. balance ;
435
399
self . nonce = other. nonce ;
436
400
self . storage_root = other. storage_root ;
@@ -443,6 +407,7 @@ impl Account {
443
407
for ( k, v) in other. storage_cache . into_inner ( ) . into_iter ( ) {
444
408
cache. insert ( k. clone ( ) , v. clone ( ) ) ; //TODO: cloning should not be required here
445
409
}
410
+ self . storage_changes = other. storage_changes ;
446
411
}
447
412
}
448
413
0 commit comments