diff --git a/evm-template/Cargo.lock b/evm-template/Cargo.lock index f403103..26a8000 100644 --- a/evm-template/Cargo.lock +++ b/evm-template/Cargo.lock @@ -6411,21 +6411,34 @@ dependencies = [ ] [[package]] -name = "num-bigint" -version = "0.4.4" +name = "num" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0" +checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c165a9ab64cf766f73521c0dd2cfdff64f488b8f0b3e621face3462d3db536d7" dependencies = [ - "autocfg", "num-integer", "num-traits", ] [[package]] name = "num-complex" -version = "0.4.5" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" dependencies = [ "num-traits", ] @@ -6456,12 +6469,22 @@ dependencies = [ ] [[package]] -name = "num-rational" -version = "0.4.1" +name = "num-iter" +version = "0.1.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" dependencies = [ "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ "num-bigint", "num-integer", "num-traits", @@ -6469,9 +6492,9 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.18" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", "libm", @@ -7136,6 +7159,34 @@ dependencies = [ "scale-info", ] +[[package]] +name = "pallet-evm-precompile-modexp" +version = "2.0.0-dev" +source = "git+https://github.com/OpenZeppelin/frontier?branch=polkadot-v1.10.0#17e7b6f09c5a1845ba65563a9f9859cf07c4c635" +dependencies = [ + "fp-evm", + "num", +] + +[[package]] +name = "pallet-evm-precompile-sha3fips" +version = "2.0.0-dev" +source = "git+https://github.com/OpenZeppelin/frontier?branch=polkadot-v1.10.0#17e7b6f09c5a1845ba65563a9f9859cf07c4c635" +dependencies = [ + "fp-evm", + "tiny-keccak", +] + +[[package]] +name = "pallet-evm-precompile-simple" +version = "2.0.0-dev" +source = "git+https://github.com/OpenZeppelin/frontier?branch=polkadot-v1.10.0#17e7b6f09c5a1845ba65563a9f9859cf07c4c635" +dependencies = [ + "fp-evm", + "ripemd", + "sp-io", +] + [[package]] name = "pallet-fast-unstake" version = "27.0.0" @@ -7885,6 +7936,7 @@ dependencies = [ "fc-rpc-core", "fc-storage", "fp-dynamic-fee", + "fp-evm", "fp-rpc", "frame-benchmarking", "frame-benchmarking-cli", @@ -7968,6 +8020,9 @@ dependencies = [ "pallet-ethereum", "pallet-evm", "pallet-evm-chain-id", + "pallet-evm-precompile-modexp", + "pallet-evm-precompile-sha3fips", + "pallet-evm-precompile-simple", "pallet-message-queue", "pallet-multisig", "pallet-preimage", @@ -8077,9 +8132,9 @@ dependencies = [ [[package]] name = "parity-scale-codec" -version = "3.6.9" +version = "3.6.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "881331e34fa842a2fb61cc2db9643a8fedc615e47cfcc52597d1af0db9a7e8fe" +checksum = "306800abfa29c7f16596b5970a588435e3d5b3149683d00c12b699cc19f895ee" dependencies = [ "arrayvec 0.7.4", "bitvec", @@ -8092,11 +8147,11 @@ dependencies = [ [[package]] name = "parity-scale-codec-derive" -version = "3.6.9" +version = "3.6.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be30eaf4b0a9fba5336683b38de57bb86d179a35862ba6bfcf57625d006bde5b" +checksum = "d830939c76d294956402033aee57a6da7b438f2294eb94864c37b0569053a42c" dependencies = [ - "proc-macro-crate 2.0.0", + "proc-macro-crate 3.1.0", "proc-macro2", "quote", "syn 1.0.109", @@ -9677,15 +9732,6 @@ dependencies = [ "toml_edit 0.19.15", ] -[[package]] -name = "proc-macro-crate" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e8366a6159044a37876a2b9817124296703c586a5c92e2c53751fa06d8d43e8" -dependencies = [ - "toml_edit 0.20.7", -] - [[package]] name = "proc-macro-crate" version = "3.1.0" @@ -9838,7 +9884,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19de2de2a00075bf566bee3bd4db014b11587e84184d3f7a791bc17f1a8e9e48" dependencies = [ "anyhow", - "itertools 0.12.1", + "itertools 0.11.0", "proc-macro2", "quote", "syn 2.0.59", @@ -12713,7 +12759,7 @@ dependencies = [ [[package]] name = "sp-crypto-ec-utils" version = "0.10.0" -source = "git+https://github.com/paritytech/polkadot-sdk#115c2477eb287df55107cd95594100ba395ed239" +source = "git+https://github.com/paritytech/polkadot-sdk#4ab078d6754147ce731523292dd1882f8a7b5775" dependencies = [ "ark-bls12-377", "ark-bls12-377-ext", @@ -12775,7 +12821,7 @@ dependencies = [ [[package]] name = "sp-debug-derive" version = "14.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#115c2477eb287df55107cd95594100ba395ed239" +source = "git+https://github.com/paritytech/polkadot-sdk#4ab078d6754147ce731523292dd1882f8a7b5775" dependencies = [ "proc-macro2", "quote", @@ -12795,7 +12841,7 @@ dependencies = [ [[package]] name = "sp-externalities" version = "0.25.0" -source = "git+https://github.com/paritytech/polkadot-sdk#115c2477eb287df55107cd95594100ba395ed239" +source = "git+https://github.com/paritytech/polkadot-sdk#4ab078d6754147ce731523292dd1882f8a7b5775" dependencies = [ "environmental", "parity-scale-codec", @@ -13008,7 +13054,7 @@ dependencies = [ [[package]] name = "sp-runtime-interface" version = "24.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#115c2477eb287df55107cd95594100ba395ed239" +source = "git+https://github.com/paritytech/polkadot-sdk#4ab078d6754147ce731523292dd1882f8a7b5775" dependencies = [ "bytes", "impl-trait-for-tuples", @@ -13040,7 +13086,7 @@ dependencies = [ [[package]] name = "sp-runtime-interface-proc-macro" version = "17.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#115c2477eb287df55107cd95594100ba395ed239" +source = "git+https://github.com/paritytech/polkadot-sdk#4ab078d6754147ce731523292dd1882f8a7b5775" dependencies = [ "Inflector", "expander 2.1.0", @@ -13129,7 +13175,7 @@ source = "git+https://github.com/paritytech/polkadot-sdk?tag=polkadot-v1.10.0#70 [[package]] name = "sp-std" version = "14.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#115c2477eb287df55107cd95594100ba395ed239" +source = "git+https://github.com/paritytech/polkadot-sdk#4ab078d6754147ce731523292dd1882f8a7b5775" [[package]] name = "sp-storage" @@ -13146,7 +13192,7 @@ dependencies = [ [[package]] name = "sp-storage" version = "19.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#115c2477eb287df55107cd95594100ba395ed239" +source = "git+https://github.com/paritytech/polkadot-sdk#4ab078d6754147ce731523292dd1882f8a7b5775" dependencies = [ "impl-serde", "parity-scale-codec", @@ -13181,7 +13227,7 @@ dependencies = [ [[package]] name = "sp-tracing" version = "16.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#115c2477eb287df55107cd95594100ba395ed239" +source = "git+https://github.com/paritytech/polkadot-sdk#4ab078d6754147ce731523292dd1882f8a7b5775" dependencies = [ "parity-scale-codec", "tracing", @@ -13278,7 +13324,7 @@ dependencies = [ [[package]] name = "sp-wasm-interface" version = "20.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#115c2477eb287df55107cd95594100ba395ed239" +source = "git+https://github.com/paritytech/polkadot-sdk#4ab078d6754147ce731523292dd1882f8a7b5775" dependencies = [ "impl-trait-for-tuples", "log", @@ -14191,17 +14237,6 @@ dependencies = [ "winnow 0.5.40", ] -[[package]] -name = "toml_edit" -version = "0.20.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70f427fce4d84c72b5b732388bf4a9f4531b53f74e2887e3ecb2481f68f66d81" -dependencies = [ - "indexmap 2.2.6", - "toml_datetime", - "winnow 0.5.40", -] - [[package]] name = "toml_edit" version = "0.21.1" diff --git a/evm-template/Cargo.toml b/evm-template/Cargo.toml index 916b200..7625fb6 100644 --- a/evm-template/Cargo.toml +++ b/evm-template/Cargo.toml @@ -152,6 +152,9 @@ pallet-evm = { git = "https://github.com/OpenZeppelin/frontier", branch = "polka "forbid-evm-reentrancy", ] } pallet-evm-chain-id = { git = "https://github.com/OpenZeppelin/frontier", branch = "polkadot-v1.10.0", default-features = false } +pallet-evm-precompile-modexp = { git = "https://github.com/OpenZeppelin/frontier", branch = "polkadot-v1.10.0", default-features = false } +pallet-evm-precompile-sha3fips = { git = "https://github.com/OpenZeppelin/frontier", branch = "polkadot-v1.10.0", default-features = false } +pallet-evm-precompile-simple = { git = "https://github.com/OpenZeppelin/frontier", branch = "polkadot-v1.10.0", default-features = false } # Fuzzer substrate-runtime-fuzzer = { git = "https://github.com/srlabs/substrate-runtime-fuzzer.git", default-features = false } diff --git a/evm-template/node/Cargo.toml b/evm-template/node/Cargo.toml index b69d9cb..13f8ebb 100644 --- a/evm-template/node/Cargo.toml +++ b/evm-template/node/Cargo.toml @@ -82,6 +82,7 @@ fc-rpc = { workspace = true } fc-rpc-core = { workspace = true } fc-storage = { workspace = true } fp-dynamic-fee = { workspace = true, features = [ "std" ] } +fp-evm = { workspace = true } fp-rpc = { workspace = true } [build-dependencies] diff --git a/evm-template/node/src/chain_spec.rs b/evm-template/node/src/chain_spec.rs index 02e54a8..8fb4a4e 100644 --- a/evm-template/node/src/chain_spec.rs +++ b/evm-template/node/src/chain_spec.rs @@ -1,12 +1,16 @@ +use std::collections::BTreeMap; + use cumulus_primitives_core::ParaId; +use fp_evm::GenesisAccount; use hex_literal::hex; use parachain_template_runtime::{ - constants::currency::EXISTENTIAL_DEPOSIT, AccountId, AuraId, Signature, + constants::currency::EXISTENTIAL_DEPOSIT, AccountId, AuraId, + OpenZeppelinPrecompiles as Precompiles, Runtime, Signature, }; use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup}; use sc_service::ChainType; use serde::{Deserialize, Serialize}; -use sp_core::{ecdsa, Pair, Public}; +use sp_core::{ecdsa, Pair, Public, H160}; use sp_runtime::traits::{IdentifyAccount, Verify}; /// Specialized `ChainSpec` for the normal parachain runtime. @@ -200,6 +204,24 @@ fn testnet_genesis( "evmChainId": { "chainId": 9999 }, + "evm": { + "accounts": Precompiles::::used_addresses() + .map(|addr| { + ( + addr, + GenesisAccount { + nonce: Default::default(), + balance: Default::default(), + storage: Default::default(), + // bytecode to revert without returning data + // (PUSH1 0x00 PUSH1 0x00 REVERT) + code: vec![0x60, 0x00, 0x60, 0x00, 0xFD], + }, + ) + }) + .into_iter() + .collect::>(), + }, "polkadotXcm": { "safeXcmVersion": Some(SAFE_XCM_VERSION), }, diff --git a/evm-template/runtime/Cargo.toml b/evm-template/runtime/Cargo.toml index 4c1b056..d7f9bd5 100644 --- a/evm-template/runtime/Cargo.toml +++ b/evm-template/runtime/Cargo.toml @@ -91,6 +91,9 @@ pallet-base-fee = { workspace = true } pallet-ethereum = { workspace = true } pallet-evm = { workspace = true } pallet-evm-chain-id = { workspace = true } +pallet-evm-precompile-modexp = { workspace = true } +pallet-evm-precompile-sha3fips = { workspace = true } +pallet-evm-precompile-simple = { workspace = true } [dev-dependencies] sp-io = { workspace = true } diff --git a/evm-template/runtime/src/configs/mod.rs b/evm-template/runtime/src/configs/mod.rs index 82f54f5..2b4c94b 100644 --- a/evm-template/runtime/src/configs/mod.rs +++ b/evm-template/runtime/src/configs/mod.rs @@ -63,10 +63,10 @@ use crate::{ Nonce, }, weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight}, - Aura, Balances, BaseFee, CollatorSelection, EVMChainId, MessageQueue, OriginCaller, PalletInfo, - ParachainSystem, Preimage, Runtime, RuntimeCall, RuntimeEvent, RuntimeFreezeReason, - RuntimeHoldReason, RuntimeOrigin, RuntimeTask, Session, SessionKeys, System, Timestamp, - Treasury, UncheckedExtrinsic, WeightToFee, XcmpQueue, + Aura, Balances, BaseFee, CollatorSelection, EVMChainId, MessageQueue, OpenZeppelinPrecompiles, + OriginCaller, PalletInfo, ParachainSystem, Preimage, Runtime, RuntimeCall, RuntimeEvent, + RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, Session, SessionKeys, + System, Timestamp, Treasury, UncheckedExtrinsic, WeightToFee, XcmpQueue, }; parameter_types! { @@ -604,6 +604,7 @@ impl pallet_ethereum::Config for Runtime { parameter_types! { pub BlockGasLimit: U256 = U256::from(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT.ref_time() / WEIGHT_PER_GAS); pub GasLimitPovSizeRatio: u64 = BlockGasLimit::get().as_u64().saturating_div(MAX_POV_SIZE); + pub PrecompilesValue: OpenZeppelinPrecompiles = OpenZeppelinPrecompiles::<_>::new(); pub WeightPerGas: Weight = Weight::from_parts(WEIGHT_PER_GAS, 0); pub SuicideQuickClearLimit: u32 = 0; } @@ -621,10 +622,8 @@ impl pallet_evm::Config for Runtime { type GasWeightMapping = pallet_evm::FixedGasWeightMapping; type OnChargeTransaction = EVMCurrencyAdapter; type OnCreate = (); - // FIXME: Will be implemented in #11 - type PrecompilesType = (); - // FIXME: Will be implemented in #11 - type PrecompilesValue = (); + type PrecompilesType = OpenZeppelinPrecompiles; + type PrecompilesValue = PrecompilesValue; type Runner = pallet_evm::runner::stack::Runner; type RuntimeEvent = RuntimeEvent; type SuicideQuickClearLimit = SuicideQuickClearLimit; diff --git a/evm-template/runtime/src/lib.rs b/evm-template/runtime/src/lib.rs index 743dcd8..6e3a578 100644 --- a/evm-template/runtime/src/lib.rs +++ b/evm-template/runtime/src/lib.rs @@ -9,6 +9,8 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); pub mod apis; pub mod configs; pub mod constants; +mod precompiles; +pub use precompiles::OpenZeppelinPrecompiles; mod types; mod weights; diff --git a/evm-template/runtime/src/precompiles.rs b/evm-template/runtime/src/precompiles.rs new file mode 100644 index 0000000..24f8199 --- /dev/null +++ b/evm-template/runtime/src/precompiles.rs @@ -0,0 +1,55 @@ +use core::marker::PhantomData; + +use pallet_evm::{ + IsPrecompileResult, Precompile, PrecompileHandle, PrecompileResult, PrecompileSet, +}; +use pallet_evm_precompile_modexp::Modexp; +use pallet_evm_precompile_sha3fips::Sha3FIPS256; +use pallet_evm_precompile_simple::{ECRecover, ECRecoverPublicKey, Identity, Ripemd160, Sha256}; +use sp_core::H160; + +#[derive(Default)] +pub struct OpenZeppelinPrecompiles(PhantomData); + +impl OpenZeppelinPrecompiles +where + R: pallet_evm::Config, +{ + pub fn new() -> Self { + Self(Default::default()) + } + + pub fn used_addresses() -> [H160; 7] { + [hash(1), hash(2), hash(3), hash(4), hash(5), hash(1024), hash(1025)] + } +} +impl PrecompileSet for OpenZeppelinPrecompiles +where + R: pallet_evm::Config, +{ + fn execute(&self, handle: &mut impl PrecompileHandle) -> Option { + 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)), + // Non-Frontier specific nor Ethereum precompiles : + a if a == hash(1024) => Some(Sha3FIPS256::execute(handle)), + a if a == hash(1025) => Some(ECRecoverPublicKey::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) +}