diff --git a/ethbloom/Cargo.toml b/ethbloom/Cargo.toml index a3a421d..16de3c0 100644 --- a/ethbloom/Cargo.toml +++ b/ethbloom/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ethbloom" -version = "0.5.1" +version = "0.6.0" authors = ["Parity Technologies "] description = "Ethereum bloom filter" license = "MIT" @@ -10,8 +10,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 } +crunchy = { version = "0.2.1", default-features = false, features = ["limit_256"] } +fixed-hash = { version = "0.3", default_features = false } ethereum-types-serialize = { version = "0.2.1", path = "../serialize", optional = true } serde = { version = "1.0", optional = true } @@ -20,7 +20,7 @@ rand = { version = "0.4" } hex-literal = "0.1.1" [features] -default = ["std", "heapsizeof", "serialize", "fixed-hash/libc"] -std = ["fixed-hash/std"] -heapsizeof = ["fixed-hash/heapsizeof"] +default = ["std", "heapsize", "serialize", "fixed-hash/libc", "fixed-hash/rustc-hex"] +std = ["fixed-hash/std", "crunchy/std"] +heapsize = ["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..8f53d7b 100644 --- a/ethereum-types/Cargo.toml +++ b/ethereum-types/Cargo.toml @@ -1,24 +1,24 @@ [package] name = "ethereum-types" -version = "0.4.0" +version = "0.5.0" authors = ["Parity Technologies "] license = "MIT" homepage = "https://github.com/paritytech/primitives" description = "Ethereum types" [dependencies] -crunchy = "0.1" -ethbloom = { path = "../ethbloom", version = "0.5.0", default-features = false } +crunchy = { version = "0.2.1", default-features = false } +ethbloom = { path = "../ethbloom", version = "0.6", 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", default_features = false } serde = { version = "1.0", optional = true } -uint = { version = "0.4", default_features = false } +uint = { version = "0.5", default_features = false } [dev-dependencies] serde_json = "1.0" [features] -default = ["std", "heapsizeof", "serialize"] -std = ["uint/std", "fixed-hash/std", "ethbloom/std"] -heapsizeof = ["uint/heapsizeof", "fixed-hash/heapsizeof", "ethbloom/heapsizeof"] +default = ["std", "heapsize", "serialize", "fixed-hash/byteorder", "fixed-hash/rustc-hex"] +std = ["uint/std", "fixed-hash/std", "ethbloom/std", "crunchy/std"] +heapsize = ["uint/heapsize", "fixed-hash/heapsize", "ethbloom/heapsize"] serialize = ["std", "ethereum-types-serialize", "serde", "ethbloom/serialize"] diff --git a/ethereum-types/src/hash.rs b/ethereum-types/src/hash.rs index 64e436d..26410ae 100644 --- a/ethereum-types/src/hash.rs +++ b/ethereum-types/src/hash.rs @@ -1,14 +1,14 @@ -use {U64, U128, U256, U512}; +use crate::{U64, U128, U256, U512}; -#[cfg(feature="serialize")] +#[cfg(feature = "serialize")] use serde::{Serialize, Serializer, Deserialize, Deserializer}; -#[cfg(feature="serialize")] +#[cfg(feature = "serialize")] use ethereum_types_serialize; macro_rules! impl_serde { ($name: ident, $len: expr) => { - #[cfg(feature="serialize")] + #[cfg(feature = "serialize")] impl Serialize for $name { fn serialize(&self, serializer: S) -> Result where S: Serializer { let mut slice = [0u8; 2 + 2 * $len]; @@ -16,7 +16,7 @@ macro_rules! impl_serde { } } - #[cfg(feature="serialize")] + #[cfg(feature = "serialize")] impl<'de> Deserialize<'de> for $name { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de> { let mut bytes = [0u8; $len]; @@ -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,54 +68,48 @@ 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); impl_uint_conversions!(H256, U256); impl_uint_conversions!(H512, U512); -#[deprecated] -impl From for H160 { - fn from(value: H256) -> H160 { - let mut ret = H160::new(); - ret.0.copy_from_slice(&value[12..32]); - ret - } -} - -#[deprecated] -impl From for H64 { - fn from(value: H256) -> H64 { - let mut ret = H64::new(); - ret.0.copy_from_slice(&value[20..28]); - ret - } -} - 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 } } +impl From for H160 { + fn from(val: u64) -> Self { + H160::from_low_u64_be(val) + } +} + +impl From for H256 { + fn from(val: u64) -> Self { + H256::from_low_u64_be(val) + } +} + #[cfg(test)] mod tests { use super::{H160, H256}; @@ -166,4 +160,4 @@ mod tests { assert!(ser::from_str::("\"0\"").unwrap_err().is_data()); assert!(ser::from_str::("\"10\"").unwrap_err().is_data()); } -} \ No newline at end of file +} diff --git a/ethereum-types/src/lib.rs b/ethereum-types/src/lib.rs index 04be076..bcda15b 100644 --- a/ethereum-types/src/lib.rs +++ b/ethereum-types/src/lib.rs @@ -1,6 +1,6 @@ -#![cfg_attr(not(feature="std"), no_std)] +#![cfg_attr(not(feature = "std"), no_std)] -#[cfg(feature="std")] +#[cfg(feature = "std")] extern crate core; #[macro_use] extern crate crunchy; @@ -10,9 +10,9 @@ extern crate uint as uint_crate; extern crate fixed_hash; extern crate ethbloom; -#[cfg(feature="serialize")] +#[cfg(feature = "serialize")] extern crate ethereum_types_serialize; -#[cfg(feature="serialize")] +#[cfg(feature = "serialize")] extern crate serde; #[cfg(test)] @@ -24,7 +24,6 @@ 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; pub type Address = H160; pub type Secret = H256; diff --git a/ethereum-types/src/uint.rs b/ethereum-types/src/uint.rs index a2de9ff..88aeb29 100644 --- a/ethereum-types/src/uint.rs +++ b/ethereum-types/src/uint.rs @@ -1,12 +1,12 @@ -#[cfg(feature="serialize")] +#[cfg(feature = "serialize")] use serde::{Serialize, Serializer, Deserialize, Deserializer}; -#[cfg(feature="serialize")] +#[cfg(feature = "serialize")] use ethereum_types_serialize; macro_rules! impl_serde { ($name: ident, $len: expr) => { - #[cfg(feature="serialize")] + #[cfg(feature = "serialize")] impl Serialize for $name { fn serialize(&self, serializer: S) -> Result where S: Serializer { let mut slice = [0u8; 2 + 2 * $len * 8]; @@ -16,7 +16,7 @@ macro_rules! impl_serde { } } - #[cfg(feature="serialize")] + #[cfg(feature = "serialize")] impl<'de> Deserialize<'de> for $name { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de> { let mut bytes = [0u8; $len * 8]; @@ -62,7 +62,7 @@ impl From for U256 { fn from(value: U512) -> U256 { let U512(ref arr) = value; if arr[4] | arr[5] | arr[6] | arr[7] != 0 { - panic!("Overflow"); + panic!("From for U256: encountered overflow") } let mut ret = [0; 4]; ret[0] = arr[0]; @@ -89,7 +89,7 @@ impl<'a> From<&'a U512> for U256 { fn from(value: &'a U512) -> U256 { let U512(ref arr) = *value; if arr[4] | arr[5] | arr[6] | arr[7] != 0 { - panic!("Overflow"); + panic!("From<&U512> for U256: encountered overflow") } let mut ret = [0; 4]; ret[0] = arr[0]; @@ -104,7 +104,7 @@ impl From for U128 { fn from(value: U256) -> U128 { let U256(ref arr) = value; if arr[2] | arr[3] != 0 { - panic!("Overflow"); + panic!("From for U128: encountered overflow") } let mut ret = [0; 2]; ret[0] = arr[0]; @@ -117,7 +117,7 @@ impl From for U128 { fn from(value: U512) -> U128 { let U512(ref arr) = value; if arr[2] | arr[3] | arr[4] | arr[5] | arr[6] | arr[7] != 0 { - panic!("Overflow"); + panic!("From for U128: encountered overflow") } let mut ret = [0; 2]; ret[0] = arr[0]; diff --git a/no-std-tests/Cargo.toml b/no-std-tests/Cargo.toml index 8cdd482..2994083 100644 --- a/no-std-tests/Cargo.toml +++ b/no-std-tests/Cargo.toml @@ -9,4 +9,7 @@ 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", default-features = false } + +[features] +default = ["fixed-hash/byteorder"] 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