Skip to content

Commit 2fe5588

Browse files
pawanjay176realbigsean
authored andcommitted
Fix slasher tests (sigp#5906)
* Fix electra tests * Add electra attestations to double vote tests
1 parent 9e3c5a6 commit 2fe5588

File tree

3 files changed

+39
-46
lines changed

3 files changed

+39
-46
lines changed

slasher/src/lib.rs

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -60,49 +60,46 @@ impl<E: EthSpec> AttesterSlashingStatus<E> {
6060
Ok(match self {
6161
NotSlashable => None,
6262
AlreadyDoubleVoted => None,
63-
DoubleVote(existing) | SurroundedByExisting(existing) => match *existing {
64-
IndexedAttestation::Base(existing_att) => {
65-
Some(AttesterSlashing::Base(AttesterSlashingBase {
66-
attestation_1: existing_att,
67-
attestation_2: new_attestation
68-
.as_base()
69-
.map_err(|e| format!("{e:?}"))?
70-
.clone(),
71-
}))
72-
}
73-
IndexedAttestation::Electra(existing_att) => {
74-
Some(AttesterSlashing::Electra(AttesterSlashingElectra {
75-
attestation_1: existing_att,
76-
// A double vote should never convert, a surround vote where the surrounding
77-
// vote is electra may convert.
63+
DoubleVote(existing) | SurroundedByExisting(existing) => {
64+
match (&*existing, new_attestation) {
65+
(IndexedAttestation::Base(existing_att), IndexedAttestation::Base(new)) => {
66+
Some(AttesterSlashing::Base(AttesterSlashingBase {
67+
attestation_1: existing_att.clone(),
68+
attestation_2: new.clone(),
69+
}))
70+
}
71+
// A slashing involving an electra attestation type must return an `AttesterSlashingElectra` type
72+
(_, _) => Some(AttesterSlashing::Electra(AttesterSlashingElectra {
73+
attestation_1: existing
74+
.clone()
75+
.to_electra()
76+
.map_err(|e| format!("{e:?}"))?,
7877
attestation_2: new_attestation
7978
.clone()
8079
.to_electra()
8180
.map_err(|e| format!("{e:?}"))?,
81+
})),
82+
}
83+
}
84+
SurroundsExisting(existing) => match (&*existing, new_attestation) {
85+
(IndexedAttestation::Base(existing_att), IndexedAttestation::Base(new)) => {
86+
Some(AttesterSlashing::Base(AttesterSlashingBase {
87+
attestation_1: new.clone(),
88+
attestation_2: existing_att.clone(),
8289
}))
8390
}
91+
// A slashing involving an electra attestation type must return an `AttesterSlashingElectra` type
92+
(_, _) => Some(AttesterSlashing::Electra(AttesterSlashingElectra {
93+
attestation_1: new_attestation
94+
.clone()
95+
.to_electra()
96+
.map_err(|e| format!("{e:?}"))?,
97+
attestation_2: existing
98+
.clone()
99+
.to_electra()
100+
.map_err(|e| format!("{e:?}"))?,
101+
})),
84102
},
85-
SurroundsExisting(existing) => {
86-
match new_attestation {
87-
IndexedAttestation::Base(new_attestation) => {
88-
Some(AttesterSlashing::Base(AttesterSlashingBase {
89-
attestation_1: existing
90-
.as_base()
91-
.map_err(|e| format!("{e:?}"))?
92-
.clone(),
93-
attestation_2: new_attestation.clone(),
94-
}))
95-
}
96-
IndexedAttestation::Electra(new_attestation) => {
97-
Some(AttesterSlashing::Electra(AttesterSlashingElectra {
98-
attestation_1: existing.to_electra().map_err(|e| format!("{e:?}"))?,
99-
// A double vote should never convert, a surround vote where the surrounding
100-
// vote is electra may convert.
101-
attestation_2: new_attestation.clone(),
102-
}))
103-
}
104-
}
105-
}
106103
})
107104
}
108105
}

slasher/src/test_utils.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,18 @@ pub fn att_slashing(
6464
attestation_1: &IndexedAttestation<E>,
6565
attestation_2: &IndexedAttestation<E>,
6666
) -> AttesterSlashing<E> {
67-
// TODO(electra): fix this one we superstruct IndexedAttestation (return the correct type)
6867
match (attestation_1, attestation_2) {
6968
(IndexedAttestation::Base(att1), IndexedAttestation::Base(att2)) => {
7069
AttesterSlashing::Base(AttesterSlashingBase {
7170
attestation_1: att1.clone(),
7271
attestation_2: att2.clone(),
7372
})
7473
}
75-
(IndexedAttestation::Electra(att1), IndexedAttestation::Electra(att2)) => {
76-
AttesterSlashing::Electra(AttesterSlashingElectra {
77-
attestation_1: att1.clone(),
78-
attestation_2: att2.clone(),
79-
})
80-
}
81-
_ => panic!("attestations must be of the same type"),
74+
// A slashing involving an electra attestation type must return an electra AttesterSlashing type
75+
(_, _) => AttesterSlashing::Electra(AttesterSlashingElectra {
76+
attestation_1: attestation_1.clone().to_electra().unwrap(),
77+
attestation_2: attestation_2.clone().to_electra().unwrap(),
78+
}),
8279
}
8380
}
8481

slasher/tests/attester_slashings.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use rayon::prelude::*;
66
use slasher::{
77
config::DEFAULT_CHUNK_SIZE,
88
test_utils::{
9-
att_slashing, chain_spec, indexed_att, indexed_att_electra,
10-
slashed_validators_from_slashings, E,
9+
att_slashing, indexed_att, indexed_att_electra, slashed_validators_from_slashings, E,
1110
},
1211
Config, Slasher,
1312
};

0 commit comments

Comments
 (0)