Add non-compiling half finished changes

This commit is contained in:
Paul Hauner 2019-08-08 10:29:27 +10:00
parent 65ce94b2ef
commit 9f9af746ea
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
2 changed files with 54 additions and 73 deletions

View File

@ -14,10 +14,7 @@ pub use self::verify_proposer_slashing::verify_proposer_slashing;
pub use is_valid_indexed_attestation::{ pub use is_valid_indexed_attestation::{
is_valid_indexed_attestation, is_valid_indexed_attestation_without_signature, is_valid_indexed_attestation, is_valid_indexed_attestation_without_signature,
}; };
pub use verify_attestation::{ pub use verify_attestation::{verify_attestation_for_block, verify_attestation_for_state};
verify_attestation, verify_attestation_time_independent_only,
verify_attestation_without_signature,
};
pub use verify_deposit::{ pub use verify_deposit::{
get_existing_validator_index, verify_deposit_merkle_proof, verify_deposit_signature, get_existing_validator_index, verify_deposit_merkle_proof, verify_deposit_signature,
}; };
@ -37,6 +34,12 @@ mod verify_exit;
mod verify_proposer_slashing; mod verify_proposer_slashing;
mod verify_transfer; mod verify_transfer;
#[derive(PartialEq)]
pub enum VerifySignatures {
True,
False,
}
/// Updates the state for a new block, whilst validating that the block is valid. /// Updates the state for a new block, whilst validating that the block is valid.
/// ///
/// Returns `Ok(())` if the block is valid and the state was successfully updated. Otherwise /// Returns `Ok(())` if the block is valid and the state was successfully updated. Otherwise
@ -312,7 +315,8 @@ pub fn process_attestations<T: EthSpec>(
.par_iter() .par_iter()
.enumerate() .enumerate()
.try_for_each(|(i, attestation)| { .try_for_each(|(i, attestation)| {
verify_attestation(state, attestation, spec).map_err(|e| e.into_with_index(i)) verify_attestation_for_block(state, attestation, spec, VerifySignatures::True)
.map_err(|e| e.into_with_index(i))
})?; })?;
// Update the state in series. // Update the state in series.

View File

@ -1,4 +1,5 @@
use super::errors::{AttestationInvalid as Invalid, AttestationValidationError as Error}; use super::errors::{AttestationInvalid as Invalid, AttestationValidationError as Error};
use super::VerifySignatures;
use crate::common::get_indexed_attestation; use crate::common::get_indexed_attestation;
use crate::per_block_processing::{ use crate::per_block_processing::{
is_valid_indexed_attestation, is_valid_indexed_attestation_without_signature, is_valid_indexed_attestation, is_valid_indexed_attestation_without_signature,
@ -6,67 +7,23 @@ use crate::per_block_processing::{
use tree_hash::TreeHash; use tree_hash::TreeHash;
use types::*; use types::*;
/// Indicates if an `Attestation` is valid to be included in a block in the current epoch of the
/// given state.
///
/// Returns `Ok(())` if the `Attestation` is valid, otherwise indicates the reason for invalidity.
///
/// Spec v0.8.0
pub fn verify_attestation<T: EthSpec>(
state: &BeaconState<T>,
attestation: &Attestation<T>,
spec: &ChainSpec,
) -> Result<(), Error> {
verify_attestation_parametric(state, attestation, spec, true, false)
}
/// Like `verify_attestation` but doesn't run checks which may become true in future states.
pub fn verify_attestation_time_independent_only<T: EthSpec>(
state: &BeaconState<T>,
attestation: &Attestation<T>,
spec: &ChainSpec,
) -> Result<(), Error> {
verify_attestation_parametric(state, attestation, spec, true, true)
}
/// Indicates if an `Attestation` is valid to be included in a block in the current epoch of the
/// given state, without validating the aggregate signature.
///
/// Returns `Ok(())` if the `Attestation` is valid, otherwise indicates the reason for invalidity.
///
/// Spec v0.8.0
pub fn verify_attestation_without_signature<T: EthSpec>(
state: &BeaconState<T>,
attestation: &Attestation<T>,
spec: &ChainSpec,
) -> Result<(), Error> {
verify_attestation_parametric(state, attestation, spec, false, false)
}
/// Indicates if an `Attestation` is valid to be included in a block in the current epoch of the /// Indicates if an `Attestation` is valid to be included in a block in the current epoch of the
/// given state, optionally validating the aggregate signature. /// given state, optionally validating the aggregate signature.
/// ///
///
/// Spec v0.8.0 /// Spec v0.8.0
fn verify_attestation_parametric<T: EthSpec>( pub fn verify_attestation_for_block<T: EthSpec>(
state: &BeaconState<T>, state: &BeaconState<T>,
attestation: &Attestation<T>, attestation: &Attestation<T>,
spec: &ChainSpec, spec: &ChainSpec,
verify_signature: bool, verify_signatures: VerifySignatures,
time_independent_only: bool,
) -> Result<(), Error> { ) -> Result<(), Error> {
let data = &attestation.data; let data = &attestation.data;
verify!(
data.crosslink.shard < T::ShardCount::to_u64(),
Invalid::BadShard
);
// Check attestation slot. // Check attestation slot.
let attestation_slot = state.get_attestation_data_slot(&data)?; let attestation_slot = state.get_attestation_data_slot(&data)?;
verify!( verify!(
time_independent_only attestation_slot + spec.min_attestation_inclusion_delay <= state.slot,
|| attestation_slot + spec.min_attestation_inclusion_delay <= state.slot,
Invalid::IncludedTooEarly { Invalid::IncludedTooEarly {
state: state.slot, state: state.slot,
delay: spec.min_attestation_inclusion_delay, delay: spec.min_attestation_inclusion_delay,
@ -81,27 +38,47 @@ fn verify_attestation_parametric<T: EthSpec>(
} }
); );
// Verify the Casper FFG vote and crosslink data. verify_attestation_for_state(state, attestation, spec, verify_signatures)
if !time_independent_only { }
let parent_crosslink = verify_casper_ffg_vote(attestation, state)?;
verify!( /// Returns `Ok(())` if `attestation` is a valid attestation to the chain that preceeds the given
data.crosslink.parent_root == Hash256::from_slice(&parent_crosslink.tree_hash_root()), /// `state`.
Invalid::BadParentCrosslinkHash ///
); /// Returns a descriptive `Err` if the attestation is malformed or does not accurately reflect the
verify!( /// prior blocks in `state`.
data.crosslink.start_epoch == parent_crosslink.end_epoch, ///
Invalid::BadParentCrosslinkStartEpoch /// Spec v0.8.0
); pub fn verify_attestation_for_state<T: EthSpec>(
verify!( state: &BeaconState<T>,
data.crosslink.end_epoch attestation: &Attestation<T>,
== std::cmp::min( spec: &ChainSpec,
data.target.epoch, verify_signature: VerifySignatures,
parent_crosslink.end_epoch + spec.max_epochs_per_crosslink ) -> Result<(), Error> {
), let data = &attestation.data;
Invalid::BadParentCrosslinkEndEpoch verify!(
); data.crosslink.shard < T::ShardCount::to_u64(),
} Invalid::BadShard
);
// Verify the Casper FFG vote and crosslink data.
let parent_crosslink = verify_casper_ffg_vote(attestation, state)?;
verify!(
data.crosslink.parent_root == Hash256::from_slice(&parent_crosslink.tree_hash_root()),
Invalid::BadParentCrosslinkHash
);
verify!(
data.crosslink.start_epoch == parent_crosslink.end_epoch,
Invalid::BadParentCrosslinkStartEpoch
);
verify!(
data.crosslink.end_epoch
== std::cmp::min(
data.target.epoch,
parent_crosslink.end_epoch + spec.max_epochs_per_crosslink
),
Invalid::BadParentCrosslinkEndEpoch
);
// Crosslink data root is zero (to be removed in phase 1). // Crosslink data root is zero (to be removed in phase 1).
verify!( verify!(
@ -111,7 +88,7 @@ fn verify_attestation_parametric<T: EthSpec>(
// Check signature and bitfields // Check signature and bitfields
let indexed_attestation = get_indexed_attestation(state, attestation)?; let indexed_attestation = get_indexed_attestation(state, attestation)?;
if verify_signature { if verify_signature == VerifySignatures::True {
is_valid_indexed_attestation(state, &indexed_attestation, spec)?; is_valid_indexed_attestation(state, &indexed_attestation, spec)?;
} else { } else {
is_valid_indexed_attestation_without_signature(state, &indexed_attestation, spec)?; is_valid_indexed_attestation_without_signature(state, &indexed_attestation, spec)?;