diff --git a/beacon_node/beacon_chain/test_harness/examples/chain.yaml b/beacon_node/beacon_chain/test_harness/examples/chain.yaml index 4c4946c5d..9e00f4569 100644 --- a/beacon_node/beacon_chain/test_harness/examples/chain.yaml +++ b/beacon_node/beacon_chain/test_harness/examples/chain.yaml @@ -20,19 +20,18 @@ test_cases: amount: 32 merkle_index: 2 proposer_slashings: - - slot: 8 # At slot 8, trigger a proposal slashing - proposer_index: 42 # Penalize the validator at position 42 - proposal_1_shard: 0 - proposal_1_slot: 15 - proposal_1_root: !!binary | - LkmqmqoodLKAslkjdkajsdljasdkajlksjdasldjasdd - proposal_2_shard: 0 - proposal_2_slot: 15 - proposal_2_root: !!binary | - DIFFERENTKAslkjdkajsdljasdkajlksjdasldjasdd + # At slot 2, trigger a proposer slashing for validator #42. + - slot: 2 + validator_index: 42 + # At slot 8, trigger a proposer slashing for validator #13. + - slot: 8 + validator_index: 13 attester_slashings: - # At slot 2, trigger an attester slashing for validators #11 and #12 + # At slot 2, trigger an attester slashing for validators #11 and #12. - slot: 2 validator_indices: [11, 12] + # At slot 5, trigger an attester slashing for validator #14. + - slot: 5 + validator_indices: [14] results: num_validators: 1000 diff --git a/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness.rs b/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness.rs index 862e037e2..9060c4ec1 100644 --- a/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness.rs +++ b/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness.rs @@ -273,32 +273,7 @@ impl BeaconChainHarness { } } - pub fn add_proposer_slashing(&mut self, mut proposer_slashing: ProposerSlashing) { - let validator = &self.validators[proposer_slashing.proposer_index as usize]; - - // This following code is a little awkward, but managing the data_1 and data_1 was getting - // rather confusing. I think this is better - let proposals = vec![ - &proposer_slashing.proposal_data_1, - &proposer_slashing.proposal_data_2, - ]; - let signatures: Vec = proposals - .iter() - .map(|proposal_data| { - let message = proposal_data.hash_tree_root(); - let epoch = proposal_data.slot.epoch(self.spec.epoch_length); - let domain = self - .beacon_chain - .state - .read() - .fork - .get_domain(epoch, self.spec.domain_proposal); - Signature::new(&message[..], domain, &validator.keypair.sk) - }) - .collect(); - proposer_slashing.proposal_signature_1 = signatures[0].clone(); - proposer_slashing.proposal_signature_2 = signatures[1].clone(); - + pub fn add_proposer_slashing(&mut self, proposer_slashing: ProposerSlashing) { self.beacon_chain .receive_proposer_slashing_for_inclusion(proposer_slashing); } diff --git a/beacon_node/beacon_chain/test_harness/src/bin.rs b/beacon_node/beacon_chain/test_harness/src/bin.rs index fa5aa45a2..7c525c768 100644 --- a/beacon_node/beacon_chain/test_harness/src/bin.rs +++ b/beacon_node/beacon_chain/test_harness/src/bin.rs @@ -7,8 +7,10 @@ use env_logger::{Builder, Env}; use log::{info, warn}; use ssz::TreeHash; use std::{fs::File, io::prelude::*}; -use types::attester_slashing::AttesterSlashingBuilder; use types::*; +use types::{ + attester_slashing::AttesterSlashingBuilder, proposer_slashing::ProposerSlashingBuilder, +}; use yaml_rust::{Yaml, YamlLoader}; mod beacon_chain_harness; @@ -99,13 +101,14 @@ impl Manifest { // Feed proposer slashings to the BeaconChain. if let Some(ref slashings) = self.config.proposer_slashings { - for (slot, slashing) in slashings { + for (slot, validator_index) in slashings { if *slot == slot_height { info!( - "Including proposer slashing at slot height {}.", - slot_height + "Including proposer slashing at slot height {} for validator #{}.", + slot_height, validator_index ); - harness.add_proposer_slashing(slashing.clone()); + let slashing = build_proposer_slashing(&harness, *validator_index); + harness.add_proposer_slashing(slashing); } } } @@ -115,8 +118,8 @@ impl Manifest { for (slot, validator_indices) in slashings { if *slot == slot_height { info!( - "Including attester slashing at slot height {}.", - slot_height + "Including attester slashing at slot height {} for validators {:?}.", + slot_height, validator_indices ); let slashing = build_double_vote_attester_slashing(&harness, &validator_indices[..]); @@ -205,8 +208,18 @@ fn build_double_vote_attester_slashing( AttesterSlashingBuilder::double_vote(validator_indices, signer, &harness.spec) } +fn build_proposer_slashing(harness: &BeaconChainHarness, validator_index: u64) -> ProposerSlashing { + let signer = |validator_index: u64, message: &[u8], epoch: Epoch, domain: u64| { + harness + .validator_sign(validator_index as usize, message, epoch, domain) + .expect("Unable to sign AttesterSlashing") + }; + + ProposerSlashingBuilder::double_vote(validator_index, signer, &harness.spec) +} + pub type DepositTuple = (u64, Deposit, Keypair); -pub type ProposerSlashingTuple = (u64, ProposerSlashing); +pub type ProposerSlashingTuple = (u64, u64); pub type AttesterSlashingTuple = (u64, Vec); struct ExecutionResult { @@ -272,40 +285,11 @@ fn parse_proposer_slashings(yaml: &Yaml) -> Option> { let mut slashings = vec![]; for slashing in yaml["proposer_slashings"].as_vec()? { - let slot = as_u64(slashing, "slot").expect("Incomplete slashing"); + let slot = as_u64(slashing, "slot").expect("Incomplete proposer slashing (slot)_"); + let validator_index = as_u64(slashing, "validator_index") + .expect("Incomplete proposer slashing (validator_index)"); - // Builds a ProposerSlashing object from YAML fields. - // - // Rustfmt make this look rather ugly, however it is just a simple struct - // instantiation. - let slashing = ProposerSlashing { - proposer_index: as_u64(slashing, "proposer_index") - .expect("Incomplete slashing (proposer_index)"), - proposal_data_1: ProposalSignedData { - slot: Slot::from( - as_u64(slashing, "proposal_1_slot") - .expect("Incomplete slashing (proposal_1_slot)."), - ), - shard: as_u64(slashing, "proposal_1_shard") - .expect("Incomplete slashing (proposal_1_shard)."), - block_root: as_hash256(slashing, "proposal_1_root") - .expect("Incomplete slashing (proposal_1_root)."), - }, - proposal_signature_1: Signature::empty_signature(), // Will be replaced with real signature at runtime. - proposal_data_2: ProposalSignedData { - slot: Slot::from( - as_u64(slashing, "proposal_2_slot") - .expect("Incomplete slashing (proposal_2_slot)."), - ), - shard: as_u64(slashing, "proposal_2_shard") - .expect("Incomplete slashing (proposal_2_shard)."), - block_root: as_hash256(slashing, "proposal_2_root") - .expect("Incomplete slashing (proposal_2_root)."), - }, - proposal_signature_2: Signature::empty_signature(), // Will be replaced with real signature at runtime. - }; - - slashings.push((slot, slashing)); + slashings.push((slot, validator_index)); } Some(slashings)