From 902fcbf4551db7999022d441965fb1c8ef48215f Mon Sep 17 00:00:00 2001 From: Aleksandr Krupenkin Date: Fri, 21 Oct 2022 17:35:22 +0900 Subject: [PATCH] Remove chain-extension-trait package (#109) * Remove chain-extension-trait package * Dapps Staking CE optimizations * Dapps Staking CE optimizations * Rename dapps-staking CE in general way * Derive Default for chain extension structs * Implement Default trait for CEs * Fix typo --- Cargo.toml | 1 - chain-extensions/dapps-staking/Cargo.toml | 7 +- chain-extensions/dapps-staking/src/lib.rs | 91 +++++++------------ chain-extensions/rmrk/Cargo.toml | 2 - chain-extensions/rmrk/src/lib.rs | 31 ++++--- chain-extensions/trait/Cargo.toml | 20 ---- chain-extensions/trait/src/lib.rs | 21 ----- .../types/dapps-staking/Cargo.toml | 2 +- .../types/dapps-staking/src/lib.rs | 10 -- chain-extensions/types/rmrk/src/lib.rs | 4 +- chain-extensions/xvm/Cargo.toml | 3 +- chain-extensions/xvm/src/lib.rs | 26 +++--- 12 files changed, 74 insertions(+), 144 deletions(-) delete mode 100644 chain-extensions/trait/Cargo.toml delete mode 100644 chain-extensions/trait/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index ddb529f6..1cb716d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,6 @@ members = [ "chain-extensions/dapps-staking", "chain-extensions/rmrk", "chain-extensions/xvm", - "chain-extensions/trait", "chain-extensions/types/*", "frame/block-reward", "frame/collator-selection", diff --git a/chain-extensions/dapps-staking/Cargo.toml b/chain-extensions/dapps-staking/Cargo.toml index 1263c608..4a6bd739 100644 --- a/chain-extensions/dapps-staking/Cargo.toml +++ b/chain-extensions/dapps-staking/Cargo.toml @@ -1,6 +1,6 @@ [package] -name = "dapps-staking-chain-extension" -version = "1.0.4" +name = "pallet-chain-extension-dapps-staking" +version = "1.1.0" authors = ["Stake Technologies "] edition = "2021" license = "Apache-2.0" @@ -23,9 +23,8 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkad sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.29", default-features = false } # Astar -chain-extension-trait = { path = "../trait", default-features = false } dapps-staking-chain-extension-types = { path = "../types/dapps-staking", default-features = false } -pallet-dapps-staking = { path = "../../frame/dapps-staking", default-features = false } +pallet-dapps-staking = { git = "https://github.com/AstarNetwork/astar-frame", branch = "polkadot-v0.9.29", default-features = false } [features] default = ["std"] diff --git a/chain-extensions/dapps-staking/src/lib.rs b/chain-extensions/dapps-staking/src/lib.rs index 863db5c8..5e746300 100644 --- a/chain-extensions/dapps-staking/src/lib.rs +++ b/chain-extensions/dapps-staking/src/lib.rs @@ -4,16 +4,15 @@ use sp_runtime::{ DispatchError, }; -use chain_extension_trait::ChainExtensionExec; -use codec::{Decode, Encode}; +use codec::Encode; use dapps_staking_chain_extension_types::{ - Contract, DSError, DappsStakingAccountInput, DappsStakingEraInput, DappsStakingNominationInput, + DSError, DappsStakingAccountInput, DappsStakingEraInput, DappsStakingNominationInput, DappsStakingValueInput, }; use frame_support::traits::{Currency, Get}; use frame_system::RawOrigin; use pallet_contracts::chain_extension::{ - Environment, Ext, InitState, RetVal, SysConfig, UncheckedFrom, + ChainExtension, Environment, Ext, InitState, RetVal, SysConfig, UncheckedFrom, }; use pallet_dapps_staking::{RewardDestination, WeightInfo}; use sp_std::marker::PhantomData; @@ -39,10 +38,10 @@ enum DappsStakingFunc { NominationTransfer, } -impl TryFrom for DappsStakingFunc { +impl TryFrom for DappsStakingFunc { type Error = DispatchError; - fn try_from(value: u32) -> Result { + fn try_from(value: u16) -> Result { match value { 1 => Ok(DappsStakingFunc::CurrentEra), 2 => Ok(DappsStakingFunc::UnbondingPeriod), @@ -65,19 +64,27 @@ impl TryFrom for DappsStakingFunc { } } -pub struct DappsStakingExtension(PhantomData); +/// Dapps Staking chain extension. +pub struct DappsStakingExtension(PhantomData); -impl ChainExtensionExec for DappsStakingExtension { - fn execute_func( - func_id: u32, - env: Environment, - ) -> Result +impl Default for DappsStakingExtension { + fn default() -> Self { + DappsStakingExtension(PhantomData) + } +} + +impl ChainExtension for DappsStakingExtension +where + T: pallet_dapps_staking::Config + pallet_contracts::Config, + ::SmartContract: From<[u8; 32]>, + ::AccountId: From<[u8; 32]>, +{ + fn call(&mut self, env: Environment) -> Result where E: Ext, - ::AccountId: - UncheckedFrom<::Hash> + AsRef<[u8]> + From<[u8; 32]>, + ::AccountId: UncheckedFrom<::Hash> + AsRef<[u8]>, { - let func_id = DappsStakingFunc::try_from(func_id)?; + let func_id = env.func_id().try_into()?; let mut env = env.buf_in_buf_out(); match func_id { @@ -133,8 +140,9 @@ impl ChainExtensionExec for DappsStakingExte DappsStakingFunc::StakedAmountOnContract => { let args: DappsStakingAccountInput = env.read_as()?; - let staker = T::AccountId::decode(&mut args.staker.as_ref()).unwrap(); - let contract = Self::decode_smart_contract(args.contract)?; + let staker: T::AccountId = args.staker.into(); + let contract: ::SmartContract = + args.contract.into(); let base_weight = ::DbWeight::get().reads(1); env.charge_weight(base_weight)?; @@ -147,7 +155,8 @@ impl ChainExtensionExec for DappsStakingExte DappsStakingFunc::ReadContractStake => { let contract_bytes: [u8; 32] = env.read_as()?; - let contract = Self::decode_smart_contract(contract_bytes)?; + let contract: ::SmartContract = + contract_bytes.into(); let base_weight = ::DbWeight::get().reads(1); env.charge_weight(base_weight.saturating_add(base_weight))?; @@ -162,7 +171,7 @@ impl ChainExtensionExec for DappsStakingExte DappsStakingFunc::BondAndStake => { let args: DappsStakingValueInput> = env.read_as()?; - let contract = Self::decode_smart_contract(args.contract)?; + let contract = args.contract.into(); let value: BalanceOf = args.value; let base_weight = ::WeightInfo::bond_and_stake(); @@ -185,7 +194,7 @@ impl ChainExtensionExec for DappsStakingExte DappsStakingFunc::UnbondAndUnstake => { let args: DappsStakingValueInput> = env.read_as()?; - let contract = Self::decode_smart_contract(args.contract)?; + let contract = args.contract.into(); let value: BalanceOf = args.value; let base_weight = @@ -228,10 +237,10 @@ impl ChainExtensionExec for DappsStakingExte DappsStakingFunc::ClaimStaker => { let contract_bytes: [u8; 32] = env.read_as()?; - let contract = Self::decode_smart_contract(contract_bytes)?; + let contract = contract_bytes.into(); - let base_weight = T::WeightInfo::claim_staker_with_restake() - .max(T::WeightInfo::claim_staker_without_restake()); + let base_weight = ::WeightInfo::claim_staker_with_restake() + .max(::WeightInfo::claim_staker_without_restake()); let charged_weight = env.charge_weight(base_weight)?; let caller = env.ext().address().clone(); @@ -259,7 +268,7 @@ impl ChainExtensionExec for DappsStakingExte DappsStakingFunc::ClaimDapp => { let args: DappsStakingEraInput = env.read_as()?; - let contract = Self::decode_smart_contract(args.contract)?; + let contract = args.contract.into(); let era: u32 = args.era; let base_weight = ::WeightInfo::claim_dapp(); @@ -313,8 +322,8 @@ impl ChainExtensionExec for DappsStakingExte DappsStakingFunc::NominationTransfer => { let args: DappsStakingNominationInput> = env.read_as()?; - let origin_smart_contract = Self::decode_smart_contract(args.origin_contract)?; - let target_smart_contract = Self::decode_smart_contract(args.target_contract)?; + let origin_smart_contract = args.origin_contract.into(); + let target_smart_contract = args.target_contract.into(); let value: BalanceOf = args.value; let base_weight = @@ -341,33 +350,3 @@ impl ChainExtensionExec for DappsStakingExte Ok(RetVal::Converging(DSError::Success as u32)) } } - -impl DappsStakingExtension { - /// Helper method to decode type SmartContract enum - pub fn decode_smart_contract( - contract_bytes: [u8; 32], - ) -> Result<::SmartContract, DispatchError> - where - R: pallet_dapps_staking::Config, - R::AccountId: From<[u8; 32]>, - { - let account: R::AccountId = contract_bytes.into(); - // Encode contract address to fit SmartContract enum. - // Since the SmartContract enum type can't be accessed from this chain extension, - // use locally defined enum clone (see Contract enum) - let contract_enum_encoded = Contract::::Wasm(account).encode(); - - // encoded enum will add one byte before the contract's address - // therefore we need to decode len(u32) + 1 byte = 33 - let smart_contract = ::SmartContract::decode( - &mut &contract_enum_encoded[..33], - ) - .map_err(|_| { - DispatchError::Other( - "[ChainExtension] Error while decoding SmartContract in ChainExtension", - ) - })?; - - Ok(smart_contract) - } -} diff --git a/chain-extensions/rmrk/Cargo.toml b/chain-extensions/rmrk/Cargo.toml index 7798919b..0d2b6c6a 100644 --- a/chain-extensions/rmrk/Cargo.toml +++ b/chain-extensions/rmrk/Cargo.toml @@ -24,7 +24,6 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkad sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.29", default-features = false } # RMRK -chain-extension-trait = { path = "../trait", default-features = false } pallet-rmrk-core = { git = "https://github.com/AstarNetwork/rmrk-substrate", branch = "polkadot-v0.9.29", default-features = false } rmrk-chain-extension-types = { path = "../types/rmrk", default-features = false } rmrk-traits = { git = "https://github.com/AstarNetwork/rmrk-substrate", branch = "polkadot-v0.9.29", default-features = false } @@ -33,7 +32,6 @@ rmrk-traits = { git = "https://github.com/AstarNetwork/rmrk-substrate", branch = default = ["std"] std = [ "codec/std", - "chain-extension-trait/std", "frame-support/std", "frame-system/std", "num-traits/std", diff --git a/chain-extensions/rmrk/src/lib.rs b/chain-extensions/rmrk/src/lib.rs index 71f2e5a6..6ccabb21 100644 --- a/chain-extensions/rmrk/src/lib.rs +++ b/chain-extensions/rmrk/src/lib.rs @@ -2,13 +2,12 @@ use sp_runtime::{traits::StaticLookup, DispatchError, Permill}; -use chain_extension_trait::ChainExtensionExec; - use codec::Encode; use frame_support::{log, weights::Weight, BoundedVec}; use frame_system::RawOrigin; use pallet_contracts::chain_extension::{ - Environment, Ext, InitState, RetVal, RetVal::Converging, SysConfig, UncheckedFrom, + ChainExtension, Environment, Ext, InitState, RetVal, RetVal::Converging, SysConfig, + UncheckedFrom, }; use pallet_rmrk_core::{BoundedResourceInfoTypeOf, StringLimitOf}; use rmrk_chain_extension_types::{RmrkError, RmrkFunc}; @@ -18,23 +17,27 @@ use rmrk_traits::{ }; use sp_std::{marker::PhantomData, vec::Vec}; -pub struct RmrkExtension(PhantomData); +/// RMRK chain extension. +pub struct RmrkExtension(PhantomData); -impl< - T: pallet_rmrk_core::Config - + pallet_uniques::Config, - > ChainExtensionExec for RmrkExtension +impl Default for RmrkExtension { + fn default() -> Self { + RmrkExtension(PhantomData) + } +} + +impl ChainExtension for RmrkExtension +where + T: pallet_contracts::Config + + pallet_rmrk_core::Config + + pallet_uniques::Config, { - fn execute_func( - func_id: u32, - env: Environment, - ) -> Result + fn call(&mut self, env: Environment) -> Result where E: Ext, ::AccountId: UncheckedFrom<::Hash> + AsRef<[u8]>, { - let func_id = RmrkFunc::try_from(func_id)?; - + let func_id = env.func_id().try_into()?; match func_id { // READ functions RmrkFunc::CollectionIndex => { diff --git a/chain-extensions/trait/Cargo.toml b/chain-extensions/trait/Cargo.toml deleted file mode 100644 index 95d42b98..00000000 --- a/chain-extensions/trait/Cargo.toml +++ /dev/null @@ -1,20 +0,0 @@ -[package] -name = "chain-extension-trait" -version = "1.0.3" -authors = ["Stake Technologies "] -edition = "2021" -license = "Apache-2.0" -homepage = "https://astar.network" -repository = "https://github.com/AstarNetwork/astar-frame" -description = "Trait definition for chain extension for WASM contracts" - -[dependencies] -pallet-contracts = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.29", default-features = false, features = ["unstable-interface"] } -sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.29", default-features = false } - -[features] -default = ["std"] -std = [ - "pallet-contracts/std", - "sp-runtime/std", -] diff --git a/chain-extensions/trait/src/lib.rs b/chain-extensions/trait/src/lib.rs deleted file mode 100644 index c3ced39f..00000000 --- a/chain-extensions/trait/src/lib.rs +++ /dev/null @@ -1,21 +0,0 @@ -#![cfg_attr(not(feature = "std"), no_std)] -use pallet_contracts::chain_extension::{ - Environment, Ext, InitState, RetVal, SysConfig, UncheckedFrom, -}; -use sp_runtime::DispatchError; - -/// A Trait used to implement a chain-extension for a pallet -/// -/// To create a chain-extension for a pallet this trait must be implemented -/// -/// T is the Config trait of the pallet -pub trait ChainExtensionExec { - fn execute_func( - func_id: u32, - env: Environment, - ) -> Result - where - E: Ext, - ::AccountId: - UncheckedFrom<::Hash> + AsRef<[u8]> + From<[u8; 32]>; -} diff --git a/chain-extensions/types/dapps-staking/Cargo.toml b/chain-extensions/types/dapps-staking/Cargo.toml index 400df664..ebb8befb 100644 --- a/chain-extensions/types/dapps-staking/Cargo.toml +++ b/chain-extensions/types/dapps-staking/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dapps-staking-chain-extension-types" -version = "1.0.2" +version = "1.1.0" authors = ["Stake Technologies "] edition = "2021" license = "Apache-2.0" diff --git a/chain-extensions/types/dapps-staking/src/lib.rs b/chain-extensions/types/dapps-staking/src/lib.rs index 4123e896..3bfc6328 100644 --- a/chain-extensions/types/dapps-staking/src/lib.rs +++ b/chain-extensions/types/dapps-staking/src/lib.rs @@ -1,7 +1,6 @@ #![cfg_attr(not(feature = "std"), no_std)] use codec::{Decode, Encode}; use frame_support::pallet_prelude::MaxEncodedLen; -use sp_core::H160; use sp_runtime::{DispatchError, ModuleError}; #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] @@ -111,15 +110,6 @@ impl TryFrom for DSError { } } -/// This is only used to encode SmartContract enum -#[derive(PartialEq, Eq, Copy, Clone, Encode, Decode, Debug)] -pub enum Contract { - // EVM smart contract instance. - Evm(H160), - // Wasm smart contract instance. Not used in this precompile - Wasm(Account), -} - #[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)] pub struct DappsStakingValueInput { pub contract: [u8; 32], diff --git a/chain-extensions/types/rmrk/src/lib.rs b/chain-extensions/types/rmrk/src/lib.rs index 58cccbce..86129a45 100644 --- a/chain-extensions/types/rmrk/src/lib.rs +++ b/chain-extensions/types/rmrk/src/lib.rs @@ -38,10 +38,10 @@ pub enum RmrkFunc { SetPriority, } -impl TryFrom for RmrkFunc { +impl TryFrom for RmrkFunc { type Error = DispatchError; - fn try_from(value: u32) -> Result { + fn try_from(value: u16) -> Result { return match value { // getters // 0x0001 => Ok(RmrkFunc::NextNftId), diff --git a/chain-extensions/xvm/Cargo.toml b/chain-extensions/xvm/Cargo.toml index 291f52e1..a06ac9b8 100644 --- a/chain-extensions/xvm/Cargo.toml +++ b/chain-extensions/xvm/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "xvm-chain-extension" +name = "pallet-chain-extension-xvm" version = "0.1.0" authors = ["Stake Technologies "] edition = "2021" @@ -23,7 +23,6 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkad sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.29", default-features = false } # Astar -chain-extension-trait = { path = "../trait", default-features = false } pallet-xvm = { path = "../../frame/pallet-xvm", default-features = false } xvm-chain-extension-types = { path = "../types/xvm", default-features = false } diff --git a/chain-extensions/xvm/src/lib.rs b/chain-extensions/xvm/src/lib.rs index c20cd470..5aac13cf 100644 --- a/chain-extensions/xvm/src/lib.rs +++ b/chain-extensions/xvm/src/lib.rs @@ -3,12 +3,12 @@ use frame_support::weights::Weight; use frame_system::RawOrigin; use pallet_contracts::chain_extension::{ - Environment, Ext, InitState, RetVal, SysConfig, UncheckedFrom, + ChainExtension, Environment, Ext, InitState, RetVal, SysConfig, UncheckedFrom, }; use pallet_xvm::XvmContext; use sp_runtime::DispatchError; +use sp_std::marker::PhantomData; -use chain_extension_trait::ChainExtensionExec; use xvm_chain_extension_types::{XvmCallArgs, XvmExecutionResult}; enum XvmFuncId { @@ -16,10 +16,10 @@ enum XvmFuncId { // TODO: expand with other calls too } -impl TryFrom for XvmFuncId { +impl TryFrom for XvmFuncId { type Error = DispatchError; - fn try_from(value: u32) -> Result { + fn try_from(value: u16) -> Result { match value { 1 => Ok(XvmFuncId::XvmCall), _ => Err(DispatchError::Other( @@ -29,21 +29,25 @@ impl TryFrom for XvmFuncId { } } -pub struct XvmExtension; +/// XVM chain extension. +pub struct XvmExtension(PhantomData); -impl ChainExtensionExec for XvmExtension +impl Default for XvmExtension { + fn default() -> Self { + XvmExtension(PhantomData) + } +} + +impl ChainExtension for XvmExtension where T: pallet_contracts::Config + pallet_xvm::Config, { - fn execute_func( - func_id: u32, - env: Environment<'_, '_, E, InitState>, - ) -> Result + fn call(&mut self, env: Environment) -> Result where E: Ext, ::AccountId: UncheckedFrom<::Hash> + AsRef<[u8]>, { - let func_id: XvmFuncId = func_id.try_into()?; + let func_id = env.func_id().try_into()?; let mut env = env.buf_in_buf_out(); match func_id {