mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-10 20:22:02 +00:00
state_processing: IndexedAttestation rename
This commit is contained in:
@@ -80,10 +80,10 @@ pub enum BlockInvalid {
|
||||
MaxExitsExceeded,
|
||||
MaxTransfersExceed,
|
||||
AttestationInvalid(usize, AttestationInvalid),
|
||||
/// A `SlashableAttestation` inside an `AttesterSlashing` was invalid.
|
||||
/// A `IndexedAttestation` inside an `AttesterSlashing` was invalid.
|
||||
///
|
||||
/// To determine the offending `AttesterSlashing` index, divide the error message `usize` by two.
|
||||
SlashableAttestationInvalid(usize, SlashableAttestationInvalid),
|
||||
IndexedAttestationInvalid(usize, IndexedAttestationInvalid),
|
||||
AttesterSlashingInvalid(usize, AttesterSlashingInvalid),
|
||||
ProposerSlashingInvalid(usize, ProposerSlashingInvalid),
|
||||
DepositInvalid(usize, DepositInvalid),
|
||||
@@ -194,10 +194,10 @@ pub enum AttesterSlashingInvalid {
|
||||
AttestationDataIdentical,
|
||||
/// The attestations were not in conflict.
|
||||
NotSlashable,
|
||||
/// The first `SlashableAttestation` was invalid.
|
||||
SlashableAttestation1Invalid(SlashableAttestationInvalid),
|
||||
/// The second `SlashableAttestation` was invalid.
|
||||
SlashableAttestation2Invalid(SlashableAttestationInvalid),
|
||||
/// The first `IndexedAttestation` was invalid.
|
||||
IndexedAttestation1Invalid(IndexedAttestationInvalid),
|
||||
/// The second `IndexedAttestation` was invalid.
|
||||
IndexedAttestation2Invalid(IndexedAttestationInvalid),
|
||||
/// The validator index is unknown. One cannot slash one who does not exist.
|
||||
UnknownValidator(u64),
|
||||
/// The specified validator has already been withdrawn.
|
||||
@@ -210,19 +210,19 @@ impl_from_beacon_state_error!(AttesterSlashingValidationError);
|
||||
impl_into_with_index_with_beacon_error!(AttesterSlashingValidationError, AttesterSlashingInvalid);
|
||||
|
||||
/*
|
||||
* `SlashableAttestation` Validation
|
||||
* `IndexedAttestation` Validation
|
||||
*/
|
||||
|
||||
/// The object is invalid or validation failed.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum SlashableAttestationValidationError {
|
||||
pub enum IndexedAttestationValidationError {
|
||||
/// Validation completed successfully and the object is invalid.
|
||||
Invalid(SlashableAttestationInvalid),
|
||||
Invalid(IndexedAttestationInvalid),
|
||||
}
|
||||
|
||||
/// Describes why an object is invalid.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum SlashableAttestationInvalid {
|
||||
pub enum IndexedAttestationInvalid {
|
||||
/// The custody bitfield has some bits set `true`. This is not allowed in phase 0.
|
||||
CustodyBitfieldHasSetBits,
|
||||
/// No validator indices were specified.
|
||||
@@ -245,17 +245,17 @@ pub enum SlashableAttestationInvalid {
|
||||
BadSignature,
|
||||
}
|
||||
|
||||
impl Into<SlashableAttestationInvalid> for SlashableAttestationValidationError {
|
||||
fn into(self) -> SlashableAttestationInvalid {
|
||||
impl Into<IndexedAttestationInvalid> for IndexedAttestationValidationError {
|
||||
fn into(self) -> IndexedAttestationInvalid {
|
||||
match self {
|
||||
SlashableAttestationValidationError::Invalid(e) => e,
|
||||
IndexedAttestationValidationError::Invalid(e) => e,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_into_with_index_without_beacon_error!(
|
||||
SlashableAttestationValidationError,
|
||||
SlashableAttestationInvalid
|
||||
IndexedAttestationValidationError,
|
||||
IndexedAttestationInvalid
|
||||
);
|
||||
|
||||
/*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::errors::{AttesterSlashingInvalid as Invalid, AttesterSlashingValidationError as Error};
|
||||
use super::verify_slashable_attestation::verify_slashable_attestation;
|
||||
use super::verify_indexed_attestation::verify_indexed_attestation;
|
||||
use types::*;
|
||||
|
||||
/// Indicates if an `AttesterSlashing` is valid to be included in a block in the current epoch of the given
|
||||
@@ -11,27 +11,27 @@ use types::*;
|
||||
pub fn verify_attester_slashing(
|
||||
state: &BeaconState,
|
||||
attester_slashing: &AttesterSlashing,
|
||||
should_verify_slashable_attestations: bool,
|
||||
should_verify_indexed_attestations: bool,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
let slashable_attestation_1 = &attester_slashing.slashable_attestation_1;
|
||||
let slashable_attestation_2 = &attester_slashing.slashable_attestation_2;
|
||||
let indexed_attestation_1 = &attester_slashing.indexed_attestation_1;
|
||||
let indexed_attestation_2 = &attester_slashing.indexed_attestation_2;
|
||||
|
||||
verify!(
|
||||
slashable_attestation_1.data != slashable_attestation_2.data,
|
||||
indexed_attestation_1.data != indexed_attestation_2.data,
|
||||
Invalid::AttestationDataIdentical
|
||||
);
|
||||
verify!(
|
||||
slashable_attestation_1.is_double_vote(slashable_attestation_2, spec)
|
||||
| slashable_attestation_1.is_surround_vote(slashable_attestation_2, spec),
|
||||
indexed_attestation_1.is_double_vote(indexed_attestation_2, spec)
|
||||
| indexed_attestation_1.is_surround_vote(indexed_attestation_2, spec),
|
||||
Invalid::NotSlashable
|
||||
);
|
||||
|
||||
if should_verify_slashable_attestations {
|
||||
verify_slashable_attestation(state, &slashable_attestation_1, spec)
|
||||
.map_err(|e| Error::Invalid(Invalid::SlashableAttestation1Invalid(e.into())))?;
|
||||
verify_slashable_attestation(state, &slashable_attestation_2, spec)
|
||||
.map_err(|e| Error::Invalid(Invalid::SlashableAttestation2Invalid(e.into())))?;
|
||||
if should_verify_indexed_attestations {
|
||||
verify_indexed_attestation(state, &indexed_attestation_1, spec)
|
||||
.map_err(|e| Error::Invalid(Invalid::IndexedAttestation1Invalid(e.into())))?;
|
||||
verify_indexed_attestation(state, &indexed_attestation_2, spec)
|
||||
.map_err(|e| Error::Invalid(Invalid::IndexedAttestation2Invalid(e.into())))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -66,31 +66,31 @@ pub fn gather_attester_slashing_indices_modular<F>(
|
||||
where
|
||||
F: Fn(u64, &Validator) -> bool,
|
||||
{
|
||||
let slashable_attestation_1 = &attester_slashing.slashable_attestation_1;
|
||||
let slashable_attestation_2 = &attester_slashing.slashable_attestation_2;
|
||||
let indexed_attestation_1 = &attester_slashing.indexed_attestation_1;
|
||||
let indexed_attestation_2 = &attester_slashing.indexed_attestation_2;
|
||||
|
||||
let mut slashable_indices = Vec::with_capacity(spec.max_indices_per_slashable_vote);
|
||||
for i in &slashable_attestation_1.validator_indices {
|
||||
let mut indexed_indices = Vec::with_capacity(spec.max_indices_per_indexed_vote);
|
||||
for i in &indexed_attestation_1.validator_indices {
|
||||
let validator = state
|
||||
.validator_registry
|
||||
.get(*i as usize)
|
||||
.ok_or_else(|| Error::Invalid(Invalid::UnknownValidator(*i)))?;
|
||||
|
||||
if slashable_attestation_2.validator_indices.contains(&i) & !is_slashed(*i, validator) {
|
||||
// TODO: verify that we should reject any slashable attestation which includes a
|
||||
if indexed_attestation_2.validator_indices.contains(&i) & !is_slashed(*i, validator) {
|
||||
// TODO: verify that we should reject any indexed attestation which includes a
|
||||
// withdrawn validator. PH has asked the question on gitter, awaiting response.
|
||||
verify!(
|
||||
validator.withdrawable_epoch > state.slot.epoch(spec.slots_per_epoch),
|
||||
Invalid::ValidatorAlreadyWithdrawn(*i)
|
||||
);
|
||||
|
||||
slashable_indices.push(*i);
|
||||
indexed_indices.push(*i);
|
||||
}
|
||||
}
|
||||
|
||||
verify!(!slashable_indices.is_empty(), Invalid::NoSlashableIndices);
|
||||
verify!(!indexed_indices.is_empty(), Invalid::NoSlashableIndices);
|
||||
|
||||
slashable_indices.shrink_to_fit();
|
||||
indexed_indices.shrink_to_fit();
|
||||
|
||||
Ok(slashable_indices)
|
||||
Ok(indexed_indices)
|
||||
}
|
||||
|
||||
@@ -1,52 +1,50 @@
|
||||
use super::errors::{
|
||||
SlashableAttestationInvalid as Invalid, SlashableAttestationValidationError as Error,
|
||||
IndexedAttestationInvalid as Invalid, IndexedAttestationValidationError as Error,
|
||||
};
|
||||
use crate::common::verify_bitfield_length;
|
||||
use tree_hash::TreeHash;
|
||||
use types::*;
|
||||
|
||||
/// Indicates if a `SlashableAttestation` is valid to be included in a block in the current epoch of the given
|
||||
/// Indicates if a `IndexedAttestation` is valid to be included in a block in the current epoch of the given
|
||||
/// state.
|
||||
///
|
||||
/// Returns `Ok(())` if the `SlashableAttestation` is valid, otherwise indicates the reason for invalidity.
|
||||
/// Returns `Ok(())` if the `IndexedAttestation` is valid, otherwise indicates the reason for invalidity.
|
||||
///
|
||||
/// Spec v0.5.1
|
||||
pub fn verify_slashable_attestation(
|
||||
pub fn verify_indexed_attestation(
|
||||
state: &BeaconState,
|
||||
slashable_attestation: &SlashableAttestation,
|
||||
indexed_attestation: &IndexedAttestation,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
if slashable_attestation.custody_bitfield.num_set_bits() > 0 {
|
||||
if indexed_attestation.custody_bitfield.num_set_bits() > 0 {
|
||||
invalid!(Invalid::CustodyBitfieldHasSetBits);
|
||||
}
|
||||
|
||||
if slashable_attestation.validator_indices.is_empty() {
|
||||
if indexed_attestation.validator_indices.is_empty() {
|
||||
invalid!(Invalid::NoValidatorIndices);
|
||||
}
|
||||
|
||||
for i in 0..(slashable_attestation.validator_indices.len() - 1) {
|
||||
if slashable_attestation.validator_indices[i]
|
||||
>= slashable_attestation.validator_indices[i + 1]
|
||||
for i in 0..(indexed_attestation.validator_indices.len() - 1) {
|
||||
if indexed_attestation.validator_indices[i] >= indexed_attestation.validator_indices[i + 1]
|
||||
{
|
||||
invalid!(Invalid::BadValidatorIndicesOrdering(i));
|
||||
}
|
||||
}
|
||||
|
||||
if !verify_bitfield_length(
|
||||
&slashable_attestation.custody_bitfield,
|
||||
slashable_attestation.validator_indices.len(),
|
||||
&indexed_attestation.custody_bitfield,
|
||||
indexed_attestation.validator_indices.len(),
|
||||
) {
|
||||
invalid!(Invalid::BadCustodyBitfieldLength(
|
||||
slashable_attestation.validator_indices.len(),
|
||||
slashable_attestation.custody_bitfield.len()
|
||||
indexed_attestation.validator_indices.len(),
|
||||
indexed_attestation.custody_bitfield.len()
|
||||
));
|
||||
}
|
||||
|
||||
if slashable_attestation.validator_indices.len() > spec.max_indices_per_slashable_vote as usize
|
||||
{
|
||||
if indexed_attestation.validator_indices.len() > spec.max_indices_per_indexed_vote as usize {
|
||||
invalid!(Invalid::MaxIndicesExceed(
|
||||
spec.max_indices_per_slashable_vote as usize,
|
||||
slashable_attestation.validator_indices.len()
|
||||
spec.max_indices_per_indexed_vote as usize,
|
||||
indexed_attestation.validator_indices.len()
|
||||
));
|
||||
}
|
||||
|
||||
@@ -57,8 +55,8 @@ pub fn verify_slashable_attestation(
|
||||
let mut aggregate_pubs = vec![AggregatePublicKey::new(); 2];
|
||||
let mut message_exists = vec![false; 2];
|
||||
|
||||
for (i, v) in slashable_attestation.validator_indices.iter().enumerate() {
|
||||
let custody_bit = match slashable_attestation.custody_bitfield.get(i) {
|
||||
for (i, v) in indexed_attestation.validator_indices.iter().enumerate() {
|
||||
let custody_bit = match indexed_attestation.custody_bitfield.get(i) {
|
||||
Ok(bit) => bit,
|
||||
Err(_) => unreachable!(),
|
||||
};
|
||||
@@ -74,12 +72,12 @@ pub fn verify_slashable_attestation(
|
||||
}
|
||||
|
||||
let message_0 = AttestationDataAndCustodyBit {
|
||||
data: slashable_attestation.data.clone(),
|
||||
data: indexed_attestation.data.clone(),
|
||||
custody_bit: false,
|
||||
}
|
||||
.tree_hash_root();
|
||||
let message_1 = AttestationDataAndCustodyBit {
|
||||
data: slashable_attestation.data.clone(),
|
||||
data: indexed_attestation.data.clone(),
|
||||
custody_bit: true,
|
||||
}
|
||||
.tree_hash_root();
|
||||
@@ -97,12 +95,12 @@ pub fn verify_slashable_attestation(
|
||||
}
|
||||
|
||||
let domain = {
|
||||
let epoch = slashable_attestation.data.slot.epoch(spec.slots_per_epoch);
|
||||
let epoch = indexed_attestation.data.slot.epoch(spec.slots_per_epoch);
|
||||
spec.get_domain(epoch, Domain::Attestation, &state.fork)
|
||||
};
|
||||
|
||||
verify!(
|
||||
slashable_attestation
|
||||
indexed_attestation
|
||||
.aggregate_signature
|
||||
.verify_multiple(&messages[..], domain, &keys[..]),
|
||||
Invalid::BadSignature
|
||||
Reference in New Issue
Block a user