Add decode_ssz
function for general deserialization
This commit is contained in:
parent
cc1fc5c0cc
commit
92ebe5da0d
@ -10,7 +10,21 @@ pub enum DecodeError {
|
||||
}
|
||||
|
||||
pub trait Decodable: Sized {
|
||||
fn ssz_decode(bytes: &[u8]) -> Result<Self, DecodeError>;
|
||||
fn ssz_decode(bytes: &[u8], index: usize) -> Result<Self, DecodeError>;
|
||||
}
|
||||
|
||||
/// Decode the given bytes for the given type
|
||||
///
|
||||
/// The single ssz encoded value will be decoded as the given type at the
|
||||
/// given index.
|
||||
pub fn decode_ssz<T>(ssz_bytes: &[u8], index: usize)
|
||||
-> Result<T, DecodeError>
|
||||
where T: Decodable
|
||||
{
|
||||
if index >= ssz_bytes.len() {
|
||||
return Err(DecodeError::OutOfBounds)
|
||||
}
|
||||
T::ssz_decode(ssz_bytes, index)
|
||||
}
|
||||
|
||||
/// Decode the nth element of some ssz list.
|
||||
|
@ -5,22 +5,23 @@ use super::{
|
||||
macro_rules! impl_decodable_for_uint {
|
||||
($type: ident, $bit_size: expr) => {
|
||||
impl Decodable for $type {
|
||||
fn ssz_decode(bytes: &[u8])
|
||||
fn ssz_decode(bytes: &[u8], index: usize)
|
||||
-> Result<Self, DecodeError>
|
||||
{
|
||||
assert!((0 < $bit_size) &
|
||||
($bit_size <= 64) &
|
||||
($bit_size % 8 == 0));
|
||||
let max_bytes = $bit_size / 8;
|
||||
if bytes.len() <= max_bytes {
|
||||
if bytes.len() >= (index + max_bytes) {
|
||||
let end_bytes = index + max_bytes;
|
||||
let mut result: $type = 0;
|
||||
for i in 0..bytes.len() {
|
||||
let offset = (bytes.len() - i - 1) * 8;
|
||||
for i in index..end_bytes {
|
||||
let offset = ((index + max_bytes) - i - 1) * 8;
|
||||
result = ((bytes[i] as $type) << offset) | result;
|
||||
};
|
||||
Ok(result)
|
||||
} else {
|
||||
Err(DecodeError::TooLong)
|
||||
Err(DecodeError::TooShort)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -37,78 +38,119 @@ impl_decodable_for_uint!(usize, 64);
|
||||
mod tests {
|
||||
use super::super::{
|
||||
DecodeError,
|
||||
decode_ssz_list_element,
|
||||
decode_ssz,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_ssz_decode_u16() {
|
||||
let ssz = vec![0, 0, 1, 0];
|
||||
let result: u16 = decode_ssz_list_element(&ssz, 0).unwrap();
|
||||
let ssz = vec![0, 0];
|
||||
let result: u16 = decode_ssz(&ssz, 0).unwrap();
|
||||
assert_eq!(result, 0);
|
||||
|
||||
let ssz = vec![0, 0, 1, 16];
|
||||
let result: u16 = decode_ssz_list_element(&ssz, 0).unwrap();
|
||||
let ssz = vec![0, 16];
|
||||
let result: u16 = decode_ssz(&ssz, 0).unwrap();
|
||||
assert_eq!(result, 16);
|
||||
|
||||
let ssz = vec![0, 0, 2, 1, 0];
|
||||
let result: u16 = decode_ssz_list_element(&ssz, 0).unwrap();
|
||||
let ssz = vec![1, 0];
|
||||
let result: u16 = decode_ssz(&ssz, 0).unwrap();
|
||||
assert_eq!(result, 256);
|
||||
|
||||
let ssz = vec![0, 0, 2, 255, 255];
|
||||
let result: u16 = decode_ssz_list_element(&ssz, 0).unwrap();
|
||||
let ssz = vec![255, 255];
|
||||
let result: u16 = decode_ssz(&ssz, 0).unwrap();
|
||||
assert_eq!(result, 65535);
|
||||
|
||||
let ssz = vec![0, 0, 3, 0, 0, 1];
|
||||
let ssz = vec![1];
|
||||
let result: Result<u16, DecodeError> =
|
||||
decode_ssz_list_element(&ssz, 0);
|
||||
assert_eq!(result, Err(DecodeError::TooLong));
|
||||
decode_ssz(&ssz, 0);
|
||||
assert_eq!(result, Err(DecodeError::TooShort));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ssz_decode_u32() {
|
||||
let ssz = vec![0, 0, 1, 0];
|
||||
let result: u32 = decode_ssz_list_element(&ssz, 0).unwrap();
|
||||
let ssz = vec![0, 0, 0, 0];
|
||||
let result: u32 = decode_ssz(&ssz, 0).unwrap();
|
||||
assert_eq!(result, 0);
|
||||
|
||||
let ssz = vec![0, 0, 4, 255, 255, 255, 255];
|
||||
let result: u32 = decode_ssz_list_element(&ssz, 0).unwrap();
|
||||
let ssz = vec![0, 0, 1, 0];
|
||||
let result: u32 = decode_ssz(&ssz, 0).unwrap();
|
||||
assert_eq!(result, 256);
|
||||
|
||||
let ssz = vec![255, 255, 255, 0, 0, 1, 0];
|
||||
let result: u32 = decode_ssz(&ssz, 3).unwrap();
|
||||
assert_eq!(result, 256);
|
||||
|
||||
let ssz = vec![0,200, 1, 0];
|
||||
let result: u32 = decode_ssz(&ssz, 0).unwrap();
|
||||
assert_eq!(result, 13107456);
|
||||
|
||||
let ssz = vec![255, 255, 255, 255];
|
||||
let result: u32 = decode_ssz(&ssz, 0).unwrap();
|
||||
assert_eq!(result, 4294967295);
|
||||
|
||||
let ssz = vec![0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 1];
|
||||
let ssz = vec![0, 0, 1];
|
||||
let result: Result<u32, DecodeError> =
|
||||
decode_ssz_list_element(&ssz, 0);
|
||||
assert_eq!(result, Err(DecodeError::TooLong));
|
||||
decode_ssz(&ssz, 0);
|
||||
assert_eq!(result, Err(DecodeError::TooShort));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ssz_decode_u64() {
|
||||
let ssz = vec![0, 0, 1, 0];
|
||||
let result: u64 = decode_ssz_list_element(&ssz, 0).unwrap();
|
||||
let ssz = vec![0, 0, 0, 0, 0, 0, 0, 0];
|
||||
let result: u64 = decode_ssz(&ssz, 0).unwrap();
|
||||
assert_eq!(result, 0);
|
||||
|
||||
let ssz = vec![0, 0, 8, 255, 255, 255, 255, 255, 255, 255, 255];
|
||||
let result: u64 = decode_ssz_list_element(&ssz, 0).unwrap();
|
||||
let ssz = vec![255, 255, 255, 255, 255, 255, 255, 255];
|
||||
let result: u64 = decode_ssz(&ssz, 0).unwrap();
|
||||
assert_eq!(result, 18446744073709551615);
|
||||
|
||||
let ssz = vec![0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 1];
|
||||
let ssz = vec![255, 255, 255, 0, 0, 0, 0, 0, 0, 0];
|
||||
let result: u64 = decode_ssz(&ssz, 2).unwrap();
|
||||
assert_eq!(result, 18374686479671623680);
|
||||
|
||||
let ssz = vec![0,0,0,0,0,0,0];
|
||||
let result: Result<u64, DecodeError> =
|
||||
decode_ssz_list_element(&ssz, 0);
|
||||
assert_eq!(result, Err(DecodeError::TooLong));
|
||||
decode_ssz(&ssz, 0);
|
||||
assert_eq!(result, Err(DecodeError::TooShort));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ssz_decode_usize() {
|
||||
let ssz = vec![0, 0, 1, 0];
|
||||
let result: usize = decode_ssz_list_element(&ssz, 0).unwrap();
|
||||
let ssz = vec![0, 0, 0, 0, 0, 0, 0, 0];
|
||||
let result: usize = decode_ssz(&ssz, 0).unwrap();
|
||||
assert_eq!(result, 0);
|
||||
|
||||
let ssz = vec![0, 0, 8, 255, 255, 255, 255, 255, 255, 255, 255];
|
||||
let result: usize = decode_ssz_list_element(&ssz, 0).unwrap();
|
||||
let result: usize = decode_ssz(&ssz, 3).unwrap();
|
||||
assert_eq!(result, 18446744073709551615);
|
||||
|
||||
let ssz = vec![0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 1];
|
||||
let ssz = vec![255, 255, 255, 255, 255, 255, 255, 255];
|
||||
let result: usize = decode_ssz(&ssz, 0).unwrap();
|
||||
assert_eq!(result, 18446744073709551615);
|
||||
|
||||
let ssz = vec![0, 0, 0, 0, 0, 0, 1];
|
||||
let result: Result<usize, DecodeError> =
|
||||
decode_ssz_list_element(&ssz, 0);
|
||||
assert_eq!(result, Err(DecodeError::TooLong));
|
||||
decode_ssz(&ssz, 0);
|
||||
assert_eq!(result, Err(DecodeError::TooShort));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decode_ssz_bounds() {
|
||||
let err: Result<u16, DecodeError> = decode_ssz(
|
||||
&vec![1],
|
||||
2
|
||||
);
|
||||
assert_eq!(err, Err(DecodeError::OutOfBounds));
|
||||
|
||||
let err: Result<u16, DecodeError> = decode_ssz(
|
||||
&vec![0, 0, 0, 0],
|
||||
3
|
||||
);
|
||||
assert_eq!(err, Err(DecodeError::TooShort));
|
||||
|
||||
let result: u16 = decode_ssz(
|
||||
&vec![0,0,0,0,1],
|
||||
3
|
||||
).unwrap();
|
||||
assert_eq!(result, 1);
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ mod impl_decode;
|
||||
pub use decode::{
|
||||
Decodable,
|
||||
DecodeError,
|
||||
decode_ssz_list_element,
|
||||
decode_ssz,
|
||||
};
|
||||
pub use encode::{
|
||||
Encodable,
|
||||
|
Loading…
Reference in New Issue
Block a user