Skip to content

Commit

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

EthRelay module tests
  • Loading branch information
hackfisher authored Dec 6, 2019
2 parents 4aad2f6 + 914f3f5 commit 73f519b
Show file tree
Hide file tree
Showing 24 changed files with 494 additions and 148 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ members = [

"srml/balances",
"srml/eth-relay",
# "srml/eth-backing",
"srml/kton",
"srml/staking",
"srml/support"
Expand Down
4 changes: 2 additions & 2 deletions core/merkle-mountain-range/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
extern crate test;

mod common;
mod merkle_mountain_range;
mod merkle_proof;
mod mmr;

#[allow(unused)]
#[cfg(all(feature = "std", test))]
mod tests;

pub use common::*;
pub use merkle_mountain_range::MerkleMountainRange;
pub use merkle_proof::MerkleProof;
pub use mmr::MerkleMountainRange;
File renamed without changes.
1 change: 1 addition & 0 deletions core/merkle-patricia-trie/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ edition = "2018"
rlp = { git = "https://github.com/darwinia-network/parity-common.git", default-features = false }
hash = { package = "keccak-hash", git = "https://github.com/darwinia-network/parity-common.git", default-features = false }
hashbrown = { version = "0.6.0" }
rstd = { package = "sr-std", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }

[dev-dependencies]
rand = "0.6.3"
Expand Down
2 changes: 1 addition & 1 deletion core/merkle-patricia-trie/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::std::*;
use hashbrown::HashMap;
use rstd::{cell::RefCell, vec::Vec};

#[derive(Debug)]
pub struct MemoryDB {
Expand Down
10 changes: 9 additions & 1 deletion core/merkle-patricia-trie/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
use crate::std::*;
use rlp::DecoderError;
use rstd::{borrow::ToOwned, fmt};

#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(not(feature = "std"))]
use alloc::format;
#[cfg(not(feature = "std"))]
use alloc::string::String;

#[derive(Debug)]
pub enum TrieError {
Expand Down
27 changes: 2 additions & 25 deletions core/merkle-patricia-trie/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,7 @@
// Ensure we're `no_std` when compiling for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(not(feature = "std"))]
extern crate core;

#[cfg(not(feature = "std"))]
mod std {
pub use alloc::borrow::ToOwned;
pub use alloc::format;
pub use alloc::rc::Rc;
pub use alloc::string::String;
pub use alloc::vec;
pub use alloc::vec::Vec;

pub use core::cell::RefCell;
pub use core::fmt;
}

#[cfg(feature = "std")]
mod std {
pub use std::cell::RefCell;
pub use std::fmt;
pub use std::rc::Rc;
}
use rstd::rc::Rc;

mod db;
mod error;
Expand Down Expand Up @@ -65,7 +42,7 @@ where
A: AsRef<[u8]> + Ord,
B: AsRef<[u8]>,
{
let memdb = std::Rc::new(MemoryDB::new());
let memdb = Rc::new(MemoryDB::new());
let mut trie = MerklePatriciaTrie::new(memdb.clone());
data.into_iter().for_each(|(key, value)| {
// TODO the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `core::ops::Try`)
Expand Down
2 changes: 1 addition & 1 deletion core/merkle-patricia-trie/src/nibbles.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::std::*;
use core::cmp::min;
use rstd::{vec, vec::Vec};

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Nibbles {
Expand Down
2 changes: 1 addition & 1 deletion core/merkle-patricia-trie/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::nibbles::Nibbles;
use crate::std::*;
use rstd::{cell::RefCell, rc::Rc, vec::Vec};

#[derive(Debug, Clone)]
pub enum Node {
Expand Down
2 changes: 1 addition & 1 deletion core/merkle-patricia-trie/src/proof.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::std::*;
use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};
use rstd::vec::Vec;

#[derive(Clone)]
#[cfg_attr(feature = "std", derive(Debug, PartialEq))]
Expand Down
2 changes: 1 addition & 1 deletion core/merkle-patricia-trie/src/trie.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::std::*;
use hash::keccak;
use hashbrown::{HashMap, HashSet};
use rlp::{Prototype, Rlp, RlpStream};
use rstd::{cell::RefCell, rc::Rc, vec, vec::Vec};

use crate::db::MemoryDB;
use crate::error::TrieError;
Expand Down
32 changes: 16 additions & 16 deletions core/sr-eth-primitives/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ enum Seal {

#[derive(Default, PartialEq, Eq, Clone, Encode, Decode, RlpEncodable, RlpDecodable, RuntimeDebug)]
pub struct EthHeader {
parent_hash: H256,
timestamp: u64,
number: BlockNumber,
author: Address,
transactions_root: H256,
uncles_hash: H256,
extra_data: Bytes,
state_root: H256,
receipts_root: H256,
log_bloom: Bloom,
gas_used: U256,
gas_limit: U256,
difficulty: U256,
seal: Vec<Bytes>,
hash: Option<H256>,
pub parent_hash: H256,
pub timestamp: u64,
pub number: BlockNumber,
pub author: Address,
pub transactions_root: H256,
pub uncles_hash: H256,
pub extra_data: Bytes,
pub state_root: H256,
pub receipts_root: H256,
pub log_bloom: Bloom,
pub gas_used: U256,
pub gas_limit: U256,
pub difficulty: U256,
pub seal: Vec<Bytes>,
pub hash: Option<H256>,
}

/// Alter value of given field, reset memoised hash if changed.
Expand Down Expand Up @@ -325,7 +325,7 @@ mod tests {
fn can_calculate_difficulty_ropsten() {
let (header1, header2) = ropsten_sequential_header();
let expected = U256::from_str("f3c49f25").unwrap();
let mut ethash_params = EthashPartial::ropsten_test();
let mut ethash_params = EthashPartial::ropsten_testnet();
// ethash_params.set_difficulty_bomb_delays(0xc3500, 5000000);
assert_eq!(ethash_params.calculate_difficulty(&header2, &header1), expected);
}
Expand Down
9 changes: 3 additions & 6 deletions core/sr-eth-primitives/src/pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ use codec::{Decode, Encode};
use core::cmp;
use core::convert::{From, Into, TryFrom};
use error::{BlockError, Mismatch, OutOfBounds};
use ethbloom::Bloom;
use ethereum_types::BigEndianHash;
use header::EthHeader;
use keccak_hash::KECCAK_EMPTY_LIST_RLP;
use primitive_types::{H160, H256, U128, U256, U512};
use primitive_types::{H256, U256, U512};
use rlp::*;
use rstd::{collections::btree_map::BTreeMap, mem, result};
use sr_primitives::RuntimeDebug;
Expand Down Expand Up @@ -86,9 +85,7 @@ impl EthashPartial {
}
}

/// TODO: to find out the exact ropsten params, only for testing.
#[cfg(feature = "std")]
pub fn ropsten_test() -> Self {
pub fn ropsten_testnet() -> Self {
EthashPartial {
minimum_difficulty: U256::from(0x20000),
difficulty_bound_divisor: U256::from(0x0800),
Expand Down Expand Up @@ -279,7 +276,7 @@ fn difficulty_to_boundary_aux<T: Into<U512>>(difficulty: T) -> ethereum_types::U
}
}

fn quick_get_difficulty(header_hash: &[u8; 32], nonce: u64, mix_hash: &[u8; 32], progpow: bool) -> [u8; 32] {
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];

Expand Down
2 changes: 1 addition & 1 deletion node/runtime/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub mod time {

pub const EPOCH_DURATION_IN_BLOCKS: BlockNumber = 10 * MINUTES;
pub const EPOCH_DURATION_IN_SLOTS: u64 = {
const SLOT_FILL_RATE: f64 = MILLISECS_PER_BLOCK as f64 / SLOT_DURATION as f64;
// const SLOT_FILL_RATE: f64 = MILLISECS_PER_BLOCK as f64 / SLOT_DURATION as f64;

// Develop
2
Expand Down
6 changes: 6 additions & 0 deletions node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,14 @@ impl staking::Trait for Runtime {
type SessionInterface = Self;
}

parameter_types! {
pub const ETH_MAINET: u64 = 0;
pub const ETH_ROPSTEN: u64 = 1;
}

impl eth_relay::Trait for Runtime {
type Event = Event;
type EthNetwork = ETH_ROPSTEN;
}

construct_runtime!(
Expand Down
3 changes: 0 additions & 3 deletions srml/balances/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

//! Test utilities

#![cfg(test)]

use crate::{GenesisConfig, Module, Trait};
use primitives::H256;
use runtime_io;
Expand Down
3 changes: 0 additions & 3 deletions srml/balances/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

//! Tests for the module.

#![cfg(test)]

use sr_primitives::traits::SignedExtension;
use support::{
assert_err, assert_noop, assert_ok,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "darwinia-ethereum-migrate"
name = "darwinia-eth-backing"
version = "0.1.0"
authors = ["Darwinia Network <[email protected]>"]
edition = "2018"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub trait Trait: system::Trait {
// config() require `serde = { version = "1.0.101", optional = true }`
// tracking issue: https://github.com/rust-lang/rust/issues/27812
decl_storage! {
trait Store for Module<T: Trait> as EthMigrate {
trait Store for Module<T: Trait> as EthBacking {
pub DepositPool get(deposit_pool) config(): RingBalanceOf<T>;
pub DepositValue get(deposit_value): RingBalanceOf<T>;

Expand Down Expand Up @@ -69,13 +69,6 @@ impl<T: Trait> Module<T> {
unimplemented!()
}

/// 1. if exists?
/// 2. verify (difficulty + prev_hash + nonce)
/// 3. challenge
fn verify(_: &Header) -> Result {
unimplemented!()
}

fn _punish(_who: &T::AccountId) -> Result {
unimplemented!()
}
Expand Down
9 changes: 9 additions & 0 deletions srml/eth-relay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ rlp = { package = "rlp", git = "https://github.com/darwinia-network/parity-commo
ethash = { git = "https://github.com/hammeWang/ethash-rs.git", rev = "70a4f078", default-features = false}
merkle-patricia-trie = { path = "../../core/merkle-patricia-trie", default-features = false}

[dev-dependencies]
runtime-io = { package = "sr-io", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop"}
primitives = { package = "substrate-primitives", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop" }
transaction-payment = { package = "srml-transaction-payment", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop" }
rustc-hex = "2.0"
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 = [
Expand Down
Loading

0 comments on commit 73f519b

Please sign in to comment.