From a3d0ce3091ff20b986d28633400ca1c3f0f2b72a Mon Sep 17 00:00:00 2001 From: HackFisher Date: Thu, 28 Nov 2019 15:46:46 +0800 Subject: [PATCH 1/2] clean the code --- Cargo.lock | 3 + core/sr-eth-primitives/Cargo.toml | 4 +- core/sr-eth-primitives/src/pow.rs | 9 +- core/sr-eth-primitives/src/receipt.rs | 3 +- node/runtime/src/lib.rs | 2 +- srml/ethereum-bridge/Cargo.toml | 13 ++- srml/ethereum-bridge/src/lib.rs | 121 +++++++++++++------------- 7 files changed, 79 insertions(+), 76 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 091895354..5683b12be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -698,8 +698,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)", @@ -4061,6 +4063,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/pow.rs b/core/sr-eth-primitives/src/pow.rs index 21ea79fe4..9ef1705d3 100644 --- a/core/sr-eth-primitives/src/pow.rs +++ b/core/sr-eth-primitives/src/pow.rs @@ -9,11 +9,12 @@ use keccak_hash::KECCAK_EMPTY_LIST_RLP; use rstd::collections::btree_map::BTreeMap; use rstd::mem; use rstd::result; +use sr_primitives::RuntimeDebug; use codec::{Decode, Encode}; use ethereum_types::BigEndianHash; -use primitive_types::{H160, H256, U128, U256, U512}; +use primitive_types::{H256, U256, U512}; use rlp::*; @@ -42,7 +43,7 @@ pub const ECIP1010_CONTINUE_TRANSITION: u64 = 0x4c4b40; pub const DIFFICULTY_HARDFORK_TRANSITION: u64 = u64::max_value(); -#[derive(Default, PartialEq, Eq, Clone, Encode, Decode)] +#[derive(Default, PartialEq, Eq, Clone, Encode, Decode, RlpDecodable, RlpEncodable, RuntimeDebug)] pub struct EthHeader { parent_hash: H256, timestamp: u64, @@ -71,7 +72,7 @@ where } } -#[derive(PartialEq, Eq, Clone, Encode, Decode, Copy)] +#[derive(PartialEq, Eq, Clone, Encode, Decode, Copy, RuntimeDebug)] enum Seal { /// The seal/signature is included. With, @@ -79,7 +80,7 @@ enum Seal { Without, } -#[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 2dd711f59..d6f10dd6a 100644 --- a/node/runtime/src/lib.rs +++ b/node/runtime/src/lib.rs @@ -399,7 +399,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 274357c2a..7f5105d8c 100644 --- a/srml/ethereum-bridge/src/lib.rs +++ b/srml/ethereum-bridge/src/lib.rs @@ -6,95 +6,98 @@ // use blake2::Blake2b; use codec::{Decode, Encode}; use rstd::vec::Vec; -use sr_eth_primitives::pow::EthHeader; -use support::{decl_event, decl_module, decl_storage, dispatch::Result, traits::Currency}; +use sr_eth_primitives::{pow::EthHeader, BestBlock, H160, H256, H64, U128, U256, U512}; +use support::{decl_event, decl_module, decl_storage, dispatch::Result}; use system::ensure_signed; -//use sr_eth_primitives::{pow::EthHeader, H160, H256, H64, U128, U256, U512}; -//use sr_primitives::RuntimeDebug; +use sr_primitives::RuntimeDebug; + +use rlp::{decode, encode}; //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}; - 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 { pub BeginNumber get(begin_number): u64; - } -} -// pub BeginHeader get(begin_header): Option; + pub BeginHeader get(begin_header): Option; -// pub BestHeader get(best_header): BestBlock; + pub BestHeader get(best_header): BestBlock; -// 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 BestHashOf get(best_hash_of): map u64 => Option; -// pub HashsOf get(hashs_of): map u64 => Vec; + 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)>; + pub HeaderForIndex get(header_for_index): map H256 => Vec<(u64, T::Hash)>; + } + add_extra_genesis { + config(header): Option>; + config(number): u64; + build(|config| { + if let Some(h) = &config.header { + let header: EthHeader = rlp::decode(&h).expect("can't deserialize the header"); + BeginNumber::put(header.number()); -// add_extra_genesis { -// config(header): Option>; -// config(number): u64; -// build(|config| { -// if let Some(h) = &config.header { -// BeginNumber::put(header.number); -// // >::::genesis_header(header); -// } else { -// BeginNumber::put(config.number); -// } -// }); -// } + } else { + BeginNumber::put(config.number); + } + }); + } +} decl_module! { pub struct Module for enum Call 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 -// } + pub fn genesis_header(origin, header: EthHeader) { + let _relayer = ensure_signed(origin)?; +// BeginHeader::put(header); + } + + 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 + } } } @@ -115,9 +118,9 @@ impl Module { /// 1. if exists? /// 2. verify (difficulty + prev_hash + nonce + re-org) /// 3. challenge - // fn verify(_: &EthHeader) -> Result { - // unimplemented!() - // } + fn verify(_: &EthHeader) -> Result { + unimplemented!() + } fn _punish(_who: &T::AccountId) -> Result { unimplemented!() From 03484c6dbecd3922ac20c5697840e996e8855a99 Mon Sep 17 00:00:00 2001 From: HackFisher Date: Thu, 28 Nov 2019 17:14:24 +0800 Subject: [PATCH 2/2] add event for debug --- srml/ethereum-bridge/src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/srml/ethereum-bridge/src/lib.rs b/srml/ethereum-bridge/src/lib.rs index 6de0a427e..d07393fe9 100644 --- a/srml/ethereum-bridge/src/lib.rs +++ b/srml/ethereum-bridge/src/lib.rs @@ -87,6 +87,10 @@ decl_module! { // 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) { @@ -102,6 +106,7 @@ decl_event! { ::AccountId { NewHeader(EthHeader), + RelayProof(ActionRecord), TODO(AccountId), } }