Skip to content

Commit

Permalink
Merge commit '92e74df74c32b08249a61b51725708ef2e7b36cf' into xavier/s…
Browse files Browse the repository at this point in the history
…taking-v3
  • Loading branch information
AurevoirXavier committed Oct 23, 2024
2 parents 53bf48a + 92e74df commit eb82927
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 249 deletions.
8 changes: 4 additions & 4 deletions node/src/chain_spec/crab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ pub fn development_config() -> ChainSpec {
// EVM stuff.
"evm": {
"accounts": BTreeMap::from_iter(
<CrabPrecompiles<Runtime>>::used_addresses()
.iter()
Precompiles::set()
.into_iter()
.map(|p| {
(
p.to_owned(),
H160(p),
GenesisAccount {
nonce: Default::default(),
balance: Default::default(),
Expand Down Expand Up @@ -232,7 +232,7 @@ pub fn genesis_config() -> ChainSpec {
// EVM stuff.
"evm": {
"accounts": BTreeMap::from_iter(
<CrabPrecompiles<Runtime>>::used_addresses().iter().map(|p| {
Precompiles::set().iter().map(|p| {
(
p,
GenesisAccount {
Expand Down
10 changes: 5 additions & 5 deletions node/src/chain_spec/darwinia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ pub fn development_config() -> ChainSpec {
// EVM stuff.
"evm": {
"accounts": BTreeMap::from_iter(
<DarwiniaPrecompiles<Runtime>>::used_addresses()
.iter()
Precompiles::set()
.into_iter()
.map(|p| {
(
p.to_owned(),
H160(p),
GenesisAccount {
nonce: Default::default(),
balance: Default::default(),
Expand Down Expand Up @@ -221,9 +221,9 @@ pub fn genesis_config() -> ChainSpec {
// EVM stuff.
"evm": {
"accounts": BTreeMap::from_iter(
<DarwiniaPrecompiles<Runtime>>::used_addresses().iter().map(|p| {
Precompiles::set().into_iter().map(|p| {
(
p,
H160(p),
GenesisAccount {
nonce: Default::default(),
balance: Default::default(),
Expand Down
8 changes: 4 additions & 4 deletions node/src/chain_spec/koi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ pub fn development_config() -> ChainSpec {
// EVM stuff.
"evm": {
"accounts": BTreeMap::from_iter(
<KoiPrecompiles<Runtime>>::used_addresses()
.iter()
Precompiles::set()
.into_iter()
.map(|p| {
(
p.to_owned(),
H160(p),
GenesisAccount {
nonce: Default::default(),
balance: Default::default(),
Expand Down Expand Up @@ -179,7 +179,7 @@ pub fn genesis_config() -> ChainSpec {
// EVM stuff.
"evm": {
"accounts": BTreeMap::from_iter(
<KoiPrecompiles<Runtime>>::used_addresses().iter().map(|p| {
Precompiles::set().iter().map(|p| {
(
p,
GenesisAccount {
Expand Down
5 changes: 3 additions & 2 deletions precompile/assets/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ where
Self(Default::default())
}

pub fn used_addresses() -> [H160; 1] {
pub fn set() -> [H160; 1] {
[addr_of(TEST_ID)]
}
}
Expand All @@ -110,7 +110,7 @@ where

fn is_precompile(&self, address: H160, _gas: u64) -> fp_evm::IsPrecompileResult {
fp_evm::IsPrecompileResult::Answer {
is_precompile: Self::used_addresses().contains(&address),
is_precompile: Self::set().contains(&address),
extra_cost: 0,
}
}
Expand Down Expand Up @@ -214,6 +214,7 @@ impl ExtBuilder {
}
}

// TODO: unify with the one in `darwinia-common-runtime`.
fn addr_of<V>(v: V) -> H160
where
V: Into<u64>,
Expand Down
4 changes: 2 additions & 2 deletions precompile/state-storage/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ where
Self(Default::default())
}

pub fn used_addresses() -> [H160; 1] {
pub fn set() -> [H160; 1] {
[addr(1)]
}
}
Expand All @@ -101,7 +101,7 @@ where

fn is_precompile(&self, address: H160, _gas: u64) -> fp_evm::IsPrecompileResult {
fp_evm::IsPrecompileResult::Answer {
is_precompile: Self::used_addresses().contains(&address),
is_precompile: Self::set().contains(&address),
extra_cost: 0,
}
}
Expand Down
3 changes: 1 addition & 2 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ impl pallet_evm::FeeCalculator for FixedGasPrice {
pub struct AssetIdConverter;
impl darwinia_precompile_assets::AccountToAssetId<AccountId, AssetId> for AssetIdConverter {
fn account_to_asset_id(account_id: AccountId) -> AssetId {
let addr: H160 = account_id.into();
addr.to_low_u64_be()
H160::from(account_id).to_low_u64_be()
}
}
77 changes: 75 additions & 2 deletions runtime/common/src/pallet_config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,76 @@
pub mod precompiles {
pub const ADDR_EC_RECOVER: [u8; 20] = address_of(0x01);
pub const ADDR_SHA256: [u8; 20] = address_of(0x02);
pub const ADDR_RIPEMD160: [u8; 20] = address_of(0x03);
pub const ADDR_IDENTITY: [u8; 20] = address_of(0x04);
pub const ADDR_MODEXP: [u8; 20] = address_of(0x05);
pub const ADDR_BN128_ADD: [u8; 20] = address_of(0x06);
pub const ADDR_BN128_MUL: [u8; 20] = address_of(0x07);
pub const ADDR_BN128_PAIRING: [u8; 20] = address_of(0x08);
pub const ADDR_BLAKE2F: [u8; 20] = address_of(0x09);
pub const ADDR_BLS12381_G1_ADD: [u8; 20] = address_of(0x0c);
pub const ADDR_BLS12381_G1_MUL: [u8; 20] = address_of(0x0d);
pub const ADDR_BLS12381_G1_MULTI_EXP: [u8; 20] = address_of(0x0e);
pub const ADDR_BLS12381_G2_ADD: [u8; 20] = address_of(0x0f);
pub const ADDR_BLS12381_G2_MUL: [u8; 20] = address_of(0x10);
pub const ADDR_BLS12381_G2_MULTI_EXP: [u8; 20] = address_of(0x11);
pub const ADDR_BLS12381_PAIRING: [u8; 20] = address_of(0x12);
pub const ADDR_BLS12381_MAP_G1: [u8; 20] = address_of(0x13);
pub const ADDR_BLS12381_MAP_G2: [u8; 20] = address_of(0x14);
// [0x400, 0x800) for stable precompiles.
pub const ADDR_STATE_STORAGE: [u8; 20] = address_of(0x400);
pub const ADDR_DISPATCH: [u8; 20] = address_of(0x401);
// [0x402, 0x600) for assets precompiles.
pub const ADDR_KTON: [u8; 20] = address_of(0x402);
pub const ADDR_USDT: [u8; 20] = address_of(0x403);
pub const ADDR_PINK: [u8; 20] = address_of(0x404);
pub const ADDR_DOT: [u8; 20] = address_of(0x405);
pub const ADDR_DEPOSIT_DEPRECATED: [u8; 20] = address_of(0x600);
pub const ADDR_STAKING_DEPRECATED: [u8; 20] = address_of(0x601);
pub const ADDR_CONVICTION_VOTING: [u8; 20] = address_of(0x602);
// [0x800..) for the experimental precompiles.
pub const ADDR_EXPERIMENTAL: [u8; 20] = address_of(0x800);

pub const fn address_of(v: u64) -> [u8; 20] {
[
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
((v >> 56) & 0xff) as u8,
((v >> 48) & 0xff) as u8,
((v >> 40) & 0xff) as u8,
((v >> 32) & 0xff) as u8,
((v >> 24) & 0xff) as u8,
((v >> 16) & 0xff) as u8,
((v >> 8) & 0xff) as u8,
(v & 0xff) as u8,
]
}

#[test]
fn address_of_should_work() {
// polkadot-sdk
use sp_core::H160;

fn non_const_address_of(v: u64) -> H160 {
H160::from_low_u64_be(v)
}

for code in 0x01..=0x800 {
assert_eq!(address_of(code), non_const_address_of(code).0);
}
}
}

// darwinia
use dc_primitives::*;
// polkadot-sdk
Expand All @@ -18,13 +91,13 @@ pub const MAXIMUM_BLOCK_WEIGHT: frame_support::weights::Weight =
cumulus_primitives_core::relay_chain::MAX_POV_SIZE as u64,
);

const BLOCK_GAS_LIMIT: u64 = 20_000_000;

#[cfg(not(feature = "runtime-benchmarks"))]
const EXISTENTIAL_DEPOSIT: Balance = 0;
#[cfg(feature = "runtime-benchmarks")]
const EXISTENTIAL_DEPOSIT: Balance = 1;

const BLOCK_GAS_LIMIT: u64 = 20_000_000;

frame_support::parameter_types! {
pub const ExistentialDeposit: Balance = EXISTENTIAL_DEPOSIT;
pub const MaxBalance: Balance = Balance::max_value();
Expand Down
119 changes: 51 additions & 68 deletions runtime/crab/src/pallets/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,106 +18,93 @@

// darwinia
use crate::*;
use pallet_config::precompiles::{self, *};
// frontier
use pallet_evm::{ExitError, IsPrecompileResult, Precompile};
use pallet_evm_precompile_dispatch::DispatchValidateT;
// polkadot-sdk
use frame_support::dispatch::{DispatchClass, GetDispatchInfo, Pays};

frame_support::parameter_types! {
pub PrecompilesValue: CrabPrecompiles<Runtime> = CrabPrecompiles::<_>::new();
pub PrecompilesValue: Precompiles = Precompiles;
}

pub struct CrabPrecompiles<R>(core::marker::PhantomData<R>);
impl<R> CrabPrecompiles<R>
where
R: pallet_evm::Config,
{
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self(Default::default())
}

pub fn used_addresses() -> [H160; 16] {
pub struct Precompiles;
impl Precompiles {
pub fn set() -> [[u8; 20]; 14] {
[
addr(0x01),
addr(0x02),
addr(0x03),
addr(0x04),
addr(0x05),
addr(0x06),
addr(0x07),
addr(0x08),
addr(0x09),
addr(0x400),
addr(0x401),
addr(0x402), // For KTON asset.
addr(0x600),
addr(0x601),
addr(0x602),
addr(0x800),
ADDR_EC_RECOVER,
ADDR_SHA256,
ADDR_RIPEMD160,
ADDR_IDENTITY,
ADDR_MODEXP,
ADDR_BN128_ADD,
ADDR_BN128_MUL,
ADDR_BN128_PAIRING,
ADDR_BLAKE2F,
ADDR_STATE_STORAGE,
ADDR_DISPATCH,
ADDR_KTON,
ADDR_CONVICTION_VOTING,
ADDR_EXPERIMENTAL,
]
}
}
impl<R> pallet_evm::PrecompileSet for CrabPrecompiles<R>
where
R: pallet_evm::Config,
{
impl pallet_evm::PrecompileSet for Precompiles {
fn execute(
&self,
handle: &mut impl pallet_evm::PrecompileHandle,
) -> Option<pallet_evm::PrecompileResult> {
// darwinia
use darwinia_precompile_assets::AccountToAssetId;

let (code_addr, context_addr) = (handle.code_address(), handle.context().address);
let (code_addr, context_addr) = (handle.code_address().0, handle.context().address.0);

// Filter known precompile addresses except Ethereum officials
if Self::used_addresses().contains(&code_addr)
&& code_addr > addr(9)
if Self::set().contains(&code_addr)
&& code_addr > precompiles::address_of(9)
&& code_addr != context_addr
{
return Some(Err(precompile_utils::prelude::revert(
"cannot be called with DELEGATECALL or CALLCODE",
"Cannot be called using `DELEGATECALL` or `CALLCODE`.",
)));
};

match code_addr {
// Ethereum precompiles:
a if a == addr(0x01) => Some(pallet_evm_precompile_simple::ECRecover::execute(handle)),
a if a == addr(0x02) => Some(pallet_evm_precompile_simple::Sha256::execute(handle)),
a if a == addr(0x03) => Some(pallet_evm_precompile_simple::Ripemd160::execute(handle)),
a if a == addr(0x04) => Some(pallet_evm_precompile_simple::Identity::execute(handle)),
a if a == addr(0x05) => Some(pallet_evm_precompile_modexp::Modexp::execute(handle)),
a if a == addr(0x06) => Some(pallet_evm_precompile_bn128::Bn128Add::execute(handle)),
a if a == addr(0x07) => Some(pallet_evm_precompile_bn128::Bn128Mul::execute(handle)),
a if a == addr(0x08) => Some(pallet_evm_precompile_bn128::Bn128Pairing::execute(handle)),
a if a == addr(0x09) => Some(pallet_evm_precompile_blake2::Blake2F::execute(handle)),
// Darwinia precompiles: [0x400, 0x800) for stable precompiles.
a if a == addr(0x400) => Some(<darwinia_precompile_state_storage::StateStorage<
let output = match code_addr {
ADDR_EC_RECOVER => pallet_evm_precompile_simple::ECRecover::execute(handle),
ADDR_SHA256 => pallet_evm_precompile_simple::Sha256::execute(handle),
ADDR_RIPEMD160 => pallet_evm_precompile_simple::Ripemd160::execute(handle),
ADDR_IDENTITY => pallet_evm_precompile_simple::Identity::execute(handle),
ADDR_MODEXP => pallet_evm_precompile_modexp::Modexp::execute(handle),
ADDR_BN128_ADD => pallet_evm_precompile_bn128::Bn128Add::execute(handle),
ADDR_BN128_MUL => pallet_evm_precompile_bn128::Bn128Mul::execute(handle),
ADDR_BN128_PAIRING => pallet_evm_precompile_bn128::Bn128Pairing::execute(handle),
ADDR_BLAKE2F => pallet_evm_precompile_blake2::Blake2F::execute(handle),
ADDR_STATE_STORAGE => <darwinia_precompile_state_storage::StateStorage<
Runtime,
darwinia_precompile_state_storage::StateStorageFilter,
>>::execute(handle)),
a if a == addr(0x401) => Some(<pallet_evm_precompile_dispatch::Dispatch<
>>::execute(handle),
ADDR_DISPATCH => <pallet_evm_precompile_dispatch::Dispatch<
Runtime,
DarwiniaDispatchValidator,
>>::execute(handle)),
// [0x402, 0x600) reserved for assets precompiles.
>>::execute(handle),
a if (0x402..0x600).contains(&AssetIdConverter::account_to_asset_id(a.into())) =>
Some(<darwinia_precompile_assets::ERC20Assets<Runtime, AssetIdConverter>>::execute(
<darwinia_precompile_assets::ERC20Assets<Runtime, AssetIdConverter>>::execute(
handle,
)),
// [0x600, 0x800) reserved for other stable precompiles.
a if a == addr(0x602) =>
Some(<pallet_evm_precompile_conviction_voting::ConvictionVotingPrecompile<Runtime>>::execute(handle)),
// [0x800..) reserved for the experimental precompiles.
a if a == addr(0x800) => Some(Err(precompile_utils::prelude::revert("This precompile is no longer supported."))),
_ => None,
}
),
ADDR_CONVICTION_VOTING =>
<pallet_evm_precompile_conviction_voting::ConvictionVotingPrecompile<Runtime>>::execute(handle),
ADDR_EXPERIMENTAL | ADDR_DEPOSIT_DEPRECATED | ADDR_STAKING_DEPRECATED =>
Err(precompile_utils::prelude::revert("This precompile is not supported.")),
_ => return None,
};

Some(output)
}

fn is_precompile(&self, address: H160, _gas: u64) -> IsPrecompileResult {
IsPrecompileResult::Answer {
is_precompile: Self::used_addresses().contains(&address),
is_precompile: Self::set().contains(&address.0),
extra_cost: 0,
}
}
Expand Down Expand Up @@ -173,10 +160,6 @@ impl DispatchValidateT<AccountId, RuntimeCall> for DarwiniaDispatchValidator {
}
}

fn addr(a: u64) -> H160 {
H160::from_low_u64_be(a)
}

impl pallet_evm::Config for Runtime {
type AddressMapping = pallet_evm::IdentityAddressMapping;
type BlockGasLimit = pallet_config::BlockGasLimit;
Expand All @@ -190,7 +173,7 @@ impl pallet_evm::Config for Runtime {
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type OnChargeTransaction = pallet_evm::EVMFungibleAdapter<Balances, ()>;
type OnCreate = ();
type PrecompilesType = CrabPrecompiles<Self>;
type PrecompilesType = Precompiles;
type PrecompilesValue = PrecompilesValue;
type Runner = pallet_evm::runner::stack::Runner<Self>;
type RuntimeEvent = RuntimeEvent;
Expand Down
Loading

0 comments on commit eb82927

Please sign in to comment.