Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ script:
- if [ "$TRAVIS_RUST_VERSION" == "nightly" ]; then
cd parity-bytes/ && cargo build --no-default-features && cd ..;
cd fixed-hash/ && cargo test --no-default-features --features="rustc-hex,byteorder" && cd..;
cd rlp/ && cargo test --no-default-features && cd ..;
fi
- cd fixed-hash/ && cargo test --all-features && cd ..
- cd uint/ && cargo test --features=std,quickcheck --release && cd ..
Expand Down
14 changes: 8 additions & 6 deletions rlp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"

[dependencies]
byteorder = "1.0"
rustc-hex = {version = "2.0", default-features = false }

[dev-dependencies]
hex-literal = "0.2"
primitive-types = { path = "../primitive-types", version = "0.4", features = ["impl-rlp"] }
byteorder = { version = "1", default-features = false }
rustc-hex = { version = "2.0", default-features = false }

[features]
default = ["std"]
std = [
"byteorder/std",
"rustc-hex/std",
]
28 changes: 2 additions & 26 deletions rlp/benches/rlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// TODO: get rid of this one fine day: https://doc.rust-lang.org/nightly/edition-guide/rust-2018/module-system/path-clarity.html#an-exception
extern crate test;

use primitive_types::U256;
use rlp::{RlpStream, Rlp};
use test::Bencher;

Expand All @@ -41,29 +40,6 @@ fn bench_decode_u64_value(b: &mut Bencher) {
});
}

#[bench]
fn bench_stream_u256_value(b: &mut Bencher) {
b.iter(|| {
// u256
let mut stream = RlpStream::new();
let uint: U256 = "8090a0b0c0d0e0f00910203040506077000000000000000100000000000012f0".into();
stream.append(&uint);
let _ = stream.out();
});
}

#[bench]
fn bench_decode_u256_value(b: &mut Bencher) {
b.iter(|| {
// u256
let data = 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];
let rlp = Rlp::new(&data);
let _ : U256 = rlp.as_val().unwrap();
});
}

