Tidy SSZ encode impls

This commit is contained in:
Paul Hauner 2019-05-06 10:26:49 +10:00
parent 56fe63f78d
commit 96ef8e7073
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6

View File

@ -49,43 +49,53 @@ impl<T: Encodable> Encodable for Vec<T> {
} }
} }
/*
impl Encodable for bool { impl Encodable for bool {
fn ssz_fixed_len() -> Option<usize> { fn is_ssz_fixed_len() -> bool {
Some(8) true
} }
fn ssz_append(&self, s: &mut SszStream) { fn ssz_fixed_len() -> usize {
s.append_fixed_bytes(&(self as u8).to_le_bytes()); 1
}
fn ssz_append(&self, buf: &mut Vec<u8>) {
buf.extend_from_slice(&(*self as u8).to_le_bytes());
} }
} }
impl Encodable for H256 { impl Encodable for H256 {
fn ssz_fixed_len() -> Option<usize> { fn is_ssz_fixed_len() -> bool {
Some(32) true
} }
fn as_ssz_bytes(&self) -> Vec<u8> { fn ssz_fixed_len() -> usize {
self.as_bytes().to_vec() 32
}
fn ssz_append(&self, buf: &mut Vec<u8>) {
buf.extend_from_slice(self.as_bytes());
} }
} }
macro_rules! impl_encodable_for_u8_array { macro_rules! impl_encodable_for_u8_array {
($len: expr) => { ($len: expr) => {
impl Encodable for [u8; $len] { impl Encodable for [u8; $len] {
fn ssz_fixed_len() -> Option<usize> { fn is_ssz_fixed_len() -> bool {
Some($len) true
} }
fn as_ssz_bytes(&self) -> Vec<u8> { fn ssz_fixed_len() -> usize {
self.to_vec() $len
}
fn ssz_append(&self, buf: &mut Vec<u8>) {
buf.extend_from_slice(&self[..]);
} }
} }
}; };
} }
impl_encodable_for_u8_array!(4); impl_encodable_for_u8_array!(4);
*/
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -164,58 +174,29 @@ mod tests {
); );
} }
/*
#[test]
fn ssz_encode_h256() {
let h = H256::zero();
let mut ssz = SszStream::new();
ssz.append(&h);
assert_eq!(ssz.drain(), vec![0; 32]);
}
#[test]
fn ssz_mixed() {
let mut stream = SszStream::new();
let h = H256::zero();
let a: u8 = 100;
let b: u16 = 65535;
let c: u32 = 1 << 24;
stream.append(&h);
stream.append(&a);
stream.append(&b);
stream.append(&c);
let ssz = stream.drain();
assert_eq!(ssz[0..32], *vec![0; 32]);
assert_eq!(ssz[32], 100);
assert_eq!(ssz[33..55], *vec![255, 255]);
assert_eq!(ssz[55..59], *vec![0, 0, 0, 1]);
}
#[test] #[test]
fn ssz_encode_bool() { fn ssz_encode_bool() {
let x: bool = false; assert_eq!(true.as_ssz_bytes(), vec![1]);
let mut ssz = SszStream::new(); assert_eq!(false.as_ssz_bytes(), vec![0]);
ssz.append(&x);
assert_eq!(ssz.drain(), vec![0b0000_0000]);
let x: bool = true;
let mut ssz = SszStream::new();
ssz.append(&x);
assert_eq!(ssz.drain(), vec![0b0000_0001]);
} }
#[test] #[test]
fn ssz_encode_u8_array() { fn ssz_encode_h256() {
let x: [u8; 4] = [0, 1, 7, 8]; assert_eq!(H256::from(&[0; 32]).as_ssz_bytes(), vec![0; 32]);
let ssz = ssz_encode(&x); assert_eq!(H256::from(&[1; 32]).as_ssz_bytes(), vec![1; 32]);
assert_eq!(ssz, vec![0, 1, 7, 8]);
let x: [u8; 4] = [255, 255, 255, 255]; let bytes = vec![
let ssz = ssz_encode(&x); 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
assert_eq!(ssz, vec![255, 255, 255, 255]); 0, 0, 0,
];
assert_eq!(H256::from_slice(&bytes).as_ssz_bytes(), bytes);
}
#[test]
fn ssz_encode_u8_array_4() {
assert_eq!([0, 0, 0, 0].as_ssz_bytes(), vec![0; 4]);
assert_eq!([1, 0, 0, 0].as_ssz_bytes(), vec![1, 0, 0, 0]);
assert_eq!([1, 2, 3, 4].as_ssz_bytes(), vec![1, 2, 3, 4]);
} }
*/
} }