diff --git a/lighthouse/state/attestation_record.rs b/lighthouse/state/attestation_record.rs index a665394a4..73ed5e543 100644 --- a/lighthouse/state/attestation_record.rs +++ b/lighthouse/state/attestation_record.rs @@ -1,6 +1,12 @@ use super::utils::types::{ Hash256, Bitfield }; use super::utils::bls::{ AggregateSignature }; -use super::ssz::{ Encodable, SszStream }; +use super::ssz::{ + Encodable, + Decodable, + DecodeError, + decode_ssz_list, + SszStream, +}; pub const MIN_SSZ_ATTESTION_RECORD_LENGTH: usize = { 8 + // slot @@ -10,9 +16,10 @@ pub const MIN_SSZ_ATTESTION_RECORD_LENGTH: usize = { 5 + // attester_bitfield (assuming 1 byte of bitfield) 8 + // justified_slot 32 + // justified_block_hash - 4 + (2 * 32) // aggregate sig (two 256 bit points) + 4 + (2 * 8) // aggregate sig (two 256 bit points) }; +#[derive(Debug)] pub struct AttestationRecord { pub slot: u64, pub shard_id: u16, @@ -34,7 +41,34 @@ impl Encodable for AttestationRecord { s.append(&self.justified_slot); s.append(&self.justified_block_hash); // TODO: encode the aggregate sig correctly - s.append_vec(&vec![0_u8; 64]) + s.append_vec(&vec![0_u8; 16]) + } +} + +impl Decodable for AttestationRecord { + fn ssz_decode(bytes: &[u8], i: usize) + -> Result<(Self, usize), DecodeError> + { + let (slot, i) = u64::ssz_decode(bytes, i)?; + let (shard_id, i) = u16::ssz_decode(bytes, i)?; + let (oblique_parent_hashes, i) = decode_ssz_list(bytes, i)?; + let (shard_block_hash, i) = Hash256::ssz_decode(bytes, i)?; + let (attester_bitfield, i) = Bitfield::ssz_decode(bytes, i)?; + let (justified_slot, i) = u64::ssz_decode(bytes, i)?; + let (justified_block_hash, i) = Hash256::ssz_decode(bytes, i)?; + // Do aggregate sig decoding properly. + let aggregate_sig = None; let i = i + 20; + let attestation_record = Self { + slot, + shard_id, + oblique_parent_hashes, + shard_block_hash, + attester_bitfield, + justified_slot, + justified_block_hash, + aggregate_sig, + }; + Ok((attestation_record, i)) } } @@ -68,4 +102,31 @@ mod tests { assert_eq!(ssz.len(), MIN_SSZ_ATTESTION_RECORD_LENGTH); } + + #[test] + pub fn test_attestation_record_min_ssz_encode_decode() { + let original = AttestationRecord { + slot: 7, + shard_id: 9, + oblique_parent_hashes: vec![Hash256::from(&vec![14; 32][..])], + shard_block_hash: Hash256::from(&vec![15; 32][..]), + attester_bitfield: Bitfield::from(&vec![17; 42][..]), + justified_slot: 19, + justified_block_hash: Hash256::from(&vec![15; 32][..]), + aggregate_sig: None, + }; + + let mut ssz_stream = SszStream::new(); + ssz_stream.append(&original); + + let (decoded, _) = AttestationRecord:: + ssz_decode(&ssz_stream.drain(), 0).unwrap(); + assert_eq!(original.slot, decoded.slot); + assert_eq!(original.shard_id, decoded.shard_id); + assert_eq!(original.oblique_parent_hashes, decoded.oblique_parent_hashes); + assert_eq!(original.shard_block_hash, decoded.shard_block_hash); + assert_eq!(original.attester_bitfield, decoded.attester_bitfield); + assert_eq!(original.justified_slot, decoded.justified_slot); + assert_eq!(original.justified_block_hash, decoded.justified_block_hash); + } } diff --git a/lighthouse/state/block/ssz_block.rs b/lighthouse/state/block/ssz_block.rs index 86fd5283f..577857a19 100644 --- a/lighthouse/state/block/ssz_block.rs +++ b/lighthouse/state/block/ssz_block.rs @@ -235,9 +235,9 @@ mod tests { // will tell us if the hash changes, not that it matches some // canonical reference. let expected_hash = [ - 214, 217, 16, 230, 17, 204, 99, 222, 104, 90, 128, 228, - 12, 249, 56, 255, 110, 10, 229, 29, 110, 107, 105, 195, - 219, 132, 138, 206, 204, 34, 21, 159 + 195, 180, 208, 144, 113, 20, 129, 108, 14, 128, 166, 170, + 137, 15, 191, 186, 34, 171, 79, 214, 74, 86, 89, 202, 255, + 9, 100, 170, 149, 160, 93, 59 ]; assert_eq!(hash, expected_hash);