From e4106316a73cbc2d531f32483cc6c074b404f439 Mon Sep 17 00:00:00 2001 From: Shaun Wang Date: Wed, 26 Jul 2023 17:16:32 +1200 Subject: [PATCH] Update CE & precompile. --- Cargo.lock | 5 -- chain-extensions/xvm/Cargo.toml | 2 - chain-extensions/xvm/src/lib.rs | 12 +-- precompiles/xvm/Cargo.toml | 11 +-- precompiles/xvm/evm_sdk/flipper.sol | 4 +- precompiles/xvm/src/lib.rs | 14 +-- precompiles/xvm/src/mock.rs | 115 +++++++++--------------- runtime/local/src/chain_extensions.rs | 7 +- runtime/local/src/lib.rs | 2 +- runtime/local/src/precompiles.rs | 6 +- runtime/shibuya/src/chain_extensions.rs | 7 +- runtime/shibuya/src/lib.rs | 2 +- runtime/shibuya/src/precompiles.rs | 6 +- 13 files changed, 76 insertions(+), 117 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d3b3762d4a..d7e28f03a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6744,7 +6744,6 @@ dependencies = [ "num-traits", "pallet-contracts", "pallet-contracts-primitives", - "pallet-xvm", "parity-scale-codec", "scale-info", "sp-core", @@ -7304,12 +7303,8 @@ dependencies = [ "num_enum", "pallet-balances", "pallet-contracts", - "pallet-ethereum", - "pallet-ethereum-checked", "pallet-evm", - "pallet-insecure-randomness-collective-flip", "pallet-timestamp", - "pallet-xvm", "parity-scale-codec", "precompile-utils", "scale-info", diff --git a/chain-extensions/xvm/Cargo.toml b/chain-extensions/xvm/Cargo.toml index 333e22d8aa..ef28b90dbd 100644 --- a/chain-extensions/xvm/Cargo.toml +++ b/chain-extensions/xvm/Cargo.toml @@ -23,7 +23,6 @@ sp-std = { workspace = true } # Astar astar-primitives = { workspace = true } -pallet-xvm = { workspace = true } xvm-chain-extension-types = { workspace = true } [features] @@ -40,6 +39,5 @@ std = [ "sp-core/std", "sp-runtime/std", # Astar - "pallet-xvm/std", "astar-primitives/std", ] diff --git a/chain-extensions/xvm/src/lib.rs b/chain-extensions/xvm/src/lib.rs index 2fb0504cab..7f9231ac86 100644 --- a/chain-extensions/xvm/src/lib.rs +++ b/chain-extensions/xvm/src/lib.rs @@ -44,17 +44,18 @@ impl TryFrom for XvmFuncId { } /// XVM chain extension. -pub struct XvmExtension(PhantomData); +pub struct XvmExtension(PhantomData<(T, XC)>); -impl Default for XvmExtension { +impl Default for XvmExtension { fn default() -> Self { XvmExtension(PhantomData) } } -impl ChainExtension for XvmExtension +impl ChainExtension for XvmExtension where - T: pallet_contracts::Config + pallet_xvm::Config, + T: pallet_contracts::Config, + XC: XvmCall, { fn call(&mut self, env: Environment) -> Result where @@ -93,8 +94,7 @@ where } } }; - let call_result = - pallet_xvm::Pallet::::call(xvm_context, vm_id, caller, to, input); + let call_result = XC::call(xvm_context, vm_id, caller, to, input); let actual_weight = match call_result { Ok(ref info) => info.used_weight, diff --git a/precompiles/xvm/Cargo.toml b/precompiles/xvm/Cargo.toml index 70b7fb997f..e88c0d5b38 100644 --- a/precompiles/xvm/Cargo.toml +++ b/precompiles/xvm/Cargo.toml @@ -27,7 +27,6 @@ pallet-evm = { workspace = true } # Astar astar-primitives = { workspace = true } -pallet-xvm = { workspace = true } [dev-dependencies] derive_more = { workspace = true } @@ -37,11 +36,8 @@ serde = { workspace = true } precompile-utils = { workspace = true, features = ["testing"] } -pallet-balances = { workspace = true } -pallet-contracts = { workspace = true } -pallet-ethereum = { workspace = true } -pallet-ethereum-checked = { workspace = true } -pallet-insecure-randomness-collective-flip = { workspace = true } +pallet-balances = { workspace = true, features = ["std"] } +pallet-contracts = { workspace = true, features = ["std"] } pallet-timestamp = { workspace = true } sp-runtime = { workspace = true } @@ -58,8 +54,5 @@ std = [ "sp-std/std", "sp-io/std", "sp-runtime/std", - "pallet-xvm/std", "astar-primitives/std", - "pallet-balances/std", - "pallet-ethereum/std", ] diff --git a/precompiles/xvm/evm_sdk/flipper.sol b/precompiles/xvm/evm_sdk/flipper.sol index a15eaefd9e..fcbfdd3d40 100644 --- a/precompiles/xvm/evm_sdk/flipper.sol +++ b/precompiles/xvm/evm_sdk/flipper.sol @@ -2,7 +2,7 @@ pragma solidity ^0.8.0; interface XVM { function xvm_call( - bytes calldata context, + uint8 calldata vm_id, bytes calldata to, bytes calldata input, ) external; @@ -13,6 +13,6 @@ library Flipper { function flip(bytes to) { bytes input = "0xcafecafe"; - XVM_PRECOMPILE.xvm_call(0x1f00, to, input); + XVM_PRECOMPILE.xvm_call(0x1F, to, input); } } diff --git a/precompiles/xvm/src/lib.rs b/precompiles/xvm/src/lib.rs index 349b5f3c34..231ebf8b0f 100644 --- a/precompiles/xvm/src/lib.rs +++ b/precompiles/xvm/src/lib.rs @@ -43,13 +43,14 @@ pub enum Action { } /// A precompile that expose XVM related functions. -pub struct XvmPrecompile(PhantomData); +pub struct XvmPrecompile(PhantomData<(T, XC)>); -impl Precompile for XvmPrecompile +impl Precompile for XvmPrecompile where - R: pallet_evm::Config + pallet_xvm::Config, + R: pallet_evm::Config, <::RuntimeCall as Dispatchable>::RuntimeOrigin: From>, + XC: XvmCall, { fn execute(handle: &mut impl PrecompileHandle) -> EvmResult { log::trace!(target: "xvm-precompile", "In XVM precompile"); @@ -65,11 +66,12 @@ where } } -impl XvmPrecompile +impl XvmPrecompile where - R: pallet_evm::Config + pallet_xvm::Config, + R: pallet_evm::Config, <::RuntimeCall as Dispatchable>::RuntimeOrigin: From>, + XC: XvmCall, { fn xvm_call(handle: &mut impl PrecompileHandle) -> EvmResult { let mut input = handle.read_input()?; @@ -91,7 +93,7 @@ where let call_input = input.read::()?.0; let from = R::AddressMapping::into_account_id(handle.context().caller); - match pallet_xvm::Pallet::::call(xvm_context, vm_id, from, call_to, call_input) { + match XC::call(xvm_context, vm_id, from, call_to, call_input) { Ok(success) => { log::trace!( target: "xvm-precompile::xvm_call", diff --git a/precompiles/xvm/src/mock.rs b/precompiles/xvm/src/mock.rs index 2179030578..4c9fe078f7 100644 --- a/precompiles/xvm/src/mock.rs +++ b/precompiles/xvm/src/mock.rs @@ -22,9 +22,7 @@ use super::*; use fp_evm::IsPrecompileResult; use frame_support::{ - construct_runtime, parameter_types, - traits::{ConstBool, ConstU32, Everything, Nothing}, - weights::Weight, + construct_runtime, ensure, parameter_types, traits::Everything, weights::Weight, }; use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; @@ -39,6 +37,8 @@ use sp_runtime::{ traits::{BlakeTwo256, IdentityLookup}, }; +use astar_primitives::xvm::{CallError::*, CallErrorWithWeight, CallInfo, XvmCallResult}; + pub type AccountId = TestAccount; pub type Balance = u128; pub type BlockNumber = u64; @@ -154,12 +154,14 @@ pub struct TestPrecompileSet(PhantomData); impl PrecompileSet for TestPrecompileSet where - R: pallet_evm::Config + pallet_xvm::Config, - XvmPrecompile: Precompile, + R: pallet_evm::Config, + XvmPrecompile: Precompile, { fn execute(&self, handle: &mut impl PrecompileHandle) -> Option { match handle.code_address() { - a if a == PRECOMPILE_ADDRESS => Some(XvmPrecompile::::execute(handle)), + a if a == PRECOMPILE_ADDRESS => { + Some(XvmPrecompile::::execute(handle)) + } _ => None, } } @@ -227,71 +229,43 @@ impl pallet_evm::Config for Runtime { type WeightInfo = (); } -impl pallet_ethereum::Config for Runtime { - type RuntimeEvent = RuntimeEvent; - type StateRoot = pallet_ethereum::IntermediateStateRoot; - type PostLogContent = (); - type ExtraDataLength = ConstU32<30>; -} - -impl pallet_insecure_randomness_collective_flip::Config for Runtime {} - -parameter_types! { - pub const DepositPerItem: Balance = 1_000; - pub const DepositPerByte: Balance = 1_000; - pub DeletionWeightLimit: Weight = Weight::from_parts(u64::MAX, u64::MAX); - pub Schedule: pallet_contracts::Schedule = Default::default(); -} - -impl pallet_contracts::Config for Runtime { - type Time = Timestamp; - type Randomness = RandomnessCollectiveFlip; - type Currency = Balances; - type RuntimeEvent = RuntimeEvent; - type RuntimeCall = RuntimeCall; - type CallFilter = Nothing; - type DepositPerItem = DepositPerItem; - type DepositPerByte = DepositPerByte; - type CallStack = [pallet_contracts::Frame; 5]; - type WeightPrice = (); - type WeightInfo = pallet_contracts::weights::SubstrateWeight; - type ChainExtension = (); - type DeletionQueueDepth = ConstU32<128>; - type DeletionWeightLimit = DeletionWeightLimit; - type Schedule = Schedule; - type AddressGenerator = pallet_contracts::DefaultAddressGenerator; - type MaxCodeLen = ConstU32<{ 123 * 1024 }>; - type MaxStorageKeyLen = ConstU32<128>; - type UnsafeUnstableInterface = ConstBool; - type MaxDebugBufferLen = ConstU32<{ 2 * 1024 * 1024 }>; -} - -pub struct HashedAccountMapping; -impl astar_primitives::ethereum_checked::AccountMapping for HashedAccountMapping { - fn into_h160(account_id: AccountId) -> H160 { - let data = (b"evm:", account_id); - return H160::from_slice(&data.using_encoded(sp_io::hashing::blake2_256)[0..20]); +struct MockXvmWithArgsCheck; +impl XvmCall for MockXvmWithArgsCheck { + fn call( + _context: Context, + vm_id: VmId, + _source: AccountId, + target: Vec, + input: Vec, + ) -> XvmCallResult { + ensure!( + vm_id != VmId::Evm, + CallErrorWithWeight { + error: SameVmCallNotAllowed, + used_weight: Weight::zero() + } + ); + ensure!( + target.len() == 20, + CallErrorWithWeight { + error: InvalidTarget, + used_weight: Weight::zero() + } + ); + ensure!( + input.len() <= 1024, + CallErrorWithWeight { + error: InputTooLarge, + used_weight: Weight::zero() + } + ); + Ok(CallInfo { + output: vec![], + used_weight: Weight::zero(), + }) } } -parameter_types! { - pub XvmTxWeightLimit: Weight = Weight::from_parts(u64::MAX, u64::MAX); -} - -impl pallet_ethereum_checked::Config for Runtime { - type ReservedXcmpWeight = XvmTxWeightLimit; - type XvmTxWeightLimit = XvmTxWeightLimit; - type InvalidEvmTransactionError = pallet_ethereum::InvalidTransactionWrapper; - type ValidatedTransaction = pallet_ethereum::ValidatedTransaction; - type AccountMapping = HashedAccountMapping; - type XcmTransactOrigin = pallet_ethereum_checked::EnsureXcmEthereumTx; - type WeightInfo = (); -} - -impl pallet_xvm::Config for Runtime { - type EthereumTransact = EthereumChecked; -} - // Configure a mock runtime to test the pallet. construct_runtime!( pub enum Runtime where @@ -303,11 +277,6 @@ construct_runtime!( Balances: pallet_balances, Evm: pallet_evm, Timestamp: pallet_timestamp, - RandomnessCollectiveFlip: pallet_insecure_randomness_collective_flip, - Contracts: pallet_contracts, - Ethereum: pallet_ethereum, - EthereumChecked: pallet_ethereum_checked, - Xvm: pallet_xvm, } ); diff --git a/runtime/local/src/chain_extensions.rs b/runtime/local/src/chain_extensions.rs index e42df76c44..9bd7609c5f 100644 --- a/runtime/local/src/chain_extensions.rs +++ b/runtime/local/src/chain_extensions.rs @@ -16,10 +16,9 @@ // You should have received a copy of the GNU General Public License // along with Astar. If not, see . -//! -use super::Runtime; +use super::{Runtime, Xvm}; + /// Registered WASM contracts chain extensions. -/// pub use pallet_chain_extension_assets::AssetsExtension; use pallet_contracts::chain_extension::RegisteredChainExtension; @@ -32,7 +31,7 @@ impl RegisteredChainExtension for DappsStakingExtension { const ID: u16 = 00; } -impl RegisteredChainExtension for XvmExtension { +impl RegisteredChainExtension for XvmExtension { const ID: u16 = 01; } diff --git a/runtime/local/src/lib.rs b/runtime/local/src/lib.rs index dc01e57087..0e4ba1ce17 100644 --- a/runtime/local/src/lib.rs +++ b/runtime/local/src/lib.rs @@ -813,7 +813,7 @@ impl pallet_contracts::Config for Runtime { type WeightInfo = pallet_contracts::weights::SubstrateWeight; type ChainExtension = ( DappsStakingExtension, - XvmExtension, + XvmExtension, AssetsExtension>, ); type DeletionQueueDepth = ConstU32<128>; diff --git a/runtime/local/src/precompiles.rs b/runtime/local/src/precompiles.rs index 6eddf6629e..9e0bf989d9 100644 --- a/runtime/local/src/precompiles.rs +++ b/runtime/local/src/precompiles.rs @@ -67,7 +67,7 @@ impl PrecompileSet for LocalNetworkPrecompiles where Erc20AssetsPrecompileSet: PrecompileSet, DappsStakingWrapper: Precompile, - XvmPrecompile: Precompile, + XvmPrecompile>: Precompile, Dispatch: Precompile, R: pallet_evm::Config + pallet_xvm::Config @@ -110,7 +110,9 @@ where // SubstrateEcdsa 0x5003 a if a == hash(20483) => Some(SubstrateEcdsaPrecompile::::execute(handle)), // Xvm 0x5005 - a if a == hash(20485) => Some(XvmPrecompile::::execute(handle)), + a if a == hash(20485) => { + Some(XvmPrecompile::>::execute(handle)) + } // If the address matches asset prefix, the we route through the asset precompile set a if &a.to_fixed_bytes()[0..4] == ASSET_PRECOMPILE_ADDRESS_PREFIX => { Erc20AssetsPrecompileSet::::new().execute(handle) diff --git a/runtime/shibuya/src/chain_extensions.rs b/runtime/shibuya/src/chain_extensions.rs index e42df76c44..9bd7609c5f 100644 --- a/runtime/shibuya/src/chain_extensions.rs +++ b/runtime/shibuya/src/chain_extensions.rs @@ -16,10 +16,9 @@ // You should have received a copy of the GNU General Public License // along with Astar. If not, see . -//! -use super::Runtime; +use super::{Runtime, Xvm}; + /// Registered WASM contracts chain extensions. -/// pub use pallet_chain_extension_assets::AssetsExtension; use pallet_contracts::chain_extension::RegisteredChainExtension; @@ -32,7 +31,7 @@ impl RegisteredChainExtension for DappsStakingExtension { const ID: u16 = 00; } -impl RegisteredChainExtension for XvmExtension { +impl RegisteredChainExtension for XvmExtension { const ID: u16 = 01; } diff --git a/runtime/shibuya/src/lib.rs b/runtime/shibuya/src/lib.rs index 8eb90e2972..16ae014b08 100644 --- a/runtime/shibuya/src/lib.rs +++ b/runtime/shibuya/src/lib.rs @@ -678,7 +678,7 @@ impl pallet_contracts::Config for Runtime { type WeightInfo = pallet_contracts::weights::SubstrateWeight; type ChainExtension = ( DappsStakingExtension, - XvmExtension, + XvmExtension, AssetsExtension>, ); type DeletionQueueDepth = ConstU32<128>; diff --git a/runtime/shibuya/src/precompiles.rs b/runtime/shibuya/src/precompiles.rs index bd8d1714c7..d84839aa4b 100644 --- a/runtime/shibuya/src/precompiles.rs +++ b/runtime/shibuya/src/precompiles.rs @@ -73,7 +73,7 @@ where Erc20AssetsPrecompileSet: PrecompileSet, DappsStakingWrapper: Precompile, XcmPrecompile: Precompile, - XvmPrecompile: Precompile, + XvmPrecompile>: Precompile, Dispatch: Precompile, R: pallet_evm::Config + pallet_assets::Config @@ -120,7 +120,9 @@ where // Xcm 0x5004 a if a == hash(20484) => Some(XcmPrecompile::::execute(handle)), // Xvm 0x5005 - a if a == hash(20485) => Some(XvmPrecompile::::execute(handle)), + a if a == hash(20485) => { + Some(XvmPrecompile::>::execute(handle)) + } // If the address matches asset prefix, the we route through the asset precompile set a if &a.to_fixed_bytes()[0..4] == ASSET_PRECOMPILE_ADDRESS_PREFIX => { Erc20AssetsPrecompileSet::::new().execute(handle)