diff --git a/eth2/state_processing/src/per_block_processing.rs b/eth2/state_processing/src/per_block_processing.rs index ac874d95e..331069d44 100644 --- a/eth2/state_processing/src/per_block_processing.rs +++ b/eth2/state_processing/src/per_block_processing.rs @@ -139,6 +139,8 @@ pub fn verify_block_signature( &state.fork, ); + println!("verify {:?}", &block); + verify!( block .signature diff --git a/eth2/state_processing/src/per_block_processing/block_processing_builder.rs b/eth2/state_processing/src/per_block_processing/block_processing_builder.rs index 489684523..fb9dbd153 100644 --- a/eth2/state_processing/src/per_block_processing/block_processing_builder.rs +++ b/eth2/state_processing/src/per_block_processing/block_processing_builder.rs @@ -30,7 +30,7 @@ impl BlockProcessingBuilder { self.state_builder.build_caches(&spec).unwrap(); } - pub fn build(mut self, spec: &ChainSpec) -> (BeaconBlock, BeaconState) { + pub fn build(mut self, randao_sk: Option, spec: &ChainSpec) -> (BeaconBlock, BeaconState) { let (state, keypairs) = self.state_builder.build(); let builder = &mut self.block_builder; @@ -41,10 +41,14 @@ impl BlockProcessingBuilder { .unwrap(); 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); (block, state) } + } diff --git a/eth2/state_processing/src/per_block_processing/tests.rs b/eth2/state_processing/src/per_block_processing/tests.rs index 6fe7e40d2..6ae7fd872 100644 --- a/eth2/state_processing/src/per_block_processing/tests.rs +++ b/eth2/state_processing/src/per_block_processing/tests.rs @@ -11,7 +11,7 @@ pub const VALIDATOR_COUNT: usize = 10; fn valid_block_ok() { let spec = ChainSpec::foundation(); 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); @@ -22,7 +22,7 @@ fn valid_block_ok() { fn invalid_block_header_state_slot() { let spec = ChainSpec::foundation(); 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); block.slot = Slot::new(424242); @@ -47,7 +47,7 @@ fn invalid_parent_block_root() { fn invalid_block_signature() { let spec = ChainSpec::foundation(); 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 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) { let mut builder = BlockProcessingBuilder::new(VALIDATOR_COUNT, &spec); diff --git a/eth2/types/src/test_utils/testing_beacon_block_builder.rs b/eth2/types/src/test_utils/testing_beacon_block_builder.rs index c5cd22ed4..c8c32d931 100644 --- a/eth2/types/src/test_utils/testing_beacon_block_builder.rs +++ b/eth2/types/src/test_utils/testing_beacon_block_builder.rs @@ -33,6 +33,7 @@ impl TestingBeaconBlockBuilder { /// Modifying the block after signing may invalidate the signature. pub fn sign(&mut self, sk: &SecretKey, fork: &Fork, spec: &ChainSpec) { let message = self.block.signed_root(); + println!("block set {:?}", self.block); let epoch = self.block.slot.epoch(spec.slots_per_epoch); let domain = spec.get_domain(epoch, Domain::BeaconBlock, fork); self.block.signature = Signature::new(&message, domain, sk); @@ -48,6 +49,11 @@ impl TestingBeaconBlockBuilder { 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. pub fn insert_proposer_slashing( &mut self,