Finish SszBlock, add tests

This commit is contained in:
Paul Hauner 2018-09-20 13:13:07 +10:00
parent a1b826906a
commit 7020a08b7d
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C

View File

@ -68,32 +68,47 @@ impl<'a> SszBlock<'a> {
}
pub fn parent_hash(&self) -> &[u8] {
&self.ssz[5..37]
&self.ssz[4..36]
}
pub fn slot_number(&self) -> u64 {
u64::ssz_decode(&self.ssz, 32).unwrap().0
/*
* An error should be unreachable from this decode
* because we checked the length of the array at
* the initalization of this struct.
*
* If you can make this function panic, please report
* it to paul@sigmaprime.io
*/
if let Ok((n, _)) = u64::ssz_decode(&self.ssz, 36) {
n
} else {
unreachable!();
}
}
pub fn randao_reveal(&self) -> &[u8] {
&self.ssz[40..72]
&self.ssz[48..80]
}
pub fn attestations(&self) -> &[u8] {
let start = 72 + LENGTH_BYTES;
let start = 80 + LENGTH_BYTES;
&self.ssz[start..(start + self.attestation_len)]
}
pub fn pow_chain_ref(&self) -> &[u8] {
&self.ssz[(self.len - 96)..(self.len - 64)]
let start = self.len - (32 + LENGTH_BYTES + 32 + LENGTH_BYTES + 32);
&self.ssz[start..(start + 32)]
}
pub fn act_state_root(&self) -> &[u8] {
&self.ssz[(self.len - 64)..(self.len - 32)]
let start = self.len - (32 + LENGTH_BYTES + 32);
&self.ssz[start..(start + 32)]
}
pub fn cry_state_root(&self) -> &[u8] {
&self.ssz[(self.len - 32)..(self.len)]
let start = self.len - 32;
&self.ssz[start..(start + 32)]
}
}
@ -103,6 +118,7 @@ mod tests {
use super::super::block::Block;
use super::super::attestation_record::AttestationRecord;
use super::super::ssz::SszStream;
use super::super::utils::types::Hash256;
fn get_block_ssz(b: &Block) -> Vec<u8> {
let mut ssz_stream = SszStream::new();
@ -110,6 +126,12 @@ mod tests {
ssz_stream.drain()
}
fn get_attestation_record_ssz(ar: &AttestationRecord) -> Vec<u8> {
let mut ssz_stream = SszStream::new();
ssz_stream.append(ar);
ssz_stream.drain()
}
#[test]
fn test_ssz_block_zero_attestation_records() {
let mut b = Block::zero();
@ -122,6 +144,18 @@ mod tests {
);
}
#[test]
fn test_ssz_block_single_attestation_record_one_byte_short() {
let mut b = Block::zero();
b.attestations = vec![AttestationRecord::zero()];
let ssz = get_block_ssz(&b);
assert_eq!(
SszBlock::from_slice(&ssz[0..(ssz.len() - 1)]),
Err(BlockValidatorError::TooShort)
);
}
#[test]
fn test_ssz_block_single_attestation_record() {
let mut b = Block::zero();
@ -130,4 +164,129 @@ mod tests {
assert!(SszBlock::from_slice(&ssz[..]).is_ok());
}
#[test]
fn test_ssz_block_block_hash() {
let mut block = Block::zero();
block.attestations.push(AttestationRecord::zero());
let serialized = get_block_ssz(&block);
let ssz_block = SszBlock::from_slice(&serialized).unwrap();
let hash = ssz_block.block_hash();
// Note: this hash was not generated by some external program,
// it was simply printed then copied into the code. This test
// will tell us if the hash changes, not that it matches some
// canonical reference.
let expected_hash = [
28, 184, 51, 12, 226, 15, 73, 50, 66, 19, 168, 149,
229, 122, 141, 111, 42, 236, 137, 157, 230, 90, 149,
58, 145, 52, 47, 62, 158, 131, 46, 147
];
assert_eq!(hash, expected_hash);
}
#[test]
fn test_ssz_block_parent_hash() {
let mut block = Block::zero();
block.attestations.push(AttestationRecord::zero());
let reference_hash = Hash256::from([42_u8; 32]);
block.parent_hash = reference_hash.clone();
let serialized = get_block_ssz(&block);
let ssz_block = SszBlock::from_slice(&serialized).unwrap();
assert_eq!(ssz_block.parent_hash(), &reference_hash.to_vec()[..]);
}
#[test]
fn test_ssz_block_slot_number() {
let mut block = Block::zero();
block.attestations.push(AttestationRecord::zero());
block.slot_number = 42;
let serialized = get_block_ssz(&block);
let ssz_block = SszBlock::from_slice(&serialized).unwrap();
assert_eq!(ssz_block.slot_number(), 42);
}
#[test]
fn test_ssz_block_randao_reveal() {
let mut block = Block::zero();
block.attestations.push(AttestationRecord::zero());
let reference_hash = Hash256::from([42_u8; 32]);
block.randao_reveal = reference_hash.clone();
let serialized = get_block_ssz(&block);
let ssz_block = SszBlock::from_slice(&serialized).unwrap();
assert_eq!(ssz_block.randao_reveal(), &reference_hash.to_vec()[..]);
}
#[test]
fn test_ssz_block_attestations() {
/*
* Single AttestationRecord
*/
let mut block = Block::zero();
block.attestations.push(AttestationRecord::zero());
let serialized = get_block_ssz(&block);
let ssz_block = SszBlock::from_slice(&serialized).unwrap();
let ssz_ar = get_attestation_record_ssz(&AttestationRecord::zero());
assert_eq!(ssz_block.attestations(), &ssz_ar[..]);
/*
* Multiple AttestationRecords
*/
let mut block = Block::zero();
block.attestations.push(AttestationRecord::zero());
block.attestations.push(AttestationRecord::zero());
let serialized = get_block_ssz(&block);
let ssz_block = SszBlock::from_slice(&serialized).unwrap();
let mut ssz_ar = get_attestation_record_ssz(&AttestationRecord::zero());
ssz_ar.append(&mut get_attestation_record_ssz(&AttestationRecord::zero()));
assert_eq!(ssz_block.attestations(), &ssz_ar[..]);
}
#[test]
fn test_ssz_block_pow_chain_ref() {
let mut block = Block::zero();
block.attestations.push(AttestationRecord::zero());
let reference_hash = Hash256::from([42_u8; 32]);
block.pow_chain_ref = reference_hash.clone();
let serialized = get_block_ssz(&block);
let ssz_block = SszBlock::from_slice(&serialized).unwrap();
assert_eq!(ssz_block.pow_chain_ref(), &reference_hash.to_vec()[..]);
}
#[test]
fn test_ssz_block_act_state_root() {
let mut block = Block::zero();
block.attestations.push(AttestationRecord::zero());
let reference_hash = Hash256::from([42_u8; 32]);
block.active_state_root = reference_hash.clone();
let serialized = get_block_ssz(&block);
let ssz_block = SszBlock::from_slice(&serialized).unwrap();
assert_eq!(ssz_block.act_state_root(), &reference_hash.to_vec()[..]);
}
#[test]
fn test_ssz_block_cry_state_root() {
let mut block = Block::zero();
block.attestations.push(AttestationRecord::zero());
let reference_hash = Hash256::from([42_u8; 32]);
block.crystallized_state_root = reference_hash.clone();
let serialized = get_block_ssz(&block);
let ssz_block = SszBlock::from_slice(&serialized).unwrap();
assert_eq!(ssz_block.cry_state_root(), &reference_hash.to_vec()[..]);
}
}