Split block validation tests into functions

This commit is contained in:
Paul Hauner 2018-09-26 13:31:58 +10:00
parent 9dcec214af
commit 84bb40855f
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C

View File

@ -54,36 +54,18 @@ impl TestStore {
}
}
#[test]
fn test_block_validation() {
let stores = TestStore::new();
let cycle_length: u8 = 2;
let shard_count: u16 = 2;
let present_slot = u64::from(cycle_length) * 10000;
let justified_slot = present_slot - u64::from(cycle_length);
let justified_block_hash = Hash256::from("justified_hash".as_bytes());
let shard_block_hash = Hash256::from("shard_hash".as_bytes());
let parent_hashes: Vec<Hash256> = (0..(cycle_length * 2))
.map(|i| Hash256::from(i as u64))
.collect();
let pow_chain_ref = Hash256::from("pow_chain".as_bytes());
let active_state_root = Hash256::from("active_state".as_bytes());
let crystallized_state_root = Hash256::from("cry_state".as_bytes());
stores.pow_chain.put_block_hash(pow_chain_ref.as_ref()).unwrap();
stores.block.put_block(justified_block_hash.as_ref(), &vec![42]).unwrap();
let validators_per_shard = 10;
let block_slot = present_slot;
let validator_index: usize = 0;
let proposer_map = {
let mut proposer_map = ProposerMap::new();
proposer_map.insert(present_slot, validator_index);
proposer_map
};
let (attester_map, attestations, _keypairs) = {
fn generate_attestations_for_slot(attestation_slot: u64,
block_slot: u64,
shard_count: u16,
validators_per_shard: usize,
cycle_length: u8,
parent_hashes: &[Hash256],
shard_block_hash: &Hash256,
justified_block_hash: &Hash256,
justified_slot: u64,
stores: &TestStore)
-> (AttesterMap, Vec<AttestationRecord>, Vec<Keypair>)
{
let mut i = 0;
let mut attester_map = AttesterMap::new();
let mut attestations = vec![];
@ -92,7 +74,6 @@ fn test_block_validation() {
let mut attesters = vec![];
let mut attester_bitfield = Bitfield::new();
let mut aggregate_sig = AggregateSignature::new();
let attestation_slot = block_slot - 1;
let parent_hashes_slice = {
let distance: usize = (block_slot - attestation_slot) as usize;
@ -106,7 +87,7 @@ fn test_block_validation() {
stream.append(&attestation_slot);
stream.append_vec(&parent_hashes_slice.to_vec());
stream.append(&shard);
stream.append(&shard_block_hash);
stream.append(shard_block_hash);
stream.append(&justified_slot);
let bytes = stream.drain();
canonical_hash(&bytes)
@ -149,16 +130,59 @@ fn test_block_validation() {
slot: attestation_slot,
shard_id: shard,
oblique_parent_hashes: vec![],
shard_block_hash,
shard_block_hash: *shard_block_hash,
attester_bitfield,
justified_slot,
justified_block_hash,
justified_block_hash: *justified_block_hash,
aggregate_sig,
};
attestations.push(attestation);
}
(attester_map, attestations, keypairs)
}
#[test]
fn test_block_validation() {
let stores = TestStore::new();
let cycle_length: u8 = 2;
let shard_count: u16 = 2;
let present_slot = u64::from(cycle_length) * 10000;
let justified_slot = present_slot - u64::from(cycle_length);
let justified_block_hash = Hash256::from("justified_hash".as_bytes());
let shard_block_hash = Hash256::from("shard_hash".as_bytes());
let parent_hashes: Vec<Hash256> = (0..(cycle_length * 2))
.map(|i| Hash256::from(i as u64))
.collect();
let pow_chain_ref = Hash256::from("pow_chain".as_bytes());
let active_state_root = Hash256::from("active_state".as_bytes());
let crystallized_state_root = Hash256::from("cry_state".as_bytes());
stores.pow_chain.put_block_hash(pow_chain_ref.as_ref()).unwrap();
stores.block.put_block(justified_block_hash.as_ref(), &vec![42]).unwrap();
let validators_per_shard = 10;
let block_slot = present_slot;
let validator_index: usize = 0;
let proposer_map = {
let mut proposer_map = ProposerMap::new();
proposer_map.insert(present_slot, validator_index);
proposer_map
};
let attestation_slot = block_slot - 1;
let (attester_map, attestations, _keypairs) =
generate_attestations_for_slot(
attestation_slot,
block_slot,
shard_count,
validators_per_shard,
cycle_length,
&parent_hashes,
&shard_block_hash,
&justified_block_hash,
justified_slot,
&stores);
let block = Block {
parent_hash: Hash256::from("parent".as_bytes()),