Fix phase0 block reward in rewards API (#5101)

* Added Block Rewards

* added new type

* added enum

* Fix phase0 block reward in rewards API (#4929)

* Merge 'guav00a/proposer-rewards-api'

* Merge unstable

* Revamp phase0 reward API tests

- Add test_rewards_base_slashings (testing #5101)
- Improve fix to not include proposer reward in attestation reward API calculation (#4856)
- Adjust test approach for phase0 tests: Pad with empty epochs to include all rewards in calculation
- Simplify and unify code across all reward tests

* Merge branch 'unstable' into fix-4929

* Merge branch 'unstable' into fix-4929

* Merge remote-tracking branch 'origin/unstable' into fix-4929

* Fix merge fallout

* Remove junk revived in merge

* Address review

- check for attestations with lower inclusion delay
- check for double attestations in block
- add test

* Merge branch 'unstable' into fix-4929

* Merge branch 'unstable' into fix-4929
This commit is contained in:
Daniel Knopik
2024-09-17 08:45:02 +02:00
committed by GitHub
parent 2f6ad34795
commit 8b085dd167
7 changed files with 443 additions and 308 deletions

View File

@@ -45,6 +45,12 @@ impl AttestationDelta {
}
}
#[derive(Debug)]
pub enum ProposerRewardCalculation {
Include,
Exclude,
}
/// Apply attester and proposer rewards.
pub fn process_rewards_and_penalties<E: EthSpec>(
state: &mut BeaconState<E>,
@@ -62,7 +68,12 @@ pub fn process_rewards_and_penalties<E: EthSpec>(
return Err(Error::ValidatorStatusesInconsistent);
}
let deltas = get_attestation_deltas_all(state, validator_statuses, spec)?;
let deltas = get_attestation_deltas_all(
state,
validator_statuses,
ProposerRewardCalculation::Include,
spec,
)?;
// Apply the deltas, erroring on overflow above but not on overflow below (saturating at 0
// instead).
@@ -79,9 +90,10 @@ pub fn process_rewards_and_penalties<E: EthSpec>(
pub fn get_attestation_deltas_all<E: EthSpec>(
state: &BeaconState<E>,
validator_statuses: &ValidatorStatuses,
proposer_reward: ProposerRewardCalculation,
spec: &ChainSpec,
) -> Result<Vec<AttestationDelta>, Error> {
get_attestation_deltas(state, validator_statuses, None, spec)
get_attestation_deltas(state, validator_statuses, proposer_reward, None, spec)
}
/// Apply rewards for participation in attestations during the previous epoch, and only compute
@@ -89,10 +101,18 @@ pub fn get_attestation_deltas_all<E: EthSpec>(
pub fn get_attestation_deltas_subset<E: EthSpec>(
state: &BeaconState<E>,
validator_statuses: &ValidatorStatuses,
proposer_reward: ProposerRewardCalculation,
validators_subset: &Vec<usize>,
spec: &ChainSpec,
) -> Result<Vec<(usize, AttestationDelta)>, Error> {
get_attestation_deltas(state, validator_statuses, Some(validators_subset), spec).map(|deltas| {
get_attestation_deltas(
state,
validator_statuses,
proposer_reward,
Some(validators_subset),
spec,
)
.map(|deltas| {
deltas
.into_iter()
.enumerate()
@@ -109,6 +129,7 @@ pub fn get_attestation_deltas_subset<E: EthSpec>(
fn get_attestation_deltas<E: EthSpec>(
state: &BeaconState<E>,
validator_statuses: &ValidatorStatuses,
proposer_reward: ProposerRewardCalculation,
maybe_validators_subset: Option<&Vec<usize>>,
spec: &ChainSpec,
) -> Result<Vec<AttestationDelta>, Error> {
@@ -169,13 +190,15 @@ fn get_attestation_deltas<E: EthSpec>(
.combine(inactivity_penalty_delta)?;
}
if let Some((proposer_index, proposer_delta)) = proposer_delta {
if include_validator_delta(proposer_index) {
deltas
.get_mut(proposer_index)
.ok_or(Error::ValidatorStatusesInconsistent)?
.inclusion_delay_delta
.combine(proposer_delta)?;
if let ProposerRewardCalculation::Include = proposer_reward {
if let Some((proposer_index, proposer_delta)) = proposer_delta {
if include_validator_delta(proposer_index) {
deltas
.get_mut(proposer_index)
.ok_or(Error::ValidatorStatusesInconsistent)?
.inclusion_delay_delta
.combine(proposer_delta)?;
}
}
}
}