invalid randao signature test implemented

This commit is contained in:
Darren Langley 2019-04-18 07:00:40 +10:00
parent d76246e600
commit 010d319fdf
4 changed files with 35 additions and 5 deletions

View File

@ -139,6 +139,8 @@ pub fn verify_block_signature(
&state.fork, &state.fork,
); );
println!("verify {:?}", &block);
verify!( verify!(
block block
.signature .signature

View File

@ -30,7 +30,7 @@ impl BlockProcessingBuilder {
self.state_builder.build_caches(&spec).unwrap(); self.state_builder.build_caches(&spec).unwrap();
} }
pub fn build(mut self, spec: &ChainSpec) -> (BeaconBlock, BeaconState) { pub fn build(mut self, randao_sk: Option<SecretKey>, spec: &ChainSpec) -> (BeaconBlock, BeaconState) {
let (state, keypairs) = self.state_builder.build(); let (state, keypairs) = self.state_builder.build();
let builder = &mut self.block_builder; let builder = &mut self.block_builder;
@ -41,10 +41,14 @@ impl BlockProcessingBuilder {
.unwrap(); .unwrap();
let keypair = &keypairs[proposer_index]; let keypair = &keypairs[proposer_index];
builder.set_randao_reveal(&keypair.sk, &state.fork, spec); match randao_sk {
Some(sk) => builder.set_randao_reveal(&sk, &state.fork, spec),
None => builder.set_randao_reveal(&keypair.sk, &state.fork, spec),
}
let block = self.block_builder.build(&keypair.sk, &state.fork, spec); let block = self.block_builder.build(&keypair.sk, &state.fork, spec);
(block, state) (block, state)
} }
} }

View File

@ -11,7 +11,7 @@ pub const VALIDATOR_COUNT: usize = 10;
fn valid_block_ok() { fn valid_block_ok() {
let spec = ChainSpec::foundation(); let spec = ChainSpec::foundation();
let builder = get_builder(&spec); let builder = get_builder(&spec);
let (block, mut state) = builder.build(&spec); let (block, mut state) = builder.build(None, &spec);
let result = per_block_processing(&mut state, &block, &spec); let result = per_block_processing(&mut state, &block, &spec);
@ -22,7 +22,7 @@ fn valid_block_ok() {
fn invalid_block_header_state_slot() { fn invalid_block_header_state_slot() {
let spec = ChainSpec::foundation(); let spec = ChainSpec::foundation();
let builder = get_builder(&spec); let builder = get_builder(&spec);
let (mut block, mut state) = builder.build(&spec); let (mut block, mut state) = builder.build(None, &spec);
state.slot = Slot::new(133713); state.slot = Slot::new(133713);
block.slot = Slot::new(424242); block.slot = Slot::new(424242);
@ -47,7 +47,7 @@ fn invalid_parent_block_root() {
fn invalid_block_signature() { fn invalid_block_signature() {
let spec = ChainSpec::foundation(); let spec = ChainSpec::foundation();
let builder = get_builder(&spec); let builder = get_builder(&spec);
let (mut block, mut state) = builder.build(&spec); let (mut block, mut state) = builder.build(None, &spec);
// sign the block with a keypair that is not the expected proposer // sign the block with a keypair that is not the expected proposer
let keypair = Keypair::random(); let keypair = Keypair::random();
@ -66,6 +66,24 @@ fn invalid_block_signature() {
); );
} }
#[test]
fn invalid_randao_reveal_signature() {
let spec = ChainSpec::foundation();
let builder = get_builder(&spec);
// sign randao reveal with random keypair
let keypair = Keypair::random();
let (block, mut state) = builder.build(Some(keypair.sk), &spec);
let result = per_block_processing(&mut state, &block, &spec);
// should get a BadRandaoSignature error
assert_eq!(
result,
Err(BlockProcessingError::Invalid(BlockInvalid::BadRandaoSignature))
);
}
fn get_builder(spec: &ChainSpec) -> (BlockProcessingBuilder) { fn get_builder(spec: &ChainSpec) -> (BlockProcessingBuilder) {
let mut builder = BlockProcessingBuilder::new(VALIDATOR_COUNT, &spec); let mut builder = BlockProcessingBuilder::new(VALIDATOR_COUNT, &spec);

View File

@ -33,6 +33,7 @@ impl TestingBeaconBlockBuilder {
/// Modifying the block after signing may invalidate the signature. /// Modifying the block after signing may invalidate the signature.
pub fn sign(&mut self, sk: &SecretKey, fork: &Fork, spec: &ChainSpec) { pub fn sign(&mut self, sk: &SecretKey, fork: &Fork, spec: &ChainSpec) {
let message = self.block.signed_root(); let message = self.block.signed_root();
println!("block set {:?}", self.block);
let epoch = self.block.slot.epoch(spec.slots_per_epoch); let epoch = self.block.slot.epoch(spec.slots_per_epoch);
let domain = spec.get_domain(epoch, Domain::BeaconBlock, fork); let domain = spec.get_domain(epoch, Domain::BeaconBlock, fork);
self.block.signature = Signature::new(&message, domain, sk); self.block.signature = Signature::new(&message, domain, sk);
@ -48,6 +49,11 @@ impl TestingBeaconBlockBuilder {
self.block.body.randao_reveal = Signature::new(&message, domain, sk); self.block.body.randao_reveal = Signature::new(&message, domain, sk);
} }
/// Has the randao reveal been set?
pub fn randao_reveal_not_set(&mut self) -> bool {
self.block.body.randao_reveal.is_empty()
}
/// Inserts a signed, valid `ProposerSlashing` for the validator. /// Inserts a signed, valid `ProposerSlashing` for the validator.
pub fn insert_proposer_slashing( pub fn insert_proposer_slashing(
&mut self, &mut self,