Skip to content

Commit

Permalink
Update CE & precompile.
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunxw committed Jul 26, 2023
1 parent 401cf8c commit e410631
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 117 deletions.
5 changes: 0 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions chain-extensions/xvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ sp-std = { workspace = true }

# Astar
astar-primitives = { workspace = true }
pallet-xvm = { workspace = true }
xvm-chain-extension-types = { workspace = true }

[features]
Expand All @@ -40,6 +39,5 @@ std = [
"sp-core/std",
"sp-runtime/std",
# Astar
"pallet-xvm/std",
"astar-primitives/std",
]
12 changes: 6 additions & 6 deletions chain-extensions/xvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,18 @@ impl TryFrom<u16> for XvmFuncId {
}

/// XVM chain extension.
pub struct XvmExtension<T>(PhantomData<T>);
pub struct XvmExtension<T, XC>(PhantomData<(T, XC)>);

impl<T> Default for XvmExtension<T> {
impl<T, XC> Default for XvmExtension<T, XC> {
fn default() -> Self {
XvmExtension(PhantomData)
}
}

impl<T> ChainExtension<T> for XvmExtension<T>
impl<T, XC> ChainExtension<T> for XvmExtension<T, XC>
where
T: pallet_contracts::Config + pallet_xvm::Config,
T: pallet_contracts::Config,
XC: XvmCall<T::AccountId>,
{
fn call<E: Ext>(&mut self, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>
where
Expand Down Expand Up @@ -93,8 +94,7 @@ where
}
}
};
let call_result =
pallet_xvm::Pallet::<T>::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,
Expand Down
11 changes: 2 additions & 9 deletions precompiles/xvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pallet-evm = { workspace = true }

# Astar
astar-primitives = { workspace = true }
pallet-xvm = { workspace = true }

[dev-dependencies]
derive_more = { workspace = true }
Expand All @@ -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 }

Expand All @@ -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",
]
4 changes: 2 additions & 2 deletions precompiles/xvm/evm_sdk/flipper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
14 changes: 8 additions & 6 deletions precompiles/xvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ pub enum Action {
}

/// A precompile that expose XVM related functions.
pub struct XvmPrecompile<T>(PhantomData<T>);
pub struct XvmPrecompile<T, XC>(PhantomData<(T, XC)>);

impl<R> Precompile for XvmPrecompile<R>
impl<R, XC> Precompile for XvmPrecompile<R, XC>
where
R: pallet_evm::Config + pallet_xvm::Config,
R: pallet_evm::Config,
<<R as frame_system::Config>::RuntimeCall as Dispatchable>::RuntimeOrigin:
From<Option<R::AccountId>>,
XC: XvmCall<R::AccountId>,
{
fn execute(handle: &mut impl PrecompileHandle) -> EvmResult<PrecompileOutput> {
log::trace!(target: "xvm-precompile", "In XVM precompile");
Expand All @@ -65,11 +66,12 @@ where
}
}

