Skip to content

Commit

Permalink
Merge pull request #114 from hammeWang/develop
Browse files Browse the repository at this point in the history
Add `Encode/Decode` trait for eth types
  • Loading branch information
hackfisher authored Nov 26, 2019
2 parents 4b69cc9 + 31cb03b commit 63f5f34
Show file tree
Hide file tree
Showing 7 changed files with 371 additions and 114 deletions.
67 changes: 66 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 14 additions & 5 deletions core/sr-eth-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2018"


[dependencies]
tiny-keccak = { version = "1.5", features = ["keccak"] }
serde = { version = "1.0.101", optional = true, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
rstd = { package = "sr-std", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }
Expand All @@ -16,13 +17,15 @@ substrate-primitives = {git = "https://github.com/darwinia-network/substrate.git
rlp = { version = "0.4.3", optional = true }
parity-crypto = { version = "0.4.2", features = ["publickey"] }
rlp_derive = { git = "https://github.com/paritytech/parity-ethereum.git", default-features = false }
primitive-types = { version = "0.6.0", default-feature = false, features = ["codec", "rlp"] }
ethereum-types = { version = "0.8.0", default-feature = false, features = ["serialize"] }
keccak-hash = "0.4.0"
ethbloom = { version = "0.8", default-features = false, features = ["serialize"] }
impl-codec = { version = "0.4", default-features = false, optional = true }
fixed-hash = { version = "0.4", default-features = false}
impl-rlp = { version = "0.2", default-features = false, optional = true }

hbloom = { package = 'ethbloom', git ="https://github.com/paritytech/parity-common.git", default-features = false}
crunchy = { version = "0.2.2", default-features = false, features = ["limit_256"] }

[dev-dependencies]
support = { package = "srml-support", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop"}
Expand All @@ -42,14 +45,20 @@ std = [
"substrate-primitives/std",
"rlp/std",
"keccak-hash/std",
"ethereum-types/std",
"primitive-types/std",
"primitive-types/serde",
"primitive-types/byteorder",
"primitive-types/rustc-hex",
"primitive-types/libc",
"parity-crypto/publickey",
"ethereum-types/std",
"ethereum-types/serialize",
"ethbloom/std",
"ethbloom/serialize",
"ethereum-types/std",
"impl-codec/std",
"fixed-hash/std",
"impl-rlp/std"
"impl-rlp/std",
"hbloom/std",
"hbloom/serialize",
"crunchy/std",
]

4 changes: 2 additions & 2 deletions core/sr-eth-primitives/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use super::*;
#[cfg(feature = "std")]
use std::error::Error;
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
#[derive(PartialEq, Eq, Clone, Copy, Debug, Encode, Decode)]
/// Error indicating value found is outside of a valid range.
pub struct OutOfBounds<T> {
/// Minimum allowed value.
Expand All @@ -13,7 +13,7 @@ pub struct OutOfBounds<T> {
pub found: T,
}

#[derive(PartialEq, Eq, Clone, Copy, Debug)]
#[derive(PartialEq, Eq, Clone, Copy, Debug, Encode, Decode)]
/// Error indicating an expected value was not found.
pub struct Mismatch<T> {
/// Value expected.
Expand Down
105 changes: 11 additions & 94 deletions core/sr-eth-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,109 +17,26 @@ pub mod keccak;
pub mod pow;
pub mod receipt;
pub mod transaction;
pub mod types;

pub use codec::{Decode, Encode};
pub use ethereum_types::{Address, BigEndianHash, Bloom, BloomInput, H160, H256, H64, U128, U256, U512};
pub use ethereum_types::BigEndianHash;
pub use impl_codec::impl_fixed_hash_codec;
pub use keccak_hash::keccak;
pub use primitive_types::{H160, H256, U128, U256, U512};
pub use rlp::{self, DecoderError, Encodable, Rlp, RlpStream};

pub type Bytes = Vec<u8>;

pub type Address = H160;
pub type BlockNumber = u64;
pub struct BestBLock {

#[derive(Clone, Copy, Eq, PartialEq, Encode, Decode)]
pub struct BestBlock {
height: u64, // enough for ethereum poa network (kovan)
hash: H256,
total_difficulty: U256,
}

// TODO: later rewrite Bloom to impl Encode and Decode
//pub use ethbloom::{BloomRef, Input};
//
//// 3 according to yellowpaper
//const BLOOM_BITS: u32 = 3;
//const BLOOM_SIZE: usize = 256;
//
//construct_fixed_hash! {pub struct Bloom(BLOOM_SIZE);}
//impl_fixed_hash_rlp!(Bloom, BLOOM_SIZE);
//impl_fixed_hash_codec!(Bloom, BLOOM_SIZE);
//
//impl<'a> From<Input<'a>> for Bloom {
// fn from(input: Input<'a>) -> Bloom {
// let mut bloom = Bloom::default();
// bloom.accrue(input);
// bloom
// }
//}
//
//impl Bloom {
// pub fn is_empty(&self) -> bool {
// self.0.iter().all(|x| *x == 0)
// }
//
// pub fn contains_input(&self, input: Input<'_>) -> bool {
// let bloom: Bloom = input.into();
// self.contains_bloom(&bloom)
// }
//
// pub fn contains_bloom<'a, B>(&self, bloom: B) -> bool
// where
// BloomRef<'a>: From<B>,
// {
// let bloom_ref: BloomRef = bloom.into();
// // workaround for https://github.com/rust-lang/rust/issues/43644
// self.contains_bloom_ref(bloom_ref)
// }
//
// fn contains_bloom_ref(&self, bloom: BloomRef) -> bool {
// let self_ref: BloomRef = self.into();
// self_ref.contains_bloom(bloom)
// }
//
// pub fn accrue(&mut self, input: Input<'_>) {
// let p = BLOOM_BITS;
//
// let m = self.0.len();
// let bloom_bits = m * 8;
// let mask = bloom_bits - 1;
// let bloom_bytes = (log2(bloom_bits) + 7) / 8;
//
// let hash: Hash = input.into();
//
// // must be a power of 2
// assert_eq!(m & (m - 1), 0);
// // out of range
// assert!(p * bloom_bytes <= hash.len() as u32);
//
// let mut ptr = 0;
//
// assert_eq!(BLOOM_BITS, 3);
// unroll! {
// for i in 0..3 {
// let _ = i;
// let mut index = 0 as usize;
// for _ in 0..bloom_bytes {
// index = (index << 8) | hash[ptr] as usize;
// ptr += 1;
// }
// index &= mask;
// self.0[m - 1 - index / 8] |= 1 << (index % 8);
// }
// }
// }
//
// pub fn accrue_bloom<'a, B>(&mut self, bloom: B)
// where
// BloomRef<'a>: From<B>,
// {
// let bloom_ref: BloomRef = bloom.into();
// assert_eq!(self.0.len(), BLOOM_SIZE);
// assert_eq!(bloom_ref.0.len(), BLOOM_SIZE);
// for i in 0..BLOOM_SIZE {
// self.0[i] |= bloom_ref.0[i];
// }
// }
//
// pub fn data(&self) -> &[u8; BLOOM_SIZE] {
// &self.0
// }
//}
construct_fixed_hash! {pub struct H64(8);}
impl_fixed_hash_rlp!(H64, 8);
impl_fixed_hash_codec!(H64, 8);
Loading

0 comments on commit 63f5f34

Please sign in to comment.