Refactor validation tests into integration tests
This commit is contained in:
parent
4d1f730c95
commit
8f5285875b
@ -36,3 +36,6 @@ ring = { git = "https://github.com/paritytech/ring" }
|
|||||||
[[bin]]
|
[[bin]]
|
||||||
path = "lighthouse/main.rs"
|
path = "lighthouse/main.rs"
|
||||||
name = "lighthouse"
|
name = "lighthouse"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
path = "lighthouse/lib.rs"
|
||||||
|
148
benches/block_validation.rs
Normal file
148
benches/block_validation.rs
Normal file
@ -0,0 +1,148 @@
|
|||||||
|
#![feature(test)]
|
||||||
|
|
||||||
|
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<F>(
|
||||||
|
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);
|
||||||
|
}
|
5
benches/main.rs
Normal file
5
benches/main.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#![feature(test)]
|
||||||
|
extern crate lighthouse;
|
||||||
|
extern crate tests;
|
||||||
|
|
||||||
|
mod block_validation;
|
@ -5,5 +5,6 @@ pub use self::bls_aggregates::AggregatePublicKey;
|
|||||||
pub use self::bls_aggregates::Signature;
|
pub use self::bls_aggregates::Signature;
|
||||||
pub use self::bls_aggregates::Keypair;
|
pub use self::bls_aggregates::Keypair;
|
||||||
pub use self::bls_aggregates::PublicKey;
|
pub use self::bls_aggregates::PublicKey;
|
||||||
|
pub use self::bls_aggregates::SecretKey;
|
||||||
|
|
||||||
pub const BLS_AGG_SIG_BYTE_SIZE: usize = 97;
|
pub const BLS_AGG_SIG_BYTE_SIZE: usize = 97;
|
||||||
|
22
lighthouse/lib.rs
Normal file
22
lighthouse/lib.rs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#[macro_use]
|
||||||
|
extern crate slog;
|
||||||
|
extern crate slog_term;
|
||||||
|
extern crate slog_async;
|
||||||
|
extern crate ssz;
|
||||||
|
extern crate clap;
|
||||||
|
extern crate network_libp2p;
|
||||||
|
extern crate futures;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub mod utils;
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub mod bls;
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub mod db;
|
||||||
|
pub mod client;
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub mod state;
|
||||||
|
#[allow(dead_code)]
|
||||||
|
mod sync;
|
||||||
|
mod config;
|
@ -61,17 +61,7 @@ impl<T> AttestationValidationContext<T>
|
|||||||
where T: ClientDB
|
where T: ClientDB
|
||||||
{
|
{
|
||||||
pub fn validate_attestation(&self, a: &AttestationRecord)
|
pub fn validate_attestation(&self, a: &AttestationRecord)
|
||||||
/*
|
|
||||||
block_slot: u64,
|
|
||||||
cycle_length: u8,
|
|
||||||
known_last_justified_slot: u64,
|
|
||||||
known_parent_hashes: &Arc<Vec<Hash256>>,
|
|
||||||
block_store: &Arc<BlockStore<T>>,
|
|
||||||
validator_store: &Arc<ValidatorStore<T>>,
|
|
||||||
attester_map: &Arc<AttesterMap>)
|
|
||||||
*/
|
|
||||||
-> Result<Option<HashSet<usize>>, AttestationValidationError>
|
-> Result<Option<HashSet<usize>>, AttestationValidationError>
|
||||||
where T: ClientDB + Sized
|
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* The attesation slot must not be higher than the block that contained it.
|
* The attesation slot must not be higher than the block that contained it.
|
||||||
|
@ -8,7 +8,7 @@ use super::utils;
|
|||||||
|
|
||||||
mod structs;
|
mod structs;
|
||||||
mod ssz_block;
|
mod ssz_block;
|
||||||
mod validation;
|
pub mod validation;
|
||||||
|
|
||||||
pub use self::structs::Block;
|
pub use self::structs::Block;
|
||||||
pub use self::ssz_block::{
|
pub use self::ssz_block::{
|
||||||
|
@ -1,8 +1,4 @@
|
|||||||
mod block_validation;
|
mod block_validation;
|
||||||
#[cfg(test)]
|
|
||||||
mod tests;
|
|
||||||
#[cfg(test)]
|
|
||||||
mod benches;
|
|
||||||
|
|
||||||
use super::attestation_record;
|
use super::attestation_record;
|
||||||
use super::{
|
use super::{
|
||||||
@ -14,7 +10,7 @@ use super::db;
|
|||||||
use super::ssz;
|
use super::ssz;
|
||||||
use super::utils;
|
use super::utils;
|
||||||
|
|
||||||
use super::common::maps::{
|
pub use super::common::maps::{
|
||||||
AttesterMap,
|
AttesterMap,
|
||||||
ProposerMap,
|
ProposerMap,
|
||||||
};
|
};
|
||||||
|
177
tests/attestation_validation/helpers.rs
Normal file
177
tests/attestation_validation/helpers.rs
Normal file
@ -0,0 +1,177 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use super::db::{
|
||||||
|
MemoryDB,
|
||||||
|
};
|
||||||
|
use super::db::stores::{
|
||||||
|
BlockStore,
|
||||||
|
ValidatorStore,
|
||||||
|
};
|
||||||
|
use super::state::attestation_record::{
|
||||||
|
AttestationRecord,
|
||||||
|
AttestationValidationContext,
|
||||||
|
AttestationValidationError,
|
||||||
|
};
|
||||||
|
use super::state::block::validation::AttesterMap;
|
||||||
|
use super::bls::{
|
||||||
|
AggregateSignature,
|
||||||
|
Keypair,
|
||||||
|
SecretKey,
|
||||||
|
Signature,
|
||||||
|
};
|
||||||
|
use super::ssz::SszStream;
|
||||||
|
use super::utils::types::{
|
||||||
|
Hash256,
|
||||||
|
Bitfield,
|
||||||
|
};
|
||||||
|
use super::utils::hash::{
|
||||||
|
canonical_hash,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
pub struct TestStore {
|
||||||
|
pub db: Arc<MemoryDB>,
|
||||||
|
pub block: Arc<BlockStore<MemoryDB>>,
|
||||||
|
pub validator: Arc<ValidatorStore<MemoryDB>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TestStore {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let db = Arc::new(MemoryDB::open());
|
||||||
|
let block = Arc::new(BlockStore::new(db.clone()));
|
||||||
|
let validator = Arc::new(ValidatorStore::new(db.clone()));
|
||||||
|
Self {
|
||||||
|
db,
|
||||||
|
block,
|
||||||
|
validator,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_message_hash(slot: u64,
|
||||||
|
parent_hashes: &[Hash256],
|
||||||
|
shard_id: u16,
|
||||||
|
shard_block_hash: &Hash256,
|
||||||
|
justified_slot: u64)
|
||||||
|
-> Vec<u8>
|
||||||
|
{
|
||||||
|
let mut stream = SszStream::new();
|
||||||
|
stream.append(&slot);
|
||||||
|
stream.append_vec(&parent_hashes.to_vec());
|
||||||
|
stream.append(&shard_id);
|
||||||
|
stream.append(shard_block_hash);
|
||||||
|
stream.append(&justified_slot);
|
||||||
|
let bytes = stream.drain();
|
||||||
|
canonical_hash(&bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate_attestation(shard_id: u16,
|
||||||
|
shard_block_hash: &Hash256,
|
||||||
|
block_slot: u64,
|
||||||
|
attestation_slot: u64,
|
||||||
|
justified_slot: u64,
|
||||||
|
justified_block_hash: &Hash256,
|
||||||
|
cycle_length: u8,
|
||||||
|
parent_hashes: &[Hash256],
|
||||||
|
signing_keys: &[Option<SecretKey>])
|
||||||
|
-> AttestationRecord
|
||||||
|
{
|
||||||
|
let mut attester_bitfield = Bitfield::new();
|
||||||
|
let mut aggregate_sig = AggregateSignature::new();
|
||||||
|
|
||||||
|
let parent_hashes_slice = {
|
||||||
|
let distance: usize = (block_slot - attestation_slot) as usize;
|
||||||
|
let last: usize = parent_hashes.len() - distance;
|
||||||
|
let first: usize = last - usize::from(cycle_length);
|
||||||
|
&parent_hashes[first..last]
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Generate the message that will be signed across for this attr record.
|
||||||
|
*/
|
||||||
|
let attestation_message = generate_message_hash(
|
||||||
|
attestation_slot,
|
||||||
|
parent_hashes_slice,
|
||||||
|
shard_id,
|
||||||
|
shard_block_hash,
|
||||||
|
justified_slot);
|
||||||
|
|
||||||
|
for (i, secret_key) in signing_keys.iter().enumerate() {
|
||||||
|
/*
|
||||||
|
* If the signing key is Some, set the bitfield bit to true
|
||||||
|
* and sign the aggregate sig.
|
||||||
|
*/
|
||||||
|
if let Some(sk) = secret_key {
|
||||||
|
attester_bitfield.set_bit(i, true);
|
||||||
|
let sig = Signature::new(&attestation_message, sk);
|
||||||
|
aggregate_sig.add(&sig);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AttestationRecord {
|
||||||
|
slot: attestation_slot,
|
||||||
|
shard_id,
|
||||||
|
oblique_parent_hashes: vec![],
|
||||||
|
shard_block_hash: shard_block_hash.clone(),
|
||||||
|
attester_bitfield,
|
||||||
|
justified_slot,
|
||||||
|
justified_block_hash: justified_block_hash.clone(),
|
||||||
|
aggregate_sig,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
fn get_valid_attestation_and_context(shard_id: u16,
|
||||||
|
shard_block_hash: Hash256,
|
||||||
|
attester_count: usize,
|
||||||
|
signing_attesters: &[usize])
|
||||||
|
-> (AttestationRecord, AttestationValidationContext<MemoryDB>)
|
||||||
|
{
|
||||||
|
let stores = TestStore::new();
|
||||||
|
|
||||||
|
let block_slot = 10000;
|
||||||
|
let cycle_length: u8 = 64;
|
||||||
|
let last_justified_slot = block_slot - u64::from(cycle_length);
|
||||||
|
let parent_hashes: Vec<Hash256> = (0..(cycle_length * 2))
|
||||||
|
.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());
|
||||||
|
|
||||||
|
stores.block.put_serialized_block(&justified_block_hash.as_ref(), &[42]).unwrap();
|
||||||
|
|
||||||
|
let aggregate_sig = AggregateSignature::new();
|
||||||
|
let attester_bitfield = Bitfield::new();
|
||||||
|
|
||||||
|
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 context: AttestationValidationContext<MemoryDB> = AttestationValidationContext {
|
||||||
|
block_slot,
|
||||||
|
cycle_length,
|
||||||
|
last_justified_slot,
|
||||||
|
parent_hashes,
|
||||||
|
block_store: stores.block.clone(),
|
||||||
|
validator_store: stores.validator.clone(),
|
||||||
|
attester_map,
|
||||||
|
};
|
||||||
|
|
||||||
|
let attestation = AttestationRecord {
|
||||||
|
slot: block_slot - 1,
|
||||||
|
shard_id,
|
||||||
|
oblique_parent_hashes: vec![],
|
||||||
|
shard_block_hash,
|
||||||
|
attester_bitfield,
|
||||||
|
justified_slot: last_justified_slot,
|
||||||
|
justified_block_hash,
|
||||||
|
aggregate_sig,
|
||||||
|
};
|
||||||
|
(attestation, context)
|
||||||
|
}
|
||||||
|
*/
|
8
tests/attestation_validation/mod.rs
Normal file
8
tests/attestation_validation/mod.rs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
pub mod helpers;
|
||||||
|
mod tests;
|
||||||
|
|
||||||
|
use super::bls;
|
||||||
|
use super::db;
|
||||||
|
use super::state;
|
||||||
|
use super::ssz;
|
||||||
|
use super::utils;
|
31
tests/attestation_validation/tests.rs
Normal file
31
tests/attestation_validation/tests.rs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use super::helpers::{
|
||||||
|
TestStore,
|
||||||
|
};
|
||||||
|
use super::state::attestation_record::{
|
||||||
|
AttestationRecord,
|
||||||
|
AttestationValidationContext,
|
||||||
|
AttestationValidationError,
|
||||||
|
};
|
||||||
|
use super::state::block::validation::AttesterMap;
|
||||||
|
use super::bls::{
|
||||||
|
AggregateSignature,
|
||||||
|
Keypair,
|
||||||
|
};
|
||||||
|
use super::db::{
|
||||||
|
MemoryDB,
|
||||||
|
};
|
||||||
|
use super::db::stores::{
|
||||||
|
BlockStore,
|
||||||
|
ValidatorStore,
|
||||||
|
};
|
||||||
|
use super::utils::types::{
|
||||||
|
Hash256,
|
||||||
|
Bitfield,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_attestation_validation_valid() {
|
||||||
|
// TODO
|
||||||
|
}
|
@ -1,26 +1,52 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use super::{
|
|
||||||
|
use super::generate_attestation;
|
||||||
|
use super::bls::{
|
||||||
|
Keypair,
|
||||||
|
};
|
||||||
|
use super::db::{
|
||||||
|
MemoryDB,
|
||||||
|
};
|
||||||
|
use super::db::stores::{
|
||||||
|
BlockStore,
|
||||||
|
PoWChainStore,
|
||||||
|
ValidatorStore,
|
||||||
|
};
|
||||||
|
use super::state::attestation_record::{
|
||||||
|
AttestationRecord,
|
||||||
|
};
|
||||||
|
use super::state::block::{
|
||||||
|
SszBlock,
|
||||||
|
Block,
|
||||||
|
};
|
||||||
|
use super::state::block::validation::{
|
||||||
BlockValidationContext,
|
BlockValidationContext,
|
||||||
SszBlockValidationError,
|
SszBlockValidationError,
|
||||||
BlockStatus,
|
BlockStatus,
|
||||||
AttesterMap,
|
AttesterMap,
|
||||||
ProposerMap,
|
ProposerMap,
|
||||||
MemoryDB,
|
|
||||||
canonical_hash,
|
|
||||||
Hash256,
|
|
||||||
Bitfield,
|
|
||||||
Keypair,
|
|
||||||
Signature,
|
|
||||||
AggregateSignature,
|
|
||||||
BlockStore,
|
|
||||||
PoWChainStore,
|
|
||||||
ValidatorStore,
|
|
||||||
SszStream,
|
|
||||||
SszBlock,
|
|
||||||
Block,
|
|
||||||
AttestationRecord,
|
|
||||||
TestParams,
|
|
||||||
};
|
};
|
||||||
|
use super::ssz::{
|
||||||
|
SszStream,
|
||||||
|
};
|
||||||
|
use super::utils::types::{
|
||||||
|
Hash256,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct BlockTestParams {
|
||||||
|
pub total_validators: usize,
|
||||||
|
pub cycle_length: u8,
|
||||||
|
pub shard_count: u16,
|
||||||
|
pub shards_per_slot: u16,
|
||||||
|
pub validators_per_shard: usize,
|
||||||
|
pub block_slot: u64,
|
||||||
|
pub attestations_justified_slot: u64,
|
||||||
|
pub parent_proposer_index: usize,
|
||||||
|
pub validation_context_slot: u64,
|
||||||
|
pub validation_context_justified_slot: u64,
|
||||||
|
pub validation_context_finalized_slot: u64,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct TestStore {
|
pub struct TestStore {
|
||||||
pub db: Arc<MemoryDB>,
|
pub db: Arc<MemoryDB>,
|
||||||
@ -48,7 +74,7 @@ type ParentHashes = Vec<Hash256>;
|
|||||||
|
|
||||||
/// Setup for a block validation function, without actually executing the
|
/// Setup for a block validation function, without actually executing the
|
||||||
/// block validation function.
|
/// block validation function.
|
||||||
pub fn setup_block_validation_scenario(params: &TestParams)
|
pub fn setup_block_validation_scenario(params: &BlockTestParams)
|
||||||
-> (Block, ParentHashes, AttesterMap, ProposerMap, TestStore)
|
-> (Block, ParentHashes, AttesterMap, ProposerMap, TestStore)
|
||||||
{
|
{
|
||||||
let stores = TestStore::new();
|
let stores = TestStore::new();
|
||||||
@ -95,72 +121,36 @@ pub fn setup_block_validation_scenario(params: &TestParams)
|
|||||||
let mut attester_map = AttesterMap::new();
|
let mut attester_map = AttesterMap::new();
|
||||||
let mut attestations = vec![];
|
let mut attestations = vec![];
|
||||||
let mut keypairs = vec![];
|
let mut keypairs = vec![];
|
||||||
|
/*
|
||||||
|
* For each shard in this slot, generate an attestation.
|
||||||
|
*/
|
||||||
for shard in 0..shards_per_slot {
|
for shard in 0..shards_per_slot {
|
||||||
|
let mut signing_keys = vec![];
|
||||||
let mut attesters = vec![];
|
let mut attesters = vec![];
|
||||||
let mut attester_bitfield = Bitfield::new();
|
/*
|
||||||
let mut aggregate_sig = AggregateSignature::new();
|
* Generate a random keypair for each validator and clone it into the
|
||||||
|
* list of keypairs. Store it in the database.
|
||||||
let parent_hashes_slice = {
|
*/
|
||||||
let distance: usize = (block_slot - attestation_slot) as usize;
|
for _ in 0..validators_per_shard {
|
||||||
let last: usize = parent_hashes.len() - distance;
|
|
||||||
let first: usize = last - usize::from(cycle_length);
|
|
||||||
&parent_hashes[first..last]
|
|
||||||
};
|
|
||||||
|
|
||||||
let attestation_message = {
|
|
||||||
let mut stream = SszStream::new();
|
|
||||||
stream.append(&attestation_slot);
|
|
||||||
stream.append_vec(&parent_hashes_slice.to_vec());
|
|
||||||
stream.append(&shard);
|
|
||||||
stream.append(&shard_block_hash);
|
|
||||||
stream.append(&attestations_justified_slot);
|
|
||||||
let bytes = stream.drain();
|
|
||||||
canonical_hash(&bytes)
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
for attestation_index in 0..validators_per_shard {
|
|
||||||
/*
|
|
||||||
* Add the attester to the attestation indices for this shard.
|
|
||||||
*/
|
|
||||||
attesters.push(i);
|
|
||||||
/*
|
|
||||||
* Set the voters bit on the bitfield to true.
|
|
||||||
*/
|
|
||||||
attester_bitfield.set_bit(attestation_index, true);
|
|
||||||
/*
|
|
||||||
* Generate a random keypair for this validatior and clone it into the
|
|
||||||
* list of keypairs.
|
|
||||||
*/
|
|
||||||
let keypair = Keypair::random();
|
let keypair = Keypair::random();
|
||||||
keypairs.push(keypair.clone());
|
keypairs.push(keypair.clone());
|
||||||
/*
|
|
||||||
* Store the validators public key in the database.
|
|
||||||
*/
|
|
||||||
stores.validator.put_public_key_by_index(i, &keypair.pk).unwrap();
|
stores.validator.put_public_key_by_index(i, &keypair.pk).unwrap();
|
||||||
/*
|
signing_keys.push(Some(keypair.sk.clone()));
|
||||||
* Generate a new signature and aggregate it on the rolling signature.
|
attesters.push(i);
|
||||||
*/
|
|
||||||
let sig = Signature::new(&attestation_message, &keypair.sk);
|
|
||||||
aggregate_sig.add(&sig);
|
|
||||||
/*
|
|
||||||
* Increment the validator counter to monotonically assign validators.
|
|
||||||
*/
|
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
attester_map.insert((attestation_slot, shard), attesters);
|
attester_map.insert((attestation_slot, shard), attesters);
|
||||||
let attestation = AttestationRecord {
|
|
||||||
slot: attestation_slot,
|
let attestation = generate_attestation(
|
||||||
shard_id: shard,
|
shard,
|
||||||
oblique_parent_hashes: vec![],
|
&shard_block_hash,
|
||||||
shard_block_hash,
|
block_slot,
|
||||||
attester_bitfield,
|
attestation_slot,
|
||||||
justified_slot: attestations_justified_slot,
|
attestations_justified_slot,
|
||||||
justified_block_hash,
|
&justified_block_hash,
|
||||||
aggregate_sig,
|
cycle_length,
|
||||||
};
|
&parent_hashes,
|
||||||
|
&signing_keys[..]);
|
||||||
attestations.push(attestation);
|
attestations.push(attestation);
|
||||||
}
|
}
|
||||||
(attester_map, attestations, keypairs)
|
(attester_map, attestations, keypairs)
|
||||||
@ -194,7 +184,7 @@ pub fn serialize_block(b: &Block) -> Vec<u8> {
|
|||||||
///
|
///
|
||||||
/// Returns the Result returned from the block validation function.
|
/// Returns the Result returned from the block validation function.
|
||||||
pub fn run_block_validation_scenario<F>(
|
pub fn run_block_validation_scenario<F>(
|
||||||
params: &TestParams,
|
params: &BlockTestParams,
|
||||||
mutator_func: F)
|
mutator_func: F)
|
||||||
-> Result<(BlockStatus, Option<Block>), SszBlockValidationError>
|
-> Result<(BlockStatus, Option<Block>), SszBlockValidationError>
|
||||||
where F: FnOnce(Block, AttesterMap, ProposerMap, TestStore)
|
where F: FnOnce(Block, AttesterMap, ProposerMap, TestStore)
|
10
tests/block_validation/mod.rs
Normal file
10
tests/block_validation/mod.rs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
mod helpers;
|
||||||
|
mod tests;
|
||||||
|
|
||||||
|
use super::bls;
|
||||||
|
use super::db;
|
||||||
|
use super::ssz;
|
||||||
|
use super::state;
|
||||||
|
use super::utils;
|
||||||
|
|
||||||
|
use super::attestation_validation::helpers::generate_attestation;
|
@ -1,62 +1,27 @@
|
|||||||
extern crate ssz;
|
use super::bls::{
|
||||||
|
AggregateSignature,
|
||||||
mod helpers;
|
};
|
||||||
|
use super::helpers::{
|
||||||
pub use self::helpers::{
|
BlockTestParams,
|
||||||
TestStore,
|
TestStore,
|
||||||
setup_block_validation_scenario,
|
|
||||||
run_block_validation_scenario,
|
run_block_validation_scenario,
|
||||||
serialize_block,
|
serialize_block,
|
||||||
};
|
};
|
||||||
|
use super::state::block::{
|
||||||
use self::ssz::{
|
SszBlock,
|
||||||
SszStream,
|
Block,
|
||||||
};
|
};
|
||||||
use super::{
|
use super::state::block::validation::{
|
||||||
BlockValidationContext,
|
|
||||||
SszBlockValidationError,
|
SszBlockValidationError,
|
||||||
BlockStatus,
|
BlockStatus,
|
||||||
AttesterMap,
|
|
||||||
ProposerMap,
|
ProposerMap,
|
||||||
};
|
};
|
||||||
use super::db::stores::{
|
|
||||||
BlockStore,
|
|
||||||
PoWChainStore,
|
|
||||||
ValidatorStore,
|
|
||||||
};
|
|
||||||
use super::db::{
|
|
||||||
MemoryDB,
|
|
||||||
};
|
|
||||||
use super::utils::hash::canonical_hash;
|
use super::utils::hash::canonical_hash;
|
||||||
use super::utils::types::{
|
use super::utils::types::{
|
||||||
Hash256,
|
Hash256,
|
||||||
Bitfield,
|
|
||||||
};
|
|
||||||
use super::SszBlock;
|
|
||||||
use super::super::Block;
|
|
||||||
use super::super::attestation_record::AttestationRecord;
|
|
||||||
use super::super::super::bls::{
|
|
||||||
Keypair,
|
|
||||||
Signature,
|
|
||||||
AggregateSignature,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
fn get_simple_params() -> BlockTestParams {
|
||||||
pub struct TestParams {
|
|
||||||
pub total_validators: usize,
|
|
||||||
pub cycle_length: u8,
|
|
||||||
pub shard_count: u16,
|
|
||||||
pub shards_per_slot: u16,
|
|
||||||
pub validators_per_shard: usize,
|
|
||||||
pub block_slot: u64,
|
|
||||||
pub attestations_justified_slot: u64,
|
|
||||||
pub parent_proposer_index: usize,
|
|
||||||
pub validation_context_slot: u64,
|
|
||||||
pub validation_context_justified_slot: u64,
|
|
||||||
pub validation_context_finalized_slot: u64,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_simple_params() -> TestParams {
|
|
||||||
let validators_per_shard: usize = 5;
|
let validators_per_shard: usize = 5;
|
||||||
let cycle_length: u8 = 2;
|
let cycle_length: u8 = 2;
|
||||||
let shard_count: u16 = 4;
|
let shard_count: u16 = 4;
|
||||||
@ -70,7 +35,7 @@ fn get_simple_params() -> TestParams {
|
|||||||
let validation_context_justified_slot = attestations_justified_slot;
|
let validation_context_justified_slot = attestations_justified_slot;
|
||||||
let validation_context_finalized_slot = 0;
|
let validation_context_finalized_slot = 0;
|
||||||
|
|
||||||
TestParams {
|
BlockTestParams {
|
||||||
total_validators,
|
total_validators,
|
||||||
cycle_length,
|
cycle_length,
|
||||||
shard_count,
|
shard_count,
|
12
tests/main.rs
Normal file
12
tests/main.rs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
extern crate lighthouse;
|
||||||
|
extern crate ssz;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod attestation_validation;
|
||||||
|
#[cfg(test)]
|
||||||
|
mod block_validation;
|
||||||
|
|
||||||
|
use lighthouse::bls;
|
||||||
|
use lighthouse::db;
|
||||||
|
use lighthouse::state;
|
||||||
|
use lighthouse::utils;
|
Loading…
Reference in New Issue
Block a user