Precompiles (#201)

* Moved PR to monorepo

* Update evm-template/runtime/src/lib.rs

Co-authored-by: Amar Singh <asinghchrony@protonmail.com>

* minimal precompile config

* try fix remaining merge conflicts by accepting all incoming

* into merge again

* clippy fix

* use workspace dep as per suggestion

* try fix

* insert revert bytecode in evm account storage at genesis for precompile addresses

* toml sort

---------

Co-authored-by: Nikita Khateev <nikita.khateev@openzeppelin.com>
This commit is contained in:
Amar Singh
2024-05-31 16:04:14 -04:00
committed by GitHub
parent dc4f0129f1
commit 7b1388b15c
8 changed files with 176 additions and 56 deletions
+7 -8
View File
@@ -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<Runtime> = 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<Self>;
type OnChargeTransaction = EVMCurrencyAdapter<Balances, ()>;
type OnCreate = ();
// FIXME: Will be implemented in #11
type PrecompilesType = ();
// FIXME: Will be implemented in #11
type PrecompilesValue = ();
type PrecompilesType = OpenZeppelinPrecompiles<Self>;
type PrecompilesValue = PrecompilesValue;
type Runner = pallet_evm::runner::stack::Runner<Self>;
type RuntimeEvent = RuntimeEvent;
type SuicideQuickClearLimit = SuicideQuickClearLimit;
+2
View File
@@ -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;
+55
View File
@@ -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<R>(PhantomData<R>);
impl<R> OpenZeppelinPrecompiles<R>
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<R> PrecompileSet for OpenZeppelinPrecompiles<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)),
// 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)
}