diff --git a/Cargo.lock b/Cargo.lock index cd08dbfdd..0ce79f052 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -807,8 +807,10 @@ name = "darwinia-ethereum-bridge" version = "0.2.0" dependencies = [ "parity-scale-codec 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rlp 0.4.4 (git+https://github.com/darwinia-network/parity-common.git)", "serde 1.0.103 (registry+https://github.com/rust-lang/crates.io-index)", "sr-eth-primitives 0.2.0", + "sr-primitives 2.0.0 (git+https://github.com/darwinia-network/substrate.git?branch=darwinia-develop)", "sr-std 2.0.0 (git+https://github.com/darwinia-network/substrate.git?branch=darwinia-develop)", "srml-support 2.0.0 (git+https://github.com/darwinia-network/substrate.git?branch=darwinia-develop)", "srml-system 2.0.0 (git+https://github.com/darwinia-network/substrate.git?branch=darwinia-develop)", @@ -4208,6 +4210,7 @@ dependencies = [ "rlp_derive 0.1.0 (git+https://github.com/darwinia-network/parity-common.git)", "rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.103 (registry+https://github.com/rust-lang/crates.io-index)", + "sr-primitives 2.0.0 (git+https://github.com/darwinia-network/substrate.git?branch=darwinia-develop)", "sr-std 2.0.0 (git+https://github.com/darwinia-network/substrate.git?branch=darwinia-develop)", "srml-support 2.0.0 (git+https://github.com/darwinia-network/substrate.git?branch=darwinia-develop)", "triehash 0.8.1 (git+https://github.com/darwinia-network/parity-common.git)", diff --git a/core/sr-eth-primitives/Cargo.toml b/core/sr-eth-primitives/Cargo.toml index 3c612eb16..43020e065 100644 --- a/core/sr-eth-primitives/Cargo.toml +++ b/core/sr-eth-primitives/Cargo.toml @@ -8,7 +8,7 @@ edition = "2018" 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 } -#substrate-primitives = {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 } 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" } primitive-types = { git = "https://github.com/darwinia-network/parity-common.git", default-features = false, features = ["codec", "rlp"] } @@ -33,7 +33,7 @@ std = [ "serde/std", "codec/std", "rstd/std", -# "substrate-primitives/std", + "sr-primitives/std", "rlp/std", "keccak-hash/std", "primitive-types/std", diff --git a/core/sr-eth-primitives/src/header.rs b/core/sr-eth-primitives/src/header.rs index e236f2cba..5b13b44a9 100644 --- a/core/sr-eth-primitives/src/header.rs +++ b/core/sr-eth-primitives/src/header.rs @@ -2,8 +2,9 @@ use super::*; use ethbloom::Bloom; use pow::EthashSeal; use rlp::RlpStream; +use sr_primitives::RuntimeDebug; -#[derive(PartialEq, Eq, Clone, Encode, Decode, Copy)] +#[derive(PartialEq, Eq, Clone, Encode, Decode, Copy, RuntimeDebug)] enum Seal { /// The seal/signature is included. With, @@ -11,7 +12,7 @@ enum Seal { Without, } -#[derive(Default, PartialEq, Eq, Clone, Encode, Decode)] +#[derive(Default, PartialEq, Eq, Clone, Encode, Decode, RlpEncodable, RlpDecodable, RuntimeDebug)] pub struct EthHeader { parent_hash: H256, timestamp: u64, diff --git a/core/sr-eth-primitives/src/lib.rs b/core/sr-eth-primitives/src/lib.rs index 86bf36390..4f98f1170 100644 --- a/core/sr-eth-primitives/src/lib.rs +++ b/core/sr-eth-primitives/src/lib.rs @@ -8,7 +8,6 @@ use rstd::vec::Vec; pub mod encoded; pub mod error; -//pub mod keccak; pub mod header; pub mod pow; pub mod receipt; diff --git a/core/sr-eth-primitives/src/pow.rs b/core/sr-eth-primitives/src/pow.rs index 89df58120..1deeec5cd 100644 --- a/core/sr-eth-primitives/src/pow.rs +++ b/core/sr-eth-primitives/src/pow.rs @@ -12,8 +12,7 @@ use keccak_hash::KECCAK_EMPTY_LIST_RLP; use primitive_types::{H160, H256, U128, U256, U512}; use rlp::*; use rstd::{collections::btree_map::BTreeMap, mem, result}; - -//use substrate_primitives::RuntimeDebug; +use sr_primitives::RuntimeDebug; #[derive(Default, PartialEq, Eq, Clone, Encode, Decode)] pub struct EthashPartial { @@ -178,7 +177,7 @@ impl EthashPartial { } } -#[derive(PartialEq, Eq, Clone, Encode, Decode)] +#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)] pub struct EthashSeal { /// Ethash seal mix_hash pub mix_hash: H256, diff --git a/core/sr-eth-primitives/src/receipt.rs b/core/sr-eth-primitives/src/receipt.rs index 09cc8a9e3..6e309cd42 100644 --- a/core/sr-eth-primitives/src/receipt.rs +++ b/core/sr-eth-primitives/src/receipt.rs @@ -1,12 +1,11 @@ use super::*; use ethbloom::{Bloom, Input as BloomInput}; use rlp::*; -use rstd::ops::Deref; use rstd::prelude::*; //use substrate_primitives::RuntimeDebug; use codec::{Decode, Encode}; -use primitive_types::{H160, H256, U128, U256, U512}; +use primitive_types::{H256, U256}; #[derive(PartialEq, Eq, Clone, Encode, Decode)] pub enum TransactionOutcome { diff --git a/node/runtime/src/lib.rs b/node/runtime/src/lib.rs index 6ea4fafb5..47e0b92d9 100644 --- a/node/runtime/src/lib.rs +++ b/node/runtime/src/lib.rs @@ -384,7 +384,7 @@ impl staking::Trait for Runtime { impl ethereum_bridge::Trait for Runtime { type Event = Event; // type Ring = Balances; - type Hash = Hash; + // type Hash = Hash; } construct_runtime!( diff --git a/srml/ethereum-bridge/Cargo.toml b/srml/ethereum-bridge/Cargo.toml index bfe68396a..86455a562 100644 --- a/srml/ethereum-bridge/Cargo.toml +++ b/srml/ethereum-bridge/Cargo.toml @@ -7,28 +7,25 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -#blake2 = { version = "0.8.1", default-features = false } serde = { version = "1.0.101", optional = true } 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 } system = { package = "srml-system", 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 } +sr-primitives = { git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false } support = { package = "srml-support", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false } sr-eth-primitives = { path = "../../core/sr-eth-primitives", default-features = false } -#merkle-mountain-range = { path = "../../core/merkle-mountain-range", default-features = false } - -#web3 = {git = "https://github.com/ABMatrix/rust-web3.git", branch = "develop", default-features = false } +rlp = { package = "rlp", git = "https://github.com/darwinia-network/parity-common.git", default-features = false} [features] default = ["std"] std = [ -# "blake2/std", "serde/std", "codec/std", "rstd/std", -# "sr-primitives/std", + "sr-primitives/std", "support/std", "system/std", - "sr-eth-primitives/std" + "sr-eth-primitives/std", + "rlp/std", ] diff --git a/srml/ethereum-bridge/src/lib.rs b/srml/ethereum-bridge/src/lib.rs index 9ca74d648..d07393fe9 100644 --- a/srml/ethereum-bridge/src/lib.rs +++ b/srml/ethereum-bridge/src/lib.rs @@ -12,32 +12,26 @@ use sr_eth_primitives::{ use support::{decl_event, decl_module, decl_storage, dispatch::Result, traits::Currency}; use system::ensure_signed; -//use sr_primitives::RuntimeDebug; +use sr_primitives::RuntimeDebug; -//use web3::types::{ -// Address, Block, BlockId, BlockNumber, Bytes, CallRequest, Filter, Index, Log, RawHeader, RawReceipt, SyncState, -// Transaction, TransactionId, TransactionReceipt, TransactionRequest, Work, H256, H520, H64, U128, U256, -//}; - -//use merkle_mountain_range::{Hash, MerkleMountainRange}; +use rlp::{decode, encode}; pub trait Trait: system::Trait { type Event: From> + Into<::Event>; - type Hash: rstd::hash::Hash; - // type Hash: {}; + // type Hash: rstd::hash::Hash; } -//#[derive(PartialEq, Eq, Clone, Encode, Decode)] -//pub struct Proof { -// pub nodes: Vec>, -//} +#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)] +pub struct Proof { + pub nodes: Vec>, +} -//#[derive(PartialEq, Eq, Clone, Encode, Decode)] -//pub struct ActionRecord { -// pub index: u64, -// pub proof: Vec, -// pub header_hash: H256, -//} +#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)] +pub struct ActionRecord { + pub index: u64, + pub proof: Vec, + pub header_hash: H256, +} decl_storage! { trait Store for Module as EthBridge { @@ -48,32 +42,30 @@ decl_storage! { /// pub BlockList get(block_list): map EthBlockNumber => EthHeader; - -// pub HeaderOf get(header_of): map H256 => Option; + pub HeaderOf get(header_of): map H256 => Option; // pub BestHashOf get(best_hash_of): map u64 => Option; // pub HashsOf get(hashs_of): map u64 => Vec; -// Block delay for verify transaction -// pub FinalizeNumber get(finalize_number): Option; + /// Block delay for verify transaction + pub FinalizeNumber get(finalize_number): Option; -// pub ActionOf get(action_of): map T::Hash => Option; + pub ActionOf get(action_of): map T::Hash => Option; -// pub HeaderForIndex get(header_for_index): map H256 => Vec<(u64, T::Hash)>; - -// add_extra_genesis { -// config(header): Option>; -// config(number): u64;z -// build(|config| { -// if let Some(h) = &config.header { -// BeginNumber::put(header.number); -// -// >::::genesis_header(header); -// } else { + pub HeaderForIndex get(header_for_index): map H256 => Vec<(u64, T::Hash)>; + } + add_extra_genesis { + config(header): Option>; + build(|config| { + if let Some(h) = &config.header { + let header: EthHeader = rlp::decode(&h).expect("can't deserialize the header"); + BeginHeader::put(header.clone()); + + >::genesis_header(header); + } else { // BeginNumber::put(config.number); -// } -// }); -// } + } + }); } } @@ -82,21 +74,29 @@ decl_module! { where origin: T::Origin { -// pub fn store_block_header(origin, header: EthHeader) { -// let _relayer = ensure_signed(origin)?; -// let _ = Self::verify(&header)?; -// } -// -// pub fn relay_receipt(origin, proof: ActionRecord) { -// // confirm that the block hash is right -// // get the MPT from the block header -// // Using MPT to verify the proof and index etc. -// } -// -// pub fn submit_header(origin, header: EthHeader) { -// // if header confirmed then return -// // if header in unverified header then challenge -// } + fn deposit_event() = default; + + pub fn store_block_header(origin, header: EthHeader) { + let _relayer = ensure_signed(origin)?; + let _ = Self::verify(&header)?; + + >::deposit_event(RawEvent::NewHeader(header)); + } + + pub fn relay_receipt(origin, proof: ActionRecord) { + // confirm that the block hash is right + // get the receipt MPT trie root from the block header + // Using receipt MPT trie root to verify the proof and index etc. + + let _relayer = ensure_signed(origin)?; + + >::deposit_event(RawEvent::RelayProof(proof)); + } + + pub fn submit_header(origin, header: EthHeader) { + // if header confirmed then return + // if header in unverified header then challenge + } } } @@ -105,11 +105,17 @@ decl_event! { where ::AccountId { + NewHeader(EthHeader), + RelayProof(ActionRecord), TODO(AccountId), } } impl Module { + pub fn genesis_header(header: EthHeader) { + unimplemented!() + } + pub fn adjust_deposit_value() { unimplemented!() }