From 7434ff47badd69746b0ac9d6b2c3f5788fee29eb Mon Sep 17 00:00:00 2001 From: thojest Date: Thu, 14 Feb 2019 13:28:42 +0100 Subject: [PATCH 1/3] added is_double_vote and is_surround_vote (lighthouse-150) --- eth2/types/src/slashable_vote_data.rs | 91 +++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/eth2/types/src/slashable_vote_data.rs b/eth2/types/src/slashable_vote_data.rs index acffca26dc..31433a0b1b 100644 --- a/eth2/types/src/slashable_vote_data.rs +++ b/eth2/types/src/slashable_vote_data.rs @@ -1,4 +1,5 @@ use super::AttestationData; +use crate::spec::ChainSpec; use crate::test_utils::TestRandom; use bls::AggregateSignature; use rand::RngCore; @@ -13,6 +14,21 @@ pub struct SlashableVoteData { pub aggregate_signature: AggregateSignature, } +impl SlashableVoteData { + pub fn is_double_vote(&self, other: &SlashableVoteData, spec: &ChainSpec) -> bool { + self.data.slot.epoch(spec.epoch_length) == other.data.slot.epoch(spec.epoch_length) + } + + pub fn is_surround_vote(&self, other: &SlashableVoteData, 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) + } +} + impl Encodable for SlashableVoteData { fn ssz_append(&self, s: &mut SszStream) { s.append_vec(&self.custody_bit_0_indices); @@ -66,9 +82,71 @@ impl TestRandom for SlashableVoteData { #[cfg(test)] mod tests { use super::*; + use crate::slot_epoch_height::{Epoch, Slot}; + use crate::spec::ChainSpec; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use ssz::ssz_encode; + #[test] + pub fn test_is_double_vote_true() { + let spec = ChainSpec::foundation(); + let slashable_vote_first = create_slashable_vote_data(1, 1, &spec); + let slashable_vote_second = create_slashable_vote_data(1, 1, &spec); + + assert_eq!( + slashable_vote_first.is_double_vote(&slashable_vote_second, &spec), + true + ) + } + + #[test] + pub fn test_is_double_vote_false() { + let spec = ChainSpec::foundation(); + let slashable_vote_first = create_slashable_vote_data(1, 1, &spec); + let slashable_vote_second = create_slashable_vote_data(2, 1, &spec); + + assert_eq!( + slashable_vote_first.is_double_vote(&slashable_vote_second, &spec), + false + ); + } + + #[test] + pub fn test_is_surround_vote_true() { + let spec = ChainSpec::foundation(); + let slashable_vote_first = create_slashable_vote_data(2, 1, &spec); + let slashable_vote_second = create_slashable_vote_data(1, 2, &spec); + + assert_eq!( + slashable_vote_first.is_surround_vote(&slashable_vote_second, &spec), + true + ); + } + + #[test] + pub fn test_is_surround_vote_false_source_epoch_fails() { + let spec = ChainSpec::foundation(); + let slashable_vote_first = create_slashable_vote_data(2, 2, &spec); + let slashable_vote_second = create_slashable_vote_data(1, 1, &spec); + + assert_eq!( + slashable_vote_first.is_surround_vote(&slashable_vote_second, &spec), + false + ); + } + + #[test] + pub fn test_is_surround_vote_false_target_epoch_fails() { + let spec = ChainSpec::foundation(); + let slashable_vote_first = create_slashable_vote_data(1, 1, &spec); + let slashable_vote_second = create_slashable_vote_data(2, 2, &spec); + + assert_eq!( + slashable_vote_first.is_surround_vote(&slashable_vote_second, &spec), + false + ); + } + #[test] pub fn test_ssz_round_trip() { let mut rng = XorShiftRng::from_seed([42; 16]); @@ -91,4 +169,17 @@ mod tests { // TODO: Add further tests // https://github.com/sigp/lighthouse/issues/170 } + + fn create_slashable_vote_data( + slot_factor: u64, + justified_epoch: u64, + spec: &ChainSpec, + ) -> SlashableVoteData { + let mut rng = XorShiftRng::from_seed([42; 16]); + let mut slashable_vote = SlashableVoteData::random_for_test(&mut rng); + + slashable_vote.data.slot = Slot::new(slot_factor * spec.epoch_length); + slashable_vote.data.justified_epoch = Epoch::new(justified_epoch); + slashable_vote + } } From 203f3b37f2b9672d4746a771f93ac6f2c3d04288 Mon Sep 17 00:00:00 2001 From: thojest Date: Fri, 15 Feb 2019 12:25:59 +0100 Subject: [PATCH 2/3] adapted import due to renaming of crate slot_epoch_height -> slot_epoch (lighthouse-150) --- eth2/types/src/slashable_vote_data.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eth2/types/src/slashable_vote_data.rs b/eth2/types/src/slashable_vote_data.rs index 31433a0b1b..170cd635e3 100644 --- a/eth2/types/src/slashable_vote_data.rs +++ b/eth2/types/src/slashable_vote_data.rs @@ -82,7 +82,7 @@ impl TestRandom for SlashableVoteData { #[cfg(test)] mod tests { use super::*; - use crate::slot_epoch_height::{Epoch, Slot}; + use crate::slot_epoch::{Epoch, Slot}; use crate::spec::ChainSpec; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use ssz::ssz_encode; From f88155625c709efd6dc42c7faae537e6bff486c6 Mon Sep 17 00:00:00 2001 From: thojest Date: Mon, 18 Feb 2019 12:12:01 +0100 Subject: [PATCH 3/3] added comment to indicate highest spec version of implemented functions; added realistic test scenario for is_surround_vote (lighthouse-150) --- eth2/types/src/slashable_vote_data.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/eth2/types/src/slashable_vote_data.rs b/eth2/types/src/slashable_vote_data.rs index 170cd635e3..ff9e8b658a 100644 --- a/eth2/types/src/slashable_vote_data.rs +++ b/eth2/types/src/slashable_vote_data.rs @@ -1,5 +1,5 @@ use super::AttestationData; -use crate::spec::ChainSpec; +use crate::chain_spec::ChainSpec; use crate::test_utils::TestRandom; use bls::AggregateSignature; use rand::RngCore; @@ -15,10 +15,16 @@ pub struct SlashableVoteData { } impl SlashableVoteData { + /// Check if ``attestation_data_1`` and ``attestation_data_2`` have the same target. + /// + /// Spec v0.3.0 pub fn is_double_vote(&self, other: &SlashableVoteData, 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: &SlashableVoteData, spec: &ChainSpec) -> bool { let source_epoch_1 = self.data.justified_epoch; let source_epoch_2 = other.data.justified_epoch; @@ -82,8 +88,8 @@ impl TestRandom for SlashableVoteData { #[cfg(test)] mod tests { use super::*; + use crate::chain_spec::ChainSpec; use crate::slot_epoch::{Epoch, Slot}; - use crate::spec::ChainSpec; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use ssz::ssz_encode; @@ -123,6 +129,18 @@ mod tests { ); } + #[test] + pub fn test_is_surround_vote_true_realistic() { + let spec = ChainSpec::foundation(); + let slashable_vote_first = create_slashable_vote_data(4, 1, &spec); + let slashable_vote_second = create_slashable_vote_data(3, 2, &spec); + + assert_eq!( + slashable_vote_first.is_surround_vote(&slashable_vote_second, &spec), + true + ); + } + #[test] pub fn test_is_surround_vote_false_source_epoch_fails() { let spec = ChainSpec::foundation();