mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-23 14:54:45 +00:00
Marge fixes to test_harness, add serdehex crate
This commit is contained in:
@@ -24,11 +24,30 @@ macro_rules! impl_decodable_for_uint {
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! impl_decodable_for_u8_array {
|
||||
($len: expr) => {
|
||||
impl Decodable for [u8; $len] {
|
||||
fn ssz_decode(bytes: &[u8], index: usize) -> Result<(Self, usize), DecodeError> {
|
||||
if index + $len > bytes.len() {
|
||||
Err(DecodeError::TooShort)
|
||||
} else {
|
||||
let mut array: [u8; $len] = [0; $len];
|
||||
array.copy_from_slice(&bytes[index..index + $len]);
|
||||
|
||||
Ok((array, index + $len))
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_decodable_for_uint!(u16, 16);
|
||||
impl_decodable_for_uint!(u32, 32);
|
||||
impl_decodable_for_uint!(u64, 64);
|
||||
impl_decodable_for_uint!(usize, 64);
|
||||
|
||||
impl_decodable_for_u8_array!(4);
|
||||
|
||||
impl Decodable for u8 {
|
||||
fn ssz_decode(bytes: &[u8], index: usize) -> Result<(Self, usize), DecodeError> {
|
||||
if index >= bytes.len() {
|
||||
@@ -246,4 +265,12 @@ mod tests {
|
||||
let result: Result<(bool, usize), DecodeError> = decode_ssz(&ssz, 0);
|
||||
assert_eq!(result, Err(DecodeError::Invalid));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decode_u8_array() {
|
||||
let ssz = vec![0, 1, 2, 3];
|
||||
let (result, index): ([u8; 4], usize) = decode_ssz(&ssz, 0).unwrap();
|
||||
assert_eq!(index, 4);
|
||||
assert_eq!(result, [0, 1, 2, 3]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user