-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
- Loading branch information
Showing
23 changed files
with
2,866 additions
and
303 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use crate::{ | ||
AccountId, Aura, BaseFee, EVMChainId, NativeCurrency, Runtime, RuntimeEvent, Timestamp, MAXIMUM_BLOCK_WEIGHT, | ||
NORMAL_DISPATCH_RATIO, | ||
}; | ||
use frame_support::{ | ||
parameter_types, | ||
traits::{ConstU32, FindAuthor}, | ||
weights::Weight, | ||
ConsensusEngineId, | ||
}; | ||
use pallet_ethereum::PostLogContent; | ||
use pallet_evm::{EnsureAddressRoot, EnsureAddressTruncated, FixedGasWeightMapping, HashedAddressMapping}; | ||
use sp_core::{crypto::ByteArray, H160, U256}; | ||
use sp_runtime::{traits::BlakeTwo256, Permill}; | ||
use sp_std::marker::PhantomData; | ||
|
||
pub use runtime_common::evm::{ | ||
precompiles::InterBtcPrecompiles, BaseFeeThreshold, GetTransactionAction, SetEvmChainId, WEIGHT_PER_GAS, | ||
}; | ||
|
||
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 Runtime { | ||
type DefaultBaseFeePerGas = DefaultBaseFeePerGas; | ||
type DefaultElasticity = DefaultElasticity; | ||
type RuntimeEvent = RuntimeEvent; | ||
type Threshold = BaseFeeThreshold; | ||
} | ||
|
||
parameter_types! { | ||
pub const PostBlockAndTxnHashes: PostLogContent = PostLogContent::BlockAndTxnHashes; | ||
} | ||
|
||
impl pallet_ethereum::Config for Runtime { | ||
type RuntimeEvent = RuntimeEvent; | ||
type StateRoot = pallet_ethereum::IntermediateStateRoot<Self>; | ||
type PostLogContent = PostBlockAndTxnHashes; | ||
type ExtraDataLength = ConstU32<30>; | ||
} | ||
|
||
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 | ||
} | ||
} | ||
|
||
parameter_types! { | ||
pub BlockGasLimit: U256 = U256::from(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT.ref_time() / WEIGHT_PER_GAS); | ||
pub PrecompilesValue: InterBtcPrecompiles<Runtime> = InterBtcPrecompiles::<_>::new(); | ||
pub WeightPerGas: Weight = Weight::from_parts(WEIGHT_PER_GAS, 0); | ||
/// The amount of gas per pov, taken from Moonbeam: | ||
/// ceil(MAXIMUM_BLOCK_WEIGHT.ref_time() / MAXIMUM_BLOCK_WEIGHT.proof_size() / WEIGHT_PER_GAS) | ||
pub const GasLimitPovSizeRatio: u64 = 4; | ||
} | ||
|
||
impl pallet_evm::Config for Runtime { | ||
type AddressMapping = HashedAddressMapping<BlakeTwo256>; | ||
type BlockGasLimit = BlockGasLimit; | ||
type BlockHashMapping = pallet_ethereum::EthereumBlockHashMapping<Self>; | ||
type CallOrigin = EnsureAddressRoot<AccountId>; | ||
type WithdrawOrigin = EnsureAddressTruncated; | ||
type ChainId = EVMChainId; | ||
type Currency = NativeCurrency; | ||
type FeeCalculator = BaseFee; | ||
type FindAuthor = FindAuthorTruncated<Aura>; | ||
type GasWeightMapping = FixedGasWeightMapping<Self>; | ||
type OnChargeTransaction = (); | ||
type OnCreate = (); | ||
type PrecompilesType = InterBtcPrecompiles<Self>; | ||
type PrecompilesValue = PrecompilesValue; | ||
type Runner = pallet_evm::runner::stack::Runner<Self>; | ||
type RuntimeEvent = RuntimeEvent; | ||
type WeightPerGas = WeightPerGas; | ||
type GasLimitPovSizeRatio = GasLimitPovSizeRatio; | ||
type Timestamp = Timestamp; | ||
type WeightInfo = pallet_evm::weights::SubstrateWeight<Runtime>; | ||
} | ||
|
||
impl pallet_evm_chain_id::Config for Runtime {} |
Oops, something went wrong.