lighthouse-pulse/lighthouse/state/validation/ssz_block.rs

55 lines
1.3 KiB
Rust
Raw Normal View History

2018-09-21 01:14:28 +00:00
use super::block::SszBlock;
use super::Logger;
use super::db::{
2018-09-22 01:13:55 +00:00
ClientDB,
DBError,
};
use super::db::stores::{
2018-09-21 01:14:28 +00:00
BlockStore,
PoWChainStore,
2018-09-22 01:13:55 +00:00
ValidatorStore,
2018-09-21 01:14:28 +00:00
};
pub enum BlockStatus {
NewBlock,
KnownBlock,
}
pub enum SszBlockValidationError {
FutureSlot,
2018-09-22 01:13:55 +00:00
UnknownPoWChainRef,
DatabaseError(String),
2018-09-21 01:14:28 +00:00
}
2018-09-22 01:13:55 +00:00
impl From<DBError> for SszBlockValidationError {
fn from(e: DBError) -> SszBlockValidationError {
SszBlockValidationError::DatabaseError(e.message)
}
2018-09-21 01:14:28 +00:00
}
#[allow(dead_code)]
2018-09-22 01:13:55 +00:00
pub fn validate_ssz_block<T>(b: &SszBlock,
expected_slot: u64,
block_store: &BlockStore<T>,
pow_store: &PoWChainStore<T>,
_validator_store: &ValidatorStore<T>,
_log: &Logger)
2018-09-21 01:14:28 +00:00
-> Result<BlockStatus, SszBlockValidationError>
2018-09-22 01:13:55 +00:00
where T: Sized + ClientDB
2018-09-21 01:14:28 +00:00
{
2018-09-22 01:13:55 +00:00
if block_store.block_exists(&b.block_hash())? {
return Ok(BlockStatus::KnownBlock);
}
2018-09-21 01:14:28 +00:00
2018-09-22 01:13:55 +00:00
if b.slot_number() > expected_slot {
return Err(SszBlockValidationError::FutureSlot);
}
2018-09-21 01:14:28 +00:00
2018-09-22 01:13:55 +00:00
if pow_store.block_hash_exists(b.pow_chain_ref())? == false {
return Err(SszBlockValidationError::UnknownPoWChainRef);
}
2018-09-21 01:14:28 +00:00
// Do validation here
Ok(BlockStatus::NewBlock)
}