lighthouse/eth2/state_processing/src/per_block_processing/tests.rs

114 lines
3.4 KiB
Rust
Raw Normal View History

#![cfg(all(test, not(feature = "fake_crypto")))]
2019-04-10 04:42:31 +00:00
use super::block_processing_builder::BlockProcessingBuilder;
2019-04-10 11:56:31 +00:00
use super::errors::*;
2019-04-15 00:38:13 +00:00
use crate::per_block_processing;
use tree_hash::SignedRoot;
use types::*;
2019-04-10 04:42:31 +00:00
pub const VALIDATOR_COUNT: usize = 10;
#[test]
2019-04-15 00:38:13 +00:00
fn valid_block_ok() {
let spec = MainnetEthSpec::default_spec();
2019-04-15 00:38:13 +00:00
let builder = get_builder(&spec);
let (block, mut state) = builder.build(None, None, &spec);
2019-04-10 11:56:31 +00:00
2019-04-15 00:38:13 +00:00
let result = per_block_processing(&mut state, &block, &spec);
assert_eq!(result, Ok(()));
2019-04-10 11:56:31 +00:00
}
#[test]
2019-04-15 00:38:13 +00:00
fn invalid_block_header_state_slot() {
let spec = MainnetEthSpec::default_spec();
2019-04-15 00:38:13 +00:00
let builder = get_builder(&spec);
let (mut block, mut state) = builder.build(None, None, &spec);
2019-04-10 11:56:31 +00:00
state.slot = Slot::new(133713);
block.slot = Slot::new(424242);
let result = per_block_processing(&mut state, &block, &spec);
2019-04-15 00:38:13 +00:00
assert_eq!(
result,
Err(BlockProcessingError::Invalid(
BlockInvalid::StateSlotMismatch
))
);
2019-04-10 11:56:31 +00:00
}
#[test]
2019-04-15 00:38:13 +00:00
fn invalid_parent_block_root() {
let spec = MainnetEthSpec::default_spec();
let builder = get_builder(&spec);
let invalid_parent_root = Hash256::from([0xAA; 32]);
let (block, mut state) = builder.build(None, Some(invalid_parent_root), &spec);
let result = per_block_processing(&mut state, &block, &spec);
assert_eq!(
result,
Err(BlockProcessingError::Invalid(
2019-05-20 06:47:44 +00:00
BlockInvalid::ParentBlockRootMismatch {
state: Hash256::from_slice(&state.latest_block_header.signed_root()),
block: block.previous_block_root
}
))
);
2019-04-10 11:56:31 +00:00
}
2019-04-15 00:38:13 +00:00
#[test]
fn invalid_block_signature() {
let spec = MainnetEthSpec::default_spec();
2019-04-15 00:38:13 +00:00
let builder = get_builder(&spec);
let (mut block, mut state) = builder.build(None, None, &spec);
2019-04-15 00:38:13 +00:00
// sign the block with a keypair that is not the expected proposer
let keypair = Keypair::random();
let message = block.signed_root();
let epoch = block.slot.epoch(MainnetEthSpec::slots_per_epoch());
let domain = spec.get_domain(epoch, Domain::BeaconProposer, &state.fork);
2019-04-15 00:38:13 +00:00
block.signature = Signature::new(&message, domain, &keypair.sk);
// process block with invalid block signature
let result = per_block_processing(&mut state, &block, &spec);
// should get a BadSignature error
assert_eq!(
result,
Err(BlockProcessingError::Invalid(BlockInvalid::BadSignature))
);
}
#[test]
fn invalid_randao_reveal_signature() {
let spec = MainnetEthSpec::default_spec();
let builder = get_builder(&spec);
// sign randao reveal with random keypair
let keypair = Keypair::random();
let (block, mut state) = builder.build(Some(keypair.sk), None, &spec);
2019-05-20 06:47:44 +00:00
let result = per_block_processing(&mut state, &block, &spec);
// should get a BadRandaoSignature error
assert_eq!(
result,
2019-05-20 06:47:44 +00:00
Err(BlockProcessingError::Invalid(
BlockInvalid::BadRandaoSignature
))
);
}
fn get_builder(spec: &ChainSpec) -> (BlockProcessingBuilder<MainnetEthSpec>) {
2019-04-10 04:42:31 +00:00
let mut builder = BlockProcessingBuilder::new(VALIDATOR_COUNT, &spec);
// Set the state and block to be in the last slot of the 4th epoch.
let last_slot_of_epoch =
(MainnetEthSpec::genesis_epoch() + 4).end_slot(MainnetEthSpec::slots_per_epoch());
builder.set_slot(last_slot_of_epoch);
2019-04-10 04:42:31 +00:00
builder.build_caches(&spec);
2019-04-15 00:38:13 +00:00
(builder)
2019-04-10 11:56:31 +00:00
}