Skip to content

Commit

Permalink
feat: add frontier evm
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <[email protected]>
  • Loading branch information
gregdhill committed Jul 27, 2023
1 parent f2f48a1 commit 0fccbdd
Show file tree
Hide file tree
Showing 30 changed files with 3,364 additions and 494 deletions.
1,731 changes: 1,319 additions & 412 deletions Cargo.lock

Large diffs are not rendered by default.

21 changes: 20 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,23 @@ orml-utilities = { git = "https://github.com/open-web3-stack//open-runtime-modul
orml-vesting = { git = "https://github.com/open-web3-stack//open-runtime-module-library", rev = "ca05423f4f32be1d30765caacdc7d90130f5554a" }
orml-xcm-support = { git = "https://github.com/open-web3-stack//open-runtime-module-library", rev = "ca05423f4f32be1d30765caacdc7d90130f5554a" }
orml-xcm = { git = "https://github.com/open-web3-stack//open-runtime-module-library", rev = "ca05423f4f32be1d30765caacdc7d90130f5554a" }
orml-xtokens = { git = "https://github.com/open-web3-stack//open-runtime-module-library", rev = "ca05423f4f32be1d30765caacdc7d90130f5554a" }
orml-xtokens = { git = "https://github.com/open-web3-stack//open-runtime-module-library", rev = "ca05423f4f32be1d30765caacdc7d90130f5554a" }

[patch."https://github.com/paritytech/frontier"]
fc-consensus = { git = "https://github.com/paritytech//frontier", branch = "polkadot-v0.9.42" }
fc-db = { git = "https://github.com/paritytech//frontier", branch = "polkadot-v0.9.42" }
fc-mapping-sync = { git = "https://github.com/paritytech//frontier", branch = "polkadot-v0.9.42" }
fc-rpc = { git = "https://github.com/paritytech//frontier", branch = "polkadot-v0.9.42" }
fc-rpc-core = { git = "https://github.com/paritytech//frontier", branch = "polkadot-v0.9.42" }
fc-storage = { git = "https://github.com/paritytech//frontier", branch = "polkadot-v0.9.42" }

fp-ethereum = { git = "https://github.com/paritytech//frontier", branch = "polkadot-v0.9.42" }
fp-evm = { git = "https://github.com/paritytech//frontier", branch = "polkadot-v0.9.42" }
fp-rpc = { git = "https://github.com/paritytech//frontier", branch = "polkadot-v0.9.42" }
fp-self-contained = { git = "https://github.com/paritytech//frontier", branch = "polkadot-v0.9.42" }

pallet-base-fee = { git = "https://github.com/paritytech//frontier", branch = "polkadot-v0.9.42" }
pallet-ethereum = { git = "https://github.com/paritytech//frontier", branch = "polkadot-v0.9.42" }
pallet-evm = { git = "https://github.com/paritytech//frontier", branch = "polkadot-v0.9.42" }
pallet-evm-chain-id = { git = "https://github.com/paritytech//frontier", branch = "polkadot-v0.9.42" }
pallet-evm-precompile-dispatch = { git = "https://github.com/paritytech//frontier", branch = "polkadot-v0.9.42" }
11 changes: 11 additions & 0 deletions parachain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ name = "interbtc-parachain"
substrate-build-script-utils = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31" }

[dependencies]
async-trait = "0.1"
clap = { version = "4.0.9", features = [ "derive" ] }
log = "0.4.8"
codec = { package = "parity-scale-codec", version = "3.1.5" }
Expand Down Expand Up @@ -113,6 +114,16 @@ polkadot-cli = { git = "https://github.com/paritytech/polkadot", branch = "relea
polkadot-service = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.31" }
polkadot-parachain = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.31" }

# Frontier dependencies
fc-consensus = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42" }
fc-db = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42" }
fc-mapping-sync = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42", features = ["sql"] }
fc-rpc = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42" }
fc-rpc-core = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42" }
fp-evm = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42" }
fp-rpc = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42" }
pallet-evm = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42" }

[features]
default = []

Expand Down
31 changes: 30 additions & 1 deletion parachain/runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ targets = ['x86_64-unknown-linux-gnu']
# Substrate dependencies
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false, optional = true }
xcm = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.31", default-features = false }
xcm-executor = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.31", default-features = false }
pallet-aura = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }

# Cumulus dependencies
parachain-info = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.31", default-features = false }

