Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.50.0 AS builder
FROM rust:1.52.1 AS builder
RUN apt-get update && apt-get install -y cmake
COPY . lighthouse
ARG PORTABLE
Expand Down
28 changes: 28 additions & 0 deletions consensus/state_processing/src/per_epoch_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
pub use base::{TotalBalances, ValidatorStatus, ValidatorStatuses};
use errors::EpochProcessingError as Error;
pub use registry_updates::process_registry_updates;
use safe_arith::SafeArith;
pub use slashings::process_slashings;
use types::{BeaconState, ChainSpec, EthSpec};
pub use weigh_justification_and_finalization::weigh_justification_and_finalization;
Expand Down Expand Up @@ -39,3 +40,30 @@ pub fn process_epoch<T: EthSpec>(
BeaconState::Altair(_) => altair::process_epoch(state, spec),
}
}

/// Used to track the changes to a validator's balance.
#[derive(Default, Clone)]
pub struct Delta {
pub rewards: u64,
pub penalties: u64,
}

impl Delta {
/// Reward the validator with the `reward`.
pub fn reward(&mut self, reward: u64) -> Result<(), Error> {
self.rewards = self.rewards.safe_add(reward)?;
Ok(())
}

/// Penalize the validator with the `penalty`.
pub fn penalize(&mut self, penalty: u64) -> Result<(), Error> {
self.penalties = self.penalties.safe_add(penalty)?;
Ok(())
}

/// Combine two deltas.
fn combine(&mut self, other: Delta) -> Result<(), Error> {
self.reward(other.rewards)?;
self.penalize(other.penalties)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,7 @@ use types::consts::altair::{
use types::{BeaconState, ChainSpec, EthSpec};

use crate::common::{altair::get_base_reward, decrease_balance, increase_balance};
use crate::per_epoch_processing::Error;

/// Use to track the changes to a validators balance.
#[derive(Default, Clone)]
pub struct Delta {
rewards: u64,
penalties: u64,
}

impl Delta {
/// Reward the validator with the `reward`.
pub fn reward(&mut self, reward: u64) -> Result<(), Error> {
self.rewards = self.rewards.safe_add(reward)?;
Ok(())
}

/// Penalize the validator with the `penalty`.
pub fn penalize(&mut self, penalty: u64) -> Result<(), Error> {
self.penalties = self.penalties.safe_add(penalty)?;
Ok(())
}

/// Combine two deltas.
fn combine(&mut self, other: Delta) -> Result<(), Error> {
self.reward(other.rewards)?;
self.penalize(other.penalties)
}
}
use crate::per_epoch_processing::{Delta, Error};

/// Apply attester and proposer rewards.
///
Expand Down Expand Up @@ -76,9 +49,9 @@ pub fn process_rewards_and_penalties<T: EthSpec>(
/// Return the deltas for a given flag index by scanning through the participation flags.
///
/// Spec v1.1.0
fn get_flag_index_deltas<T: EthSpec>(
pub fn get_flag_index_deltas<T: EthSpec>(
deltas: &mut Vec<Delta>,
state: &mut BeaconState<T>,
state: &BeaconState<T>,
flag_index: u32,
weight: u64,
total_active_balance: u64,
Expand Down Expand Up @@ -119,7 +92,7 @@ fn get_flag_index_deltas<T: EthSpec>(
Ok(())
}

fn get_inactivity_penalty_deltas<T: EthSpec>(
pub fn get_inactivity_penalty_deltas<T: EthSpec>(
deltas: &mut Vec<Delta>,
state: &BeaconState<T>,
total_active_balance: u64,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,48 @@ use crate::common::{base::get_base_reward, decrease_balance, increase_balance};
use crate::per_epoch_processing::validator_statuses::{
TotalBalances, ValidatorStatus, ValidatorStatuses,
};
use crate::per_epoch_processing::Error;
use crate::per_epoch_processing::{Delta, Error};
use safe_arith::SafeArith;
use std::array::IntoIter as ArrayIter;
use types::{BeaconState, ChainSpec, EthSpec};

/// Use to track the changes to a validators balance.
/// Combination of several deltas for different components of an attestation reward.
///
/// Exists only for compatibility with EF rewards tests.
#[derive(Default, Clone)]
pub struct Delta {
rewards: u64,
penalties: u64,
pub struct AttestationDelta {
pub source_delta: Delta,
pub target_delta: Delta,
pub head_delta: Delta,
pub inclusion_delay_delta: Delta,
pub inactivity_penalty_delta: Delta,
}

impl Delta {
/// Reward the validator with the `reward`.
pub fn reward(&mut self, reward: u64) -> Result<(), Error> {
self.rewards = self.rewards.safe_add(reward)?;
Ok(())
}

/// Penalize the validator with the `penalty`.
pub fn penalize(&mut self, penalty: u64) -> Result<(), Error> {
self.penalties = self.penalties.safe_add(penalty)?;
Ok(())
}

/// Combine two deltas.
fn combine(&mut self, other: Delta) -> Result<(), Error> {
self.reward(other.rewards)?;
self.penalize(other.penalties)
impl AttestationDelta {
/// Flatten into a single delta.
pub fn flatten(self) -> Result<Delta, Error> {
let AttestationDelta {
source_delta,
target_delta,
head_delta,
inclusion_delay_delta,
inactivity_penalty_delta,
} = self;
let mut result = Delta::default();
for delta in ArrayIter::new([
source_delta,
target_delta,
head_delta,
inclusion_delay_delta,
inactivity_penalty_delta,
]) {
result.combine(delta)?;
}
Ok(result)
}
}

/// Apply attester and proposer rewards.
///
/// Spec v0.12.1
pub fn process_rewards_and_penalties<T: EthSpec>(
state: &mut BeaconState<T>,
validator_statuses: &mut ValidatorStatuses,
Expand All @@ -56,28 +64,27 @@ pub fn process_rewards_and_penalties<T: EthSpec>(

// Apply the deltas, erroring on overflow above but not on overflow below (saturating at 0
// instead).
for (i, delta) in deltas.iter().enumerate() {
increase_balance(state, i, delta.rewards)?;
decrease_balance(state, i, delta.penalties)?;
for (i, delta) in deltas.into_iter().enumerate() {
let combined_delta = delta.flatten()?;
increase_balance(state, i, combined_delta.rewards)?;
decrease_balance(state, i, combined_delta.penalties)?;
}

Ok(())
}

/// Apply rewards for participation in attestations during the previous epoch.
///
/// Spec v0.12.1
fn get_attestation_deltas<T: EthSpec>(
pub fn get_attestation_deltas<T: EthSpec>(
state: &BeaconState<T>,
validator_statuses: &ValidatorStatuses,
spec: &ChainSpec,
) -> Result<Vec<Delta>, Error> {
) -> Result<Vec<AttestationDelta>, Error> {
let finality_delay = state
.previous_epoch()
.safe_sub(state.finalized_checkpoint().epoch)?
.as_u64();

let mut deltas = vec![Delta::default(); state.validators().len()];
let mut deltas = vec![AttestationDelta::default(); state.validators().len()];

let total_balances = &validator_statuses.total_balances;

Expand Down Expand Up @@ -106,16 +113,19 @@ fn get_attestation_deltas<T: EthSpec>(
let delta = deltas
.get_mut(index)
.ok_or(Error::DeltaOutOfBounds(index))?;
delta.combine(source_delta)?;
delta.combine(target_delta)?;
delta.combine(head_delta)?;
delta.combine(inclusion_delay_delta)?;
delta.combine(inactivity_penalty_delta)?;
delta.source_delta.combine(source_delta)?;
delta.target_delta.combine(target_delta)?;
delta.head_delta.combine(head_delta)?;
delta.inclusion_delay_delta.combine(inclusion_delay_delta)?;
delta
.inactivity_penalty_delta
.combine(inactivity_penalty_delta)?;

if let Some((proposer_index, proposer_delta)) = proposer_delta {
deltas
.get_mut(proposer_index)
.ok_or(Error::ValidatorStatusesInconsistent)?
.inclusion_delay_delta
.combine(proposer_delta)?;
}
}
Expand Down
1 change: 1 addition & 0 deletions testing/ef_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fake_crypto = ["bls/fake_crypto"]
[dependencies]
bls = { path = "../../crypto/bls", default-features = false }
compare_fields = { path = "../../common/compare_fields" }
compare_fields_derive = { path = "../../common/compare_fields_derive" }
derivative = "2.1.1"
ethereum-types = "0.9.2"
hex = "0.4.2"
Expand Down
2 changes: 2 additions & 0 deletions testing/ef_tests/src/cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod fork;
mod genesis_initialization;
mod genesis_validity;
mod operations;
mod rewards;
mod sanity_blocks;
mod sanity_slots;
mod shuffling;
Expand All @@ -32,6 +33,7 @@ pub use fork::ForkTest;
pub use genesis_initialization::*;
pub use genesis_validity::*;
pub use operations::*;
pub use rewards::RewardsTest;
pub use sanity_blocks::*;
pub use sanity_slots::*;
pub use shuffling::*;
Expand Down
Loading