Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use array-bytes for array/bytes/hex operations #695

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
args: -p uint --all-features

- name: Test fixed-hash no_std
run: cargo test -p fixed-hash --no-default-features --features='byteorder,rustc-hex'
run: cargo test -p fixed-hash --no-default-features --features='array-bytes,byteorder'

- name: Test fixed-hash all-features
uses: actions-rs/cargo@v1
Expand Down
21 changes: 15 additions & 6 deletions ethbloom/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,27 @@ impl-codec = { version = "0.6.0", path = "../primitive-types/impls/codec", defau
scale-info = { version = ">=1.0, <3", features = ["derive"], default-features = false, optional = true }

[dev-dependencies]
array-bytes = "6.0"
criterion = "0.4.0"
rand = "0.8.0"
hex-literal = "0.3.1"

[features]
default = ["std", "rlp", "serialize", "rustc-hex"]
std = ["fixed-hash/std", "crunchy/std"]
serialize = ["impl-serde"]
rustc-hex = ["fixed-hash/rustc-hex"]
default = [
"std",
"array-bytes",
"rlp",
"serialize",
]
std = [
"crunchy/std",
"fixed-hash/std",
]

arbitrary = ["fixed-hash/arbitrary"]
rlp = ["impl-rlp"]
array-bytes = ["fixed-hash/array-bytes"]
codec = ["impl-codec", "scale-info"]
rlp = ["impl-rlp"]
serialize = ["impl-serde"]

[[bench]]
name = "bloom"
Expand Down
6 changes: 3 additions & 3 deletions ethbloom/benches/bloom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use array_bytes::hex2bytes_unchecked;
use criterion::{criterion_group, criterion_main, Criterion};
use ethbloom::{Bloom, Input};
use hex_literal::hex;
use tiny_keccak::{Hasher, Keccak};

fn test_bloom() -> Bloom {
Expand Down Expand Up @@ -43,11 +43,11 @@ fn keccak256(input: &[u8]) -> [u8; 32] {
}

fn test_topic() -> Vec<u8> {
hex!("02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc").to_vec()
hex2bytes_unchecked("02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc")
}

fn test_address() -> Vec<u8> {
hex!("ef2d6d194084c2de36e0dabfce45d046b37d1106").to_vec()
hex2bytes_unchecked("ef2d6d194084c2de36e0dabfce45d046b37d1106")
}

fn test_dummy() -> Vec<u8> {
Expand Down
16 changes: 9 additions & 7 deletions ethbloom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// except according to those terms.

//! ```
//! use hex_literal::hex;
//! use array_bytes::hex2bytes_unchecked;
//! use ethbloom::{Bloom, Input};
//!
//! use std::str::FromStr;
Expand All @@ -29,8 +29,8 @@
//! 00000000000000000000000000000000\
//! 00000000000000000000000000000000"
//! ).unwrap();
//! let address = hex!("ef2d6d194084c2de36e0dabfce45d046b37d1106");
//! let topic = hex!("02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc");
//! let address = hex2bytes_unchecked("ef2d6d194084c2de36e0dabfce45d046b37d1106");
//! let topic = hex2bytes_unchecked("02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc");
//!
//! let mut my_bloom = Bloom::default();
//! assert!(!my_bloom.contains_input(Input::Raw(&address)));
Expand Down Expand Up @@ -273,9 +273,11 @@ impl<'a> From<&'a Bloom> for BloomRef<'a> {

#[cfg(test)]
mod tests {
use super::{Bloom, Input};
use core::str::FromStr;
use hex_literal::hex;

use super::{Bloom, Input};

use array_bytes::hex2bytes_unchecked;

#[test]
#[rustfmt::skip]
Expand All @@ -298,8 +300,8 @@ mod tests {
00000000000000000000000000000000\
00000000000000000000000000000000",
).unwrap();
let address = hex!("ef2d6d194084c2de36e0dabfce45d046b37d1106");
let topic = hex!("02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc");
let address = hex2bytes_unchecked("ef2d6d194084c2de36e0dabfce45d046b37d1106");
let topic = hex2bytes_unchecked("02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc");

let mut my_bloom = Bloom::default();
assert!(!my_bloom.contains_input(Input::Raw(&address)));
Expand Down
4 changes: 2 additions & 2 deletions ethereum-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ rust-version = "1.60.0"

[dependencies]
ethbloom = { path = "../ethbloom", version = "0.13", optional = true, default-features = false }
fixed-hash = { path = "../fixed-hash", version = "0.8", default-features = false, features = ["byteorder", "rustc-hex"] }
fixed-hash = { path = "../fixed-hash", version = "0.8", default-features = false, features = ["array-bytes", "byteorder"] }
uint-crate = { path = "../uint", package = "uint", version = "0.9", default-features = false }
primitive-types = { path = "../primitive-types", version = "0.12", features = ["byteorder", "rustc-hex"], default-features = false }
primitive-types = { path = "../primitive-types", version = "0.12", features = ["array-bytes", "byteorder"], default-features = false }
impl-serde = { path = "../primitive-types/impls/serde", version = "0.4.0", default-features = false, optional = true }
impl-rlp = { path = "../primitive-types/impls/rlp", version = "0.3", default-features = false, optional = true }
impl-codec = { version = "0.6.0", path = "../primitive-types/impls/codec", default-features = false, optional = true }
Expand Down
18 changes: 13 additions & 5 deletions fixed-hash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,29 @@ rust-version = "1.56.1"
features = ["quickcheck", "api-dummy"]

[dependencies]
arbitrary = { version = "1.0", optional = true }
array-bytes = { version = "6.0", optional = true }
byteorder = { version = "1.4.2", optional = true, default-features = false }
quickcheck = { version = "1", optional = true }
rand = { version = "0.8.0", optional = true, default-features = false }
rustc-hex = { version = "2.0.1", optional = true, default-features = false }
static_assertions = "1.0.0"
arbitrary = { version = "1.0", optional = true }

[dev-dependencies]
rand_xorshift = "0.3.0"
criterion = "0.4.0"
rand = { version = "0.8.0", default-features = false, features = ["std_rng"] }
rand_xorshift = "0.3.0"

[features]
default = ["std", "rand", "rustc-hex", "byteorder"]
std = ["rustc-hex/std", "rand/std", "byteorder/std"]
default = [
"std",
"array-bytes",
"byteorder",
"rand",
]
std = [
"byteorder/std",
"rand/std",
]

api-dummy = [] # Feature used by docs.rs to display documentation of hash types

Expand Down
3 changes: 1 addition & 2 deletions fixed-hash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ construct_fixed_hash!{

## Features

By default this is an standard library depending crate.
By default this is an standard library depending crate.
For a `#[no_std]` environment use it as follows:

```
Expand All @@ -50,7 +50,6 @@ fixed-hash = { version = "0.3", default-features = false }

- `std`: Use the standard library instead of the core library.
- Using this feature enables the following features
- `rustc-hex/std`
- `rand/std`
- `byteorder/std`
- Enabled by default.
Expand Down
33 changes: 12 additions & 21 deletions fixed-hash/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ macro_rules! construct_fixed_hash {
impl_byteorder_for_fixed_hash!($name);
impl_rand_for_fixed_hash!($name);
impl_cmp_for_fixed_hash!($name);
impl_rustc_hex_for_fixed_hash!($name);
impl_array_bytes_for_fixed_hash!($name);
impl_quickcheck_for_fixed_hash!($name);
impl_arbitrary_for_fixed_hash!($name);
}
Expand Down Expand Up @@ -547,34 +547,34 @@ macro_rules! impl_cmp_for_fixed_hash {
};
}

// Implementation for disabled rustc-hex crate support.
// Implementation for disabled array-bytes crate support.
//
// # Note
//
// Feature guarded macro definitions instead of feature guarded impl blocks
// to work around the problems of introducing `rustc-hex` crate feature in
// to work around the problems of introducing `array-bytes` crate feature in
// a user crate.
#[cfg(not(feature = "rustc-hex"))]
#[cfg(not(feature = "array-bytes"))]
#[macro_export]
#[doc(hidden)]
macro_rules! impl_rustc_hex_for_fixed_hash {
macro_rules! impl_array_bytes_for_fixed_hash {
( $name:ident ) => {};
}

// Implementation for enabled rustc-hex crate support.
// Implementation for enabled array-bytes crate support.
//
// # Note
//
// Feature guarded macro definitions instead of feature guarded impl blocks
// to work around the problems of introducing `rustc-hex` crate feature in
// to work around the problems of introducing `array-bytes` crate feature in
// a user crate.
#[cfg(feature = "rustc-hex")]
#[cfg(feature = "array-bytes")]
#[macro_export]
#[doc(hidden)]
macro_rules! impl_rustc_hex_for_fixed_hash {
macro_rules! impl_array_bytes_for_fixed_hash {
( $name:ident ) => {
impl $crate::core_::str::FromStr for $name {
type Err = $crate::rustc_hex::FromHexError;
type Err = $crate::array_bytes::Error;

/// Creates a hash type instance from the given string.
///
Expand All @@ -586,17 +586,8 @@ macro_rules! impl_rustc_hex_for_fixed_hash {
///
/// - When encountering invalid non hex-digits
/// - Upon empty string input or invalid input length in general
fn from_str(input: &str) -> $crate::core_::result::Result<$name, $crate::rustc_hex::FromHexError> {
let input = input.strip_prefix("0x").unwrap_or(input);
let mut iter = $crate::rustc_hex::FromHexIter::new(input);
let mut result = Self::zero();
for byte in result.as_mut() {
*byte = iter.next().ok_or(Self::Err::InvalidHexLength)??;
}
if iter.next().is_some() {
return Err(Self::Err::InvalidHexLength)
}
Ok(result)
fn from_str(input: &str) -> $crate::core_::result::Result<$name, $crate::array_bytes::Error> {
$crate::array_bytes::hex_n_into::<&str, Self, { Self::len_bytes() }>(input)
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions fixed-hash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ pub use static_assertions::const_assert;
#[doc(hidden)]
pub use byteorder;

#[cfg(feature = "rustc-hex")]
#[cfg(feature = "array-bytes")]
#[doc(hidden)]
pub use rustc_hex;
pub use array_bytes;

#[cfg(feature = "rand")]
#[doc(hidden)]
Expand Down
2 changes: 1 addition & 1 deletion fixed-hash/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ mod rand {
}
}

#[cfg(feature = "rustc-hex")]
#[cfg(feature = "array-bytes")]
mod from_str {
use super::*;

Expand Down
11 changes: 6 additions & 5 deletions primitive-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ scale-info-crate = { package = "scale-info", version = ">=0.9, <3", features = [
[features]
default = ["std"]
std = ["uint/std", "fixed-hash/std", "impl-codec?/std"]

arbitrary = ["fixed-hash/arbitrary", "uint/arbitrary"]
array-bytes = ["fixed-hash/array-bytes"]
byteorder = ["fixed-hash/byteorder"]
rustc-hex = ["fixed-hash/rustc-hex"]
serde = ["std", "impl-serde", "impl-serde/std"]
serde_no_std = ["impl-serde"]
codec = ["impl-codec"]
scale-info = ["codec", "scale-info-crate"]
rlp = ["impl-rlp"]
arbitrary = ["fixed-hash/arbitrary", "uint/arbitrary"]
fp-conversion = ["std"]
num-traits = ["impl-num-traits"]
rlp = ["impl-rlp"]
serde = ["std", "impl-serde", "impl-serde/std"]
serde_no_std = ["impl-serde"]

[[test]]
name = "scale_info"
Expand Down
6 changes: 3 additions & 3 deletions rlp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ edition = "2021"
rust-version = "1.56.1"

[dependencies]
array-bytes = { version = "6.0" }
bytes = { version = "1", default-features = false }
rustc-hex = { version = "2.0.1", default-features = false }
rlp-derive = { version = "0.1", path = "../rlp-derive", optional = true }

[dev-dependencies]
criterion = "0.4.0"
hex-literal = "0.3.1"
primitive-types = { path = "../primitive-types", version = "0.12", features = ["impl-rlp"] }

[features]
default = ["std"]
std = ["bytes/std", "rustc-hex/std"]
std = ["bytes/std"]

derive = ["rlp-derive"]

[[bench]]
Expand Down
4 changes: 1 addition & 3 deletions rlp/src/rlpin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use alloc::{string::String, vec::Vec};
use core::{cell::Cell, fmt};

use rustc_hex::ToHex;

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

/// rlp offset
Expand Down Expand Up @@ -110,7 +108,7 @@ impl<'a> fmt::Display for Rlp<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self.prototype() {
Ok(Prototype::Null) => write!(f, "null"),
Ok(Prototype::Data(_)) => write!(f, "\"0x{}\"", self.data().unwrap().to_hex::<String>()),
Ok(Prototype::Data(_)) => write!(f, "\"{}\"", array_bytes::bytes2hex("0x", self.data().unwrap())),
Ok(Prototype::List(len)) => {
write!(f, "[")?;
for i in 0..len - 1 {
Expand Down
Loading