Skip to content
This repository was archived by the owner on Sep 19, 2019. It is now read-only.
Closed
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
7 changes: 3 additions & 4 deletions ethbloom/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ repository = "https://github.com/paritytech/primitives"
[dependencies]
tiny-keccak = "1.4"
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 }
fixed-hash = { version = "0.3", default-features = false }
impl-serde = { version = "0.1", default-features = false, optional = true }

[dev-dependencies]
rand = { version = "0.4" }
Expand All @@ -23,4 +22,4 @@ hex-literal = "0.1.1"
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"]
serialize = ["std", "impl-serde"]
35 changes: 8 additions & 27 deletions ethbloom/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//!
//!
//! ```rust
//! extern crate ethbloom;
//! #[macro_use] extern crate hex_literal;
Expand Down Expand Up @@ -26,15 +26,15 @@
//! ).unwrap();
//! let address = hex!("ef2d6d194084c2de36e0dabfce45d046b37d1106");
//! let topic = hex!("02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc");
//!
//!
//! let mut my_bloom = Bloom::default();
//! assert!(!my_bloom.contains_input(Input::Raw(&address)));
//! assert!(!my_bloom.contains_input(Input::Raw(&topic)));
//!
//! my_bloom.accrue(Input::Raw(&address));
//! assert!(my_bloom.contains_input(Input::Raw(&address)));
//! assert!(!my_bloom.contains_input(Input::Raw(&topic)));
//!
//!
//! my_bloom.accrue(Input::Raw(&topic));
//! assert!(my_bloom.contains_input(Input::Raw(&address)));
//! assert!(my_bloom.contains_input(Input::Raw(&topic)));
Expand All @@ -56,18 +56,13 @@ extern crate crunchy;
extern crate fixed_hash;

#[cfg(feature="serialize")]
extern crate ethereum_types_serialize;

#[cfg(feature="serialize")]
extern crate serde;
#[macro_use]
extern crate impl_serde;

#[cfg(test)]
#[macro_use]
extern crate hex_literal;

#[cfg(feature="serialize")]
use serde::{Serialize, Serializer, Deserialize, Deserializer};

use core::{ops, mem};
use tiny_keccak::keccak256;

Expand Down Expand Up @@ -227,7 +222,7 @@ impl<'a> BloomRef<'a> {
let bloom: Bloom = input.into();
self.contains_bloom(&bloom)
}

pub fn contains_bloom<'b, B>(&self, bloom: B) -> bool where BloomRef<'b>: From<B> {
let bloom_ref: BloomRef = bloom.into();
assert_eq!(self.0.len(), BLOOM_SIZE);
Expand Down Expand Up @@ -259,22 +254,8 @@ impl<'a> From<&'a Bloom> for BloomRef<'a> {
}
}

#[cfg(feature="serialize")]
impl Serialize for Bloom {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
let mut slice = [0u8; 2 + 2 * BLOOM_SIZE];
ethereum_types_serialize::serialize(&mut slice, &self.0, serializer)
}
}

#[cfg(feature="serialize")]
impl<'de> Deserialize<'de> for Bloom {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
let mut bytes = [0; BLOOM_SIZE];
ethereum_types_serialize::deserialize_check_len(deserializer, ethereum_types_serialize::ExpectedLen::Exact(&mut bytes))?;
Ok(Bloom(bytes))
}
}
#[cfg(feature = "serialize")]
impl_fixed_hash_serde!(Bloom, BLOOM_SIZE);

