@@ -100,7 +100,7 @@ pub struct HDiffBuffer {
100
100
state : Vec < u8 > ,
101
101
balances : Vec < u64 > ,
102
102
inactivity_scores : Vec < u64 > ,
103
- validators : Arc < [ Validator ] > ,
103
+ validators : Vec < Arc < Validator > > ,
104
104
historical_roots : Vec < Hash256 > ,
105
105
historical_summaries : Vec < HistoricalSummary > ,
106
106
}
@@ -183,8 +183,11 @@ impl HDiffBuffer {
183
183
// is post altair, all its items will show up in the diff as is.
184
184
vec ! [ ]
185
185
} ;
186
- let validators = std:: mem:: take ( beacon_state. validators_mut ( ) ) . to_vec ( ) ;
187
- let validators = Arc :: from ( validators) ;
186
+ let validators = std:: mem:: take ( beacon_state. validators_mut ( ) )
187
+ . iter ( )
188
+ . cloned ( )
189
+ . map ( Arc :: new)
190
+ . collect :: < Vec < _ > > ( ) ;
188
191
let historical_roots = std:: mem:: take ( beacon_state. historical_roots_mut ( ) ) . to_vec ( ) ;
189
192
let historical_summaries =
190
193
if let Ok ( historical_summaries) = beacon_state. historical_summaries_mut ( ) {
@@ -222,8 +225,9 @@ impl HDiffBuffer {
222
225
. map_err ( |_| Error :: InvalidBalancesLength ) ?;
223
226
}
224
227
225
- * state. validators_mut ( ) = List :: try_from_iter ( self . validators . iter ( ) . cloned ( ) )
226
- . map_err ( |_| Error :: InvalidBalancesLength ) ?;
228
+ * state. validators_mut ( ) =
229
+ List :: try_from_iter ( self . validators . iter ( ) . map ( |arc| ( * * arc) . clone ( ) ) )
230
+ . map_err ( |_| Error :: InvalidBalancesLength ) ?;
227
231
228
232
* state. historical_roots_mut ( ) = List :: try_from_iter ( self . historical_roots . iter ( ) . copied ( ) )
229
233
. map_err ( |_| Error :: InvalidBalancesLength ) ?;
@@ -283,10 +287,8 @@ impl HDiff {
283
287
self . balances_diff ( ) . apply ( & mut source. balances , config) ?;
284
288
self . inactivity_scores_diff ( )
285
289
. apply ( & mut source. inactivity_scores , config) ?;
286
-
287
- let mut validators_vec = source. validators . to_vec ( ) ;
288
- self . validators_diff ( ) . apply ( & mut validators_vec, config) ?;
289
- source. validators = Arc :: from ( validators_vec) ;
290
+ self . validators_diff ( )
291
+ . apply ( & mut source. validators , config) ?;
290
292
291
293
self . historical_roots ( ) . apply ( & mut source. historical_roots ) ;
292
294
self . historical_summaries ( )
@@ -450,8 +452,8 @@ fn uncompress_bytes(input: &[u8], config: &StoreConfig) -> Result<Vec<u8>, Error
450
452
451
453
impl ValidatorsDiff {
452
454
pub fn compute (
453
- xs : & [ Validator ] ,
454
- ys : & [ Validator ] ,
455
+ xs : & [ Arc < Validator > ] ,
456
+ ys : & [ Arc < Validator > ] ,
455
457
config : & StoreConfig ,
456
458
) -> Result < Self , Error > {
457
459
if xs. len ( ) > ys. len ( ) {
@@ -461,8 +463,10 @@ impl ValidatorsDiff {
461
463
let uncompressed_bytes = ys
462
464
. iter ( )
463
465
. enumerate ( )
464
- . filter_map ( |( i, y) | {
465
- let validator_diff = if let Some ( x) = xs. get ( i) {
466
+ . filter_map ( |( i, y_arc) | {
467
+ let y = & * * y_arc;
468
+ let validator_diff = if let Some ( x_arc) = xs. get ( i) {
469
+ let x = & * * x_arc;
466
470
if y == x {
467
471
return None ;
468
472
} else {
@@ -542,7 +546,7 @@ impl ValidatorsDiff {
542
546
} )
543
547
}
544
548
545
- pub fn apply ( & self , xs : & mut Vec < Validator > , config : & StoreConfig ) -> Result < ( ) , Error > {
549
+ pub fn apply ( & self , xs : & mut Vec < Arc < Validator > > , config : & StoreConfig ) -> Result < ( ) , Error > {
546
550
let validator_diff_bytes = uncompress_bytes ( & self . bytes , config) ?;
547
551
548
552
for diff_bytes in
@@ -554,7 +558,8 @@ impl ValidatorsDiff {
554
558
} = ValidatorDiffEntry :: from_ssz_bytes ( diff_bytes)
555
559
. map_err ( |_| Error :: BalancesIncompleteChunk ) ?;
556
560
557
- if let Some ( x) = xs. get_mut ( index as usize ) {
561
+ if let Some ( x_arc) = xs. get_mut ( index as usize ) {
562
+ let mut x = ( * * x_arc) . clone ( ) ;
558
563
// Note: a pubkey change implies index re-use. In that case over-write
559
564
// withdrawal_credentials and slashed inconditionally as their default values
560
565
// are valid values.
@@ -584,7 +589,7 @@ impl ValidatorsDiff {
584
589
x. withdrawable_epoch = diff. withdrawable_epoch ;
585
590
}
586
591
} else {
587
- xs. push ( diff)
592
+ xs. push ( Arc :: new ( diff) )
588
593
}
589
594
}
590
595
@@ -922,10 +927,11 @@ mod tests {
922
927
let config = & StoreConfig :: default ( ) ;
923
928
let xs = ( 0 ..10 )
924
929
. map ( |_| rand_validator ( & mut rng) )
930
+ . map ( Arc :: new)
925
931
. collect :: < Vec < _ > > ( ) ;
926
932
let mut ys = xs. clone ( ) ;
927
- ys[ 5 ] = rand_validator ( & mut rng) ;
928
- ys. push ( rand_validator ( & mut rng) ) ;
933
+ ys[ 5 ] = Arc :: new ( rand_validator ( & mut rng) ) ;
934
+ ys. push ( Arc :: new ( rand_validator ( & mut rng) ) ) ;
929
935
let diff = ValidatorsDiff :: compute ( & xs, & ys, config) . unwrap ( ) ;
930
936
931
937
let mut xs_out = xs. clone ( ) ;
@@ -963,7 +969,10 @@ mod tests {
963
969
let pre_inactivity_scores = vec ! [ 1 , 1 , 1 ] ;
964
970
let post_inactivity_scores = vec ! [ 0 , 0 , 0 , 1 ] ;
965
971
966
- let pre_validators = ( 0 ..3 ) . map ( |_| rand_validator ( & mut rng) ) . collect :: < Vec < _ > > ( ) ;
972
+ let pre_validators = ( 0 ..3 )
973
+ . map ( |_| rand_validator ( & mut rng) )
974
+ . map ( Arc :: new)
975
+ . collect :: < Vec < _ > > ( ) ;
967
976
let post_validators = pre_validators. clone ( ) ;
968
977
969
978
let pre_historical_roots = vec ! [ Hash256 :: repeat_byte( 0xff ) ] ;
@@ -976,15 +985,15 @@ mod tests {
976
985
state : vec ! [ 0 , 1 , 2 , 3 , 3 , 2 , 1 , 0 ] ,
977
986
balances : pre_balances,
978
987
inactivity_scores : pre_inactivity_scores,
979
- validators : Arc :: from ( pre_validators) ,
988
+ validators : pre_validators,
980
989
historical_roots : pre_historical_roots,
981
990
historical_summaries : pre_historical_summaries,
982
991
} ;
983
992
let post_buffer = HDiffBuffer {
984
993
state : vec ! [ 0 , 1 , 3 , 2 , 2 , 3 , 1 , 1 ] ,
985
994
balances : post_balances,
986
995
inactivity_scores : post_inactivity_scores,
987
- validators : Arc :: from ( post_validators) ,
996
+ validators : post_validators,
988
997
historical_roots : post_historical_roots,
989
998
historical_summaries : post_historical_summaries,
990
999
} ;
0 commit comments