superstruct the AttesterSlashing (#5636)

* `superstruct` Attester Fork Variants

* Push a little further

* Deal with Encode / Decode of AttesterSlashing

* not so sure about this..

* Stop Encode/Decode Bounds from Propagating Out

* Tons of Changes..

* More Conversions to AttestationRef

* Add AsReference trait (#15)

* Add AsReference trait

* Fix some snafus

* Got it Compiling! :D

* Got Tests Building

* Get beacon chain tests compiling

---------

Co-authored-by: Michael Sproul <micsproul@gmail.com>
This commit is contained in:
ethDreamer
2024-05-02 18:00:21 -05:00
committed by GitHub
parent 3b7132bc0d
commit e6c7f145dd
53 changed files with 1405 additions and 437 deletions

View File

@@ -28,7 +28,8 @@ pub use database::{
};
pub use error::Error;
use types::{AttesterSlashing, EthSpec, IndexedAttestation, ProposerSlashing};
use types::{AttesterSlashing, AttesterSlashingBase, AttesterSlashingElectra};
use types::{EthSpec, IndexedAttestation, ProposerSlashing};
#[derive(Debug, PartialEq)]
pub enum AttesterSlashingStatus<E: EthSpec> {
@@ -59,14 +60,48 @@ impl<E: EthSpec> AttesterSlashingStatus<E> {
match self {
NotSlashable => None,
AlreadyDoubleVoted => None,
DoubleVote(existing) | SurroundedByExisting(existing) => Some(AttesterSlashing {
attestation_1: *existing,
attestation_2: new_attestation.clone(),
}),
SurroundsExisting(existing) => Some(AttesterSlashing {
DoubleVote(existing) | SurroundedByExisting(existing) => {
match (*existing, new_attestation) {
// TODO(electra) - determine when we would convert a Base attestation to Electra / how to handle mismatched attestations here
(IndexedAttestation::Base(existing_att), IndexedAttestation::Base(new_att)) => {
Some(AttesterSlashing::Base(AttesterSlashingBase {
attestation_1: existing_att,
attestation_2: new_att.clone(),
}))
}
(
IndexedAttestation::Electra(existing_att),
IndexedAttestation::Electra(new_att),
) => Some(AttesterSlashing::Electra(AttesterSlashingElectra {
attestation_1: existing_att,
attestation_2: new_att.clone(),
})),
_ => panic!("attestations must be of the same type"),
}
}
// TODO(electra): fix this once we superstruct IndexedAttestation (return the correct type)
SurroundsExisting(existing) => match (*existing, new_attestation) {
(IndexedAttestation::Base(existing_att), IndexedAttestation::Base(new_att)) => {
Some(AttesterSlashing::Base(AttesterSlashingBase {
attestation_1: new_att.clone(),
attestation_2: existing_att,
}))
}
(
IndexedAttestation::Electra(existing_att),
IndexedAttestation::Electra(new_att),
) => Some(AttesterSlashing::Electra(AttesterSlashingElectra {
attestation_1: new_att.clone(),
attestation_2: existing_att,
})),
_ => panic!("attestations must be of the same type"),
},
/*
Some(AttesterSlashing::Base(AttesterSlashingBase {
attestation_1: new_attestation.clone(),
attestation_2: *existing,
}),
})),
*/
}
}
}

View File

@@ -299,7 +299,7 @@ impl<E: EthSpec> Slasher<E> {
self.log,
"Found double-vote slashing";
"validator_index" => validator_index,
"epoch" => slashing.attestation_1.data().target.epoch,
"epoch" => slashing.attestation_1().data().target.epoch,
);
slashings.insert(slashing);
}

View File

@@ -1,8 +1,8 @@
use std::collections::HashSet;
use types::{
indexed_attestation::IndexedAttestationBase, AggregateSignature, AttestationData,
AttesterSlashing, BeaconBlockHeader, Checkpoint, Epoch, Hash256, IndexedAttestation,
MainnetEthSpec, Signature, SignedBeaconBlockHeader, Slot,
AttesterSlashing, AttesterSlashingBase, AttesterSlashingElectra, BeaconBlockHeader, Checkpoint,
Epoch, Hash256, IndexedAttestation, MainnetEthSpec, Signature, SignedBeaconBlockHeader, Slot,
};
pub type E = MainnetEthSpec;
@@ -37,9 +37,21 @@ pub fn att_slashing(
attestation_1: &IndexedAttestation<E>,
attestation_2: &IndexedAttestation<E>,
) -> AttesterSlashing<E> {
AttesterSlashing {
attestation_1: attestation_1.clone(),
attestation_2: attestation_2.clone(),
// TODO(electra): fix this one we superstruct IndexedAttestation (return the correct type)
match (attestation_1, attestation_2) {
(IndexedAttestation::Base(att1), IndexedAttestation::Base(att2)) => {
AttesterSlashing::Base(AttesterSlashingBase {
attestation_1: att1.clone(),
attestation_2: att2.clone(),
})
}
(IndexedAttestation::Electra(att1), IndexedAttestation::Electra(att2)) => {
AttesterSlashing::Electra(AttesterSlashingElectra {
attestation_1: att1.clone(),
attestation_2: att2.clone(),
})
}
_ => panic!("attestations must be of the same type"),
}
}
@@ -61,8 +73,8 @@ pub fn slashed_validators_from_slashings(slashings: &HashSet<AttesterSlashing<E>
slashings
.iter()
.flat_map(|slashing| {
let att1 = &slashing.attestation_1;
let att2 = &slashing.attestation_2;
let att1 = slashing.attestation_1();
let att2 = slashing.attestation_2();
assert!(
att1.is_double_vote(att2) || att1.is_surround_vote(att2),
"invalid slashing: {:#?}",