Skip to content

Commit 014e076

Browse files
changed Arc[Validator] to Vec<Arc<Validator>> and modified the compute and apply functions, alongside tests
1 parent a551c84 commit 014e076

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

beacon_node/store/src/hdiff.rs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub struct HDiffBuffer {
100100
state: Vec<u8>,
101101
balances: Vec<u64>,
102102
inactivity_scores: Vec<u64>,
103-
validators: Arc<[Validator]>,
103+
validators: Vec<Arc<Validator>>,
104104
historical_roots: Vec<Hash256>,
105105
historical_summaries: Vec<HistoricalSummary>,
106106
}
@@ -183,8 +183,11 @@ impl HDiffBuffer {
183183
// is post altair, all its items will show up in the diff as is.
184184
vec![]
185185
};
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<_>>();
188191
let historical_roots = std::mem::take(beacon_state.historical_roots_mut()).to_vec();
189192
let historical_summaries =
190193
if let Ok(historical_summaries) = beacon_state.historical_summaries_mut() {
@@ -222,8 +225,9 @@ impl HDiffBuffer {
222225
.map_err(|_| Error::InvalidBalancesLength)?;
223226
}
224227

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)?;
227231

228232
*state.historical_roots_mut() = List::try_from_iter(self.historical_roots.iter().copied())
229233
.map_err(|_| Error::InvalidBalancesLength)?;
@@ -283,10 +287,8 @@ impl HDiff {
283287
self.balances_diff().apply(&mut source.balances, config)?;
284288
self.inactivity_scores_diff()
285289
.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)?;
290292

291293
self.historical_roots().apply(&mut source.historical_roots);
292294
self.historical_summaries()
@@ -450,8 +452,8 @@ fn uncompress_bytes(input: &[u8], config: &StoreConfig) -> Result<Vec<u8>, Error
450452

451453
impl ValidatorsDiff {
452454
pub fn compute(
453-
xs: &[Validator],
454-
ys: &[Validator],
455+
xs: &[Arc<Validator>],
456+
ys: &[Arc<Validator>],
455457
config: &StoreConfig,
456458
) -> Result<Self, Error> {
457459
if xs.len() > ys.len() {
@@ -461,8 +463,10 @@ impl ValidatorsDiff {
461463
let uncompressed_bytes = ys
462464
.iter()
463465
.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;
466470
if y == x {
467471
return None;
468472
} else {
@@ -542,7 +546,7 @@ impl ValidatorsDiff {
542546
})
543547
}
544548

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> {
546550
let validator_diff_bytes = uncompress_bytes(&self.bytes, config)?;
547551

548552
for diff_bytes in
@@ -554,7 +558,8 @@ impl ValidatorsDiff {
554558
} = ValidatorDiffEntry::from_ssz_bytes(diff_bytes)
555559
.map_err(|_| Error::BalancesIncompleteChunk)?;
556560

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();
558563
// Note: a pubkey change implies index re-use. In that case over-write
559564
// withdrawal_credentials and slashed inconditionally as their default values
560565
// are valid values.
@@ -584,7 +589,7 @@ impl ValidatorsDiff {
584589
x.withdrawable_epoch = diff.withdrawable_epoch;
585590
}
586591
} else {
587-
xs.push(diff)
592+
xs.push(Arc::new(diff))
588593
}
589594
}
590595

@@ -922,10 +927,11 @@ mod tests {
922927
let config = &StoreConfig::default();
923928
let xs = (0..10)
924929
.map(|_| rand_validator(&mut rng))
930+
.map(Arc::new)
925931
.collect::<Vec<_>>();
926932
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)));
929935
let diff = ValidatorsDiff::compute(&xs, &ys, config).unwrap();
930936

931937
let mut xs_out = xs.clone();
@@ -963,7 +969,10 @@ mod tests {
963969
let pre_inactivity_scores = vec![1, 1, 1];
964970
let post_inactivity_scores = vec![0, 0, 0, 1];
965971

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<_>>();
967976
let post_validators = pre_validators.clone();
968977

969978
let pre_historical_roots = vec![Hash256::repeat_byte(0xff)];
@@ -976,15 +985,15 @@ mod tests {
976985
state: vec![0, 1, 2, 3, 3, 2, 1, 0],
977986
balances: pre_balances,
978987
inactivity_scores: pre_inactivity_scores,
979-
validators: Arc::from(pre_validators),
988+
validators: pre_validators,
980989
historical_roots: pre_historical_roots,
981990
historical_summaries: pre_historical_summaries,
982991
};
983992
let post_buffer = HDiffBuffer {
984993
state: vec![0, 1, 3, 2, 2, 3, 1, 1],
985994
balances: post_balances,
986995
inactivity_scores: post_inactivity_scores,
987-
validators: Arc::from(post_validators),
996+
validators: post_validators,
988997
historical_roots: post_historical_roots,
989998
historical_summaries: post_historical_summaries,
990999
};

0 commit comments

Comments
 (0)