Files
lighthouse/beacon_node/beacon_chain/src/sync_committee_rewards.rs
Daniel Knopik 574b204bdb decouple eth2 from store and lighthouse_network (#6680)
- #6452 (partially)


  Remove dependencies on `store` and `lighthouse_network`  from `eth2`. This was achieved as follows:

- depend on `enr` and `multiaddr` directly instead of using `lighthouse_network`'s reexports.
- make `lighthouse_network` responsible for converting between API and internal types.
- in two cases, remove complex internal types and use the generic `serde_json::Value` instead - this is not ideal, but should be fine for now, as this affects two internal non-spec endpoints which are meant for debugging, unstable, and subject to change without notice anyway. Inspired by #6679. The alternative is to move all relevant types to `eth2` or `types` instead - what do you think?
2025-03-14 16:44:48 +00:00

104 lines
3.7 KiB
Rust

use crate::{BeaconChain, BeaconChainError, BeaconChainTypes};
use eth2::types::SyncCommitteeReward;
use safe_arith::SafeArith;
use state_processing::per_block_processing::altair::sync_committee::compute_sync_aggregate_rewards;
use std::collections::HashMap;
use store::RelativeEpoch;
use tracing::error;
use types::{AbstractExecPayload, BeaconBlockRef, BeaconState};
impl<T: BeaconChainTypes> BeaconChain<T> {
pub fn compute_sync_committee_rewards<Payload: AbstractExecPayload<T::EthSpec>>(
&self,
block: BeaconBlockRef<'_, T::EthSpec, Payload>,
state: &mut BeaconState<T::EthSpec>,
) -> Result<Vec<SyncCommitteeReward>, BeaconChainError> {
if block.slot() != state.slot() {
return Err(BeaconChainError::BlockRewardSlotError);
}
let spec = &self.spec;
state.build_committee_cache(RelativeEpoch::Current, spec)?;
let sync_aggregate = block.body().sync_aggregate()?;
let sync_committee = state.current_sync_committee()?.clone();
let sync_committee_indices = state.get_sync_committee_indices(&sync_committee)?;
let (participant_reward_value, proposer_reward_per_bit) =
compute_sync_aggregate_rewards(state, spec).map_err(|e| {
error!(
error = ?e,
"Error calculating sync aggregate rewards"
);
BeaconChainError::SyncCommitteeRewardsSyncError
})?;
let mut balances = HashMap::<usize, u64>::new();
for &validator_index in &sync_committee_indices {
balances.insert(
validator_index,
*state
.balances()
.get(validator_index)
.ok_or(BeaconChainError::SyncCommitteeRewardsSyncError)?,
);
}
let proposer_index = block.proposer_index() as usize;
balances.insert(
proposer_index,
*state
.balances()
.get(proposer_index)
.ok_or(BeaconChainError::SyncCommitteeRewardsSyncError)?,
);
let mut total_proposer_rewards = 0;
// Apply rewards to participant balances. Keep track of proposer rewards
for (validator_index, participant_bit) in sync_committee_indices
.iter()
.zip(sync_aggregate.sync_committee_bits.iter())
{
let participant_balance = balances
.get_mut(validator_index)
.ok_or(BeaconChainError::SyncCommitteeRewardsSyncError)?;
if participant_bit {
participant_balance.safe_add_assign(participant_reward_value)?;
balances
.get_mut(&proposer_index)
.ok_or(BeaconChainError::SyncCommitteeRewardsSyncError)?
.safe_add_assign(proposer_reward_per_bit)?;
total_proposer_rewards.safe_add_assign(proposer_reward_per_bit)?;
} else {
*participant_balance = participant_balance.saturating_sub(participant_reward_value);
}
}
Ok(balances
.iter()
.filter_map(|(&i, &new_balance)| {
let initial_balance = *state.balances().get(i)? as i64;
let reward = if i != proposer_index {
new_balance as i64 - initial_balance
} else if sync_committee_indices.contains(&i) {
new_balance as i64 - initial_balance - total_proposer_rewards as i64
} else {
return None;
};
Some(SyncCommitteeReward {
validator_index: i as u64,
reward,
})
})
.collect())
}
}