2019-05-16 13:06:41 +00:00
|
|
|
#![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;
|
2019-05-16 13:06:41 +00:00
|
|
|
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() {
|
2019-06-08 12:49:04 +00:00
|
|
|
let spec = MainnetEthSpec::default_spec();
|
2019-04-15 00:38:13 +00:00
|
|
|
let builder = get_builder(&spec);
|
2019-05-16 13:06:41 +00:00
|
|
|
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() {
|
2019-06-08 12:49:04 +00:00
|
|
|
let spec = MainnetEthSpec::default_spec();
|
2019-04-15 00:38:13 +00:00
|
|
|
let builder = get_builder(&spec);
|
2019-05-16 13:06:41 +00:00
|
|
|
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() {
|
2019-06-08 12:49:04 +00:00
|
|
|
let spec = MainnetEthSpec::default_spec();
|
2019-05-16 13:06:41 +00:00
|
|
|
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()),
|
2019-05-16 13:06:41 +00:00
|
|
|
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() {
|
2019-06-08 12:49:04 +00:00
|
|
|
let spec = MainnetEthSpec::default_spec();
|
2019-04-15 00:38:13 +00:00
|
|
|
let builder = get_builder(&spec);
|
2019-05-16 13:06:41 +00:00
|
|
|
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();
|
2019-06-08 12:49:04 +00:00
|
|
|
let epoch = block.slot.epoch(MainnetEthSpec::slots_per_epoch());
|
2019-06-03 06:13:51 +00:00
|
|
|
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))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-17 21:00:40 +00:00
|
|
|
#[test]
|
|
|
|
fn invalid_randao_reveal_signature() {
|
2019-06-08 12:49:04 +00:00
|
|
|
let spec = MainnetEthSpec::default_spec();
|
2019-04-17 21:00:40 +00:00
|
|
|
let builder = get_builder(&spec);
|
|
|
|
|
|
|
|
// sign randao reveal with random keypair
|
|
|
|
let keypair = Keypair::random();
|
2019-05-16 13:06:41 +00:00
|
|
|
let (block, mut state) = builder.build(Some(keypair.sk), None, &spec);
|
2019-05-20 06:47:44 +00:00
|
|
|
|
2019-04-17 21:00:40 +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
|
|
|
|
))
|
2019-04-17 21:00:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-08 12:49:04 +00:00
|
|
|
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.
|
2019-06-08 11:57:25 +00:00
|
|
|
let last_slot_of_epoch =
|
2019-06-08 12:49:04 +00:00
|
|
|
(MainnetEthSpec::genesis_epoch() + 4).end_slot(MainnetEthSpec::slots_per_epoch());
|
2019-06-09 10:26:34 +00:00
|
|
|
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
|
|
|
}
|