From ecea8846b7b9c23c7bcbac84b5927b27e7029031 Mon Sep 17 00:00:00 2001 From: koushiro Date: Mon, 27 May 2019 14:22:14 +0800 Subject: [PATCH 1/5] Support no_std for rlp; Migrate rlp crate to 2018 edition Signed-off-by: koushiro --- .travis.yml | 1 + rlp/Cargo.toml | 15 ++++---- rlp/benches/rlp.rs | 30 ++-------------- rlp/src/error.rs | 4 ++- rlp/src/impls.rs | 15 +++++--- rlp/src/lib.rs | 21 +++++------ rlp/src/rlpin.rs | 19 ++++++---- rlp/src/stream.rs | 8 +++-- rlp/src/traits.rs | 7 +++- rlp/tests/tests.rs | 88 ++++++++++++++++------------------------------ 10 files changed, 92 insertions(+), 116 deletions(-) diff --git a/.travis.yml b/.travis.yml index c41f4b36d..b270d383a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,3 +24,4 @@ script: - cd uint/ && cargo test --features=std,quickcheck --release && cd .. - cd plain_hasher/ && cargo test --no-default-features && cd .. - cd parity-util-mem/ && cargo test --features=estimate-heapsize && cd .. + - cd rlp/ && cargo test --no-default-features && cd .. diff --git a/rlp/Cargo.toml b/rlp/Cargo.toml index f7bb9e56c..a1bb8cada 100644 --- a/rlp/Cargo.toml +++ b/rlp/Cargo.toml @@ -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 "] +edition = "2018" [dependencies] -byteorder = "1.0" -rustc-hex = {version = "2.0", default-features = false } - -[dev-dependencies] -hex-literal = "0.1" -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", +] diff --git a/rlp/benches/rlp.rs b/rlp/benches/rlp.rs index 1dda88d8a..7482d2c32 100644 --- a/rlp/benches/rlp.rs +++ b/rlp/benches/rlp.rs @@ -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; @@ -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(|| { @@ -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); } 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(); } }); } diff --git a/rlp/src/error.rs b/rlp/src/error.rs index 7aef6cfbf..d810130b0 100644 --- a/rlp/src/error.rs +++ b/rlp/src/error.rs @@ -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)] @@ -36,6 +37,7 @@ pub enum DecoderError { Custom(&'static str), } +#[cfg(feature = "std")] impl StdError for DecoderError { fn description(&self) -> &str { "builder error" diff --git a/rlp/src/impls.rs b/rlp/src/impls.rs index 6d385b3c2..d89ae2a86 100644 --- a/rlp/src/impls.rs +++ b/rlp/src/impls.rs @@ -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 { match bytes.len() { diff --git a/rlp/src/lib.rs b/rlp/src/lib.rs index 235a546b0..1baa52c2f 100644 --- a/rlp/src/lib.rs +++ b/rlp/src/lib.rs @@ -32,11 +32,10 @@ //! * 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; @@ -44,12 +43,14 @@ 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]; diff --git a/rlp/src/rlpin.rs b/rlp/src/rlpin.rs index 678af0ac8..e52c0898c 100644 --- a/rlp/src/rlpin.rs +++ b/rlp/src/rlpin.rs @@ -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)] @@ -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 = rustc_hex::FromHex::from_hex("f84d0589010efbef67941f79b2a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").unwrap(); let rlp = Rlp::new(&data); assert_eq!(format!("{}", rlp), "[\"0x05\", \"0x010efbef67941f79b2\", \"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421\", \"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470\"]"); } diff --git a/rlp/src/stream.rs b/rlp/src/stream.rs index b13fd3b64..01a66d369 100644 --- a/rlp/src/stream.rs +++ b/rlp/src/stream.rs @@ -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 { diff --git a/rlp/src/traits.rs b/rlp/src/traits.rs index 7490f801c..95d1cdab9 100644 --- a/rlp/src/traits.rs +++ b/rlp/src/traits.rs @@ -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 { diff --git a/rlp/tests/tests.rs b/rlp/tests/tests.rs index 6f534a325..57fa41cc7 100644 --- a/rlp/tests/tests.rs +++ b/rlp/tests/tests.rs @@ -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() { @@ -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']), @@ -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 (Bytes) is treated as a single value #[test] fn encode_vector_u8() { @@ -274,20 +248,20 @@ 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_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() { @@ -308,16 +282,16 @@ 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_address() { +// let tests = vec![ +// DTestPair(H160::from(FromHex::from_hex("ef2d6d194084c2de36e0dabfce45d046b37d1106").unwrap()), +// 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() { @@ -484,7 +458,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 = FromHex::from_hex("f83e82022bd79020010db83c4d001500000000abcdef12820cfa8215a8d79020010db885a308d313198a2e037073488208ae82823a8443b9a355c5010203040531b9019afde696e582a78fa8d95ea13ce3297d4afb8ba6433e4154caa5ac6431af1b80ba76023fa4090c408f6b4bc3701562c031041d4702971d102c9ab7fa5eed4cd6bab8f7af956f7d565ee1917084a95398b6a21eac920fe3dd1345ec0a7ef39367ee69ddf092cbfe5b93e5e568ebc491983c09c76d922dc3").unwrap(); let rlp = Rlp::new(&raw); let _rlp1 = rlp.at(1).unwrap(); From bfca031d6a0c50b0b5505aaff2b637e867da1b97 Mon Sep 17 00:00:00 2001 From: koushiro Date: Mon, 27 May 2019 14:24:26 +0800 Subject: [PATCH 2/5] Modify CI config to support the no_std test of rlp Signed-off-by: koushiro --- appveyor.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/appveyor.yml b/appveyor.yml index d930e318c..ed460554a 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -27,3 +27,4 @@ test_script: - cd uint/ && cargo test --features=std,quickcheck --release && cd .. - cd plain_hasher/ && cargo test --no-default-features && cd .. - cd parity-util-mem/ && cargo test --features=estimate-heapsize && cd .. + - cd rlp/ && cargo test --no-default-features && cd .. From 17a5a219f3a44f0921e80b89c35e3f9ea622a6eb Mon Sep 17 00:00:00 2001 From: koushiro Date: Mon, 27 May 2019 14:26:52 +0800 Subject: [PATCH 3/5] Remove useless test Signed-off-by: koushiro --- rlp/tests/tests.rs | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/rlp/tests/tests.rs b/rlp/tests/tests.rs index 57fa41cc7..e3178c1f0 100644 --- a/rlp/tests/tests.rs +++ b/rlp/tests/tests.rs @@ -248,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']), @@ -282,17 +267,6 @@ fn decode_untrusted_str() { run_decode_tests(tests); } -//#[test] -//fn decode_untrusted_address() { -// let tests = vec![ -// DTestPair(H160::from(FromHex::from_hex("ef2d6d194084c2de36e0dabfce45d046b37d1106").unwrap()), -// 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![ From bb7651569e139cd069671c3a39bd99bca30777ca Mon Sep 17 00:00:00 2001 From: koushiro Date: Mon, 27 May 2019 14:58:00 +0800 Subject: [PATCH 4/5] Fix ci Signed-off-by: koushiro --- .travis.yml | 5 +++-- appveyor.yml | 1 - 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index b270d383a..c46414f87 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,10 +18,11 @@ 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 .. - cd plain_hasher/ && cargo test --no-default-features && cd .. - cd parity-util-mem/ && cargo test --features=estimate-heapsize && cd .. - - cd rlp/ && cargo test --no-default-features && cd .. diff --git a/appveyor.yml b/appveyor.yml index ed460554a..d930e318c 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -27,4 +27,3 @@ test_script: - cd uint/ && cargo test --features=std,quickcheck --release && cd .. - cd plain_hasher/ && cargo test --no-default-features && cd .. - cd parity-util-mem/ && cargo test --features=estimate-heapsize && cd .. - - cd rlp/ && cargo test --no-default-features && cd .. From a137df0c1074a2fe84655cca473e54bd9e3ff983 Mon Sep 17 00:00:00 2001 From: koushiro Date: Wed, 29 May 2019 11:09:31 +0800 Subject: [PATCH 5/5] Move tests Signed-off-by: koushiro --- rlp/src/impls.rs | 10 ++++++---- rlp/src/rlpin.rs | 36 ++++++------------------------------ rlp/src/traits.rs | 8 +++++--- rlp/tests/tests.rs | 17 ++++++++++++++++- 4 files changed, 33 insertions(+), 38 deletions(-) diff --git a/rlp/src/impls.rs b/rlp/src/impls.rs index 0b0faf546..d3d5c8ff1 100644 --- a/rlp/src/impls.rs +++ b/rlp/src/impls.rs @@ -13,10 +13,12 @@ use core::iter::{once, empty}; use byteorder::{ByteOrder, BigEndian}; -use crate::error::DecoderError; -use crate::rlpin::Rlp; -use crate::stream::RlpStream; -use crate::traits::{Encodable, Decodable}; +use crate::{ + error::DecoderError, + rlpin::Rlp, + stream::RlpStream, + traits::{Encodable, Decodable}, +}; pub fn decode_usize(bytes: &[u8]) -> Result { match bytes.len() { diff --git a/rlp/src/rlpin.rs b/rlp/src/rlpin.rs index c00b3ce82..e45c0c21a 100644 --- a/rlp/src/rlpin.rs +++ b/rlp/src/rlpin.rs @@ -7,15 +7,17 @@ // except according to those terms. #[cfg(not(feature = "std"))] -use alloc::{vec::Vec, string::String}; +use alloc::{string::String, vec::Vec}; use core::cell::Cell; use core::fmt; use rustc_hex::ToHex; -use crate::error::DecoderError; -use crate::impls::decode_usize; -use crate::traits::Decodable; +use crate::{ + error::DecoderError, + impls::decode_usize, + traits::Decodable, +}; /// rlp offset #[derive(Copy, Clone, Debug)] @@ -389,29 +391,3 @@ impl<'a> BasicDecoder<'a> { } } } - -#[cfg(test)] -mod tests { - #[cfg(not(feature = "std"))] - use alloc::{format, vec::Vec}; - use super::{Rlp, DecoderError}; - - fn hex(s: &str) -> Vec { - 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 = rlp.as_val(); - assert_eq!(Err(DecoderError::RlpInvalidLength), res); - } -} diff --git a/rlp/src/traits.rs b/rlp/src/traits.rs index 13531a1b6..b3187fc8b 100644 --- a/rlp/src/traits.rs +++ b/rlp/src/traits.rs @@ -10,9 +10,11 @@ #[cfg(not(feature = "std"))] use alloc::vec::Vec; -use crate::error::DecoderError; -use crate::rlpin::Rlp; -use crate::stream::RlpStream; +use crate::{ + error::DecoderError, + rlpin::Rlp, + stream::RlpStream, +}; /// RLP decodable trait pub trait Decodable: Sized { diff --git a/rlp/tests/tests.rs b/rlp/tests/tests.rs index 1a2645d6c..089406707 100644 --- a/rlp/tests/tests.rs +++ b/rlp/tests/tests.rs @@ -9,7 +9,7 @@ #[cfg(not(feature = "std"))] extern crate alloc; #[cfg(not(feature = "std"))] -use alloc::{vec::Vec, string::String}; +use alloc::{format, string::String, vec::Vec}; use core::{fmt, cmp}; use rlp::{Encodable, Decodable, Rlp, RlpStream, DecoderError}; @@ -18,6 +18,21 @@ fn hex(s: &str) -> Vec { 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 = rlp.as_val(); + assert_eq!(Err(DecoderError::RlpInvalidLength), res); +} + #[test] fn rlp_at() { let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];