Add ProposerSlashingBuilder

It is capable of producing double votes
This commit is contained in:
Paul Hauner 2019-03-02 18:36:44 +11:00
parent bb4d392a98
commit 8e1380d7c4
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
2 changed files with 52 additions and 0 deletions

View File

@ -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,

View File

@ -0,0 +1,48 @@
use crate::*;
use ssz::TreeHash;
pub struct ProposerSlashingBuilder();
impl ProposerSlashingBuilder {
pub fn double_vote<F>(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,
}
}
}