2021-07-09 06:15:32 +00:00
|
|
|
#![deny(clippy::wildcard_imports)]
|
|
|
|
|
2021-07-27 07:01:01 +00:00
|
|
|
pub use epoch_processing_summary::EpochProcessingSummary;
|
2019-03-07 00:32:53 +00:00
|
|
|
use errors::EpochProcessingError as Error;
|
2022-07-25 23:53:26 +00:00
|
|
|
pub use justification_and_finalization_state::JustificationAndFinalizationState;
|
2021-07-09 06:15:32 +00:00
|
|
|
pub use registry_updates::process_registry_updates;
|
2020-04-20 02:35:11 +00:00
|
|
|
use safe_arith::SafeArith;
|
2021-07-09 06:15:32 +00:00
|
|
|
pub use slashings::process_slashings;
|
|
|
|
use types::{BeaconState, ChainSpec, EthSpec};
|
|
|
|
pub use weigh_justification_and_finalization::weigh_justification_and_finalization;
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2021-07-09 06:15:32 +00:00
|
|
|
pub mod altair;
|
|
|
|
pub mod base;
|
|
|
|
pub mod effective_balance_updates;
|
2021-07-27 07:01:01 +00:00
|
|
|
pub mod epoch_processing_summary;
|
2019-03-06 07:57:41 +00:00
|
|
|
pub mod errors;
|
2021-07-09 06:15:32 +00:00
|
|
|
pub mod historical_roots_update;
|
2022-07-25 23:53:26 +00:00
|
|
|
pub mod justification_and_finalization_state;
|
2019-05-13 07:32:06 +00:00
|
|
|
pub mod registry_updates;
|
2021-07-09 06:15:32 +00:00
|
|
|
pub mod resets;
|
|
|
|
pub mod slashings;
|
2019-03-07 00:32:53 +00:00
|
|
|
pub mod tests;
|
2021-07-09 06:15:32 +00:00
|
|
|
pub mod weigh_justification_and_finalization;
|
2019-08-30 03:29:26 +00:00
|
|
|
|
2019-03-12 07:02:53 +00:00
|
|
|
/// Performs per-epoch processing on some BeaconState.
|
|
|
|
///
|
|
|
|
/// Mutates the given `BeaconState`, returning early if an error is encountered. If an error is
|
|
|
|
/// returned, a state might be "half-processed" and therefore in an invalid state.
|
2021-07-09 06:15:32 +00:00
|
|
|
pub fn process_epoch<T: EthSpec>(
|
2019-05-08 05:36:02 +00:00
|
|
|
state: &mut BeaconState<T>,
|
|
|
|
spec: &ChainSpec,
|
2021-08-31 23:31:36 +00:00
|
|
|
) -> Result<EpochProcessingSummary<T>, Error> {
|
2021-07-09 06:15:32 +00:00
|
|
|
// Verify that the `BeaconState` instantiation matches the fork at `state.slot()`.
|
|
|
|
state
|
|
|
|
.fork_name(spec)
|
|
|
|
.map_err(Error::InconsistentStateFork)?;
|
|
|
|
|
|
|
|
match state {
|
|
|
|
BeaconState::Base(_) => base::process_epoch(state, spec),
|
2021-09-08 18:45:22 +00:00
|
|
|
BeaconState::Altair(_) | BeaconState::Merge(_) => altair::process_epoch(state, spec),
|
2019-03-19 06:16:33 +00:00
|
|
|
}
|
2019-03-06 07:57:41 +00:00
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2021-07-09 06:15:32 +00:00
|
|
|
/// Used to track the changes to a validator's balance.
|
|
|
|
#[derive(Default, Clone)]
|
|
|
|
pub struct Delta {
|
|
|
|
pub rewards: u64,
|
|
|
|
pub penalties: u64,
|
|
|
|
}
|
2019-03-08 23:37:41 +00:00
|
|
|
|
2021-07-09 06:15:32 +00:00
|
|
|
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(())
|
2019-05-14 05:00:18 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 06:15:32 +00:00
|
|
|
/// Penalize the validator with the `penalty`.
|
|
|
|
pub fn penalize(&mut self, penalty: u64) -> Result<(), Error> {
|
|
|
|
self.penalties = self.penalties.safe_add(penalty)?;
|
|
|
|
Ok(())
|
2019-05-14 05:00:18 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 06:15:32 +00:00
|
|
|
/// Combine two deltas.
|
|
|
|
fn combine(&mut self, other: Delta) -> Result<(), Error> {
|
|
|
|
self.reward(other.rewards)?;
|
|
|
|
self.penalize(other.penalties)
|
2019-03-19 08:27:10 +00:00
|
|
|
}
|
2019-03-08 23:37:41 +00:00
|
|
|
}
|