2018-09-22 02:11:10 +00:00
|
|
|
use super::ethereum_types::H256;
|
2018-09-10 22:13:44 +00:00
|
|
|
use super::{
|
|
|
|
DecodeError,
|
|
|
|
Decodable,
|
|
|
|
};
|
2018-09-22 02:11:10 +00:00
|
|
|
|
|
|
|
|
2018-09-10 22:13:44 +00:00
|
|
|
macro_rules! impl_decodable_for_uint {
|
|
|
|
($type: ident, $bit_size: expr) => {
|
|
|
|
impl Decodable for $type {
|
2018-10-20 20:15:58 +00:00
|
|
|
fn ssz_decode(bytes: &[u8], start_bytes: usize)
|
2018-09-18 08:47:25 +00:00
|
|
|
-> Result<(Self, usize), DecodeError>
|
2018-09-10 22:13:44 +00:00
|
|
|
{
|
2018-09-11 11:26:45 +00:00
|
|
|
assert!((0 < $bit_size) &
|
|
|
|
($bit_size <= 64) &
|
|
|
|
($bit_size % 8 == 0));
|
|
|
|
let max_bytes = $bit_size / 8;
|
2018-10-20 20:15:58 +00:00
|
|
|
if bytes.len() >= (start_bytes + max_bytes) {
|
|
|
|
let end_bytes = start_bytes + max_bytes;
|
2018-09-11 11:26:45 +00:00
|
|
|
let mut result: $type = 0;
|
2018-10-20 20:15:58 +00:00
|
|
|
for (index, byte) in bytes.iter().enumerate().take(end_bytes).skip(start_bytes) {
|
|
|
|
let offset = (end_bytes - index - 1) * 8;
|
|
|
|
result |= ($type::from(*byte)) << offset;
|
|
|
|
}
|
2018-09-18 08:47:25 +00:00
|
|
|
Ok((result, end_bytes))
|
2018-09-10 22:13:44 +00:00
|
|
|
} else {
|
2018-09-18 05:53:53 +00:00
|
|
|
Err(DecodeError::TooShort)
|
2018-09-10 22:13:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-11 11:26:45 +00:00
|
|
|
impl_decodable_for_uint!(u16, 16);
|
|
|
|
impl_decodable_for_uint!(u32, 32);
|
|
|
|
impl_decodable_for_uint!(u64, 64);
|
|
|
|
impl_decodable_for_uint!(usize, 64);
|
2018-09-10 22:13:44 +00:00
|
|
|
|
2018-09-23 07:22:27 +00:00
|
|
|
impl Decodable for u8 {
|
|
|
|
fn ssz_decode(bytes: &[u8], index: usize)
|
|
|
|
-> Result<(Self, usize), DecodeError>
|
|
|
|
{
|
|
|
|
if index >= bytes.len() {
|
|
|
|
Err(DecodeError::TooShort)
|
|
|
|
} else {
|
|
|
|
Ok((bytes[index], index + 1))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-22 02:11:10 +00:00
|
|
|
impl Decodable for H256 {
|
|
|
|
fn ssz_decode(bytes: &[u8], index: usize)
|
|
|
|
-> Result<(Self, usize), DecodeError>
|
|
|
|
{
|
2018-10-20 19:20:23 +00:00
|
|
|
if bytes.len() < 32 || bytes.len() - 32 < index {
|
2018-10-20 22:38:32 +00:00
|
|
|
Err(DecodeError::TooShort)
|
2018-09-22 02:11:10 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-10-20 22:38:32 +00:00
|
|
|
Ok((H256::from(&bytes[index..(index + 32)]), index + 32))
|
2018-09-22 02:11:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-11 11:26:45 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2018-09-22 02:11:10 +00:00
|
|
|
use super::*;
|
2018-09-11 11:26:45 +00:00
|
|
|
use super::super::{
|
|
|
|
DecodeError,
|
2018-09-18 05:53:53 +00:00
|
|
|
decode_ssz,
|
2018-09-11 11:26:45 +00:00
|
|
|
};
|
|
|
|
|
2018-09-22 02:11:10 +00:00
|
|
|
#[test]
|
|
|
|
fn test_ssz_decode_h256() {
|
|
|
|
/*
|
|
|
|
* Input is exact length
|
|
|
|
*/
|
|
|
|
let input = vec![42_u8; 32];
|
|
|
|
let (decoded, i) = H256::ssz_decode(&input, 0).unwrap();
|
|
|
|
assert_eq!(decoded.to_vec(), input);
|
|
|
|
assert_eq!(i, 32);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Input is too long
|
|
|
|
*/
|
|
|
|
let mut input = vec![42_u8; 32];
|
|
|
|
input.push(12);
|
|
|
|
let (decoded, i) = H256::ssz_decode(&input, 0).unwrap();
|
|
|
|
assert_eq!(decoded.to_vec()[..], input[0..32]);
|
|
|
|
assert_eq!(i, 32);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Input is too short
|
|
|
|
*/
|
|
|
|
let input = vec![42_u8; 31];
|
|
|
|
let res = H256::ssz_decode(&input, 0);
|
|
|
|
assert_eq!(res, Err(DecodeError::TooShort));
|
|
|
|
}
|
|
|
|
|
2018-09-11 11:26:45 +00:00
|
|
|
#[test]
|
|
|
|
fn test_ssz_decode_u16() {
|
2018-09-18 05:53:53 +00:00
|
|
|
let ssz = vec![0, 0];
|
2018-09-18 08:47:25 +00:00
|
|
|
|
|
|
|
let (result, index): (u16, usize) = decode_ssz(&ssz, 0).unwrap();
|
2018-09-11 11:26:45 +00:00
|
|
|
assert_eq!(result, 0);
|
2018-09-18 08:47:25 +00:00
|
|
|
assert_eq!(index, 2);
|
2018-09-11 11:26:45 +00:00
|
|
|
|
2018-09-18 05:53:53 +00:00
|
|
|
let ssz = vec![0, 16];
|
2018-09-18 08:47:25 +00:00
|
|
|
let (result, index): (u16, usize) = decode_ssz(&ssz, 0).unwrap();
|
2018-09-11 11:26:45 +00:00
|
|
|
assert_eq!(result, 16);
|
2018-09-18 08:47:25 +00:00
|
|
|
assert_eq!(index, 2);
|
2018-09-11 11:26:45 +00:00
|
|
|
|
2018-09-18 05:53:53 +00:00
|
|
|
let ssz = vec![1, 0];
|
2018-09-18 08:47:25 +00:00
|
|
|
let (result, index): (u16, usize) = decode_ssz(&ssz, 0).unwrap();
|
2018-09-11 11:26:45 +00:00
|
|
|
assert_eq!(result, 256);
|
2018-09-18 08:47:25 +00:00
|
|
|
assert_eq!(index, 2);
|
2018-09-11 11:26:45 +00:00
|
|
|
|
2018-09-18 05:53:53 +00:00
|
|
|
let ssz = vec![255, 255];
|
2018-09-18 08:47:25 +00:00
|
|
|
let (result, index): (u16, usize) = decode_ssz(&ssz, 0).unwrap();
|
|
|
|
assert_eq!(index, 2);
|
2018-09-11 11:26:45 +00:00
|
|
|
assert_eq!(result, 65535);
|
|
|
|
|
2018-09-18 05:53:53 +00:00
|
|
|
let ssz = vec![1];
|
2018-09-18 08:47:25 +00:00
|
|
|
let result: Result<(u16, usize), DecodeError> =
|
2018-09-18 05:53:53 +00:00
|
|
|
decode_ssz(&ssz, 0);
|
|
|
|
assert_eq!(result, Err(DecodeError::TooShort));
|
2018-09-11 11:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ssz_decode_u32() {
|
2018-09-18 05:53:53 +00:00
|
|
|
let ssz = vec![0, 0, 0, 0];
|
2018-09-18 08:47:25 +00:00
|
|
|
let (result, index): (u32, usize) = decode_ssz(&ssz, 0).unwrap();
|
2018-09-11 11:26:45 +00:00
|
|
|
assert_eq!(result, 0);
|
2018-09-18 08:47:25 +00:00
|
|
|
assert_eq!(index, 4);
|
2018-09-11 11:26:45 +00:00
|
|
|
|
2018-09-18 05:53:53 +00:00
|
|
|
let ssz = vec![0, 0, 1, 0];
|
2018-09-18 08:47:25 +00:00
|
|
|
let (result, index): (u32, usize) = decode_ssz(&ssz, 0).unwrap();
|
|
|
|
assert_eq!(index, 4);
|
2018-09-18 05:53:53 +00:00
|
|
|
assert_eq!(result, 256);
|
|
|
|
|
|
|
|
let ssz = vec![255, 255, 255, 0, 0, 1, 0];
|
2018-09-18 08:47:25 +00:00
|
|
|
let (result, index): (u32, usize) = decode_ssz(&ssz, 3).unwrap();
|
|
|
|
assert_eq!(index, 7);
|
2018-09-18 05:53:53 +00:00
|
|
|
assert_eq!(result, 256);
|
|
|
|
|
|
|
|
let ssz = vec![0,200, 1, 0];
|
2018-09-18 08:47:25 +00:00
|
|
|
let (result, index): (u32, usize) = decode_ssz(&ssz, 0).unwrap();
|
|
|
|
assert_eq!(index, 4);
|
2018-09-18 05:53:53 +00:00
|
|
|
assert_eq!(result, 13107456);
|
|
|
|
|
|
|
|
let ssz = vec![255, 255, 255, 255];
|
2018-09-18 08:47:25 +00:00
|
|
|
let (result, index): (u32, usize) = decode_ssz(&ssz, 0).unwrap();
|
|
|
|
assert_eq!(index, 4);
|
2018-09-11 11:26:45 +00:00
|
|
|
assert_eq!(result, 4294967295);
|
|
|
|
|
2018-09-18 05:53:53 +00:00
|
|
|
let ssz = vec![0, 0, 1];
|
2018-09-18 08:47:25 +00:00
|
|
|
let result: Result<(u32, usize), DecodeError> =
|
2018-09-18 05:53:53 +00:00
|
|
|
decode_ssz(&ssz, 0);
|
|
|
|
assert_eq!(result, Err(DecodeError::TooShort));
|
2018-09-11 11:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ssz_decode_u64() {
|
2018-09-18 05:53:53 +00:00
|
|
|
let ssz = vec![0, 0, 0, 0, 0, 0, 0, 0];
|
2018-09-18 08:47:25 +00:00
|
|
|
let (result, index): (u64, usize) = decode_ssz(&ssz, 0).unwrap();
|
|
|
|
assert_eq!(index, 8);
|
2018-09-11 11:26:45 +00:00
|
|
|
assert_eq!(result, 0);
|
|
|
|
|
2018-09-18 05:53:53 +00:00
|
|
|
let ssz = vec![255, 255, 255, 255, 255, 255, 255, 255];
|
2018-09-18 08:47:25 +00:00
|
|
|
let (result, index): (u64, usize) = decode_ssz(&ssz, 0).unwrap();
|
|
|
|
assert_eq!(index, 8);
|
2018-09-11 11:26:45 +00:00
|
|
|
assert_eq!(result, 18446744073709551615);
|
|
|
|
|
2018-09-18 08:47:25 +00:00
|
|
|
let ssz = vec![0, 0, 8, 255, 0, 0, 0, 0, 0, 0, 0];
|
|
|
|
let (result, index): (u64, usize) = decode_ssz(&ssz, 3).unwrap();
|
|
|
|
assert_eq!(index, 11);
|
2018-09-18 05:53:53 +00:00
|
|
|
assert_eq!(result, 18374686479671623680);
|
|
|
|
|
|
|
|
let ssz = vec![0,0,0,0,0,0,0];
|
2018-09-18 08:47:25 +00:00
|
|
|
let result: Result<(u64, usize), DecodeError> =
|
2018-09-18 05:53:53 +00:00
|
|
|
decode_ssz(&ssz, 0);
|
|
|
|
assert_eq!(result, Err(DecodeError::TooShort));
|
2018-09-11 11:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ssz_decode_usize() {
|
2018-09-18 05:53:53 +00:00
|
|
|
let ssz = vec![0, 0, 0, 0, 0, 0, 0, 0];
|
2018-09-18 08:47:25 +00:00
|
|
|
let (result, index): (usize, usize) = decode_ssz(&ssz, 0).unwrap();
|
|
|
|
assert_eq!(index, 8);
|
2018-09-11 11:26:45 +00:00
|
|
|
assert_eq!(result, 0);
|
|
|
|
|
2018-09-12 07:57:07 +00:00
|
|
|
let ssz = vec![0, 0, 8, 255, 255, 255, 255, 255, 255, 255, 255];
|
2018-09-18 08:47:25 +00:00
|
|
|
let (result, index): (usize, usize) = decode_ssz(&ssz, 3).unwrap();
|
|
|
|
assert_eq!(index, 11);
|
2018-09-18 05:53:53 +00:00
|
|
|
assert_eq!(result, 18446744073709551615);
|
|
|
|
|
2018-09-18 08:47:25 +00:00
|
|
|
let ssz = vec![255, 255, 255, 255, 255, 255, 255, 255, 255];
|
|
|
|
let (result, index): (usize, usize) = decode_ssz(&ssz, 0).unwrap();
|
|
|
|
assert_eq!(index, 8);
|
2018-09-11 11:26:45 +00:00
|
|
|
assert_eq!(result, 18446744073709551615);
|
|
|
|
|
2018-09-18 05:53:53 +00:00
|
|
|
let ssz = vec![0, 0, 0, 0, 0, 0, 1];
|
2018-09-18 08:47:25 +00:00
|
|
|
let result: Result<(usize, usize), DecodeError> =
|
2018-09-18 05:53:53 +00:00
|
|
|
decode_ssz(&ssz, 0);
|
|
|
|
assert_eq!(result, Err(DecodeError::TooShort));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_decode_ssz_bounds() {
|
2018-09-18 08:47:25 +00:00
|
|
|
let err: Result<(u16, usize), DecodeError> = decode_ssz(
|
2018-09-18 05:53:53 +00:00
|
|
|
&vec![1],
|
|
|
|
2
|
|
|
|
);
|
2018-10-02 06:41:10 +00:00
|
|
|
assert_eq!(err, Err(DecodeError::TooShort));
|
2018-09-18 05:53:53 +00:00
|
|
|
|
2018-09-18 08:47:25 +00:00
|
|
|
let err: Result<(u16,usize), DecodeError> = decode_ssz(
|
2018-09-18 05:53:53 +00:00
|
|
|
&vec![0, 0, 0, 0],
|
|
|
|
3
|
|
|
|
);
|
|
|
|
assert_eq!(err, Err(DecodeError::TooShort));
|
|
|
|
|
|
|
|
let result: u16 = decode_ssz(
|
|
|
|
&vec![0,0,0,0,1],
|
|
|
|
3
|
2018-09-18 08:47:25 +00:00
|
|
|
).unwrap().0;
|
2018-09-18 05:53:53 +00:00
|
|
|
assert_eq!(result, 1);
|
2018-09-11 11:26:45 +00:00
|
|
|
}
|
|
|
|
}
|