From 8e1380d7c4ad9afe10db13735ce52c2d6ab380cd Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sat, 2 Mar 2019 18:36:44 +1100 Subject: [PATCH] Add ProposerSlashingBuilder It is capable of producing double votes --- eth2/types/src/proposer_slashing.rs | 4 ++ eth2/types/src/proposer_slashing/builder.rs | 48 +++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 eth2/types/src/proposer_slashing/builder.rs diff --git a/eth2/types/src/proposer_slashing.rs b/eth2/types/src/proposer_slashing.rs index 610017c0c..ea30d46ec 100644 --- a/eth2/types/src/proposer_slashing.rs +++ b/eth2/types/src/proposer_slashing.rs @@ -6,6 +6,10 @@ use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; use test_random_derive::TestRandom; +mod builder; + +pub use builder::ProposerSlashingBuilder; + #[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct ProposerSlashing { pub proposer_index: u64, diff --git a/eth2/types/src/proposer_slashing/builder.rs b/eth2/types/src/proposer_slashing/builder.rs new file mode 100644 index 000000000..a43a73ff0 --- /dev/null +++ b/eth2/types/src/proposer_slashing/builder.rs @@ -0,0 +1,48 @@ +use crate::*; +use ssz::TreeHash; + +pub struct ProposerSlashingBuilder(); + +impl ProposerSlashingBuilder { + pub fn double_vote(proposer_index: u64, signer: F, spec: &ChainSpec) -> ProposerSlashing + where + F: Fn(u64, &[u8], Epoch, u64) -> Signature, + { + let slot = Slot::new(0); + let shard = 0; + + let proposal_data_1 = ProposalSignedData { + slot, + shard, + block_root: Hash256::from("one".as_bytes()), + }; + + let proposal_data_2 = ProposalSignedData { + slot, + shard, + block_root: Hash256::from("two".as_bytes()), + }; + + let proposal_signature_1 = { + let message = proposal_data_1.hash_tree_root(); + let epoch = slot.epoch(spec.epoch_length); + let domain = spec.domain_proposal; + signer(proposer_index, &message[..], epoch, domain) + }; + + let proposal_signature_2 = { + let message = proposal_data_2.hash_tree_root(); + let epoch = slot.epoch(spec.epoch_length); + let domain = spec.domain_proposal; + signer(proposer_index, &message[..], epoch, domain) + }; + + ProposerSlashing { + proposer_index, + proposal_data_1, + proposal_signature_1, + proposal_data_2, + proposal_signature_2, + } + } +}