2018-10-29 19:29:15 +00:00
|
|
|
use super::BeaconChain;
|
|
|
|
use db::stores::BeaconBlockAtSlotError;
|
|
|
|
use db::ClientDB;
|
|
|
|
use ssz_helpers::ssz_beacon_block::SszBeaconBlock;
|
2018-10-23 11:16:26 +00:00
|
|
|
use std::sync::Arc;
|
2018-10-29 19:29:15 +00:00
|
|
|
use types::Hash256;
|
|
|
|
use validation::block_validation::BeaconBlockValidationContext;
|
2018-10-23 11:16:26 +00:00
|
|
|
|
2018-10-25 08:14:43 +00:00
|
|
|
pub enum BlockValidationContextError {
|
2018-10-23 11:16:26 +00:00
|
|
|
UnknownCrystallizedState,
|
|
|
|
UnknownActiveState,
|
|
|
|
UnknownAttesterProposerMaps,
|
|
|
|
NoParentHash,
|
|
|
|
UnknownJustifiedBlock,
|
|
|
|
BlockAlreadyKnown,
|
|
|
|
BlockSlotLookupError(BeaconBlockAtSlotError),
|
|
|
|
}
|
|
|
|
|
2018-10-25 08:14:43 +00:00
|
|
|
impl From<BeaconBlockAtSlotError> for BlockValidationContextError {
|
|
|
|
fn from(e: BeaconBlockAtSlotError) -> BlockValidationContextError {
|
|
|
|
BlockValidationContextError::BlockSlotLookupError(e)
|
2018-10-23 11:16:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> BeaconChain<T>
|
2018-10-29 19:29:15 +00:00
|
|
|
where
|
|
|
|
T: ClientDB + Sized,
|
2018-10-23 11:16:26 +00:00
|
|
|
{
|
2018-10-29 19:29:15 +00:00
|
|
|
pub(crate) fn block_validation_context(
|
|
|
|
&self,
|
|
|
|
block: &SszBeaconBlock,
|
|
|
|
parent_block: &SszBeaconBlock,
|
|
|
|
present_slot: u64,
|
|
|
|
) -> Result<BeaconBlockValidationContext<T>, BlockValidationContextError> {
|
2018-10-23 11:16:26 +00:00
|
|
|
/*
|
|
|
|
* Load the crystallized state for this block from our caches.
|
|
|
|
*
|
|
|
|
* Fail if the crystallized state is unknown.
|
|
|
|
*/
|
2018-10-29 19:29:15 +00:00
|
|
|
let cry_state_root = Hash256::from(parent_block.cry_state_root());
|
|
|
|
let cry_state = self
|
|
|
|
.crystallized_states
|
|
|
|
.get(&cry_state_root)
|
2018-10-25 08:14:43 +00:00
|
|
|
.ok_or(BlockValidationContextError::UnknownCrystallizedState)?;
|
2018-10-23 11:16:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Load the active state for this block from our caches.
|
|
|
|
*
|
|
|
|
* Fail if the active state is unknown.
|
|
|
|
*/
|
2018-10-29 19:29:15 +00:00
|
|
|
let act_state_root = Hash256::from(parent_block.act_state_root());
|
|
|
|
let act_state = self
|
|
|
|
.active_states
|
|
|
|
.get(&act_state_root)
|
2018-10-25 08:14:43 +00:00
|
|
|
.ok_or(BlockValidationContextError::UnknownActiveState)?;
|
2018-10-23 11:16:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Learn the last justified slot from the crystallized state and load
|
|
|
|
* the hash of this block from the database
|
|
|
|
*/
|
|
|
|
let last_justified_slot = cry_state.last_justified_slot;
|
2018-10-29 19:29:15 +00:00
|
|
|
let parent_block_hash = block
|
|
|
|
.parent_hash()
|
2018-10-25 08:14:43 +00:00
|
|
|
.ok_or(BlockValidationContextError::NoParentHash)?;
|
2018-10-29 19:29:15 +00:00
|
|
|
let (last_justified_block_hash, _) = self
|
|
|
|
.store
|
|
|
|
.block
|
|
|
|
.block_at_slot(&parent_block_hash, last_justified_slot)?
|
2018-10-25 08:14:43 +00:00
|
|
|
.ok_or(BlockValidationContextError::UnknownJustifiedBlock)?;
|
2018-10-23 11:16:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Load the attester and proposer maps for the crystallized state.
|
|
|
|
*/
|
2018-10-29 19:29:15 +00:00
|
|
|
let (attester_map, proposer_map) = self
|
|
|
|
.attester_proposer_maps
|
|
|
|
.get(&cry_state_root)
|
2018-10-25 08:14:43 +00:00
|
|
|
.ok_or(BlockValidationContextError::UnknownAttesterProposerMaps)?;
|
2018-10-23 11:16:26 +00:00
|
|
|
|
2018-10-25 08:14:43 +00:00
|
|
|
Ok(BeaconBlockValidationContext {
|
2018-10-23 11:16:26 +00:00
|
|
|
present_slot,
|
|
|
|
cycle_length: self.config.cycle_length,
|
|
|
|
last_justified_slot: cry_state.last_justified_slot,
|
|
|
|
last_justified_block_hash: Hash256::from(&last_justified_block_hash[..]),
|
|
|
|
last_finalized_slot: self.last_finalized_slot,
|
|
|
|
recent_block_hashes: Arc::new(act_state.recent_block_hashes.clone()),
|
|
|
|
proposer_map: proposer_map.clone(),
|
|
|
|
attester_map: attester_map.clone(),
|
|
|
|
block_store: self.store.block.clone(),
|
|
|
|
validator_store: self.store.validator.clone(),
|
|
|
|
pow_store: self.store.pow_chain.clone(),
|
2018-10-25 08:14:43 +00:00
|
|
|
})
|
2018-10-23 11:16:26 +00:00
|
|
|
}
|
|
|
|
}
|