@@ -133,30 +133,18 @@ impl ChainData {
133
133
self . account_bytes_stored = 0 ;
134
134
135
135
for ( _, writes) in self . accounts . iter_mut ( ) {
136
- let newest_rooted_write = writes
137
- . iter ( )
138
- . rev ( )
139
- . find ( |w| {
140
- w. slot <= self . newest_rooted_slot
141
- && self
142
- . slots
143
- . get ( & w. slot )
144
- . map ( |s| {
145
- // sometimes we seem not to get notifications about slots
146
- // getting rooted, hence assume non-uncle slots < newest_rooted_slot
147
- // are rooted too
148
- s. status == SlotStatus :: Rooted
149
- || s. chain == self . best_chain_slot
150
- } )
151
- // preserved account writes for deleted slots <= newest_rooted_slot
152
- // are expected to be rooted
153
- . unwrap_or ( true )
154
- } )
155
- . map ( |w| w. slot )
156
- // no rooted write found: produce no effect, since writes > newest_rooted_slot are retained anyway
157
- . unwrap_or ( self . newest_rooted_slot + 1 ) ;
158
- writes
159
- . retain ( |w| w. slot == newest_rooted_write || w. slot > self . newest_rooted_slot ) ;
136
+ let newest_rooted_write_slot = Self :: newest_rooted_write (
137
+ writes,
138
+ self . newest_rooted_slot ,
139
+ self . best_chain_slot ,
140
+ & self . slots ,
141
+ )
142
+ . map ( |w| w. slot )
143
+ // no rooted write found: produce no effect, since writes > newest_rooted_slot are retained anyway
144
+ . unwrap_or ( self . newest_rooted_slot + 1 ) ;
145
+ writes. retain ( |w| {
146
+ w. slot == newest_rooted_write_slot || w. slot > self . newest_rooted_slot
147
+ } ) ;
160
148
self . account_versions_stored += writes. len ( ) ;
161
149
self . account_bytes_stored +=
162
150
writes. iter ( ) . map ( |w| w. account . data ( ) . len ( ) ) . sum :: < usize > ( )
@@ -212,17 +200,32 @@ impl ChainData {
212
200
. unwrap_or ( write. slot <= self . newest_rooted_slot || write. slot > self . best_chain_slot )
213
201
}
214
202
203
+ fn newest_rooted_write < ' a > (
204
+ writes : & ' a [ AccountData ] ,
205
+ newest_rooted_slot : u64 ,
206
+ best_chain_slot : u64 ,
207
+ slots : & HashMap < u64 , SlotData > ,
208
+ ) -> Option < & ' a AccountData > {
209
+ writes. iter ( ) . rev ( ) . find ( |w| {
210
+ w. slot <= newest_rooted_slot
211
+ && slots
212
+ . get ( & w. slot )
213
+ . map ( |s| {
214
+ // sometimes we seem not to get notifications about slots
215
+ // getting rooted, hence assume non-uncle slots < newest_rooted_slot
216
+ // are rooted too
217
+ s. status == SlotStatus :: Rooted || s. chain == best_chain_slot
218
+ } )
219
+ // preserved account writes for deleted slots <= newest_rooted_slot
220
+ // are expected to be rooted
221
+ . unwrap_or ( true )
222
+ } )
223
+ }
224
+
215
225
/// Cloned snapshot of all the most recent live writes per pubkey
216
226
pub fn accounts_snapshot ( & self ) -> HashMap < Pubkey , AccountData > {
217
- self . accounts
218
- . iter ( )
219
- . filter_map ( |( pubkey, writes) | {
220
- let latest_good_write = writes
221
- . iter ( )
222
- . rev ( )
223
- . find ( |w| self . is_account_write_live ( w) ) ?;
224
- Some ( ( * pubkey, latest_good_write. clone ( ) ) )
225
- } )
227
+ self . iter_accounts ( )
228
+ . map ( |( pk, a) | ( * pk, a. clone ( ) ) )
226
229
. collect ( )
227
230
}
228
231
@@ -237,6 +240,7 @@ impl ChainData {
237
240
. ok_or_else ( || anyhow:: anyhow!( "account {} has no live data" , pubkey) )
238
241
}
239
242
243
+ /// Iterate over the most recent live data for all stored accounts
240
244
pub fn iter_accounts ( & self ) -> impl Iterator < Item = ( & Pubkey , & AccountData ) > {
241
245
self . accounts . iter ( ) . filter_map ( |( pk, writes) | {
242
246
writes
@@ -247,6 +251,19 @@ impl ChainData {
247
251
} )
248
252
}
249
253
254
+ /// Iterate over the most recent rooted data for all stored accounts
255
+ pub fn iter_accounts_rooted ( & self ) -> impl Iterator < Item = ( & Pubkey , & AccountData ) > {
256
+ self . accounts . iter ( ) . filter_map ( |( pk, writes) | {
257
+ Self :: newest_rooted_write (
258
+ writes,
259
+ self . newest_rooted_slot ,
260
+ self . best_chain_slot ,
261
+ & self . slots ,
262
+ )
263
+ . map ( |latest_write| ( pk, latest_write) )
264
+ } )
265
+ }
266
+
250
267
pub fn slots_count ( & self ) -> usize {
251
268
self . slots . len ( )
252
269
}
@@ -270,6 +287,18 @@ impl ChainData {
270
287
pub fn newest_rooted_slot ( & self ) -> u64 {
271
288
self . newest_rooted_slot
272
289
}
290
+
291
+ pub fn newest_processed_slot ( & self ) -> u64 {
292
+ self . newest_processed_slot
293
+ }
294
+
295
+ pub fn raw_account_data ( & self ) -> & HashMap < Pubkey , Vec < AccountData > > {
296
+ & self . accounts
297
+ }
298
+
299
+ pub fn raw_slot_data ( & self ) -> & HashMap < u64 , SlotData > {
300
+ & self . slots
301
+ }
273
302
}
274
303
275
304
pub struct ChainDataMetrics {
0 commit comments