Skip to content

Commit

Permalink
Adds ChainId as parameter for Ethereum pallet (#48)
Browse files Browse the repository at this point in the history
* Adds ChainId as parameter for Ethereum pallet

* Updates RPC net_version

* Set correct ChainId for template runtime

* Checks for chainId

* Updates substrate (evm v0.17)
  • Loading branch information
crystalin authored Jul 2, 2020
1 parent 2a0fbd5 commit 59399fc
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 11 deletions.
32 changes: 28 additions & 4 deletions Cargo.lock

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

18 changes: 16 additions & 2 deletions frame/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// Ensure we're `no_std` when compiling for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::{decl_module, decl_storage, decl_event, weights::Weight};
use frame_support::{decl_module, decl_storage, decl_error, decl_event, ensure, weights::Weight, traits::Get};
use sp_std::prelude::*;
use frame_system::{self as system, ensure_none};
use ethereum_types::{H160, H64, H256, U256, Bloom};
Expand Down Expand Up @@ -53,6 +53,7 @@ pub type BalanceOf<T> = <T as pallet_balances::Trait>::Balance;
pub trait Trait: frame_system::Trait<Hash=H256> + pallet_balances::Trait + pallet_timestamp::Trait + pallet_evm::Trait {
/// The overarching event type.
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
type ChainId: Get<u64>;
}

decl_storage! {
Expand Down Expand Up @@ -117,6 +118,15 @@ decl_event!(
// against them as the first thing you do in your function. There are three convenience calls
// in system that do the matching for you and return a convenient result: `ensure_signed`,
// `ensure_root` and `ensure_none`.


decl_error! {
pub enum Error for Module<T: Trait> {
/// Transaction signed with wrong chain id
InvalidChainId,
}
}

decl_module! {
// Simple declaration of the `Module` type. Lets the macro know what its working on.
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
Expand All @@ -131,12 +141,16 @@ decl_module! {
fn transact(origin, transaction: ethereum::Transaction) {
ensure_none(origin)?;

ensure!(
transaction.signature.chain_id().unwrap_or_default() == T::ChainId::get(),
Error::<T>::InvalidChainId
);
let mut sig = [0u8; 65];
let mut msg = [0u8; 32];
sig[0..32].copy_from_slice(&transaction.signature.r()[..]);
sig[32..64].copy_from_slice(&transaction.signature.s()[..]);
sig[64] = transaction.signature.standard_v();
msg.copy_from_slice(&transaction.message_hash(Some(sp_io::misc::chain_id()))[..]);
msg.copy_from_slice(&transaction.message_hash(Some(T::ChainId::get()))[..]);

let pubkey = sp_io::crypto::secp256k1_ecdsa_recover(&sig, &msg)
.map_err(|_| "Recover public key failed")?;
Expand Down
6 changes: 4 additions & 2 deletions frame/ethereum/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ impl FeeCalculator for FixedGasPrice {
}

parameter_types! {
pub const TransactionByteFee: u64 = 1;
pub const TransactionByteFee: u64 = 1;
pub const ChainId: u64 = 42;
pub const EVMModuleId: ModuleId = ModuleId(*b"py/evmpa");
}

Expand All @@ -115,7 +116,8 @@ impl pallet_evm::Trait for Test {
}

impl Trait for Test {
type Event = ();
type Event = ();
type ChainId = ChainId;
}

pub type System = frame_system::Module<Test>;
Expand Down
2 changes: 1 addition & 1 deletion rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,6 @@ impl<B, C, SC, P, CT, BE> EthApiT for EthApi<B, C, SC, P, CT, BE> where
Ok(true)
}
fn version(&self) -> Result<String> {
Ok("2.0".to_string())
Ok(self.chain_id().unwrap().unwrap().to_string())
}
}
4 changes: 3 additions & 1 deletion template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ impl balances::Trait for Runtime {

parameter_types! {
pub const TransactionByteFee: Balance = 1;
pub const ChainId: u64 = 42;
}

impl transaction_payment::Trait for Runtime {
Expand Down Expand Up @@ -304,6 +305,7 @@ impl evm::Trait for Runtime {

impl ethereum::Trait for Runtime {
type Event = Event;
type ChainId = ChainId;
}

construct_runtime!(
Expand Down Expand Up @@ -448,7 +450,7 @@ impl_runtime_apis! {

impl frontier_rpc_primitives::EthereumRuntimeApi<Block> for Runtime {
fn chain_id() -> u64 {
sp_io::misc::chain_id()
ChainId::get()
}

fn account_basic(address: H160) -> EVMAccount {
Expand Down
2 changes: 1 addition & 1 deletion vendor/substrate

0 comments on commit 59399fc

Please sign in to comment.