Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evm part 2 #1349

Merged
merged 21 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ fc-mapping-sync = { git = "https://github.com/PureStake/frontier", default-featu
fc-rpc = { git = "https://github.com/PureStake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.37" }
fc-rpc-core = { git = "https://github.com/PureStake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.37" }
fp-consensus = { git = "https://github.com/PureStake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.37" }
fp-dynamic-fee = { git = "https://github.com/PureStake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.37" }
fp-rpc = { git = "https://github.com/PureStake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.37" }
fp-storage = { git = "https://github.com/PureStake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.37" }

Expand Down Expand Up @@ -189,7 +188,10 @@ std = [
"cumulus-primitives-core/std",
"cumulus-primitives-parachain-inherent/std",
"development-runtime/std",
"fp-dynamic-fee/std",
"fp-consensus/std",
"fp-rpc/std",
"fp-storage/std",
"futures/std",
"frame-benchmarking/std",
"log/std",
"pallet-anchors/std",
Expand All @@ -203,6 +205,7 @@ std = [
"sp-block-builder/std",
"sp-consensus-aura/std",
"sp-core/std",
"sp-io/std",
"sp-keystore/std",
"sp-offchain/std",
"sp-runtime/std",
Expand Down
25 changes: 25 additions & 0 deletions runtime/altair/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ orml-xcm = { git = "https://github.com/open-web3-stack/open-runtime-module-libra
orml-xcm-support = { git = "https://github.com/open-web3-stack/open-runtime-module-library", default-features = false, branch = "polkadot-v0.9.37" }
orml-xtokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", default-features = false, branch = "polkadot-v0.9.37" }

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

# our custom pallets
cfg-primitives = { path = "../../libs/primitives", default-features = false }
cfg-traits = { path = "../../libs/traits", default-features = false }
Expand Down Expand Up @@ -126,6 +135,8 @@ std = [
"cumulus-pallet-xcm/std",
"cumulus-pallet-xcmp-queue/std",
"cumulus-primitives-timestamp/std",
"fp-rpc/std",
"fp-self-contained/std",
"frame-executive/std",
"frame-support/std",
"frame-system-rpc-runtime-api/std",
Expand All @@ -142,6 +153,7 @@ std = [
"pallet-aura/std",
"pallet-authorship/std",
"pallet-balances/std",
"pallet-base-fee/std",
"pallet-block-rewards/std",
"pallet-collator-allowlist/std",
"pallet-collator-selection/std",
Expand All @@ -150,6 +162,10 @@ std = [
"pallet-crowdloan-reward/std",
"pallet-democracy/std",
"pallet-elections-phragmen/std",
"pallet-ethereum/std",
"pallet-evm/std",
"pallet-evm-precompile-dispatch/std",
"pallet-evm-chain-id/std",
"pallet-fees/std",
"pallet-identity/std",
"pallet-interest-accrual/std",
Expand Down Expand Up @@ -220,6 +236,8 @@ runtime-benchmarks = [
"pallet-crowdloan-reward/runtime-benchmarks",
"pallet-democracy/runtime-benchmarks",
"pallet-elections-phragmen/runtime-benchmarks",
"pallet-ethereum/runtime-benchmarks",
"pallet-evm/runtime-benchmarks",
"pallet-fees/runtime-benchmarks",
"pallet-identity/runtime-benchmarks",
"pallet-interest-accrual/runtime-benchmarks",
Expand Down Expand Up @@ -254,6 +272,7 @@ runtime-benchmarks = [
"polkadot-runtime-common/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"xcm-executor/runtime-benchmarks",
"pallet-evm/runtime-benchmarks",
]

try-runtime = [
Expand All @@ -268,6 +287,7 @@ try-runtime = [
"cumulus-pallet-parachain-system/try-runtime",
"cumulus-pallet-xcm/try-runtime",
"cumulus-pallet-xcmp-queue/try-runtime",
"fp-self-contained/try-runtime",
"frame-executive/try-runtime",
"frame-support/try-runtime",
"frame-system/try-runtime",
Expand All @@ -280,6 +300,7 @@ try-runtime = [
"pallet-aura/try-runtime",
"pallet-authorship/try-runtime",
"pallet-balances/try-runtime",
"pallet-base-fee/try-runtime",
"pallet-block-rewards/try-runtime",
"pallet-collator-allowlist/try-runtime",
"pallet-collator-selection/try-runtime",
Expand All @@ -288,6 +309,9 @@ try-runtime = [
"pallet-crowdloan-reward/try-runtime",
"pallet-democracy/try-runtime",
"pallet-elections-phragmen/try-runtime",
"pallet-ethereum/try-runtime",
"pallet-evm/try-runtime",
"pallet-evm-chain-id/try-runtime",
"pallet-fees/try-runtime",
"pallet-identity/try-runtime",
"pallet-interest-accrual/try-runtime",
Expand Down Expand Up @@ -316,6 +340,7 @@ try-runtime = [
"pallet-xcm/try-runtime",
"parachain-info/try-runtime",
"runtime-common/try-runtime",
"sp-runtime/try-runtime",
]

# A feature that should be enabled when the runtime should be build for on-chain
Expand Down
84 changes: 84 additions & 0 deletions runtime/altair/src/evm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2023 Centrifuge Foundation (centrifuge.io).
//
// This file is part of the Centrifuge chain project.
// Centrifuge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version (see http://www.gnu.org/licenses).
// Centrifuge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use cfg_primitives::{MAXIMUM_BLOCK_WEIGHT, NORMAL_DISPATCH_RATIO};
use frame_support::{parameter_types, traits::FindAuthor, weights::Weight, ConsensusEngineId};
use pallet_evm::{EnsureAddressRoot, EnsureAddressTruncated};
use runtime_common::evm::{
precompile::CentrifugePrecompiles, BaseFeeThreshold, ExpandedAddressMapping, WEIGHT_PER_GAS,
};
use sp_core::{crypto::ByteArray, H160, U256};
use sp_runtime::Permill;
use sp_std::marker::PhantomData;

use crate::Aura;

/// To create valid Ethereum-compatible blocks, we need a 20-byte
/// "author" for the block. Since that author is purely informational,
/// we do a simple truncation of the 32-byte Substrate author
pub struct FindAuthorTruncated<F>(PhantomData<F>);
impl<F: FindAuthor<u32>> FindAuthor<H160> for FindAuthorTruncated<F> {
fn find_author<'a, I>(digests: I) -> Option<H160>
where
I: 'a + IntoIterator<Item = (ConsensusEngineId, &'a [u8])>,
{
if let Some(author_index) = F::find_author(digests) {
let authority_id = Aura::authorities()[author_index as usize].clone();
return Some(H160::from_slice(&authority_id.to_raw_vec()[4..24]));
}
None
}
}
Comment on lines +29 to +41
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of runtime-agnostic trait impl could to to runtime-commons and just be referenced in the different runtimes instead of duplicated in all of them 🧹

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it needs to be made generic over the Runtime (that Aura in there is really pallet_aura::Pallet::<Runtime>), but indeed could be made properly runtime agnostic. Worth doing for sure

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, my bad, I didn't spot the Aura bit 🙃 I don't know how to make stuff generic over a runtime when referencing a specific pallet from that runtime tho.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add a Get<Vec<Authorities>> and impl that for Aura in the actual runtime. But feel like this makes it more opaque than actually easier to understand.


parameter_types! {
pub BlockGasLimit: U256 = U256::from(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT.ref_time() / WEIGHT_PER_GAS);
pub PrecompilesValue: CentrifugePrecompiles<crate::Runtime> = CentrifugePrecompiles::<_>::new();
pub WeightPerGas: Weight = Weight::from_ref_time(WEIGHT_PER_GAS);
}

impl pallet_evm::Config for crate::Runtime {
type AddressMapping = ExpandedAddressMapping;
type BlockGasLimit = BlockGasLimit;
type BlockHashMapping = pallet_ethereum::EthereumBlockHashMapping<Self>;
type CallOrigin = EnsureAddressRoot<crate::AccountId>;
type ChainId = crate::EVMChainId;
type Currency = crate::Balances;
type FeeCalculator = crate::BaseFee;
type FindAuthor = FindAuthorTruncated<Aura>;
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type OnChargeTransaction = ();
type PrecompilesType = CentrifugePrecompiles<Self>;
type PrecompilesValue = PrecompilesValue;
type Runner = pallet_evm::runner::stack::Runner<Self>;
type RuntimeEvent = crate::RuntimeEvent;
type WeightPerGas = WeightPerGas;
type WithdrawOrigin = EnsureAddressTruncated;
}

impl pallet_evm_chain_id::Config for crate::Runtime {}

parameter_types! {
pub DefaultBaseFeePerGas: U256 = U256::from(1_000_000_000);
pub DefaultElasticity: Permill = Permill::from_parts(125_000);
}

impl pallet_base_fee::Config for crate::Runtime {
type DefaultBaseFeePerGas = DefaultBaseFeePerGas;
type DefaultElasticity = DefaultElasticity;
type RuntimeEvent = crate::RuntimeEvent;
type Threshold = BaseFeeThreshold;
}

impl pallet_ethereum::Config for crate::Runtime {
type RuntimeEvent = crate::RuntimeEvent;
type StateRoot = pallet_ethereum::IntermediateStateRoot<Self>;
}
Loading