Update boolean bitfield to new SSZ
This commit is contained in:
parent
1f098ecbde
commit
ebbeb03349
@ -195,28 +195,22 @@ impl std::ops::BitOr for BooleanBitfield {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Encodable for BooleanBitfield {
|
impl Encodable for BooleanBitfield {
|
||||||
// ssz_append encodes Self according to the `ssz` spec.
|
fn is_ssz_fixed_len() -> bool {
|
||||||
fn ssz_append(&self, s: &mut ssz::SszStream) {
|
false
|
||||||
s.append_vec(&self.to_bytes())
|
}
|
||||||
|
|
||||||
|
fn ssz_append(&self, buf: &mut Vec<u8>) {
|
||||||
|
buf.append(&mut self.to_bytes())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Decodable for BooleanBitfield {
|
impl Decodable for BooleanBitfield {
|
||||||
fn ssz_decode(bytes: &[u8], index: usize) -> Result<(Self, usize), ssz::DecodeError> {
|
fn is_ssz_fixed_len() -> bool {
|
||||||
let len = ssz::decode::decode_length(bytes, index, ssz::LENGTH_BYTES)?;
|
false
|
||||||
if (ssz::LENGTH_BYTES + len) > bytes.len() {
|
}
|
||||||
return Err(ssz::DecodeError::TooShort);
|
|
||||||
}
|
|
||||||
|
|
||||||
if len == 0 {
|
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, ssz::DecodeError> {
|
||||||
Ok((BooleanBitfield::new(), index + ssz::LENGTH_BYTES))
|
Ok(BooleanBitfield::from_bytes(bytes))
|
||||||
} else {
|
|
||||||
let bytes = &bytes[(index + 4)..(index + len + 4)];
|
|
||||||
let field = BooleanBitfield::from_bytes(bytes);
|
|
||||||
|
|
||||||
let index = index + ssz::LENGTH_BYTES + len;
|
|
||||||
Ok((field, index))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -277,7 +271,7 @@ cached_tree_hash_bytes_as_list!(BooleanBitfield);
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use serde_yaml;
|
use serde_yaml;
|
||||||
use ssz::{decode, ssz_encode, SszStream};
|
use ssz::ssz_encode;
|
||||||
use tree_hash::TreeHash;
|
use tree_hash::TreeHash;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -452,30 +446,17 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_ssz_encode() {
|
fn test_ssz_encode() {
|
||||||
let field = create_test_bitfield();
|
let field = create_test_bitfield();
|
||||||
let mut stream = SszStream::new();
|
assert_eq!(field.as_ssz_bytes(), vec![0b0000_0011, 0b1000_0111]);
|
||||||
stream.append(&field);
|
|
||||||
assert_eq!(stream.drain(), vec![2, 0, 0, 0, 0b0000_0011, 0b1000_0111]);
|
|
||||||
|
|
||||||
let field = BooleanBitfield::from_elem(18, true);
|
let field = BooleanBitfield::from_elem(18, true);
|
||||||
let mut stream = SszStream::new();
|
|
||||||
stream.append(&field);
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
stream.drain(),
|
field.as_ssz_bytes(),
|
||||||
vec![3, 0, 0, 0, 0b0000_0011, 0b1111_1111, 0b1111_1111]
|
vec![0b0000_0011, 0b1111_1111, 0b1111_1111]
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut b = BooleanBitfield::new();
|
let mut b = BooleanBitfield::new();
|
||||||
b.set(1, true);
|
b.set(1, true);
|
||||||
assert_eq!(
|
assert_eq!(ssz_encode(&b), vec![0b0000_0010]);
|
||||||
ssz_encode(&b),
|
|
||||||
vec![
|
|
||||||
0b0000_0001,
|
|
||||||
0b0000_0000,
|
|
||||||
0b0000_0000,
|
|
||||||
0b0000_0000,
|
|
||||||
0b0000_0010
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_test_bitfield() -> BooleanBitfield {
|
fn create_test_bitfield() -> BooleanBitfield {
|
||||||
@ -491,13 +472,13 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_ssz_decode() {
|
fn test_ssz_decode() {
|
||||||
let encoded = vec![2, 0, 0, 0, 0b0000_0011, 0b1000_0111];
|
let encoded = vec![0b0000_0011, 0b1000_0111];
|
||||||
let field = decode::<BooleanBitfield>(&encoded).unwrap();
|
let field = BooleanBitfield::from_ssz_bytes(&encoded).unwrap();
|
||||||
let expected = create_test_bitfield();
|
let expected = create_test_bitfield();
|
||||||
assert_eq!(field, expected);
|
assert_eq!(field, expected);
|
||||||
|
|
||||||
let encoded = vec![3, 0, 0, 0, 255, 255, 3];
|
let encoded = vec![255, 255, 3];
|
||||||
let field = decode::<BooleanBitfield>(&encoded).unwrap();
|
let field = BooleanBitfield::from_ssz_bytes(&encoded).unwrap();
|
||||||
let expected = BooleanBitfield::from_bytes(&[255, 255, 3]);
|
let expected = BooleanBitfield::from_bytes(&[255, 255, 3]);
|
||||||
assert_eq!(field, expected);
|
assert_eq!(field, expected);
|
||||||
}
|
}
|
||||||
@ -527,7 +508,7 @@ mod tests {
|
|||||||
fn test_ssz_round_trip() {
|
fn test_ssz_round_trip() {
|
||||||
let original = BooleanBitfield::from_bytes(&vec![18; 12][..]);
|
let original = BooleanBitfield::from_bytes(&vec![18; 12][..]);
|
||||||
let ssz = ssz_encode(&original);
|
let ssz = ssz_encode(&original);
|
||||||
let decoded = decode::<BooleanBitfield>(&ssz).unwrap();
|
let decoded = BooleanBitfield::from_ssz_bytes(&ssz).unwrap();
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user