Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ script:
- cargo check --all --tests
- cargo build --all
- cargo test --all --exclude uint --exclude fixed-hash
- if [ "$TRAVIS_RUST_VERSION" == "nightly" ]; then cd parity-bytes/ && cargo build --no-default-features && cd ..;
- if [ "$TRAVIS_RUST_VERSION" == "nightly" ]; then
cd parity-bytes/ && cargo build --no-default-features && 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
15 changes: 9 additions & 6 deletions rlp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ description = "Recursive-length prefix encoding, decoding, and compression"
repository = "https://github.com/paritytech/parity-common"
license = "MIT/Apache-2.0"
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.1"

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.

Why prefer rustc-hex to hex-literal?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think rustc-hex is enough to encode/decode hex. There is no need to introduce more dependencies, even if it is just dev-dependencies.

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.

Tests need to be as readable as possible, which is why I thinkhex-literal is better.

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.

(The new 0.2 of hex-literal is edition compatible too!)

primitive-types = { path = "../primitive-types", version = "0.3", 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",
]
30 changes: 2 additions & 28 deletions rlp/benches/rlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@

#![feature(test)]

extern crate ethereum_types;
extern crate rlp;
extern crate test;

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

Expand All @@ -42,29 +39,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 @@ -106,13 +80,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
15 changes: 10 additions & 5 deletions rlp/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
// 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 traits::{Encodable, Decodable};
use stream::RlpStream;
use {Rlp, DecoderError};

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

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

extern crate byteorder;
extern crate rustc_hex;
#[cfg(test)]
#[macro_use]
extern crate hex_literal;
#![cfg_attr(not(feature = "std"), no_std)]

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

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

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

pub use error::DecoderError;
pub use traits::{Decodable, Encodable};
pub use rlpin::{Rlp, RlpIterator, PayloadInfo, Prototype};
pub use stream::RlpStream;
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
19 changes: 13 additions & 6 deletions rlp/src/rlpin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
// 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::{vec::Vec, string::String};
use core::cell::Cell;
use core::fmt;

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

use super::error::DecoderError;
use super::impls::decode_usize;
use super::traits::Decodable;

/// rlp offset
#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -387,11 +392,13 @@ impl<'a> BasicDecoder<'a> {

#[cfg(test)]
mod tests {
use {Rlp, DecoderError};
#[cfg(not(feature = "std"))]
use alloc::format;
use super::*;

#[test]
fn test_rlp_display() {
let data = hex!("f84d0589010efbef67941f79b2a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470");
let data: Vec<u8> = rustc_hex::FromHex::from_hex("f84d0589010efbef67941f79b2a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").unwrap();
Comment thread
dvdplm marked this conversation as resolved.
Outdated
let rlp = Rlp::new(&data);
assert_eq!(format!("{}", rlp), "[\"0x05\", \"0x010efbef67941f79b2\", \"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421\", \"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470\"]");
}
Expand Down
8 changes: 6 additions & 2 deletions rlp/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
// 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 traits::Encodable;

use super::traits::Encodable;

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

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

use super::error::DecoderError;
use super::rlpin::Rlp;
use super::stream::RlpStream;

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

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

use primitive_types::{H160, U256};
use std::{fmt, cmp};
use rlp::{Encodable, Decodable, Rlp, RlpStream, DecoderError};
use rustc_hex::FromHex;

#[test]
fn rlp_at() {
Expand Down Expand Up @@ -132,21 +132,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 @@ -164,17 +149,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 @@ -274,21 +248,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 @@ -308,17 +267,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 @@ -484,7 +432,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: Vec<u8> = FromHex::from_hex("f83e82022bd79020010db83c4d001500000000abcdef12820cfa8215a8d79020010db885a308d313198a2e037073488208ae82823a8443b9a355c5010203040531b9019afde696e582a78fa8d95ea13ce3297d4afb8ba6433e4154caa5ac6431af1b80ba76023fa4090c408f6b4bc3701562c031041d4702971d102c9ab7fa5eed4cd6bab8f7af956f7d565ee1917084a95398b6a21eac920fe3dd1345ec0a7ef39367ee69ddf092cbfe5b93e5e568ebc491983c09c76d922dc3").unwrap();

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