Skip to content

Commit

Permalink
Track slashed indices
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Feb 9, 2023
1 parent 6af13e0 commit 8ad941d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
7 changes: 6 additions & 1 deletion beacon_node/beacon_chain/src/beacon_fork_choice_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use proto_array::JustifiedBalances;
use safe_arith::ArithError;
use ssz_derive::{Decode, Encode};
use std::collections::BTreeSet;
use std::iter::FromIterator;
use std::marker::PhantomData;
use std::sync::Arc;
use store::{Error as StoreError, HotColdDB, ItemStore};
Expand Down Expand Up @@ -186,6 +187,8 @@ where
};
let finalized_checkpoint = justified_checkpoint;
let justified_balances = JustifiedBalances::from_justified_state(anchor_state)?;
let equivocating_indices =
BTreeSet::from_iter(justified_balances.slashed_indices.iter().copied());

Ok(Self {
store,
Expand All @@ -198,7 +201,7 @@ where
unrealized_justified_checkpoint: justified_checkpoint,
unrealized_finalized_checkpoint: finalized_checkpoint,
proposer_boost_root: Hash256::zero(),
equivocating_indices: BTreeSet::new(),
equivocating_indices,
_phantom: PhantomData,
})
}
Expand Down Expand Up @@ -328,6 +331,8 @@ where
.ok_or_else(|| Error::MissingState(justified_block.state_root()))?;

self.justified_balances = JustifiedBalances::from_justified_state(&state)?;
self.equivocating_indices
.extend(self.justified_balances.slashed_indices.iter().copied());
}

Ok(())
Expand Down
14 changes: 13 additions & 1 deletion consensus/proto_array/src/justified_balances.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use safe_arith::{ArithError, SafeArith};
use std::collections::HashSet;
use types::{BeaconState, EthSpec};

#[derive(Debug, PartialEq, Clone, Default)]
Expand All @@ -12,18 +13,26 @@ pub struct JustifiedBalances {
pub total_effective_balance: u64,
/// The number of active validators included in `self.effective_balances`.
pub num_active_validators: u64,
/// The set of all slashed validators.
pub slashed_indices: HashSet<u64>,
}

impl JustifiedBalances {
pub fn from_justified_state<T: EthSpec>(state: &BeaconState<T>) -> Result<Self, ArithError> {
let current_epoch = state.current_epoch();
let mut total_effective_balance = 0u64;
let mut num_active_validators = 0u64;
let mut slashed_indices = HashSet::new();

let effective_balances = state
.validators()
.iter()
.map(|validator| {
.enumerate()
.map(|(validator_index, validator)| {
if validator.slashed {
slashed_indices.insert(validator_index as u64);
}

if validator.is_active_at(current_epoch) {
total_effective_balance.safe_add_assign(validator.effective_balance)?;
num_active_validators.safe_add_assign(1)?;
Expand All @@ -39,6 +48,7 @@ impl JustifiedBalances {
effective_balances,
total_effective_balance,
num_active_validators,
slashed_indices,
})
}

Expand All @@ -57,6 +67,8 @@ impl JustifiedBalances {
effective_balances,
total_effective_balance,
num_active_validators,
// TODO(paul): is an empty set sensible?
slashed_indices: <_>::default(),
})
}
}

0 comments on commit 8ad941d

Please sign in to comment.