From da1e0c8a59e289e0941f566f6d6482a99bb54c3d Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 25 Sep 2018 22:09:30 +1000 Subject: [PATCH] Change bitfield serialization to little-endian --- boolean-bitfield/src/lib.rs | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/boolean-bitfield/src/lib.rs b/boolean-bitfield/src/lib.rs index 01e8f9f79..6ac8e082b 100644 --- a/boolean-bitfield/src/lib.rs +++ b/boolean-bitfield/src/lib.rs @@ -123,6 +123,11 @@ impl BooleanBitfield { } /// Clone and return the underlying byte array (`Vec`). + pub fn to_vec(&self) -> Vec { + self.vec.clone() + } + + /// Clone and return the underlying byte array (`Vec`) in big-endinan format. pub fn to_be_vec(&self) -> Vec { let mut o = self.vec.clone(); o.reverse(); @@ -150,7 +155,7 @@ impl PartialEq for BooleanBitfield { impl ssz::Encodable for BooleanBitfield { fn ssz_append(&self, s: &mut ssz::SszStream) { - s.append_vec(&self.to_be_vec()); + s.append_vec(&self.to_vec()); } } @@ -191,7 +196,7 @@ mod tests { let mut stream = ssz::SszStream::new(); stream.append(&b); - assert_eq!(stream.drain(), vec![0, 0, 0, 2, 1, 0]); + assert_eq!(stream.drain(), vec![0, 0, 0, 2, 0, 1]); } #[test] @@ -199,7 +204,7 @@ mod tests { /* * Correct input */ - let input = vec![0, 0, 0, 2, 1, 0]; + let input = vec![0, 0, 0, 2, 0, 1]; let (b, i) = BooleanBitfield::ssz_decode(&input, 0).unwrap(); assert_eq!(i, 6); assert_eq!(b.num_true_bits(), 1); @@ -208,7 +213,7 @@ mod tests { /* * Input too long */ - let mut input = vec![0, 0, 0, 2, 1, 0]; + let mut input = vec![0, 0, 0, 2, 0, 1]; input.push(42); let (b, i) = BooleanBitfield::ssz_decode(&input, 0).unwrap(); assert_eq!(i, 6); @@ -234,17 +239,6 @@ mod tests { assert_eq!(b.to_be_vec(), vec![0]); } - #[test] - fn test_new_bitfield_len() { - let b = BooleanBitfield::new(); - assert_eq!(b.len(), 0); - assert_eq!(b.to_be_vec(), vec![0]); - - let b = BooleanBitfield::with_capacity(100); - assert_eq!(b.len(), 0); - assert_eq!(b.to_be_vec(), vec![0]); - } - #[test] fn test_bitfield_set() { let mut b = BooleanBitfield::new();