-
Notifications
You must be signed in to change notification settings - Fork 83
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
feat: add frontier evm #1133
Merged
Merged
feat: add frontier evm #1133
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to benchmark this somehow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm actually not sure, but this is where Moonbeam defines it: https://github.com/moonbeam-foundation/moonbeam/blob/c55870bdda5bd40e97daffc27a5e3e1c037c5da9/runtime/moonbeam/src/lib.rs#L365-L369
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK that is used in production and Centrifuge also copied that for their runtime.