difftreelog
feat(weight) Changed weight calculation system for transfer & transfer_from (FT)
in: master
9 files changed
Cargo.lockdiffbeforeafterboth655865586559[[package]]6559[[package]]6560name = "pallet-fungible"6560name = "pallet-fungible"6561version = "0.1.10"6561version = "0.1.11"6562dependencies = [6562dependencies = [6563 "evm-coder",6563 "evm-coder",6564 "frame-benchmarking",6564 "frame-benchmarking",pallets/foreign-assets/src/impl_fungibles.rsdiffbeforeafterboth453 amount.into(),453 amount.into(),454 &Value::new(0),454 &Value::new(0),455 )?;455 )456 .map_err(|e| e.error)?;456457457 Ok(amount)458 Ok(amount)458 }459 }pallets/fungible/CHANGELOG.mddiffbeforeafterboth445<!-- bureaucrate goes here -->5<!-- bureaucrate goes here -->667## [0.1.11] - 2023-03-2889### Fixed1011- The weight of `transfer` and `transfer_from`.127## [0.1.10] - 2023-02-0113## [0.1.10] - 2023-02-018149### Added15### Addedpallets/fungible/Cargo.tomldiffbeforeafterboth2edition = "2021"2edition = "2021"3license = "GPLv3"3license = "GPLv3"4name = "pallet-fungible"4name = "pallet-fungible"5version = "0.1.10"5version = "0.1.11"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/fungible/src/benchmarking.rsdiffbeforeafterboth92 <Pallet<T>>::create_item(&collection, &owner, (owner_eth.clone(), 200), &Unlimited)?;92 <Pallet<T>>::create_item(&collection, &owner, (owner_eth.clone(), 200), &Unlimited)?;93 }: {<Pallet<T>>::set_allowance_from(&collection, &sender, &owner_eth, &spender, 100)?}93 }: {<Pallet<T>>::set_allowance_from(&collection, &sender, &owner_eth, &spender, 100)?}949495 transfer_from {95 check_allowed_raw {96 bench_init!{96 bench_init!{97 owner: sub; collection: collection(owner);97 owner: sub; collection: collection(owner);98 owner: cross_from_sub; sender: cross_sub; spender: cross_sub; receiver: cross_sub;98 owner: cross_from_sub; sender: cross_sub; spender: cross_sub;99 };99 };100 <Pallet<T>>::create_item(&collection, &owner, (sender.clone(), 200), &Unlimited)?;100 <Pallet<T>>::create_item(&collection, &owner, (sender.clone(), 200), &Unlimited)?;101 <Pallet<T>>::set_allowance(&collection, &sender, &spender, 200)?;101 <Pallet<T>>::set_allowance(&collection, &sender, &spender, 200)?;102 }: {<Pallet<T>>::transfer_from(&collection, &spender, &sender, &receiver, 100, &Unlimited)?}102 }: {<Pallet<T>>::check_allowed(&collection, &spender, &sender, 200, &Unlimited)?;}103104 set_allowance_unchecked_raw {105 bench_init!{106 owner: sub; collection: collection(owner);107 owner: cross_from_sub; sender: cross_sub; spender: cross_sub;108 };109 <Pallet<T>>::create_item(&collection, &owner, (sender.clone(), 200), &Unlimited)?;110 }: {<Pallet<T>>::set_allowance_unchecked(&collection, &sender, &spender, 200);}103111104 burn_from {112 burn_from {105 bench_init!{113 bench_init!{pallets/fungible/src/common.rsdiffbeforeafterboth22};22};23use pallet_common::{23use pallet_common::{24 CommonCollectionOperations, CommonWeightInfo, RefungibleExtensions, with_weight,24 CommonCollectionOperations, CommonWeightInfo, RefungibleExtensions, with_weight,25 weights::WeightInfo as _,25 weights::WeightInfo as _, SelfWeightOf as PalletCommonWeightOf,26};26};27use pallet_structure::Error as StructureError;27use pallet_structure::Error as StructureError;28use sp_runtime::ArithmeticError;28use sp_runtime::ArithmeticError;78 }78 }797980 fn transfer() -> Weight {80 fn transfer() -> Weight {81 <SelfWeightOf<T>>::transfer()81 <SelfWeightOf<T>>::transfer() + <PalletCommonWeightOf<T>>::check_accesslist() * 282 }82 }838384 fn approve() -> Weight {84 fn approve() -> Weight {90 }90 }919192 fn transfer_from() -> Weight {92 fn transfer_from() -> Weight {93 <SelfWeightOf<T>>::transfer()94 + <SelfWeightOf<T>>::check_allowed_raw()93 <SelfWeightOf<T>>::transfer_from()95 + <SelfWeightOf<T>>::set_allowance_unchecked_raw()94 }96 }959796 fn burn_from() -> Weight {98 fn burn_from() -> Weight {232 <Error<T>>::FungibleItemsHaveNoId234 <Error<T>>::FungibleItemsHaveNoId233 );235 );234236235 with_weight(236 <Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget),237 <Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget)237 <CommonWeights<T>>::transfer(),238 )239 }238 }240239241 fn approve(240 fn approve(289 <Error<T>>::FungibleItemsHaveNoId288 <Error<T>>::FungibleItemsHaveNoId290 );289 );291290292 with_weight(293 <Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget),291 <Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget)294 <CommonWeights<T>>::transfer_from(),295 )296 }292 }297293298 fn burn_from(294 fn burn_from(pallets/fungible/src/erc.rsdiffbeforeafterboth26 CollectionHandle,26 CollectionHandle,27 erc::{CommonEvmHandler, PrecompileResult, CollectionCall},27 erc::{CommonEvmHandler, PrecompileResult, CollectionCall},28 eth::CrossAddress,28 eth::CrossAddress,29 CommonWeightInfo as _,29};30};30use sp_std::vec::Vec;31use sp_std::vec::Vec;31use pallet_evm::{account::CrossAccountId, PrecompileHandle};32use pallet_evm::{account::CrossAccountId, PrecompileHandle};394040use crate::{41use crate::{41 Allowance, Balance, Config, FungibleHandle, Pallet, TotalSupply, SelfWeightOf,42 Allowance, Balance, Config, FungibleHandle, Pallet, TotalSupply, SelfWeightOf,42 weights::WeightInfo,43 weights::WeightInfo, common::CommonWeights,43};44};444545frontier_contract! {46frontier_contract! {99 let balance = <Balance<T>>::get((self.id, owner));100 let balance = <Balance<T>>::get((self.id, owner));100 Ok(balance.into())101 Ok(balance.into())101 }102 }102 #[weight(<SelfWeightOf<T>>::transfer())]103 #[weight(<CommonWeights<T>>::transfer())]103 fn transfer(&mut self, caller: Caller, to: Address, amount: U256) -> Result<bool> {104 fn transfer(&mut self, caller: Caller, to: Address, amount: U256) -> Result<bool> {104 let caller = T::CrossAccountId::from_eth(caller);105 let caller = T::CrossAccountId::from_eth(caller);105 let to = T::CrossAccountId::from_eth(to);106 let to = T::CrossAccountId::from_eth(to);112 Ok(true)113 Ok(true)113 }114 }114115115 #[weight(<SelfWeightOf<T>>::transfer_from())]116 #[weight(<CommonWeights<T>>::transfer_from())]116 fn transfer_from(117 fn transfer_from(117 &mut self,118 &mut self,118 caller: Caller,119 caller: Caller,129 .weight_calls_budget(<StructureWeight<T>>::find_parent());130 .weight_calls_budget(<StructureWeight<T>>::find_parent());130131131 <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)132 <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)132 .map_err(dispatch_to_evm::<T>)?;133 .map_err(|e| dispatch_to_evm::<T>(e.error))?;133 Ok(true)134 Ok(true)134 }135 }135 #[weight(<SelfWeightOf<T>>::approve())]136 #[weight(<SelfWeightOf<T>>::approve())]201 let budget = self202 let budget = self202 .recorder203 .recorder203 .weight_calls_budget(<StructureWeight<T>>::find_parent());204 .weight_calls_budget(<StructureWeight<T>>::find_parent());204 <Pallet<T>>::create_item(&self, &caller, (to, amount), &budget)205 <Pallet<T>>::create_item(self, &caller, (to, amount), &budget)205 .map_err(dispatch_to_evm::<T>)?;206 .map_err(dispatch_to_evm::<T>)?;206 Ok(true)207 Ok(true)207 }208 }289 Ok(true)290 Ok(true)290 }291 }291292292 #[weight(<SelfWeightOf<T>>::transfer())]293 #[weight(<CommonWeights<T>>::transfer())]293 fn transfer_cross(&mut self, caller: Caller, to: CrossAddress, amount: U256) -> Result<bool> {294 fn transfer_cross(&mut self, caller: Caller, to: CrossAddress, amount: U256) -> Result<bool> {294 let caller = T::CrossAccountId::from_eth(caller);295 let caller = T::CrossAccountId::from_eth(caller);295 let to = to.into_sub_cross_account::<T>()?;296 let to = to.into_sub_cross_account::<T>()?;302 Ok(true)303 Ok(true)303 }304 }304305305 #[weight(<SelfWeightOf<T>>::transfer_from())]306 #[weight(<CommonWeights<T>>::transfer_from())]306 fn transfer_from_cross(307 fn transfer_from_cross(307 &mut self,308 &mut self,308 caller: Caller,309 caller: Caller,319 .weight_calls_budget(<StructureWeight<T>>::find_parent());320 .weight_calls_budget(<StructureWeight<T>>::find_parent());320321321 <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)322 <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)322 .map_err(dispatch_to_evm::<T>)?;323 .map_err(|e| dispatch_to_evm::<T>(e.error))?;323 Ok(true)324 Ok(true)324 }325 }325326pallets/fungible/src/lib.rsdiffbeforeafterboth808081use core::ops::Deref;81use core::ops::Deref;82use evm_coder::ToLog;82use evm_coder::ToLog;83use frame_support::ensure;83use frame_support::{84 ensure,85 pallet_prelude::{DispatchResultWithPostInfo, Pays},86 dispatch::PostDispatchInfo,87};84use pallet_evm::account::CrossAccountId;88use pallet_evm::account::CrossAccountId;85use up_data_structs::{89use up_data_structs::{86 AccessMode, CollectionId, CollectionFlags, TokenId, CreateCollectionData,90 AccessMode, CollectionId, CollectionFlags, TokenId, CreateCollectionData,87 mapping::TokenAddressMapping, budget::Budget, PropertyKey, Property,91 mapping::TokenAddressMapping, budget::Budget, PropertyKey, Property,88};92};89use pallet_common::{93use pallet_common::{90 Error as CommonError, Event as CommonEvent, Pallet as PalletCommon,94 Error as CommonError, Event as CommonEvent, Pallet as PalletCommon,91 eth::collection_id_to_address,95 eth::collection_id_to_address, SelfWeightOf as PalletCommonWeightOf,96 weights::WeightInfo as CommonWeightInfo, helpers::add_weight_to_post_info,92};97};93use pallet_evm::Pallet as PalletEvm;98use pallet_evm::Pallet as PalletEvm;94use pallet_structure::Pallet as PalletStructure;99use pallet_structure::Pallet as PalletStructure;95use pallet_evm_coder_substrate::WithRecorder;100use pallet_evm_coder_substrate::WithRecorder;96use sp_core::H160;101use sp_core::H160;97use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};102use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};98use sp_std::{collections::btree_map::BTreeMap, vec::Vec};103use sp_std::{collections::btree_map::BTreeMap, vec::Vec};99104use weights::WeightInfo;100pub use pallet::*;105pub use pallet::*;101106102use crate::erc::ERC20Events;107use crate::erc::ERC20Events;389 to: &T::CrossAccountId,394 to: &T::CrossAccountId,390 amount: u128,395 amount: u128,391 nesting_budget: &dyn Budget,396 nesting_budget: &dyn Budget,392 ) -> DispatchResult {397 ) -> DispatchResultWithPostInfo {393 ensure!(398 ensure!(394 collection.limits.transfers_enabled(),399 collection.limits.transfers_enabled(),395 <CommonError<T>>::TransferNotAllowed,400 <CommonError<T>>::TransferNotAllowed,396 );401 );402403 let mut actual_weight = <SelfWeightOf<T>>::transfer();397404398 if collection.permissions.access() == AccessMode::AllowList {405 if collection.permissions.access() == AccessMode::AllowList {399 collection.check_allowlist(from)?;406 collection.check_allowlist(from)?;400 collection.check_allowlist(to)?;407 collection.check_allowlist(to)?;408 actual_weight += <PalletCommonWeightOf<T>>::check_accesslist() * 2;401 }409 }402 <PalletCommon<T>>::ensure_correct_receiver(to)?;410 <PalletCommon<T>>::ensure_correct_receiver(to)?;403452 amount,459 amount,453 ));460 ));461454 Ok(())462 Ok(PostDispatchInfo {463 actual_weight: Some(actual_weight),464 pays_fee: Pays::Yes,465 })455 }466 }456467457 /// Minting tokens for multiple IDs.468 /// Minting tokens for multiple IDs.464 nesting_budget: &dyn Budget,475 nesting_budget: &dyn Budget,465 ) -> DispatchResult {476 ) -> DispatchResult {466 let total_supply = data477 let total_supply = data467 .iter()478 .values()468 .map(|(_, v)| *v)479 .copied()469 .try_fold(<TotalSupply<T>>::get(collection.id), |acc, v| {480 .try_fold(<TotalSupply<T>>::get(collection.id), |acc, v| {470 acc.checked_add(v)481 acc.checked_add(v)471 })482 })726 to: &T::CrossAccountId,736 to: &T::CrossAccountId,727 amount: u128,737 amount: u128,728 nesting_budget: &dyn Budget,738 nesting_budget: &dyn Budget,729 ) -> DispatchResult {739 ) -> DispatchResultWithPostInfo {730 let allowance = Self::check_allowed(collection, spender, from, amount, nesting_budget)?;740 let allowance = Self::check_allowed(collection, spender, from, amount, nesting_budget)?;731741732 // =========742 // =========733743734 Self::transfer(collection, from, to, amount, nesting_budget)?;744 let mut result = Self::transfer(collection, from, to, amount, nesting_budget);745 add_weight_to_post_info(&mut result, <SelfWeightOf<T>>::check_allowed_raw());746 result?;747735 if let Some(allowance) = allowance {748 if let Some(allowance) = allowance {736 Self::set_allowance_unchecked(collection, from, spender, allowance);749 Self::set_allowance_unchecked(collection, from, spender, allowance);750 add_weight_to_post_info(751 &mut result,752 <SelfWeightOf<T>>::set_allowance_unchecked_raw(),753 )737 }754 }738 Ok(())755 result739 }756 }740757741 /// Burn fungible tokens from the account.758 /// Burn fungible tokens from the account.pallets/fungible/src/weights.rsdiffbeforeafterboth40 fn transfer() -> Weight;40 fn transfer() -> Weight;41 fn approve() -> Weight;41 fn approve() -> Weight;42 fn approve_from() -> Weight;42 fn approve_from() -> Weight;43 fn transfer_from() -> Weight;43 fn check_allowed_raw() -> Weight;44 fn set_allowance_unchecked_raw() -> Weight;44 fn burn_from() -> Weight;45 fn burn_from() -> Weight;45}46}4647129 .saturating_add(T::DbWeight::get().reads(1_u64))130 .saturating_add(T::DbWeight::get().reads(1_u64))130 .saturating_add(T::DbWeight::get().writes(1_u64))131 .saturating_add(T::DbWeight::get().writes(1_u64))131 }132 }132 /// Storage: Fungible Allowance (r:1 w:1)133 // Storage: Fungible Allowance (r:1 w:0)133 /// Proof: Fungible Allowance (max_values: None, max_size: Some(93), added: 2568, mode: MaxEncodedLen)134 fn check_allowed_raw() -> Weight {134 /// Storage: Fungible Balance (r:2 w:2)135 Weight::from_ref_time(3_550_000 as u64)135 /// Proof: Fungible Balance (max_values: None, max_size: Some(77), added: 2552, mode: MaxEncodedLen)136 .saturating_add(T::DbWeight::get().reads(1 as u64))137 }138 // Storage: Fungible Allowance (r:1 w:1)136 fn transfer_from() -> Weight {139 fn set_allowance_unchecked_raw() -> Weight {137 // Proof Size summary in bytes:138 // Measured: `300`139 // Estimated: `7672`140 // Minimum execution time: 21_667_000 picoseconds.141 Weight::from_parts(22_166_000, 7672)140 Weight::from_ref_time(10_682_000 as u64)142 .saturating_add(T::DbWeight::get().reads(3_u64))141 .saturating_add(T::DbWeight::get().reads(1 as u64))143 .saturating_add(T::DbWeight::get().writes(3_u64))142 .saturating_add(T::DbWeight::get().writes(1 as u64))144 }143 }145 /// Storage: Fungible Allowance (r:1 w:1)144 /// Storage: Fungible Allowance (r:1 w:1)146 /// Proof: Fungible Allowance (max_values: None, max_size: Some(93), added: 2568, mode: MaxEncodedLen)145 /// Proof: Fungible Allowance (max_values: None, max_size: Some(93), added: 2568, mode: MaxEncodedLen)243 .saturating_add(RocksDbWeight::get().reads(1_u64))242 .saturating_add(RocksDbWeight::get().reads(1_u64))244 .saturating_add(RocksDbWeight::get().writes(1_u64))243 .saturating_add(RocksDbWeight::get().writes(1_u64))245 }244 }246 /// Storage: Fungible Allowance (r:1 w:1)245 // Storage: Fungible Allowance (r:1 w:0)247 /// Proof: Fungible Allowance (max_values: None, max_size: Some(93), added: 2568, mode: MaxEncodedLen)246 fn check_allowed_raw() -> Weight {248 /// Storage: Fungible Balance (r:2 w:2)247 Weight::from_ref_time(3_550_000 as u64)249 /// Proof: Fungible Balance (max_values: None, max_size: Some(77), added: 2552, mode: MaxEncodedLen)248 .saturating_add(RocksDbWeight::get().reads(1 as u64))249 }250 // Storage: Fungible Allowance (r:1 w:1)250 fn transfer_from() -> Weight {251 fn set_allowance_unchecked_raw() -> Weight {251 // Proof Size summary in bytes:252 // Measured: `300`253 // Estimated: `7672`254 // Minimum execution time: 21_667_000 picoseconds.255 Weight::from_parts(22_166_000, 7672)252 Weight::from_ref_time(10_682_000 as u64)256 .saturating_add(RocksDbWeight::get().reads(3_u64))253 .saturating_add(RocksDbWeight::get().reads(1 as u64))257 .saturating_add(RocksDbWeight::get().writes(3_u64))254 .saturating_add(RocksDbWeight::get().writes(1 as u64))258 }255 }259 /// Storage: Fungible Allowance (r:1 w:1)256 /// Storage: Fungible Allowance (r:1 w:1)260 /// Proof: Fungible Allowance (max_values: None, max_size: Some(93), added: 2568, mode: MaxEncodedLen)257 /// Proof: Fungible Allowance (max_values: None, max_size: Some(93), added: 2568, mode: MaxEncodedLen)