rlp serialization refactor (#4873)
* fixed naming of rlp modules * RlpStream cleanup * appending short rlp lists (0...55 bytes) is 25% faster * RlpStream does not use bytes module, nor trait Stream * removed unused code from rlp module * compiling ethcore-util with new rlp serialization * compiling parity with new rlp serialization * fixed compiling ethcore-light with new rlp serialization * fixed compiling ethsync with new rlp serialization * removed redundant comment, print * removed redundant double-space * replace usage of WriteBytesExt with ByteOrder
This commit is contained in:
@@ -10,3 +10,4 @@ elastic-array = { git = "https://github.com/ethcore/elastic-array" }
|
||||
ethcore-bigint = { path = "../bigint" }
|
||||
lazy_static = "0.2"
|
||||
rustc-serialize = "0.3"
|
||||
byteorder = "1.0"
|
||||
|
||||
@@ -17,152 +17,9 @@
|
||||
//! Unified interfaces for RLP bytes operations on basic types
|
||||
//!
|
||||
|
||||
use std::mem;
|
||||
use std::fmt;
|
||||
use std::cmp::Ordering;
|
||||
use std::{mem, fmt, cmp};
|
||||
use std::error::Error as StdError;
|
||||
use bigint::prelude::{Uint, U128, U256, 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);
|
||||
use bigint::prelude::{U128, U256, H64, H128, H160, H256, H512, H520, H2048};
|
||||
|
||||
/// Error returned when `FromBytes` conversation goes wrong
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
@@ -268,9 +125,9 @@ macro_rules! impl_hash_from_bytes {
|
||||
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 => {
|
||||
cmp::Ordering::Less => Err(FromBytesError::DataIsTooShort),
|
||||
cmp::Ordering::Greater => Err(FromBytesError::DataIsTooLong),
|
||||
cmp::Ordering::Equal => {
|
||||
let mut t = [0u8; $size];
|
||||
t.copy_from_slice(bytes);
|
||||
Ok($name(t))
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
//! Contains RLPs used for compression.
|
||||
|
||||
use rlpcompression::InvalidRlpSwapper;
|
||||
use compression::InvalidRlpSwapper;
|
||||
|
||||
lazy_static! {
|
||||
/// Swapper for snapshot compression.
|
||||
@@ -47,4 +47,4 @@ static COMMON_RLPS: &'static [&'static [u8]] = &[
|
||||
&[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]];
|
||||
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]];
|
||||
@@ -14,11 +14,10 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use ::{UntrustedRlp, View, Compressible, encode, Stream, RlpStream};
|
||||
use commonrlps::{BLOCKS_RLP_SWAPPER, SNAPSHOT_RLP_SWAPPER};
|
||||
|
||||
use std::collections::HashMap;
|
||||
use elastic_array::ElasticArray1024;
|
||||
use common::{BLOCKS_RLP_SWAPPER, SNAPSHOT_RLP_SWAPPER};
|
||||
use {UntrustedRlp, View, Compressible, encode, RlpStream};
|
||||
|
||||
/// Stores RLPs used for compression
|
||||
pub struct InvalidRlpSwapper<'a> {
|
||||
@@ -149,8 +148,6 @@ fn deep_decompress(rlp: &UntrustedRlp, swapper: &InvalidRlpSwapper) -> Option<El
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
impl<'a> Compressible for UntrustedRlp<'a> {
|
||||
type DataType = RlpType;
|
||||
|
||||
@@ -171,8 +168,8 @@ impl<'a> Compressible for UntrustedRlp<'a> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use ::{UntrustedRlp, Compressible, View, RlpType};
|
||||
use rlpcompression::InvalidRlpSwapper;
|
||||
use compression::InvalidRlpSwapper;
|
||||
use {UntrustedRlp, Compressible, View, RlpType};
|
||||
|
||||
#[test]
|
||||
fn invalid_rlp_swapper() {
|
||||
120
util/rlp/src/impls.rs
Normal file
120
util/rlp/src/impls.rs
Normal file
@@ -0,0 +1,120 @@
|
||||
use byteorder::{ByteOrder, BigEndian};
|
||||
use bigint::prelude::{Uint, U128, U256, H64, H128, H160, H256, H512, H520, H2048};
|
||||
use traits::Encodable;
|
||||
use stream::RlpStream;
|
||||
|
||||
impl Encodable for bool {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
if *self {
|
||||
s.encoder().encode_value(&[1]);
|
||||
} else {
|
||||
s.encoder().encode_value(&[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Encodable for &'a [u8] {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
s.encoder().encode_value(self);
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodable for Vec<u8> {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
s.encoder().encode_value(self);
|
||||
}
|
||||
}
|
||||
|
||||
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 value) => {
|
||||
s.begin_list(1);
|
||||
s.append(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodable for u8 {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
if *self != 0 {
|
||||
s.encoder().encode_value(&[*self]);
|
||||
} else {
|
||||
s.encoder().encode_value(&[]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_encodable_for_u {
|
||||
($name: ident, $func: ident, $size: expr) => {
|
||||
impl Encodable for $name {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
let leading_empty_bytes = self.leading_zeros() as usize / 8;
|
||||
let mut buffer = [0u8; $size];
|
||||
BigEndian::$func(&mut buffer, *self);
|
||||
s.encoder().encode_value(&buffer[leading_empty_bytes..]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_encodable_for_u!(u16, write_u16, 2);
|
||||
impl_encodable_for_u!(u32, write_u32, 4);
|
||||
impl_encodable_for_u!(u64, write_u64, 8);
|
||||
|
||||
impl Encodable for usize {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
(*self as u64).rlp_append(s);
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_encodable_for_hash {
|
||||
($name: ident) => {
|
||||
impl Encodable for $name {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
s.encoder().encode_value(self);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_encodable_for_hash!(H64);
|
||||
impl_encodable_for_hash!(H128);
|
||||
impl_encodable_for_hash!(H160);
|
||||
impl_encodable_for_hash!(H256);
|
||||
impl_encodable_for_hash!(H512);
|
||||
impl_encodable_for_hash!(H520);
|
||||
impl_encodable_for_hash!(H2048);
|
||||
|
||||
macro_rules! impl_encodable_for_uint {
|
||||
($name: ident, $size: expr) => {
|
||||
impl Encodable for $name {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
let leading_empty_bytes = $size - (self.bits() + 7) / 8;
|
||||
let mut buffer = [0u8; $size];
|
||||
self.to_big_endian(&mut buffer);
|
||||
s.encoder().encode_value(&buffer[leading_empty_bytes..]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_encodable_for_uint!(U256, 32);
|
||||
impl_encodable_for_uint!(U128, 16);
|
||||
|
||||
impl<'a> Encodable for &'a str {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
s.encoder().encode_value(self.as_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodable for String {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
s.encoder().encode_value(self.as_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,25 +46,7 @@
|
||||
//! * 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;
|
||||
|
||||
extern crate byteorder;
|
||||
extern crate ethcore_bigint as bigint;
|
||||
extern crate elastic_array;
|
||||
extern crate rustc_serialize;
|
||||
@@ -72,8 +54,29 @@ extern crate rustc_serialize;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
mod traits;
|
||||
mod error;
|
||||
mod rlpin;
|
||||
mod untrusted_rlp;
|
||||
mod stream;
|
||||
mod compression;
|
||||
mod common;
|
||||
mod bytes;
|
||||
mod impls;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
use std::borrow::Borrow;
|
||||
use elastic_array::ElasticArray1024;
|
||||
|
||||
pub use error::DecoderError;
|
||||
pub use traits::{Decoder, Decodable, View, Encodable, RlpDecodable, Compressible};
|
||||
pub use untrusted_rlp::{UntrustedRlp, UntrustedRlpIterator, PayloadInfo, Prototype};
|
||||
pub use rlpin::{Rlp, RlpIterator};
|
||||
pub use stream::RlpStream;
|
||||
pub use compression::RlpType;
|
||||
|
||||
/// The RLP encoded empty data (used to mean "null value").
|
||||
pub const NULL_RLP: [u8; 1] = [0x80; 1];
|
||||
/// The RLP encoded empty list.
|
||||
@@ -106,8 +109,14 @@ pub fn decode<T>(bytes: &[u8]) -> T where T: RlpDecodable {
|
||||
/// assert_eq!(out, vec![0x83, b'c', b'a', b't']);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn encode<E>(object: &E) -> ElasticArray1024<u8> where E: RlpEncodable {
|
||||
pub fn encode<E>(object: &E) -> ElasticArray1024<u8> where E: Encodable {
|
||||
let mut stream = RlpStream::new();
|
||||
stream.append(object);
|
||||
stream.drain()
|
||||
}
|
||||
|
||||
pub fn encode_list<E, K>(object: &[K]) -> ElasticArray1024<u8> where E: Encodable, K: Borrow<E> {
|
||||
let mut stream = RlpStream::new();
|
||||
stream.append_list(object);
|
||||
stream.drain()
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
use std::fmt;
|
||||
use rustc_serialize::hex::ToHex;
|
||||
use ::{View, DecoderError, UntrustedRlp, PayloadInfo, Prototype, RlpDecodable};
|
||||
use {View, DecoderError, UntrustedRlp, PayloadInfo, Prototype, RlpDecodable};
|
||||
|
||||
impl<'a> From<UntrustedRlp<'a>> for Rlp<'a> {
|
||||
fn from(rlp: UntrustedRlp<'a>) -> Rlp<'a> {
|
||||
|
||||
@@ -1,357 +0,0 @@
|
||||
// Copyright 2015-2017 Parity Technologies (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 ::{Stream, Encoder, Encodable};
|
||||
use bytes::{ToBytes, VecLike};
|
||||
use 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)
|
||||
}
|
||||
}
|
||||
|
||||
335
util/rlp/src/stream.rs
Normal file
335
util/rlp/src/stream.rs
Normal file
@@ -0,0 +1,335 @@
|
||||
// Copyright 2015-2017 Parity Technologies (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::borrow::Borrow;
|
||||
use byteorder::{ByteOrder, BigEndian};
|
||||
use elastic_array::{ElasticArray16, ElasticArray1024};
|
||||
use traits::Encodable;
|
||||
|
||||
#[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>,
|
||||
buffer: ElasticArray1024<u8>,
|
||||
finished_list: bool,
|
||||
}
|
||||
|
||||
impl Default for RlpStream {
|
||||
fn default() -> Self {
|
||||
RlpStream::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl RlpStream {
|
||||
/// Initializes instance of empty `Stream`.
|
||||
pub fn new() -> Self {
|
||||
RlpStream {
|
||||
unfinished_lists: ElasticArray16::new(),
|
||||
buffer: ElasticArray1024::new(),
|
||||
finished_list: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Initializes the `Stream` as a list.
|
||||
pub fn new_list(len: usize) -> Self {
|
||||
let mut stream = RlpStream::new();
|
||||
stream.begin_list(len);
|
||||
stream
|
||||
}
|
||||
|
||||
/// Appends value to the end of stream, chainable.
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate rlp;
|
||||
/// use 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']);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn append<'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
|
||||
}
|
||||
|
||||
/// Appends list of values to the end of stream, chainable.
|
||||
pub fn append_list<'a, E, K>(&'a mut self, values: &[K]) -> &'a mut Self where E: Encodable, K: Borrow<E> {
|
||||
self.begin_list(values.len());
|
||||
for value in values {
|
||||
self.append(value.borrow());
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Appends value to the end of stream, but do not count it as an appended item.
|
||||
/// It's useful for wrapper types
|
||||
pub fn append_internal<'a, E>(&'a mut self, value: &E) -> &'a mut Self where E: Encodable {
|
||||
value.rlp_append(self);
|
||||
self
|
||||
}
|
||||
|
||||
/// Declare appending the list of given size, chainable.
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate rlp;
|
||||
/// use 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]);
|
||||
/// }
|
||||
/// ```
|
||||
pub 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.buffer.push(0xc0u8);
|
||||
self.note_appended(1);
|
||||
self.finished_list = true;
|
||||
},
|
||||
_ => {
|
||||
// payload is longer than 1 byte only for lists > 55 bytes
|
||||
// by pushing always this 1 byte we may avoid unnecessary shift of data
|
||||
self.buffer.push(0);
|
||||
|
||||
let position = self.buffer.len();
|
||||
self.unfinished_lists.push(ListInfo::new(position, len));
|
||||
},
|
||||
}
|
||||
|
||||
// return chainable self
|
||||
self
|
||||
}
|
||||
|
||||
/// Apends null to the end of stream, chainable.
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate rlp;
|
||||
/// use 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]);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn append_empty_data(&mut self) -> &mut RlpStream {
|
||||
// self push raw item
|
||||
self.buffer.push(0x80);
|
||||
|
||||
// try to finish and prepend the length
|
||||
self.note_appended(1);
|
||||
|
||||
// return chainable self
|
||||
self
|
||||
}
|
||||
|
||||
/// Appends raw (pre-serialised) RLP data. Use with caution. Chainable.
|
||||
pub fn append_raw<'a>(&'a mut self, bytes: &[u8], item_count: usize) -> &'a mut RlpStream {
|
||||
// push raw items
|
||||
self.buffer.append_slice(bytes);
|
||||
|
||||
// try to finish and prepend the length
|
||||
self.note_appended(item_count);
|
||||
|
||||
// return chainable self
|
||||
self
|
||||
}
|
||||
|
||||
/// Clear the output stream so far.
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate rlp;
|
||||
/// use 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']);
|
||||
/// }
|
||||
pub fn clear(&mut self) {
|
||||
// clear bytes
|
||||
self.buffer.clear();
|
||||
|
||||
// clear lists
|
||||
self.unfinished_lists.clear();
|
||||
}
|
||||
|
||||
/// Returns true if stream doesnt expect any more items.
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate rlp;
|
||||
/// use 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']);
|
||||
/// }
|
||||
pub fn is_finished(&self) -> bool {
|
||||
self.unfinished_lists.len() == 0
|
||||
}
|
||||
|
||||
/// Get raw encoded bytes
|
||||
pub fn as_raw(&self) -> &[u8] {
|
||||
//&self.encoder.bytes
|
||||
&self.buffer
|
||||
}
|
||||
|
||||
/// Streams out encoded bytes.
|
||||
///
|
||||
/// panic! if stream is not finished.
|
||||
pub fn out(self) -> Vec<u8> {
|
||||
match self.is_finished() {
|
||||
//true => self.encoder.out().to_vec(),
|
||||
true => self.buffer.to_vec(),
|
||||
false => panic!()
|
||||
}
|
||||
}
|
||||
|
||||
/// 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.buffer.len() - x.position;
|
||||
self.encoder().insert_list_payload(len, x.position);
|
||||
self.note_appended(1);
|
||||
}
|
||||
self.finished_list = should_finish;
|
||||
}
|
||||
|
||||
pub fn encoder(&mut self) -> BasicEncoder {
|
||||
BasicEncoder::new(self)
|
||||
}
|
||||
|
||||
/// Drain the object and return the underlying ElasticArray.
|
||||
pub fn drain(self) -> ElasticArray1024<u8> {
|
||||
match self.is_finished() {
|
||||
true => self.buffer,
|
||||
false => panic!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BasicEncoder<'a> {
|
||||
buffer: &'a mut ElasticArray1024<u8>,
|
||||
}
|
||||
|
||||
impl<'a> BasicEncoder<'a> {
|
||||
fn new(stream: &'a mut RlpStream) -> Self {
|
||||
BasicEncoder {
|
||||
buffer: &mut stream.buffer
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_size(&mut self, size: usize, position: usize) -> u8 {
|
||||
let size = size as u32;
|
||||
let leading_empty_bytes = size.leading_zeros() as usize / 8;
|
||||
let size_bytes = 4 - leading_empty_bytes as u8;
|
||||
let mut buffer = [0u8; 4];
|
||||
BigEndian::write_u32(&mut buffer, size);
|
||||
self.buffer.insert_slice(position, &buffer[leading_empty_bytes..]);
|
||||
size_bytes as u8
|
||||
}
|
||||
|
||||
/// Inserts list prefix at given position
|
||||
fn insert_list_payload(&mut self, len: usize, pos: usize) {
|
||||
// 1 byte was already reserved for payload earlier
|
||||
match len {
|
||||
0...55 => {
|
||||
self.buffer[pos - 1] = 0xc0u8 + len as u8;
|
||||
},
|
||||
_ => {
|
||||
let inserted_bytes = self.insert_size(len, pos);
|
||||
self.buffer[pos - 1] = 0xf7u8 + inserted_bytes;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Pushes encoded value to the end of buffer
|
||||
pub fn encode_value(&mut self, value: &[u8]) {
|
||||
match value.len() {
|
||||
// just 0
|
||||
0 => self.buffer.push(0x80u8),
|
||||
// byte is its own encoding if < 0x80
|
||||
1 if value[0] < 0x80 => self.buffer.push(value[0]),
|
||||
// (prefix + length), followed by the string
|
||||
len @ 1 ... 55 => {
|
||||
self.buffer.push(0x80u8 + len as u8);
|
||||
self.buffer.append_slice(value);
|
||||
}
|
||||
// (prefix + length of length), followed by the length, followd by the string
|
||||
len => {
|
||||
self.buffer.push(0);
|
||||
let position = self.buffer.len();
|
||||
let inserted_bytes = self.insert_size(len, position);
|
||||
self.buffer[position - 1] = 0xb7 + inserted_bytes;
|
||||
self.buffer.append_slice(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,9 +15,8 @@
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::{fmt, cmp};
|
||||
use std::str::FromStr;
|
||||
use ::{Encodable, RlpDecodable, UntrustedRlp, RlpStream, View, Stream, DecoderError};
|
||||
use bigint::prelude::U256;
|
||||
use {Encodable, RlpDecodable, UntrustedRlp, RlpStream, View, DecoderError};
|
||||
|
||||
#[test]
|
||||
fn rlp_at() {
|
||||
@@ -95,6 +94,17 @@ fn run_encode_tests<T>(tests: Vec<ETestPair<T>>)
|
||||
}
|
||||
}
|
||||
|
||||
struct VETestPair<T>(Vec<T>, Vec<u8>) where T: Encodable;
|
||||
|
||||
fn run_encode_tests_list<T>(tests: Vec<VETestPair<T>>)
|
||||
where T: Encodable
|
||||
{
|
||||
for t in &tests {
|
||||
let res = super::encode_list(&t.0);
|
||||
assert_eq!(&res[..], &t.1[..]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_u16() {
|
||||
let tests = vec![
|
||||
@@ -131,9 +141,8 @@ fn encode_u256() {
|
||||
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(),
|
||||
ETestPair(("8090a0b0c0d0e0f00910203040506077000000000000\
|
||||
000100000000000012f0").into(),
|
||||
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,
|
||||
@@ -186,19 +195,19 @@ fn encode_vector_u8() {
|
||||
#[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]),
|
||||
VETestPair(vec![], vec![0xc0]),
|
||||
VETestPair(vec![15u64], vec![0xc1, 0x0f]),
|
||||
VETestPair(vec![1, 2, 3, 7, 0xff], vec![0xc6, 1, 2, 3, 7, 0x81, 0xff]),
|
||||
VETestPair(vec![0xffffffff, 1, 2, 3, 7, 0xff], vec![0xcb, 0x84, 0xff, 0xff, 0xff, 0xff, 1, 2, 3, 7, 0x81, 0xff]),
|
||||
];
|
||||
run_encode_tests(tests);
|
||||
run_encode_tests_list(tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_vector_str() {
|
||||
let tests = vec![ETestPair(vec!["cat", "dog"],
|
||||
let tests = vec![VETestPair(vec!["cat", "dog"],
|
||||
vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'])];
|
||||
run_encode_tests(tests);
|
||||
run_encode_tests_list(tests);
|
||||
}
|
||||
|
||||
struct DTestPair<T>(T, Vec<u8>) where T: RlpDecodable + fmt::Debug + cmp::Eq;
|
||||
@@ -265,9 +274,8 @@ fn decode_untrusted_u256() {
|
||||
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(),
|
||||
DTestPair(("8090a0b0c0d0e0f00910203040506077000000000000\
|
||||
000100000000000012f0").into(),
|
||||
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,
|
||||
@@ -335,7 +343,7 @@ fn decode_untrusted_vector_of_vectors_str() {
|
||||
#[test]
|
||||
fn test_decoding_array() {
|
||||
let v = vec![5u16, 2u16];
|
||||
let res = super::encode(&v);
|
||||
let res = super::encode_list(&v);
|
||||
let arr: [u16; 2] = super::decode(&res);
|
||||
assert_eq!(arr[0], 5);
|
||||
assert_eq!(arr[1], 2);
|
||||
@@ -396,7 +404,7 @@ fn test_rlp_2bytes_data_length_check()
|
||||
#[test]
|
||||
fn test_rlp_nested_empty_list_encode() {
|
||||
let mut stream = RlpStream::new_list(2);
|
||||
stream.append(&(Vec::new() as Vec<u32>));
|
||||
stream.append_list(&(Vec::new() as Vec<u32>));
|
||||
stream.append(&40u32);
|
||||
assert_eq!(stream.drain()[..], [0xc2u8, 0xc0u8, 40u8][..]);
|
||||
}
|
||||
|
||||
@@ -15,11 +15,9 @@
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Common RLP traits
|
||||
use ::{DecoderError, UntrustedRlp};
|
||||
use bytes::VecLike;
|
||||
use rlpstream::RlpStream;
|
||||
|
||||
use elastic_array::ElasticArray1024;
|
||||
use stream::RlpStream;
|
||||
use {DecoderError, UntrustedRlp};
|
||||
|
||||
/// Type is able to decode RLP.
|
||||
pub trait Decoder: Sized {
|
||||
@@ -226,23 +224,7 @@ pub trait View<'a, 'view>: Sized {
|
||||
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
|
||||
/// Structure encodable to RLP
|
||||
pub trait Encodable {
|
||||
/// Append a value to the stream
|
||||
fn rlp_append(&self, s: &mut RlpStream);
|
||||
@@ -255,112 +237,6 @@ pub trait Encodable {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 rlp;
|
||||
/// use 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 rlp;
|
||||
/// use 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 rlp;
|
||||
/// use 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 rlp;
|
||||
/// use 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 rlp;
|
||||
/// use 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.
|
||||
Reference in New Issue
Block a user