Update validation as per new spec

- Block -> BeaconBlock
- Updates to SszBeaconBlock
This commit is contained in:
Paul Hauner 2018-10-16 13:59:45 +11:00
parent c45e05ca02
commit c3d88a7e80
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
5 changed files with 143 additions and 129 deletions

View File

@ -13,8 +13,8 @@ use super::db::{
DBError DBError
}; };
use super::db::stores::{ use super::db::stores::{
BlockStore, BeaconBlockStore,
BlockAtSlotError, BeaconBlockAtSlotError,
ValidatorStore, ValidatorStore,
}; };
use super::types::{ use super::types::{
@ -64,7 +64,7 @@ pub struct AttestationValidationContext<T>
/// A vec of the hashes of the blocks preceeding the present slot. /// A vec of the hashes of the blocks preceeding the present slot.
pub parent_hashes: Arc<Vec<Hash256>>, pub parent_hashes: Arc<Vec<Hash256>>,
/// The store containing block information. /// The store containing block information.
pub block_store: Arc<BlockStore<T>>, pub block_store: Arc<BeaconBlockStore<T>>,
/// The store containing validator information. /// The store containing validator information.
pub validator_store: Arc<ValidatorStore<T>>, pub validator_store: Arc<ValidatorStore<T>>,
/// A map of (slot, shard_id) to the attestation set of validation indices. /// A map of (slot, shard_id) to the attestation set of validation indices.
@ -223,10 +223,10 @@ impl From<ParentHashesError> for AttestationValidationError {
} }
} }
impl From<BlockAtSlotError> for AttestationValidationError { impl From<BeaconBlockAtSlotError> for AttestationValidationError {
fn from(e: BlockAtSlotError) -> Self { fn from(e: BeaconBlockAtSlotError) -> Self {
match e { match e {
BlockAtSlotError::DBError(s) => AttestationValidationError::DBError(s), BeaconBlockAtSlotError::DBError(s) => AttestationValidationError::DBError(s),
_ => AttestationValidationError::InvalidJustifiedBlockHash _ => AttestationValidationError::InvalidJustifiedBlockHash
} }

View File

@ -13,7 +13,7 @@ use super::attestation_validation::{
use super::types::{ use super::types::{
AttestationRecord, AttestationRecord,
AttesterMap, AttesterMap,
Block, BeaconBlock,
ProposerMap, ProposerMap,
}; };
use super::ssz_helpers::attestation_ssz_splitter::{ use super::ssz_helpers::attestation_ssz_splitter::{
@ -21,16 +21,16 @@ use super::ssz_helpers::attestation_ssz_splitter::{
split_all_attestations, split_all_attestations,
AttestationSplitError, AttestationSplitError,
}; };
use super::ssz_helpers::ssz_block::{ use super::ssz_helpers::ssz_beacon_block::{
SszBlock, SszBeaconBlock,
SszBlockError, SszBeaconBlockError,
}; };
use super::db::{ use super::db::{
ClientDB, ClientDB,
DBError, DBError,
}; };
use super::db::stores::{ use super::db::stores::{
BlockStore, BeaconBlockStore,
PoWChainStore, PoWChainStore,
ValidatorStore, ValidatorStore,
}; };
@ -41,18 +41,20 @@ use super::ssz::{
use super::types::Hash256; use super::types::Hash256;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum BlockStatus { pub enum BeaconBlockStatus {
NewBlock, NewBlock,
KnownBlock, KnownBlock,
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum SszBlockValidationError { pub enum SszBeaconBlockValidationError {
FutureSlot, FutureSlot,
SlotAlreadyFinalized, SlotAlreadyFinalized,
UnknownPoWChainRef, UnknownPoWChainRef,
UnknownParentHash, UnknownParentHash,
BadAttestationSsz, BadAttestationSsz,
BadAncestorHashesSsz,
BadSpecialsSsz,
ParentSlotHigherThanBlockSlot, ParentSlotHigherThanBlockSlot,
AttestationValidationError(AttestationValidationError), AttestationValidationError(AttestationValidationError),
AttestationSignatureFailed, AttestationSignatureFailed,
@ -64,7 +66,7 @@ pub enum SszBlockValidationError {
} }
/// The context against which a block should be validated. /// The context against which a block should be validated.
pub struct BlockValidationContext<T> pub struct BeaconBlockValidationContext<T>
where T: ClientDB + Sized where T: ClientDB + Sized
{ {
/// The slot as determined by the system time. /// The slot as determined by the system time.
@ -84,20 +86,20 @@ pub struct BlockValidationContext<T>
/// A map of (slot, shard_id) to the attestation set of validation indices. /// A map of (slot, shard_id) to the attestation set of validation indices.
pub attester_map: Arc<AttesterMap>, pub attester_map: Arc<AttesterMap>,
/// The store containing block information. /// The store containing block information.
pub block_store: Arc<BlockStore<T>>, pub block_store: Arc<BeaconBlockStore<T>>,
/// The store containing validator information. /// The store containing validator information.
pub validator_store: Arc<ValidatorStore<T>>, pub validator_store: Arc<ValidatorStore<T>>,
/// The store containing information about the proof-of-work chain. /// The store containing information about the proof-of-work chain.
pub pow_store: Arc<PoWChainStore<T>>, pub pow_store: Arc<PoWChainStore<T>>,
} }
impl<T> BlockValidationContext<T> impl<T> BeaconBlockValidationContext<T>
where T: ClientDB where T: ClientDB
{ {
/// Validate some SszBlock against a block validation context. An SszBlock varies from a Block in /// Validate some SszBeaconBlock against a block validation context. An SszBeaconBlock varies from a BeaconBlock in
/// that is a read-only structure that reads directly from encoded SSZ. /// 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 SzzBeaconBlock 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
/// all blocks coming from the network. /// all blocks coming from the network.
/// ///
@ -107,8 +109,8 @@ impl<T> BlockValidationContext<T>
/// 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(&self, b: &SszBlock) pub fn validate_ssz_block(&self, b: &SszBeaconBlock)
-> Result<(BlockStatus, Option<Block>), SszBlockValidationError> -> Result<(BeaconBlockStatus, Option<BeaconBlock>), SszBeaconBlockValidationError>
where T: ClientDB + Sized where T: ClientDB + Sized
{ {
@ -118,7 +120,7 @@ impl<T> BlockValidationContext<T>
*/ */
let block_hash = &b.block_hash(); let block_hash = &b.block_hash();
if self.block_store.block_exists(&block_hash)? { if self.block_store.block_exists(&block_hash)? {
return Ok((BlockStatus::KnownBlock, None)); return Ok((BeaconBlockStatus::KnownBlock, None));
} }
/* /*
@ -127,9 +129,9 @@ impl<T> BlockValidationContext<T>
* It is up to the calling fn to determine what should be done with "future" blocks (e.g., * It is up to the calling fn to determine what should be done with "future" blocks (e.g.,
* cache or discard). * cache or discard).
*/ */
let block_slot = b.slot_number(); let block_slot = b.slot();
if block_slot > self.present_slot { if block_slot > self.present_slot {
return Err(SszBlockValidationError::FutureSlot); return Err(SszBeaconBlockValidationError::FutureSlot);
} }
/* /*
@ -143,7 +145,7 @@ impl<T> BlockValidationContext<T>
* `last_finalized_block` in it's chain. * `last_finalized_block` in it's chain.
*/ */
if block_slot <= self.last_finalized_slot { if block_slot <= self.last_finalized_slot {
return Err(SszBlockValidationError::SlotAlreadyFinalized); return Err(SszBeaconBlockValidationError::SlotAlreadyFinalized);
} }
/* /*
@ -155,15 +157,15 @@ impl<T> BlockValidationContext<T>
* "sufficienty deep in the canonical PoW chain". This should be clarified as the spec * "sufficienty deep in the canonical PoW chain". This should be clarified as the spec
* crystallizes. * crystallizes.
*/ */
let pow_chain_ref = b.pow_chain_ref(); let pow_chain_reference = b.pow_chain_reference();
if !self.pow_store.block_hash_exists(b.pow_chain_ref())? { if !self.pow_store.block_hash_exists(b.pow_chain_reference())? {
return Err(SszBlockValidationError::UnknownPoWChainRef); return Err(SszBeaconBlockValidationError::UnknownPoWChainRef);
} }
/* /*
* Store a slice of the serialized attestations from the block SSZ. * Store a slice of the serialized attestations from the block SSZ.
*/ */
let attestations_ssz = &b.attestations(); let attestations_ssz = &b.attestations_without_length();
/* /*
* Get a slice of the first serialized attestation (the 0'th) and decode it into * Get a slice of the first serialized attestation (the 0'th) and decode it into
@ -185,7 +187,7 @@ impl<T> BlockValidationContext<T>
* of the previous block is attesting to some other block than the one they produced. * of the previous block is attesting to some other block than the one they produced.
*/ */
if first_attestation.oblique_parent_hashes.len() > 0 { if first_attestation.oblique_parent_hashes.len() > 0 {
return Err(SszBlockValidationError::ProposerAttestationHasObliqueHashes); return Err(SszBeaconBlockValidationError::ProposerAttestationHasObliqueHashes);
} }
/* /*
@ -196,12 +198,13 @@ impl<T> BlockValidationContext<T>
* *
* Also, read the slot from the parent block for later use. * Also, read the slot from the parent block for later use.
*/ */
let parent_hash = b.parent_hash(); let parent_hash = b.parent_hash()
.ok_or(SszBeaconBlockValidationError::BadAncestorHashesSsz)?;
let parent_block_slot = match self.block_store.get_serialized_block(&parent_hash)? { let parent_block_slot = match self.block_store.get_serialized_block(&parent_hash)? {
None => return Err(SszBlockValidationError::UnknownParentHash), None => return Err(SszBeaconBlockValidationError::UnknownParentHash),
Some(ssz) => { Some(ssz) => {
let parent_block = SszBlock::from_slice(&ssz[..])?; let parent_block = SszBeaconBlock::from_slice(&ssz[..])?;
parent_block.slot_number() parent_block.slot()
} }
}; };
@ -211,7 +214,7 @@ impl<T> BlockValidationContext<T>
* In other words, the parent must come before the child. * In other words, the parent must come before the child.
*/ */
if parent_block_slot >= block_slot { if parent_block_slot >= block_slot {
return Err(SszBlockValidationError::ParentSlotHigherThanBlockSlot); return Err(SszBeaconBlockValidationError::ParentSlotHigherThanBlockSlot);
} }
/* /*
@ -242,9 +245,9 @@ impl<T> BlockValidationContext<T>
* attestation of this block, reject the block. * attestation of this block, reject the block.
*/ */
let parent_block_proposer = self.proposer_map.get(&parent_block_slot) let parent_block_proposer = self.proposer_map.get(&parent_block_slot)
.ok_or(SszBlockValidationError::BadProposerMap)?; .ok_or(SszBeaconBlockValidationError::BadProposerMap)?;
if !attestation_voters.contains(&parent_block_proposer) { if !attestation_voters.contains(&parent_block_proposer) {
return Err(SszBlockValidationError::NoProposerSignature); return Err(SszBeaconBlockValidationError::NoProposerSignature);
} }
/* /*
@ -265,7 +268,7 @@ impl<T> BlockValidationContext<T>
* validation. This is so all attestation validation is halted if a single bad attestation * validation. This is so all attestation validation is halted if a single bad attestation
* is found. * is found.
*/ */
let failure: RwLock<Option<SszBlockValidationError>> = RwLock::new(None); let failure: RwLock<Option<SszBeaconBlockValidationError>> = RwLock::new(None);
let mut deserialized_attestations: Vec<AttestationRecord> = other_attestations let mut deserialized_attestations: Vec<AttestationRecord> = other_attestations
.par_iter() .par_iter()
.filter_map(|attestation_ssz| { .filter_map(|attestation_ssz| {
@ -286,7 +289,7 @@ impl<T> BlockValidationContext<T>
*/ */
Err(e) => { Err(e) => {
let mut failure = failure.write().unwrap(); let mut failure = failure.write().unwrap();
*failure = Some(SszBlockValidationError::from(e)); *failure = Some(SszBeaconBlockValidationError::from(e));
None None
} }
/* /*
@ -299,7 +302,7 @@ impl<T> BlockValidationContext<T>
*/ */
Err(e) => { Err(e) => {
let mut failure = failure.write().unwrap(); let mut failure = failure.write().unwrap();
*failure = Some(SszBlockValidationError::from(e)); *failure = Some(SszBeaconBlockValidationError::from(e));
None None
} }
/* /*
@ -313,7 +316,7 @@ impl<T> BlockValidationContext<T>
.collect(); .collect();
match failure.into_inner() { match failure.into_inner() {
Err(_) => return Err(SszBlockValidationError::RwLockPoisoned), Err(_) => return Err(SszBeaconBlockValidationError::RwLockPoisoned),
Ok(failure) => { Ok(failure) => {
match failure { match failure {
Some(error) => return Err(error), Some(error) => return Err(error),
@ -329,63 +332,69 @@ impl<T> BlockValidationContext<T>
*/ */
deserialized_attestations.insert(0, first_attestation); deserialized_attestations.insert(0, first_attestation);
let (ancestor_hashes, _) = Decodable::ssz_decode(&b.ancestor_hashes(), 0)
.map_err(|_| SszBeaconBlockValidationError::BadAncestorHashesSsz)?;
let (specials, _) = Decodable::ssz_decode(&b.specials(), 0)
.map_err(|_| SszBeaconBlockValidationError::BadSpecialsSsz)?;
/* /*
* If we have reached this point, the block is a new valid block that is worthy of * If we have reached this point, the block is a new valid block that is worthy of
* processing. * processing.
*/ */
let block = Block { let block = BeaconBlock {
parent_hash: Hash256::from(parent_hash), slot: block_slot,
slot_number: block_slot,
randao_reveal: Hash256::from(b.randao_reveal()), randao_reveal: Hash256::from(b.randao_reveal()),
attestations: deserialized_attestations, pow_chain_reference: Hash256::from(pow_chain_reference),
pow_chain_ref: Hash256::from(pow_chain_ref), ancestor_hashes,
active_state_root: Hash256::from(b.act_state_root()), active_state_root: Hash256::from(b.act_state_root()),
crystallized_state_root: Hash256::from(b.cry_state_root()), crystallized_state_root: Hash256::from(b.cry_state_root()),
attestations: deserialized_attestations,
specials,
}; };
Ok((BlockStatus::NewBlock, Some(block))) Ok((BeaconBlockStatus::NewBlock, Some(block)))
} }
} }
impl From<DBError> for SszBlockValidationError { impl From<DBError> for SszBeaconBlockValidationError {
fn from(e: DBError) -> Self { fn from(e: DBError) -> Self {
SszBlockValidationError::DBError(e.message) SszBeaconBlockValidationError::DBError(e.message)
} }
} }
impl From<AttestationSplitError> for SszBlockValidationError { impl From<AttestationSplitError> for SszBeaconBlockValidationError {
fn from(e: AttestationSplitError) -> Self { fn from(e: AttestationSplitError) -> Self {
match e { match e {
AttestationSplitError::TooShort => AttestationSplitError::TooShort =>
SszBlockValidationError::BadAttestationSsz SszBeaconBlockValidationError::BadAttestationSsz
} }
} }
} }
impl From<SszBlockError> for SszBlockValidationError { impl From<SszBeaconBlockError> for SszBeaconBlockValidationError {
fn from(e: SszBlockError) -> Self { fn from(e: SszBeaconBlockError) -> Self {
match e { match e {
SszBlockError::TooShort => SszBeaconBlockError::TooShort =>
SszBlockValidationError::DBError("Bad parent block in db.".to_string()), SszBeaconBlockValidationError::DBError("Bad parent block in db.".to_string()),
SszBlockError::TooLong => SszBeaconBlockError::TooLong =>
SszBlockValidationError::DBError("Bad parent block in db.".to_string()), SszBeaconBlockValidationError::DBError("Bad parent block in db.".to_string()),
} }
} }
} }
impl From<DecodeError> for SszBlockValidationError { impl From<DecodeError> for SszBeaconBlockValidationError {
fn from(e: DecodeError) -> Self { fn from(e: DecodeError) -> Self {
match e { match e {
DecodeError::TooShort => DecodeError::TooShort =>
SszBlockValidationError::BadAttestationSsz, SszBeaconBlockValidationError::BadAttestationSsz,
DecodeError::TooLong => DecodeError::TooLong =>
SszBlockValidationError::BadAttestationSsz, SszBeaconBlockValidationError::BadAttestationSsz,
} }
} }
} }
impl From<AttestationValidationError> for SszBlockValidationError { impl From<AttestationValidationError> for SszBeaconBlockValidationError {
fn from(e: AttestationValidationError) -> Self { fn from(e: AttestationValidationError) -> Self {
SszBlockValidationError::AttestationValidationError(e) SszBeaconBlockValidationError::AttestationValidationError(e)
} }
} }

View File

@ -5,13 +5,13 @@ use super::db::{
}; };
use super::db::stores::{ use super::db::stores::{
ValidatorStore, ValidatorStore,
BlockStore, BeaconBlockStore,
}; };
use super::types::{ use super::types::{
AttestationRecord, AttestationRecord,
AttesterMap, AttesterMap,
Bitfield, Bitfield,
Block, BeaconBlock,
Hash256, Hash256,
}; };
use super::validation::attestation_validation::{ use super::validation::attestation_validation::{
@ -32,14 +32,14 @@ use super::hashing::{
pub struct TestStore { pub struct TestStore {
pub db: Arc<MemoryDB>, pub db: Arc<MemoryDB>,
pub validator: Arc<ValidatorStore<MemoryDB>>, pub validator: Arc<ValidatorStore<MemoryDB>>,
pub block: Arc<BlockStore<MemoryDB>>, pub block: Arc<BeaconBlockStore<MemoryDB>>,
} }
impl TestStore { impl TestStore {
pub fn new() -> Self { pub fn new() -> Self {
let db = Arc::new(MemoryDB::open()); let db = Arc::new(MemoryDB::open());
let validator = Arc::new(ValidatorStore::new(db.clone())); let validator = Arc::new(ValidatorStore::new(db.clone()));
let block = Arc::new(BlockStore::new(db.clone())); let block = Arc::new(BeaconBlockStore::new(db.clone()));
Self { Self {
db, db,
validator, validator,
@ -81,7 +81,7 @@ pub fn generate_attestation(shard_id: u16,
cycle_length: u8, cycle_length: u8,
parent_hashes: &[Hash256], parent_hashes: &[Hash256],
signing_keys: &[Option<SecretKey>], signing_keys: &[Option<SecretKey>],
block_store: &BlockStore<MemoryDB>) block_store: &BeaconBlockStore<MemoryDB>)
-> AttestationRecord -> AttestationRecord
{ {
let mut attester_bitfield = Bitfield::new(); let mut attester_bitfield = Bitfield::new();
@ -136,10 +136,10 @@ pub fn generate_attestation(shard_id: u16,
/// Create a minimum viable block at some slot. /// Create a minimum viable block at some slot.
/// ///
/// Allows the validation function to read the block and verify its slot. /// Allows the validation function to read the block and verify its slot.
pub fn create_block_at_slot(block_store: &BlockStore<MemoryDB>, hash: &Hash256, slot: u64) { pub fn create_block_at_slot(block_store: &BeaconBlockStore<MemoryDB>, hash: &Hash256, slot: u64) {
let mut justified_block = Block::zero(); let mut justified_block = BeaconBlock::zero();
justified_block.attestations.push(AttestationRecord::zero()); justified_block.attestations.push(AttestationRecord::zero());
justified_block.slot_number = slot; justified_block.slot = slot;
let mut s = SszStream::new(); let mut s = SszStream::new();
s.append(&justified_block); s.append(&justified_block);
let justified_block_ssz = s.drain(); let justified_block_ssz = s.drain();

View File

@ -11,29 +11,29 @@ use super::db::{
MemoryDB, MemoryDB,
}; };
use super::db::stores::{ use super::db::stores::{
BlockStore, BeaconBlockStore,
PoWChainStore, PoWChainStore,
ValidatorStore, ValidatorStore,
}; };
use super::types::{ use super::types::{
AttestationRecord, AttestationRecord,
AttesterMap, AttesterMap,
Block, BeaconBlock,
Hash256, Hash256,
ProposerMap, ProposerMap,
}; };
use super::ssz_helpers::ssz_block::SszBlock; use super::ssz_helpers::ssz_beacon_block::SszBeaconBlock;
use super::validation::block_validation::{ use super::validation::block_validation::{
BlockValidationContext, BeaconBlockValidationContext,
SszBlockValidationError, SszBeaconBlockValidationError,
BlockStatus, BeaconBlockStatus,
}; };
use super::ssz::{ use super::ssz::{
SszStream, SszStream,
}; };
#[derive(Debug)] #[derive(Debug)]
pub struct BlockTestParams { pub struct BeaconBlockTestParams {
pub total_validators: usize, pub total_validators: usize,
pub cycle_length: u8, pub cycle_length: u8,
pub shard_count: u16, pub shard_count: u16,
@ -50,7 +50,7 @@ pub struct BlockTestParams {
pub struct TestStore { pub struct TestStore {
pub db: Arc<MemoryDB>, pub db: Arc<MemoryDB>,
pub block: Arc<BlockStore<MemoryDB>>, pub block: Arc<BeaconBlockStore<MemoryDB>>,
pub pow_chain: Arc<PoWChainStore<MemoryDB>>, pub pow_chain: Arc<PoWChainStore<MemoryDB>>,
pub validator: Arc<ValidatorStore<MemoryDB>>, pub validator: Arc<ValidatorStore<MemoryDB>>,
} }
@ -58,7 +58,7 @@ pub struct TestStore {
impl TestStore { impl TestStore {
pub fn new() -> Self { pub fn new() -> Self {
let db = Arc::new(MemoryDB::open()); let db = Arc::new(MemoryDB::open());
let block = Arc::new(BlockStore::new(db.clone())); let block = Arc::new(BeaconBlockStore::new(db.clone()));
let pow_chain = Arc::new(PoWChainStore::new(db.clone())); let pow_chain = Arc::new(PoWChainStore::new(db.clone()));
let validator = Arc::new(ValidatorStore::new(db.clone())); let validator = Arc::new(ValidatorStore::new(db.clone()));
Self { Self {
@ -74,8 +74,8 @@ 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: &BlockTestParams) pub fn setup_block_validation_scenario(params: &BeaconBlockTestParams)
-> (Block, ParentHashes, AttesterMap, ProposerMap, TestStore) -> (BeaconBlock, ParentHashes, AttesterMap, ProposerMap, TestStore)
{ {
let stores = TestStore::new(); let stores = TestStore::new();
@ -89,6 +89,7 @@ pub fn setup_block_validation_scenario(params: &BlockTestParams)
.map(|i| Hash256::from(i as u64)) .map(|i| Hash256::from(i as u64))
.collect(); .collect();
let parent_hash = Hash256::from("parent_hash".as_bytes()); let parent_hash = Hash256::from("parent_hash".as_bytes());
let ancestor_hashes = vec![parent_hash.clone(); 32];
let randao_reveal = Hash256::from("randao_reveal".as_bytes()); let randao_reveal = Hash256::from("randao_reveal".as_bytes());
let justified_block_hash = Hash256::from("justified_hash".as_bytes()); let justified_block_hash = Hash256::from("justified_hash".as_bytes());
let pow_chain_ref = Hash256::from("pow_chain".as_bytes()); let pow_chain_ref = Hash256::from("pow_chain".as_bytes());
@ -104,16 +105,16 @@ pub fn setup_block_validation_scenario(params: &BlockTestParams)
/* /*
* Generate a minimum viable parent block and store it in the database. * Generate a minimum viable parent block and store it in the database.
*/ */
let mut parent_block = Block::zero(); let mut parent_block = BeaconBlock::zero();
let parent_attestation = AttestationRecord::zero(); let parent_attestation = AttestationRecord::zero();
parent_block.slot_number = block_slot - 1; parent_block.slot = block_slot - 1;
parent_block.attestations.push(parent_attestation); parent_block.attestations.push(parent_attestation);
let parent_block_ssz = serialize_block(&parent_block); 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 proposer_map = {
let mut proposer_map = ProposerMap::new(); let mut proposer_map = ProposerMap::new();
proposer_map.insert(parent_block.slot_number, params.parent_proposer_index); proposer_map.insert(parent_block.slot, params.parent_proposer_index);
proposer_map proposer_map
}; };
@ -168,14 +169,15 @@ pub fn setup_block_validation_scenario(params: &BlockTestParams)
(attester_map, attestations, keypairs) (attester_map, attestations, keypairs)
}; };
let block = Block { let block = BeaconBlock {
parent_hash, slot: block_slot,
slot_number: block_slot,
randao_reveal, randao_reveal,
attestations, pow_chain_reference: pow_chain_ref,
pow_chain_ref, ancestor_hashes,
active_state_root, active_state_root,
crystallized_state_root, crystallized_state_root,
attestations,
specials: vec![],
}; };
(block, (block,
@ -185,8 +187,8 @@ pub fn setup_block_validation_scenario(params: &BlockTestParams)
stores) stores)
} }
/// Helper function to take some Block and SSZ serialize it. /// Helper function to take some BeaconBlock and SSZ serialize it.
pub fn serialize_block(b: &Block) -> Vec<u8> { pub fn serialize_block(b: &BeaconBlock) -> Vec<u8> {
let mut stream = SszStream::new(); let mut stream = SszStream::new();
stream.append(b); stream.append(b);
stream.drain() stream.drain()
@ -196,11 +198,11 @@ 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: &BlockTestParams, params: &BeaconBlockTestParams,
mutator_func: F) mutator_func: F)
-> Result<(BlockStatus, Option<Block>), SszBlockValidationError> -> Result<(BeaconBlockStatus, Option<BeaconBlock>), SszBeaconBlockValidationError>
where F: FnOnce(Block, AttesterMap, ProposerMap, TestStore) where F: FnOnce(BeaconBlock, AttesterMap, ProposerMap, TestStore)
-> (Block, AttesterMap, ProposerMap, TestStore) -> (BeaconBlock, AttesterMap, ProposerMap, TestStore)
{ {
let (block, let (block,
parent_hashes, parent_hashes,
@ -214,10 +216,10 @@ pub fn run_block_validation_scenario<F>(
stores) = mutator_func(block, attester_map, proposer_map, stores); stores) = mutator_func(block, attester_map, proposer_map, stores);
let ssz_bytes = serialize_block(&block); let ssz_bytes = serialize_block(&block);
let ssz_block = SszBlock::from_slice(&ssz_bytes[..]) let ssz_block = SszBeaconBlock::from_slice(&ssz_bytes[..])
.unwrap(); .unwrap();
let context = BlockValidationContext { let context = BeaconBlockValidationContext {
present_slot: params.validation_context_slot, present_slot: params.validation_context_slot,
cycle_length: params.cycle_length, cycle_length: params.cycle_length,
last_justified_slot: params.validation_context_justified_slot, last_justified_slot: params.validation_context_justified_slot,

View File

@ -2,27 +2,27 @@ use super::bls::{
AggregateSignature, AggregateSignature,
}; };
use super::helpers::{ use super::helpers::{
BlockTestParams, BeaconBlockTestParams,
TestStore, TestStore,
run_block_validation_scenario, run_block_validation_scenario,
serialize_block, serialize_block,
}; };
use super::types::{ use super::types::{
Block, BeaconBlock,
Hash256, Hash256,
ProposerMap, ProposerMap,
}; };
use super::ssz_helpers::ssz_block::SszBlock; use super::ssz_helpers::ssz_beacon_block::SszBeaconBlock;
use super::validation::block_validation::{ use super::validation::block_validation::{
SszBlockValidationError, SszBeaconBlockValidationError,
BlockStatus, BeaconBlockStatus,
}; };
use super::validation::attestation_validation::{ use super::validation::attestation_validation::{
AttestationValidationError, AttestationValidationError,
}; };
use super::hashing::canonical_hash; use super::hashing::canonical_hash;
fn get_simple_params() -> BlockTestParams { fn get_simple_params() -> BeaconBlockTestParams {
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;
@ -37,7 +37,7 @@ fn get_simple_params() -> BlockTestParams {
let validation_context_justified_block_hash = Hash256::from("justified_hash".as_bytes()); let validation_context_justified_block_hash = Hash256::from("justified_hash".as_bytes());
let validation_context_finalized_slot = 0; let validation_context_finalized_slot = 0;
BlockTestParams { BeaconBlockTestParams {
total_validators, total_validators,
cycle_length, cycle_length,
shard_count, shard_count,
@ -59,7 +59,7 @@ fn get_simple_params() -> BlockTestParams {
fn test_block_validation_valid() { fn test_block_validation_valid() {
let params = get_simple_params(); let params = get_simple_params();
let mutator = |block: Block, attester_map, proposer_map, stores| { let mutator = |block: BeaconBlock, attester_map, proposer_map, stores| {
/* /*
* Do not mutate * Do not mutate
*/ */
@ -70,14 +70,14 @@ fn test_block_validation_valid() {
&params, &params,
mutator); mutator);
assert_eq!(status.unwrap().0, BlockStatus::NewBlock); assert_eq!(status.unwrap().0, BeaconBlockStatus::NewBlock);
} }
#[test] #[test]
fn test_block_validation_valid_known_block() { fn test_block_validation_valid_known_block() {
let params = get_simple_params(); let params = get_simple_params();
let mutator = |block: Block, attester_map, proposer_map, stores: TestStore| { let mutator = |block: BeaconBlock, attester_map, proposer_map, stores: TestStore| {
/* /*
* Pre-store the block in the database * Pre-store the block in the database
*/ */
@ -91,15 +91,15 @@ fn test_block_validation_valid_known_block() {
&params, &params,
mutator); mutator);
assert_eq!(status.unwrap(), (BlockStatus::KnownBlock, None)); assert_eq!(status.unwrap(), (BeaconBlockStatus::KnownBlock, None));
} }
#[test] #[test]
fn test_block_validation_parent_slot_too_high() { fn test_block_validation_parent_slot_too_high() {
let params = get_simple_params(); let params = get_simple_params();
let mutator = |mut block: Block, attester_map, proposer_map, stores| { let mutator = |mut block: BeaconBlock, attester_map, proposer_map, stores| {
block.slot_number = params.validation_context_justified_slot + 1; block.slot = params.validation_context_justified_slot + 1;
(block, attester_map, proposer_map, stores) (block, attester_map, proposer_map, stores)
}; };
@ -107,15 +107,15 @@ fn test_block_validation_parent_slot_too_high() {
&params, &params,
mutator); mutator);
assert_eq!(status, Err(SszBlockValidationError::ParentSlotHigherThanBlockSlot)); assert_eq!(status, Err(SszBeaconBlockValidationError::ParentSlotHigherThanBlockSlot));
} }
#[test] #[test]
fn test_block_validation_invalid_future_slot() { fn test_block_validation_invalid_future_slot() {
let params = get_simple_params(); let params = get_simple_params();
let mutator = |mut block: Block, attester_map, proposer_map, stores| { let mutator = |mut block: BeaconBlock, attester_map, proposer_map, stores| {
block.slot_number = block.slot_number + 1; block.slot = block.slot + 1;
(block, attester_map, proposer_map, stores) (block, attester_map, proposer_map, stores)
}; };
@ -123,7 +123,7 @@ fn test_block_validation_invalid_future_slot() {
&params, &params,
mutator); mutator);
assert_eq!(status, Err(SszBlockValidationError::FutureSlot)); assert_eq!(status, Err(SszBeaconBlockValidationError::FutureSlot));
} }
#[test] #[test]
@ -145,15 +145,15 @@ fn test_block_validation_invalid_slot_already_finalized() {
&params, &params,
mutator); mutator);
assert_eq!(status, Err(SszBlockValidationError::SlotAlreadyFinalized)); assert_eq!(status, Err(SszBeaconBlockValidationError::SlotAlreadyFinalized));
} }
#[test] #[test]
fn test_block_validation_invalid_unknown_pow_hash() { fn test_block_validation_invalid_unknown_pow_hash() {
let params = get_simple_params(); let params = get_simple_params();
let mutator = |mut block: Block, attester_map, proposer_map, stores| { let mutator = |mut block: BeaconBlock, attester_map, proposer_map, stores| {
block.pow_chain_ref = Hash256::from("unknown pow hash".as_bytes()); block.pow_chain_reference = Hash256::from("unknown pow hash".as_bytes());
(block, attester_map, proposer_map, stores) (block, attester_map, proposer_map, stores)
}; };
@ -161,15 +161,15 @@ fn test_block_validation_invalid_unknown_pow_hash() {
&params, &params,
mutator); mutator);
assert_eq!(status, Err(SszBlockValidationError::UnknownPoWChainRef)); assert_eq!(status, Err(SszBeaconBlockValidationError::UnknownPoWChainRef));
} }
#[test] #[test]
fn test_block_validation_invalid_unknown_parent_hash() { fn test_block_validation_invalid_unknown_parent_hash() {
let params = get_simple_params(); let params = get_simple_params();
let mutator = |mut block: Block, attester_map, proposer_map, stores| { let mutator = |mut block: BeaconBlock, attester_map, proposer_map, stores| {
block.parent_hash = Hash256::from("unknown parent block".as_bytes()); block.ancestor_hashes[0] = Hash256::from("unknown parent block".as_bytes());
(block, attester_map, proposer_map, stores) (block, attester_map, proposer_map, stores)
}; };
@ -177,14 +177,14 @@ fn test_block_validation_invalid_unknown_parent_hash() {
&params, &params,
mutator); mutator);
assert_eq!(status, Err(SszBlockValidationError::UnknownParentHash)); assert_eq!(status, Err(SszBeaconBlockValidationError::UnknownParentHash));
} }
#[test] #[test]
fn test_block_validation_invalid_1st_attestation_signature() { fn test_block_validation_invalid_1st_attestation_signature() {
let params = get_simple_params(); let params = get_simple_params();
let mutator = |mut block: Block, attester_map, proposer_map, stores| { let mutator = |mut block: BeaconBlock, attester_map, proposer_map, stores| {
/* /*
* Set the second attestaion record to have an invalid signature. * Set the second attestaion record to have an invalid signature.
*/ */
@ -196,7 +196,7 @@ fn test_block_validation_invalid_1st_attestation_signature() {
&params, &params,
mutator); mutator);
assert_eq!(status, Err(SszBlockValidationError::AttestationValidationError( assert_eq!(status, Err(SszBeaconBlockValidationError::AttestationValidationError(
AttestationValidationError::BadAggregateSignature))); AttestationValidationError::BadAggregateSignature)));
} }
@ -204,12 +204,15 @@ fn test_block_validation_invalid_1st_attestation_signature() {
fn test_block_validation_invalid_no_parent_proposer_signature() { fn test_block_validation_invalid_no_parent_proposer_signature() {
let params = get_simple_params(); let params = get_simple_params();
let mutator = |block: Block, attester_map, mut proposer_map: ProposerMap, stores: TestStore| { let mutator = |block: BeaconBlock, attester_map, mut proposer_map: ProposerMap, stores: TestStore| {
/* /*
* Set the proposer for this slot to be a validator that does not exist. * Set the proposer for this slot to be a validator that does not exist.
*/ */
let ssz = stores.block.get_serialized_block(&block.parent_hash.as_ref()).unwrap().unwrap(); let ssz = {
let parent_block_slot = SszBlock::from_slice(&ssz[..]).unwrap().slot_number(); let parent_hash = block.parent_hash().unwrap().as_ref();
stores.block.get_serialized_block(parent_hash).unwrap().unwrap()
};
let parent_block_slot = SszBeaconBlock::from_slice(&ssz[..]).unwrap().slot();
proposer_map.insert(parent_block_slot, params.total_validators + 1); proposer_map.insert(parent_block_slot, params.total_validators + 1);
(block, attester_map, proposer_map, stores) (block, attester_map, proposer_map, stores)
}; };
@ -218,7 +221,7 @@ fn test_block_validation_invalid_no_parent_proposer_signature() {
&params, &params,
mutator); mutator);
assert_eq!(status, Err(SszBlockValidationError::NoProposerSignature)); assert_eq!(status, Err(SszBeaconBlockValidationError::NoProposerSignature));
} }
#[test] #[test]
@ -237,14 +240,14 @@ fn test_block_validation_invalid_bad_proposer_map() {
&params, &params,
mutator); mutator);
assert_eq!(status, Err(SszBlockValidationError::BadProposerMap)); assert_eq!(status, Err(SszBeaconBlockValidationError::BadProposerMap));
} }
#[test] #[test]
fn test_block_validation_invalid_2nd_attestation_signature() { fn test_block_validation_invalid_2nd_attestation_signature() {
let params = get_simple_params(); let params = get_simple_params();
let mutator = |mut block: Block, attester_map, proposer_map, stores| { let mutator = |mut block: BeaconBlock, attester_map, proposer_map, stores| {
/* /*
* Set the second attestaion record to have an invalid signature. * Set the second attestaion record to have an invalid signature.
*/ */
@ -256,6 +259,6 @@ fn test_block_validation_invalid_2nd_attestation_signature() {
&params, &params,
mutator); mutator);
assert_eq!(status, Err(SszBlockValidationError::AttestationValidationError( assert_eq!(status, Err(SszBeaconBlockValidationError::AttestationValidationError(
AttestationValidationError::BadAggregateSignature))); AttestationValidationError::BadAggregateSignature)));
} }