Skip to content

Commit

Permalink
Merge pull request #118 from darwinia-network/denny_fixing_ethereum_b…
Browse files Browse the repository at this point in the history
…ridge

Fixing ethereum bridge
  • Loading branch information
hackfisher authored Nov 27, 2019
2 parents 027e809 + fddfa56 commit d5a1d6d
Show file tree
Hide file tree
Showing 15 changed files with 855 additions and 1,326 deletions.
1,217 changes: 547 additions & 670 deletions Cargo.lock

Large diffs are not rendered by default.

54 changes: 22 additions & 32 deletions core/sr-eth-primitives/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,61 +1,51 @@
[package]
name = "sr-eth-primitives"
version = "0.1.0"
authors = ["hammeWang <[email protected]>"]
version = "0.2.0"
authors = ["Darwinia Network <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html


[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 }
sr-primitives = {git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }
substrate-primitives = {git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }
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"
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/hammeWang/ethbloom.git", default-features = false, features = ["serialize"]}
crunchy = { version = "0.2.2", default-features = false, features = ["limit_256"] }
#substrate-primitives = {git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }
rlp = { package = "rlp", git = "https://github.com/darwinia-network/parity-common.git", default-features = false}
rlp_derive = { git = "https://github.com/darwinia-network/parity-common.git", default-features = false }
primitive-types = { git = "https://github.com/darwinia-network/parity-common.git", default-features = false, features = ["codec", "rlp"] }
ethereum-types = { git = "https://github.com/darwinia-network/parity-common.git", default-features = false, features = ["serialize"] }
keccak-hash = { git = "https://github.com/darwinia-network/parity-common.git", default-features = false}
impl-codec = { git = "https://github.com/darwinia-network/parity-common.git", default-features = false}
fixed-hash = { git = "https://github.com/darwinia-network/parity-common.git", default-features = false}
impl-rlp = { git = "https://github.com/darwinia-network/parity-common.git", default-features = false}
ethbloom = { git ="https://github.com/darwinia-network/parity-common.git", default-features = false, features = ["serialize"]}

[dev-dependencies]
support = { package = "srml-support", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop"}
rustc-hex = "2.0"
keccak-hasher = "0.1"
triehash-ethereum = { version = "0.2", git = "https://github.com/paritytech/parity-ethereum.git" }
keccak-hasher = "0.15.2"
triehash = { package = "triehash", git = "https://github.com/darwinia-network/parity-common.git" }
hex-literal = "0.2.1"


[features]
default = ["std"]
std = [
"serde",
"serde/std",
"codec/std",
"rstd/std",
"sr-primitives/std",
"substrate-primitives/std",
# "substrate-primitives/std",
"rlp/std",
"keccak-hash/std",
"primitive-types/std",
"primitive-types/serde",
"primitive-types/byteorder",
"primitive-types/rustc-hex",
"primitive-types/libc",
"parity-crypto/publickey",
# "primitive-types/serde",
# "primitive-types/byteorder",
# "primitive-types/rustc-hex",
# "primitive-types/libc",
"ethereum-types/std",
"impl-codec/std",
"fixed-hash/std",
"impl-rlp/std",
"hbloom/std",
"hbloom/serialize",
"crunchy/std",
"ethbloom/std",
"ethbloom/serialize",
]

2 changes: 2 additions & 0 deletions core/sr-eth-primitives/src/encoded.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use rstd::vec::Vec;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Header(Vec<u8>);
impl Header {
Expand Down
5 changes: 3 additions & 2 deletions core/sr-eth-primitives/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/// Define errors when verifying eth blocks
use super::*;
#[cfg(feature = "std")]
use std::error::Error;

use codec::{Decode, Encode};

#[derive(PartialEq, Eq, Clone, Copy, Debug, Encode, Decode)]
/// Error indicating value found is outside of a valid range.
pub struct OutOfBounds<T> {
Expand Down
56 changes: 0 additions & 56 deletions core/sr-eth-primitives/src/keccak.rs

This file was deleted.

26 changes: 11 additions & 15 deletions core/sr-eth-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,30 @@
#![cfg_attr(not(feature = "std"), no_std)]

#[macro_use]
extern crate impl_codec;
#[macro_use]
extern crate fixed_hash;
#[macro_use]
extern crate impl_rlp;
pub extern crate rlp_derive;

#[macro_use]
extern crate rlp_derive;
use rstd::vec::Vec;

pub mod encoded;
pub mod error;
pub mod keccak;
//pub mod keccak;
pub mod pow;
pub mod receipt;
pub mod transaction;
//pub mod transaction;

use codec::{Decode, Encode};
pub use ethbloom::{Bloom, Input as BloomInput};
use fixed_hash::construct_fixed_hash;
use impl_codec::impl_fixed_hash_codec;
use impl_rlp::impl_fixed_hash_rlp;

pub use codec::{Decode, Encode};
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;

#[derive(Clone, Copy, Eq, PartialEq, Encode, Decode)]
#[derive(Default, Clone, Copy, Eq, PartialEq, Encode, Decode)]
pub struct BestBlock {
height: u64, // enough for ethereum poa network (kovan)
hash: H256,
Expand Down
64 changes: 43 additions & 21 deletions core/sr-eth-primitives/src/pow.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
/// A simplified prototype for light verification for pow.
use super::*;
use crate::keccak::{keccak_256, keccak_512, H256 as BH256};
//use crate::keccak::{keccak_256, keccak_512, H256 as BH256};
use core::cmp;
use core::convert::{From, Into, TryFrom};
use error::{BlockError, Mismatch, OutOfBounds};
use hbloom::Bloom;
use ethbloom::Bloom;
use keccak_hash::KECCAK_EMPTY_LIST_RLP;
use rstd::collections::btree_map::BTreeMap;
use rstd::mem;
use rstd::result;
use sr_primitives::traits::Saturating;

use codec::{Decode, Encode};

use ethereum_types::BigEndianHash;
use primitive_types::{H160, H256, U128, U256, U512};

use rlp::*;

//use substrate_primitives::RuntimeDebug;

pub const MINIMUM_DIFFICULTY: u128 = 131072;
// TODO: please keep an eye on this.
Expand Down Expand Up @@ -197,12 +205,12 @@ impl EthHeader {

/// Get the hash of this header (keccak of the RLP with seal).
pub fn hash(&self) -> H256 {
self.hash.unwrap_or_else(|| keccak(self.rlp(Seal::With)))
self.hash.unwrap_or_else(|| keccak_hash::keccak(self.rlp(Seal::With)))
}

/// Get the hash of the header excluding the seal
pub fn bare_hash(&self) -> H256 {
keccak(self.rlp(Seal::Without))
keccak_hash::keccak(self.rlp(Seal::Without))
}

/// Encode the header, getting a type-safe wrapper around the RLP.
Expand Down Expand Up @@ -296,23 +304,38 @@ fn difficulty_to_boundary_aux<T: Into<U512>>(difficulty: T) -> ethereum_types::U
}
}

fn quick_get_difficulty(header_hash: &BH256, nonce: u64, mix_hash: &BH256, progpow: bool) -> BH256 {
fn quick_get_difficulty(header_hash: &[u8; 32], nonce: u64, mix_hash: &[u8; 32], progpow: bool) -> [u8; 32] {
let mut first_buf = [0u8; 40];
let mut buf = [0u8; 64 + 32];

#[cfg(feature = "std")]
unsafe {
let hash_len = header_hash.len();
buf[..hash_len].copy_from_slice(header_hash);
buf[hash_len..hash_len + mem::size_of::<u64>()].copy_from_slice(&nonce.to_ne_bytes());

keccak_512::unchecked(buf.as_mut_ptr(), 64, buf.as_ptr(), 40);
buf[64..].copy_from_slice(mix_hash);

let mut hash = [0u8; 32];
keccak_256::unchecked(hash.as_mut_ptr(), hash.len(), buf.as_ptr(), buf.len());

hash
}
let hash_len = header_hash.len();
first_buf[..hash_len].copy_from_slice(header_hash);
first_buf[hash_len..hash_len + mem::size_of::<u64>()].copy_from_slice(&nonce.to_ne_bytes());

keccak_hash::keccak_512(&first_buf, &mut buf);
buf[64..].copy_from_slice(mix_hash);

let mut hash = [0u8; 32];
keccak_hash::keccak_256(&buf, &mut hash);

hash

// let mut buf = [0u8; 64 + 32];
//
// #[cfg(feature = "std")]
// unsafe {
// let hash_len = header_hash.len();
// buf[..hash_len].copy_from_slice(header_hash);
// buf[hash_len..hash_len + mem::size_of::<u64>()].copy_from_slice(&nonce.to_ne_bytes());
//
// keccak_512::unchecked(buf.as_mut_ptr(), 64, buf.as_ptr(), 40);
// buf[64..].copy_from_slice(mix_hash);
//
// let mut hash = [0u8; 32];
// keccak_256::unchecked(hash.as_mut_ptr(), hash.len(), buf.as_ptr(), buf.len());
//
// hash
// }
}

fn calculate_difficulty(header: &EthHeader, parent: &EthHeader) -> U256 {
Expand Down Expand Up @@ -485,5 +508,4 @@ mod tests {
let expected = U256::from_str("92c07e50de0b9").unwrap();
assert_eq!(calculate_difficulty(&header2, &header1), expected);
}

}
30 changes: 19 additions & 11 deletions core/sr-eth-primitives/src/receipt.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use super::*;
//pub use ethereum_types::{Address, Bloom, BloomInput, H160, H256, U128, U256};
use hbloom::{Bloom, Input as BloomInput};
use rlp::{self, Decodable, DecoderError, Encodable, Rlp, RlpStream};
use ethbloom::{Bloom, Input as BloomInput};
use rlp::*;
use rstd::ops::Deref;
use rstd::prelude::*;
use substrate_primitives::RuntimeDebug;
//use substrate_primitives::RuntimeDebug;

#[derive(PartialEq, Eq, Clone, RuntimeDebug, Encode, Decode)]
use codec::{Decode, Encode};
use primitive_types::{H160, H256, U128, U256, U512};

#[derive(PartialEq, Eq, Clone, Encode, Decode)]
pub enum TransactionOutcome {
/// Status and state root are unknown under EIP-98 rules.
Unknown,
Expand All @@ -16,7 +18,7 @@ pub enum TransactionOutcome {
StatusCode(u8),
}

#[derive(PartialEq, Eq, Clone, RuntimeDebug, RlpEncodable, RlpDecodable, Encode, Decode)]
#[derive(PartialEq, Eq, Clone, RlpEncodable, RlpDecodable, Encode, Decode)]
pub struct LogEntry {
/// The address of the contract executing at the point of the `LOG` operation.
pub address: Address,
Expand All @@ -38,8 +40,7 @@ impl LogEntry {
}
}

// TODO: impl Bloom with codec::Encode and codec::Decode
#[derive(PartialEq, Eq, Clone, RuntimeDebug, Encode, Decode)]
#[derive(PartialEq, Eq, Clone, Encode, Decode)]
pub struct Receipt {
/// The total gas used in the block following execution of the transaction.
pub gas_used: U256,
Expand Down Expand Up @@ -117,10 +118,14 @@ impl Decodable for Receipt {
#[cfg(test)]
mod tests {
use super::{Address, LogEntry, Receipt, TransactionOutcome, H256, U128, U256};
use hbloom::Bloom;
use ethbloom::Bloom;
use hex_literal::*;
use rustc_hex::FromHex;
use std::str::FromStr;

use keccak_hasher::KeccakHasher;
use triehash::ordered_trie_root;

#[inline]
fn construct_receipts(
root: Option<H256>,
Expand Down Expand Up @@ -181,9 +186,12 @@ mod tests {
log_entries,
)];

let receipts_root: H256 = triehash_ethereum::ordered_trie_root(receipts.iter().map(|x| ::rlp::encode(x)));
let receipts_root: H256 = H256(triehash::ordered_trie_root::<KeccakHasher, _>(
receipts.iter().map(|x| ::rlp::encode(x)),
));

// let receipts_root: H256 = triehash_ethereum::ordered_trie_root<KeccakHasher, _>();

assert_eq!(receipts_root, expected_root);
}

}
Loading

0 comments on commit d5a1d6d

Please sign in to comment.