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

beacon: pass all process_attestation tests

parent 8e925c4d
Branches
Tags
No related merge requests found
use crate::primitives::*;
use crate::types::*;
use crate::{Config, BeaconState, Error, BLSConfig, utils};
use bm_le::{tree_root, MaxVec};
use core::cmp::min;
impl<C: Config> BeaconState<C> {
/// Push a new `Attestation` to the state.
pub fn process_attestation<BLS: BLSConfig>(&mut self, attestation: Attestation<C>) -> Result<(), Error> {
let data = attestation.data.clone();
if !(data.crosslink.shard < C::shard_count()) {
return Err(Error::AttestationIncorrectCrosslinkData)
}
if !(data.target.epoch == self.current_epoch() ||
data.target.epoch == self.previous_epoch())
{
return Err(Error::AttestationIncorrectJustifiedEpochOrBlockRoot)
}
let attestation_slot = self.attestation_data_slot(&data)?;
if !(attestation_slot + C::min_attestation_inclusion_delay() <=
self.slot &&
self.slot <=
attestation_slot + C::slots_per_epoch())
{
return Err(Error::AttestationSubmittedTooQuickly)
}
let pending_attestation = PendingAttestation {
data: data.clone(),
aggregation_bits: attestation.aggregation_bits.clone(),
inclusion_delay: self.slot - attestation_slot,
proposer_index: self.beacon_proposer_index()?,
};
let (push_current, parent_crosslink) =
if data.target.epoch == self.current_epoch() {
if data.source != self.current_justified_checkpoint {
return Err(Error::AttestationInvalidData)
}
(true, self.current_crosslinks[data.crosslink.shard as usize].clone())
} else {
(false, self.previous_crosslinks[data.crosslink.shard as usize].clone())
};
if !(data.crosslink.parent_root == tree_root::<C::Digest, _>(&parent_crosslink) &&
data.crosslink.start_epoch == parent_crosslink.end_epoch &&
data.crosslink.end_epoch == min(data.target.epoch,
parent_crosslink.end_epoch +
C::max_epochs_per_crosslink()) &&
data.crosslink.data_root == Default::default())
{
return Err(Error::AttestationInvalidCrosslink)
}
if !self.is_valid_indexed_attestation::<BLS>(&self.indexed_attestation(attestation)?) {
return Err(Error::AttestationInvalidSignature)
}
if push_current {
self.current_epoch_attestations.push(pending_attestation);
} else {
self.previous_epoch_attestations.push(pending_attestation);
}
Ok(())
}
}
mod proposer_slashing;
mod attester_slashing;
mod attestation;
......@@ -38,7 +38,7 @@ fn main() {
fn run_all<C: Config + DeserializeOwned>(runner: &str, file: File) {
match runner {
// "attestation" => run::<AttestationTest, _>(file, &config),
"attestation" => run::<AttestationTest<C>>(file),
"attester_slashing" => run::<AttesterSlashingTest<C>>(file),
// "block_header" => run::<BlockHeaderTest, _>(file, &config),
// "deposit" => run::<DepositTest, _>(file, &config),
......
......@@ -3,25 +3,25 @@ use beacon::types::*;
use beacon::{BeaconState, Config, BLSConfig};
use crate::{TestWithBLS, run_test_with};
// #[derive(Serialize, Deserialize, Debug)]
// #[serde(deny_unknown_fields)]
// pub struct AttestationTest {
// pub bls_setting: Option<usize>,
// pub description: String,
// pub pre: BeaconState,
// pub attestation: Attestation,
// pub post: Option<BeaconState>,
// }
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct AttestationTest<C: Config> {
pub bls_setting: Option<usize>,
pub description: String,
pub pre: BeaconState<C>,
pub attestation: Attestation<C>,
pub post: Option<BeaconState<C>>,
}
// impl TestWithBLS for AttestationTest {
// fn bls_setting(&self) -> Option<usize> { self.bls_setting }
impl<C: Config> TestWithBLS for AttestationTest<C> {
fn bls_setting(&self) -> Option<usize> { self.bls_setting }
// fn run<C: Config>(&self, config: &C) {
// run_test_with(&self.description, &self.pre, self.post.as_ref(), config, |executive| {
// executive.process_attestation(self.attestation.clone())
// });
// }
// }
fn run<BLS: BLSConfig>(&self) {
run_test_with(&self.description, &self.pre, self.post.as_ref(), |state| {
state.process_attestation::<BLS>(self.attestation.clone())
});
}
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
......
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