Skip to content
Snippets Groups Projects
Commit 93f30c4c authored by Wei Tang's avatar Wei Tang
Browse files

beacon, yamltests: process slashings and pass the test

parent 0385ea86
Branches
No related merge requests found
......@@ -3,3 +3,4 @@ mod justification;
mod crosslink;
mod reward;
mod registry;
mod slashing;
use crate::{Config, BeaconState};
use core::cmp::min;
impl<C: Config> BeaconState<C> {
/// Process slashings
pub fn process_slashings(&mut self) {
let current_epoch = self.current_epoch();
let total_balance = self.total_active_balance();
for index in 0..(self.validators.len() as u64) {
let penalty = {
let validator = &self.validators[index as usize];
if validator.slashed &&
current_epoch + C::epochs_per_slashings_vector() / 2 ==
validator.withdrawable_epoch
{
let increment = C::effective_balance_increment();
let penalty_numerator = validator.effective_balance / increment *
min(self.slashings.iter().fold(0, |acc, x| acc + *x) * 3, total_balance);
let penalty = penalty_numerator / total_balance * increment;
Some(penalty)
} else {
None
}
};
if let Some(penalty) = penalty {
self.decrease_balance(index, penalty);
}
}
}
}
......@@ -49,3 +49,19 @@ impl<C: Config> Test for RegistryUpdatesTest<C> {
});
}
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct SlashingsTest<C: Config> {
pub description: String,
pub pre: BeaconState<C>,
pub post: Option<BeaconState<C>>,
}
impl<C: Config> Test for SlashingsTest<C> {
fn run(&self) {
run_test_with(&self.description, &self.pre, self.post.as_ref(), |state| {
Ok(state.process_slashings())
});
}
}
......@@ -48,6 +48,7 @@ fn run_all<C: Config + serde::Serialize + DeserializeOwned>(runner: &str, file:
"justification_and_finalization" => run::<JustificationAndFinalizationTest<C>>(file),
"crosslinks" => run::<CrosslinksTest<C>>(file),
"registry_updates" => run::<RegistryUpdatesTest<C>>(file),
"slashings" => run::<SlashingsTest<C>>(file),
// "blocks" => run::<BlocksTest, _>(file, &config),
// "slots" => run::<SlotsTest, _>(file, &config),
_ => panic!("Unsupported runner"),
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment