Move ssz block min length into block.rs, add tests

This commit is contained in:
Paul Hauner 2018-09-19 16:57:21 +10:00
parent d0d336c341
commit ca951d0361
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
2 changed files with 30 additions and 39 deletions

View File

@ -1,8 +1,20 @@
use super::utils::types::Hash256;
use super::attestation_record::AttestationRecord;
use super::attestation_record::{
AttestationRecord,
MIN_SSZ_ATTESTION_RECORD_LENGTH,
};
use super::ssz::{ Encodable, SszStream };
const SSZ_BLOCK_LENGTH: usize = 192;
pub const MIN_SSZ_BLOCK_LENGTH: usize = {
4 + 32 + // parent_hash
8 + // slot_number
4 + 32 + // randao_reveal
4 + MIN_SSZ_ATTESTION_RECORD_LENGTH + // attestations (minimum one)
4 + 32 + // pow_chain_ref
4 + 32 + // active_state_root
4 + 32 // crystallized_state_root
};
pub const MAX_SSZ_BLOCK_LENGTH: usize = MIN_SSZ_BLOCK_LENGTH + (1 << 24);
pub struct Block {
pub parent_hash: Hash256,
@ -26,23 +38,6 @@ impl Block {
crystallized_state_root: Hash256::zero(),
}
}
/// Return the bytes that should be signed in order to
/// attest for this block.
pub fn encode_for_signing(&self)
-> [u8; SSZ_BLOCK_LENGTH]
{
let mut s = SszStream::new();
s.append(&self.parent_hash);
s.append(&self.slot_number);
s.append(&self.randao_reveal);
s.append(&self.pow_chain_ref);
s.append(&self.active_state_root);
s.append(&self.crystallized_state_root);
let vec = s.drain();
let mut encoded = [0; SSZ_BLOCK_LENGTH];
encoded.copy_from_slice(&vec); encoded
}
}
impl Encodable for Block {
@ -54,8 +49,6 @@ impl Encodable for Block {
s.append(&self.pow_chain_ref);
s.append(&self.active_state_root);
s.append(&self.crystallized_state_root);
// TODO: encode the aggregate sig correctly
s.append_vec(&vec![0_u8; 64])
}
}
@ -75,4 +68,16 @@ mod tests {
assert!(b.active_state_root.is_zero());
assert!(b.crystallized_state_root.is_zero());
}
#[test]
pub fn test_block_min_ssz_length() {
let mut b = Block::zero();
b.attestations = vec![AttestationRecord::zero()];
let mut ssz_stream = SszStream::new();
ssz_stream.append(&b);
let ssz = ssz_stream.drain();
assert_eq!(ssz.len(), MIN_SSZ_BLOCK_LENGTH);
}
}

View File

@ -3,7 +3,10 @@ use super::ssz::decode::{
Decodable,
};
use super::utils::hash::canonical_hash;
use super::attestation_record::MIN_SSZ_ATTESTION_RECORD_LENGTH;
use super::block::{
MIN_SSZ_BLOCK_LENGTH,
MAX_SSZ_BLOCK_LENGTH,
};
#[derive(Debug, PartialEq)]
pub enum BlockValidatorError {
@ -16,16 +19,6 @@ pub enum BlockValidatorError {
}
const LENGTH_BYTES: usize = 4;
const MIN_SSZ_BLOCK_LENGTH: usize = {
32 + // parent_hash
8 + // slot_number
32 + // randao_reveal
LENGTH_BYTES + // attestations (assuming zero)
32 + // pow_chain_ref
32 + // active_state_root
32 // crystallized_state_root
};
const MAX_SSZ_BLOCK_LENGTH: usize = MIN_SSZ_BLOCK_LENGTH + (1 << 24);
/// Allows for reading of block values directly from serialized
/// ssz bytes.
@ -61,13 +54,6 @@ impl<'a> SszBlock<'a> {
*/
let attestation_len = decode_length(ssz, 72, LENGTH_BYTES)
.map_err(|_| BlockValidatorError::TooShort)?;
if attestation_len < MIN_SSZ_ATTESTION_RECORD_LENGTH {
return Err(BlockValidatorError::NoAttestationRecords)
}
/*
* Ensure that the SSZ is long enough, now we know the
* length of the attestation records field.
*/
if len < (76 + attestation_len + 96) {
return Err(BlockValidatorError::TooShort);
}