Skip to content

Commit

Permalink
Fix slasher tests (sigp#5906)
Browse files Browse the repository at this point in the history
* Fix electra tests

* Add electra attestations to double vote tests
  • Loading branch information
pawanjay176 authored and realbigsean committed Jun 25, 2024
1 parent ec3cbc9 commit 951c710
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 46 deletions.
69 changes: 33 additions & 36 deletions slasher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,49 +60,46 @@ impl<E: EthSpec> AttesterSlashingStatus<E> {
Ok(match self {
NotSlashable => None,
AlreadyDoubleVoted => None,
DoubleVote(existing) | SurroundedByExisting(existing) => match *existing {
IndexedAttestation::Base(existing_att) => {
Some(AttesterSlashing::Base(AttesterSlashingBase {
attestation_1: existing_att,
attestation_2: new_attestation
.as_base()
.map_err(|e| format!("{e:?}"))?
.clone(),
}))
}
IndexedAttestation::Electra(existing_att) => {
Some(AttesterSlashing::Electra(AttesterSlashingElectra {
attestation_1: existing_att,
// A double vote should never convert, a surround vote where the surrounding
// vote is electra may convert.
DoubleVote(existing) | SurroundedByExisting(existing) => {
match (&*existing, new_attestation) {
(IndexedAttestation::Base(existing_att), IndexedAttestation::Base(new)) => {
Some(AttesterSlashing::Base(AttesterSlashingBase {
attestation_1: existing_att.clone(),
attestation_2: new.clone(),
}))
}
// A slashing involving an electra attestation type must return an `AttesterSlashingElectra` type
(_, _) => Some(AttesterSlashing::Electra(AttesterSlashingElectra {
attestation_1: existing
.clone()
.to_electra()
.map_err(|e| format!("{e:?}"))?,
attestation_2: new_attestation
.clone()
.to_electra()
.map_err(|e| format!("{e:?}"))?,
})),
}
}
SurroundsExisting(existing) => match (&*existing, new_attestation) {
(IndexedAttestation::Base(existing_att), IndexedAttestation::Base(new)) => {
Some(AttesterSlashing::Base(AttesterSlashingBase {
attestation_1: new.clone(),
attestation_2: existing_att.clone(),
}))
}
// A slashing involving an electra attestation type must return an `AttesterSlashingElectra` type
(_, _) => Some(AttesterSlashing::Electra(AttesterSlashingElectra {
attestation_1: new_attestation
.clone()
.to_electra()
.map_err(|e| format!("{e:?}"))?,
attestation_2: existing
.clone()
.to_electra()
.map_err(|e| format!("{e:?}"))?,
})),
},
SurroundsExisting(existing) => {
match new_attestation {
IndexedAttestation::Base(new_attestation) => {
Some(AttesterSlashing::Base(AttesterSlashingBase {
attestation_1: existing
.as_base()
.map_err(|e| format!("{e:?}"))?
.clone(),
attestation_2: new_attestation.clone(),
}))
}
IndexedAttestation::Electra(new_attestation) => {
Some(AttesterSlashing::Electra(AttesterSlashingElectra {
attestation_1: existing.to_electra().map_err(|e| format!("{e:?}"))?,
// A double vote should never convert, a surround vote where the surrounding
// vote is electra may convert.
attestation_2: new_attestation.clone(),
}))
}
}
}
})
}
}
13 changes: 5 additions & 8 deletions slasher/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,18 @@ pub fn att_slashing(
attestation_1: &IndexedAttestation<E>,
attestation_2: &IndexedAttestation<E>,
) -> AttesterSlashing<E> {
// 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"),
// A slashing involving an electra attestation type must return an electra AttesterSlashing type
(_, _) => AttesterSlashing::Electra(AttesterSlashingElectra {
attestation_1: attestation_1.clone().to_electra().unwrap(),
attestation_2: attestation_2.clone().to_electra().unwrap(),
}),
}
}

Expand Down
3 changes: 1 addition & 2 deletions slasher/tests/attester_slashings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use rayon::prelude::*;
use slasher::{
config::DEFAULT_CHUNK_SIZE,
test_utils::{
att_slashing, chain_spec, indexed_att, indexed_att_electra,
slashed_validators_from_slashings, E,
att_slashing, indexed_att, indexed_att_electra, slashed_validators_from_slashings, E,
},
Config, Slasher,
};
Expand Down

0 comments on commit 951c710

Please sign in to comment.