From 908b58f6b3818f258741501e5a6140e7843c7653 Mon Sep 17 00:00:00 2001 From: PraetorP Date: Thu, 30 Mar 2023 05:52:12 +0000 Subject: [PATCH] feat(weight): Changed weight calculation system for transfer & transfer_from (FT) --- --- a/Cargo.lock +++ b/Cargo.lock @@ -6558,7 +6558,7 @@ [[package]] name = "pallet-fungible" -version = "0.1.10" +version = "0.1.11" dependencies = [ "evm-coder", "frame-benchmarking", --- a/pallets/foreign-assets/src/impl_fungibles.rs +++ b/pallets/foreign-assets/src/impl_fungibles.rs @@ -452,7 +452,8 @@ &T::CrossAccountId::from_sub(dest.clone()), amount.into(), &Value::new(0), - )?; + ) + .map_err(|e| e.error)?; Ok(amount) } --- a/pallets/fungible/CHANGELOG.md +++ b/pallets/fungible/CHANGELOG.md @@ -4,6 +4,12 @@ +## [0.1.11] - 2023-03-28 + +### Fixed + +- The weight of `transfer` and `transfer_from`. + ## [0.1.10] - 2023-02-01 ### Added --- a/pallets/fungible/Cargo.toml +++ b/pallets/fungible/Cargo.toml @@ -2,7 +2,7 @@ edition = "2021" license = "GPLv3" name = "pallet-fungible" -version = "0.1.10" +version = "0.1.11" [dependencies] # Note: `package = "parity-scale-codec"` must be supplied since the `Encode` macro searches for it. --- a/pallets/fungible/src/benchmarking.rs +++ b/pallets/fungible/src/benchmarking.rs @@ -92,14 +92,22 @@ >::create_item(&collection, &owner, (owner_eth.clone(), 200), &Unlimited)?; }: {>::set_allowance_from(&collection, &sender, &owner_eth, &spender, 100)?} - transfer_from { + check_allowed_raw { bench_init!{ owner: sub; collection: collection(owner); - owner: cross_from_sub; sender: cross_sub; spender: cross_sub; receiver: cross_sub; + owner: cross_from_sub; sender: cross_sub; spender: cross_sub; }; >::create_item(&collection, &owner, (sender.clone(), 200), &Unlimited)?; >::set_allowance(&collection, &sender, &spender, 200)?; - }: {>::transfer_from(&collection, &spender, &sender, &receiver, 100, &Unlimited)?} + }: {>::check_allowed(&collection, &spender, &sender, 200, &Unlimited)?;} + + set_allowance_unchecked_raw { + bench_init!{ + owner: sub; collection: collection(owner); + owner: cross_from_sub; sender: cross_sub; spender: cross_sub; + }; + >::create_item(&collection, &owner, (sender.clone(), 200), &Unlimited)?; + }: {>::set_allowance_unchecked(&collection, &sender, &spender, 200);} burn_from { bench_init!{ --- a/pallets/fungible/src/common.rs +++ b/pallets/fungible/src/common.rs @@ -22,7 +22,7 @@ }; use pallet_common::{ CommonCollectionOperations, CommonWeightInfo, RefungibleExtensions, with_weight, - weights::WeightInfo as _, + weights::WeightInfo as _, SelfWeightOf as PalletCommonWeightOf, }; use pallet_structure::Error as StructureError; use sp_runtime::ArithmeticError; @@ -78,7 +78,7 @@ } fn transfer() -> Weight { - >::transfer() + >::transfer() + >::check_accesslist() * 2 } fn approve() -> Weight { @@ -90,7 +90,9 @@ } fn transfer_from() -> Weight { - >::transfer_from() + >::transfer() + + >::check_allowed_raw() + + >::set_allowance_unchecked_raw() } fn burn_from() -> Weight { @@ -232,10 +234,7 @@ >::FungibleItemsHaveNoId ); - with_weight( - >::transfer(self, &from, &to, amount, nesting_budget), - >::transfer(), - ) + >::transfer(self, &from, &to, amount, nesting_budget) } fn approve( @@ -289,10 +288,7 @@ >::FungibleItemsHaveNoId ); - with_weight( - >::transfer_from(self, &sender, &from, &to, amount, nesting_budget), - >::transfer_from(), - ) + >::transfer_from(self, &sender, &from, &to, amount, nesting_budget) } fn burn_from( --- a/pallets/fungible/src/erc.rs +++ b/pallets/fungible/src/erc.rs @@ -26,6 +26,7 @@ CollectionHandle, erc::{CommonEvmHandler, PrecompileResult, CollectionCall}, eth::CrossAddress, + CommonWeightInfo as _, }; use sp_std::vec::Vec; use pallet_evm::{account::CrossAccountId, PrecompileHandle}; @@ -39,7 +40,7 @@ use crate::{ Allowance, Balance, Config, FungibleHandle, Pallet, TotalSupply, SelfWeightOf, - weights::WeightInfo, + weights::WeightInfo, common::CommonWeights, }; frontier_contract! { @@ -99,7 +100,7 @@ let balance = >::get((self.id, owner)); Ok(balance.into()) } - #[weight(>::transfer())] + #[weight(>::transfer())] fn transfer(&mut self, caller: Caller, to: Address, amount: U256) -> Result { let caller = T::CrossAccountId::from_eth(caller); let to = T::CrossAccountId::from_eth(to); @@ -112,7 +113,7 @@ Ok(true) } - #[weight(>::transfer_from())] + #[weight(>::transfer_from())] fn transfer_from( &mut self, caller: Caller, @@ -129,7 +130,7 @@ .weight_calls_budget(>::find_parent()); >::transfer_from(self, &caller, &from, &to, amount, &budget) - .map_err(dispatch_to_evm::)?; + .map_err(|e| dispatch_to_evm::(e.error))?; Ok(true) } #[weight(>::approve())] @@ -201,7 +202,7 @@ let budget = self .recorder .weight_calls_budget(>::find_parent()); - >::create_item(&self, &caller, (to, amount), &budget) + >::create_item(self, &caller, (to, amount), &budget) .map_err(dispatch_to_evm::)?; Ok(true) } @@ -289,7 +290,7 @@ Ok(true) } - #[weight(>::transfer())] + #[weight(>::transfer())] fn transfer_cross(&mut self, caller: Caller, to: CrossAddress, amount: U256) -> Result { let caller = T::CrossAccountId::from_eth(caller); let to = to.into_sub_cross_account::()?; @@ -302,7 +303,7 @@ Ok(true) } - #[weight(>::transfer_from())] + #[weight(>::transfer_from())] fn transfer_from_cross( &mut self, caller: Caller, @@ -319,7 +320,7 @@ .weight_calls_budget(>::find_parent()); >::transfer_from(self, &caller, &from, &to, amount, &budget) - .map_err(dispatch_to_evm::)?; + .map_err(|e| dispatch_to_evm::(e.error))?; Ok(true) } --- a/pallets/fungible/src/lib.rs +++ b/pallets/fungible/src/lib.rs @@ -80,7 +80,11 @@ use core::ops::Deref; use evm_coder::ToLog; -use frame_support::ensure; +use frame_support::{ + ensure, + pallet_prelude::{DispatchResultWithPostInfo, Pays}, + dispatch::PostDispatchInfo, +}; use pallet_evm::account::CrossAccountId; use up_data_structs::{ AccessMode, CollectionId, CollectionFlags, TokenId, CreateCollectionData, @@ -88,7 +92,8 @@ }; use pallet_common::{ Error as CommonError, Event as CommonEvent, Pallet as PalletCommon, - eth::collection_id_to_address, + eth::collection_id_to_address, SelfWeightOf as PalletCommonWeightOf, + weights::WeightInfo as CommonWeightInfo, helpers::add_weight_to_post_info, }; use pallet_evm::Pallet as PalletEvm; use pallet_structure::Pallet as PalletStructure; @@ -96,7 +101,7 @@ use sp_core::H160; use sp_runtime::{ArithmeticError, DispatchError, DispatchResult}; use sp_std::{collections::btree_map::BTreeMap, vec::Vec}; - +use weights::WeightInfo; pub use pallet::*; use crate::erc::ERC20Events; @@ -389,18 +394,20 @@ to: &T::CrossAccountId, amount: u128, nesting_budget: &dyn Budget, - ) -> DispatchResult { + ) -> DispatchResultWithPostInfo { ensure!( collection.limits.transfers_enabled(), >::TransferNotAllowed, ); + let mut actual_weight = >::transfer(); + if collection.permissions.access() == AccessMode::AllowList { collection.check_allowlist(from)?; collection.check_allowlist(to)?; + actual_weight += >::check_accesslist() * 2; } >::ensure_correct_receiver(to)?; - let balance_from = >::get((collection.id, from)) .checked_sub(amount) .ok_or(>::TokenValueTooLow)?; @@ -451,7 +458,11 @@ to.clone(), amount, )); - Ok(()) + + Ok(PostDispatchInfo { + actual_weight: Some(actual_weight), + pays_fee: Pays::Yes, + }) } /// Minting tokens for multiple IDs. @@ -464,8 +475,8 @@ nesting_budget: &dyn Budget, ) -> DispatchResult { let total_supply = data - .iter() - .map(|(_, v)| *v) + .values() + .copied() .try_fold(>::get(collection.id), |acc, v| { acc.checked_add(v) }) @@ -718,7 +729,6 @@ /// Same as the [`transfer`][`Pallet::transfer`] but spender doesn't needs to be an owner of the token pieces. /// The owner should set allowance for the spender to transfer pieces. /// See [`set_allowance`][`Pallet::set_allowance`] for more details. - pub fn transfer_from( collection: &FungibleHandle, spender: &T::CrossAccountId, @@ -726,16 +736,23 @@ to: &T::CrossAccountId, amount: u128, nesting_budget: &dyn Budget, - ) -> DispatchResult { + ) -> DispatchResultWithPostInfo { let allowance = Self::check_allowed(collection, spender, from, amount, nesting_budget)?; // ========= - Self::transfer(collection, from, to, amount, nesting_budget)?; + let mut result = Self::transfer(collection, from, to, amount, nesting_budget); + add_weight_to_post_info(&mut result, >::check_allowed_raw()); + result?; + if let Some(allowance) = allowance { Self::set_allowance_unchecked(collection, from, spender, allowance); + add_weight_to_post_info( + &mut result, + >::set_allowance_unchecked_raw(), + ) } - Ok(()) + result } /// Burn fungible tokens from the account. --- a/pallets/fungible/src/weights.rs +++ b/pallets/fungible/src/weights.rs @@ -40,7 +40,8 @@ fn transfer() -> Weight; fn approve() -> Weight; fn approve_from() -> Weight; - fn transfer_from() -> Weight; + fn check_allowed_raw() -> Weight; + fn set_allowance_unchecked_raw() -> Weight; fn burn_from() -> Weight; } @@ -129,19 +130,17 @@ .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } - /// Storage: Fungible Allowance (r:1 w:1) - /// Proof: Fungible Allowance (max_values: None, max_size: Some(93), added: 2568, mode: MaxEncodedLen) - /// Storage: Fungible Balance (r:2 w:2) - /// Proof: Fungible Balance (max_values: None, max_size: Some(77), added: 2552, mode: MaxEncodedLen) - fn transfer_from() -> Weight { - // Proof Size summary in bytes: - // Measured: `300` - // Estimated: `7672` - // Minimum execution time: 21_667_000 picoseconds. - Weight::from_parts(22_166_000, 7672) - .saturating_add(T::DbWeight::get().reads(3_u64)) - .saturating_add(T::DbWeight::get().writes(3_u64)) + // Storage: Fungible Allowance (r:1 w:0) + fn check_allowed_raw() -> Weight { + Weight::from_ref_time(3_550_000 as u64) + .saturating_add(T::DbWeight::get().reads(1 as u64)) } + // Storage: Fungible Allowance (r:1 w:1) + fn set_allowance_unchecked_raw() -> Weight { + Weight::from_ref_time(10_682_000 as u64) + .saturating_add(T::DbWeight::get().reads(1 as u64)) + .saturating_add(T::DbWeight::get().writes(1 as u64)) + } /// Storage: Fungible Allowance (r:1 w:1) /// Proof: Fungible Allowance (max_values: None, max_size: Some(93), added: 2568, mode: MaxEncodedLen) /// Storage: Fungible TotalSupply (r:1 w:1) @@ -243,18 +242,16 @@ .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } - /// Storage: Fungible Allowance (r:1 w:1) - /// Proof: Fungible Allowance (max_values: None, max_size: Some(93), added: 2568, mode: MaxEncodedLen) - /// Storage: Fungible Balance (r:2 w:2) - /// Proof: Fungible Balance (max_values: None, max_size: Some(77), added: 2552, mode: MaxEncodedLen) - fn transfer_from() -> Weight { - // Proof Size summary in bytes: - // Measured: `300` - // Estimated: `7672` - // Minimum execution time: 21_667_000 picoseconds. - Weight::from_parts(22_166_000, 7672) - .saturating_add(RocksDbWeight::get().reads(3_u64)) - .saturating_add(RocksDbWeight::get().writes(3_u64)) + // Storage: Fungible Allowance (r:1 w:0) + fn check_allowed_raw() -> Weight { + Weight::from_ref_time(3_550_000 as u64) + .saturating_add(RocksDbWeight::get().reads(1 as u64)) + } + // Storage: Fungible Allowance (r:1 w:1) + fn set_allowance_unchecked_raw() -> Weight { + Weight::from_ref_time(10_682_000 as u64) + .saturating_add(RocksDbWeight::get().reads(1 as u64)) + .saturating_add(RocksDbWeight::get().writes(1 as u64)) } /// Storage: Fungible Allowance (r:1 w:1) /// Proof: Fungible Allowance (max_values: None, max_size: Some(93), added: 2568, mode: MaxEncodedLen) -- gitstuff