|
| 1 | +use crate::{BeaconChain, BeaconChainError, BeaconChainTypes}; |
| 2 | +use eth2::lighthouse::{AttestationRewards, BlockReward, BlockRewardMeta}; |
| 3 | +use operation_pool::{AttMaxCover, MaxCover}; |
| 4 | +use state_processing::per_block_processing::altair::sync_committee::compute_sync_aggregate_rewards; |
| 5 | +use types::{BeaconBlockRef, BeaconState, EthSpec, Hash256, RelativeEpoch}; |
| 6 | + |
| 7 | +impl<T: BeaconChainTypes> BeaconChain<T> { |
| 8 | + pub fn compute_block_reward( |
| 9 | + &self, |
| 10 | + block: BeaconBlockRef<'_, T::EthSpec>, |
| 11 | + block_root: Hash256, |
| 12 | + state: &BeaconState<T::EthSpec>, |
| 13 | + ) -> Result<BlockReward, BeaconChainError> { |
| 14 | + if block.slot() != state.slot() { |
| 15 | + return Err(BeaconChainError::BlockRewardSlotError); |
| 16 | + } |
| 17 | + |
| 18 | + let active_indices = state.get_cached_active_validator_indices(RelativeEpoch::Current)?; |
| 19 | + let total_active_balance = state.get_total_balance(active_indices, &self.spec)?; |
| 20 | + let mut per_attestation_rewards = block |
| 21 | + .body() |
| 22 | + .attestations() |
| 23 | + .iter() |
| 24 | + .map(|att| { |
| 25 | + AttMaxCover::new(att, state, total_active_balance, &self.spec) |
| 26 | + .ok_or(BeaconChainError::BlockRewardAttestationError) |
| 27 | + }) |
| 28 | + .collect::<Result<Vec<_>, _>>()?; |
| 29 | + |
| 30 | + // Update the attestation rewards for each previous attestation included. |
| 31 | + // This is O(n^2) in the number of attestations n. |
| 32 | + for i in 0..per_attestation_rewards.len() { |
| 33 | + let (updated, to_update) = per_attestation_rewards.split_at_mut(i + 1); |
| 34 | + let latest_att = &updated[i]; |
| 35 | + |
| 36 | + for att in to_update { |
| 37 | + att.update_covering_set(latest_att.object(), latest_att.covering_set()); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + let mut prev_epoch_total = 0; |
| 42 | + let mut curr_epoch_total = 0; |
| 43 | + |
| 44 | + for cover in &per_attestation_rewards { |
| 45 | + for &reward in cover.fresh_validators_rewards.values() { |
| 46 | + if cover.att.data.slot.epoch(T::EthSpec::slots_per_epoch()) == state.current_epoch() |
| 47 | + { |
| 48 | + curr_epoch_total += reward; |
| 49 | + } else { |
| 50 | + prev_epoch_total += reward; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + let attestation_total = prev_epoch_total + curr_epoch_total; |
| 56 | + |
| 57 | + // Drop the covers. |
| 58 | + let per_attestation_rewards = per_attestation_rewards |
| 59 | + .into_iter() |
| 60 | + .map(|cover| cover.fresh_validators_rewards) |
| 61 | + .collect(); |
| 62 | + |
| 63 | + let attestation_rewards = AttestationRewards { |
| 64 | + total: attestation_total, |
| 65 | + prev_epoch_total, |
| 66 | + curr_epoch_total, |
| 67 | + per_attestation_rewards, |
| 68 | + }; |
| 69 | + |
| 70 | + // Sync committee rewards. |
| 71 | + let sync_committee_rewards = if let Ok(sync_aggregate) = block.body().sync_aggregate() { |
| 72 | + let (_, proposer_reward_per_bit) = compute_sync_aggregate_rewards(state, &self.spec) |
| 73 | + .map_err(|_| BeaconChainError::BlockRewardSyncError)?; |
| 74 | + sync_aggregate.sync_committee_bits.num_set_bits() as u64 * proposer_reward_per_bit |
| 75 | + } else { |
| 76 | + 0 |
| 77 | + }; |
| 78 | + |
| 79 | + // Total, metadata |
| 80 | + let total = attestation_total + sync_committee_rewards; |
| 81 | + |
| 82 | + let meta = BlockRewardMeta { |
| 83 | + slot: block.slot(), |
| 84 | + parent_slot: state.latest_block_header().slot, |
| 85 | + proposer_index: block.proposer_index(), |
| 86 | + graffiti: block.body().graffiti().as_utf8_lossy(), |
| 87 | + }; |
| 88 | + |
| 89 | + Ok(BlockReward { |
| 90 | + total, |
| 91 | + block_root, |
| 92 | + meta, |
| 93 | + attestation_rewards, |
| 94 | + sync_committee_rewards, |
| 95 | + }) |
| 96 | + } |
| 97 | +} |
0 commit comments