From 4da401fd39fa34056cfbe451ce0de72dd97174bd Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 27 Jan 2020 13:30:37 +1100 Subject: [PATCH] Ensure effective balances are used --- .../src/fork_choice/checkpoint_manager.rs | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/beacon_node/beacon_chain/src/fork_choice/checkpoint_manager.rs b/beacon_node/beacon_chain/src/fork_choice/checkpoint_manager.rs index 12e752f4ef..660f043315 100644 --- a/beacon_node/beacon_chain/src/fork_choice/checkpoint_manager.rs +++ b/beacon_node/beacon_chain/src/fork_choice/checkpoint_manager.rs @@ -47,7 +47,7 @@ impl BalancesCache { if self.position(epoch_boundary_root).is_none() { let item = CacheItem { block_root: epoch_boundary_root, - balances: state.balances.clone().into(), + balances: get_effective_balances(state), }; if self.items.len() == MAX_BALANCE_CACHE_SIZE { @@ -97,6 +97,24 @@ impl BalancesCache { } } +/// Returns the effective balances for every validator in the given `state`. +/// +/// Any validator who is not active in the epoch of the given `state` is assigned a balance of +/// zero. +fn get_effective_balances(state: &BeaconState) -> Vec { + state + .validators + .iter() + .map(|validator| { + if validator.is_active_at(state.current_epoch()) { + validator.effective_balance + } else { + 0 + } + }) + .collect() +} + /// A `types::Checkpoint` that also stores the validator balances from a `BeaconState`. /// /// Useful because we need to track the justified checkpoint balances. @@ -285,7 +303,7 @@ impl CheckpointManager { .get_state_caching_only_with_committee_caches(&block.state_root, Some(block.slot))? .ok_or_else(|| Error::UnknownJustifiedState(block.state_root))?; - Ok(state.balances.into()) + Ok(get_effective_balances(&state)) } }