move rlp to separate crate, port ethcore-util to it
This commit is contained in:
@@ -90,29 +90,32 @@
|
||||
extern crate rustc_serialize;
|
||||
extern crate rand;
|
||||
extern crate rocksdb;
|
||||
#[macro_use]
|
||||
extern crate heapsize;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
#[macro_use]
|
||||
extern crate itertools;
|
||||
extern crate env_logger;
|
||||
extern crate crypto as rcrypto;
|
||||
extern crate secp256k1;
|
||||
extern crate arrayvec;
|
||||
extern crate elastic_array;
|
||||
#[macro_use]
|
||||
extern crate log as rlog;
|
||||
extern crate time;
|
||||
extern crate ethcore_devtools as devtools;
|
||||
extern crate libc;
|
||||
extern crate target_info;
|
||||
extern crate bigint;
|
||||
extern crate parking_lot;
|
||||
pub extern crate using_queue;
|
||||
pub extern crate table;
|
||||
extern crate ansi_term;
|
||||
extern crate tiny_keccak;
|
||||
extern crate rlp;
|
||||
|
||||
#[macro_use]
|
||||
extern crate heapsize;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
#[macro_use]
|
||||
extern crate itertools;
|
||||
#[macro_use]
|
||||
extern crate log as rlog;
|
||||
|
||||
pub extern crate using_queue;
|
||||
pub extern crate table;
|
||||
|
||||
pub mod bloom;
|
||||
pub mod standard;
|
||||
@@ -122,7 +125,6 @@ pub mod from_json;
|
||||
pub mod common;
|
||||
pub mod error;
|
||||
pub mod bytes;
|
||||
pub mod rlp;
|
||||
pub mod misc;
|
||||
pub mod vector;
|
||||
pub mod sha3;
|
||||
@@ -144,7 +146,6 @@ mod timer;
|
||||
|
||||
pub use common::*;
|
||||
pub use misc::*;
|
||||
pub use rlp::*;
|
||||
pub use hashdb::*;
|
||||
pub use memorydb::*;
|
||||
pub use overlaydb::*;
|
||||
|
||||
@@ -1,290 +0,0 @@
|
||||
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Unified interfaces for RLP bytes operations on basic types
|
||||
//!
|
||||
|
||||
use std::mem;
|
||||
use std::fmt;
|
||||
use std::cmp::Ordering;
|
||||
use std::error::Error as StdError;
|
||||
use bigint::uint::{Uint, U128, U256};
|
||||
use hash::{H64, H128, H160, H256, H512, H520, H2048};
|
||||
use elastic_array::*;
|
||||
|
||||
/// Vector like object
|
||||
pub trait VecLike<T> {
|
||||
/// Add an element to the collection
|
||||
fn vec_push(&mut self, value: T);
|
||||
|
||||
/// Add a slice to the collection
|
||||
fn vec_extend(&mut self, slice: &[T]);
|
||||
}
|
||||
|
||||
impl<T> VecLike<T> for Vec<T> where T: Copy {
|
||||
fn vec_push(&mut self, value: T) {
|
||||
Vec::<T>::push(self, value)
|
||||
}
|
||||
|
||||
fn vec_extend(&mut self, slice: &[T]) {
|
||||
Vec::<T>::extend_from_slice(self, slice)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_veclike_for_elastic_array {
|
||||
($from: ident) => {
|
||||
impl<T> VecLike<T> for $from<T> where T: Copy {
|
||||
fn vec_push(&mut self, value: T) {
|
||||
$from::<T>::push(self, value)
|
||||
}
|
||||
fn vec_extend(&mut self, slice: &[T]) {
|
||||
$from::<T>::append_slice(self, slice)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_veclike_for_elastic_array!(ElasticArray16);
|
||||
impl_veclike_for_elastic_array!(ElasticArray32);
|
||||
impl_veclike_for_elastic_array!(ElasticArray1024);
|
||||
|
||||
/// Converts given type to its shortest representation in bytes
|
||||
///
|
||||
/// TODO: optimise some conversations
|
||||
pub trait ToBytes {
|
||||
/// Serialize self to byte array
|
||||
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V);
|
||||
/// Get length of serialized data in bytes
|
||||
fn to_bytes_len(&self) -> usize;
|
||||
}
|
||||
|
||||
impl <'a> ToBytes for &'a str {
|
||||
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
|
||||
out.vec_extend(self.as_bytes());
|
||||
}
|
||||
|
||||
fn to_bytes_len(&self) -> usize {
|
||||
self.as_bytes().len()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToBytes for String {
|
||||
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
|
||||
out.vec_extend(self.as_bytes());
|
||||
}
|
||||
|
||||
fn to_bytes_len(&self) -> usize {
|
||||
self.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToBytes for u64 {
|
||||
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
|
||||
let count = self.to_bytes_len();
|
||||
for i in 0..count {
|
||||
let j = count - 1 - i;
|
||||
out.vec_push((*self >> (j * 8)) as u8);
|
||||
}
|
||||
}
|
||||
|
||||
fn to_bytes_len(&self) -> usize { 8 - self.leading_zeros() as usize / 8 }
|
||||
}
|
||||
|
||||
impl ToBytes for bool {
|
||||
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
|
||||
out.vec_push(if *self { 1u8 } else { 0u8 })
|
||||
}
|
||||
|
||||
fn to_bytes_len(&self) -> usize { 1 }
|
||||
}
|
||||
|
||||
macro_rules! impl_map_to_bytes {
|
||||
($from: ident, $to: ty) => {
|
||||
impl ToBytes for $from {
|
||||
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
|
||||
(*self as $to).to_bytes(out)
|
||||
}
|
||||
|
||||
fn to_bytes_len(&self) -> usize { (*self as $to).to_bytes_len() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_map_to_bytes!(usize, u64);
|
||||
impl_map_to_bytes!(u16, u64);
|
||||
impl_map_to_bytes!(u32, u64);
|
||||
|
||||
macro_rules! impl_uint_to_bytes {
|
||||
($name: ident) => {
|
||||
impl ToBytes for $name {
|
||||
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
|
||||
let count = self.to_bytes_len();
|
||||
for i in 0..count {
|
||||
let j = count - 1 - i;
|
||||
out.vec_push(self.byte(j));
|
||||
}
|
||||
}
|
||||
fn to_bytes_len(&self) -> usize { (self.bits() + 7) / 8 }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_uint_to_bytes!(U256);
|
||||
impl_uint_to_bytes!(U128);
|
||||
|
||||
macro_rules! impl_hash_to_bytes {
|
||||
($name: ident) => {
|
||||
impl ToBytes for $name {
|
||||
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
|
||||
out.vec_extend(&self);
|
||||
}
|
||||
fn to_bytes_len(&self) -> usize { self.len() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_hash_to_bytes!(H64);
|
||||
impl_hash_to_bytes!(H128);
|
||||
impl_hash_to_bytes!(H160);
|
||||
impl_hash_to_bytes!(H256);
|
||||
impl_hash_to_bytes!(H512);
|
||||
impl_hash_to_bytes!(H520);
|
||||
impl_hash_to_bytes!(H2048);
|
||||
|
||||
/// Error returned when `FromBytes` conversation goes wrong
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum FromBytesError {
|
||||
/// Expected more RLP data
|
||||
DataIsTooShort,
|
||||
/// Extra bytes after the end of the last item
|
||||
DataIsTooLong,
|
||||
/// Integer-representation is non-canonically prefixed with zero byte(s).
|
||||
ZeroPrefixedInt,
|
||||
}
|
||||
|
||||
impl StdError for FromBytesError {
|
||||
fn description(&self) -> &str { "from_bytes error" }
|
||||
}
|
||||
|
||||
impl fmt::Display for FromBytesError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt::Debug::fmt(&self, f)
|
||||
}
|
||||
}
|
||||
|
||||
/// Alias for the result of `FromBytes` trait
|
||||
pub type FromBytesResult<T> = Result<T, FromBytesError>;
|
||||
|
||||
/// Converts to given type from its bytes representation
|
||||
///
|
||||
/// TODO: check size of bytes before conversation and return appropriate error
|
||||
pub trait FromBytes: Sized {
|
||||
/// Create a value from bytes
|
||||
fn from_bytes(bytes: &[u8]) -> FromBytesResult<Self>;
|
||||
}
|
||||
|
||||
impl FromBytes for String {
|
||||
fn from_bytes(bytes: &[u8]) -> FromBytesResult<String> {
|
||||
Ok(::std::str::from_utf8(bytes).unwrap().to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_uint_from_bytes {
|
||||
($to: ident) => {
|
||||
impl FromBytes for $to {
|
||||
fn from_bytes(bytes: &[u8]) -> FromBytesResult<$to> {
|
||||
match bytes.len() {
|
||||
0 => Ok(0),
|
||||
l if l <= mem::size_of::<$to>() => {
|
||||
if bytes[0] == 0 {
|
||||
return Err(FromBytesError::ZeroPrefixedInt)
|
||||
}
|
||||
let mut res = 0 as $to;
|
||||
for i in 0..l {
|
||||
let shift = (l - 1 - i) * 8;
|
||||
res = res + ((bytes[i] as $to) << shift);
|
||||
}
|
||||
Ok(res)
|
||||
}
|
||||
_ => Err(FromBytesError::DataIsTooLong)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromBytes for bool {
|
||||
fn from_bytes(bytes: &[u8]) -> FromBytesResult<bool> {
|
||||
match bytes.len() {
|
||||
0 => Ok(false),
|
||||
1 => Ok(bytes[0] != 0),
|
||||
_ => Err(FromBytesError::DataIsTooLong),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//impl_uint_from_bytes!(u8);
|
||||
impl_uint_from_bytes!(u16);
|
||||
impl_uint_from_bytes!(u32);
|
||||
impl_uint_from_bytes!(u64);
|
||||
impl_uint_from_bytes!(usize);
|
||||
|
||||
macro_rules! impl_uint_from_bytes {
|
||||
($name: ident, $size: expr) => {
|
||||
impl FromBytes for $name {
|
||||
fn from_bytes(bytes: &[u8]) -> FromBytesResult<$name> {
|
||||
if !bytes.is_empty() && bytes[0] == 0 {
|
||||
Err(FromBytesError::ZeroPrefixedInt)
|
||||
} else if bytes.len() <= $size {
|
||||
Ok($name::from(bytes))
|
||||
} else {
|
||||
Err(FromBytesError::DataIsTooLong)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_uint_from_bytes!(U256, 32);
|
||||
impl_uint_from_bytes!(U128, 16);
|
||||
|
||||
macro_rules! impl_hash_from_bytes {
|
||||
($name: ident, $size: expr) => {
|
||||
impl FromBytes for $name {
|
||||
fn from_bytes(bytes: &[u8]) -> FromBytesResult<$name> {
|
||||
match bytes.len().cmp(&$size) {
|
||||
Ordering::Less => Err(FromBytesError::DataIsTooShort),
|
||||
Ordering::Greater => Err(FromBytesError::DataIsTooLong),
|
||||
Ordering::Equal => {
|
||||
let mut t = [0u8; $size];
|
||||
t.copy_from_slice(bytes);
|
||||
Ok($name(t))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_hash_from_bytes!(H64, 8);
|
||||
impl_hash_from_bytes!(H128, 16);
|
||||
impl_hash_from_bytes!(H160, 20);
|
||||
impl_hash_from_bytes!(H256, 32);
|
||||
impl_hash_from_bytes!(H512, 64);
|
||||
impl_hash_from_bytes!(H520, 65);
|
||||
impl_hash_from_bytes!(H2048, 256);
|
||||
|
||||
@@ -1,106 +0,0 @@
|
||||
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Contains RLPs used for compression.
|
||||
|
||||
use rlp::rlpcompression::InvalidRlpSwapper;
|
||||
|
||||
lazy_static! {
|
||||
/// Swapper for snapshot compression.
|
||||
pub static ref SNAPSHOT_RLP_SWAPPER: InvalidRlpSwapper<'static> = InvalidRlpSwapper::new(EMPTY_RLPS, INVALID_RLPS);
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
/// Swapper with common long RLPs, up to 127 can be added.
|
||||
pub static ref BLOCKS_RLP_SWAPPER: InvalidRlpSwapper<'static> = InvalidRlpSwapper::new(COMMON_RLPS, INVALID_RLPS);
|
||||
}
|
||||
|
||||
static EMPTY_RLPS: &'static [&'static [u8]] = &[
|
||||
// RLP of SHA3_NULL_RLP
|
||||
&[160, 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, 91, 72, 224, 27, 153, 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33],
|
||||
// RLP of SHA3_EMPTY
|
||||
&[160, 197, 210, 70, 1, 134, 247, 35, 60, 146, 126, 125, 178, 220, 199, 3, 192, 229, 0, 182, 83, 202, 130, 39, 59, 123, 250, 216, 4, 93, 133, 164, 112]
|
||||
];
|
||||
|
||||
static COMMON_RLPS: &'static [&'static [u8]] = &[
|
||||
// RLP of SHA3_NULL_RLP
|
||||
&[160, 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, 91, 72, 224, 27, 153, 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33],
|
||||
// RLP of SHA3_EMPTY
|
||||
&[160, 197, 210, 70, 1, 134, 247, 35, 60, 146, 126, 125, 178, 220, 199, 3, 192, 229, 0, 182, 83, 202, 130, 39, 59, 123, 250, 216, 4, 93, 133, 164, 112],
|
||||
// Other RLPs found in blocks DB using the test below.
|
||||
&[160, 29, 204, 77, 232, 222, 199, 93, 122, 171, 133, 181, 103, 182, 204, 212, 26, 211, 18, 69, 27, 148, 138, 116, 19, 240, 161, 66, 253, 64, 212, 147, 71],
|
||||
&[148, 50, 190, 52, 59, 148, 248, 96, 18, 77, 196, 254, 226, 120, 253, 203, 211, 140, 16, 45, 136],
|
||||
&[148, 82, 188, 68, 213, 55, 131, 9, 238, 42, 191, 21, 57, 191, 113, 222, 27, 125, 123, 227, 181],
|
||||
&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||
];
|
||||
|
||||
static INVALID_RLPS: &'static [&'static [u8]] = &[&[0x81, 0x0], &[0x81, 0x1], &[0x81, 0x2], &[0x81, 0x3], &[0x81, 0x4], &[0x81, 0x5], &[0x81, 0x6], &[0x81, 0x7], &[0x81, 0x8], &[0x81, 0x9], &[0x81, 0xa], &[0x81, 0xb], &[0x81, 0xc], &[0x81, 0xd], &[0x81, 0xe], &[0x81, 0xf], &[0x81, 0x10], &[0x81, 0x11], &[0x81, 0x12], &[0x81, 0x13], &[0x81, 0x14], &[0x81, 0x15], &[0x81, 0x16], &[0x81, 0x17], &[0x81, 0x18], &[0x81, 0x19], &[0x81, 0x1a], &[0x81, 0x1b], &[0x81, 0x1c], &[0x81, 0x1d], &[0x81, 0x1e], &[0x81, 0x1f], &[0x81, 0x20], &[0x81, 0x21], &[0x81, 0x22], &[0x81, 0x23], &[0x81, 0x24], &[0x81, 0x25], &[0x81, 0x26], &[0x81, 0x27], &[0x81, 0x28], &[0x81, 0x29], &[0x81, 0x2a], &[0x81, 0x2b], &[0x81, 0x2c], &[0x81, 0x2d], &[0x81, 0x2e], &[0x81, 0x2f], &[0x81, 0x30], &[0x81, 0x31], &[0x81, 0x32], &[0x81, 0x33], &[0x81, 0x34], &[0x81, 0x35], &[0x81, 0x36], &[0x81, 0x37], &[0x81, 0x38], &[0x81, 0x39], &[0x81, 0x3a], &[0x81, 0x3b], &[0x81, 0x3c], &[0x81, 0x3d], &[0x81, 0x3e], &[0x81, 0x3f], &[0x81, 0x40], &[0x81, 0x41], &[0x81, 0x42], &[0x81, 0x43], &[0x81, 0x44], &[0x81, 0x45], &[0x81, 0x46], &[0x81, 0x47], &[0x81, 0x48], &[0x81, 0x49], &[0x81, 0x4a], &[0x81, 0x4b], &[0x81, 0x4c], &[0x81, 0x4d], &[0x81, 0x4e], &[0x81, 0x4f], &[0x81, 0x50], &[0x81, 0x51], &[0x81, 0x52], &[0x81, 0x53], &[0x81, 0x54], &[0x81, 0x55], &[0x81, 0x56], &[0x81, 0x57], &[0x81, 0x58], &[0x81, 0x59], &[0x81, 0x5a], &[0x81, 0x5b], &[0x81, 0x5c], &[0x81, 0x5d], &[0x81, 0x5e], &[0x81, 0x5f], &[0x81, 0x60], &[0x81, 0x61], &[0x81, 0x62], &[0x81, 0x63], &[0x81, 0x64], &[0x81, 0x65], &[0x81, 0x66], &[0x81, 0x67], &[0x81, 0x68], &[0x81, 0x69], &[0x81, 0x6a], &[0x81, 0x6b], &[0x81, 0x6c], &[0x81, 0x6d], &[0x81, 0x6e], &[0x81, 0x6f], &[0x81, 0x70], &[0x81, 0x71], &[0x81, 0x72], &[0x81, 0x73], &[0x81, 0x74], &[0x81, 0x75], &[0x81, 0x76], &[0x81, 0x77], &[0x81, 0x78], &[0x81, 0x79], &[0x81, 0x7a], &[0x81, 0x7b], &[0x81, 0x7c], &[0x81, 0x7d], &[0x81, 0x7e]];
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn analyze_db() {
|
||||
use rlp::{UntrustedRlp, View};
|
||||
use std::collections::HashMap;
|
||||
use kvdb::*;
|
||||
|
||||
let path = "db path".to_string();
|
||||
let values: Vec<_> = Database::open_default(&path).unwrap().iter(Some(2)).map(|(_, v)| v).collect();
|
||||
let mut rlp_counts: HashMap<_, u32> = HashMap::new();
|
||||
let mut rlp_sizes: HashMap<_, u32> = HashMap::new();
|
||||
|
||||
fn flat_rlp<'a>(acc: &mut Vec<UntrustedRlp<'a>>, rlp: UntrustedRlp<'a>) {
|
||||
match rlp.is_data() {
|
||||
true => if rlp.size()>=70 {
|
||||
match rlp.data() {
|
||||
Ok(x) => flat_rlp(acc, UntrustedRlp::new(x)),
|
||||
_ => acc.push(rlp),
|
||||
}
|
||||
} else {
|
||||
acc.push(rlp);
|
||||
},
|
||||
false => for r in rlp.iter() { flat_rlp(acc, r); },
|
||||
}
|
||||
}
|
||||
|
||||
fn space_saving(bytes: &[u8]) -> u32 {
|
||||
let l = bytes.len() as u32;
|
||||
match l >= 2 {
|
||||
true => l-2,
|
||||
false => 0,
|
||||
}
|
||||
}
|
||||
|
||||
for v in &values {
|
||||
let rlp = UntrustedRlp::new(v);
|
||||
let mut flat = Vec::new();
|
||||
flat_rlp(&mut flat, rlp);
|
||||
for r in &flat {
|
||||
*rlp_counts.entry(r.as_raw()).or_insert(0) += 1;
|
||||
*rlp_sizes.entry(r.as_raw()).or_insert(0) += space_saving(r.as_raw());
|
||||
}
|
||||
}
|
||||
let mut size_vec: Vec<_> = rlp_sizes.iter().collect();
|
||||
size_vec.sort_by(|a, b| b.1.cmp(a.1));
|
||||
|
||||
// Exclude rare large RLPs.
|
||||
for v in size_vec.iter().filter(|v| rlp_counts.get(v.0).unwrap()>&100).take(20) {
|
||||
println!("{:?}, {:?}", v, rlp_counts.get(v.0).unwrap());
|
||||
}
|
||||
println!("DONE");
|
||||
}
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Rlp serialization module
|
||||
//!
|
||||
//! Allows encoding, decoding, and view onto rlp-slice
|
||||
//!
|
||||
//!# What should you use when?
|
||||
//!
|
||||
//!### Use `encode` function when:
|
||||
//! * You want to encode something inline.
|
||||
//! * You do not work on big set of data.
|
||||
//! * You want to encode whole data structure at once.
|
||||
//!
|
||||
//!### Use `decode` function when:
|
||||
//! * You want to decode something inline.
|
||||
//! * You do not work on big set of data.
|
||||
//! * You want to decode whole rlp at once.
|
||||
//!
|
||||
//!### Use `RlpStream` when:
|
||||
//! * You want to encode something in portions.
|
||||
//! * You encode a big set of data.
|
||||
//!
|
||||
//!### Use `Rlp` when:
|
||||
//! * You are working on trusted data (not corrupted).
|
||||
//! * You want to get view onto rlp-slice.
|
||||
//! * You don't want to decode whole rlp at once.
|
||||
//!
|
||||
//!### Use `UntrustedRlp` when:
|
||||
//! * You are working on untrusted data (~corrupted).
|
||||
//! * You need to handle data corruption errors.
|
||||
//! * You are working on input data.
|
||||
//! * You want to get view onto rlp-slice.
|
||||
//! * You don't want to decode whole rlp at once.
|
||||
|
||||
pub mod rlptraits;
|
||||
mod rlperrors;
|
||||
mod rlpin;
|
||||
mod untrusted_rlp;
|
||||
mod rlpstream;
|
||||
mod rlpcompression;
|
||||
mod commonrlps;
|
||||
mod bytes;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
pub use self::rlperrors::DecoderError;
|
||||
pub use self::rlptraits::{Decoder, Decodable, View, Stream, Encodable, Encoder, RlpEncodable, RlpDecodable, Compressible};
|
||||
pub use self::untrusted_rlp::{UntrustedRlp, UntrustedRlpIterator, PayloadInfo, Prototype};
|
||||
pub use self::rlpin::{Rlp, RlpIterator};
|
||||
pub use self::rlpstream::RlpStream;
|
||||
pub use self::rlpcompression::RlpType;
|
||||
pub use elastic_array::ElasticArray1024;
|
||||
use super::hash::H256;
|
||||
|
||||
/// The RLP encoded empty data (used to mean "null value").
|
||||
pub const NULL_RLP: [u8; 1] = [0x80; 1];
|
||||
/// The RLP encoded empty list.
|
||||
pub const EMPTY_LIST_RLP: [u8; 1] = [0xC0; 1];
|
||||
/// The SHA3 of the RLP encoding of empty data.
|
||||
pub const SHA3_NULL_RLP: H256 = H256( [0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, 0x6e, 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, 0x21] );
|
||||
/// The SHA3 of the RLP encoding of empty list.
|
||||
pub const SHA3_EMPTY_LIST_RLP: H256 = H256( [0x1d, 0xcc, 0x4d, 0xe8, 0xde, 0xc7, 0x5d, 0x7a, 0xab, 0x85, 0xb5, 0x67, 0xb6, 0xcc, 0xd4, 0x1a, 0xd3, 0x12, 0x45, 0x1b, 0x94, 0x8a, 0x74, 0x13, 0xf0, 0xa1, 0x42, 0xfd, 0x40, 0xd4, 0x93, 0x47] );
|
||||
|
||||
/// Shortcut function to decode trusted rlp
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::rlp::*;
|
||||
///
|
||||
/// fn main () {
|
||||
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
||||
/// let animals: Vec<String> = decode(&data);
|
||||
/// assert_eq!(animals, vec!["cat".to_string(), "dog".to_string()]);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn decode<T>(bytes: &[u8]) -> T where T: RlpDecodable {
|
||||
let rlp = Rlp::new(bytes);
|
||||
rlp.as_val()
|
||||
}
|
||||
|
||||
/// Shortcut function to encode structure into rlp.
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::rlp::*;
|
||||
///
|
||||
/// fn main () {
|
||||
/// let animal = "cat";
|
||||
/// let out = encode(&animal).to_vec();
|
||||
/// assert_eq!(out, vec![0x83, b'c', b'a', b't']);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn encode<E>(object: &E) -> ElasticArray1024<u8> where E: RlpEncodable {
|
||||
let mut stream = RlpStream::new();
|
||||
stream.append(object);
|
||||
stream.drain()
|
||||
}
|
||||
@@ -1,245 +0,0 @@
|
||||
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use rlp::{UntrustedRlp, View, Compressible, encode, ElasticArray1024, Stream, RlpStream};
|
||||
use rlp::commonrlps::{BLOCKS_RLP_SWAPPER, SNAPSHOT_RLP_SWAPPER};
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Stores RLPs used for compression
|
||||
pub struct InvalidRlpSwapper<'a> {
|
||||
invalid_to_valid: HashMap<&'a [u8], &'a [u8]>,
|
||||
valid_to_invalid: HashMap<&'a [u8], &'a [u8]>,
|
||||
}
|
||||
|
||||
impl<'a> InvalidRlpSwapper<'a> {
|
||||
/// Construct a swapper from a list of common RLPs
|
||||
pub fn new(rlps_to_swap: &[&'a [u8]], invalid_rlps: &[&'a [u8]]) -> Self {
|
||||
if rlps_to_swap.len() > 0x7e {
|
||||
panic!("Invalid usage, only 127 RLPs can be swappable.");
|
||||
}
|
||||
let mut invalid_to_valid = HashMap::new();
|
||||
let mut valid_to_invalid = HashMap::new();
|
||||
for (&rlp, &invalid) in rlps_to_swap.iter().zip(invalid_rlps.iter()) {
|
||||
invalid_to_valid.insert(invalid, rlp);
|
||||
valid_to_invalid.insert(rlp, invalid);
|
||||
}
|
||||
InvalidRlpSwapper {
|
||||
invalid_to_valid: invalid_to_valid,
|
||||
valid_to_invalid: valid_to_invalid
|
||||
}
|
||||
}
|
||||
/// Get a valid RLP corresponding to an invalid one
|
||||
fn get_valid(&self, invalid_rlp: &[u8]) -> Option<&[u8]> {
|
||||
self.invalid_to_valid.get(invalid_rlp).cloned()
|
||||
}
|
||||
/// Get an invalid RLP corresponding to a valid one
|
||||
fn get_invalid(&self, valid_rlp: &[u8]) -> Option<&[u8]> {
|
||||
self.valid_to_invalid.get(valid_rlp).cloned()
|
||||
}
|
||||
}
|
||||
|
||||
/// Type of RLP indicating its origin database.
|
||||
pub enum RlpType {
|
||||
/// RLP used in blocks database.
|
||||
Blocks,
|
||||
/// RLP used in snapshots.
|
||||
Snapshot,
|
||||
}
|
||||
|
||||
fn to_elastic(slice: &[u8]) -> ElasticArray1024<u8> {
|
||||
let mut out = ElasticArray1024::new();
|
||||
out.append_slice(slice);
|
||||
out
|
||||
}
|
||||
|
||||
fn map_rlp<F>(rlp: &UntrustedRlp, f: F) -> Option<ElasticArray1024<u8>> where
|
||||
F: Fn(&UntrustedRlp) -> Option<ElasticArray1024<u8>> {
|
||||
match rlp.iter()
|
||||
.fold((false, RlpStream::new_list(rlp.item_count())),
|
||||
|(is_some, mut acc), subrlp| {
|
||||
let new = f(&subrlp);
|
||||
if let Some(ref insert) = new {
|
||||
acc.append_raw(&insert[..], 1);
|
||||
} else {
|
||||
acc.append_raw(subrlp.as_raw(), 1);
|
||||
}
|
||||
(is_some || new.is_some(), acc)
|
||||
}) {
|
||||
(true, s) => Some(s.drain()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Replace common RLPs with invalid shorter ones.
|
||||
fn simple_compress(rlp: &UntrustedRlp, swapper: &InvalidRlpSwapper) -> ElasticArray1024<u8> {
|
||||
if rlp.is_data() {
|
||||
to_elastic(swapper.get_invalid(rlp.as_raw()).unwrap_or_else(|| rlp.as_raw()))
|
||||
} else {
|
||||
map_rlp(rlp, |r| Some(simple_compress(r, swapper))).unwrap_or_else(|| to_elastic(rlp.as_raw()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Recover valid RLP from a compressed form.
|
||||
fn simple_decompress(rlp: &UntrustedRlp, swapper: &InvalidRlpSwapper) -> ElasticArray1024<u8> {
|
||||
if rlp.is_data() {
|
||||
to_elastic(swapper.get_valid(rlp.as_raw()).unwrap_or_else(|| rlp.as_raw()))
|
||||
} else {
|
||||
map_rlp(rlp, |r| Some(simple_decompress(r, swapper))).unwrap_or_else(|| to_elastic(rlp.as_raw()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Replace common RLPs with invalid shorter ones, None if no compression achieved.
|
||||
/// Tries to compress data insides.
|
||||
fn deep_compress(rlp: &UntrustedRlp, swapper: &InvalidRlpSwapper) -> Option<ElasticArray1024<u8>> {
|
||||
let simple_swap = ||
|
||||
swapper.get_invalid(rlp.as_raw()).map(to_elastic);
|
||||
if rlp.is_data() {
|
||||
// Try to treat the inside as RLP.
|
||||
return match rlp.payload_info() {
|
||||
// Shortest decompressed account is 70, so simply try to swap the value.
|
||||
Ok(ref p) if p.value_len < 70 => simple_swap(),
|
||||
_ => {
|
||||
if let Ok(d) = rlp.data() {
|
||||
let internal_rlp = UntrustedRlp::new(d);
|
||||
if let Some(new_d) = deep_compress(&internal_rlp, swapper) {
|
||||
// If compressed put in a special list, with first element being invalid code.
|
||||
let mut rlp = RlpStream::new_list(2);
|
||||
rlp.append_raw(&[0x81, 0x7f], 1);
|
||||
rlp.append_raw(&new_d[..], 1);
|
||||
return Some(rlp.drain());
|
||||
}
|
||||
}
|
||||
simple_swap()
|
||||
},
|
||||
};
|
||||
}
|
||||
// Iterate through RLP while checking if it has been compressed.
|
||||
map_rlp(rlp, |r| deep_compress(r, swapper))
|
||||
}
|
||||
|
||||
/// Recover valid RLP from a compressed form, None if no decompression achieved.
|
||||
/// Tries to decompress compressed data insides.
|
||||
fn deep_decompress(rlp: &UntrustedRlp, swapper: &InvalidRlpSwapper) -> Option<ElasticArray1024<u8>> {
|
||||
let simple_swap = ||
|
||||
swapper.get_valid(rlp.as_raw()).map(to_elastic);
|
||||
// Simply decompress data.
|
||||
if rlp.is_data() { return simple_swap(); }
|
||||
match rlp.item_count() {
|
||||
// Look for special compressed list, which contains nested data.
|
||||
2 if rlp.at(0).map(|r| r.as_raw() == &[0x81, 0x7f]).unwrap_or(false) =>
|
||||
rlp.at(1).ok().map_or(simple_swap(),
|
||||
|r| deep_decompress(&r, swapper).map(|d| { let v = d.to_vec(); encode(&v) })),
|
||||
// Iterate through RLP while checking if it has been compressed.
|
||||
_ => map_rlp(rlp, |r| deep_decompress(r, swapper)),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
impl<'a> Compressible for UntrustedRlp<'a> {
|
||||
type DataType = RlpType;
|
||||
|
||||
fn compress(&self, t: RlpType) -> ElasticArray1024<u8> {
|
||||
match t {
|
||||
RlpType::Snapshot => simple_compress(self, &SNAPSHOT_RLP_SWAPPER),
|
||||
RlpType::Blocks => deep_compress(self, &BLOCKS_RLP_SWAPPER).unwrap_or_else(|| to_elastic(self.as_raw())),
|
||||
}
|
||||
}
|
||||
|
||||
fn decompress(&self, t: RlpType) -> ElasticArray1024<u8> {
|
||||
match t {
|
||||
RlpType::Snapshot => simple_decompress(self, &SNAPSHOT_RLP_SWAPPER),
|
||||
RlpType::Blocks => deep_decompress(self, &BLOCKS_RLP_SWAPPER).unwrap_or_else(|| to_elastic(self.as_raw())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use rlp::{UntrustedRlp, Compressible, View, RlpType};
|
||||
use rlp::rlpcompression::InvalidRlpSwapper;
|
||||
|
||||
#[test]
|
||||
fn invalid_rlp_swapper() {
|
||||
let to_swap: &[&[u8]] = &[&[0x83, b'c', b'a', b't'], &[0x83, b'd', b'o', b'g']];
|
||||
let invalid_rlp: &[&[u8]] = &[&[0x81, 0x00], &[0x81, 0x01]];
|
||||
let swapper = InvalidRlpSwapper::new(to_swap, invalid_rlp);
|
||||
assert_eq!(Some(invalid_rlp[0]), swapper.get_invalid(&[0x83, b'c', b'a', b't']));
|
||||
assert_eq!(None, swapper.get_invalid(&[0x83, b'b', b'a', b't']));
|
||||
assert_eq!(Some(to_swap[1]), swapper.get_valid(invalid_rlp[1]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn simple_compression() {
|
||||
let basic_account_rlp = vec![248, 68, 4, 2, 160, 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, 91, 72, 224, 27, 153, 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33, 160, 197, 210, 70, 1, 134, 247, 35, 60, 146, 126, 125, 178, 220, 199, 3, 192, 229, 0, 182, 83, 202, 130, 39, 59, 123, 250, 216, 4, 93, 133, 164, 112];
|
||||
let rlp = UntrustedRlp::new(&basic_account_rlp);
|
||||
let compressed = rlp.compress(RlpType::Snapshot).to_vec();
|
||||
assert_eq!(compressed, vec![198, 4, 2, 129, 0, 129, 1]);
|
||||
let compressed_rlp = UntrustedRlp::new(&compressed);
|
||||
assert_eq!(compressed_rlp.decompress(RlpType::Snapshot).to_vec(), basic_account_rlp);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_compression() {
|
||||
let data_basic_account_rlp = vec![184, 70, 248, 68, 4, 2, 160, 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, 91, 72, 224, 27, 153, 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33, 160, 197, 210, 70, 1, 134, 247, 35, 60, 146, 126, 125, 178, 220, 199, 3, 192, 229, 0, 182, 83, 202, 130, 39, 59, 123, 250, 216, 4, 93, 133, 164, 112];
|
||||
let data_rlp = UntrustedRlp::new(&data_basic_account_rlp);
|
||||
let compressed = data_rlp.compress(RlpType::Blocks).to_vec();
|
||||
assert_eq!(compressed, vec![201, 129, 127, 198, 4, 2, 129, 0, 129, 1]);
|
||||
let compressed_rlp = UntrustedRlp::new(&compressed);
|
||||
assert_eq!(compressed_rlp.decompress(RlpType::Blocks).to_vec(), data_basic_account_rlp);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_list_rlp() {
|
||||
let nested_basic_account_rlp = vec![228, 4, 226, 2, 160, 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, 91, 72, 224, 27, 153, 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33];
|
||||
let nested_rlp = UntrustedRlp::new(&nested_basic_account_rlp);
|
||||
let compressed = nested_rlp.compress(RlpType::Blocks).to_vec();
|
||||
assert_eq!(compressed, vec![197, 4, 195, 2, 129, 0]);
|
||||
let compressed_rlp = UntrustedRlp::new(&compressed);
|
||||
assert_eq!(compressed_rlp.decompress(RlpType::Blocks).to_vec(), nested_basic_account_rlp);
|
||||
let compressed = nested_rlp.compress(RlpType::Snapshot).to_vec();
|
||||
assert_eq!(compressed, vec![197, 4, 195, 2, 129, 0]);
|
||||
let compressed_rlp = UntrustedRlp::new(&compressed);
|
||||
assert_eq!(compressed_rlp.decompress(RlpType::Snapshot).to_vec(), nested_basic_account_rlp);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_rlp() {
|
||||
let malformed = vec![248, 81, 128, 128, 128, 128, 128, 160, 12, 51, 241, 93, 69, 218, 74, 138, 79, 115, 227, 44, 216, 81, 46, 132, 85, 235, 96, 45, 252, 48, 181, 29, 75, 141, 217, 215, 86, 160, 109, 130, 160, 140, 36, 93, 200, 109, 215, 100, 241, 246, 99, 135, 92, 168, 149, 170, 114, 9, 143, 4, 93, 25, 76, 54, 176, 119, 230, 170, 154, 105, 47, 121, 10, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128];
|
||||
let malformed_rlp = UntrustedRlp::new(&malformed);
|
||||
assert_eq!(malformed_rlp.decompress(RlpType::Blocks).to_vec(), malformed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_compression() {
|
||||
use kvdb::*;
|
||||
let path = "db to test".to_string();
|
||||
let values: Vec<_> = Database::open_default(&path).unwrap().iter(Some(2)).map(|(_, v)| v).collect();
|
||||
let mut decomp_size = 0;
|
||||
let mut comp_size = 0;
|
||||
|
||||
for v in &values {
|
||||
let rlp = UntrustedRlp::new(v);
|
||||
let compressed = rlp.compress(RlpType::Blocks).to_vec();
|
||||
comp_size += compressed.len();
|
||||
let decompressed = rlp.decompress(RlpType::Blocks).to_vec();
|
||||
decomp_size += decompressed.len();
|
||||
}
|
||||
println!("Decompressed bytes {:?}, compressed bytes: {:?}", decomp_size, comp_size);
|
||||
assert!(decomp_size > comp_size);
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::fmt;
|
||||
use std::error::Error as StdError;
|
||||
use rlp::bytes::FromBytesError;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
/// Error concerning the RLP decoder.
|
||||
pub enum DecoderError {
|
||||
/// Couldn't convert given bytes to an instance of required type.
|
||||
FromBytesError(FromBytesError),
|
||||
/// Data has additional bytes at the end of the valid RLP fragment.
|
||||
RlpIsTooBig,
|
||||
/// Data has too few bytes for valid RLP.
|
||||
RlpIsTooShort,
|
||||
/// Expect an encoded list, RLP was something else.
|
||||
RlpExpectedToBeList,
|
||||
/// Expect encoded data, RLP was something else.
|
||||
RlpExpectedToBeData,
|
||||
/// Expected a different size list.
|
||||
RlpIncorrectListLen,
|
||||
/// Data length number has a prefixed zero byte, invalid for numbers.
|
||||
RlpDataLenWithZeroPrefix,
|
||||
/// List length number has a prefixed zero byte, invalid for numbers.
|
||||
RlpListLenWithZeroPrefix,
|
||||
/// Non-canonical (longer than necessary) representation used for data or list.
|
||||
RlpInvalidIndirection,
|
||||
/// Declared length is inconsistent with data specified after.
|
||||
RlpInconsistentLengthAndData,
|
||||
/// Custom rlp decoding error.
|
||||
Custom(&'static str),
|
||||
}
|
||||
|
||||
impl StdError for DecoderError {
|
||||
fn description(&self) -> &str {
|
||||
"builder error"
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for DecoderError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt::Debug::fmt(&self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<FromBytesError> for DecoderError {
|
||||
fn from(err: FromBytesError) -> DecoderError {
|
||||
DecoderError::FromBytesError(err)
|
||||
}
|
||||
}
|
||||
@@ -1,170 +0,0 @@
|
||||
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::fmt;
|
||||
use rustc_serialize::hex::ToHex;
|
||||
use rlp::{View, DecoderError, UntrustedRlp, PayloadInfo, Prototype, RlpDecodable};
|
||||
|
||||
impl<'a> From<UntrustedRlp<'a>> for Rlp<'a> {
|
||||
fn from(rlp: UntrustedRlp<'a>) -> Rlp<'a> {
|
||||
Rlp { rlp: rlp }
|
||||
}
|
||||
}
|
||||
|
||||
/// Data-oriented view onto trusted rlp-slice.
|
||||
///
|
||||
/// Unlikely to `UntrustedRlp` doesn't bother you with error
|
||||
/// handling. It assumes that you know what you are doing.
|
||||
#[derive(Debug)]
|
||||
pub struct Rlp<'a> {
|
||||
rlp: UntrustedRlp<'a>
|
||||
}
|
||||
|
||||
impl<'a> fmt::Display for Rlp<'a> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
write!(f, "{}", self.rlp)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'view> View<'a, 'view> for Rlp<'a> where 'a: 'view {
|
||||
type Prototype = Prototype;
|
||||
type PayloadInfo = PayloadInfo;
|
||||
type Data = &'a [u8];
|
||||
type Item = Rlp<'a>;
|
||||
type Iter = RlpIterator<'a, 'view>;
|
||||
|
||||
/// Create a new instance of `Rlp`
|
||||
fn new(bytes: &'a [u8]) -> Rlp<'a> {
|
||||
Rlp {
|
||||
rlp: UntrustedRlp::new(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
fn as_raw(&'view self) -> &'a [u8] {
|
||||
self.rlp.as_raw()
|
||||
}
|
||||
|
||||
fn prototype(&self) -> Self::Prototype {
|
||||
self.rlp.prototype().unwrap()
|
||||
}
|
||||
|
||||
fn payload_info(&self) -> Self::PayloadInfo {
|
||||
self.rlp.payload_info().unwrap()
|
||||
}
|
||||
|
||||
fn data(&'view self) -> Self::Data {
|
||||
self.rlp.data().unwrap()
|
||||
}
|
||||
|
||||
fn item_count(&self) -> usize {
|
||||
self.rlp.item_count()
|
||||
}
|
||||
|
||||
fn size(&self) -> usize {
|
||||
self.rlp.size()
|
||||
}
|
||||
|
||||
fn at(&'view self, index: usize) -> Self::Item {
|
||||
From::from(self.rlp.at(index).unwrap())
|
||||
}
|
||||
|
||||
fn is_null(&self) -> bool {
|
||||
self.rlp.is_null()
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> bool {
|
||||
self.rlp.is_empty()
|
||||
}
|
||||
|
||||
fn is_list(&self) -> bool {
|
||||
self.rlp.is_list()
|
||||
}
|
||||
|
||||
fn is_data(&self) -> bool {
|
||||
self.rlp.is_data()
|
||||
}
|
||||
|
||||
fn is_int(&self) -> bool {
|
||||
self.rlp.is_int()
|
||||
}
|
||||
|
||||
fn iter(&'view self) -> Self::Iter {
|
||||
self.into_iter()
|
||||
}
|
||||
|
||||
fn as_val<T>(&self) -> Result<T, DecoderError> where T: RlpDecodable {
|
||||
self.rlp.as_val()
|
||||
}
|
||||
|
||||
fn val_at<T>(&self, index: usize) -> Result<T, DecoderError> where T: RlpDecodable {
|
||||
self.at(index).rlp.as_val()
|
||||
}
|
||||
}
|
||||
|
||||
impl <'a, 'view> Rlp<'a> where 'a: 'view {
|
||||
fn view_as_val<T, R>(r: &'view R) -> T where R: View<'a, 'view>, T: RlpDecodable {
|
||||
let res: Result<T, DecoderError> = r.as_val();
|
||||
res.unwrap_or_else(|e| panic!("DecodeError: {}, {}", e, r.as_raw().to_hex()))
|
||||
}
|
||||
|
||||
/// Decode into an object
|
||||
pub fn as_val<T>(&self) -> T where T: RlpDecodable {
|
||||
Self::view_as_val(self)
|
||||
}
|
||||
|
||||
/// Decode list item at given index into an object
|
||||
pub fn val_at<T>(&self, index: usize) -> T where T: RlpDecodable {
|
||||
Self::view_as_val(&self.at(index))
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterator over trusted rlp-slice list elements.
|
||||
pub struct RlpIterator<'a, 'view> where 'a: 'view {
|
||||
rlp: &'view Rlp<'a>,
|
||||
index: usize
|
||||
}
|
||||
|
||||
impl<'a, 'view> IntoIterator for &'view Rlp<'a> where 'a: 'view {
|
||||
type Item = Rlp<'a>;
|
||||
type IntoIter = RlpIterator<'a, 'view>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
RlpIterator {
|
||||
rlp: self,
|
||||
index: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'view> Iterator for RlpIterator<'a, 'view> {
|
||||
type Item = Rlp<'a>;
|
||||
|
||||
fn next(&mut self) -> Option<Rlp<'a>> {
|
||||
let index = self.index;
|
||||
let result = self.rlp.rlp.at(index).ok().map(From::from);
|
||||
self.index += 1;
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn break_it() {
|
||||
use common::*;
|
||||
let h: Bytes = FromHex::from_hex("f84d0589010efbef67941f79b2a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").unwrap();
|
||||
let r: Rlp = Rlp::new(&h);
|
||||
let u: U256 = r.val_at(1);
|
||||
assert_eq!(format!("{}", u), "19526463837540678066");
|
||||
}
|
||||
@@ -1,356 +0,0 @@
|
||||
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use elastic_array::*;
|
||||
use rlp::bytes::{ToBytes, VecLike};
|
||||
use rlp::{Stream, Encoder, Encodable};
|
||||
use rlp::rlptraits::{ByteEncodable, RlpEncodable};
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
struct ListInfo {
|
||||
position: usize,
|
||||
current: usize,
|
||||
max: usize,
|
||||
}
|
||||
|
||||
impl ListInfo {
|
||||
fn new(position: usize, max: usize) -> ListInfo {
|
||||
ListInfo {
|
||||
position: position,
|
||||
current: 0,
|
||||
max: max,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Appendable rlp encoder.
|
||||
pub struct RlpStream {
|
||||
unfinished_lists: ElasticArray16<ListInfo>,
|
||||
encoder: BasicEncoder,
|
||||
finished_list: bool,
|
||||
}
|
||||
|
||||
impl Default for RlpStream {
|
||||
fn default() -> Self {
|
||||
RlpStream::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Stream for RlpStream {
|
||||
fn new() -> Self {
|
||||
RlpStream {
|
||||
unfinished_lists: ElasticArray16::new(),
|
||||
encoder: BasicEncoder::new(),
|
||||
finished_list: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn new_list(len: usize) -> Self {
|
||||
let mut stream = RlpStream::new();
|
||||
stream.begin_list(len);
|
||||
stream
|
||||
}
|
||||
|
||||
fn append<'a, E>(&'a mut self, value: &E) -> &'a mut Self where E: RlpEncodable {
|
||||
self.finished_list = false;
|
||||
value.rlp_append(self);
|
||||
if !self.finished_list {
|
||||
self.note_appended(1);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
fn begin_list(&mut self, len: usize) -> &mut RlpStream {
|
||||
self.finished_list = false;
|
||||
match len {
|
||||
0 => {
|
||||
// we may finish, if the appended list len is equal 0
|
||||
self.encoder.bytes.push(0xc0u8);
|
||||
self.note_appended(1);
|
||||
self.finished_list = true;
|
||||
},
|
||||
_ => {
|
||||
let position = self.encoder.bytes.len();
|
||||
self.unfinished_lists.push(ListInfo::new(position, len));
|
||||
},
|
||||
}
|
||||
|
||||
// return chainable self
|
||||
self
|
||||
}
|
||||
|
||||
fn append_empty_data(&mut self) -> &mut RlpStream {
|
||||
// self push raw item
|
||||
self.encoder.bytes.push(0x80);
|
||||
|
||||
// try to finish and prepend the length
|
||||
self.note_appended(1);
|
||||
|
||||
// return chainable self
|
||||
self
|
||||
}
|
||||
|
||||
fn append_raw<'a>(&'a mut self, bytes: &[u8], item_count: usize) -> &'a mut RlpStream {
|
||||
// push raw items
|
||||
self.encoder.bytes.append_slice(bytes);
|
||||
|
||||
// try to finish and prepend the length
|
||||
self.note_appended(item_count);
|
||||
|
||||
// return chainable self
|
||||
self
|
||||
}
|
||||
|
||||
fn clear(&mut self) {
|
||||
// clear bytes
|
||||
self.encoder.bytes.clear();
|
||||
|
||||
// clear lists
|
||||
self.unfinished_lists.clear();
|
||||
}
|
||||
|
||||
fn is_finished(&self) -> bool {
|
||||
self.unfinished_lists.len() == 0
|
||||
}
|
||||
|
||||
fn as_raw(&self) -> &[u8] {
|
||||
&self.encoder.bytes
|
||||
}
|
||||
|
||||
fn out(self) -> Vec<u8> {
|
||||
match self.is_finished() {
|
||||
true => self.encoder.out().to_vec(),
|
||||
false => panic!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RlpStream {
|
||||
|
||||
/// Appends primitive value to the end of stream
|
||||
fn append_value<E>(&mut self, object: &E) where E: ByteEncodable {
|
||||
// encode given value and add it at the end of the stream
|
||||
self.encoder.emit_value(object);
|
||||
}
|
||||
|
||||
fn append_internal<'a, E>(&'a mut self, value: &E) -> &'a mut Self where E: Encodable {
|
||||
self.finished_list = false;
|
||||
value.rlp_append(self);
|
||||
if !self.finished_list {
|
||||
self.note_appended(1);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Try to finish lists
|
||||
fn note_appended(&mut self, inserted_items: usize) -> () {
|
||||
if self.unfinished_lists.len() == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
let back = self.unfinished_lists.len() - 1;
|
||||
let should_finish = match self.unfinished_lists.get_mut(back) {
|
||||
None => false,
|
||||
Some(ref mut x) => {
|
||||
x.current += inserted_items;
|
||||
if x.current > x.max {
|
||||
panic!("You cannot append more items then you expect!");
|
||||
}
|
||||
x.current == x.max
|
||||
}
|
||||
};
|
||||
|
||||
if should_finish {
|
||||
let x = self.unfinished_lists.pop().unwrap();
|
||||
let len = self.encoder.bytes.len() - x.position;
|
||||
self.encoder.insert_list_len_at_pos(len, x.position);
|
||||
self.note_appended(1);
|
||||
}
|
||||
self.finished_list = should_finish;
|
||||
}
|
||||
|
||||
/// Drain the object and return the underlying ElasticArray.
|
||||
pub fn drain(self) -> ElasticArray1024<u8> {
|
||||
match self.is_finished() {
|
||||
true => self.encoder.bytes,
|
||||
false => panic!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct BasicEncoder {
|
||||
bytes: ElasticArray1024<u8>,
|
||||
}
|
||||
|
||||
impl Default for BasicEncoder {
|
||||
fn default() -> Self {
|
||||
BasicEncoder::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl BasicEncoder {
|
||||
fn new() -> Self {
|
||||
BasicEncoder { bytes: ElasticArray1024::new() }
|
||||
}
|
||||
|
||||
/// inserts list prefix at given position
|
||||
/// TODO: optimise it further?
|
||||
fn insert_list_len_at_pos(&mut self, len: usize, pos: usize) -> () {
|
||||
let mut res = ElasticArray16::new();
|
||||
match len {
|
||||
0...55 => res.push(0xc0u8 + len as u8),
|
||||
_ => {
|
||||
res.push(0xf7u8 + len.to_bytes_len() as u8);
|
||||
ToBytes::to_bytes(&len, &mut res);
|
||||
}
|
||||
};
|
||||
|
||||
self.bytes.insert_slice(pos, &res);
|
||||
}
|
||||
|
||||
/// get encoded value
|
||||
fn out(self) -> ElasticArray1024<u8> {
|
||||
self.bytes
|
||||
}
|
||||
}
|
||||
|
||||
impl Encoder for BasicEncoder {
|
||||
fn emit_value<E: ByteEncodable>(&mut self, value: &E) {
|
||||
match value.bytes_len() {
|
||||
// just 0
|
||||
0 => self.bytes.push(0x80u8),
|
||||
// byte is its own encoding if < 0x80
|
||||
1 => {
|
||||
value.to_bytes(&mut self.bytes);
|
||||
let len = self.bytes.len();
|
||||
let last_byte = self.bytes[len - 1];
|
||||
if last_byte >= 0x80 {
|
||||
self.bytes.push(last_byte);
|
||||
self.bytes[len - 1] = 0x81;
|
||||
}
|
||||
}
|
||||
// (prefix + length), followed by the string
|
||||
len @ 2 ... 55 => {
|
||||
self.bytes.push(0x80u8 + len as u8);
|
||||
value.to_bytes(&mut self.bytes);
|
||||
}
|
||||
// (prefix + length of length), followed by the length, followd by the string
|
||||
len => {
|
||||
self.bytes.push(0xb7 + len.to_bytes_len() as u8);
|
||||
ToBytes::to_bytes(&len, &mut self.bytes);
|
||||
value.to_bytes(&mut self.bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn emit_raw(&mut self, bytes: &[u8]) -> () {
|
||||
self.bytes.append_slice(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ByteEncodable for T where T: ToBytes {
|
||||
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
|
||||
ToBytes::to_bytes(self, out)
|
||||
}
|
||||
|
||||
fn bytes_len(&self) -> usize {
|
||||
ToBytes::to_bytes_len(self)
|
||||
}
|
||||
}
|
||||
|
||||
struct U8Slice<'a>(&'a [u8]);
|
||||
|
||||
impl<'a> ByteEncodable for U8Slice<'a> {
|
||||
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
|
||||
out.vec_extend(self.0)
|
||||
}
|
||||
|
||||
fn bytes_len(&self) -> usize {
|
||||
self.0.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Encodable for &'a[u8] {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
s.append_value(&U8Slice(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodable for Vec<u8> {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
s.append_value(&U8Slice(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Encodable for T where T: ByteEncodable {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
s.append_value(self)
|
||||
}
|
||||
}
|
||||
|
||||
struct EncodableU8 (u8);
|
||||
|
||||
impl ByteEncodable for EncodableU8 {
|
||||
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
|
||||
if self.0 != 0 {
|
||||
out.vec_push(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
fn bytes_len(&self) -> usize {
|
||||
match self.0 { 0 => 0, _ => 1 }
|
||||
}
|
||||
}
|
||||
|
||||
impl RlpEncodable for u8 {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
s.append_value(&EncodableU8(*self))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> Encodable for &'a[T] where T: Encodable {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
s.begin_list(self.len());
|
||||
for el in self.iter() {
|
||||
s.append_internal(el);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Encodable for Vec<T> where T: Encodable {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
Encodable::rlp_append(&self.as_slice(), s);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Encodable for Option<T> where T: Encodable {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
match *self {
|
||||
None => { s.begin_list(0); },
|
||||
Some(ref x) => {
|
||||
s.begin_list(1);
|
||||
s.append_internal(x);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> RlpEncodable for T where T: Encodable {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
Encodable::rlp_append(self, s)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,377 +0,0 @@
|
||||
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Common RLP traits
|
||||
use rlp::bytes::VecLike;
|
||||
use rlp::{DecoderError, UntrustedRlp};
|
||||
use rlp::rlpstream::RlpStream;
|
||||
use elastic_array::ElasticArray1024;
|
||||
use hash::H256;
|
||||
use sha3::*;
|
||||
|
||||
/// Type is able to decode RLP.
|
||||
pub trait Decoder: Sized {
|
||||
/// Read a value from the RLP into a given type.
|
||||
fn read_value<T, F>(&self, f: &F) -> Result<T, DecoderError>
|
||||
where F: Fn(&[u8]) -> Result<T, DecoderError>;
|
||||
|
||||
/// Get underlying `UntrustedRLP` object.
|
||||
fn as_rlp(&self) -> &UntrustedRlp;
|
||||
/// Get underlying raw bytes slice.
|
||||
fn as_raw(&self) -> &[u8];
|
||||
}
|
||||
|
||||
/// RLP decodable trait
|
||||
pub trait Decodable: Sized {
|
||||
/// Decode a value from RLP bytes
|
||||
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder;
|
||||
}
|
||||
|
||||
/// Internal helper trait. Implement `Decodable` for custom types.
|
||||
pub trait RlpDecodable: Sized {
|
||||
/// Decode a value from RLP bytes
|
||||
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder;
|
||||
}
|
||||
|
||||
/// A view into RLP encoded data
|
||||
pub trait View<'a, 'view>: Sized {
|
||||
/// RLP prototype type
|
||||
type Prototype;
|
||||
/// Payload info type
|
||||
type PayloadInfo;
|
||||
/// Data type
|
||||
type Data;
|
||||
/// Item type
|
||||
type Item;
|
||||
/// Iterator type
|
||||
type Iter;
|
||||
|
||||
/// Creates a new instance of `Rlp` reader
|
||||
fn new(bytes: &'a [u8]) -> Self;
|
||||
|
||||
/// The raw data of the RLP as slice.
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::rlp::*;
|
||||
///
|
||||
/// fn main () {
|
||||
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
||||
/// let rlp = Rlp::new(&data);
|
||||
/// let dog = rlp.at(1).as_raw();
|
||||
/// assert_eq!(dog, &[0x83, b'd', b'o', b'g']);
|
||||
/// }
|
||||
/// ```
|
||||
fn as_raw(&'view self) -> &'a [u8];
|
||||
|
||||
/// Get the prototype of the RLP.
|
||||
fn prototype(&self) -> Self::Prototype;
|
||||
|
||||
/// Get payload info.
|
||||
fn payload_info(&self) -> Self::PayloadInfo;
|
||||
|
||||
/// Get underlieing data.
|
||||
fn data(&'view self) -> Self::Data;
|
||||
|
||||
/// Returns number of RLP items.
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::rlp::*;
|
||||
///
|
||||
/// fn main () {
|
||||
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
||||
/// let rlp = Rlp::new(&data);
|
||||
/// assert_eq!(rlp.item_count(), 2);
|
||||
/// let view = rlp.at(1);
|
||||
/// assert_eq!(view.item_count(), 0);
|
||||
/// }
|
||||
/// ```
|
||||
fn item_count(&self) -> usize;
|
||||
|
||||
/// Returns the number of bytes in the data, or zero if it isn't data.
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::rlp::*;
|
||||
///
|
||||
/// fn main () {
|
||||
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
||||
/// let rlp = Rlp::new(&data);
|
||||
/// assert_eq!(rlp.size(), 0);
|
||||
/// let view = rlp.at(1);
|
||||
/// assert_eq!(view.size(), 3);
|
||||
/// }
|
||||
/// ```
|
||||
fn size(&self) -> usize;
|
||||
|
||||
/// Get view onto RLP-slice at index.
|
||||
///
|
||||
/// Caches offset to given index, so access to successive
|
||||
/// slices is faster.
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::rlp::*;
|
||||
///
|
||||
/// fn main () {
|
||||
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
||||
/// let rlp = Rlp::new(&data);
|
||||
/// let dog: String = rlp.at(1).as_val();
|
||||
/// assert_eq!(dog, "dog".to_string());
|
||||
/// }
|
||||
fn at(&'view self, index: usize) -> Self::Item;
|
||||
|
||||
/// No value
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::rlp::*;
|
||||
///
|
||||
/// fn main () {
|
||||
/// let data = vec![];
|
||||
/// let rlp = Rlp::new(&data);
|
||||
/// assert!(rlp.is_null());
|
||||
/// }
|
||||
/// ```
|
||||
fn is_null(&self) -> bool;
|
||||
|
||||
/// Contains a zero-length string or zero-length list.
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::rlp::*;
|
||||
///
|
||||
/// fn main () {
|
||||
/// let data = vec![0xc0];
|
||||
/// let rlp = Rlp::new(&data);
|
||||
/// assert!(rlp.is_empty());
|
||||
/// }
|
||||
/// ```
|
||||
fn is_empty(&self) -> bool;
|
||||
|
||||
/// List value
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::rlp::*;
|
||||
///
|
||||
/// fn main () {
|
||||
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
||||
/// let rlp = Rlp::new(&data);
|
||||
/// assert!(rlp.is_list());
|
||||
/// }
|
||||
/// ```
|
||||
fn is_list(&self) -> bool;
|
||||
|
||||
/// String value
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::rlp::*;
|
||||
///
|
||||
/// fn main () {
|
||||
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
||||
/// let rlp = Rlp::new(&data);
|
||||
/// assert!(rlp.at(1).is_data());
|
||||
/// }
|
||||
/// ```
|
||||
fn is_data(&self) -> bool;
|
||||
|
||||
/// Int value
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::rlp::*;
|
||||
///
|
||||
/// fn main () {
|
||||
/// let data = vec![0xc1, 0x10];
|
||||
/// let rlp = Rlp::new(&data);
|
||||
/// assert_eq!(rlp.is_int(), false);
|
||||
/// assert_eq!(rlp.at(0).is_int(), true);
|
||||
/// }
|
||||
/// ```
|
||||
fn is_int(&self) -> bool;
|
||||
|
||||
/// Get iterator over rlp-slices
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::rlp::*;
|
||||
///
|
||||
/// fn main () {
|
||||
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
||||
/// let rlp = Rlp::new(&data);
|
||||
/// let strings: Vec<String> = rlp.iter().map(| i | i.as_val()).collect();
|
||||
/// }
|
||||
/// ```
|
||||
fn iter(&'view self) -> Self::Iter;
|
||||
|
||||
/// Decode data into an object
|
||||
fn as_val<T>(&self) -> Result<T, DecoderError> where T: RlpDecodable;
|
||||
|
||||
/// Decode data at given list index into an object
|
||||
fn val_at<T>(&self, index: usize) -> Result<T, DecoderError> where T: RlpDecodable;
|
||||
}
|
||||
|
||||
/// Raw RLP encoder
|
||||
pub trait Encoder {
|
||||
/// Write a value represented as bytes
|
||||
fn emit_value<E: ByteEncodable>(&mut self, value: &E);
|
||||
/// Write raw preencoded data to the output
|
||||
fn emit_raw(&mut self, bytes: &[u8]) -> ();
|
||||
}
|
||||
|
||||
/// Primitive data type encodable to RLP
|
||||
pub trait ByteEncodable {
|
||||
/// Serialize this object to given byte container
|
||||
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V);
|
||||
/// Get size of serialised data in bytes
|
||||
fn bytes_len(&self) -> usize;
|
||||
}
|
||||
|
||||
/// Structure encodable to RLP. Implement this trait for
|
||||
pub trait Encodable {
|
||||
/// Append a value to the stream
|
||||
fn rlp_append(&self, s: &mut RlpStream);
|
||||
|
||||
/// Get rlp-encoded bytes for this instance
|
||||
fn rlp_bytes(&self) -> ElasticArray1024<u8> {
|
||||
let mut s = RlpStream::new();
|
||||
self.rlp_append(&mut s);
|
||||
s.drain()
|
||||
}
|
||||
|
||||
/// Get the hash or RLP encoded representation
|
||||
fn rlp_sha3(&self) -> H256 { (&*self.rlp_bytes()).sha3() }
|
||||
}
|
||||
|
||||
/// Encodable wrapper trait required to handle special case of encoding a &[u8] as string and not as list
|
||||
pub trait RlpEncodable {
|
||||
/// Append a value to the stream
|
||||
fn rlp_append(&self, s: &mut RlpStream);
|
||||
}
|
||||
|
||||
/// RLP encoding stream
|
||||
pub trait Stream: Sized {
|
||||
|
||||
/// Initializes instance of empty `Stream`.
|
||||
fn new() -> Self;
|
||||
|
||||
/// Initializes the `Stream` as a list.
|
||||
fn new_list(len: usize) -> Self;
|
||||
|
||||
/// Apends value to the end of stream, chainable.
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::rlp::*;
|
||||
///
|
||||
/// fn main () {
|
||||
/// let mut stream = RlpStream::new_list(2);
|
||||
/// stream.append(&"cat").append(&"dog");
|
||||
/// let out = stream.out();
|
||||
/// assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
|
||||
/// }
|
||||
/// ```
|
||||
fn append<'a, E>(&'a mut self, value: &E) -> &'a mut Self where E: RlpEncodable;
|
||||
|
||||
/// Declare appending the list of given size, chainable.
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::rlp::*;
|
||||
///
|
||||
/// fn main () {
|
||||
/// let mut stream = RlpStream::new_list(2);
|
||||
/// stream.begin_list(2).append(&"cat").append(&"dog");
|
||||
/// stream.append(&"");
|
||||
/// let out = stream.out();
|
||||
/// assert_eq!(out, vec![0xca, 0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g', 0x80]);
|
||||
/// }
|
||||
/// ```
|
||||
fn begin_list(&mut self, len: usize) -> &mut Self;
|
||||
|
||||
/// Apends null to the end of stream, chainable.
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::rlp::*;
|
||||
///
|
||||
/// fn main () {
|
||||
/// let mut stream = RlpStream::new_list(2);
|
||||
/// stream.append_empty_data().append_empty_data();
|
||||
/// let out = stream.out();
|
||||
/// assert_eq!(out, vec![0xc2, 0x80, 0x80]);
|
||||
/// }
|
||||
/// ```
|
||||
fn append_empty_data(&mut self) -> &mut Self;
|
||||
|
||||
/// Appends raw (pre-serialised) RLP data. Use with caution. Chainable.
|
||||
fn append_raw<'a>(&'a mut self, bytes: &[u8], item_count: usize) -> &'a mut Self;
|
||||
|
||||
/// Clear the output stream so far.
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::rlp::*;
|
||||
///
|
||||
/// fn main () {
|
||||
/// let mut stream = RlpStream::new_list(3);
|
||||
/// stream.append(&"cat");
|
||||
/// stream.clear();
|
||||
/// stream.append(&"dog");
|
||||
/// let out = stream.out();
|
||||
/// assert_eq!(out, vec![0x83, b'd', b'o', b'g']);
|
||||
/// }
|
||||
fn clear(&mut self);
|
||||
|
||||
/// Returns true if stream doesnt expect any more items.
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::rlp::*;
|
||||
///
|
||||
/// fn main () {
|
||||
/// let mut stream = RlpStream::new_list(2);
|
||||
/// stream.append(&"cat");
|
||||
/// assert_eq!(stream.is_finished(), false);
|
||||
/// stream.append(&"dog");
|
||||
/// assert_eq!(stream.is_finished(), true);
|
||||
/// let out = stream.out();
|
||||
/// assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
|
||||
/// }
|
||||
fn is_finished(&self) -> bool;
|
||||
|
||||
/// Get raw encoded bytes
|
||||
fn as_raw(&self) -> &[u8];
|
||||
|
||||
/// Streams out encoded bytes.
|
||||
///
|
||||
/// panic! if stream is not finished.
|
||||
fn out(self) -> Vec<u8>;
|
||||
}
|
||||
|
||||
/// Trait for compressing and decompressing RLP by replacement of common terms.
|
||||
pub trait Compressible: Sized {
|
||||
/// Indicates the origin of RLP to be compressed.
|
||||
type DataType;
|
||||
|
||||
/// Compress given RLP type using appropriate methods.
|
||||
fn compress(&self, t: Self::DataType) -> ElasticArray1024<u8>;
|
||||
/// Decompress given RLP type using appropriate methods.
|
||||
fn decompress(&self, t: Self::DataType) -> ElasticArray1024<u8>;
|
||||
}
|
||||
@@ -1,415 +0,0 @@
|
||||
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::{fmt, cmp};
|
||||
use std::str::FromStr;
|
||||
use rlp;
|
||||
use rlp::{UntrustedRlp, RlpStream, View, Stream, DecoderError};
|
||||
use bigint::uint::U256;
|
||||
|
||||
#[test]
|
||||
fn rlp_at() {
|
||||
let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
||||
{
|
||||
let rlp = UntrustedRlp::new(&data);
|
||||
assert!(rlp.is_list());
|
||||
//let animals = <Vec<String> as rlp::RlpDecodable>::decode_untrusted(&rlp).unwrap();
|
||||
let animals: Vec<String> = rlp.as_val().unwrap();
|
||||
assert_eq!(animals, vec!["cat".to_owned(), "dog".to_owned()]);
|
||||
|
||||
let cat = rlp.at(0).unwrap();
|
||||
assert!(cat.is_data());
|
||||
assert_eq!(cat.as_raw(), &[0x83, b'c', b'a', b't']);
|
||||
//assert_eq!(String::decode_untrusted(&cat).unwrap(), "cat".to_owned());
|
||||
assert_eq!(cat.as_val::<String>().unwrap(), "cat".to_owned());
|
||||
|
||||
let dog = rlp.at(1).unwrap();
|
||||
assert!(dog.is_data());
|
||||
assert_eq!(dog.as_raw(), &[0x83, b'd', b'o', b'g']);
|
||||
//assert_eq!(String::decode_untrusted(&dog).unwrap(), "dog".to_owned());
|
||||
assert_eq!(dog.as_val::<String>().unwrap(), "dog".to_owned());
|
||||
|
||||
let cat_again = rlp.at(0).unwrap();
|
||||
assert!(cat_again.is_data());
|
||||
assert_eq!(cat_again.as_raw(), &[0x83, b'c', b'a', b't']);
|
||||
//assert_eq!(String::decode_untrusted(&cat_again).unwrap(), "cat".to_owned());
|
||||
assert_eq!(cat_again.as_val::<String>().unwrap(), "cat".to_owned());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rlp_at_err() {
|
||||
let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o'];
|
||||
{
|
||||
let rlp = UntrustedRlp::new(&data);
|
||||
assert!(rlp.is_list());
|
||||
|
||||
let cat_err = rlp.at(0).unwrap_err();
|
||||
assert_eq!(cat_err, rlp::DecoderError::RlpIsTooShort);
|
||||
|
||||
let dog_err = rlp.at(1).unwrap_err();
|
||||
assert_eq!(dog_err, rlp::DecoderError::RlpIsTooShort);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rlp_iter() {
|
||||
let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
||||
{
|
||||
let rlp = UntrustedRlp::new(&data);
|
||||
let mut iter = rlp.iter();
|
||||
|
||||
let cat = iter.next().unwrap();
|
||||
assert!(cat.is_data());
|
||||
assert_eq!(cat.as_raw(), &[0x83, b'c', b'a', b't']);
|
||||
|
||||
let dog = iter.next().unwrap();
|
||||
assert!(dog.is_data());
|
||||
assert_eq!(dog.as_raw(), &[0x83, b'd', b'o', b'g']);
|
||||
|
||||
let none = iter.next();
|
||||
assert!(none.is_none());
|
||||
|
||||
let cat_again = rlp.at(0).unwrap();
|
||||
assert!(cat_again.is_data());
|
||||
assert_eq!(cat_again.as_raw(), &[0x83, b'c', b'a', b't']);
|
||||
}
|
||||
}
|
||||
|
||||
struct ETestPair<T>(T, Vec<u8>) where T: rlp::Encodable;
|
||||
|
||||
fn run_encode_tests<T>(tests: Vec<ETestPair<T>>)
|
||||
where T: rlp::Encodable
|
||||
{
|
||||
for t in &tests {
|
||||
let res = rlp::encode(&t.0);
|
||||
assert_eq!(&res[..], &t.1[..]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_u16() {
|
||||
let tests = vec![
|
||||
ETestPair(0u16, vec![0x80u8]),
|
||||
ETestPair(0x100, vec![0x82, 0x01, 0x00]),
|
||||
ETestPair(0xffff, vec![0x82, 0xff, 0xff]),
|
||||
];
|
||||
run_encode_tests(tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_u32() {
|
||||
let tests = vec![
|
||||
ETestPair(0u32, vec![0x80u8]),
|
||||
ETestPair(0x10000, vec![0x83, 0x01, 0x00, 0x00]),
|
||||
ETestPair(0xffffff, vec![0x83, 0xff, 0xff, 0xff]),
|
||||
];
|
||||
run_encode_tests(tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_u64() {
|
||||
let tests = vec![
|
||||
ETestPair(0u64, vec![0x80u8]),
|
||||
ETestPair(0x1000000, vec![0x84, 0x01, 0x00, 0x00, 0x00]),
|
||||
ETestPair(0xFFFFFFFF, vec![0x84, 0xff, 0xff, 0xff, 0xff]),
|
||||
];
|
||||
run_encode_tests(tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_u256() {
|
||||
let tests = vec![ETestPair(U256::from(0u64), vec![0x80u8]),
|
||||
ETestPair(U256::from(0x1000000u64), vec![0x84, 0x01, 0x00, 0x00, 0x00]),
|
||||
ETestPair(U256::from(0xffffffffu64),
|
||||
vec![0x84, 0xff, 0xff, 0xff, 0xff]),
|
||||
ETestPair(U256::from_str("8090a0b0c0d0e0f00910203040506077000000000000\
|
||||
000100000000000012f0")
|
||||
.unwrap(),
|
||||
vec![0xa0, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0,
|
||||
0x09, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x77, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x12, 0xf0])];
|
||||
run_encode_tests(tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_str() {
|
||||
let tests = vec![ETestPair("cat", vec![0x83, b'c', b'a', b't']),
|
||||
ETestPair("dog", vec![0x83, b'd', b'o', b'g']),
|
||||
ETestPair("Marek", vec![0x85, b'M', b'a', b'r', b'e', b'k']),
|
||||
ETestPair("", vec![0x80]),
|
||||
ETestPair("Lorem ipsum dolor sit amet, consectetur adipisicing elit",
|
||||
vec![0xb8, 0x38, b'L', b'o', b'r', b'e', b'm', b' ', b'i',
|
||||
b'p', b's', b'u', b'm', b' ', b'd', b'o', b'l', b'o',
|
||||
b'r', b' ', b's', b'i', b't', b' ', b'a', b'm', b'e',
|
||||
b't', b',', b' ', b'c', b'o', b'n', b's', b'e', b'c',
|
||||
b't', b'e', b't', b'u', b'r', b' ', b'a', b'd', b'i',
|
||||
b'p', b'i', b's', b'i', b'c', b'i', b'n', b'g', b' ',
|
||||
b'e', b'l', b'i', b't'])];
|
||||
run_encode_tests(tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_address() {
|
||||
use hash::*;
|
||||
|
||||
let tests = vec![
|
||||
ETestPair(H160::from("ef2d6d194084c2de36e0dabfce45d046b37d1106"),
|
||||
vec![0x94, 0xef, 0x2d, 0x6d, 0x19, 0x40, 0x84, 0xc2, 0xde,
|
||||
0x36, 0xe0, 0xda, 0xbf, 0xce, 0x45, 0xd0, 0x46,
|
||||
0xb3, 0x7d, 0x11, 0x06])
|
||||
];
|
||||
run_encode_tests(tests);
|
||||
}
|
||||
|
||||
/// Vec<u8> (Bytes) is treated as a single value
|
||||
#[test]
|
||||
fn encode_vector_u8() {
|
||||
let tests = vec![
|
||||
ETestPair(vec![], vec![0x80]),
|
||||
ETestPair(vec![0u8], vec![0]),
|
||||
ETestPair(vec![0x15], vec![0x15]),
|
||||
ETestPair(vec![0x40, 0x00], vec![0x82, 0x40, 0x00]),
|
||||
];
|
||||
run_encode_tests(tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_vector_u64() {
|
||||
let tests = vec![
|
||||
ETestPair(vec![], vec![0xc0]),
|
||||
ETestPair(vec![15u64], vec![0xc1, 0x0f]),
|
||||
ETestPair(vec![1, 2, 3, 7, 0xff], vec![0xc6, 1, 2, 3, 7, 0x81, 0xff]),
|
||||
ETestPair(vec![0xffffffff, 1, 2, 3, 7, 0xff], vec![0xcb, 0x84, 0xff, 0xff, 0xff, 0xff, 1, 2, 3, 7, 0x81, 0xff]),
|
||||
];
|
||||
run_encode_tests(tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_vector_str() {
|
||||
let tests = vec![ETestPair(vec!["cat", "dog"],
|
||||
vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'])];
|
||||
run_encode_tests(tests);
|
||||
}
|
||||
|
||||
struct DTestPair<T>(T, Vec<u8>) where T: rlp::RlpDecodable + fmt::Debug + cmp::Eq;
|
||||
|
||||
fn run_decode_tests<T>(tests: Vec<DTestPair<T>>) where T: rlp::RlpDecodable + fmt::Debug + cmp::Eq {
|
||||
for t in &tests {
|
||||
let res: T = rlp::decode(&t.1);
|
||||
assert_eq!(res, t.0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Vec<u8> (Bytes) is treated as a single value
|
||||
#[test]
|
||||
fn decode_vector_u8() {
|
||||
let tests = vec![
|
||||
DTestPair(vec![], vec![0x80]),
|
||||
DTestPair(vec![0u8], vec![0]),
|
||||
DTestPair(vec![0x15], vec![0x15]),
|
||||
DTestPair(vec![0x40, 0x00], vec![0x82, 0x40, 0x00]),
|
||||
];
|
||||
run_decode_tests(tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_untrusted_u8() {
|
||||
let tests = vec![
|
||||
DTestPair(0x0u8, vec![0x80]),
|
||||
DTestPair(0x77u8, vec![0x77]),
|
||||
DTestPair(0xccu8, vec![0x81, 0xcc]),
|
||||
];
|
||||
run_decode_tests(tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_untrusted_u16() {
|
||||
let tests = vec![
|
||||
DTestPair(0x100u16, vec![0x82, 0x01, 0x00]),
|
||||
DTestPair(0xffffu16, vec![0x82, 0xff, 0xff]),
|
||||
];
|
||||
run_decode_tests(tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_untrusted_u32() {
|
||||
let tests = vec![
|
||||
DTestPair(0x10000u32, vec![0x83, 0x01, 0x00, 0x00]),
|
||||
DTestPair(0xffffffu32, vec![0x83, 0xff, 0xff, 0xff]),
|
||||
];
|
||||
run_decode_tests(tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_untrusted_u64() {
|
||||
let tests = vec![
|
||||
DTestPair(0x1000000u64, vec![0x84, 0x01, 0x00, 0x00, 0x00]),
|
||||
DTestPair(0xFFFFFFFFu64, vec![0x84, 0xff, 0xff, 0xff, 0xff]),
|
||||
];
|
||||
run_decode_tests(tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_untrusted_u256() {
|
||||
let tests = vec![DTestPair(U256::from(0u64), vec![0x80u8]),
|
||||
DTestPair(U256::from(0x1000000u64), vec![0x84, 0x01, 0x00, 0x00, 0x00]),
|
||||
DTestPair(U256::from(0xffffffffu64),
|
||||
vec![0x84, 0xff, 0xff, 0xff, 0xff]),
|
||||
DTestPair(U256::from_str("8090a0b0c0d0e0f00910203040506077000000000000\
|
||||
000100000000000012f0")
|
||||
.unwrap(),
|
||||
vec![0xa0, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0,
|
||||
0x09, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x77, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x12, 0xf0])];
|
||||
run_decode_tests(tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_untrusted_str() {
|
||||
let tests = vec![DTestPair("cat".to_owned(), vec![0x83, b'c', b'a', b't']),
|
||||
DTestPair("dog".to_owned(), vec![0x83, b'd', b'o', b'g']),
|
||||
DTestPair("Marek".to_owned(),
|
||||
vec![0x85, b'M', b'a', b'r', b'e', b'k']),
|
||||
DTestPair("".to_owned(), vec![0x80]),
|
||||
DTestPair("Lorem ipsum dolor sit amet, consectetur adipisicing elit"
|
||||
.to_owned(),
|
||||
vec![0xb8, 0x38, b'L', b'o', b'r', b'e', b'm', b' ', b'i',
|
||||
b'p', b's', b'u', b'm', b' ', b'd', b'o', b'l', b'o',
|
||||
b'r', b' ', b's', b'i', b't', b' ', b'a', b'm', b'e',
|
||||
b't', b',', b' ', b'c', b'o', b'n', b's', b'e', b'c',
|
||||
b't', b'e', b't', b'u', b'r', b' ', b'a', b'd', b'i',
|
||||
b'p', b'i', b's', b'i', b'c', b'i', b'n', b'g', b' ',
|
||||
b'e', b'l', b'i', b't'])];
|
||||
run_decode_tests(tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_untrusted_address() {
|
||||
use hash::*;
|
||||
|
||||
let tests = vec![
|
||||
DTestPair(H160::from("ef2d6d194084c2de36e0dabfce45d046b37d1106"),
|
||||
vec![0x94, 0xef, 0x2d, 0x6d, 0x19, 0x40, 0x84, 0xc2, 0xde,
|
||||
0x36, 0xe0, 0xda, 0xbf, 0xce, 0x45, 0xd0, 0x46,
|
||||
0xb3, 0x7d, 0x11, 0x06])
|
||||
];
|
||||
run_decode_tests(tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_untrusted_vector_u64() {
|
||||
let tests = vec![
|
||||
DTestPair(vec![], vec![0xc0]),
|
||||
DTestPair(vec![15u64], vec![0xc1, 0x0f]),
|
||||
DTestPair(vec![1, 2, 3, 7, 0xff], vec![0xc6, 1, 2, 3, 7, 0x81, 0xff]),
|
||||
DTestPair(vec![0xffffffff, 1, 2, 3, 7, 0xff], vec![0xcb, 0x84, 0xff, 0xff, 0xff, 0xff, 1, 2, 3, 7, 0x81, 0xff]),
|
||||
];
|
||||
run_decode_tests(tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_untrusted_vector_str() {
|
||||
let tests = vec![DTestPair(vec!["cat".to_owned(), "dog".to_owned()],
|
||||
vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'])];
|
||||
run_decode_tests(tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_untrusted_vector_of_vectors_str() {
|
||||
let tests = vec![DTestPair(vec![vec!["cat".to_owned()]],
|
||||
vec![0xc5, 0xc4, 0x83, b'c', b'a', b't'])];
|
||||
run_decode_tests(tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decoding_array() {
|
||||
let v = vec![5u16, 2u16];
|
||||
let res = rlp::encode(&v);
|
||||
let arr: [u16; 2] = rlp::decode(&res);
|
||||
assert_eq!(arr[0], 5);
|
||||
assert_eq!(arr[1], 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rlp_data_length_check()
|
||||
{
|
||||
let data = vec![0x84, b'c', b'a', b't'];
|
||||
let rlp = UntrustedRlp::new(&data);
|
||||
|
||||
let as_val: Result<String, DecoderError> = rlp.as_val();
|
||||
assert_eq!(Err(DecoderError::RlpInconsistentLengthAndData), as_val);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rlp_long_data_length_check()
|
||||
{
|
||||
let mut data: Vec<u8> = vec![0xb8, 255];
|
||||
for _ in 0..253 {
|
||||
data.push(b'c');
|
||||
}
|
||||
|
||||
let rlp = UntrustedRlp::new(&data);
|
||||
|
||||
let as_val: Result<String, DecoderError> = rlp.as_val();
|
||||
assert_eq!(Err(DecoderError::RlpInconsistentLengthAndData), as_val);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_the_exact_long_string()
|
||||
{
|
||||
let mut data: Vec<u8> = vec![0xb8, 255];
|
||||
for _ in 0..255 {
|
||||
data.push(b'c');
|
||||
}
|
||||
|
||||
let rlp = UntrustedRlp::new(&data);
|
||||
|
||||
let as_val: Result<String, DecoderError> = rlp.as_val();
|
||||
assert!(as_val.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rlp_2bytes_data_length_check()
|
||||
{
|
||||
let mut data: Vec<u8> = vec![0xb9, 2, 255]; // 512+255
|
||||
for _ in 0..700 {
|
||||
data.push(b'c');
|
||||
}
|
||||
|
||||
let rlp = UntrustedRlp::new(&data);
|
||||
|
||||
let as_val: Result<String, DecoderError> = rlp.as_val();
|
||||
assert_eq!(Err(DecoderError::RlpInconsistentLengthAndData), as_val);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rlp_nested_empty_list_encode() {
|
||||
let mut stream = RlpStream::new_list(2);
|
||||
stream.append(&(Vec::new() as Vec<u32>));
|
||||
stream.append(&40u32);
|
||||
assert_eq!(stream.drain()[..], [0xc2u8, 0xc0u8, 40u8][..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rlp_list_length_overflow() {
|
||||
let data: Vec<u8> = vec![0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00];
|
||||
let rlp = UntrustedRlp::new(&data);
|
||||
let as_val: Result<String, DecoderError> = rlp.val_at(0);
|
||||
assert_eq!(Err(DecoderError::RlpIsTooShort), as_val);
|
||||
}
|
||||
@@ -1,501 +0,0 @@
|
||||
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::fmt;
|
||||
use rustc_serialize::hex::ToHex;
|
||||
use rlp::bytes::{FromBytes, FromBytesResult, FromBytesError};
|
||||
use rlp::{View, Decoder, Decodable, DecoderError, RlpDecodable};
|
||||
|
||||
/// rlp offset
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
struct OffsetCache {
|
||||
index: usize,
|
||||
offset: usize,
|
||||
}
|
||||
|
||||
impl OffsetCache {
|
||||
fn new(index: usize, offset: usize) -> OffsetCache {
|
||||
OffsetCache {
|
||||
index: index,
|
||||
offset: offset,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
/// RLP prototype
|
||||
pub enum Prototype {
|
||||
/// Empty
|
||||
Null,
|
||||
/// Value
|
||||
Data(usize),
|
||||
/// List
|
||||
List(usize),
|
||||
}
|
||||
|
||||
/// Stores basic information about item
|
||||
pub struct PayloadInfo {
|
||||
/// Header length in bytes
|
||||
pub header_len: usize,
|
||||
/// Value length in bytes
|
||||
pub value_len: usize,
|
||||
}
|
||||
|
||||
fn calculate_payload_info(header_bytes: &[u8], len_of_len: usize) -> Result<PayloadInfo, DecoderError> {
|
||||
let header_len = 1 + len_of_len;
|
||||
match header_bytes.get(1) {
|
||||
Some(&0) => return Err(DecoderError::RlpDataLenWithZeroPrefix),
|
||||
None => return Err(DecoderError::RlpIsTooShort),
|
||||
_ => (),
|
||||
}
|
||||
if header_bytes.len() < header_len { return Err(DecoderError::RlpIsTooShort); }
|
||||
let value_len = try!(usize::from_bytes(&header_bytes[1..header_len]));
|
||||
Ok(PayloadInfo::new(header_len, value_len))
|
||||
}
|
||||
|
||||
impl PayloadInfo {
|
||||
fn new(header_len: usize, value_len: usize) -> PayloadInfo {
|
||||
PayloadInfo {
|
||||
header_len: header_len,
|
||||
value_len: value_len,
|
||||
}
|
||||
}
|
||||
|
||||
/// Total size of the RLP.
|
||||
pub fn total(&self) -> usize { self.header_len + self.value_len }
|
||||
|
||||
/// Create a new object from the given bytes RLP. The bytes
|
||||
pub fn from(header_bytes: &[u8]) -> Result<PayloadInfo, DecoderError> {
|
||||
match header_bytes.first().cloned() {
|
||||
None => Err(DecoderError::RlpIsTooShort),
|
||||
Some(0...0x7f) => Ok(PayloadInfo::new(0, 1)),
|
||||
Some(l @ 0x80...0xb7) => Ok(PayloadInfo::new(1, l as usize - 0x80)),
|
||||
Some(l @ 0xb8...0xbf) => {
|
||||
let len_of_len = l as usize - 0xb7;
|
||||
calculate_payload_info(header_bytes, len_of_len)
|
||||
}
|
||||
Some(l @ 0xc0...0xf7) => Ok(PayloadInfo::new(1, l as usize - 0xc0)),
|
||||
Some(l @ 0xf8...0xff) => {
|
||||
let len_of_len = l as usize - 0xf7;
|
||||
calculate_payload_info(header_bytes, len_of_len)
|
||||
},
|
||||
// we cant reach this place, but rust requires _ to be implemented
|
||||
_ => { unreachable!(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Data-oriented view onto rlp-slice.
|
||||
///
|
||||
/// This is immutable structere. No operations change it.
|
||||
///
|
||||
/// Should be used in places where, error handling is required,
|
||||
/// eg. on input
|
||||
#[derive(Debug)]
|
||||
pub struct UntrustedRlp<'a> {
|
||||
bytes: &'a [u8],
|
||||
offset_cache: Cell<OffsetCache>,
|
||||
count_cache: Cell<Option<usize>>,
|
||||
}
|
||||
|
||||
impl<'a> Clone for UntrustedRlp<'a> {
|
||||
fn clone(&self) -> UntrustedRlp<'a> {
|
||||
UntrustedRlp {
|
||||
bytes: self.bytes,
|
||||
offset_cache: self.offset_cache.clone(),
|
||||
count_cache: self.count_cache.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> fmt::Display for UntrustedRlp<'a> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
match self.prototype() {
|
||||
Ok(Prototype::Null) => write!(f, "null"),
|
||||
Ok(Prototype::Data(_)) => write!(f, "\"0x{}\"", self.data().unwrap().to_hex()),
|
||||
Ok(Prototype::List(len)) => {
|
||||
try!(write!(f, "["));
|
||||
for i in 0..len-1 {
|
||||
try!(write!(f, "{}, ", self.at(i).unwrap()));
|
||||
}
|
||||
try!(write!(f, "{}", self.at(len - 1).unwrap()));
|
||||
write!(f, "]")
|
||||
},
|
||||
Err(err) => write!(f, "{:?}", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'view> View<'a, 'view> for UntrustedRlp<'a> where 'a: 'view {
|
||||
type Prototype = Result<Prototype, DecoderError>;
|
||||
type PayloadInfo = Result<PayloadInfo, DecoderError>;
|
||||
type Data = Result<&'a [u8], DecoderError>;
|
||||
type Item = Result<UntrustedRlp<'a>, DecoderError>;
|
||||
type Iter = UntrustedRlpIterator<'a, 'view>;
|
||||
|
||||
//returns new instance of `UntrustedRlp`
|
||||
fn new(bytes: &'a [u8]) -> UntrustedRlp<'a> {
|
||||
UntrustedRlp {
|
||||
bytes: bytes,
|
||||
offset_cache: Cell::new(OffsetCache::new(usize::max_value(), 0)),
|
||||
count_cache: Cell::new(None)
|
||||
}
|
||||
}
|
||||
|
||||
fn as_raw(&'view self) -> &'a [u8] {
|
||||
self.bytes
|
||||
}
|
||||
|
||||
fn prototype(&self) -> Self::Prototype {
|
||||
// optimize? && return appropriate errors
|
||||
if self.is_data() {
|
||||
Ok(Prototype::Data(self.size()))
|
||||
} else if self.is_list() {
|
||||
Ok(Prototype::List(self.item_count()))
|
||||
} else {
|
||||
Ok(Prototype::Null)
|
||||
}
|
||||
}
|
||||
|
||||
fn payload_info(&self) -> Self::PayloadInfo {
|
||||
BasicDecoder::payload_info(self.bytes)
|
||||
}
|
||||
|
||||
fn data(&'view self) -> Self::Data {
|
||||
let pi = try!(BasicDecoder::payload_info(self.bytes));
|
||||
Ok(&self.bytes[pi.header_len..(pi.header_len + pi.value_len)])
|
||||
}
|
||||
|
||||
fn item_count(&self) -> usize {
|
||||
match self.is_list() {
|
||||
true => match self.count_cache.get() {
|
||||
Some(c) => c,
|
||||
None => {
|
||||
let c = self.iter().count();
|
||||
self.count_cache.set(Some(c));
|
||||
c
|
||||
}
|
||||
},
|
||||
false => 0
|
||||
}
|
||||
}
|
||||
|
||||
fn size(&self) -> usize {
|
||||
match self.is_data() {
|
||||
// TODO: No panic on malformed data, but ideally would Err on no PayloadInfo.
|
||||
true => BasicDecoder::payload_info(self.bytes).map(|b| b.value_len).unwrap_or(0),
|
||||
false => 0
|
||||
}
|
||||
}
|
||||
|
||||
fn at(&'view self, index: usize) -> Self::Item {
|
||||
if !self.is_list() {
|
||||
return Err(DecoderError::RlpExpectedToBeList);
|
||||
}
|
||||
|
||||
// move to cached position if its index is less or equal to
|
||||
// current search index, otherwise move to beginning of list
|
||||
let c = self.offset_cache.get();
|
||||
let (mut bytes, to_skip) = match c.index <= index {
|
||||
true => (try!(UntrustedRlp::consume(self.bytes, c.offset)), index - c.index),
|
||||
false => (try!(self.consume_list_prefix()), index),
|
||||
};
|
||||
|
||||
// skip up to x items
|
||||
bytes = try!(UntrustedRlp::consume_items(bytes, to_skip));
|
||||
|
||||
// update the cache
|
||||
self.offset_cache.set(OffsetCache::new(index, self.bytes.len() - bytes.len()));
|
||||
|
||||
// construct new rlp
|
||||
let found = try!(BasicDecoder::payload_info(bytes));
|
||||
Ok(UntrustedRlp::new(&bytes[0..found.header_len + found.value_len]))
|
||||
}
|
||||
|
||||
fn is_null(&self) -> bool {
|
||||
self.bytes.len() == 0
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> bool {
|
||||
!self.is_null() && (self.bytes[0] == 0xc0 || self.bytes[0] == 0x80)
|
||||
}
|
||||
|
||||
fn is_list(&self) -> bool {
|
||||
!self.is_null() && self.bytes[0] >= 0xc0
|
||||
}
|
||||
|
||||
fn is_data(&self) -> bool {
|
||||
!self.is_null() && self.bytes[0] < 0xc0
|
||||
}
|
||||
|
||||
fn is_int(&self) -> bool {
|
||||
if self.is_null() {
|
||||
return false;
|
||||
}
|
||||
|
||||
match self.bytes[0] {
|
||||
0...0x80 => true,
|
||||
0x81...0xb7 => self.bytes[1] != 0,
|
||||
b @ 0xb8...0xbf => self.bytes[1 + b as usize - 0xb7] != 0,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
fn iter(&'view self) -> Self::Iter {
|
||||
self.into_iter()
|
||||
}
|
||||
|
||||
fn as_val<T>(&self) -> Result<T, DecoderError> where T: RlpDecodable {
|
||||
// optimize, so it doesn't use clone (although This clone is cheap)
|
||||
T::decode(&BasicDecoder::new(self.clone()))
|
||||
}
|
||||
|
||||
fn val_at<T>(&self, index: usize) -> Result<T, DecoderError> where T: RlpDecodable {
|
||||
try!(self.at(index)).as_val()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> UntrustedRlp<'a> {
|
||||
/// consumes first found prefix
|
||||
fn consume_list_prefix(&self) -> Result<&'a [u8], DecoderError> {
|
||||
let item = try!(BasicDecoder::payload_info(self.bytes));
|
||||
let bytes = try!(UntrustedRlp::consume(self.bytes, item.header_len));
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
/// consumes fixed number of items
|
||||
fn consume_items(bytes: &'a [u8], items: usize) -> Result<&'a [u8], DecoderError> {
|
||||
let mut result = bytes;
|
||||
for _ in 0..items {
|
||||
let i = try!(BasicDecoder::payload_info(result));
|
||||
result = try!(UntrustedRlp::consume(result, (i.header_len + i.value_len)));
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
|
||||
/// consumes slice prefix of length `len`
|
||||
fn consume(bytes: &'a [u8], len: usize) -> Result<&'a [u8], DecoderError> {
|
||||
match bytes.len() >= len {
|
||||
true => Ok(&bytes[len..]),
|
||||
false => Err(DecoderError::RlpIsTooShort),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterator over rlp-slice list elements.
|
||||
pub struct UntrustedRlpIterator<'a, 'view> where 'a: 'view {
|
||||
rlp: &'view UntrustedRlp<'a>,
|
||||
index: usize,
|
||||
}
|
||||
|
||||
impl<'a, 'view> IntoIterator for &'view UntrustedRlp<'a> where 'a: 'view {
|
||||
type Item = UntrustedRlp<'a>;
|
||||
type IntoIter = UntrustedRlpIterator<'a, 'view>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
UntrustedRlpIterator {
|
||||
rlp: self,
|
||||
index: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'view> Iterator for UntrustedRlpIterator<'a, 'view> {
|
||||
type Item = UntrustedRlp<'a>;
|
||||
|
||||
fn next(&mut self) -> Option<UntrustedRlp<'a>> {
|
||||
let index = self.index;
|
||||
let result = self.rlp.at(index).ok();
|
||||
self.index += 1;
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
struct BasicDecoder<'a> {
|
||||
rlp: UntrustedRlp<'a>
|
||||
}
|
||||
|
||||
impl<'a> BasicDecoder<'a> {
|
||||
pub fn new(rlp: UntrustedRlp<'a>) -> BasicDecoder<'a> {
|
||||
BasicDecoder {
|
||||
rlp: rlp
|
||||
}
|
||||
}
|
||||
|
||||
/// Return first item info.
|
||||
fn payload_info(bytes: &[u8]) -> Result<PayloadInfo, DecoderError> {
|
||||
let item = try!(PayloadInfo::from(bytes));
|
||||
match item.header_len.checked_add(item.value_len) {
|
||||
Some(x) if x <= bytes.len() => Ok(item),
|
||||
_ => Err(DecoderError::RlpIsTooShort),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Decoder for BasicDecoder<'a> {
|
||||
fn read_value<T, F>(&self, f: &F) -> Result<T, DecoderError>
|
||||
where F: Fn(&[u8]) -> Result<T, DecoderError> {
|
||||
|
||||
let bytes = self.rlp.as_raw();
|
||||
|
||||
match bytes.first().cloned() {
|
||||
// RLP is too short.
|
||||
None => Err(DecoderError::RlpIsTooShort),
|
||||
// Single byte value.
|
||||
Some(l @ 0...0x7f) => Ok(try!(f(&[l]))),
|
||||
// 0-55 bytes
|
||||
Some(l @ 0x80...0xb7) => {
|
||||
let last_index_of = 1 + l as usize - 0x80;
|
||||
if bytes.len() < last_index_of {
|
||||
return Err(DecoderError::RlpInconsistentLengthAndData);
|
||||
}
|
||||
let d = &bytes[1..last_index_of];
|
||||
if l == 0x81 && d[0] < 0x80 {
|
||||
return Err(DecoderError::RlpInvalidIndirection);
|
||||
}
|
||||
Ok(try!(f(d)))
|
||||
},
|
||||
// Longer than 55 bytes.
|
||||
Some(l @ 0xb8...0xbf) => {
|
||||
let len_of_len = l as usize - 0xb7;
|
||||
let begin_of_value = 1 as usize + len_of_len;
|
||||
if bytes.len() < begin_of_value {
|
||||
return Err(DecoderError::RlpInconsistentLengthAndData);
|
||||
}
|
||||
let len = try!(usize::from_bytes(&bytes[1..begin_of_value]));
|
||||
|
||||
let last_index_of_value = begin_of_value + len;
|
||||
if bytes.len() < last_index_of_value {
|
||||
return Err(DecoderError::RlpInconsistentLengthAndData);
|
||||
}
|
||||
Ok(try!(f(&bytes[begin_of_value..last_index_of_value])))
|
||||
}
|
||||
// We are reading value, not a list!
|
||||
_ => Err(DecoderError::RlpExpectedToBeData)
|
||||
}
|
||||
}
|
||||
|
||||
fn as_raw(&self) -> &[u8] {
|
||||
self.rlp.as_raw()
|
||||
}
|
||||
|
||||
fn as_rlp(&self) -> &UntrustedRlp {
|
||||
&self.rlp
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Decodable for T where T: FromBytes {
|
||||
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
||||
decoder.read_value(&|bytes: &[u8]| Ok(try!(T::from_bytes(bytes))))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Decodable for Vec<T> where T: Decodable {
|
||||
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
||||
decoder.as_rlp().iter().map(|d| T::decode(&BasicDecoder::new(d))).collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Decodable for Option<T> where T: Decodable {
|
||||
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
||||
decoder.as_rlp().iter().map(|d| T::decode(&BasicDecoder::new(d))).collect::<Result<Vec<_>, DecoderError>>().map(|mut a| a.pop())
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for Vec<u8> {
|
||||
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
||||
decoder.read_value(&|bytes: &[u8]| Ok(bytes.to_vec()))
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_array_decodable {
|
||||
($index_type:ty, $len:expr ) => (
|
||||
impl<T> Decodable for [T; $len] where T: Decodable {
|
||||
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
||||
let decoders = decoder.as_rlp();
|
||||
|
||||
let mut result: [T; $len] = unsafe { ::std::mem::uninitialized() };
|
||||
if decoders.item_count() != $len {
|
||||
return Err(DecoderError::RlpIncorrectListLen);
|
||||
}
|
||||
|
||||
for i in 0..decoders.item_count() {
|
||||
result[i] = try!(T::decode(&BasicDecoder::new(try!(decoders.at(i)))));
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
macro_rules! impl_array_decodable_recursive {
|
||||
($index_type:ty, ) => ();
|
||||
($index_type:ty, $len:expr, $($more:expr,)*) => (
|
||||
impl_array_decodable!($index_type, $len);
|
||||
impl_array_decodable_recursive!($index_type, $($more,)*);
|
||||
);
|
||||
}
|
||||
|
||||
impl_array_decodable_recursive!(
|
||||
u8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
||||
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
|
||||
32, 40, 48, 56, 64, 72, 96, 128, 160, 192, 224,
|
||||
);
|
||||
|
||||
impl<T> RlpDecodable for T where T: Decodable {
|
||||
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
||||
Decodable::decode(decoder)
|
||||
}
|
||||
}
|
||||
|
||||
struct DecodableU8 (u8);
|
||||
|
||||
impl FromBytes for DecodableU8 {
|
||||
fn from_bytes(bytes: &[u8]) -> FromBytesResult<DecodableU8> {
|
||||
match bytes.len() {
|
||||
0 => Ok(DecodableU8(0u8)),
|
||||
1 => {
|
||||
if bytes[0] == 0 {
|
||||
return Err(FromBytesError::ZeroPrefixedInt)
|
||||
}
|
||||
Ok(DecodableU8(bytes[0]))
|
||||
}
|
||||
_ => Err(FromBytesError::DataIsTooLong)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RlpDecodable for u8 {
|
||||
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
||||
let u: DecodableU8 = try!(Decodable::decode(decoder));
|
||||
Ok(u.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use rlp::{UntrustedRlp, View};
|
||||
#[test]
|
||||
fn test_rlp_display() {
|
||||
use rustc_serialize::hex::FromHex;
|
||||
let data = "f84d0589010efbef67941f79b2a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470".from_hex().unwrap();
|
||||
let rlp = UntrustedRlp::new(&data);
|
||||
assert_eq!(format!("{}", rlp), "[\"0x05\", \"0x010efbef67941f79b2\", \"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421\", \"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470\"]");
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Wrapper around tiny-keccak crate.
|
||||
//! Wrapper around tiny-keccak crate as well as common hash constants.
|
||||
extern crate sha3 as sha3_ext;
|
||||
|
||||
use std::io;
|
||||
@@ -27,6 +27,11 @@ use self::sha3_ext::*;
|
||||
/// Get the SHA3 (i.e. Keccak) hash of the empty bytes string.
|
||||
pub const SHA3_EMPTY: H256 = H256( [0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c, 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0, 0xe5, 0x00, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b, 0x7b, 0xfa, 0xd8, 0x04, 0x5d, 0x85, 0xa4, 0x70] );
|
||||
|
||||
/// The SHA3 of the RLP encoding of empty data.
|
||||
pub const SHA3_NULL_RLP: H256 = H256( [0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, 0x6e, 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, 0x21] );
|
||||
|
||||
/// The SHA3 of the RLP encoding of empty list.
|
||||
pub const SHA3_EMPTY_LIST_RLP: H256 = H256( [0x1d, 0xcc, 0x4d, 0xe8, 0xde, 0xc7, 0x5d, 0x7a, 0xab, 0x85, 0xb5, 0x67, 0xb6, 0xcc, 0xd4, 0x1a, 0xd3, 0x12, 0x45, 0x1b, 0x94, 0x8a, 0x74, 0x13, 0xf0, 0xa1, 0x42, 0xfd, 0x40, 0xd4, 0x93, 0x47] );
|
||||
|
||||
/// Types implementing this trait are sha3able.
|
||||
///
|
||||
|
||||
@@ -84,7 +84,7 @@ pub trait Trie {
|
||||
fn root(&self) -> &H256;
|
||||
|
||||
/// Is the trie empty?
|
||||
fn is_empty(&self) -> bool { *self.root() == ::rlp::SHA3_NULL_RLP }
|
||||
fn is_empty(&self) -> bool { *self.root() == ::sha3::SHA3_NULL_RLP }
|
||||
|
||||
/// Does the trie contain a given key?
|
||||
fn contains(&self, key: &[u8]) -> Result<bool> {
|
||||
|
||||
@@ -32,11 +32,11 @@ use super::{Trie, TrieItem, TrieError};
|
||||
/// # Example
|
||||
/// ```
|
||||
/// extern crate ethcore_util as util;
|
||||
///
|
||||
/// use util::trie::*;
|
||||
/// use util::hashdb::*;
|
||||
/// use util::memorydb::*;
|
||||
/// use util::hash::*;
|
||||
/// use util::rlp::*;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let mut memdb = MemoryDB::new();
|
||||
|
||||
@@ -19,10 +19,11 @@
|
||||
use super::{TrieError, TrieMut};
|
||||
use super::node::Node as RlpNode;
|
||||
|
||||
use ::{Bytes, HashDB, H256, SHA3_NULL_RLP};
|
||||
use ::{Bytes, HashDB, H256};
|
||||
use ::bytes::ToPretty;
|
||||
use ::nibbleslice::NibbleSlice;
|
||||
use ::rlp::{Rlp, RlpStream, View, Stream};
|
||||
use ::sha3::SHA3_NULL_RLP;
|
||||
|
||||
use elastic_array::ElasticArray1024;
|
||||
|
||||
@@ -261,18 +262,18 @@ impl<'a> Index<&'a StorageHandle> for NodeStorage {
|
||||
/// # Example
|
||||
/// ```
|
||||
/// extern crate ethcore_util as util;
|
||||
///
|
||||
/// use util::trie::*;
|
||||
/// use util::hashdb::*;
|
||||
/// use util::memorydb::*;
|
||||
/// use util::hash::*;
|
||||
/// use util::rlp::*;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let mut memdb = MemoryDB::new();
|
||||
/// let mut root = H256::new();
|
||||
/// let mut t = TrieDBMut::new(&mut memdb, &mut root);
|
||||
/// assert!(t.is_empty());
|
||||
/// assert_eq!(*t.root(), SHA3_NULL_RLP);
|
||||
/// assert_eq!(*t.root(), ::util::sha3::SHA3_NULL_RLP);
|
||||
/// t.insert(b"foo", b"bar").unwrap();
|
||||
/// assert!(t.contains(b"foo").unwrap());
|
||||
/// assert_eq!(t.get(b"foo").unwrap().unwrap(), b"bar");
|
||||
@@ -951,8 +952,8 @@ mod tests {
|
||||
use hashdb::*;
|
||||
use memorydb::*;
|
||||
use super::*;
|
||||
use rlp::*;
|
||||
use bytes::ToPretty;
|
||||
use sha3::SHA3_NULL_RLP;
|
||||
use super::super::TrieMut;
|
||||
use super::super::standardmap::*;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user