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

spec compliant process_sync_aggregate #15

Merged
merged 2 commits into from
Feb 19, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use crate::{signature_sets::sync_aggregate_signature_set, VerifySignatures};
use safe_arith::SafeArith;
use std::borrow::Cow;
use types::consts::altair::{PROPOSER_WEIGHT, SYNC_REWARD_WEIGHT, WEIGHT_DENOMINATOR};
use types::{BeaconState, ChainSpec, EthSpec, PublicKeyBytes, SyncAggregate, Unsigned};
use types::{
BeaconState, BeaconStateError, ChainSpec, EthSpec, PublicKeyBytes, SyncAggregate, Unsigned,
};

pub fn process_sync_aggregate<T: EthSpec>(
state: &mut BeaconState<T>,
Expand Down Expand Up @@ -47,20 +49,35 @@ pub fn process_sync_aggregate<T: EthSpec>(
// Apply participant and proposer rewards
let committee_indices = state.get_sync_committee_indices(&current_sync_committee)?;

let mut total_proposer_reward = 0;
let proposer_index = proposer_index as usize;
let mut proposer_balance = *state
.balances()
.get(proposer_index)
.ok_or(BeaconStateError::BalancesOutOfBounds(proposer_index))?;

for (participant_index, participation_bit) in committee_indices
.into_iter()
.zip(aggregate.sync_committee_bits.iter())
{
// FIXME(sproul): double-check this for Capella, proposer shouldn't have 0 effective balance
if participation_bit {
increase_balance(state, participant_index, participant_reward)?;
total_proposer_reward.safe_add_assign(proposer_reward)?;
// Accumulate proposer rewards in a temp var in case the proposer has very low balance, is
// part of the sync committee, does not participate and its penalties saturate.
if participant_index == proposer_index {
proposer_balance.safe_add_assign(participant_reward)?;
} else {
increase_balance(state, participant_index, participant_reward)?;
}
proposer_balance.safe_add_assign(proposer_reward)?;
} else {
decrease_balance(state, participant_index, participant_reward)?;
if participant_index == proposer_index {
proposer_balance = proposer_balance.saturating_sub(participant_reward);
} else {
decrease_balance(state, participant_index, participant_reward)?;
}
}
}
increase_balance(state, proposer_index as usize, total_proposer_reward)?;

*state.get_balance_mut(proposer_index)? = proposer_balance;

Ok(())
}
Expand Down
Loading