From 4a57aec4723f7ddf9a8f8afad6fcaf70b4c31318 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 4 Mar 2019 14:24:29 +1100 Subject: [PATCH] Unfinished progress --- eth2/types/src/lib.rs | 2 ++ eth2/types/src/proposal.rs | 46 +++++++++++++++++++++++++++++ eth2/types/src/proposer_slashing.rs | 12 ++++---- 3 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 eth2/types/src/proposal.rs diff --git a/eth2/types/src/lib.rs b/eth2/types/src/lib.rs index 4f196b9e9..ff73a6ea6 100644 --- a/eth2/types/src/lib.rs +++ b/eth2/types/src/lib.rs @@ -19,6 +19,7 @@ pub mod exit; pub mod fork; pub mod free_attestation; pub mod pending_attestation; +pub mod proposal; pub mod proposal_signed_data; pub mod proposer_slashing; pub mod readers; @@ -57,6 +58,7 @@ pub use crate::exit::Exit; pub use crate::fork::Fork; pub use crate::free_attestation::FreeAttestation; pub use crate::pending_attestation::PendingAttestation; +pub use crate::proposal::Proposal; pub use crate::proposal_signed_data::ProposalSignedData; pub use crate::proposer_slashing::ProposerSlashing; pub use crate::slashable_attestation::SlashableAttestation; diff --git a/eth2/types/src/proposal.rs b/eth2/types/src/proposal.rs new file mode 100644 index 000000000..ac92dbce8 --- /dev/null +++ b/eth2/types/src/proposal.rs @@ -0,0 +1,46 @@ +use crate::test_utils::TestRandom; +use crate::{Hash256, Slot}; +use bls::Signature; +use rand::RngCore; +use serde_derive::Serialize; +use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; + +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] +pub struct Proposal { + pub slot: Slot, + /// Shard number (spec.beacon_chain_shard_number for beacon chain) + pub shard: u64, + pub block_root: Hash256, + pub signature: Signature, +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; + use ssz::{ssz_encode, Decodable, TreeHash}; + + #[test] + pub fn test_ssz_round_trip() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = Proposal::random_for_test(&mut rng); + + let bytes = ssz_encode(&original); + let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap(); + + assert_eq!(original, decoded); + } + + #[test] + pub fn test_hash_tree_root_internal() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = Proposal::random_for_test(&mut rng); + + let result = original.hash_tree_root_internal(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } +} diff --git a/eth2/types/src/proposer_slashing.rs b/eth2/types/src/proposer_slashing.rs index ea30d46ec..3b6f57c40 100644 --- a/eth2/types/src/proposer_slashing.rs +++ b/eth2/types/src/proposer_slashing.rs @@ -1,6 +1,5 @@ -use super::ProposalSignedData; +use super::Proposal; use crate::test_utils::TestRandom; -use bls::Signature; use rand::RngCore; use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; @@ -10,13 +9,14 @@ mod builder; pub use builder::ProposerSlashingBuilder; +/// Two conflicting proposals from the same proposer (validator). +/// +/// Spec v0.4.0 #[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct ProposerSlashing { pub proposer_index: u64, - pub proposal_data_1: ProposalSignedData, - pub proposal_signature_1: Signature, - pub proposal_data_2: ProposalSignedData, - pub proposal_signature_2: Signature, + pub proposer_1: Proposal, + pub proposer_2: Proposal, } #[cfg(test)]