From 8e094b358f9cfcc71aa983b4477c251b06340991 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 1 Oct 2018 17:19:08 +0930 Subject: [PATCH] Implement framework for testing attestation val. --- lighthouse/state/block/validation/benches.rs | 146 ------------------ .../block/validation/block_validation.rs | 5 + tests/attestation_validation/helpers.rs | 62 ++++---- tests/attestation_validation/tests.rs | 7 +- tests/block_validation/helpers.rs | 2 +- 5 files changed, 45 insertions(+), 177 deletions(-) delete mode 100644 lighthouse/state/block/validation/benches.rs diff --git a/lighthouse/state/block/validation/benches.rs b/lighthouse/state/block/validation/benches.rs deleted file mode 100644 index 538ade41fa..0000000000 --- a/lighthouse/state/block/validation/benches.rs +++ /dev/null @@ -1,146 +0,0 @@ -extern crate test; - -use self::test::Bencher; - -use std::sync::Arc; - -use super::{ - BlockValidationContext, - AttesterMap, - ProposerMap, -}; - -use super::tests::{ - TestStore, - TestParams, - setup_block_validation_scenario, - serialize_block, -}; - -use super::super::{ - Block, - SszBlock, -}; - -fn bench_block_validation_scenario( - b: &mut Bencher, - params: &TestParams, - mutator_func: F) - where F: FnOnce(Block, AttesterMap, ProposerMap, TestStore) - -> (Block, AttesterMap, ProposerMap, TestStore) -{ - let (block, - parent_hashes, - attester_map, - proposer_map, - stores) = setup_block_validation_scenario(¶ms); - - let (block, - attester_map, - proposer_map, - stores) = mutator_func(block, attester_map, proposer_map, stores); - - let ssz_bytes = serialize_block(&block); - let ssz_block = SszBlock::from_slice(&ssz_bytes[..]) - .unwrap(); - - let parent_hashes = Arc::new(parent_hashes); - let proposer_map = Arc::new(proposer_map); - let attester_map = Arc::new(attester_map); - b.iter(|| { - let context = BlockValidationContext { - present_slot: params.validation_context_slot, - cycle_length: params.cycle_length, - last_justified_slot: params.validation_context_justified_slot, - last_finalized_slot: params.validation_context_finalized_slot, - parent_hashes: parent_hashes.clone(), - proposer_map: proposer_map.clone(), - attester_map: attester_map.clone(), - block_store: stores.block.clone(), - validator_store: stores.validator.clone(), - pow_store: stores.pow_chain.clone() - }; - let result = context.validate_ssz_block(&ssz_block); - assert!(result.is_ok()); - }); -} - -#[bench] -#[ignore] -fn bench_block_validation_10m_eth(b: &mut Bencher) { - let total_validators: usize = 10_000_000 / 32; - let cycle_length: u8 = 64; - let shard_count: u16 = 1024; - let shards_per_slot: u16 = 1024 / u16::from(cycle_length); - let validators_per_shard: usize = total_validators / usize::from(shard_count); - let block_slot = u64::from(cycle_length) * 10000; - let attestations_justified_slot = block_slot - u64::from(cycle_length); - let parent_proposer_index = 0; - - let validation_context_slot = block_slot; - let validation_context_justified_slot = attestations_justified_slot; - let validation_context_finalized_slot = 0; - - let params = TestParams { - total_validators, - cycle_length, - shard_count, - shards_per_slot, - validators_per_shard, - parent_proposer_index, - block_slot, - attestations_justified_slot, - validation_context_slot, - validation_context_justified_slot, - validation_context_finalized_slot, - }; - - let no_mutate = |block, attester_map, proposer_map, stores| { - (block, attester_map, proposer_map, stores) - }; - - bench_block_validation_scenario( - b, - ¶ms, - no_mutate); -} - -#[bench] -#[ignore] -fn bench_block_validation_100m_eth(b: &mut Bencher) { - let total_validators: usize = 100_000_000 / 32; - let cycle_length: u8 = 64; - let shard_count: u16 = 1024; - let shards_per_slot: u16 = 1024 / u16::from(cycle_length); - let validators_per_shard: usize = total_validators / usize::from(shard_count); - let block_slot = u64::from(cycle_length) * 10000; - let attestations_justified_slot = block_slot - u64::from(cycle_length); - let parent_proposer_index = 0; - - let validation_context_slot = block_slot; - let validation_context_justified_slot = attestations_justified_slot; - let validation_context_finalized_slot = 0; - - let params = TestParams { - total_validators, - cycle_length, - shard_count, - shards_per_slot, - validators_per_shard, - parent_proposer_index, - block_slot, - attestations_justified_slot, - validation_context_slot, - validation_context_justified_slot, - validation_context_finalized_slot, - }; - - let no_mutate = |block, attester_map, proposer_map, stores| { - (block, attester_map, proposer_map, stores) - }; - - bench_block_validation_scenario( - b, - ¶ms, - no_mutate); -} diff --git a/lighthouse/state/block/validation/block_validation.rs b/lighthouse/state/block/validation/block_validation.rs index 11ea54e4d9..555a4228b2 100644 --- a/lighthouse/state/block/validation/block_validation.rs +++ b/lighthouse/state/block/validation/block_validation.rs @@ -379,3 +379,8 @@ impl From for SszBlockValidationError { SszBlockValidationError::AttestationValidationError(e) } } + +/* + * Tests for block validation are contained in the root directory "tests" directory (AKA + * "integration tests directory"). + */ diff --git a/tests/attestation_validation/helpers.rs b/tests/attestation_validation/helpers.rs index c06c239482..a93d8d531e 100644 --- a/tests/attestation_validation/helpers.rs +++ b/tests/attestation_validation/helpers.rs @@ -120,12 +120,8 @@ pub fn generate_attestation(shard_id: u16, } } -/* -fn get_valid_attestation_and_context(shard_id: u16, - shard_block_hash: Hash256, - attester_count: usize, - signing_attesters: &[usize]) - -> (AttestationRecord, AttestationValidationContext) +pub fn setup_attestation_validation_test(shard_id: u16, attester_count: usize) + -> (AttestationRecord, AttestationValidationContext, TestStore) { let stores = TestStore::new(); @@ -136,42 +132,50 @@ fn get_valid_attestation_and_context(shard_id: u16, .map(|i| Hash256::from(i as u64)) .collect(); let parent_hashes = Arc::new(parent_hashes); - let attester_map = Arc::new(AttesterMap::new()); let justified_block_hash = Hash256::from("justified_block".as_bytes()); + let shard_block_hash = Hash256::from("shard_block".as_bytes()); stores.block.put_serialized_block(&justified_block_hash.as_ref(), &[42]).unwrap(); - let aggregate_sig = AggregateSignature::new(); - let attester_bitfield = Bitfield::new(); + let attestation_slot = block_slot - 1; - let mut attestation_indices = vec![]; - for attester_index in 0..attester_count { - let kp = Keypair::random(); - let validator_index = attester_count - attester_index; - attestation_indices.push(validator_index); - stores.validator.put_public_key_by_index(validator_index, &kp.pk); + let mut keypairs = vec![]; + let mut signing_keys = vec![]; + let mut attester_map = AttesterMap::new(); + let mut attesters = vec![]; + + /* + * Generate a random keypair for each validator and clone it into the + * list of keypairs. Store it in the database. + */ + for i in 0..attester_count { + let keypair = Keypair::random(); + keypairs.push(keypair.clone()); + stores.validator.put_public_key_by_index(i, &keypair.pk).unwrap(); + signing_keys.push(Some(keypair.sk.clone())); + attesters.push(i); } + attester_map.insert((attestation_slot, shard_id), attesters); let context: AttestationValidationContext = AttestationValidationContext { block_slot, cycle_length, last_justified_slot, - parent_hashes, + parent_hashes: parent_hashes.clone(), block_store: stores.block.clone(), validator_store: stores.validator.clone(), - attester_map, + attester_map: Arc::new(attester_map), }; - - let attestation = AttestationRecord { - slot: block_slot - 1, + let attestation = generate_attestation( shard_id, - oblique_parent_hashes: vec![], - shard_block_hash, - attester_bitfield, - justified_slot: last_justified_slot, - justified_block_hash, - aggregate_sig, - }; - (attestation, context) + &shard_block_hash, + block_slot, + attestation_slot, + last_justified_slot, + &justified_block_hash, + cycle_length, + &parent_hashes.clone(), + &signing_keys); + + (attestation, context, stores) } -*/ diff --git a/tests/attestation_validation/tests.rs b/tests/attestation_validation/tests.rs index a57192d8d5..6ea3d722b6 100644 --- a/tests/attestation_validation/tests.rs +++ b/tests/attestation_validation/tests.rs @@ -2,6 +2,7 @@ use std::sync::Arc; use super::helpers::{ TestStore, + setup_attestation_validation_test, }; use super::state::attestation_record::{ AttestationRecord, @@ -27,5 +28,9 @@ use super::utils::types::{ #[test] fn test_attestation_validation_valid() { - // TODO + let (a, c, _stores) = setup_attestation_validation_test(10, 2); + + let result = c.validate_attestation(&a); + + assert!(result.unwrap().is_some()); } diff --git a/tests/block_validation/helpers.rs b/tests/block_validation/helpers.rs index 62fffc0714..7e085320db 100644 --- a/tests/block_validation/helpers.rs +++ b/tests/block_validation/helpers.rs @@ -126,7 +126,7 @@ pub fn setup_block_validation_scenario(params: &BlockTestParams) */ for shard in 0..shards_per_slot { let mut signing_keys = vec![]; - let mut attesters = vec![]; + let mut attesters = vec![]; /* * Generate a random keypair for each validator and clone it into the * list of keypairs. Store it in the database.