Skip to content
This repository has been archived by the owner on Mar 13, 2023. It is now read-only.

Commit

Permalink
add ethereum network type support, fixed #272
Browse files Browse the repository at this point in the history
  • Loading branch information
hackfisher committed Sep 11, 2020
1 parent e320bdf commit 1acf69f
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 19 deletions.
2 changes: 2 additions & 0 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,10 +842,12 @@ type EnsureRootOrHalfTechnicalComittee = EnsureOneOf<
>;
parameter_types! {
pub const EthereumRelayModuleId: ModuleId = ModuleId(*b"da/ethrl");
pub const EthereumNetwork: ethereum_primitives::EthereumNetworkType = ethereum_primitives::EthereumNetworkType::Ropsten;
}
impl darwinia_ethereum_relay::Trait for Runtime {
type ModuleId = EthereumRelayModuleId;
type Event = Event;
type EthereumNetwork = EthereumNetwork;
type Call = Call;
type Currency = Ring;
type RelayerGame = EthereumRelayerGame;
Expand Down
3 changes: 1 addition & 2 deletions frame/bridge/ethereum/backing/src/test_with_linear_relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ use sp_runtime::{traits::Dispatchable, AccountId32};
// --- darwinia ---
use crate::*;
use array_bytes::hex_bytes_unchecked;
use darwinia_ethereum_linear_relay::EthereumNetworkType;
use darwinia_staking::{RewardDestination, StakingBalance, StakingLedger, TimeDepositItem};
use darwinia_support::balance::lock::StakingLock;
use ethereum_primitives::{header::EthereumHeader, receipt::EthereumReceiptProof};
use ethereum_primitives::{EthereumNetworkType, header::EthereumHeader, receipt::EthereumReceiptProof};

type EthereumRelay = darwinia_ethereum_linear_relay::Module<Test>;

Expand Down
4 changes: 3 additions & 1 deletion frame/bridge/ethereum/backing/src/test_with_relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ use darwinia_ethereum_relay::{EthereumHeaderThing, EthereumHeaderThingWithProof,
use darwinia_relay_primitives::*;
use darwinia_staking::{RewardDestination, StakingBalance, StakingLedger, TimeDepositItem};
use darwinia_support::balance::lock::StakingLock;
use ethereum_primitives::{header::EthereumHeader, receipt::EthereumReceiptProof};
use ethereum_primitives::{header::EthereumHeader, receipt::EthereumReceiptProof, EthereumNetworkType};

type EthereumRelay = darwinia_ethereum_relay::Module<Test>;

decl_tests!();

parameter_types! {
pub const EthereumRelayModuleId: ModuleId = ModuleId(*b"da/ethrl");
pub const EthereumNetwork: EthereumNetworkType = EthereumNetworkType::Ropsten;
}
impl darwinia_ethereum_relay::Trait for Test {
type ModuleId = EthereumRelayModuleId;
type Event = ();
type EthereumNetwork = EthereumNetwork;
type RelayerGame = UnusedRelayerGame;
type ApproveOrigin = EnsureRoot<AccountId>;
type RejectOrigin = EnsureRoot<AccountId>;
Expand Down
13 changes: 1 addition & 12 deletions frame/bridge/ethereum/linear-relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ use ethereum_primitives::{
header::EthereumHeader,
pow::EthashPartial,
receipt::{EthereumReceipt, EthereumReceiptProof, EthereumTransactionIndex},
EthereumBlockNumber, H256, U256,
EthereumBlockNumber, H256, U256, EthereumNetworkType
};
use types::*;

Expand Down Expand Up @@ -817,17 +817,6 @@ impl<T: Trait + Send + Sync> SignedExtension for CheckEthereumRelayHeaderHash<T>
}
}

#[derive(Clone, PartialEq, Encode, Decode)]
pub enum EthereumNetworkType {
Mainnet,
Ropsten,
}
impl Default for EthereumNetworkType {
fn default() -> EthereumNetworkType {
EthereumNetworkType::Mainnet
}
}

/// Familial details concerning a block
#[derive(Clone, Default, PartialEq, Encode, Decode)]
pub struct EthereumHeaderBrief<AccountId> {
Expand Down
17 changes: 13 additions & 4 deletions frame/bridge/ethereum/relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use ethereum_primitives::{
header::EthereumHeader,
pow::EthashPartial,
receipt::{EthereumReceipt, EthereumReceiptProof, EthereumTransactionIndex},
EthereumBlockNumber, H256,
EthereumBlockNumber, H256, EthereumNetworkType
};
use types::*;

Expand All @@ -67,6 +67,8 @@ pub trait Trait: frame_system::Trait {

type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;

type EthereumNetwork: Get<EthereumNetworkType>;

type Call: Dispatchable + From<Call<Self>> + IsSubType<Call<Self>> + Clone;

type Currency: LockableCurrency<Self::AccountId, Moment = Self::BlockNumber>
Expand Down Expand Up @@ -304,9 +306,16 @@ impl<T: Trait> Module<T> {
T::ModuleId::get().into_account()
}

fn ethash_params() -> EthashPartial {
match T::EthereumNetwork::get() {
EthereumNetworkType::Mainnet => EthashPartial::production(),
EthereumNetworkType::Ropsten => EthashPartial::ropsten_testnet(),
}
}

/// validate block with the hash, difficulty of confirmed headers
fn verify_block_with_confirmed_blocks(header: &EthereumHeader) -> bool {
let eth_partial = EthashPartial::production();
let eth_partial = Self::ethash_params();
let last_confirmed_block = Self::best_block_number();

if header.number <= last_confirmed_block {
Expand Down Expand Up @@ -338,7 +347,7 @@ impl<T: Trait> Module<T> {
return false;
}

let eth_partial = EthashPartial::production();
let eth_partial = Self::ethash_params();

if eth_partial.verify_block_basic(header).is_err() {
return false;
Expand Down Expand Up @@ -516,7 +525,7 @@ impl<T: Trait> Relayable for Module<T> {
fn on_chain_arbitrate(proposal: Vec<Self::HeaderThing>) -> DispatchResult {
// Currently Ethereum samples function is continuously sampling

let eth_partial = EthashPartial::production();
let eth_partial = Self::ethash_params();

for i in 1..proposal.len() - 1 {
let header = &proposal[i].header;
Expand Down
2 changes: 2 additions & 0 deletions frame/bridge/ethereum/relay/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ darwinia_support::impl_account_data! {
pub struct Test;
parameter_types! {
pub const EthereumRelayModuleId: ModuleId = ModuleId(*b"da/ethrl");
pub const EthereumNetwork: EthereumNetworkType = EthereumNetworkType::Mainnet;
}
impl Trait for Test {
type ModuleId = EthereumRelayModuleId;
type Event = ();
type EthereumNetwork = EthereumNetwork;
type Call = Call;
type Currency = Ring;
type RelayerGame = UnusedRelayerGame;
Expand Down
12 changes: 12 additions & 0 deletions primitives/ethereum-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@ pub use ethereum_types::H64;
pub use primitive_types::{H160, H256, U128, U256, U512};

use sp_std::prelude::*;
use codec::{Decode, Encode};

pub type Bytes = Vec<u8>;
pub type EthereumAddress = H160;
pub type EthereumBlockNumber = u64;

#[derive(Clone, PartialEq, Encode, Decode)]
pub enum EthereumNetworkType {
Mainnet,
Ropsten,
}
impl Default for EthereumNetworkType {
fn default() -> EthereumNetworkType {
EthereumNetworkType::Mainnet
}
}

0 comments on commit 1acf69f

Please sign in to comment.