@@ -3,195 +3,197 @@ const Tree = require('functional-red-black-tree')
3
3
const Account = require ( 'ethereumjs-account' )
4
4
const async = require ( 'async' )
5
5
6
- var Cache = module . exports = function ( trie ) {
7
- this . _cache = Tree ( )
8
- this . _checkpoints = [ ]
9
- this . _trie = trie
10
- }
11
-
12
- /**
13
- * Puts account to cache under its address.
14
- * @param {Buffer } key - Address of account
15
- * @param {Account } val - Account
16
- * @param {bool } fromTrie
17
- */
18
- Cache . prototype . put = function ( key , val , fromTrie ) {
19
- var modified = ! fromTrie
20
- this . _update ( key , val , modified , false )
21
- }
22
-
23
- /**
24
- * Returns the queried account or an empty account.
25
- * @param {Buffer } key - Address of account
26
- */
27
- Cache . prototype . get = function ( key ) {
28
- var account = this . lookup ( key )
29
- if ( ! account ) {
30
- account = new Account ( )
6
+ module . exports = class Cache {
7
+ constructor ( trie ) {
8
+ this . _cache = Tree ( )
9
+ this . _checkpoints = [ ]
10
+ this . _trie = trie
31
11
}
32
- return account
33
- }
34
12
35
- /**
36
- * Returns the queried account or undefined.
37
- * @param {buffer } key - Address of account
38
- */
39
- Cache . prototype . lookup = function ( key ) {
40
- key = key . toString ( 'hex' )
13
+ /**
14
+ * Puts account to cache under its address.
15
+ * @param {Buffer } key - Address of account
16
+ * @param {Account } val - Account
17
+ * @param {bool } fromTrie
18
+ */
19
+ put ( key , val , fromTrie ) {
20
+ var modified = ! fromTrie
21
+ this . _update ( key , val , modified , false )
22
+ }
41
23
42
- var it = this . _cache . find ( key )
43
- if ( it . node ) {
44
- var account = new Account ( it . value . val )
24
+ /**
25
+ * Returns the queried account or an empty account.
26
+ * @param {Buffer } key - Address of account
27
+ */
28
+ get ( key ) {
29
+ var account = this . lookup ( key )
30
+ if ( ! account ) {
31
+ account = new Account ( )
32
+ }
45
33
return account
46
34
}
47
- }
48
35
49
- /**
50
- * Looks up address in underlying trie .
51
- * @param {Buffer } address - Address of account
52
- * @param { Function } cb - Callback with params (err, account)
53
- */
54
- Cache . prototype . _lookupAccount = function ( address , cb ) {
55
- var self = this
56
- self . _trie . get ( address , function ( err , raw ) {
57
- if ( err ) return cb ( err )
58
- var account = new Account ( raw )
59
- cb ( null , account )
60
- } )
61
- }
36
+ /**
37
+ * Returns the queried account or undefined .
38
+ * @param {buffer } key - Address of account
39
+ */
40
+ lookup ( key ) {
41
+ key = key . toString ( 'hex' )
42
+
43
+ var it = this . _cache . find ( key )
44
+ if ( it . node ) {
45
+ var account = new Account ( it . value . val )
46
+ return account
47
+ }
48
+ }
62
49
63
- /**
64
- * Looks up address in cache, if not found, looks it up
65
- * in the underlying trie.
66
- * @param {Buffer } key - Address of account
67
- * @param {Function } cb - Callback with params (err, account)
68
- */
69
- Cache . prototype . getOrLoad = function ( key , cb ) {
70
- var self = this
71
- var account = this . lookup ( key )
72
- if ( account ) {
73
- async . nextTick ( cb , null , account )
74
- } else {
75
- self . _lookupAccount ( key , function ( err , account ) {
50
+ /**
51
+ * Looks up address in underlying trie.
52
+ * @param {Buffer } address - Address of account
53
+ * @param {Function } cb - Callback with params (err, account)
54
+ */
55
+ _lookupAccount ( address , cb ) {
56
+ var self = this
57
+ self . _trie . get ( address , function ( err , raw ) {
76
58
if ( err ) return cb ( err )
77
- self . _update ( key , account , false , false )
59
+ var account = new Account ( raw )
78
60
cb ( null , account )
79
61
} )
80
62
}
81
- }
82
63
83
- /**
84
- * Warms cache by loading their respective account from trie
85
- * and putting them in cache.
86
- * @param {Array } addresses - Array of addresses
87
- * @param {Function } cb - Callback
88
- */
89
- Cache . prototype . warm = function ( addresses , cb ) {
90
- var self = this
91
- // shim till async supports iterators
92
- var accountArr = [ ]
93
- addresses . forEach ( function ( val ) {
94
- if ( val ) accountArr . push ( val )
95
- } )
96
-
97
- async . eachSeries ( accountArr , function ( addressHex , done ) {
98
- var address = Buffer . from ( addressHex , 'hex' )
99
- self . _lookupAccount ( address , function ( err , account ) {
100
- if ( err ) return done ( err )
101
- self . _update ( address , account , false , false )
102
- done ( )
64
+ /**
65
+ * Looks up address in cache, if not found, looks it up
66
+ * in the underlying trie.
67
+ * @param {Buffer } key - Address of account
68
+ * @param {Function } cb - Callback with params (err, account)
69
+ */
70
+ getOrLoad ( key , cb ) {
71
+ var self = this
72
+ var account = this . lookup ( key )
73
+ if ( account ) {
74
+ async . nextTick ( cb , null , account )
75
+ } else {
76
+ self . _lookupAccount ( key , function ( err , account ) {
77
+ if ( err ) return cb ( err )
78
+ self . _update ( key , account , false , false )
79
+ cb ( null , account )
80
+ } )
81
+ }
82
+ }
83
+
84
+ /**
85
+ * Warms cache by loading their respective account from trie
86
+ * and putting them in cache.
87
+ * @param {Array } addresses - Array of addresses
88
+ * @param {Function } cb - Callback
89
+ */
90
+ warm ( addresses , cb ) {
91
+ var self = this
92
+ // shim till async supports iterators
93
+ var accountArr = [ ]
94
+ addresses . forEach ( function ( val ) {
95
+ if ( val ) accountArr . push ( val )
103
96
} )
104
- } , cb )
105
- }
106
97
107
- /**
108
- * Flushes cache by updating accounts that have been modified
109
- * and removing accounts that have been deleted.
110
- * @param {function } cb - Callback
111
- */
112
- Cache . prototype . flush = function ( cb ) {
113
- var it = this . _cache . begin
114
- var self = this
115
- var next = true
116
- async . whilst ( function ( ) {
117
- return next
118
- } , function ( done ) {
119
- if ( it . value && it . value . modified ) {
120
- it . value . modified = false
121
- it . value . val = it . value . val . serialize ( )
122
- self . _trie . put ( Buffer . from ( it . key , 'hex' ) , it . value . val , function ( ) {
123
- next = it . hasNext
124
- it . next ( )
98
+ async . eachSeries ( accountArr , function ( addressHex , done ) {
99
+ var address = Buffer . from ( addressHex , 'hex' )
100
+ self . _lookupAccount ( address , function ( err , account ) {
101
+ if ( err ) return done ( err )
102
+ self . _update ( address , account , false , false )
125
103
done ( )
126
104
} )
127
- } else if ( it . value && it . value . deleted ) {
128
- it . value . modified = false
129
- it . value . deleted = false
130
- it . value . val = ( new Account ( ) ) . serialize ( )
131
- self . _trie . del ( Buffer . from ( it . key , 'hex' ) , function ( ) {
105
+ } , cb )
106
+ }
107
+
108
+ /**
109
+ * Flushes cache by updating accounts that have been modified
110
+ * and removing accounts that have been deleted.
111
+ * @param {function } cb - Callback
112
+ */
113
+ flush ( cb ) {
114
+ var it = this . _cache . begin
115
+ var self = this
116
+ var next = true
117
+ async . whilst ( function ( ) {
118
+ return next
119
+ } , function ( done ) {
120
+ if ( it . value && it . value . modified ) {
121
+ it . value . modified = false
122
+ it . value . val = it . value . val . serialize ( )
123
+ self . _trie . put ( Buffer . from ( it . key , 'hex' ) , it . value . val , function ( ) {
124
+ next = it . hasNext
125
+ it . next ( )
126
+ done ( )
127
+ } )
128
+ } else if ( it . value && it . value . deleted ) {
129
+ it . value . modified = false
130
+ it . value . deleted = false
131
+ it . value . val = ( new Account ( ) ) . serialize ( )
132
+ self . _trie . del ( Buffer . from ( it . key , 'hex' ) , function ( ) {
133
+ next = it . hasNext
134
+ it . next ( )
135
+ done ( )
136
+ } )
137
+ } else {
132
138
next = it . hasNext
133
139
it . next ( )
134
- done ( )
135
- } )
136
- } else {
137
- next = it . hasNext
138
- it . next ( )
139
- async . nextTick ( done )
140
- }
141
- } , cb )
142
- }
140
+ async . nextTick ( done )
141
+ }
142
+ } , cb )
143
+ }
143
144
144
- /**
145
- * Marks current state of cache as checkpoint, which can
146
- * later on be reverted or commited.
147
- */
148
- Cache . prototype . checkpoint = function ( ) {
149
- this . _checkpoints . push ( this . _cache )
150
- }
145
+ /**
146
+ * Marks current state of cache as checkpoint, which can
147
+ * later on be reverted or commited.
148
+ */
149
+ checkpoint ( ) {
150
+ this . _checkpoints . push ( this . _cache )
151
+ }
151
152
152
- /**
153
- * Revert changes to cache last checkpoint (no effect on trie).
154
- */
155
- Cache . prototype . revert = function ( ) {
156
- this . _cache = this . _checkpoints . pop ( this . _cache )
157
- }
153
+ /**
154
+ * Revert changes to cache last checkpoint (no effect on trie).
155
+ */
156
+ revert ( ) {
157
+ this . _cache = this . _checkpoints . pop ( this . _cache )
158
+ }
158
159
159
- /**
160
- * Commits to current state of cache (no effect on trie).
161
- */
162
- Cache . prototype . commit = function ( ) {
163
- this . _checkpoints . pop ( )
164
- }
160
+ /**
161
+ * Commits to current state of cache (no effect on trie).
162
+ */
163
+ commit ( ) {
164
+ this . _checkpoints . pop ( )
165
+ }
165
166
166
- /**
167
- * Clears cache.
168
- */
169
- Cache . prototype . clear = function ( ) {
170
- this . _cache = Tree ( )
171
- }
167
+ /**
168
+ * Clears cache.
169
+ */
170
+ clear ( ) {
171
+ this . _cache = Tree ( )
172
+ }
172
173
173
- /**
174
- * Marks address as deleted in cache.
175
- * @params {Buffer} key - Address
176
- */
177
- Cache . prototype . del = function ( key ) {
178
- this . _update ( key , new Account ( ) , false , true )
179
- }
174
+ /**
175
+ * Marks address as deleted in cache.
176
+ * @params {Buffer} key - Address
177
+ */
178
+ del ( key ) {
179
+ this . _update ( key , new Account ( ) , false , true )
180
+ }
180
181
181
- Cache . prototype . _update = function ( key , val , modified , deleted ) {
182
- key = key . toString ( 'hex' )
183
- var it = this . _cache . find ( key )
184
- if ( it . node ) {
185
- this . _cache = it . update ( {
186
- val : val ,
187
- modified : modified ,
188
- deleted : deleted
189
- } )
190
- } else {
191
- this . _cache = this . _cache . insert ( key , {
192
- val : val ,
193
- modified : modified ,
194
- deleted : deleted
195
- } )
182
+ _update ( key , val , modified , deleted ) {
183
+ key = key . toString ( 'hex' )
184
+ var it = this . _cache . find ( key )
185
+ if ( it . node ) {
186
+ this . _cache = it . update ( {
187
+ val : val ,
188
+ modified : modified ,
189
+ deleted : deleted
190
+ } )
191
+ } else {
192
+ this . _cache = this . _cache . insert ( key , {
193
+ val : val ,
194
+ modified : modified ,
195
+ deleted : deleted
196
+ } )
197
+ }
196
198
}
197
199
}
0 commit comments