From f3a3cfcc45357cd691b9bbb663482056704ebf40 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sat, 2 Mar 2019 15:33:52 +1100 Subject: [PATCH] Add surround/dbl vote fns to SlashableAttestation Copied from `SlashableVoteData` --- eth2/types/src/attester_slashing.rs | 2 +- eth2/types/src/slashable_attestation.rs | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/eth2/types/src/attester_slashing.rs b/eth2/types/src/attester_slashing.rs index 2a1df9e0cc..9cb2123249 100644 --- a/eth2/types/src/attester_slashing.rs +++ b/eth2/types/src/attester_slashing.rs @@ -1,4 +1,4 @@ -use crate::{test_utils::TestRandom, SlashableAttestation}; +use crate::{test_utils::TestRandom, ChainSpec, SlashableAttestation}; use rand::RngCore; use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; diff --git a/eth2/types/src/slashable_attestation.rs b/eth2/types/src/slashable_attestation.rs index c4a12338a5..d24c5dde42 100644 --- a/eth2/types/src/slashable_attestation.rs +++ b/eth2/types/src/slashable_attestation.rs @@ -1,4 +1,4 @@ -use crate::{test_utils::TestRandom, AggregateSignature, AttestationData, Bitfield}; +use crate::{test_utils::TestRandom, AggregateSignature, AttestationData, Bitfield, ChainSpec}; use rand::RngCore; use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; @@ -12,6 +12,27 @@ pub struct SlashableAttestation { pub aggregate_signature: AggregateSignature, } +impl SlashableAttestation { + /// Check if ``attestation_data_1`` and ``attestation_data_2`` have the same target. + /// + /// Spec v0.3.0 + pub fn is_double_vote(&self, other: &SlashableAttestation, spec: &ChainSpec) -> bool { + self.data.slot.epoch(spec.epoch_length) == other.data.slot.epoch(spec.epoch_length) + } + + /// Check if ``attestation_data_1`` surrounds ``attestation_data_2``. + /// + /// Spec v0.3.0 + pub fn is_surround_vote(&self, other: &SlashableAttestation, spec: &ChainSpec) -> bool { + let source_epoch_1 = self.data.justified_epoch; + let source_epoch_2 = other.data.justified_epoch; + let target_epoch_1 = self.data.slot.epoch(spec.epoch_length); + let target_epoch_2 = other.data.slot.epoch(spec.epoch_length); + + (source_epoch_1 < source_epoch_2) && (target_epoch_2 < target_epoch_1) + } +} + #[cfg(test)] mod tests { use super::*;