Update ethbloom and ethereum-types to new `fixed-hash` version `0.3.0-beta`

This commit is contained in:
Herobird 2018-11-05 17:03:12 +01:00
parent 10db5f7e66
commit f78004a8cc
8 changed files with 112 additions and 47 deletions

View File

@ -11,7 +11,8 @@ repository = "https://github.com/paritytech/primitives"
[dependencies] [dependencies]
tiny-keccak = "1.4" tiny-keccak = "1.4"
crunchy = { version = "0.1.6", features = ["limit_256"] } 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 } ethereum-types-serialize = { version = "0.2.1", path = "../serialize", optional = true }
serde = { version = "1.0", optional = true } serde = { version = "1.0", optional = true }
@ -20,7 +21,7 @@ rand = { version = "0.4" }
hex-literal = "0.1.1" hex-literal = "0.1.1"
[features] [features]
default = ["std", "heapsizeof", "serialize", "fixed-hash/libc"] default = ["std", "heapsizeof", "serialize", "fixed-hash/libc", "fixed-hash/rustc-hex"]
std = ["fixed-hash/std"] std = ["fixed-hash/std"]
heapsizeof = ["fixed-hash/heapsizeof"] heapsizeof = ["fixed-hash/heapsize"]
serialize = ["std", "ethereum-types-serialize", "serde"] serialize = ["std", "ethereum-types-serialize", "serde"]

View File

