Update Attesters struct

- Renames variables
- Moves total balance calculation into struct
This commit is contained in:
Paul Hauner
2019-03-14 12:49:48 +11:00
parent a319144835
commit 95599ddc66
3 changed files with 88 additions and 192 deletions

View File

@@ -26,32 +26,21 @@ pub type WinningRootHashSet = HashMap<u64, WinningRoot>;
///
/// Spec v0.4.0
pub fn per_epoch_processing(state: &mut BeaconState, spec: &ChainSpec) -> Result<(), Error> {
let previous_epoch = state.previous_epoch(spec);
// Ensure all of the caches are built.
state.build_epoch_cache(RelativeEpoch::Previous, spec)?;
state.build_epoch_cache(RelativeEpoch::Current, spec)?;
state.build_epoch_cache(RelativeEpoch::Next, spec)?;
let active_validator_indices = calculate_active_validator_indices(&state, spec);
let current_total_balance = state.get_total_balance(&active_validator_indices[..], spec);
let previous_total_balance = state.get_total_balance(
&get_active_validator_indices(&state.validator_registry, previous_epoch)[..],
spec,
);
let mut attesters = calculate_attester_sets(&state, &active_validator_indices, spec)?;
let mut attesters = calculate_attester_sets(&state, spec)?;
process_eth1_data(state, spec);
process_justification(
state,
current_total_balance,
previous_total_balance,
attesters.balances.previous_epoch_boundary,
attesters.balances.current_epoch_boundary,
attesters.balances.current_epoch_total,
attesters.balances.previous_epoch_total,
attesters.balances.previous_epoch_boundary_attesters,
attesters.balances.current_epoch_boundary_attesters,
spec,
);
@@ -59,13 +48,7 @@ pub fn per_epoch_processing(state: &mut BeaconState, spec: &ChainSpec) -> Result
let winning_root_for_shards = process_crosslinks(state, spec)?;
// Rewards and Penalities
process_rewards_and_penalities(
state,
&mut attesters,
previous_total_balance,
&winning_root_for_shards,
spec,
)?;
process_rewards_and_penalities(state, &mut attesters, &winning_root_for_shards, spec)?;
// Ejections
state.process_ejections(spec);
@@ -104,12 +87,12 @@ pub fn calculate_active_validator_indices(state: &BeaconState, spec: &ChainSpec)
/// Spec v0.4.0
pub fn calculate_attester_sets(
state: &BeaconState,
active_validator_indices: &[usize],
spec: &ChainSpec,
) -> Result<Attesters, BeaconStateError> {
let mut attesters = Attesters::empty(state.validator_registry.len());
attesters.process_active_validator_indices(&active_validator_indices);
let mut attesters = Attesters::new(state, spec);
attesters.process_attestations(&state, &state.latest_attestations, spec)?;
Ok(attesters)
}
@@ -285,12 +268,13 @@ pub fn process_crosslinks(
pub fn process_rewards_and_penalities(
state: &mut BeaconState,
attesters: &mut Attesters,
previous_total_balance: u64,
winning_root_for_shards: &WinningRootHashSet,
spec: &ChainSpec,
) -> Result<(), Error> {
let next_epoch = state.next_epoch(spec);
let previous_total_balance = attesters.balances.previous_epoch_total;
let base_reward_quotient = previous_total_balance.integer_sqrt() / spec.base_reward_quotient;
if base_reward_quotient == 0 {
@@ -317,34 +301,35 @@ pub fn process_rewards_and_penalities(
if epochs_since_finality <= 4 {
// Expected FFG source
if status.is_previous_epoch {
if status.is_previous_epoch_attester {
safe_add_assign!(
balance,
base_reward * attesters.balances.previous_epoch / previous_total_balance
base_reward * attesters.balances.previous_epoch_attesters
/ previous_total_balance
);
} else if status.is_active {
} else if status.is_active_in_previous_epoch {
safe_sub_assign!(balance, base_reward);
}
// Expected FFG target
if status.is_previous_epoch_boundary {
if status.is_previous_epoch_boundary_attester {
safe_add_assign!(
balance,
base_reward * attesters.balances.previous_epoch_boundary
base_reward * attesters.balances.previous_epoch_boundary_attesters
/ previous_total_balance
);
} else if status.is_active {
} else if status.is_active_in_previous_epoch {
safe_sub_assign!(balance, base_reward);
}
// Expected beacon chain head
if status.is_previous_epoch_head {
if status.is_previous_epoch_head_attester {
safe_add_assign!(
balance,
base_reward * attesters.balances.previous_epoch_head
base_reward * attesters.balances.previous_epoch_head_attesters
/ previous_total_balance
);
} else if status.is_active {
} else if status.is_active_in_previous_epoch {
safe_sub_assign!(balance, base_reward);
};
} else {
@@ -355,14 +340,14 @@ pub fn process_rewards_and_penalities(
spec,
);
if status.is_active {
if !status.is_previous_epoch {
if status.is_active_in_previous_epoch {
if !status.is_previous_epoch_attester {
safe_sub_assign!(balance, inactivity_penalty);
}
if !status.is_previous_epoch_boundary {
if !status.is_previous_epoch_boundary_attester {
safe_sub_assign!(balance, inactivity_penalty);
}
if !status.is_previous_epoch_head {
if !status.is_previous_epoch_head_attester {
safe_sub_assign!(balance, inactivity_penalty);
}
@@ -393,7 +378,7 @@ pub fn process_rewards_and_penalities(
for (index, _validator) in state.validator_registry.iter().enumerate() {
let status = &attesters.statuses[index];
if status.is_previous_epoch {
if status.is_previous_epoch_attester {
let proposer_index = status.inclusion_info.proposer_index;
let inclusion_distance = status.inclusion_info.distance;

View File

@@ -42,28 +42,31 @@ impl InclusionInfo {
#[derive(Default, Clone)]
pub struct AttesterStatus {
pub is_active: bool,
pub is_active_in_current_epoch: bool,
pub is_active_in_previous_epoch: bool,
pub is_current_epoch: bool,
pub is_current_epoch_boundary: bool,
pub is_previous_epoch: bool,
pub is_previous_epoch_boundary: bool,
pub is_previous_epoch_head: bool,
pub is_current_epoch_attester: bool,
pub is_current_epoch_boundary_attester: bool,
pub is_previous_epoch_attester: bool,
pub is_previous_epoch_boundary_attester: bool,
pub is_previous_epoch_head_attester: bool,
pub inclusion_info: InclusionInfo,
pub winning_root_info: Option<WinningRootInfo>,
}
impl AttesterStatus {
/// Note: does not update the winning root info.
pub fn update(&mut self, other: &Self) {
// Update all the bool fields, only updating `self` if `other` is true (never setting
// `self` to false).
set_self_if_other_is_true!(self, other, is_active);
set_self_if_other_is_true!(self, other, is_current_epoch);
set_self_if_other_is_true!(self, other, is_current_epoch_boundary);
set_self_if_other_is_true!(self, other, is_previous_epoch);
set_self_if_other_is_true!(self, other, is_previous_epoch_boundary);
set_self_if_other_is_true!(self, other, is_previous_epoch_head);
set_self_if_other_is_true!(self, other, is_active_in_current_epoch);
set_self_if_other_is_true!(self, other, is_active_in_previous_epoch);
set_self_if_other_is_true!(self, other, is_current_epoch_attester);
set_self_if_other_is_true!(self, other, is_current_epoch_boundary_attester);
set_self_if_other_is_true!(self, other, is_previous_epoch_attester);
set_self_if_other_is_true!(self, other, is_previous_epoch_boundary_attester);
set_self_if_other_is_true!(self, other, is_previous_epoch_head_attester);
self.inclusion_info.update(&other.inclusion_info);
}
@@ -71,11 +74,13 @@ impl AttesterStatus {
#[derive(Default, Clone)]
pub struct TotalBalances {
pub current_epoch: u64,
pub current_epoch_boundary: u64,
pub previous_epoch: u64,
pub previous_epoch_boundary: u64,
pub previous_epoch_head: u64,
pub current_epoch_total: u64,
pub previous_epoch_total: u64,
pub current_epoch_attesters: u64,
pub current_epoch_boundary_attesters: u64,
pub previous_epoch_attesters: u64,
pub previous_epoch_boundary_attesters: u64,
pub previous_epoch_head_attesters: u64,
}
#[derive(Clone)]
@@ -85,22 +90,27 @@ pub struct Attesters {
}
impl Attesters {
pub fn empty(num_validators: usize) -> Self {
Self {
statuses: vec![AttesterStatus::default(); num_validators],
balances: TotalBalances::default(),
}
}
pub fn new(state: &BeaconState, spec: &ChainSpec) -> Self {
let mut statuses = Vec::with_capacity(state.validator_registry.len());
let mut balances = TotalBalances::default();
pub fn process_active_validator_indices(&mut self, active_validator_indices: &[usize]) {
let status = AttesterStatus {
is_active: true,
..AttesterStatus::default()
};
for (i, validator) in state.validator_registry.iter().enumerate() {
let mut status = AttesterStatus::default();
for &i in active_validator_indices {
self.statuses[i].update(&status);
if validator.is_active_at(state.current_epoch(spec)) {
status.is_active_in_current_epoch = true;
balances.current_epoch_total += state.get_effective_balance(i, spec);
}
if validator.is_active_at(state.previous_epoch(spec)) {
status.is_active_in_previous_epoch = true;
balances.previous_epoch_total += state.get_effective_balance(i, spec);
}
statuses.push(status);
}
Self { statuses, balances }
}
pub fn process_attestations(
@@ -119,16 +129,16 @@ impl Attesters {
// Profile this attestation, updating the total balances and generating an
// `AttesterStatus` object that applies to all participants in the attestation.
if is_from_epoch(a, state.current_epoch(spec), spec) {
self.balances.current_epoch += attesting_balance;
status.is_current_epoch = true;
self.balances.current_epoch_attesters += attesting_balance;
status.is_current_epoch_attester = true;
if has_common_epoch_boundary_root(a, state, state.current_epoch(spec), spec)? {
self.balances.current_epoch_boundary += attesting_balance;
status.is_current_epoch_boundary = true;
self.balances.current_epoch_boundary_attesters += attesting_balance;
status.is_current_epoch_boundary_attester = true;
}
} else if is_from_epoch(a, state.previous_epoch(spec), spec) {
self.balances.previous_epoch += attesting_balance;
status.is_previous_epoch = true;
self.balances.previous_epoch_attesters += attesting_balance;
status.is_previous_epoch_attester = true;
// The inclusion slot and distance are only required for previous epoch attesters.
status.inclusion_info = InclusionInfo {
@@ -138,13 +148,13 @@ impl Attesters {
};
if has_common_epoch_boundary_root(a, state, state.previous_epoch(spec), spec)? {
self.balances.previous_epoch_boundary += attesting_balance;
status.is_previous_epoch_boundary = true;
self.balances.previous_epoch_boundary_attesters += attesting_balance;
status.is_previous_epoch_boundary_attester = true;
}
if has_common_beacon_block_root(a, state, spec)? {
self.balances.previous_epoch_head += attesting_balance;
status.is_previous_epoch_head = true;
self.balances.previous_epoch_head_attesters += attesting_balance;
status.is_previous_epoch_head_attester = true;
}
}