#[bench]
fn bench_stream_nested_empty_lists(b: &mut Bencher) {
b.iter(|| {
Expand Down Expand Up @@ -105,13 +81,13 @@ fn bench_stream_1000_empty_lists(b: &mut Bencher) {
fn bench_decode_1000_values(b: &mut Bencher) {
let mut stream = RlpStream::new_list(1000);
for _ in 0..1000 {
stream.append(&U256::from(1));
stream.append(&1u64);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the amount of data tested, making it impossible to compare tests before/after this change.

In general I'm not sure why the dev dependencies can't depend on primitive-types (or any other code, be it no_std or not). I think the benchmark use the big uint types because it is such a common use case for the crate. What's the point of having the benchmarks be no_std?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the same data length as before here.

}
let data= stream.out();
b.iter(|| {
let rlp = Rlp::new(&data);
for i in 0..1000 {
let _: U256 = rlp.val_at(i).unwrap();
let _: u64 = rlp.val_at(i).unwrap();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the same data length as before here.

}
});
}
4 changes: 3 additions & 1 deletion rlp/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::fmt;
use core::fmt;
#[cfg(feature = "std")]
use std::error::Error as StdError;

#[derive(Debug, PartialEq, Eq, Clone)]
Expand Down Expand Up @@ -36,6 +37,7 @@ pub enum DecoderError {
Custom(&'static str),
}

#[cfg(feature = "std")]
impl StdError for DecoderError {
fn description(&self) -> &str {
"builder error"
Expand Down
17 changes: 12 additions & 5 deletions rlp/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::{mem, str};
use std::iter::{once, empty};
#[cfg(not(feature = "std"))]
use alloc::{borrow::ToOwned, vec::Vec, string::String};
use core::{mem, str};
use core::iter::{once, empty};

use byteorder::{ByteOrder, BigEndian};
use crate::traits::{Encodable, Decodable};
use crate::stream::RlpStream;
use crate::{Rlp, DecoderError};

use crate::{
error::DecoderError,
rlpin::Rlp,
stream::RlpStream,
traits::{Encodable, Decodable},
};

pub fn decode_usize(bytes: &[u8]) -> Result<usize, DecoderError> {
match bytes.len() {
Expand Down
17 changes: 12 additions & 5 deletions rlp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,25 @@
//! * You want to get view onto rlp-slice.
//! * You don't want to decode whole rlp at once.

use std::borrow::Borrow;
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
extern crate alloc;

mod traits;
mod error;
mod rlpin;
mod stream;
mod impls;

pub use crate::error::DecoderError;
pub use crate::traits::{Decodable, Encodable};
pub use crate::rlpin::{Rlp, RlpIterator, PayloadInfo, Prototype};
pub use crate::stream::RlpStream;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::borrow::Borrow;

pub use self::error::DecoderError;
pub use self::rlpin::{Rlp, RlpIterator, PayloadInfo, Prototype};
pub use self::stream::RlpStream;
pub use self::traits::{Decodable, Encodable};

/// The RLP encoded empty data (used to mean "null value").
pub const NULL_RLP: [u8; 1] = [0x80; 1];
Expand Down
36 changes: 11 additions & 25 deletions rlp/src/rlpin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::cell::Cell;
use std::fmt;
#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};
use core::cell::Cell;
use core::fmt;

use rustc_hex::ToHex;
use crate::impls::decode_usize;
use crate::{Decodable, DecoderError};

use crate::{
error::DecoderError,
impls::decode_usize,
traits::Decodable,
};

/// rlp offset
#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -384,24 +391,3 @@ impl<'a> BasicDecoder<'a> {
}
}
}

#[cfg(test)]
mod tests {
use crate::{Rlp, DecoderError};
Comment thread
dvdplm marked this conversation as resolved.
use hex_literal::hex;

#[test]
fn test_rlp_display() {
let data = hex!("f84d0589010efbef67941f79b2a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470");
let rlp = Rlp::new(&data);
assert_eq!(format!("{}", rlp), "[\"0x05\", \"0x010efbef67941f79b2\", \"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421\", \"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470\"]");
}

#[test]
fn length_overflow() {
let bs = [0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe5];
let rlp = Rlp::new(&bs);
let res: Result<u8, DecoderError> = rlp.as_val();
assert_eq!(Err(DecoderError::RlpInvalidLength), res);
}
}
6 changes: 5 additions & 1 deletion rlp/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::borrow::Borrow;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::borrow::Borrow;

use byteorder::{ByteOrder, BigEndian};

use crate::traits::Encodable;

#[derive(Debug, Copy, Clone)]
Expand Down
9 changes: 8 additions & 1 deletion rlp/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
// except according to those terms.

//! Common RLP traits
use crate::{DecoderError, Rlp, RlpStream};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use crate::{
error::DecoderError,
rlpin::Rlp,
stream::RlpStream,
};

/// RLP decodable trait
pub trait Decodable: Sized {
Expand Down
82 changes: 26 additions & 56 deletions rlp/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,32 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use primitive_types::{H160, U256};
use std::{fmt, cmp};
#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(not(feature = "std"))]
use alloc::{format, string::String, vec::Vec};
use core::{fmt, cmp};

use rlp::{Encodable, Decodable, Rlp, RlpStream, DecoderError};
use hex_literal::hex;

fn hex(s: &str) -> Vec<u8> {
rustc_hex::FromHex::from_hex(s).unwrap()
}

#[test]
fn test_rlp_display() {
let data = hex("f84d0589010efbef67941f79b2a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470");
let rlp = Rlp::new(&data);
assert_eq!(format!("{}", rlp), "[\"0x05\", \"0x010efbef67941f79b2\", \"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421\", \"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470\"]");
}

#[test]
fn length_overflow() {
let bs = [0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe5];
let rlp = Rlp::new(&bs);
let res: Result<u8, DecoderError> = rlp.as_val();
assert_eq!(Err(DecoderError::RlpInvalidLength), res);
}

#[test]
fn rlp_at() {
Expand Down Expand Up @@ -128,21 +150,6 @@ fn encode_u64() {
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(("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,
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']),
Expand All @@ -160,17 +167,6 @@ fn encode_str() {
run_encode_tests(tests);
}

#[test]
fn encode_address() {
let tests = vec![
ETestPair(H160::from(hex!("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() {
Expand Down Expand Up @@ -270,21 +266,6 @@ fn decode_untrusted_u64() {
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(("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,
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']),
Expand All @@ -304,17 +285,6 @@ fn decode_untrusted_str() {
run_decode_tests(tests);
}

#[test]
fn decode_untrusted_address() {
let tests = vec![
DTestPair(H160::from(hex!("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![
Expand Down Expand Up @@ -480,7 +450,7 @@ fn test_inner_length_capping_for_short_lists() {
// https://github.com/paritytech/parity-ethereum/pull/9663
#[test]
fn test_list_at() {
let raw = hex!("f83e82022bd79020010db83c4d001500000000abcdef12820cfa8215a8d79020010db885a308d313198a2e037073488208ae82823a8443b9a355c5010203040531b9019afde696e582a78fa8d95ea13ce3297d4afb8ba6433e4154caa5ac6431af1b80ba76023fa4090c408f6b4bc3701562c031041d4702971d102c9ab7fa5eed4cd6bab8f7af956f7d565ee1917084a95398b6a21eac920fe3dd1345ec0a7ef39367ee69ddf092cbfe5b93e5e568ebc491983c09c76d922dc3");
let raw = hex("f83e82022bd79020010db83c4d001500000000abcdef12820cfa8215a8d79020010db885a308d313198a2e037073488208ae82823a8443b9a355c5010203040531b9019afde696e582a78fa8d95ea13ce3297d4afb8ba6433e4154caa5ac6431af1b80ba76023fa4090c408f6b4bc3701562c031041d4702971d102c9ab7fa5eed4cd6bab8f7af956f7d565ee1917084a95398b6a21eac920fe3dd1345ec0a7ef39367ee69ddf092cbfe5b93e5e568ebc491983c09c76d922dc3");

let rlp = Rlp::new(&raw);
let _rlp1 = rlp.at(1).unwrap();
Expand Down