Ensure attestation val. check parent.slot

As per comments by Danny Ryan on PR#33
This commit is contained in:
Paul Hauner
2018-10-12 20:41:18 +11:00
parent b5441535ba
commit 1f089d423e
4 changed files with 32 additions and 15 deletions

View File

@@ -28,8 +28,9 @@ use super::signature_verification::{
#[derive(Debug,PartialEq)]
pub enum AttestationValidationError {
SlotTooHigh,
SlotTooLow,
ParentSlotTooHigh,
BlockSlotTooHigh,
BlockSlotTooLow,
JustifiedSlotIncorrect,
InvalidJustifiedBlockHash,
TooManyObliqueHashes,
@@ -54,6 +55,8 @@ pub struct AttestationValidationContext<T>
{
/// The slot as determined by the system time.
pub block_slot: u64,
/// The slot of the parent of the block that contained this attestation.
pub parent_block_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.
@@ -82,10 +85,11 @@ impl<T> AttestationValidationContext<T>
-> Result<HashSet<usize>, AttestationValidationError>
{
/*
* The attesation slot must not be higher than the block that contained it.
* The attesation slot must be less than or equal to the parent of the slot of the block
* that contained the attestation.
*/
if a.slot > self.block_slot {
return Err(AttestationValidationError::SlotTooHigh);
if a.slot > self.parent_block_slot {
return Err(AttestationValidationError::ParentSlotTooHigh);
}
/*
@@ -94,7 +98,7 @@ impl<T> AttestationValidationContext<T>
*/
if a.slot < self.block_slot
.saturating_sub(u64::from(self.cycle_length).saturating_add(1)) {
return Err(AttestationValidationError::SlotTooLow);
return Err(AttestationValidationError::BlockSlotTooLow);
}
/*
@@ -210,9 +214,9 @@ impl From<ParentHashesError> for AttestationValidationError {
ParentHashesError::BadObliqueHashes
=> AttestationValidationError::BadObliqueHashes,
ParentHashesError::SlotTooLow
=> AttestationValidationError::SlotTooLow,
=> AttestationValidationError::BlockSlotTooLow,
ParentHashesError::SlotTooHigh
=> AttestationValidationError::SlotTooHigh,
=> AttestationValidationError::BlockSlotTooHigh,
ParentHashesError::IntWrapping
=> AttestationValidationError::IntWrapping
}

View File

@@ -196,7 +196,7 @@ impl<T> BlockValidationContext<T>
* Also, read the slot from the parent block for later use.
*/
let parent_hash = b.parent_hash();
let parent_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),
Some(ssz) => {
let parent_block = SszBlock::from_slice(&ssz[..])?;
@@ -209,6 +209,7 @@ impl<T> BlockValidationContext<T>
*/
let attestation_validation_context = Arc::new(AttestationValidationContext {
block_slot,
parent_block_slot,
cycle_length: self.cycle_length,
last_justified_slot: self.last_justified_slot,
parent_hashes: self.parent_hashes.clone(),
@@ -230,7 +231,7 @@ impl<T> BlockValidationContext<T>
* If the signature of proposer for the parent slot was not present in the first (0'th)
* attestation of this block, reject the block.
*/
let parent_block_proposer = self.proposer_map.get(&parent_slot)
let parent_block_proposer = self.proposer_map.get(&parent_block_slot)
.ok_or(SszBlockValidationError::BadProposerMap)?;
if !attestation_voters.contains(&parent_block_proposer) {
return Err(SszBlockValidationError::NoProposerSignature);