mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-07 16:55:46 +00:00
Add untested minimum viable block processing
This commit is contained in:
@@ -1,35 +1,14 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::attestation_validation::helpers::{
|
||||
generate_attestation,
|
||||
insert_justified_block_hash,
|
||||
};
|
||||
use super::bls::{
|
||||
Keypair,
|
||||
};
|
||||
use super::db::{
|
||||
MemoryDB,
|
||||
};
|
||||
use super::db::stores::{
|
||||
BeaconBlockStore,
|
||||
PoWChainStore,
|
||||
ValidatorStore,
|
||||
};
|
||||
use super::types::{
|
||||
AttestationRecord,
|
||||
AttesterMap,
|
||||
BeaconBlock,
|
||||
Hash256,
|
||||
ProposerMap,
|
||||
};
|
||||
use super::attestation_validation::helpers::{generate_attestation, insert_justified_block_hash};
|
||||
use super::bls::Keypair;
|
||||
use super::db::stores::{BeaconBlockStore, PoWChainStore, ValidatorStore};
|
||||
use super::db::MemoryDB;
|
||||
use super::ssz::SszStream;
|
||||
use super::ssz_helpers::ssz_beacon_block::SszBeaconBlock;
|
||||
use super::types::{AttestationRecord, AttesterMap, BeaconBlock, Hash256, ProposerMap};
|
||||
use super::validation::block_validation::{
|
||||
BeaconBlockValidationContext,
|
||||
SszBeaconBlockValidationError,
|
||||
BeaconBlockStatus,
|
||||
};
|
||||
use super::ssz::{
|
||||
SszStream,
|
||||
BeaconBlockValidationContext, SszBeaconBlockValidationError,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -74,9 +53,15 @@ type ParentHashes = Vec<Hash256>;
|
||||
|
||||
/// Setup for a block validation function, without actually executing the
|
||||
/// block validation function.
|
||||
pub fn setup_block_validation_scenario(params: &BeaconBlockTestParams)
|
||||
-> (BeaconBlock, ParentHashes, AttesterMap, ProposerMap, TestStore)
|
||||
{
|
||||
pub fn setup_block_validation_scenario(
|
||||
params: &BeaconBlockTestParams,
|
||||
) -> (
|
||||
BeaconBlock,
|
||||
ParentHashes,
|
||||
AttesterMap,
|
||||
ProposerMap,
|
||||
TestStore,
|
||||
) {
|
||||
let stores = TestStore::new();
|
||||
|
||||
let cycle_length = params.cycle_length;
|
||||
@@ -100,7 +85,10 @@ pub fn setup_block_validation_scenario(params: &BeaconBlockTestParams)
|
||||
/*
|
||||
* Store a valid PoW chain ref
|
||||
*/
|
||||
stores.pow_chain.put_block_hash(pow_chain_ref.as_ref()).unwrap();
|
||||
stores
|
||||
.pow_chain
|
||||
.put_block_hash(pow_chain_ref.as_ref())
|
||||
.unwrap();
|
||||
|
||||
/*
|
||||
* Generate a minimum viable parent block and store it in the database.
|
||||
@@ -110,7 +98,10 @@ pub fn setup_block_validation_scenario(params: &BeaconBlockTestParams)
|
||||
parent_block.slot = block_slot - 1;
|
||||
parent_block.attestations.push(parent_attestation);
|
||||
let parent_block_ssz = serialize_block(&parent_block);
|
||||
stores.block.put_serialized_block(parent_hash.as_ref(), &parent_block_ssz).unwrap();
|
||||
stores
|
||||
.block
|
||||
.put_serialized_block(parent_hash.as_ref(), &parent_block_ssz)
|
||||
.unwrap();
|
||||
|
||||
let proposer_map = {
|
||||
let mut proposer_map = ProposerMap::new();
|
||||
@@ -132,24 +123,28 @@ pub fn setup_block_validation_scenario(params: &BeaconBlockTestParams)
|
||||
&mut parent_hashes,
|
||||
&justified_block_hash,
|
||||
block_slot,
|
||||
attestation_slot);
|
||||
attestation_slot,
|
||||
);
|
||||
/*
|
||||
* For each shard in this slot, generate an attestation.
|
||||
*/
|
||||
for shard in 0..shards_per_slot {
|
||||
let mut signing_keys = 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.
|
||||
*/
|
||||
let mut signing_keys = 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.
|
||||
*/
|
||||
for _ in 0..validators_per_shard {
|
||||
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);
|
||||
i += 1;
|
||||
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);
|
||||
i += 1;
|
||||
}
|
||||
attester_map.insert((attestation_slot, shard), attesters);
|
||||
|
||||
@@ -163,7 +158,8 @@ pub fn setup_block_validation_scenario(params: &BeaconBlockTestParams)
|
||||
cycle_length,
|
||||
&parent_hashes,
|
||||
&signing_keys[..],
|
||||
&stores.block);
|
||||
&stores.block,
|
||||
);
|
||||
attestations.push(attestation);
|
||||
}
|
||||
(attester_map, attestations, keypairs)
|
||||
@@ -180,11 +176,7 @@ pub fn setup_block_validation_scenario(params: &BeaconBlockTestParams)
|
||||
specials: vec![],
|
||||
};
|
||||
|
||||
(block,
|
||||
parent_hashes,
|
||||
attester_map,
|
||||
proposer_map,
|
||||
stores)
|
||||
(block, parent_hashes, attester_map, proposer_map, stores)
|
||||
}
|
||||
|
||||
/// Helper function to take some BeaconBlock and SSZ serialize it.
|
||||
@@ -199,25 +191,20 @@ pub fn serialize_block(b: &BeaconBlock) -> Vec<u8> {
|
||||
/// Returns the Result returned from the block validation function.
|
||||
pub fn run_block_validation_scenario<F>(
|
||||
params: &BeaconBlockTestParams,
|
||||
mutator_func: F)
|
||||
-> Result<(BeaconBlockStatus, Option<BeaconBlock>), SszBeaconBlockValidationError>
|
||||
where F: FnOnce(BeaconBlock, AttesterMap, ProposerMap, TestStore)
|
||||
-> (BeaconBlock, AttesterMap, ProposerMap, TestStore)
|
||||
mutator_func: F,
|
||||
) -> Result<BeaconBlock, SszBeaconBlockValidationError>
|
||||
where
|
||||
F: FnOnce(BeaconBlock, AttesterMap, ProposerMap, TestStore)
|
||||
-> (BeaconBlock, AttesterMap, ProposerMap, TestStore),
|
||||
{
|
||||
let (block,
|
||||
parent_hashes,
|
||||
attester_map,
|
||||
proposer_map,
|
||||
stores) = setup_block_validation_scenario(¶ms);
|
||||
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 (block, attester_map, proposer_map, stores) =
|
||||
mutator_func(block, attester_map, proposer_map, stores);
|
||||
|
||||
let ssz_bytes = serialize_block(&block);
|
||||
let ssz_block = SszBeaconBlock::from_slice(&ssz_bytes[..])
|
||||
.unwrap();
|
||||
let ssz_block = SszBeaconBlock::from_slice(&ssz_bytes[..]).unwrap();
|
||||
|
||||
let context = BeaconBlockValidationContext {
|
||||
present_slot: params.validation_context_slot,
|
||||
@@ -230,17 +217,17 @@ pub fn run_block_validation_scenario<F>(
|
||||
attester_map: Arc::new(attester_map),
|
||||
block_store: stores.block.clone(),
|
||||
validator_store: stores.validator.clone(),
|
||||
pow_store: stores.pow_chain.clone()
|
||||
pow_store: stores.pow_chain.clone(),
|
||||
};
|
||||
let block_hash = Hash256::from(&ssz_block.block_hash()[..]);
|
||||
let validation_status = context.validate_ssz_block(&block_hash, &ssz_block);
|
||||
let validation_result = context.validate_ssz_block(&ssz_block);
|
||||
/*
|
||||
* If validation returned a block, make sure it's the same block we supplied to it.
|
||||
*
|
||||
* I.e., there were no errors during the serialization -> deserialization process.
|
||||
*/
|
||||
if let Ok((_, Some(returned_block))) = &validation_status {
|
||||
if let Ok(returned_block) = &validation_result {
|
||||
assert_eq!(*returned_block, block);
|
||||
};
|
||||
validation_status
|
||||
validation_result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user