Change bitfield serialization to little-endian

This commit is contained in:
Paul Hauner 2018-09-25 22:09:30 +10:00
parent dc7d2ff150
commit da1e0c8a59
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C

View File

@ -123,6 +123,11 @@ impl BooleanBitfield {
} }
/// Clone and return the underlying byte array (`Vec<u8>`). /// Clone and return the underlying byte array (`Vec<u8>`).
pub fn to_vec(&self) -> Vec<u8> {
self.vec.clone()
}
/// Clone and return the underlying byte array (`Vec<u8>`) in big-endinan format.
pub fn to_be_vec(&self) -> Vec<u8> { pub fn to_be_vec(&self) -> Vec<u8> {
let mut o = self.vec.clone(); let mut o = self.vec.clone();
o.reverse(); o.reverse();
@ -150,7 +155,7 @@ impl PartialEq for BooleanBitfield {
impl ssz::Encodable for BooleanBitfield { impl ssz::Encodable for BooleanBitfield {
fn ssz_append(&self, s: &mut ssz::SszStream) { 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(); let mut stream = ssz::SszStream::new();
stream.append(&b); 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] #[test]
@ -199,7 +204,7 @@ mod tests {
/* /*
* Correct input * 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(); let (b, i) = BooleanBitfield::ssz_decode(&input, 0).unwrap();
assert_eq!(i, 6); assert_eq!(i, 6);
assert_eq!(b.num_true_bits(), 1); assert_eq!(b.num_true_bits(), 1);
@ -208,7 +213,7 @@ mod tests {
/* /*
* Input too long * 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); input.push(42);
let (b, i) = BooleanBitfield::ssz_decode(&input, 0).unwrap(); let (b, i) = BooleanBitfield::ssz_decode(&input, 0).unwrap();
assert_eq!(i, 6); assert_eq!(i, 6);
@ -234,17 +239,6 @@ mod tests {
assert_eq!(b.to_be_vec(), vec![0]); 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] #[test]
fn test_bitfield_set() { fn test_bitfield_set() {
let mut b = BooleanBitfield::new(); let mut b = BooleanBitfield::new();