mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-16 03:12:41 +00:00
Merge branch 'deneb-free-blobs' of https://github.com/sigp/lighthouse into refactor-deneb-networking
This commit is contained in:
@@ -17,3 +17,15 @@ pub fn get_base_reward<T: EthSpec>(
|
||||
.safe_div(spec.base_rewards_per_epoch)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub fn get_base_reward_from_effective_balance<T: EthSpec>(
|
||||
effective_balance: u64,
|
||||
total_active_balance: u64,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<u64, BeaconStateError> {
|
||||
effective_balance
|
||||
.safe_mul(spec.base_reward_factor)?
|
||||
.safe_div(total_active_balance.integer_sqrt())?
|
||||
.safe_div(spec.base_rewards_per_epoch)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ pub fn process_epoch<T: EthSpec>(
|
||||
justification_and_finalization_state.apply_changes_to_state(state);
|
||||
|
||||
// Rewards and Penalties.
|
||||
process_rewards_and_penalties(state, &mut validator_statuses, spec)?;
|
||||
process_rewards_and_penalties(state, &validator_statuses, spec)?;
|
||||
|
||||
// Registry Updates.
|
||||
process_registry_updates(state, spec)?;
|
||||
|
||||
@@ -45,7 +45,7 @@ impl AttestationDelta {
|
||||
/// Apply attester and proposer rewards.
|
||||
pub fn process_rewards_and_penalties<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
validator_statuses: &mut ValidatorStatuses,
|
||||
validator_statuses: &ValidatorStatuses,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
if state.current_epoch() == T::genesis_epoch() {
|
||||
@@ -59,7 +59,7 @@ pub fn process_rewards_and_penalties<T: EthSpec>(
|
||||
return Err(Error::ValidatorStatusesInconsistent);
|
||||
}
|
||||
|
||||
let deltas = get_attestation_deltas(state, validator_statuses, spec)?;
|
||||
let deltas = get_attestation_deltas_all(state, validator_statuses, spec)?;
|
||||
|
||||
// Apply the deltas, erroring on overflow above but not on overflow below (saturating at 0
|
||||
// instead).
|
||||
@@ -73,10 +73,41 @@ pub fn process_rewards_and_penalties<T: EthSpec>(
|
||||
}
|
||||
|
||||
/// Apply rewards for participation in attestations during the previous epoch.
|
||||
pub fn get_attestation_deltas<T: EthSpec>(
|
||||
pub fn get_attestation_deltas_all<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
validator_statuses: &ValidatorStatuses,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<Vec<AttestationDelta>, Error> {
|
||||
get_attestation_deltas(state, validator_statuses, None, spec)
|
||||
}
|
||||
|
||||
/// Apply rewards for participation in attestations during the previous epoch, and only compute
|
||||
/// rewards for a subset of validators.
|
||||
pub fn get_attestation_deltas_subset<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
validator_statuses: &ValidatorStatuses,
|
||||
validators_subset: &Vec<usize>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<Vec<(usize, AttestationDelta)>, Error> {
|
||||
get_attestation_deltas(state, validator_statuses, Some(validators_subset), spec).map(|deltas| {
|
||||
deltas
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.filter(|(index, _)| validators_subset.contains(index))
|
||||
.collect()
|
||||
})
|
||||
}
|
||||
|
||||
/// Apply rewards for participation in attestations during the previous epoch.
|
||||
/// If `maybe_validators_subset` specified, only the deltas for the specified validator subset is
|
||||
/// returned, otherwise deltas for all validators are returned.
|
||||
///
|
||||
/// Returns a vec of validator indices to `AttestationDelta`.
|
||||
fn get_attestation_deltas<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
validator_statuses: &ValidatorStatuses,
|
||||
maybe_validators_subset: Option<&Vec<usize>>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<Vec<AttestationDelta>, Error> {
|
||||
let previous_epoch = state.previous_epoch();
|
||||
let finality_delay = state
|
||||
@@ -88,6 +119,13 @@ pub fn get_attestation_deltas<T: EthSpec>(
|
||||
|
||||
let total_balances = &validator_statuses.total_balances;
|
||||
|
||||
// Ignore validator if a subset is specified and validator is not in the subset
|
||||
let include_validator_delta = |idx| match maybe_validators_subset.as_ref() {
|
||||
None => true,
|
||||
Some(validators_subset) if validators_subset.contains(&idx) => true,
|
||||
Some(_) => false,
|
||||
};
|
||||
|
||||
for (index, validator) in validator_statuses.statuses.iter().enumerate() {
|
||||
// Ignore ineligible validators. All sub-functions of the spec do this except for
|
||||
// `get_inclusion_delay_deltas`. It's safe to do so here because any validator that is in
|
||||
@@ -99,41 +137,46 @@ pub fn get_attestation_deltas<T: EthSpec>(
|
||||
|
||||
let base_reward = get_base_reward(state, index, total_balances.current_epoch(), spec)?;
|
||||
|
||||
let source_delta =
|
||||
get_source_delta(validator, base_reward, total_balances, finality_delay, spec)?;
|
||||
let target_delta =
|
||||
get_target_delta(validator, base_reward, total_balances, finality_delay, spec)?;
|
||||
let head_delta =
|
||||
get_head_delta(validator, base_reward, total_balances, finality_delay, spec)?;
|
||||
let (inclusion_delay_delta, proposer_delta) =
|
||||
get_inclusion_delay_delta(validator, base_reward, spec)?;
|
||||
let inactivity_penalty_delta =
|
||||
get_inactivity_penalty_delta(validator, base_reward, finality_delay, spec)?;
|
||||
|
||||
let delta = deltas
|
||||
.get_mut(index)
|
||||
.ok_or(Error::DeltaOutOfBounds(index))?;
|
||||
delta.source_delta.combine(source_delta)?;
|
||||
delta.target_delta.combine(target_delta)?;
|
||||
delta.head_delta.combine(head_delta)?;
|
||||
delta.inclusion_delay_delta.combine(inclusion_delay_delta)?;
|
||||
delta
|
||||
.inactivity_penalty_delta
|
||||
.combine(inactivity_penalty_delta)?;
|
||||
if include_validator_delta(index) {
|
||||
let source_delta =
|
||||
get_source_delta(validator, base_reward, total_balances, finality_delay, spec)?;
|
||||
let target_delta =
|
||||
get_target_delta(validator, base_reward, total_balances, finality_delay, spec)?;
|
||||
let head_delta =
|
||||
get_head_delta(validator, base_reward, total_balances, finality_delay, spec)?;
|
||||
let inactivity_penalty_delta =
|
||||
get_inactivity_penalty_delta(validator, base_reward, finality_delay, spec)?;
|
||||
|
||||
let delta = deltas
|
||||
.get_mut(index)
|
||||
.ok_or(Error::DeltaOutOfBounds(index))?;
|
||||
delta.source_delta.combine(source_delta)?;
|
||||
delta.target_delta.combine(target_delta)?;
|
||||
delta.head_delta.combine(head_delta)?;
|
||||
delta.inclusion_delay_delta.combine(inclusion_delay_delta)?;
|
||||
delta
|
||||
.inactivity_penalty_delta
|
||||
.combine(inactivity_penalty_delta)?;
|
||||
}
|
||||
|
||||
if let Some((proposer_index, proposer_delta)) = proposer_delta {
|
||||
deltas
|
||||
.get_mut(proposer_index)
|
||||
.ok_or(Error::ValidatorStatusesInconsistent)?
|
||||
.inclusion_delay_delta
|
||||
.combine(proposer_delta)?;
|
||||
if include_validator_delta(proposer_index) {
|
||||
deltas
|
||||
.get_mut(proposer_index)
|
||||
.ok_or(Error::ValidatorStatusesInconsistent)?
|
||||
.inclusion_delay_delta
|
||||
.combine(proposer_delta)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(deltas)
|
||||
}
|
||||
|
||||
fn get_attestation_component_delta(
|
||||
pub fn get_attestation_component_delta(
|
||||
index_in_unslashed_attesting_indices: bool,
|
||||
attesting_balance: u64,
|
||||
total_balances: &TotalBalances,
|
||||
@@ -216,7 +259,7 @@ fn get_head_delta(
|
||||
)
|
||||
}
|
||||
|
||||
fn get_inclusion_delay_delta(
|
||||
pub fn get_inclusion_delay_delta(
|
||||
validator: &ValidatorStatus,
|
||||
base_reward: u64,
|
||||
spec: &ChainSpec,
|
||||
@@ -242,7 +285,7 @@ fn get_inclusion_delay_delta(
|
||||
}
|
||||
}
|
||||
|
||||
fn get_inactivity_penalty_delta(
|
||||
pub fn get_inactivity_penalty_delta(
|
||||
validator: &ValidatorStatus,
|
||||
base_reward: u64,
|
||||
finality_delay: u64,
|
||||
|
||||
Reference in New Issue
Block a user