Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update reward calculations to v0.11.0 - Handle u64 overflow #921

Merged
merged 1 commit into from Mar 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions eth2/state_processing/src/per_epoch_processing/apply_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ fn get_attestation_deltas<T: EthSpec>(

/// Determine the delta for a single validator, sans proposer rewards.
///
/// Spec v0.10.1
/// Spec v0.11.0
fn get_attestation_delta<T: EthSpec>(
validator: &ValidatorStatus,
total_balances: &TotalBalances,
Expand All @@ -152,16 +152,24 @@ fn get_attestation_delta<T: EthSpec>(
return delta;
}

let total_balance = total_balances.current_epoch;
let total_attesting_balance = total_balances.previous_epoch_attesters;
let matching_target_balance = total_balances.previous_epoch_target_attesters;
let matching_head_balance = total_balances.previous_epoch_head_attesters;
// Handle integer overflow by dividing these quantities by EFFECTIVE_BALANCE_INCREMENT
// Spec:
// - increment = EFFECTIVE_BALANCE_INCREMENT
// - reward_numerator = get_base_reward(state, index) * (attesting_balance // increment)
// - rewards[index] = reward_numerator // (total_balance // increment)
let total_balance_ebi = total_balances.current_epoch / spec.effective_balance_increment;
let total_attesting_balance_ebi =
total_balances.previous_epoch_attesters / spec.effective_balance_increment;
let matching_target_balance_ebi =
total_balances.previous_epoch_target_attesters / spec.effective_balance_increment;
let matching_head_balance_ebi =
total_balances.previous_epoch_head_attesters / spec.effective_balance_increment;

// Expected FFG source.
// Spec:
// - validator index in `get_unslashed_attesting_indices(state, matching_source_attestations)`
if validator.is_previous_epoch_attester && !validator.is_slashed {
delta.reward(base_reward * total_attesting_balance / total_balance);
delta.reward(base_reward * total_attesting_balance_ebi / total_balance_ebi);
// Inclusion speed bonus
let proposer_reward = base_reward / spec.proposer_reward_quotient;
let max_attester_reward = base_reward - proposer_reward;
Expand All @@ -177,7 +185,7 @@ fn get_attestation_delta<T: EthSpec>(
// Spec:
// - validator index in `get_unslashed_attesting_indices(state, matching_target_attestations)`
if validator.is_previous_epoch_target_attester && !validator.is_slashed {
delta.reward(base_reward * matching_target_balance / total_balance);
delta.reward(base_reward * matching_target_balance_ebi / total_balance_ebi);
} else {
delta.penalize(base_reward);
}
Expand All @@ -186,7 +194,7 @@ fn get_attestation_delta<T: EthSpec>(
// Spec:
// - validator index in `get_unslashed_attesting_indices(state, matching_head_attestations)`
if validator.is_previous_epoch_head_attester && !validator.is_slashed {
delta.reward(base_reward * matching_head_balance / total_balance);
delta.reward(base_reward * matching_head_balance_ebi / total_balance_ebi);
} else {
delta.penalize(base_reward);
}
Expand Down