impl<R> XvmPrecompile<R>
impl<R, XC> XvmPrecompile<R, XC>
where
R: pallet_evm::Config + pallet_xvm::Config,
R: pallet_evm::Config,
<<R as frame_system::Config>::RuntimeCall as Dispatchable>::RuntimeOrigin:
From<Option<R::AccountId>>,
XC: XvmCall<R::AccountId>,
{
fn xvm_call(handle: &mut impl PrecompileHandle) -> EvmResult<PrecompileOutput> {
let mut input = handle.read_input()?;
Expand All @@ -91,7 +93,7 @@ where
let call_input = input.read::<Bytes>()?.0;

let from = R::AddressMapping::into_account_id(handle.context().caller);
match pallet_xvm::Pallet::<R>::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",
Expand Down
115 changes: 42 additions & 73 deletions precompiles/xvm/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -154,12 +154,14 @@ pub struct TestPrecompileSet<R>(PhantomData<R>);

impl<R> PrecompileSet for TestPrecompileSet<R>
where
R: pallet_evm::Config + pallet_xvm::Config,
XvmPrecompile<R>: Precompile,
R: pallet_evm::Config,
XvmPrecompile<R, MockXvmWithArgsCheck>: Precompile,
{
fn execute(&self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {
match handle.code_address() {
a if a == PRECOMPILE_ADDRESS => Some(XvmPrecompile::<R>::execute(handle)),
a if a == PRECOMPILE_ADDRESS => {
Some(XvmPrecompile::<R, MockXvmWithArgsCheck>::execute(handle))
}
_ => None,
}
}
Expand Down Expand Up @@ -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<Self>;
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<Runtime> = 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<Self>; 5];
type WeightPrice = ();
type WeightInfo = pallet_contracts::weights::SubstrateWeight<Self>;
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<true>;
type MaxDebugBufferLen = ConstU32<{ 2 * 1024 * 1024 }>;
}

pub struct HashedAccountMapping;
impl astar_primitives::ethereum_checked::AccountMapping<AccountId> 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<AccountId> for MockXvmWithArgsCheck {
fn call(
_context: Context,
vm_id: VmId,
_source: AccountId,
target: Vec<u8>,
input: Vec<u8>,
) -> 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<Self>;
type AccountMapping = HashedAccountMapping;
type XcmTransactOrigin = pallet_ethereum_checked::EnsureXcmEthereumTx<AccountId>;
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
Expand All @@ -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,
}
);

Expand Down
7 changes: 3 additions & 4 deletions runtime/local/src/chain_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
// You should have received a copy of the GNU General Public License
// along with Astar. If not, see <http://www.gnu.org/licenses/>.

//!
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;

Expand All @@ -32,7 +31,7 @@ impl RegisteredChainExtension<Runtime> for DappsStakingExtension<Runtime> {
const ID: u16 = 00;
}

impl RegisteredChainExtension<Runtime> for XvmExtension<Runtime> {
impl RegisteredChainExtension<Runtime> for XvmExtension<Runtime, Xvm> {
const ID: u16 = 01;
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/local/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ impl pallet_contracts::Config for Runtime {
type WeightInfo = pallet_contracts::weights::SubstrateWeight<Self>;
type ChainExtension = (
DappsStakingExtension<Self>,
XvmExtension<Self>,
XvmExtension<Self, Xvm>,
AssetsExtension<Self, pallet_chain_extension_assets::weights::SubstrateWeight<Self>>,
);
type DeletionQueueDepth = ConstU32<128>;
Expand Down
6 changes: 4 additions & 2 deletions runtime/local/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl<R> PrecompileSet for LocalNetworkPrecompiles<R>
where
Erc20AssetsPrecompileSet<R>: PrecompileSet,
DappsStakingWrapper<R>: Precompile,
XvmPrecompile<R>: Precompile,
XvmPrecompile<R, pallet_xvm::Pallet<R>>: Precompile,
Dispatch<R>: Precompile,
R: pallet_evm::Config
+ pallet_xvm::Config
Expand Down Expand Up @@ -110,7 +110,9 @@ where
// SubstrateEcdsa 0x5003
a if a == hash(20483) => Some(SubstrateEcdsaPrecompile::<R>::execute(handle)),
// Xvm 0x5005
a if a == hash(20485) => Some(XvmPrecompile::<R>::execute(handle)),
a if a == hash(20485) => {
Some(XvmPrecompile::<R, pallet_xvm::Pallet<R>>::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::<R>::new().execute(handle)
Expand Down
7 changes: 3 additions & 4 deletions runtime/shibuya/src/chain_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
// You should have received a copy of the GNU General Public License
// along with Astar. If not, see <http://www.gnu.org/licenses/>.

//!
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;

Expand All @@ -32,7 +31,7 @@ impl RegisteredChainExtension<Runtime> for DappsStakingExtension<Runtime> {
const ID: u16 = 00;
}

impl RegisteredChainExtension<Runtime> for XvmExtension<Runtime> {
impl RegisteredChainExtension<Runtime> for XvmExtension<Runtime, Xvm> {
const ID: u16 = 01;
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/shibuya/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ impl pallet_contracts::Config for Runtime {
type WeightInfo = pallet_contracts::weights::SubstrateWeight<Self>;
type ChainExtension = (
DappsStakingExtension<Self>,
XvmExtension<Self>,
XvmExtension<Self, Xvm>,
AssetsExtension<Self, pallet_chain_extension_assets::weights::SubstrateWeight<Self>>,
);
type DeletionQueueDepth = ConstU32<128>;
Expand Down
Loading

0 comments on commit e410631

Please sign in to comment.