difftreelog
feat(weight) added benchs for decompose weight Changed weight calculation system for `transfer` & `transfer_from` (NFT)
in: master
13 files changed
Cargo.lockdiffbeforeafterboth626262626263[[package]]6263[[package]]6264name = "pallet-common"6264name = "pallet-common"6265version = "0.1.13"6265version = "0.1.14"6266dependencies = [6266dependencies = [6267 "ethereum",6267 "ethereum",6268 "evm-coder",6268 "evm-coder",681468146815[[package]]6815[[package]]6816name = "pallet-nonfungible"6816name = "pallet-nonfungible"6817version = "0.1.13"6817version = "0.1.14"6818dependencies = [6818dependencies = [6819 "evm-coder",6819 "evm-coder",6820 "frame-benchmarking",6820 "frame-benchmarking",pallets/app-promotion/src/lib.rsdiffbeforeafterboth845 /// - `staker`: staker account.845 /// - `staker`: staker account.846 pub fn total_staked_by_id(staker: impl EncodeLike<T::AccountId>) -> Option<BalanceOf<T>> {846 pub fn total_staked_by_id(staker: impl EncodeLike<T::AccountId>) -> Option<BalanceOf<T>> {847 let staked = Staked::<T>::iter_prefix((staker,))847 let staked = Staked::<T>::iter_prefix((staker,))848 .into_iter()849 .fold(<BalanceOf<T>>::default(), |acc, (_, (amount, _))| {848 .fold(<BalanceOf<T>>::default(), |acc, (_, (amount, _))| {850 acc + amount849 acc + amount851 });850 });864 staker: impl EncodeLike<T::AccountId>,863 staker: impl EncodeLike<T::AccountId>,865 ) -> Option<Vec<(T::BlockNumber, BalanceOf<T>)>> {864 ) -> Option<Vec<(T::BlockNumber, BalanceOf<T>)>> {866 let mut staked = Staked::<T>::iter_prefix((staker,))865 let mut staked = Staked::<T>::iter_prefix((staker,))867 .into_iter()868 .map(|(block, (amount, _))| (block, amount))866 .map(|(block, (amount, _))| (block, amount))869 .collect::<Vec<_>>();867 .collect::<Vec<_>>();870 staked.sort_by_key(|(block, _)| *block);868 staked.sort_by_key(|(block, _)| *block);884 })882 })885 }883 }886887 // pub fn cross_id_locked_balance(staker: T::CrossAccountId) -> BalanceOf<T> {888 // Self::get_locked_balance(staker.as_sub())889 // .map(|l| l.amount)890 // .unwrap_or_default()891 // }892884893 /// Returns all relay block numbers when stake was made,885 /// Returns all relay block numbers when stake was made,894 /// the amount of the stake.886 /// the amount of the stake.pallets/common/CHANGELOG.mddiffbeforeafterboth445<!-- bureaucrate goes here -->5<!-- bureaucrate goes here -->667## [0.1.14] - 2023-03-2889### Added1011- Added benchmark to check if user is contained in AllowList (`check_accesslist()`).127## [0.1.13] - 2023-01-2013## [0.1.13] - 2023-01-208149### Changed15### Changedpallets/common/Cargo.tomldiffbeforeafterboth2edition = "2021"2edition = "2021"3license = "GPLv3"3license = "GPLv3"4name = "pallet-common"4name = "pallet-common"5version = "0.1.13"5version = "0.1.14"667[dependencies]7[dependencies]8# Note: `package = "parity-scale-codec"` must be supplied since the `Encode` macro searches for it.8# Note: `package = "parity-scale-codec"` must be supplied since the `Encode` macro searches for it.pallets/common/src/benchmarking.rsdiffbeforeafterboth22use frame_benchmarking::{benchmarks, account};22use frame_benchmarking::{benchmarks, account};23use up_data_structs::{23use up_data_structs::{24 CollectionMode, CollectionFlags, CreateCollectionData, CollectionId, Property, PropertyKey,24 CollectionMode, CollectionFlags, CreateCollectionData, CollectionId, Property, PropertyKey,25 PropertyValue, CollectionPermissions, NestingPermissions, MAX_COLLECTION_NAME_LENGTH,25 PropertyValue, CollectionPermissions, NestingPermissions, AccessMode,26 MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH, MAX_PROPERTIES_PER_ITEM,26 MAX_COLLECTION_NAME_LENGTH, MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH,27 MAX_PROPERTIES_PER_ITEM,27};28};194 let to_delete = (0..b).map(|p| property_key(p as usize)).collect::<Vec<_>>();195 let to_delete = (0..b).map(|p| property_key(p as usize)).collect::<Vec<_>>();195 }: {<Pallet<T>>::delete_collection_properties(&collection, &owner, to_delete.into_iter())?}196 }: {<Pallet<T>>::delete_collection_properties(&collection, &owner, to_delete.into_iter())?}197198 check_accesslist{199 bench_init!{200 owner: sub; collection: collection(owner);201 sender: cross_from_sub(owner); receiver: cross_sub;202 };203204 let mut collection_handle = <CollectionHandle<T>>::try_get(collection.id)?;205 <Pallet<T>>::update_permissions(206 &sender,207 &mut collection_handle,208 CollectionPermissions { access: Some(AccessMode::AllowList), ..Default::default() }209 )?;210211 <Pallet<T>>::toggle_allowlist(212 &collection,213 &sender,214 &sender,215 true,216 )?;217218 <Pallet<T>>::toggle_allowlist(219 &collection,220 &sender,221 &receiver,222 true,223 )?;224225 assert_eq!(collection_handle.permissions.access(), AccessMode::AllowList);226227 collection_handle.check_allowlist(&sender)?;228 collection_handle.check_allowlist(&receiver)?;229230 }: {collection_handle.check_allowlist(&sender)?;}196}231}197232pallets/common/src/weights.rsdiffbeforeafterboth36pub trait WeightInfo {36pub trait WeightInfo {37 fn set_collection_properties(b: u32, ) -> Weight;37 fn set_collection_properties(b: u32, ) -> Weight;38 fn delete_collection_properties(b: u32, ) -> Weight;38 fn delete_collection_properties(b: u32, ) -> Weight;39 fn check_accesslist() -> Weight;39}40}404141/// Weights for pallet_common using the Substrate node and recommended hardware.42/// Weights for pallet_common using the Substrate node and recommended hardware.66 Weight::from_parts(6_684_000, 43467)67 Weight::from_parts(6_684_000, 43467)67 // Standard Error: 67_98468 // Standard Error: 67_98468 .saturating_add(Weight::from_parts(21_502_829, 0).saturating_mul(b.into()))69 .saturating_add(Weight::from_parts(21_502_829, 0).saturating_mul(b.into()))69 .saturating_add(T::DbWeight::get().reads(1_u64))70 .saturating_add(T::DbWeight::get().writes(1_u64))70 .saturating_add(T::DbWeight::get().writes(1_u64))71 }71 }72}7374// For backwards compatibility and tests72// For backwards compatibility and tests75impl WeightInfo for () {73impl WeightInfo for () {76 /// Storage: Common CollectionProperties (r:1 w:1)74 /// Storage: Common CollectionProperties (r:1 w:1)98 Weight::from_parts(6_684_000, 43467)96 Weight::from_parts(6_684_000, 43467)99 // Standard Error: 67_98497 // Standard Error: 67_984100 .saturating_add(Weight::from_parts(21_502_829, 0).saturating_mul(b.into()))98 .saturating_add(Weight::from_parts(21_502_829, 0).saturating_mul(b.into()))101 .saturating_add(RocksDbWeight::get().reads(1_u64))102 .saturating_add(RocksDbWeight::get().writes(1_u64))99 .saturating_add(RocksDbWeight::get().writes(1_u64))103 }100 }104}105106101pallets/nonfungible/CHANGELOG.mddiffbeforeafterboth445<!-- bureaucrate goes here -->5<!-- bureaucrate goes here -->667## [0.1.14] - 2023-03-2889### Fixed1011- The weight of `transfer` and `transfer_from`.127## [0.1.13] - 2023-01-2013## [0.1.13] - 2023-01-208149### Fixed15### Fixedpallets/nonfungible/Cargo.tomldiffbeforeafterboth2edition = "2021"2edition = "2021"3license = "GPLv3"3license = "GPLv3"4name = "pallet-nonfungible"4name = "pallet-nonfungible"5version = "0.1.13"5version = "0.1.14"667[dependencies]7[dependencies]8# Note: `package = "parity-scale-codec"` must be supplied since the `Encode` macro searches for it.8# Note: `package = "parity-scale-codec"` must be supplied since the `Encode` macro searches for it.pallets/nonfungible/src/benchmarking.rsdiffbeforeafterboth146 let item = create_max_item(&collection, &owner, owner_eth.clone())?;146 let item = create_max_item(&collection, &owner, owner_eth.clone())?;147 }: {<Pallet<T>>::set_allowance_from(&collection, &sender, &owner_eth, item, Some(&spender))?}147 }: {<Pallet<T>>::set_allowance_from(&collection, &sender, &owner_eth, item, Some(&spender))?}148148149 transfer_from {149 checks_for_transfer_from {150 bench_init!{150 bench_init!{151 owner: sub; collection: collection(owner);151 owner: sub; collection: collection(owner);152 owner: cross_from_sub; sender: cross_sub; spender: cross_sub; receiver: cross_sub;152 owner: cross_from_sub; sender: cross_sub; spender: cross_sub; receiver: cross_sub;153 };153 };154 let item = create_max_item(&collection, &owner, sender.clone())?;154 let item = create_max_item(&collection, &owner, sender.clone())?;155 <Pallet<T>>::set_allowance(&collection, &sender, item, Some(&spender))?;155 <Pallet<T>>::set_allowance(&collection, &sender, item, Some(&spender))?;156 }: {<Pallet<T>>::transfer_from(&collection, &spender, &sender, &receiver, item, &Unlimited)?}156 }: {<Pallet<T>>::check_allowed(&collection, &spender, &sender, item, &Unlimited)?}157157158 burn_from {158 burn_from {159 bench_init!{159 bench_init!{pallets/nonfungible/src/common.rsdiffbeforeafterboth23};23};24use pallet_common::{24use pallet_common::{25 CommonCollectionOperations, CommonWeightInfo, RefungibleExtensions, with_weight,25 CommonCollectionOperations, CommonWeightInfo, RefungibleExtensions, with_weight,26 weights::WeightInfo as _,26 weights::WeightInfo as _, SelfWeightOf as PalletCommonWeightOf,27};27};28use sp_runtime::DispatchError;28use sp_runtime::DispatchError;29use sp_std::{vec::Vec, vec};29use sp_std::{vec::Vec, vec};91 }91 }929293 fn transfer() -> Weight {93 fn transfer() -> Weight {94 <SelfWeightOf<T>>::transfer()94 <SelfWeightOf<T>>::transfer() + <PalletCommonWeightOf<T>>::check_accesslist() * 295 }95 }969697 fn approve() -> Weight {97 fn approve() -> Weight {103 }103 }104104105 fn transfer_from() -> Weight {105 fn transfer_from() -> Weight {106 <SelfWeightOf<T>>::transfer_from()106 Self::transfer() + <SelfWeightOf<T>>::checks_for_transfer_from()107 }107 }108108109 fn burn_from() -> Weight {109 fn burn_from() -> Weight {325 ) -> DispatchResultWithPostInfo {325 ) -> DispatchResultWithPostInfo {326 ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);326 ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);327 if amount == 1 {327 if amount == 1 {328 with_weight(329 <Pallet<T>>::transfer(self, &from, &to, token, nesting_budget),328 <Pallet<T>>::transfer(self, &from, &to, token, nesting_budget)330 <CommonWeights<T>>::transfer(),331 )332 } else {329 } else {333 <Pallet<T>>::check_token_immediate_ownership(self, token, &from)?;330 <Pallet<T>>::check_token_immediate_ownership(self, token, &from)?;334 Ok(().into())331 Ok(().into())386 ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);383 ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);387384388 if amount == 1 {385 if amount == 1 {389 with_weight(390 <Pallet<T>>::transfer_from(self, &sender, &from, &to, token, nesting_budget),386 <Pallet<T>>::transfer_from(self, &sender, &from, &to, token, nesting_budget)391 <CommonWeights<T>>::transfer_from(),392 )393 } else {387 } else {394 <Pallet<T>>::check_allowed(self, &sender, &from, token, nesting_budget)?;388 <Pallet<T>>::check_allowed(self, &sender, &from, token, nesting_budget)?;395389pallets/nonfungible/src/erc.rsdiffbeforeafterboth39 CollectionHandle, CollectionPropertyPermissions, CommonCollectionOperations,39 CollectionHandle, CollectionPropertyPermissions, CommonCollectionOperations,40 erc::{CommonEvmHandler, PrecompileResult, CollectionCall, static_property::key},40 erc::{CommonEvmHandler, PrecompileResult, CollectionCall, static_property::key},41 eth::{self, TokenUri},41 eth::{self, TokenUri},42 CommonWeightInfo,42};43};43use pallet_evm::{account::CrossAccountId, PrecompileHandle};44use pallet_evm::{account::CrossAccountId, PrecompileHandle};44use pallet_evm_coder_substrate::call;45use pallet_evm_coder_substrate::call;474848use crate::{49use crate::{49 AccountBalance, Config, CreateItemData, NonfungibleHandle, Pallet, TokenData, TokensMinted,50 AccountBalance, Config, CreateItemData, NonfungibleHandle, Pallet, TokenData, TokensMinted,50 TokenProperties, SelfWeightOf, weights::WeightInfo,51 TokenProperties, SelfWeightOf, weights::WeightInfo, common::CommonWeights,51};52};525353/// Nft events.54/// Nft events.458 /// @param from The current owner of the NFT459 /// @param from The current owner of the NFT459 /// @param to The new owner460 /// @param to The new owner460 /// @param tokenId The NFT to transfer461 /// @param tokenId The NFT to transfer461 #[weight(<SelfWeightOf<T>>::transfer_from())]462 #[weight(<CommonWeights<T>>::transfer_from())]462 fn transfer_from(463 fn transfer_from(463 &mut self,464 &mut self,464 caller: Caller,465 caller: Caller,475 .weight_calls_budget(<StructureWeight<T>>::find_parent());476 .weight_calls_budget(<StructureWeight<T>>::find_parent());476477477 <Pallet<T>>::transfer_from(self, &caller, &from, &to, token, &budget)478 <Pallet<T>>::transfer_from(self, &caller, &from, &to, token, &budget)478 .map_err(dispatch_to_evm::<T>)?;479 .map_err(|e| dispatch_to_evm::<T>(e.error))?;479 Ok(())480 Ok(())480 }481 }481482824 /// is the zero address. Throws if `tokenId` is not a valid NFT.825 /// is the zero address. Throws if `tokenId` is not a valid NFT.825 /// @param to The new owner826 /// @param to The new owner826 /// @param tokenId The NFT to transfer827 /// @param tokenId The NFT to transfer827 #[weight(<SelfWeightOf<T>>::transfer())]828 #[weight(<CommonWeights<T>>::transfer())]828 fn transfer(&mut self, caller: Caller, to: Address, token_id: U256) -> Result<()> {829 fn transfer(&mut self, caller: Caller, to: Address, token_id: U256) -> Result<()> {829 let caller = T::CrossAccountId::from_eth(caller);830 let caller = T::CrossAccountId::from_eth(caller);830 let to = T::CrossAccountId::from_eth(to);831 let to = T::CrossAccountId::from_eth(to);834 .weight_calls_budget(<StructureWeight<T>>::find_parent());835 .weight_calls_budget(<StructureWeight<T>>::find_parent());835836836 <Pallet<T>>::transfer(self, &caller, &to, token, &budget).map_err(dispatch_to_evm::<T>)?;837 <Pallet<T>>::transfer(self, &caller, &to, token, &budget)838 .map_err(|e| dispatch_to_evm::<T>(e.error))?;837 Ok(())839 Ok(())838 }840 }839841842 /// is the zero address. Throws if `tokenId` is not a valid NFT.844 /// is the zero address. Throws if `tokenId` is not a valid NFT.843 /// @param to The new owner845 /// @param to The new owner844 /// @param tokenId The NFT to transfer846 /// @param tokenId The NFT to transfer845 #[weight(<SelfWeightOf<T>>::transfer())]847 #[weight(<CommonWeights<T>>::transfer())]846 fn transfer_cross(848 fn transfer_cross(847 &mut self,849 &mut self,848 caller: Caller,850 caller: Caller,857 .weight_calls_budget(<StructureWeight<T>>::find_parent());859 .weight_calls_budget(<StructureWeight<T>>::find_parent());858860859 <Pallet<T>>::transfer(self, &caller, &to, token, &budget).map_err(dispatch_to_evm::<T>)?;861 <Pallet<T>>::transfer(self, &caller, &to, token, &budget)862 .map_err(|e| dispatch_to_evm::<T>(e.error))?;860 Ok(())863 Ok(())861 }864 }862865866 /// @param from Cross acccount address of current owner869 /// @param from Cross acccount address of current owner867 /// @param to Cross acccount address of new owner870 /// @param to Cross acccount address of new owner868 /// @param tokenId The NFT to transfer871 /// @param tokenId The NFT to transfer869 #[weight(<SelfWeightOf<T>>::transfer())]872 #[weight(<CommonWeights<T>>::transfer_from())]870 fn transfer_from_cross(873 fn transfer_from_cross(871 &mut self,874 &mut self,872 caller: Caller,875 caller: Caller,882 .recorder885 .recorder883 .weight_calls_budget(<StructureWeight<T>>::find_parent());886 .weight_calls_budget(<StructureWeight<T>>::find_parent());884 Pallet::<T>::transfer_from(self, &caller, &from, &to, token_id, &budget)887 Pallet::<T>::transfer_from(self, &caller, &from, &to, token_id, &budget)885 .map_err(dispatch_to_evm::<T>)?;888 .map_err(|e| dispatch_to_evm::<T>(e.error))?;886 Ok(())889 Ok(())887 }890 }888891pallets/nonfungible/src/lib.rsdiffbeforeafterboth108use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};108use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};109use pallet_common::{109use pallet_common::{110 Error as CommonError, Pallet as PalletCommon, Event as CommonEvent, CollectionHandle,110 Error as CommonError, Pallet as PalletCommon, Event as CommonEvent, CollectionHandle,111 eth::collection_id_to_address,111 eth::collection_id_to_address, SelfWeightOf as PalletCommonWeightOf,112 weights::WeightInfo as CommonWeightInfo,112};113};113use pallet_structure::{Pallet as PalletStructure, Error as StructureError};114use pallet_structure::{Pallet as PalletStructure, Error as StructureError};114use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};115use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};802 to: &T::CrossAccountId,803 to: &T::CrossAccountId,803 token: TokenId,804 token: TokenId,804 nesting_budget: &dyn Budget,805 nesting_budget: &dyn Budget,805 ) -> DispatchResult {806 ) -> DispatchResultWithPostInfo {806 ensure!(807 ensure!(807 collection.limits.transfers_enabled(),808 collection.limits.transfers_enabled(),808 <CommonError<T>>::TransferNotAllowed809 <CommonError<T>>::TransferNotAllowed812 <TokenData<T>>::get((collection.id, token)).ok_or(<CommonError<T>>::TokenNotFound)?;813 <TokenData<T>>::get((collection.id, token)).ok_or(<CommonError<T>>::TokenNotFound)?;813 ensure!(&token_data.owner == from, <CommonError<T>>::NoPermission);814 ensure!(&token_data.owner == from, <CommonError<T>>::NoPermission);814815815 if collection.permissions.access() == AccessMode::AllowList {816 let is_allow_list_mode = collection.permissions.access() == AccessMode::AllowList;817 if is_allow_list_mode {816 collection.check_allowlist(from)?;818 collection.check_allowlist(from)?;817 collection.check_allowlist(to)?;819 collection.check_allowlist(to)?;818 }820 }885 1,887 1,886 ));888 ));889890 let actual_weight = match is_allow_list_mode {891 true => Some(892 <SelfWeightOf<T>>::transfer() + <PalletCommonWeightOf<T>>::check_accesslist() * 2,893 ),894 false => Some(<SelfWeightOf<T>>::transfer()),895 };896887 Ok(())897 Ok(PostDispatchInfo {898 actual_weight,899 pays_fee: Pays::Yes,900 })888 }901 }889902890 /// Batch operation to mint multiple NFT tokens.903 /// Batch operation to mint multiple NFT tokens.1228 to: &T::CrossAccountId,1241 to: &T::CrossAccountId,1229 token: TokenId,1242 token: TokenId,1230 nesting_budget: &dyn Budget,1243 nesting_budget: &dyn Budget,1231 ) -> DispatchResult {1244 ) -> DispatchResultWithPostInfo {1232 Self::check_allowed(collection, spender, from, token, nesting_budget)?;1245 Self::check_allowed(collection, spender, from, token, nesting_budget)?;123312461234 // =========1247 // =========123512481236 // Allowance is reset in [`transfer`]1249 // Allowance is reset in [`transfer`]1237 Self::transfer(collection, from, to, token, nesting_budget)1250 Self::transfer(collection, from, to, token, nesting_budget).map(|mut p| {1251 p.actual_weight = Some(1252 p.actual_weight.unwrap_or_default() + <SelfWeightOf<T>>::checks_for_transfer_from(),1253 );1254 p1255 })1238 }1256 }123912571240 /// Burn NFT token for `from` account.1258 /// Burn NFT token for `from` account.pallets/nonfungible/src/weights.rsdiffbeforeafterboth43 fn transfer() -> Weight;43 fn transfer() -> Weight;44 fn approve() -> Weight;44 fn approve() -> Weight;45 fn approve_from() -> Weight;45 fn approve_from() -> Weight;46 fn transfer_from() -> Weight;46 fn checks_for_transfer_from() -> Weight;47 fn burn_from() -> Weight;47 fn burn_from() -> Weight;48 fn set_token_property_permissions(b: u32, ) -> Weight;48 fn set_token_property_permissions(b: u32, ) -> Weight;49 fn set_token_properties(b: u32, ) -> Weight;49 fn set_token_properties(b: u32, ) -> Weight;