From f78004a8cc807282bb97b1c53257b1c561d13a67 Mon Sep 17 00:00:00 2001 From: Herobird Date: Mon, 5 Nov 2018 17:03:12 +0100 Subject: [PATCH] Update ethbloom and ethereum-types to new `fixed-hash` version `0.3.0-beta` --- ethbloom/Cargo.toml | 7 ++-- ethbloom/benches/bloom.rs | 20 +++++++++++- ethbloom/src/lib.rs | 47 ++++++++++++++++++++++++--- ethereum-types/Cargo.toml | 6 ++-- ethereum-types/src/hash.rs | 66 ++++++++++++++++++++------------------ ethereum-types/src/lib.rs | 9 +++++- no-std-tests/Cargo.toml | 2 +- no-std-tests/src/main.rs | 2 -- 8 files changed, 112 insertions(+), 47 deletions(-) diff --git a/ethbloom/Cargo.toml b/ethbloom/Cargo.toml index a3a421d..c3be170 100644 --- a/ethbloom/Cargo.toml +++ b/ethbloom/Cargo.toml @@ -11,7 +11,8 @@ repository = "https://github.com/paritytech/primitives" [dependencies] tiny-keccak = "1.4" crunchy = { version = "0.1.6", features = ["limit_256"] } -fixed-hash = { version = "0.2", default_features = false } +rustc-hex = { version = "2.0" } +fixed-hash = { version = "0.3.0-beta.2", default_features = false } ethereum-types-serialize = { version = "0.2.1", path = "../serialize", optional = true } serde = { version = "1.0", optional = true } @@ -20,7 +21,7 @@ rand = { version = "0.4" } hex-literal = "0.1.1" [features] -default = ["std", "heapsizeof", "serialize", "fixed-hash/libc"] +default = ["std", "heapsizeof", "serialize", "fixed-hash/libc", "fixed-hash/rustc-hex"] std = ["fixed-hash/std"] -heapsizeof = ["fixed-hash/heapsizeof"] +heapsizeof = ["fixed-hash/heapsize"] serialize = ["std", "ethereum-types-serialize", "serde"] diff --git a/ethbloom/benches/bloom.rs b/ethbloom/benches/bloom.rs index 674eeab..7c998e9 100644 --- a/ethbloom/benches/bloom.rs +++ b/ethbloom/benches/bloom.rs @@ -11,7 +11,25 @@ use tiny_keccak::keccak256; use ethbloom::{Bloom, Input}; fn test_bloom() -> Bloom { - "00000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002020000000000000000000000000000000000000000000008000000001000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000".into() + use std::str::FromStr; + Bloom::from_str( + "00000000000000000000000000000000\ + 00000000100000000000000000000000\ + 00000000000000000000000000000000\ + 00000000000000000000000000000000\ + 00000000000000000000000000000000\ + 00000000000000000000000000000000\ + 00000002020000000000000000000000\ + 00000000000000000000000800000000\ + 10000000000000000000000000000000\ + 00000000000000000000001000000000\ + 00000000000000000000000000000000\ + 00000000000000000000000000000000\ + 00000000000000000000000000000000\ + 00000000000000000000000000000000\ + 00000000000000000000000000000000\ + 00000000000000000000000000000000" + ).unwrap() } fn test_topic() -> Vec { diff --git a/ethbloom/src/lib.rs b/ethbloom/src/lib.rs index 462551c..cfd5ab6 100644 --- a/ethbloom/src/lib.rs +++ b/ethbloom/src/lib.rs @@ -5,7 +5,25 @@ //! use ethbloom::{Bloom, Input}; //! //! fn main() { -//! let bloom: Bloom = "00000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002020000000000000000000000000000000000000000000008000000001000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000".into(); +//! use std::str::FromStr; +//! let bloom = Bloom::from_str( +//! "00000000000000000000000000000000\ +//! 00000000100000000000000000000000\ +//! 00000000000000000000000000000000\ +//! 00000000000000000000000000000000\ +//! 00000000000000000000000000000000\ +//! 00000000000000000000000000000000\ +//! 00000002020000000000000000000000\ +//! 00000000000000000000000800000000\ +//! 10000000000000000000000000000000\ +//! 00000000000000000000001000000000\ +//! 00000000000000000000000000000000\ +//! 00000000000000000000000000000000\ +//! 00000000000000000000000000000000\ +//! 00000000000000000000000000000000\ +//! 00000000000000000000000000000000\ +//! 00000000000000000000000000000000" +//! ).unwrap(); //! let address = hex!("ef2d6d194084c2de36e0dabfce45d046b37d1106"); //! let topic = hex!("02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc"); //! @@ -60,7 +78,10 @@ use core::str; const BLOOM_BITS: u32 = 3; const BLOOM_SIZE: usize = 256; -construct_hash!(Bloom, BLOOM_SIZE); +construct_fixed_hash!{ + /// Bloom hash type with 256 bytes (2048 bits) size. + pub struct Bloom(BLOOM_SIZE); +} /// Returns log2. fn log2(x: usize) -> u32 { @@ -257,11 +278,29 @@ impl<'de> Deserialize<'de> for Bloom { #[cfg(test)] mod tests { - use {Bloom, Input}; + use super::{Bloom, Input}; #[test] fn it_works() { - let bloom: Bloom = "00000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002020000000000000000000000000000000000000000000008000000001000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000".into(); + use std::str::FromStr; + let bloom = Bloom::from_str( + "00000000000000000000000000000000\ + 00000000100000000000000000000000\ + 00000000000000000000000000000000\ + 00000000000000000000000000000000\ + 00000000000000000000000000000000\ + 00000000000000000000000000000000\ + 00000002020000000000000000000000\ + 00000000000000000000000800000000\ + 10000000000000000000000000000000\ + 00000000000000000000001000000000\ + 00000000000000000000000000000000\ + 00000000000000000000000000000000\ + 00000000000000000000000000000000\ + 00000000000000000000000000000000\ + 00000000000000000000000000000000\ + 00000000000000000000000000000000" + ).unwrap(); let address = hex!("ef2d6d194084c2de36e0dabfce45d046b37d1106"); let topic = hex!("02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc"); diff --git a/ethereum-types/Cargo.toml b/ethereum-types/Cargo.toml index ccf2b7a..d713bd9 100644 --- a/ethereum-types/Cargo.toml +++ b/ethereum-types/Cargo.toml @@ -10,7 +10,7 @@ description = "Ethereum types" crunchy = "0.1" ethbloom = { path = "../ethbloom", version = "0.5.0", default-features = false } ethereum-types-serialize = { version = "0.2.1", path = "../serialize", optional = true } -fixed-hash = { version = "0.2", default_features = false } +fixed-hash = { version = "0.3.0-beta.2", default_features = false } serde = { version = "1.0", optional = true } uint = { version = "0.4", default_features = false } @@ -18,7 +18,7 @@ uint = { version = "0.4", default_features = false } serde_json = "1.0" [features] -default = ["std", "heapsizeof", "serialize"] +default = ["std", "heapsizeof", "serialize", "fixed-hash/byteorder", "fixed-hash/rustc-hex"] std = ["uint/std", "fixed-hash/std", "ethbloom/std"] -heapsizeof = ["uint/heapsizeof", "fixed-hash/heapsizeof", "ethbloom/heapsizeof"] +heapsizeof = ["uint/heapsizeof", "fixed-hash/heapsize", "ethbloom/heapsizeof"] serialize = ["std", "ethereum-types-serialize", "serde", "ethbloom/serialize"] diff --git a/ethereum-types/src/hash.rs b/ethereum-types/src/hash.rs index 64e436d..4e801eb 100644 --- a/ethereum-types/src/hash.rs +++ b/ethereum-types/src/hash.rs @@ -31,16 +31,16 @@ macro_rules! impl_uint_conversions { ($hash: ident, $uint: ident) => { impl From<$uint> for $hash { fn from(value: $uint) -> Self { - let mut ret = $hash::new(); - value.to_big_endian(&mut ret); + let mut ret = $hash::zero(); + value.to_big_endian(ret.as_bytes_mut()); ret } } impl<'a> From<&'a $uint> for $hash { fn from(value: &'a $uint) -> Self { - let mut ret = $hash::new(); - value.to_big_endian(&mut ret); + let mut ret = $hash::zero(); + value.to_big_endian(ret.as_bytes_mut()); ret } } @@ -68,14 +68,14 @@ impl_serde!(H264, 33); impl_serde!(H512, 64); impl_serde!(H520, 65); -construct_hash!(H32, 4); -construct_hash!(H64, 8); -construct_hash!(H128, 16); -construct_hash!(H160, 20); -construct_hash!(H256, 32); -construct_hash!(H264, 33); -construct_hash!(H512, 64); -construct_hash!(H520, 65); +construct_fixed_hash!{ pub struct H32(4); } +construct_fixed_hash!{ pub struct H64(8); } +construct_fixed_hash!{ pub struct H128(16); } +construct_fixed_hash!{ pub struct H160(20); } +construct_fixed_hash!{ pub struct H256(32); } +construct_fixed_hash!{ pub struct H264(33); } +construct_fixed_hash!{ pub struct H512(64); } +construct_fixed_hash!{ pub struct H520(65); } impl_uint_conversions!(H64, U64); impl_uint_conversions!(H128, U128); @@ -85,7 +85,7 @@ impl_uint_conversions!(H512, U512); #[deprecated] impl From for H160 { fn from(value: H256) -> H160 { - let mut ret = H160::new(); + let mut ret = H160::zero(); ret.0.copy_from_slice(&value[12..32]); ret } @@ -94,7 +94,7 @@ impl From for H160 { #[deprecated] impl From for H64 { fn from(value: H256) -> H64 { - let mut ret = H64::new(); + let mut ret = H64::zero(); ret.0.copy_from_slice(&value[20..28]); ret } @@ -102,16 +102,16 @@ impl From for H64 { impl From for H256 { fn from(value: H160) -> H256 { - let mut ret = H256::new(); - ret.0[12..32].copy_from_slice(&value); + let mut ret = H256::zero(); + ret.0[12..32].copy_from_slice(value.as_bytes()); ret } } impl<'a> From<&'a H160> for H256 { fn from(value: &'a H160) -> H256 { - let mut ret = H256::new(); - ret.0[12..32].copy_from_slice(value); + let mut ret = H256::zero(); + ret.0[12..32].copy_from_slice(value.as_bytes()); ret } } @@ -121,16 +121,17 @@ mod tests { use super::{H160, H256}; use serde_json as ser; + // #[cfg(feature = "fixed-hash/byteorder-support")] #[test] fn test_serialize_h160() { let tests = vec![ - (H160::from(0), "0x0000000000000000000000000000000000000000"), - (H160::from(2), "0x0000000000000000000000000000000000000002"), - (H160::from(15), "0x000000000000000000000000000000000000000f"), - (H160::from(16), "0x0000000000000000000000000000000000000010"), - (H160::from(1_000), "0x00000000000000000000000000000000000003e8"), - (H160::from(100_000), "0x00000000000000000000000000000000000186a0"), - (H160::from(u64::max_value()), "0x000000000000000000000000ffffffffffffffff"), + (H160::from_low_u64_be(0), "0x0000000000000000000000000000000000000000"), + (H160::from_low_u64_be(2), "0x0000000000000000000000000000000000000002"), + (H160::from_low_u64_be(15), "0x000000000000000000000000000000000000000f"), + (H160::from_low_u64_be(16), "0x0000000000000000000000000000000000000010"), + (H160::from_low_u64_be(1_000), "0x00000000000000000000000000000000000003e8"), + (H160::from_low_u64_be(100_000), "0x00000000000000000000000000000000000186a0"), + (H160::from_low_u64_be(u64::max_value()), "0x000000000000000000000000ffffffffffffffff"), ]; for (number, expected) in tests { @@ -139,16 +140,17 @@ mod tests { } } + #[cfg(feature = "byteorder-support")] #[test] fn test_serialize_h256() { let tests = vec![ - (H256::from(0), "0x0000000000000000000000000000000000000000000000000000000000000000"), - (H256::from(2), "0x0000000000000000000000000000000000000000000000000000000000000002"), - (H256::from(15), "0x000000000000000000000000000000000000000000000000000000000000000f"), - (H256::from(16), "0x0000000000000000000000000000000000000000000000000000000000000010"), - (H256::from(1_000), "0x00000000000000000000000000000000000000000000000000000000000003e8"), - (H256::from(100_000), "0x00000000000000000000000000000000000000000000000000000000000186a0"), - (H256::from(u64::max_value()), "0x000000000000000000000000000000000000000000000000ffffffffffffffff"), + (H256::from_low_u64_be(0), "0x0000000000000000000000000000000000000000000000000000000000000000"), + (H256::from_low_u64_be(2), "0x0000000000000000000000000000000000000000000000000000000000000002"), + (H256::from_low_u64_be(15), "0x000000000000000000000000000000000000000000000000000000000000000f"), + (H256::from_low_u64_be(16), "0x0000000000000000000000000000000000000000000000000000000000000010"), + (H256::from_low_u64_be(1_000), "0x00000000000000000000000000000000000000000000000000000000000003e8"), + (H256::from_low_u64_be(100_000), "0x00000000000000000000000000000000000000000000000000000000000186a0"), + (H256::from_low_u64_be(u64::max_value()), "0x000000000000000000000000000000000000000000000000ffffffffffffffff"), ]; for (number, expected) in tests { diff --git a/ethereum-types/src/lib.rs b/ethereum-types/src/lib.rs index 04be076..98552bb 100644 --- a/ethereum-types/src/lib.rs +++ b/ethereum-types/src/lib.rs @@ -24,7 +24,14 @@ mod uint; pub use uint::{U64, U128, U256, U512}; pub use hash::{H32, H64, H128, H160, H256, H264, H512, H520}; pub use ethbloom::{Bloom, BloomRef, Input as BloomInput}; -pub use fixed_hash::clean_0x; + +/// Cuts away `"0x"` prefix on the given `input` if existing and returns the result. +pub fn clean_0x(input: &str) -> &str { + if input.starts_with("0x") { + return &input[2..] + } + input +} pub type Address = H160; pub type Secret = H256; diff --git a/no-std-tests/Cargo.toml b/no-std-tests/Cargo.toml index 8cdd482..cbe6765 100644 --- a/no-std-tests/Cargo.toml +++ b/no-std-tests/Cargo.toml @@ -9,4 +9,4 @@ license = "MIT" libc = { version = "0.2", default-features = false } ethereum-types = { path = "../ethereum-types", default-features = false } ethbloom = { path = "../ethbloom", default-features = false } -fixed-hash = { version = "0.2", default-features = false } \ No newline at end of file +fixed-hash = { version = "0.3.0-beta.2", default-features = false } diff --git a/no-std-tests/src/main.rs b/no-std-tests/src/main.rs index 0fa4c3a..a5ec78d 100644 --- a/no-std-tests/src/main.rs +++ b/no-std-tests/src/main.rs @@ -6,8 +6,6 @@ extern crate ethereum_types; extern crate ethbloom; extern crate fixed_hash; -use ethereum_types::{Address, Public, Secret, Signature}; - #[start] fn start(_argc: isize, _argv: *const *const u8) -> isize { 0