2019-08-30 05:49:06 +00:00
|
|
|
#![cfg(not(feature = "fake_crypto"))]
|
|
|
|
|
2019-08-29 01:34:25 +00:00
|
|
|
use state_processing::{
|
|
|
|
per_block_processing, test_utils::BlockBuilder, BlockProcessingError, BlockSignatureStrategy,
|
|
|
|
};
|
|
|
|
use types::{
|
2020-02-10 23:19:36 +00:00
|
|
|
AggregateSignature, BeaconState, ChainSpec, EthSpec, Keypair, MinimalEthSpec, Signature,
|
|
|
|
SignedBeaconBlock, Slot,
|
2019-08-29 01:34:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const VALIDATOR_COUNT: usize = 64;
|
|
|
|
|
2020-02-10 23:19:36 +00:00
|
|
|
fn get_block<T, F>(mut mutate_builder: F) -> (SignedBeaconBlock<T>, BeaconState<T>)
|
2019-08-29 01:34:25 +00:00
|
|
|
where
|
|
|
|
T: EthSpec,
|
|
|
|
F: FnMut(&mut BlockBuilder<T>),
|
|
|
|
{
|
|
|
|
let spec = T::default_spec();
|
|
|
|
let mut builder: BlockBuilder<T> = BlockBuilder::new(VALIDATOR_COUNT, &spec);
|
|
|
|
builder.set_slot(Slot::from(T::slots_per_epoch() * 3 - 2));
|
|
|
|
builder.build_caches(&spec);
|
|
|
|
mutate_builder(&mut builder);
|
|
|
|
builder.build(&spec)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_scenario<T: EthSpec, F, G>(mutate_builder: F, mut invalidate_block: G, spec: &ChainSpec)
|
|
|
|
where
|
|
|
|
T: EthSpec,
|
|
|
|
F: FnMut(&mut BlockBuilder<T>),
|
2020-02-10 23:19:36 +00:00
|
|
|
G: FnMut(&mut SignedBeaconBlock<T>),
|
2019-08-29 01:34:25 +00:00
|
|
|
{
|
2020-01-21 07:38:56 +00:00
|
|
|
let (mut block, mut state) = get_block::<T, _>(mutate_builder);
|
2019-08-29 01:34:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Control check to ensure the valid block should pass verification.
|
|
|
|
*/
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
per_block_processing(
|
|
|
|
&mut state.clone(),
|
|
|
|
&block,
|
|
|
|
None,
|
|
|
|
BlockSignatureStrategy::VerifyIndividual,
|
|
|
|
spec
|
|
|
|
),
|
|
|
|
Ok(()),
|
|
|
|
"valid block should pass with verify individual"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
per_block_processing(
|
|
|
|
&mut state.clone(),
|
|
|
|
&block,
|
|
|
|
None,
|
|
|
|
BlockSignatureStrategy::VerifyBulk,
|
|
|
|
spec
|
|
|
|
),
|
|
|
|
Ok(()),
|
|
|
|
"valid block should pass with verify bulk"
|
|
|
|
);
|
|
|
|
|
|
|
|
invalidate_block(&mut block);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to ensure the invalid block fails.
|
|
|
|
*/
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
per_block_processing(
|
|
|
|
&mut state.clone(),
|
|
|
|
&block,
|
|
|
|
None,
|
|
|
|
BlockSignatureStrategy::VerifyIndividual,
|
|
|
|
spec
|
|
|
|
)
|
|
|
|
.is_err(),
|
|
|
|
"invalid block should fail with verify individual"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
per_block_processing(
|
2020-01-21 07:38:56 +00:00
|
|
|
&mut state,
|
2019-08-29 01:34:25 +00:00
|
|
|
&block,
|
|
|
|
None,
|
|
|
|
BlockSignatureStrategy::VerifyBulk,
|
|
|
|
spec
|
|
|
|
),
|
|
|
|
Err(BlockProcessingError::BulkSignatureVerificationFailed),
|
|
|
|
"invalid block should fail with verify bulk"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: use lazy static
|
|
|
|
fn agg_sig() -> AggregateSignature {
|
|
|
|
let mut agg_sig = AggregateSignature::new();
|
|
|
|
agg_sig.add(&sig());
|
|
|
|
agg_sig
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: use lazy static
|
|
|
|
fn sig() -> Signature {
|
|
|
|
let keypair = Keypair::random();
|
2020-02-10 23:19:36 +00:00
|
|
|
Signature::new(&[42, 42], &keypair.sk)
|
2019-08-29 01:34:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TestEthSpec = MinimalEthSpec;
|
|
|
|
|
|
|
|
mod signatures_minimal {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn block_proposal() {
|
|
|
|
let spec = &TestEthSpec::default_spec();
|
|
|
|
|
|
|
|
test_scenario::<TestEthSpec, _, _>(|_| {}, |block| block.signature = sig(), spec);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn randao() {
|
|
|
|
let spec = &TestEthSpec::default_spec();
|
|
|
|
|
2020-02-10 23:19:36 +00:00
|
|
|
test_scenario::<TestEthSpec, _, _>(
|
|
|
|
|_| {},
|
|
|
|
|block| block.message.body.randao_reveal = sig(),
|
|
|
|
spec,
|
|
|
|
);
|
2019-08-29 01:34:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn proposer_slashing() {
|
|
|
|
let spec = &TestEthSpec::default_spec();
|
|
|
|
|
|
|
|
test_scenario::<TestEthSpec, _, _>(
|
|
|
|
|mut builder| {
|
|
|
|
builder.num_proposer_slashings = 1;
|
|
|
|
},
|
2020-02-10 23:19:36 +00:00
|
|
|
|block| {
|
|
|
|
block.message.body.proposer_slashings[0]
|
|
|
|
.signed_header_1
|
|
|
|
.signature = sig()
|
|
|
|
},
|
2019-08-29 01:34:25 +00:00
|
|
|
spec,
|
|
|
|
);
|
|
|
|
test_scenario::<TestEthSpec, _, _>(
|
|
|
|
|mut builder| {
|
|
|
|
builder.num_proposer_slashings = 1;
|
|
|
|
},
|
2020-02-10 23:19:36 +00:00
|
|
|
|block| {
|
|
|
|
block.message.body.proposer_slashings[0]
|
|
|
|
.signed_header_2
|
|
|
|
.signature = sig()
|
|
|
|
},
|
2019-08-29 01:34:25 +00:00
|
|
|
spec,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn attester_slashing() {
|
|
|
|
let spec = &TestEthSpec::default_spec();
|
|
|
|
|
|
|
|
test_scenario::<TestEthSpec, _, _>(
|
|
|
|
|mut builder| {
|
|
|
|
builder.num_attester_slashings = 1;
|
|
|
|
},
|
2020-02-10 23:19:36 +00:00
|
|
|
|block| {
|
|
|
|
block.message.body.attester_slashings[0]
|
|
|
|
.attestation_1
|
|
|
|
.signature = agg_sig()
|
|
|
|
},
|
2019-08-29 01:34:25 +00:00
|
|
|
spec,
|
|
|
|
);
|
|
|
|
test_scenario::<TestEthSpec, _, _>(
|
|
|
|
|mut builder| {
|
|
|
|
builder.num_attester_slashings = 1;
|
|
|
|
},
|
2020-02-10 23:19:36 +00:00
|
|
|
|block| {
|
|
|
|
block.message.body.attester_slashings[0]
|
|
|
|
.attestation_2
|
|
|
|
.signature = agg_sig()
|
|
|
|
},
|
2019-08-29 01:34:25 +00:00
|
|
|
spec,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn attestation() {
|
|
|
|
let spec = &TestEthSpec::default_spec();
|
|
|
|
|
|
|
|
test_scenario::<TestEthSpec, _, _>(
|
|
|
|
|mut builder| {
|
|
|
|
builder.num_attestations = 1;
|
|
|
|
},
|
2020-02-10 23:19:36 +00:00
|
|
|
|block| block.message.body.attestations[0].signature = agg_sig(),
|
2019-08-29 01:34:25 +00:00
|
|
|
spec,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
// TODO: fix fail by making valid merkle proofs.
|
|
|
|
#[should_panic]
|
|
|
|
fn deposit() {
|
|
|
|
let spec = &TestEthSpec::default_spec();
|
|
|
|
|
|
|
|
test_scenario::<TestEthSpec, _, _>(
|
|
|
|
|mut builder| {
|
|
|
|
builder.num_deposits = 1;
|
|
|
|
},
|
2020-02-10 23:19:36 +00:00
|
|
|
|block| block.message.body.deposits[0].data.signature = sig().into(),
|
2019-08-29 01:34:25 +00:00
|
|
|
spec,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn exit() {
|
|
|
|
let mut spec = &mut TestEthSpec::default_spec();
|
|
|
|
|
|
|
|
// Allows the test to pass.
|
|
|
|
spec.persistent_committee_period = 0;
|
|
|
|
|
|
|
|
test_scenario::<TestEthSpec, _, _>(
|
|
|
|
|mut builder| {
|
|
|
|
builder.num_exits = 1;
|
|
|
|
},
|
2020-02-10 23:19:36 +00:00
|
|
|
|block| block.message.body.voluntary_exits[0].signature = sig(),
|
2019-08-29 01:34:25 +00:00
|
|
|
spec,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|