2018-09-11 10:17:54 +00:00
|
|
|
use super::{
|
|
|
|
LENGTH_BYTES,
|
|
|
|
};
|
|
|
|
|
2018-09-11 11:32:23 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
2018-09-11 10:17:54 +00:00
|
|
|
pub enum DecodeError {
|
|
|
|
TooShort,
|
|
|
|
TooLong,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Decodable: Sized {
|
2018-09-18 08:47:25 +00:00
|
|
|
fn ssz_decode(bytes: &[u8], index: usize) -> Result<(Self, usize), DecodeError>;
|
2018-09-18 05:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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)
|
2018-09-18 08:47:25 +00:00
|
|
|
-> Result<(T, usize), DecodeError>
|
2018-09-18 05:53:53 +00:00
|
|
|
where T: Decodable
|
|
|
|
{
|
|
|
|
if index >= ssz_bytes.len() {
|
2018-09-24 13:35:51 +00:00
|
|
|
return Err(DecodeError::TooShort)
|
2018-09-18 05:53:53 +00:00
|
|
|
}
|
|
|
|
T::ssz_decode(ssz_bytes, index)
|
2018-09-11 10:17:54 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 08:48:09 +00:00
|
|
|
/// Decode a vector (list) of encoded bytes.
|
2018-09-11 11:32:23 +00:00
|
|
|
///
|
2018-09-18 08:48:09 +00:00
|
|
|
/// Each element in the list will be decoded and placed into the vector.
|
|
|
|
pub fn decode_ssz_list<T>(ssz_bytes: &[u8], index: usize)
|
|
|
|
-> Result<(Vec<T>, usize), DecodeError>
|
|
|
|
where T: Decodable
|
2018-09-11 10:17:54 +00:00
|
|
|
{
|
|
|
|
|
2018-09-18 08:48:09 +00:00
|
|
|
if index + LENGTH_BYTES > ssz_bytes.len() {
|
2018-09-18 13:19:45 +00:00
|
|
|
return Err(DecodeError::TooShort);
|
2018-09-18 08:48:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// get the length
|
2018-09-18 13:19:45 +00:00
|
|
|
let serialized_length = match decode_length(ssz_bytes, index, LENGTH_BYTES) {
|
2018-09-18 08:48:09 +00:00
|
|
|
Err(v) => return Err(v),
|
|
|
|
Ok(v) => v,
|
|
|
|
};
|
|
|
|
|
|
|
|
let final_len: usize = index + LENGTH_BYTES + serialized_length;
|
|
|
|
|
|
|
|
if final_len > ssz_bytes.len() {
|
2018-09-18 13:19:45 +00:00
|
|
|
return Err(DecodeError::TooShort);
|
2018-09-18 08:48:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut tmp_index = index + LENGTH_BYTES;
|
|
|
|
let mut res_vec: Vec<T> = Vec::new();
|
|
|
|
|
|
|
|
while tmp_index < final_len {
|
|
|
|
match T::ssz_decode(ssz_bytes, tmp_index) {
|
|
|
|
Err(v) => return Err(v),
|
|
|
|
Ok(v) => {
|
|
|
|
tmp_index = v.1;
|
|
|
|
res_vec.push(v.0);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((res_vec, final_len))
|
2018-09-11 10:17:54 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 11:32:23 +00:00
|
|
|
/// Given some number of bytes, interpret the first four
|
|
|
|
/// bytes as a 32-bit big-endian integer and return the
|
|
|
|
/// result.
|
2018-09-19 04:48:42 +00:00
|
|
|
pub fn decode_length(bytes: &[u8], index: usize, length_bytes: usize)
|
2018-09-11 10:17:54 +00:00
|
|
|
-> Result<usize, DecodeError>
|
|
|
|
{
|
|
|
|
if bytes.len() < length_bytes {
|
|
|
|
return Err(DecodeError::TooShort);
|
|
|
|
};
|
|
|
|
let mut len: usize = 0;
|
2018-09-18 13:19:45 +00:00
|
|
|
for i in index..index+length_bytes {
|
|
|
|
let offset = (index+length_bytes - i - 1) * 8;
|
2018-09-11 10:17:54 +00:00
|
|
|
len = ((bytes[i] as usize) << offset) | len;
|
|
|
|
};
|
|
|
|
Ok(len)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-09-11 10:25:49 +00:00
|
|
|
use super::super::encode::encode_length;
|
2018-09-11 10:17:54 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ssz_decode_length() {
|
|
|
|
let decoded = decode_length(
|
2018-09-18 08:47:25 +00:00
|
|
|
&vec![0, 0, 0, 1],
|
2018-09-18 13:19:45 +00:00
|
|
|
0,
|
2018-09-11 10:17:54 +00:00
|
|
|
LENGTH_BYTES);
|
|
|
|
assert_eq!(decoded.unwrap(), 1);
|
|
|
|
|
|
|
|
let decoded = decode_length(
|
2018-09-18 08:47:25 +00:00
|
|
|
&vec![0, 0, 1, 0],
|
2018-09-18 13:19:45 +00:00
|
|
|
0,
|
2018-09-11 10:17:54 +00:00
|
|
|
LENGTH_BYTES);
|
|
|
|
assert_eq!(decoded.unwrap(), 256);
|
|
|
|
|
|
|
|
let decoded = decode_length(
|
2018-09-18 08:47:25 +00:00
|
|
|
&vec![0, 0, 1, 255],
|
2018-09-18 13:19:45 +00:00
|
|
|
0,
|
2018-09-11 10:17:54 +00:00
|
|
|
LENGTH_BYTES);
|
|
|
|
assert_eq!(decoded.unwrap(), 511);
|
|
|
|
|
|
|
|
let decoded = decode_length(
|
2018-09-18 08:47:25 +00:00
|
|
|
&vec![255, 255, 255, 255],
|
2018-09-18 13:19:45 +00:00
|
|
|
0,
|
2018-09-11 10:17:54 +00:00
|
|
|
LENGTH_BYTES);
|
2018-09-18 08:47:25 +00:00
|
|
|
assert_eq!(decoded.unwrap(), 4294967295);
|
2018-09-11 10:17:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_encode_decode_length() {
|
|
|
|
let params: Vec<usize> = vec![
|
|
|
|
0, 1, 2, 3, 7, 8, 16,
|
|
|
|
2^8, 2^8 + 1,
|
|
|
|
2^16, 2^16 + 1,
|
|
|
|
2^24, 2^24 + 1,
|
|
|
|
2^32,
|
|
|
|
];
|
|
|
|
for i in params {
|
|
|
|
let decoded = decode_length(
|
|
|
|
&encode_length(i, LENGTH_BYTES),
|
2018-09-18 13:19:45 +00:00
|
|
|
0,
|
2018-09-11 10:17:54 +00:00
|
|
|
LENGTH_BYTES).unwrap();
|
|
|
|
assert_eq!(i, decoded);
|
|
|
|
}
|
|
|
|
}
|
2018-09-18 13:19:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_decode_ssz_list() {
|
|
|
|
// u16
|
|
|
|
let v: Vec<u16> = vec![10, 10, 10, 10];
|
|
|
|
let decoded: (Vec<u16>, usize) = decode_ssz_list(
|
|
|
|
&vec![0, 0, 0, 8, 0, 10, 0, 10, 0, 10, 0, 10],
|
|
|
|
0
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(decoded.0, v);
|
|
|
|
assert_eq!(decoded.1, 12);
|
|
|
|
|
|
|
|
// u32
|
|
|
|
let v: Vec<u32> = vec![10, 10, 10, 10];
|
|
|
|
let decoded: (Vec<u32>, usize) = decode_ssz_list(
|
|
|
|
&vec![
|
|
|
|
0, 0, 0, 16,
|
|
|
|
0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10
|
|
|
|
],
|
|
|
|
0
|
|
|
|
).unwrap();
|
|
|
|
assert_eq!(decoded.0, v);
|
|
|
|
assert_eq!(decoded.1, 20);
|
|
|
|
|
|
|
|
|
|
|
|
// u64
|
|
|
|
let v: Vec<u64> = vec![10,10,10,10];
|
|
|
|
let decoded: (Vec<u64>, usize) = decode_ssz_list(
|
|
|
|
&vec![0, 0, 0, 32,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 10,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 10,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 10,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 10,
|
|
|
|
],
|
|
|
|
0
|
|
|
|
).unwrap();
|
|
|
|
assert_eq!(decoded.0, v);
|
|
|
|
assert_eq!(decoded.1, 36);
|
|
|
|
|
|
|
|
// Check that it can accept index
|
|
|
|
let v: Vec<usize> = vec![15,15,15,15];
|
|
|
|
let decoded: (Vec<usize>, usize) = decode_ssz_list(
|
|
|
|
&vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
|
|
|
0, 0, 0, 32,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 15,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 15,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 15,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 15,
|
|
|
|
],
|
|
|
|
10
|
|
|
|
).unwrap();
|
|
|
|
assert_eq!(decoded.0, v);
|
|
|
|
assert_eq!(decoded.1, 46);
|
|
|
|
|
|
|
|
// Check that length > bytes throws error
|
|
|
|
let decoded: Result<(Vec<usize>, usize), DecodeError> = decode_ssz_list(
|
|
|
|
&vec![0, 0, 0, 32,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 15,
|
|
|
|
],
|
|
|
|
0
|
|
|
|
);
|
|
|
|
assert_eq!(decoded, Err(DecodeError::TooShort));
|
|
|
|
|
|
|
|
// Check that incorrect index throws error
|
|
|
|
let decoded: Result<(Vec<usize>, usize), DecodeError> = decode_ssz_list(
|
|
|
|
&vec![
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 15,
|
|
|
|
],
|
|
|
|
16
|
|
|
|
);
|
|
|
|
assert_eq!(decoded, Err(DecodeError::TooShort));
|
|
|
|
}
|
2018-09-11 10:17:54 +00:00
|
|
|
}
|