Skip to content
This repository was archived by the owner on Aug 24, 2023. It is now read-only.

Commit a5d3b27

Browse files
authored
Merge a61c3bd into d81940b
2 parents d81940b + a61c3bd commit a5d3b27

File tree

4 files changed

+147
-4
lines changed

4 files changed

+147
-4
lines changed

Cargo.lock

Lines changed: 79 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

meta/meta-runtime/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ pallet-dynamic-fee = { default-features = false, git = "https://github.co
4141
pallet-ethereum = { default-features = false, git = "https://github.com/paritytech/frontier.git", branch = "polkadot-v0.9.27" }
4242
pallet-evm = { default-features = false, git = "https://github.com/paritytech/frontier.git", branch = "polkadot-v0.9.27" }
4343
pallet-evm-chain-id = { default-features = false, git = "https://github.com/paritytech/frontier.git", branch = "polkadot-v0.9.27" }
44+
pallet-evm-precompile-modexp = { default-features = false, git = "https://github.com/paritytech/frontier.git", branch = "polkadot-v0.9.27" }
45+
pallet-evm-precompile-sha3fips = { default-features = false, git = "https://github.com/paritytech/frontier.git", branch = "polkadot-v0.9.27" }
46+
pallet-evm-precompile-simple = { default-features = false, git = "https://github.com/paritytech/frontier.git", branch = "polkadot-v0.9.27" }
4447

4548
[build-dependencies]
4649
substrate-wasm-builder = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }
@@ -81,6 +84,9 @@ std = [
8184
"pallet-ethereum/std",
8285
"pallet-evm/std",
8386
"pallet-evm-chain-id/std",
87+
"pallet-evm-precompile-modexp/std",
88+
"pallet-evm-precompile-sha3fips/std",
89+
"pallet-evm-precompile-simple/std",
8490
"pallet-sudo/std",
8591
"pallet-timestamp/std",
8692
"pallet-transaction-payment/std",

meta/meta-runtime/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ use pallet_transaction_payment::CurrencyAdapter;
4848
pub use sp_runtime::BuildStorage;
4949
pub use sp_runtime::{Perbill, Permill};
5050

51+
mod precompiles;
52+
use precompiles::FrontierPrecompiles;
53+
5154
#[cfg(test)]
5255
mod mock;
5356
#[cfg(test)]
@@ -239,6 +242,7 @@ impl GasWeightMapping for FixedGasWeightMapping {
239242

240243
parameter_types! {
241244
pub BlockGasLimit: U256 = U256::from(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT / WEIGHT_PER_GAS);
245+
pub PrecompilesValue: FrontierPrecompiles<Runtime> = FrontierPrecompiles::<_>::new();
242246
}
243247

244248
impl pallet_evm::Config for Runtime {
@@ -251,8 +255,8 @@ impl pallet_evm::Config for Runtime {
251255
type Currency = Balances;
252256
type Event = Event;
253257
type Runner = pallet_evm::runner::stack::Runner<Self>;
254-
type PrecompilesType = ();
255-
type PrecompilesValue = ();
258+
type PrecompilesType = FrontierPrecompiles<Self>;
259+
type PrecompilesValue = PrecompilesValue;
256260
type ChainId = EVMChainId;
257261
type BlockGasLimit = BlockGasLimit;
258262
type OnChargeTransaction = ();

meta/meta-runtime/src/precompiles.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use pallet_evm::{Precompile, PrecompileHandle, PrecompileResult, PrecompileSet};
2+
use sp_core::H160;
3+
use sp_std::marker::PhantomData;
4+
5+
use pallet_evm_precompile_modexp::Modexp;
6+
use pallet_evm_precompile_sha3fips::Sha3FIPS256;
7+
use pallet_evm_precompile_simple::{ECRecover, ECRecoverPublicKey, Identity, Ripemd160, Sha256};
8+
9+
pub struct FrontierPrecompiles<R>(PhantomData<R>);
10+
11+
impl<R> FrontierPrecompiles<R>
12+
where
13+
R: pallet_evm::Config,
14+
{
15+
pub fn new() -> Self {
16+
Self(Default::default())
17+
}
18+
pub fn used_addresses() -> [H160; 7] {
19+
[
20+
hash(1),
21+
hash(2),
22+
hash(3),
23+
hash(4),
24+
hash(5),
25+
hash(1024),
26+
hash(1025),
27+
]
28+
}
29+
}
30+
impl<R> PrecompileSet for FrontierPrecompiles<R>
31+
where
32+
R: pallet_evm::Config,
33+
{
34+
fn execute(&self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {
35+
match handle.code_address() {
36+
// Ethereum precompiles :
37+
a if a == hash(1) => Some(ECRecover::execute(handle)),
38+
a if a == hash(2) => Some(Sha256::execute(handle)),
39+
a if a == hash(3) => Some(Ripemd160::execute(handle)),
40+
a if a == hash(4) => Some(Identity::execute(handle)),
41+
a if a == hash(5) => Some(Modexp::execute(handle)),
42+
// Non-Frontier specific nor Ethereum precompiles :
43+
a if a == hash(1024) => Some(Sha3FIPS256::execute(handle)),
44+
a if a == hash(1025) => Some(ECRecoverPublicKey::execute(handle)),
45+
_ => None,
46+
}
47+
}
48+
49+
fn is_precompile(&self, address: H160) -> bool {
50+
Self::used_addresses().contains(&address)
51+
}
52+
}
53+
54+
fn hash(a: u64) -> H160 {
55+
H160::from_low_u64_be(a)
56+
}

0 commit comments

Comments
 (0)