Remove last inclusion_slot(..) call

This commit is contained in:
Paul Hauner 2019-03-10 21:06:40 +11:00
parent 21d75ef0bd
commit 53456a6c79
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6

View File

@ -2,7 +2,6 @@ use attester_sets::AttesterSets;
use errors::EpochProcessingError as Error; use errors::EpochProcessingError as Error;
use fnv::FnvHashMap; use fnv::FnvHashMap;
use fnv::FnvHashSet; use fnv::FnvHashSet;
use inclusion_distance::{inclusion_distance, inclusion_slot};
use integer_sqrt::IntegerSquareRoot; use integer_sqrt::IntegerSquareRoot;
use log::debug; use log::debug;
use rayon::prelude::*; use rayon::prelude::*;
@ -280,6 +279,28 @@ pub fn process_rewards_and_penalities(
return Err(Error::PreviousTotalBalanceIsZero); return Err(Error::PreviousTotalBalanceIsZero);
} }
// Map is ValidatorIndex -> ProposerIndex
let mut inclusion_slots: FnvHashMap<usize, (Slot, usize)> = FnvHashMap::default();
for a in &previous_epoch_attestations {
let participants =
state.get_attestation_participants(&a.data, &a.aggregation_bitfield, spec)?;
let inclusion_distance = (a.inclusion_slot - a.data.slot).as_u64();
for participant in participants {
if let Some((existing_distance, _)) = inclusion_slots.get(&participant) {
if *existing_distance <= inclusion_distance {
continue;
}
}
let proposer_index = state
.get_beacon_proposer_index(a.data.slot, spec)
.map_err(|_| Error::UnableToDetermineProducer)?;
inclusion_slots.insert(
participant,
(Slot::from(inclusion_distance), proposer_index),
);
}
}
// Justification and finalization // Justification and finalization
let epochs_since_finality = next_epoch - state.finalized_epoch; let epochs_since_finality = next_epoch - state.finalized_epoch;
@ -327,17 +348,17 @@ pub fn process_rewards_and_penalities(
if attesters.previous_epoch.indices.contains(&index) { if attesters.previous_epoch.indices.contains(&index) {
let base_reward = state.base_reward(index, base_reward_quotient, spec); let base_reward = state.base_reward(index, base_reward_quotient, spec);
let inclusion_distance =
inclusion_distance(state, &previous_epoch_attestations, index, spec);
if let Ok(inclusion_distance) = inclusion_distance { let (inclusion_distance, _) = inclusion_slots
if inclusion_distance > 0 { .get(&index)
safe_add_assign!( .expect("Inconsistent inclusion_slots.");
balance,
base_reward * spec.min_attestation_inclusion_delay if *inclusion_distance > 0 {
/ inclusion_distance safe_add_assign!(
) balance,
} base_reward * spec.min_attestation_inclusion_delay
/ inclusion_distance.as_u64()
)
} }
} }
@ -378,18 +399,17 @@ pub fn process_rewards_and_penalities(
if attesters.previous_epoch.indices.contains(&index) { if attesters.previous_epoch.indices.contains(&index) {
let base_reward = state.base_reward(index, base_reward_quotient, spec); let base_reward = state.base_reward(index, base_reward_quotient, spec);
let inclusion_distance =
inclusion_distance(state, &previous_epoch_attestations, index, spec);
if let Ok(inclusion_distance) = inclusion_distance { let (inclusion_distance, _) = inclusion_slots
if inclusion_distance > 0 { .get(&index)
safe_sub_assign!( .expect("Inconsistent inclusion_slots.");
balance,
base_reward if *inclusion_distance > 0 {
- base_reward * spec.min_attestation_inclusion_delay safe_add_assign!(
/ inclusion_distance balance,
); base_reward * spec.min_attestation_inclusion_delay
} / inclusion_distance.as_u64()
)
} }
} }
@ -399,34 +419,17 @@ pub fn process_rewards_and_penalities(
} }
// Attestation inclusion // Attestation inclusion
let mut inclusion_slots: FnvHashMap<usize, (Slot, Slot)> = FnvHashMap::default(); //
for a in previous_epoch_attestations {
let participants =
state.get_attestation_participants(&a.data, &a.aggregation_bitfield, spec)?;
let inclusion_distance = (a.inclusion_slot - a.data.slot).as_u64();
for participant in participants {
if let Some((existing_distance, _)) = inclusion_slots.get(&participant) {
if *existing_distance <= inclusion_distance {
continue;
}
}
inclusion_slots.insert(participant, (Slot::from(inclusion_distance), a.data.slot));
}
}
for &index in &attesters.previous_epoch.indices { for &index in &attesters.previous_epoch.indices {
let (_, inclusion_slot) = inclusion_slots let (_, proposer_index) = inclusion_slots
.get(&index) .get(&index)
.ok_or_else(|| Error::InclusionSlotsInconsistent(index))?; .ok_or_else(|| Error::InclusionSlotsInconsistent(index))?;
let proposer_index = state let base_reward = state.base_reward(*proposer_index, base_reward_quotient, spec);
.get_beacon_proposer_index(*inclusion_slot, spec)
.map_err(|_| Error::UnableToDetermineProducer)?;
let base_reward = state.base_reward(proposer_index, base_reward_quotient, spec);
safe_add_assign!( safe_add_assign!(
state.validator_balances[proposer_index], state.validator_balances[*proposer_index],
base_reward / spec.attestation_inclusion_reward_quotient base_reward / spec.attestation_inclusion_reward_quotient
); );
} }