@@ -29,22 +29,22 @@ impl<'a, T: 'a + Clone> AccountsIndex<T> {
29
29
{
30
30
for ( pubkey, list) in iter {
31
31
let list_r = & list. 1 . read ( ) . unwrap ( ) ;
32
- if let Some ( index) = self . latest_slot ( ancestors, & list_r) {
32
+ if let Some ( index) = self . latest_slot ( Some ( ancestors) , & list_r) {
33
33
func ( pubkey, ( & list_r[ index] . 1 , list_r[ index] . 0 ) ) ;
34
34
}
35
35
}
36
36
}
37
37
38
38
/// call func with every pubkey and index visible from a given set of ancestors
39
- pub fn scan_accounts < F > ( & self , ancestors : & Ancestors , func : F )
39
+ pub ( crate ) fn scan_accounts < F > ( & self , ancestors : & Ancestors , func : F )
40
40
where
41
41
F : FnMut ( & Pubkey , ( & T , Slot ) ) ,
42
42
{
43
43
self . do_scan_accounts ( ancestors, func, self . account_maps . iter ( ) ) ;
44
44
}
45
45
46
46
/// call func with every pubkey and index visible from a given set of ancestors with range
47
- pub fn range_scan_accounts < F , R > ( & self , ancestors : & Ancestors , range : R , func : F )
47
+ pub ( crate ) fn range_scan_accounts < F , R > ( & self , ancestors : & Ancestors , range : R , func : F )
48
48
where
49
49
F : FnMut ( & Pubkey , ( & T , Slot ) ) ,
50
50
R : RangeBounds < Pubkey > ,
@@ -76,11 +76,14 @@ impl<'a, T: 'a + Clone> AccountsIndex<T> {
76
76
77
77
// find the latest slot and T in a slice for a given ancestor
78
78
// returns index into 'slice' if found, None if not.
79
- fn latest_slot ( & self , ancestors : & Ancestors , slice : SlotSlice < T > ) -> Option < usize > {
79
+ fn latest_slot ( & self , ancestors : Option < & Ancestors > , slice : SlotSlice < T > ) -> Option < usize > {
80
80
let mut max = 0 ;
81
81
let mut rv = None ;
82
82
for ( i, ( slot, _t) ) in slice. iter ( ) . rev ( ) . enumerate ( ) {
83
- if * slot >= max && ( ancestors. contains_key ( slot) || self . is_root ( * slot) ) {
83
+ if * slot >= max
84
+ && ( ancestors. map_or ( false , |ancestors| ancestors. contains_key ( slot) )
85
+ || self . is_root ( * slot) )
86
+ {
84
87
rv = Some ( ( slice. len ( ) - 1 ) - i) ;
85
88
max = * slot;
86
89
}
@@ -90,10 +93,10 @@ impl<'a, T: 'a + Clone> AccountsIndex<T> {
90
93
91
94
/// Get an account
92
95
/// The latest account that appears in `ancestors` or `roots` is returned.
93
- pub fn get (
96
+ pub ( crate ) fn get (
94
97
& self ,
95
98
pubkey : & Pubkey ,
96
- ancestors : & Ancestors ,
99
+ ancestors : Option < & Ancestors > ,
97
100
) -> Option < ( RwLockReadGuard < SlotList < T > > , usize ) > {
98
101
self . account_maps . get ( pubkey) . and_then ( |list| {
99
102
let list_r = list. 1 . read ( ) . unwrap ( ) ;
@@ -245,7 +248,8 @@ mod tests {
245
248
let key = Keypair :: new ( ) ;
246
249
let index = AccountsIndex :: < bool > :: default ( ) ;
247
250
let ancestors = HashMap :: new ( ) ;
248
- assert ! ( index. get( & key. pubkey( ) , & ancestors) . is_none( ) ) ;
251
+ assert ! ( index. get( & key. pubkey( ) , Some ( & ancestors) ) . is_none( ) ) ;
252
+ assert ! ( index. get( & key. pubkey( ) , None ) . is_none( ) ) ;
249
253
250
254
let mut num = 0 ;
251
255
index. scan_accounts ( & ancestors, |_pubkey, _index| num += 1 ) ;
@@ -261,7 +265,8 @@ mod tests {
261
265
assert ! ( gc. is_empty( ) ) ;
262
266
263
267
let ancestors = HashMap :: new ( ) ;
264
- assert ! ( index. get( & key. pubkey( ) , & ancestors) . is_none( ) ) ;
268
+ assert ! ( index. get( & key. pubkey( ) , Some ( & ancestors) ) . is_none( ) ) ;
269
+ assert ! ( index. get( & key. pubkey( ) , None ) . is_none( ) ) ;
265
270
266
271
let mut num = 0 ;
267
272
index. scan_accounts ( & ancestors, |_pubkey, _index| num += 1 ) ;
@@ -277,7 +282,7 @@ mod tests {
277
282
assert ! ( gc. is_empty( ) ) ;
278
283
279
284
let ancestors = vec ! [ ( 1 , 1 ) ] . into_iter ( ) . collect ( ) ;
280
- assert ! ( index. get( & key. pubkey( ) , & ancestors) . is_none( ) ) ;
285
+ assert ! ( index. get( & key. pubkey( ) , Some ( & ancestors) ) . is_none( ) ) ;
281
286
282
287
let mut num = 0 ;
283
288
index. scan_accounts ( & ancestors, |_pubkey, _index| num += 1 ) ;
@@ -293,7 +298,7 @@ mod tests {
293
298
assert ! ( gc. is_empty( ) ) ;
294
299
295
300
let ancestors = vec ! [ ( 0 , 0 ) ] . into_iter ( ) . collect ( ) ;
296
- let ( list, idx) = index. get ( & key. pubkey ( ) , & ancestors) . unwrap ( ) ;
301
+ let ( list, idx) = index. get ( & key. pubkey ( ) , Some ( & ancestors) ) . unwrap ( ) ;
297
302
assert_eq ! ( list[ idx] , ( 0 , true ) ) ;
298
303
299
304
let mut num = 0 ;
@@ -324,9 +329,8 @@ mod tests {
324
329
index. insert ( 0 , & key. pubkey ( ) , true , & mut gc) ;
325
330
assert ! ( gc. is_empty( ) ) ;
326
331
327
- let ancestors = vec ! [ ] . into_iter ( ) . collect ( ) ;
328
332
index. add_root ( 0 ) ;
329
- let ( list, idx) = index. get ( & key. pubkey ( ) , & ancestors ) . unwrap ( ) ;
333
+ let ( list, idx) = index. get ( & key. pubkey ( ) , None ) . unwrap ( ) ;
330
334
assert_eq ! ( list[ idx] , ( 0 , true ) ) ;
331
335
}
332
336
@@ -369,14 +373,14 @@ mod tests {
369
373
let mut gc = Vec :: new ( ) ;
370
374
index. insert ( 0 , & key. pubkey ( ) , true , & mut gc) ;
371
375
assert ! ( gc. is_empty( ) ) ;
372
- let ( list, idx) = index. get ( & key. pubkey ( ) , & ancestors) . unwrap ( ) ;
376
+ let ( list, idx) = index. get ( & key. pubkey ( ) , Some ( & ancestors) ) . unwrap ( ) ;
373
377
assert_eq ! ( list[ idx] , ( 0 , true ) ) ;
374
378
drop ( list) ;
375
379
376
380
let mut gc = Vec :: new ( ) ;
377
381
index. insert ( 0 , & key. pubkey ( ) , false , & mut gc) ;
378
382
assert_eq ! ( gc, vec![ ( 0 , true ) ] ) ;
379
- let ( list, idx) = index. get ( & key. pubkey ( ) , & ancestors) . unwrap ( ) ;
383
+ let ( list, idx) = index. get ( & key. pubkey ( ) , Some ( & ancestors) ) . unwrap ( ) ;
380
384
assert_eq ! ( list[ idx] , ( 0 , false ) ) ;
381
385
}
382
386
@@ -391,10 +395,10 @@ mod tests {
391
395
assert ! ( gc. is_empty( ) ) ;
392
396
index. insert ( 1 , & key. pubkey ( ) , false , & mut gc) ;
393
397
assert ! ( gc. is_empty( ) ) ;
394
- let ( list, idx) = index. get ( & key. pubkey ( ) , & ancestors) . unwrap ( ) ;
398
+ let ( list, idx) = index. get ( & key. pubkey ( ) , Some ( & ancestors) ) . unwrap ( ) ;
395
399
assert_eq ! ( list[ idx] , ( 0 , true ) ) ;
396
400
let ancestors = vec ! [ ( 1 , 0 ) ] . into_iter ( ) . collect ( ) ;
397
- let ( list, idx) = index. get ( & key. pubkey ( ) , & ancestors) . unwrap ( ) ;
401
+ let ( list, idx) = index. get ( & key. pubkey ( ) , Some ( & ancestors) ) . unwrap ( ) ;
398
402
assert_eq ! ( list[ idx] , ( 1 , false ) ) ;
399
403
}
400
404
@@ -413,13 +417,12 @@ mod tests {
413
417
index. add_root ( 3 ) ;
414
418
index. insert ( 4 , & key. pubkey ( ) , true , & mut gc) ;
415
419
assert_eq ! ( gc, vec![ ( 0 , true ) , ( 1 , false ) , ( 2 , true ) ] ) ;
416
- let ancestors = vec ! [ ] . into_iter ( ) . collect ( ) ;
417
- let ( list, idx) = index. get ( & key. pubkey ( ) , & ancestors) . unwrap ( ) ;
420
+ let ( list, idx) = index. get ( & key. pubkey ( ) , None ) . unwrap ( ) ;
418
421
assert_eq ! ( list[ idx] , ( 3 , true ) ) ;
419
422
420
423
let mut num = 0 ;
421
424
let mut found_key = false ;
422
- index. scan_accounts ( & ancestors , |pubkey, _index| {
425
+ index. scan_accounts ( & Ancestors :: new ( ) , |pubkey, _index| {
423
426
if pubkey == & key. pubkey ( ) {
424
427
found_key = true ;
425
428
assert_eq ! ( _index, ( & true , 3 ) ) ;
0 commit comments