Skip to content

Commit bb6675e

Browse files
Clean up progressive balance slashings further (sigp#4834)
* Clean up progressive balance slashings further * Fix Rayon deadlock in test utils * Fix cargo-fmt
1 parent b121e69 commit bb6675e

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

beacon_node/beacon_chain/src/test_utils.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ use std::collections::{HashMap, HashSet};
4949
use std::fmt;
5050
use std::marker::PhantomData;
5151
use std::str::FromStr;
52+
use std::sync::atomic::{AtomicUsize, Ordering};
5253
use std::sync::Arc;
5354
use std::time::Duration;
5455
use store::{config::StoreConfig, HotColdDB, ItemStore, LevelDB, MemoryStore};
@@ -1149,9 +1150,9 @@ where
11491150
) -> (Vec<CommitteeAttestations<E>>, Vec<usize>) {
11501151
let MakeAttestationOptions { limit, fork } = opts;
11511152
let committee_count = state.get_committee_count_at_slot(state.slot()).unwrap();
1152-
let attesters = Mutex::new(vec![]);
1153+
let num_attesters = AtomicUsize::new(0);
11531154

1154-
let attestations = state
1155+
let (attestations, split_attesters) = state
11551156
.get_beacon_committees_at_slot(attestation_slot)
11561157
.expect("should get committees")
11571158
.iter()
@@ -1164,13 +1165,14 @@ where
11641165
return None;
11651166
}
11661167

1167-
let mut attesters = attesters.lock();
11681168
if let Some(limit) = limit {
1169-
if attesters.len() >= limit {
1169+
// This atomics stuff is necessary because we're under a par_iter,
1170+
// and Rayon will deadlock if we use a mutex.
1171+
if num_attesters.fetch_add(1, Ordering::Relaxed) >= limit {
1172+
num_attesters.fetch_sub(1, Ordering::Relaxed);
11701173
return None;
11711174
}
11721175
}
1173-
attesters.push(*validator_index);
11741176

11751177
let mut attestation = self
11761178
.produce_unaggregated_attestation_for_block(
@@ -1210,14 +1212,17 @@ where
12101212
)
12111213
.unwrap();
12121214

1213-
Some((attestation, subnet_id))
1215+
Some(((attestation, subnet_id), validator_index))
12141216
})
1215-
.collect::<Vec<_>>()
1217+
.unzip::<_, _, Vec<_>, Vec<_>>()
12161218
})
1217-
.collect::<Vec<_>>();
1219+
.unzip::<_, _, Vec<_>, Vec<_>>();
1220+
1221+
// Flatten attesters.
1222+
let attesters = split_attesters.into_iter().flatten().collect::<Vec<_>>();
12181223

1219-
let attesters = attesters.into_inner();
12201224
if let Some(limit) = limit {
1225+
assert_eq!(limit, num_attesters.load(Ordering::Relaxed));
12211226
assert_eq!(
12221227
limit,
12231228
attesters.len(),

consensus/state_processing/src/common/update_progressive_balances_cache.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ pub fn update_progressive_balances_on_attestation<T: EthSpec>(
6969
validator_effective_balance: u64,
7070
validator_slashed: bool,
7171
) -> Result<(), BlockProcessingError> {
72-
if is_progressive_balances_enabled(state) && !validator_slashed {
72+
if is_progressive_balances_enabled(state) {
7373
state.progressive_balances_cache_mut().on_new_attestation(
7474
epoch,
75+
validator_slashed,
7576
flag_index,
7677
validator_effective_balance,
7778
)?;

consensus/types/src/beacon_state/progressive_balances_cache.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,13 @@ impl EpochTotalBalances {
6464

6565
pub fn on_new_attestation(
6666
&mut self,
67+
is_slashed: bool,
6768
flag_index: usize,
6869
validator_effective_balance: u64,
6970
) -> Result<(), BeaconStateError> {
71+
if is_slashed {
72+
return Ok(());
73+
}
7074
let balance = self
7175
.total_flag_balances
7276
.get_mut(flag_index)
@@ -152,19 +156,24 @@ impl ProgressiveBalancesCache {
152156
pub fn on_new_attestation(
153157
&mut self,
154158
epoch: Epoch,
159+
is_slashed: bool,
155160
flag_index: usize,
156161
validator_effective_balance: u64,
157162
) -> Result<(), BeaconStateError> {
158163
let cache = self.get_inner_mut()?;
159164

160165
if epoch == cache.current_epoch {
161-
cache
162-
.current_epoch_cache
163-
.on_new_attestation(flag_index, validator_effective_balance)?;
166+
cache.current_epoch_cache.on_new_attestation(
167+
is_slashed,
168+
flag_index,
169+
validator_effective_balance,
170+
)?;
164171
} else if epoch.safe_add(1)? == cache.current_epoch {
165-
cache
166-
.previous_epoch_cache
167-
.on_new_attestation(flag_index, validator_effective_balance)?;
172+
cache.previous_epoch_cache.on_new_attestation(
173+
is_slashed,
174+
flag_index,
175+
validator_effective_balance,
176+
)?;
168177
} else {
169178
return Err(BeaconStateError::ProgressiveBalancesCacheInconsistent);
170179
}

0 commit comments

Comments
 (0)