#[cfg(test)]
mod tests {
Expand Down
19 changes: 10 additions & 9 deletions ethereum-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ homepage = "https://github.com/paritytech/primitives"
description = "Ethereum types"

[dependencies]
crunchy = { version = "0.2.1", default-features = false }
crunchy = { version = "0.2", 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.3", default_features = false }
serde = { version = "1.0", optional = true }
uint = { version = "0.5", default_features = false }
fixed-hash = { version = "0.3", default-features = false, features = ["byteorder", "rustc-hex"] }
uint = { version = "0.5", default-features = false }
primitive-types = { version = "0.1", features = ["rlp", "byteorder", "rustc-hex"], default-features = false }
impl-serde = { version = "0.1", default-features = false, optional = true }
impl-rlp = { version = "0.1", default-features = false }

[dev-dependencies]
serde_json = "1.0"

[features]
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"]
default = ["std", "heapsize", "serialize"]
std = ["uint/std", "fixed-hash/std", "ethbloom/std", "crunchy/std", "primitive-types/std"]
heapsize = ["uint/heapsize", "primitive-types/heapsize", "fixed-hash/heapsize", "ethbloom/heapsize"]
serialize = ["std", "impl-serde", "primitive-types/serde", "ethbloom/serialize"]
123 changes: 34 additions & 89 deletions ethereum-types/src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,115 +1,60 @@
use crate::{U64, U128, U256, U512};

#[cfg(feature = "serialize")]
use serde::{Serialize, Serializer, Deserialize, Deserializer};

#[cfg(feature = "serialize")]
use ethereum_types_serialize;

macro_rules! impl_serde {
($name: ident, $len: expr) => {
#[cfg(feature = "serialize")]
impl Serialize for $name {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
let mut slice = [0u8; 2 + 2 * $len];
ethereum_types_serialize::serialize(&mut slice, &self.0, serializer)
}
}
pub trait BigEndianHash {
type Uint;

#[cfg(feature = "serialize")]
impl<'de> Deserialize<'de> for $name {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
let mut bytes = [0u8; $len];
ethereum_types_serialize::deserialize_check_len(deserializer, ethereum_types_serialize::ExpectedLen::Exact(&mut bytes))?;
Ok($name(bytes))
}
}
}
fn from_uint(val: &Self::Uint) -> Self;
fn into_uint(&self) -> Self::Uint;
}

construct_fixed_hash!{ pub struct H32(4); }
impl_fixed_hash_rlp!(H32, 4);
#[cfg(feature = "serialize")] impl_fixed_hash_serde!(H32, 4);

construct_fixed_hash!{ pub struct H64(8); }
impl_fixed_hash_rlp!(H64, 8);
#[cfg(feature = "serialize")] impl_fixed_hash_serde!(H64, 8);

construct_fixed_hash!{ pub struct H128(16); }
impl_fixed_hash_rlp!(H128, 16);
#[cfg(feature = "serialize")] impl_fixed_hash_serde!(H128, 16);

pub use primitive_types::H160;
pub use primitive_types::H256;

construct_fixed_hash!{ pub struct H264(33); }
impl_fixed_hash_rlp!(H264, 33);
#[cfg(feature = "serialize")] impl_fixed_hash_serde!(H264, 33);

pub use primitive_types::H512;

construct_fixed_hash!{ pub struct H520(65); }
impl_fixed_hash_rlp!(H520, 65);
#[cfg(feature = "serialize")] impl_fixed_hash_serde!(H520, 65);

macro_rules! impl_uint_conversions {
($hash: ident, $uint: ident) => {
impl From<$uint> for $hash {
fn from(value: $uint) -> Self {
let mut ret = $hash::zero();
value.to_big_endian(ret.as_bytes_mut());
ret
}
}
impl BigEndianHash for $hash {
type Uint = $uint;

impl<'a> From<&'a $uint> for $hash {
fn from(value: &'a $uint) -> Self {
fn from_uint(value: &$uint) -> Self {
let mut ret = $hash::zero();
value.to_big_endian(ret.as_bytes_mut());
ret
}
}

impl From<$hash> for $uint {
fn from(value: $hash) -> Self {
Self::from(&value)
}
}

impl<'a> From<&'a $hash> for $uint {
fn from(value: &'a $hash) -> Self {
Self::from(value.as_ref() as &[u8])
fn into_uint(&self) -> $uint {
$uint::from(self.as_ref() as &[u8])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really introduced by this PR but the casting it to &[u8] is redundant because fixed_hash impl AsRef<[u8]>

See, https://github.com/paritytech/parity-common/blob/master/fixed-hash/src/hash.rs#L101-#L106

}
}
}
}

impl_serde!(H32, 4);
impl_serde!(H64, 8);
impl_serde!(H128, 16);
impl_serde!(H160, 20);
impl_serde!(H256, 32);
impl_serde!(H264, 33);
impl_serde!(H512, 64);
impl_serde!(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);

impl From<H160> for H256 {
fn from(value: H160) -> H256 {
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::zero();
ret.0[12..32].copy_from_slice(value.as_bytes());
ret
}
}

impl From<u64> for H160 {
fn from(val: u64) -> Self {
H160::from_low_u64_be(val)
}
}

impl From<u64> for H256 {
fn from(val: u64) -> Self {
H256::from_low_u64_be(val)
}
}

#[cfg(test)]
mod tests {
use super::{H160, H256};
Expand Down
11 changes: 7 additions & 4 deletions ethereum-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ extern crate uint as uint_crate;
#[macro_use]
extern crate fixed_hash;
extern crate ethbloom;
extern crate primitive_types;

#[cfg(feature = "serialize")]
extern crate ethereum_types_serialize;
#[cfg(feature = "serialize")]
extern crate serde;
#[macro_use]
extern crate impl_serde;

#[macro_use]
extern crate impl_rlp;

#[cfg(test)]
extern crate serde_json;
Expand All @@ -22,7 +25,7 @@ mod hash;
mod uint;

pub use uint::{U64, U128, U256, U512};
pub use hash::{H32, H64, H128, H160, H256, H264, H512, H520};
pub use hash::{BigEndianHash, H32, H64, H128, H160, H256, H264, H512, H520};
pub use ethbloom::{Bloom, BloomRef, Input as BloomInput};

pub type Address = H160;
Expand Down
Loading