@ -11,7 +11,25 @@ use tiny_keccak::keccak256;
use ethbloom::{Bloom, Input}; use ethbloom::{Bloom, Input};
fn test_bloom() -> Bloom { 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<u8> { fn test_topic() -> Vec<u8> {

View File

@ -5,7 +5,25 @@
//! use ethbloom::{Bloom, Input}; //! use ethbloom::{Bloom, Input};
//! //!
//! fn main() { //! 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 address = hex!("ef2d6d194084c2de36e0dabfce45d046b37d1106");
//! let topic = hex!("02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc"); //! let topic = hex!("02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc");
//! //!
@ -60,7 +78,10 @@ use core::str;
const BLOOM_BITS: u32 = 3; const BLOOM_BITS: u32 = 3;
const BLOOM_SIZE: usize = 256; 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. /// Returns log2.
fn log2(x: usize) -> u32 { fn log2(x: usize) -> u32 {
@ -257,11 +278,29 @@ impl<'de> Deserialize<'de> for Bloom {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use {Bloom, Input}; use super::{Bloom, Input};
#[test] #[test]
fn it_works() { 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 address = hex!("ef2d6d194084c2de36e0dabfce45d046b37d1106");
let topic = hex!("02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc"); let topic = hex!("02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc");

View File

@ -10,7 +10,7 @@ description = "Ethereum types"
crunchy = "0.1" crunchy = "0.1"
ethbloom = { path = "../ethbloom", version = "0.5.0", default-features = false } ethbloom = { path = "../ethbloom", version = "0.5.0", default-features = false }
ethereum-types-serialize = { version = "0.2.1", path = "../serialize", optional = true } 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 } serde = { version = "1.0", optional = true }
uint = { version = "0.4", default_features = false } uint = { version = "0.4", default_features = false }
@ -18,7 +18,7 @@ uint = { version = "0.4", default_features = false }
serde_json = "1.0" serde_json = "1.0"
[features] [features]
default = ["std", "heapsizeof", "serialize"] default = ["std", "heapsizeof", "serialize", "fixed-hash/byteorder", "fixed-hash/rustc-hex"]
std = ["uint/std", "fixed-hash/std", "ethbloom/std"] 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"] serialize = ["std", "ethereum-types-serialize", "serde", "ethbloom/serialize"]

View File

@ -31,16 +31,16 @@ macro_rules! impl_uint_conversions {
($hash: ident, $uint: ident) => { ($hash: ident, $uint: ident) => {
impl From<$uint> for $hash { impl From<$uint> for $hash {
fn from(value: $uint) -> Self { fn from(value: $uint) -> Self {
let mut ret = $hash::new(); let mut ret = $hash::zero();
value.to_big_endian(&mut ret); value.to_big_endian(ret.as_bytes_mut());
ret ret
} }
} }
impl<'a> From<&'a $uint> for $hash { impl<'a> From<&'a $uint> for $hash {
fn from(value: &'a $uint) -> Self { fn from(value: &'a $uint) -> Self {
let mut ret = $hash::new(); let mut ret = $hash::zero();
value.to_big_endian(&mut ret); value.to_big_endian(ret.as_bytes_mut());
ret ret
} }
} }
@ -68,14 +68,14 @@ impl_serde!(H264, 33);
impl_serde!(H512, 64); impl_serde!(H512, 64);
impl_serde!(H520, 65); impl_serde!(H520, 65);
construct_hash!(H32, 4); construct_fixed_hash!{ pub struct H32(4); }
construct_hash!(H64, 8); construct_fixed_hash!{ pub struct H64(8); }
construct_hash!(H128, 16); construct_fixed_hash!{ pub struct H128(16); }
construct_hash!(H160, 20); construct_fixed_hash!{ pub struct H160(20); }
construct_hash!(H256, 32); construct_fixed_hash!{ pub struct H256(32); }
construct_hash!(H264, 33); construct_fixed_hash!{ pub struct H264(33); }
construct_hash!(H512, 64); construct_fixed_hash!{ pub struct H512(64); }
construct_hash!(H520, 65); construct_fixed_hash!{ pub struct H520(65); }
impl_uint_conversions!(H64, U64); impl_uint_conversions!(H64, U64);
impl_uint_conversions!(H128, U128); impl_uint_conversions!(H128, U128);
@ -85,7 +85,7 @@ impl_uint_conversions!(H512, U512);
#[deprecated] #[deprecated]
impl From<H256> for H160 { impl From<H256> for H160 {
fn from(value: H256) -> 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.0.copy_from_slice(&value[12..32]);
ret ret
} }
@ -94,7 +94,7 @@ impl From<H256> for H160 {
#[deprecated] #[deprecated]
impl From<H256> for H64 { impl From<H256> for H64 {
fn from(value: H256) -> 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.0.copy_from_slice(&value[20..28]);
ret ret
} }
@ -102,16 +102,16 @@ impl From<H256> for H64 {
impl From<H160> for H256 { impl From<H160> for H256 {
fn from(value: H160) -> H256 { fn from(value: H160) -> H256 {
let mut ret = H256::new(); let mut ret = H256::zero();
ret.0[12..32].copy_from_slice(&value); ret.0[12..32].copy_from_slice(value.as_bytes());
ret ret
} }
} }
impl<'a> From<&'a H160> for H256 { impl<'a> From<&'a H160> for H256 {
fn from(value: &'a H160) -> H256 { fn from(value: &'a H160) -> H256 {
let mut ret = H256::new(); let mut ret = H256::zero();
ret.0[12..32].copy_from_slice(value); ret.0[12..32].copy_from_slice(value.as_bytes());
ret ret
} }
} }
@ -121,16 +121,17 @@ mod tests {
use super::{H160, H256}; use super::{H160, H256};
use serde_json as ser; use serde_json as ser;
// #[cfg(feature = "fixed-hash/byteorder-support")]
#[test] #[test]
fn test_serialize_h160() { fn test_serialize_h160() {
let tests = vec![ let tests = vec![
(H160::from(0), "0x0000000000000000000000000000000000000000"), (H160::from_low_u64_be(0), "0x0000000000000000000000000000000000000000"),
(H160::from(2), "0x0000000000000000000000000000000000000002"), (H160::from_low_u64_be(2), "0x0000000000000000000000000000000000000002"),
(H160::from(15), "0x000000000000000000000000000000000000000f"), (H160::from_low_u64_be(15), "0x000000000000000000000000000000000000000f"),
(H160::from(16), "0x0000000000000000000000000000000000000010"), (H160::from_low_u64_be(16), "0x0000000000000000000000000000000000000010"),
(H160::from(1_000), "0x00000000000000000000000000000000000003e8"), (H160::from_low_u64_be(1_000), "0x00000000000000000000000000000000000003e8"),
(H160::from(100_000), "0x00000000000000000000000000000000000186a0"), (H160::from_low_u64_be(100_000), "0x00000000000000000000000000000000000186a0"),
(H160::from(u64::max_value()), "0x000000000000000000000000ffffffffffffffff"), (H160::from_low_u64_be(u64::max_value()), "0x000000000000000000000000ffffffffffffffff"),
]; ];
for (number, expected) in tests { for (number, expected) in tests {
@ -139,16 +140,17 @@ mod tests {
} }
} }
#[cfg(feature = "byteorder-support")]
#[test] #[test]
fn test_serialize_h256() { fn test_serialize_h256() {
let tests = vec![ let tests = vec![
(H256::from(0), "0x0000000000000000000000000000000000000000000000000000000000000000"), (H256::from_low_u64_be(0), "0x0000000000000000000000000000000000000000000000000000000000000000"),
(H256::from(2), "0x0000000000000000000000000000000000000000000000000000000000000002"), (H256::from_low_u64_be(2), "0x0000000000000000000000000000000000000000000000000000000000000002"),
(H256::from(15), "0x000000000000000000000000000000000000000000000000000000000000000f"), (H256::from_low_u64_be(15), "0x000000000000000000000000000000000000000000000000000000000000000f"),
(H256::from(16), "0x0000000000000000000000000000000000000000000000000000000000000010"), (H256::from_low_u64_be(16), "0x0000000000000000000000000000000000000000000000000000000000000010"),
(H256::from(1_000), "0x00000000000000000000000000000000000000000000000000000000000003e8"), (H256::from_low_u64_be(1_000), "0x00000000000000000000000000000000000000000000000000000000000003e8"),
(H256::from(100_000), "0x00000000000000000000000000000000000000000000000000000000000186a0"), (H256::from_low_u64_be(100_000), "0x00000000000000000000000000000000000000000000000000000000000186a0"),
(H256::from(u64::max_value()), "0x000000000000000000000000000000000000000000000000ffffffffffffffff"), (H256::from_low_u64_be(u64::max_value()), "0x000000000000000000000000000000000000000000000000ffffffffffffffff"),
]; ];
for (number, expected) in tests { for (number, expected) in tests {

View File

@ -24,7 +24,14 @@ mod uint;
pub use uint::{U64, U128, U256, U512}; pub use uint::{U64, U128, U256, U512};
pub use hash::{H32, H64, H128, H160, H256, H264, H512, H520}; pub use hash::{H32, H64, H128, H160, H256, H264, H512, H520};
pub use ethbloom::{Bloom, BloomRef, Input as BloomInput}; 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 Address = H160;
pub type Secret = H256; pub type Secret = H256;

View File

@ -9,4 +9,4 @@ license = "MIT"
libc = { version = "0.2", default-features = false } libc = { version = "0.2", default-features = false }
ethereum-types = { path = "../ethereum-types", default-features = false } ethereum-types = { path = "../ethereum-types", default-features = false }
ethbloom = { path = "../ethbloom", default-features = false } ethbloom = { path = "../ethbloom", default-features = false }
fixed-hash = { version = "0.2", default-features = false } fixed-hash = { version = "0.3.0-beta.2", default-features = false }

View File

@ -6,8 +6,6 @@ extern crate ethereum_types;
extern crate ethbloom; extern crate ethbloom;
extern crate fixed_hash; extern crate fixed_hash;
use ethereum_types::{Address, Public, Secret, Signature};
#[start] #[start]
fn start(_argc: isize, _argv: *const *const u8) -> isize { fn start(_argc: isize, _argv: *const *const u8) -> isize {
0 0