git.delta.rocks / unique-network / refs/commits / 908b58f6b381

difftreelog

feat(weight) Changed weight calculation system for transfer & transfer_from (FT)

PraetorP2023-03-30parent: #918595f.patch.diff
in: master

9 files changed

modifiedCargo.lockdiffbeforeafterboth
--- 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",
modifiedpallets/foreign-assets/src/impl_fungibles.rsdiffbeforeafterboth
--- 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)
 			}
modifiedpallets/fungible/CHANGELOG.mddiffbeforeafterboth
--- a/pallets/fungible/CHANGELOG.md
+++ b/pallets/fungible/CHANGELOG.md
@@ -4,6 +4,12 @@
 
 <!-- bureaucrate goes here -->
 
+## [0.1.11] - 2023-03-28
+
+### Fixed
+
+- The weight of `transfer` and `transfer_from`.
+
 ## [0.1.10] - 2023-02-01
 
 ### Added
modifiedpallets/fungible/Cargo.tomldiffbeforeafterboth
--- 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.
modifiedpallets/fungible/src/benchmarking.rsdiffbeforeafterboth
--- a/pallets/fungible/src/benchmarking.rs
+++ b/pallets/fungible/src/benchmarking.rs
@@ -92,14 +92,22 @@
 		<Pallet<T>>::create_item(&collection, &owner, (owner_eth.clone(), 200), &Unlimited)?;
 	}: {<Pallet<T>>::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;
 		};
 		<Pallet<T>>::create_item(&collection, &owner, (sender.clone(), 200), &Unlimited)?;
 		<Pallet<T>>::set_allowance(&collection, &sender, &spender, 200)?;
-	}: {<Pallet<T>>::transfer_from(&collection, &spender, &sender, &receiver, 100, &Unlimited)?}
+	}: {<Pallet<T>>::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;
+		};
+		<Pallet<T>>::create_item(&collection, &owner, (sender.clone(), 200), &Unlimited)?;
+	}: {<Pallet<T>>::set_allowance_unchecked(&collection, &sender, &spender, 200);}
 
 	burn_from {
 		bench_init!{
modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
--- 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 {
-		<SelfWeightOf<T>>::transfer()
+		<SelfWeightOf<T>>::transfer() + <PalletCommonWeightOf<T>>::check_accesslist() * 2
 	}
 
 	fn approve() -> Weight {
@@ -90,7 +90,9 @@
 	}
 
 	fn transfer_from() -> Weight {
-		<SelfWeightOf<T>>::transfer_from()
+		<SelfWeightOf<T>>::transfer()
+			+ <SelfWeightOf<T>>::check_allowed_raw()
+			+ <SelfWeightOf<T>>::set_allowance_unchecked_raw()
 	}
 
 	fn burn_from() -> Weight {
@@ -232,10 +234,7 @@
 			<Error<T>>::FungibleItemsHaveNoId
 		);
 
-		with_weight(
-			<Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget),
-			<CommonWeights<T>>::transfer(),
-		)
+		<Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget)
 	}
 
 	fn approve(
@@ -289,10 +288,7 @@
 			<Error<T>>::FungibleItemsHaveNoId
 		);
 
-		with_weight(
-			<Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget),
-			<CommonWeights<T>>::transfer_from(),
-		)
+		<Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget)
 	}
 
 	fn burn_from(
modifiedpallets/fungible/src/erc.rsdiffbeforeafterboth
26 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};
3940
40use 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};
4445
45frontier_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 }
114115
115 #[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());
130131
131 <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 = self
202 .recorder203 .recorder
203 .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 }
291292
292 #[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 }
304305
305 #[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());
320321
321 <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 }
325326
modifiedpallets/fungible/src/lib.rsdiffbeforeafterboth
--- 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(),
 			<CommonError<T>>::TransferNotAllowed,
 		);
 
+		let mut actual_weight = <SelfWeightOf<T>>::transfer();
+
 		if collection.permissions.access() == AccessMode::AllowList {
 			collection.check_allowlist(from)?;
 			collection.check_allowlist(to)?;
+			actual_weight += <PalletCommonWeightOf<T>>::check_accesslist() * 2;
 		}
 		<PalletCommon<T>>::ensure_correct_receiver(to)?;
-
 		let balance_from = <Balance<T>>::get((collection.id, from))
 			.checked_sub(amount)
 			.ok_or(<CommonError<T>>::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(<TotalSupply<T>>::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<T>,
 		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, <SelfWeightOf<T>>::check_allowed_raw());
+		result?;
+
 		if let Some(allowance) = allowance {
 			Self::set_allowance_unchecked(collection, from, spender, allowance);
+			add_weight_to_post_info(
+				&mut result,
+				<SelfWeightOf<T>>::set_allowance_unchecked_raw(),
+			)
 		}
-		Ok(())
+		result
 	}
 
 	/// Burn fungible tokens from the account.
modifiedpallets/fungible/src/weights.rsdiffbeforeafterboth
--- 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)