# Parachain dependencies
annuity = { path = "../../../crates/annuity", default-features = false }
btc-relay = { path = "../../../crates/btc-relay", default-features = false }
Expand Down Expand Up @@ -53,6 +57,15 @@ orml-xtokens = { git = "https://github.com/open-web3-stack/open-runtime-module-l
orml-xcm-support = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "3fcd3cf9e63fe80fd9671912833a900ba09d1cc0", default-features = false }
orml-unknown-tokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "3fcd3cf9e63fe80fd9671912833a900ba09d1cc0", default-features = false }

# Frontier dependencies
pallet-base-fee = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42", default-features = false }
pallet-ethereum = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42", default-features = false }
pallet-evm = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42", default-features = false }
pallet-evm-chain-id = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42", default-features = false }
pallet-evm-precompile-modexp = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42", default-features = false }
pallet-evm-precompile-sha3fips = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42", default-features = false }
pallet-evm-precompile-simple = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42", default-features = false }

# TODO: enable weak dependency activation when available
# https://github.com/rust-lang/cargo/issues/8832
# https://github.com/paritytech/substrate/issues/8161
Expand All @@ -62,13 +75,16 @@ default = ["std"]
std = [
"sp-std/std",
"sp-runtime/std",
"sp-core/std",
"frame-support/std",
"frame-system/std",
"frame-benchmarking?/std",
"xcm/std",
"xcm-executor/std",
"pallet-aura/std",

"parachain-info/std",

"annuity/std",
"btc-relay/std",
"clients-info/std",
Expand Down Expand Up @@ -100,6 +116,14 @@ std = [
"orml-xtokens/std",
"orml-xcm-support/std",
"orml-unknown-tokens/std",

"pallet-base-fee/std",
"pallet-ethereum/std",
"pallet-evm/std",
"pallet-evm-chain-id/std",
"pallet-evm-precompile-modexp/std",
"pallet-evm-precompile-sha3fips/std",
"pallet-evm-precompile-simple/std",
]
try-runtime = [
"frame-support/try-runtime",
Expand Down Expand Up @@ -128,7 +152,12 @@ try-runtime = [
"orml-vesting/try-runtime",
"orml-asset-registry/try-runtime",
"orml-xtokens/try-runtime",
"orml-unknown-tokens/try-runtime"
"orml-unknown-tokens/try-runtime",

"pallet-base-fee/try-runtime",
"pallet-ethereum/try-runtime",
"pallet-evm/try-runtime",
"pallet-evm-chain-id/try-runtime",
]
runtime-benchmarks = [
"frame-benchmarking",
Expand Down
71 changes: 71 additions & 0 deletions parachain/runtime/common/src/evm/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use frame_support::{
traits::OnRuntimeUpgrade,
weights::{constants::WEIGHT_REF_TIME_PER_SECOND, Weight},
};
use pallet_ethereum::{Transaction, TransactionAction};
use sp_core::Get;
use sp_runtime::Permill;
use sp_std::marker::PhantomData;

pub mod precompiles;

/// Current approximation of the gas/s consumption (Moonbeam)
pub const GAS_PER_SECOND: u64 = 40_000_000;
/// Approximate ratio of the amount of Weight per Gas (Moonbeam)
pub const WEIGHT_PER_GAS: u64 = WEIGHT_REF_TIME_PER_SECOND / GAS_PER_SECOND;

/// Sets the ideal block fullness to 50%.
/// If the block weight is between:
/// - 0-50% the gas fee will decrease
/// - 50-100% the gas fee will increase
pub struct BaseFeeThreshold;
impl pallet_base_fee::BaseFeeThreshold for BaseFeeThreshold {
fn lower() -> Permill {
Permill::zero()
}
fn ideal() -> Permill {
Permill::from_parts(500_000)
}
fn upper() -> Permill {
Permill::from_parts(1_000_000)
}
}

/// Get the "action" (call or create) of an Ethereum transaction
pub trait GetTransactionAction {
fn action(&self) -> TransactionAction;
}

impl GetTransactionAction for Transaction {
fn action(&self) -> TransactionAction {
match self {
Transaction::Legacy(transaction) => transaction.action,
Transaction::EIP2930(transaction) => transaction.action,
Transaction::EIP1559(transaction) => transaction.action,
}
}
}

/// Set the EVM chain ID based on the parachain ID
pub struct SetEvmChainId<T>(PhantomData<T>);
impl<T> OnRuntimeUpgrade for SetEvmChainId<T>
where
T: frame_system::Config + parachain_info::Config + pallet_evm_chain_id::Config,
{
fn on_runtime_upgrade() -> Weight {
let para_id: u32 = parachain_info::Pallet::<T>::parachain_id().into();
let evm_id: u64 = para_id.into();
pallet_evm_chain_id::ChainId::<T>::put(evm_id);
<T as frame_system::Config>::DbWeight::get().reads_writes(1, 1)
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<sp_std::vec::Vec<u8>, &'static str> {
Ok(Default::default())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_: sp_std::vec::Vec<u8>) -> Result<(), &'static str> {
Ok(())
}
}
45 changes: 45 additions & 0 deletions parachain/runtime/common/src/evm/precompiles.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use pallet_evm::{IsPrecompileResult, Precompile, PrecompileHandle, PrecompileResult, PrecompileSet};
use sp_core::H160;
use sp_std::marker::PhantomData;

use pallet_evm_precompile_modexp::Modexp;
use pallet_evm_precompile_simple::{ECRecover, Identity, Ripemd160, Sha256};

pub struct InterBtcPrecompiles<R>(PhantomData<R>);

impl<R> InterBtcPrecompiles<R> {
pub fn new() -> Self {
Self(Default::default())
}
pub fn used_addresses() -> [H160; 5] {
[hash(1), hash(2), hash(3), hash(4), hash(5)]
}
}

impl<R> PrecompileSet for InterBtcPrecompiles<R>
where
R: pallet_evm::Config,
{
fn execute(&self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {
match handle.code_address() {
// Ethereum precompiles:
a if a == hash(1) => Some(ECRecover::execute(handle)),
a if a == hash(2) => Some(Sha256::execute(handle)),
a if a == hash(3) => Some(Ripemd160::execute(handle)),
a if a == hash(4) => Some(Identity::execute(handle)),
a if a == hash(5) => Some(Modexp::execute(handle)),
_ => None,
}
}

fn is_precompile(&self, address: H160, _gas: u64) -> IsPrecompileResult {
IsPrecompileResult::Answer {
is_precompile: Self::used_addresses().contains(&address),
extra_cost: 0,
}
}
}

fn hash(a: u64) -> H160 {
H160::from_low_u64_be(a)
}
2 changes: 2 additions & 0 deletions parachain/runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use sp_std::prelude::*;
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;

pub mod evm;

// The relay chain is limited to 12s to include parachain blocks.
pub const MILLISECS_PER_BLOCK: u64 = 12000;
pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);
Expand Down
26 changes: 26 additions & 0 deletions parachain/runtime/interlay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ orml-xtokens = { git = "https://github.com/open-web3-stack/open-runtime-module-l
orml-xcm-support = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "dc39cfddefb10ef0de23655e2c3dcdab66a19404", default-features = false }
orml-unknown-tokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "dc39cfddefb10ef0de23655e2c3dcdab66a19404", default-features = false }

# Frontier dependencies
fp-rpc = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42", default-features = false }
fp-self-contained = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42", default-features = false }
pallet-base-fee = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42", default-features = false }
pallet-ethereum = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42", default-features = false }
pallet-evm = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42", default-features = false }
pallet-evm-chain-id = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42", default-features = false }
pallet-evm-precompile-dispatch = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.42", default-features = false }

[dev-dependencies]
hex = '0.4.2'
mocktopus = "0.8.0"
Expand Down Expand Up @@ -258,6 +267,14 @@ std = [
"orml-xtokens/std",
"orml-xcm-support/std",
"orml-unknown-tokens/std",

"fp-rpc/std",
"fp-self-contained/std",
"pallet-base-fee/std",
"pallet-ethereum/std",
"pallet-evm/std",
"pallet-evm-chain-id/std",
"pallet-evm-precompile-dispatch/std",
]
runtime-benchmarks = [
"frame-benchmarking",
Expand Down Expand Up @@ -307,6 +324,9 @@ runtime-benchmarks = [

"primitives/runtime-benchmarks",
"runtime-common/runtime-benchmarks",

"pallet-ethereum/runtime-benchmarks",
"pallet-evm/runtime-benchmarks",
]
try-runtime = [
"frame-try-runtime",
Expand Down Expand Up @@ -371,4 +391,10 @@ try-runtime = [
"orml-asset-registry/try-runtime",
"orml-xtokens/try-runtime",
"orml-unknown-tokens/try-runtime",

"fp-self-contained/try-runtime",
"pallet-base-fee/try-runtime",
"pallet-ethereum/try-runtime",
"pallet-evm/try-runtime",
"pallet-evm-chain-id/try-runtime",
]
Loading

0 comments on commit 0fccbdd

Please sign in to comment.