Refactor block val. into "BlockValidationContext"
This commit is contained in:
parent
bc27be147f
commit
0b99951bf8
@ -5,7 +5,7 @@ use self::test::Bencher;
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
validate_ssz_block,
|
BlockValidationContext,
|
||||||
AttesterMap,
|
AttesterMap,
|
||||||
ProposerMap,
|
ProposerMap,
|
||||||
};
|
};
|
||||||
@ -50,17 +50,18 @@ fn bench_block_validation_scenario<F>(
|
|||||||
let proposer_map = Arc::new(proposer_map);
|
let proposer_map = Arc::new(proposer_map);
|
||||||
let attester_map = Arc::new(attester_map);
|
let attester_map = Arc::new(attester_map);
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
validate_ssz_block(
|
let context = BlockValidationContext {
|
||||||
&ssz_block,
|
present_slot: validation_slot,
|
||||||
validation_slot,
|
cycle_length: params.cycle_length,
|
||||||
params.cycle_length,
|
last_justified_slot: validation_last_justified_slot,
|
||||||
validation_last_justified_slot,
|
parent_hashes: parent_hashes.clone(),
|
||||||
&parent_hashes.clone(),
|
proposer_map: proposer_map.clone(),
|
||||||
&proposer_map.clone(),
|
attester_map: attester_map.clone(),
|
||||||
&attester_map.clone(),
|
block_store: stores.block.clone(),
|
||||||
&stores.block.clone(),
|
validator_store: stores.validator.clone(),
|
||||||
&stores.validator.clone(),
|
pow_store: stores.pow_chain.clone()
|
||||||
&stores.pow_chain.clone())
|
};
|
||||||
|
context.validate_ssz_block(&ssz_block)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ use super::common::maps::{
|
|||||||
ProposerMap,
|
ProposerMap,
|
||||||
};
|
};
|
||||||
pub use self::validate_ssz_block::{
|
pub use self::validate_ssz_block::{
|
||||||
validate_ssz_block,
|
BlockValidationContext,
|
||||||
SszBlockValidationError,
|
SszBlockValidationError,
|
||||||
BlockStatus,
|
BlockStatus,
|
||||||
};
|
};
|
||||||
|
@ -5,7 +5,7 @@ use self::ssz::{
|
|||||||
};
|
};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use super::{
|
use super::{
|
||||||
validate_ssz_block,
|
BlockValidationContext,
|
||||||
SszBlockValidationError,
|
SszBlockValidationError,
|
||||||
BlockStatus,
|
BlockStatus,
|
||||||
AttesterMap,
|
AttesterMap,
|
||||||
@ -101,21 +101,7 @@ pub fn setup_block_validation_scenario(params: &TestParams)
|
|||||||
proposer_map.insert(block_slot, validator_index);
|
proposer_map.insert(block_slot, validator_index);
|
||||||
proposer_map
|
proposer_map
|
||||||
};
|
};
|
||||||
/*
|
|
||||||
let attestation_slot = block_slot - 1;
|
|
||||||
let (attester_map, attestations, _keypairs) =
|
|
||||||
generate_attestations_for_slot(
|
|
||||||
attestation_slot,
|
|
||||||
block_slot,
|
|
||||||
shards_per_slot,
|
|
||||||
validators_per_shard,
|
|
||||||
cycle_length,
|
|
||||||
&parent_hashes,
|
|
||||||
&Hash256::from("shard_hash".as_bytes()),
|
|
||||||
&justified_block_hash,
|
|
||||||
attestations_justified_slot,
|
|
||||||
&stores);
|
|
||||||
*/
|
|
||||||
let (attester_map, attestations, _keypairs) = {
|
let (attester_map, attestations, _keypairs) = {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
let attestation_slot = block_slot - 1;
|
let attestation_slot = block_slot - 1;
|
||||||
@ -244,17 +230,18 @@ pub fn run_block_validation_scenario<F>(
|
|||||||
let ssz_block = SszBlock::from_slice(&ssz_bytes[..])
|
let ssz_block = SszBlock::from_slice(&ssz_bytes[..])
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
validate_ssz_block(
|
let context = BlockValidationContext {
|
||||||
&ssz_block,
|
present_slot: validation_slot,
|
||||||
validation_slot,
|
cycle_length: params.cycle_length,
|
||||||
params.cycle_length,
|
last_justified_slot: validation_last_justified_slot,
|
||||||
validation_last_justified_slot,
|
parent_hashes: Arc::new(parent_hashes),
|
||||||
&Arc::new(parent_hashes),
|
proposer_map: Arc::new(proposer_map),
|
||||||
&Arc::new(proposer_map),
|
attester_map: Arc::new(attester_map),
|
||||||
&Arc::new(attester_map),
|
block_store: stores.block.clone(),
|
||||||
&stores.block.clone(),
|
validator_store: stores.validator.clone(),
|
||||||
&stores.validator.clone(),
|
pow_store: stores.pow_chain.clone()
|
||||||
&stores.pow_chain.clone())
|
};
|
||||||
|
context.validate_ssz_block(&ssz_block)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_simple_params() -> TestParams {
|
fn get_simple_params() -> TestParams {
|
||||||
|
@ -60,8 +60,35 @@ pub enum SszBlockValidationError {
|
|||||||
DatabaseError(String),
|
DatabaseError(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Validate some SszBlock. An SszBlock varies from a Block in that is a read-only structure
|
/// The context against which a block should be validated.
|
||||||
/// that reads directly from encoded SSZ.
|
pub struct BlockValidationContext<T>
|
||||||
|
where T: ClientDB + Sized
|
||||||
|
{
|
||||||
|
/// The slot as determined by the system time.
|
||||||
|
pub present_slot: u64,
|
||||||
|
/// The cycle_length as determined by the chain configuration.
|
||||||
|
pub cycle_length: u8,
|
||||||
|
/// The last justified slot as per the client's view of the canonical chain.
|
||||||
|
pub last_justified_slot: u64,
|
||||||
|
/// A vec of the hashes of the blocks preceeding the present slot.
|
||||||
|
pub parent_hashes: Arc<Vec<Hash256>>,
|
||||||
|
/// A map of slots to a block proposer validation index.
|
||||||
|
pub proposer_map: Arc<ProposerMap>,
|
||||||
|
/// A map of (slot, shard_id) to the attestation set of validation indices.
|
||||||
|
pub attester_map: Arc<AttesterMap>,
|
||||||
|
/// The store containing block information.
|
||||||
|
pub block_store: Arc<BlockStore<T>>,
|
||||||
|
/// The store containing validator information.
|
||||||
|
pub validator_store: Arc<ValidatorStore<T>>,
|
||||||
|
/// The store containing information about the proof-of-work chain.
|
||||||
|
pub pow_store: Arc<PoWChainStore<T>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> BlockValidationContext<T>
|
||||||
|
where T: ClientDB
|
||||||
|
{
|
||||||
|
/// Validate some SszBlock against a block validation context. An SszBlock varies from a Block in
|
||||||
|
/// that is a read-only structure that reads directly from encoded SSZ.
|
||||||
///
|
///
|
||||||
/// The reason to validate an SzzBlock is to avoid decoding it in its entirety if there is
|
/// The reason to validate an SzzBlock is to avoid decoding it in its entirety if there is
|
||||||
/// a suspicion that the block might be invalid. Such a suspicion should be applied to
|
/// a suspicion that the block might be invalid. Such a suspicion should be applied to
|
||||||
@ -77,16 +104,7 @@ pub enum SszBlockValidationError {
|
|||||||
/// Note: this function does not implement randao_reveal checking as it is not in the
|
/// Note: this function does not implement randao_reveal checking as it is not in the
|
||||||
/// specification.
|
/// specification.
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn validate_ssz_block<T>(b: &SszBlock,
|
pub fn validate_ssz_block(&self, b: &SszBlock)
|
||||||
expected_slot: u64,
|
|
||||||
cycle_length: u8,
|
|
||||||
last_justified_slot: u64,
|
|
||||||
parent_hashes: &Arc<Vec<Hash256>>,
|
|
||||||
proposer_map: &Arc<ProposerMap>,
|
|
||||||
attester_map: &Arc<AttesterMap>,
|
|
||||||
block_store: &Arc<BlockStore<T>>,
|
|
||||||
validator_store: &Arc<ValidatorStore<T>>,
|
|
||||||
pow_store: &Arc<PoWChainStore<T>>)
|
|
||||||
-> Result<(BlockStatus, Option<Block>), SszBlockValidationError>
|
-> Result<(BlockStatus, Option<Block>), SszBlockValidationError>
|
||||||
where T: ClientDB + Sized
|
where T: ClientDB + Sized
|
||||||
{
|
{
|
||||||
@ -96,7 +114,7 @@ pub fn validate_ssz_block<T>(b: &SszBlock,
|
|||||||
* drop it.
|
* drop it.
|
||||||
*/
|
*/
|
||||||
let block_slot = b.slot_number();
|
let block_slot = b.slot_number();
|
||||||
if block_slot > expected_slot {
|
if block_slot > self.present_slot {
|
||||||
return Err(SszBlockValidationError::FutureSlot);
|
return Err(SszBlockValidationError::FutureSlot);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,7 +122,7 @@ pub fn validate_ssz_block<T>(b: &SszBlock,
|
|||||||
* If this block is already known, return immediately.
|
* If this block is already known, return immediately.
|
||||||
*/
|
*/
|
||||||
let block_hash = &b.block_hash();
|
let block_hash = &b.block_hash();
|
||||||
if block_store.block_exists(&block_hash)? {
|
if self.block_store.block_exists(&block_hash)? {
|
||||||
return Ok((BlockStatus::KnownBlock, None));
|
return Ok((BlockStatus::KnownBlock, None));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,7 +136,7 @@ pub fn validate_ssz_block<T>(b: &SszBlock,
|
|||||||
* it means "sufficienty deep in the canonical PoW chain".
|
* it means "sufficienty deep in the canonical PoW chain".
|
||||||
*/
|
*/
|
||||||
let pow_chain_ref = b.pow_chain_ref();
|
let pow_chain_ref = b.pow_chain_ref();
|
||||||
if !pow_store.block_hash_exists(b.pow_chain_ref())? {
|
if !self.pow_store.block_hash_exists(b.pow_chain_ref())? {
|
||||||
return Err(SszBlockValidationError::UnknownPoWChainRef);
|
return Err(SszBlockValidationError::UnknownPoWChainRef);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,12 +164,12 @@ pub fn validate_ssz_block<T>(b: &SszBlock,
|
|||||||
let attestation_voters = validate_attestation(
|
let attestation_voters = validate_attestation(
|
||||||
&first_attestation,
|
&first_attestation,
|
||||||
block_slot,
|
block_slot,
|
||||||
cycle_length,
|
self.cycle_length,
|
||||||
last_justified_slot,
|
self.last_justified_slot,
|
||||||
&parent_hashes,
|
&self.parent_hashes,
|
||||||
&block_store,
|
&self.block_store,
|
||||||
&validator_store,
|
&self.validator_store,
|
||||||
&attester_map)?;
|
&self.attester_map)?;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If the set of voters is None, the attestation was invalid.
|
* If the set of voters is None, the attestation was invalid.
|
||||||
@ -163,7 +181,7 @@ pub fn validate_ssz_block<T>(b: &SszBlock,
|
|||||||
/*
|
/*
|
||||||
* Read the proposer from the map of slot -> validator index.
|
* Read the proposer from the map of slot -> validator index.
|
||||||
*/
|
*/
|
||||||
let proposer = proposer_map.get(&block_slot)
|
let proposer = self.proposer_map.get(&block_slot)
|
||||||
.ok_or(SszBlockValidationError::BadProposerMap)?;
|
.ok_or(SszBlockValidationError::BadProposerMap)?;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -222,12 +240,12 @@ pub fn validate_ssz_block<T>(b: &SszBlock,
|
|||||||
let result = validate_attestation(
|
let result = validate_attestation(
|
||||||
&attestation,
|
&attestation,
|
||||||
block_slot,
|
block_slot,
|
||||||
cycle_length,
|
self.cycle_length,
|
||||||
last_justified_slot,
|
self.last_justified_slot,
|
||||||
&parent_hashes,
|
&self.parent_hashes,
|
||||||
&block_store,
|
&self.block_store,
|
||||||
&validator_store,
|
&self.validator_store,
|
||||||
&attester_map);
|
&self.attester_map);
|
||||||
match result {
|
match result {
|
||||||
/*
|
/*
|
||||||
* Attestation validation failed with some error.
|
* Attestation validation failed with some error.
|
||||||
@ -276,7 +294,7 @@ pub fn validate_ssz_block<T>(b: &SszBlock,
|
|||||||
* new block in the canonical chain. Otherwise, it's a new block in a fork chain.
|
* new block in the canonical chain. Otherwise, it's a new block in a fork chain.
|
||||||
*/
|
*/
|
||||||
let parent_hash = b.parent_hash();
|
let parent_hash = b.parent_hash();
|
||||||
let status = if block_store.block_exists_in_canonical_chain(&parent_hash)? {
|
let status = if self.block_store.block_exists_in_canonical_chain(&parent_hash)? {
|
||||||
BlockStatus::NewBlockInCanonicalChain
|
BlockStatus::NewBlockInCanonicalChain
|
||||||
} else {
|
} else {
|
||||||
BlockStatus::NewBlockInForkChain
|
BlockStatus::NewBlockInForkChain
|
||||||
@ -292,6 +310,7 @@ pub fn validate_ssz_block<T>(b: &SszBlock,
|
|||||||
};
|
};
|
||||||
Ok((status, Some(block)))
|
Ok((status, Some(block)))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<DBError> for SszBlockValidationError {
|
impl From<DBError> for SszBlockValidationError {
|
||||||
fn from(e: DBError) -> Self {
|
fn from(e: DBError) -> Self {
|
||||||
|
Loading…
Reference in New Issue
Block a user