git.delta.rocks / unique-network / refs/commits / f4c0a8800bca

difftreelog

refactor impl of `transfer` `transfer_from` functions, bencmarks for `Common` & `NFT` pallets

PraetorP2023-03-29parent: #b4ec6cd.patch.diff
in: master

7 files changed

modifiedpallets/common/src/benchmarking.rsdiffbeforeafterboth
--- a/pallets/common/src/benchmarking.rs
+++ b/pallets/common/src/benchmarking.rs
@@ -215,17 +215,9 @@
 				true,
 			)?;
 
-		<Pallet<T>>::toggle_allowlist(
-				&collection,
-				&sender,
-				&receiver,
-				true,
-			)?;
-
 		assert_eq!(collection_handle.permissions.access(), AccessMode::AllowList);
 
 		collection_handle.check_allowlist(&sender)?;
-		collection_handle.check_allowlist(&receiver)?;
 
 	}: {collection_handle.check_allowlist(&sender)?;}
 }
addedpallets/common/src/helpers.rsdiffbeforeafterboth
--- /dev/null
+++ b/pallets/common/src/helpers.rs
@@ -0,0 +1,30 @@
+//! # Helpers module
+//!
+//! The module contains helpers.
+//!
+use frame_support::{
+	pallet_prelude::DispatchResultWithPostInfo,
+	weights::Weight,
+	dispatch::{DispatchErrorWithPostInfo, PostDispatchInfo},
+};
+
+/// Add weight for a `DispatchResultWithPostInfo`
+///
+/// - `target`: DispatchResultWithPostInfo to which weight will be added
+/// - `additional_weight`: Weight to be added
+pub fn add_weight_to_post_info(target: &mut DispatchResultWithPostInfo, additional_weight: Weight) {
+	match target {
+		Ok(PostDispatchInfo {
+			actual_weight: Some(weight),
+			..
+		})
+		| Err(DispatchErrorWithPostInfo {
+			post_info: PostDispatchInfo {
+				actual_weight: Some(weight),
+				..
+			},
+			..
+		}) => *weight += additional_weight,
+		_ => {}
+	}
+}
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -92,9 +92,9 @@
 pub mod dispatch;
 pub mod erc;
 pub mod eth;
+pub mod helpers;
 #[allow(missing_docs)]
 pub mod weights;
-
 /// Weight info.
 pub type SelfWeightOf<T> = <T as Config>::WeightInfo;
 
modifiedpallets/nonfungible/src/benchmarking.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/benchmarking.rs
+++ b/pallets/nonfungible/src/benchmarking.rs
@@ -146,7 +146,7 @@
 		let item = create_max_item(&collection, &owner, owner_eth.clone())?;
 	}: {<Pallet<T>>::set_allowance_from(&collection, &sender, &owner_eth, item, Some(&spender))?}
 
-	checks_for_transfer_from {
+	checks_allowed_raw {
 		bench_init!{
 			owner: sub; collection: collection(owner);
 			owner: cross_from_sub; sender: cross_sub; spender: cross_sub; receiver: cross_sub;
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
103 }103 }
104104
105 fn transfer_from() -> Weight {105 fn transfer_from() -> Weight {
106 Self::transfer() + <SelfWeightOf<T>>::checks_for_transfer_from()106 Self::transfer() + <SelfWeightOf<T>>::checks_allowed_raw()
107 }107 }
108108
109 fn burn_from() -> Weight {109 fn burn_from() -> Weight {
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -109,7 +109,7 @@
 use pallet_common::{
 	Error as CommonError, Pallet as PalletCommon, Event as CommonEvent, CollectionHandle,
 	eth::collection_id_to_address, SelfWeightOf as PalletCommonWeightOf,
-	weights::WeightInfo as CommonWeightInfo,
+	weights::WeightInfo as CommonWeightInfo, helpers::add_weight_to_post_info,
 };
 use pallet_structure::{Pallet as PalletStructure, Error as StructureError};
 use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};
@@ -809,14 +809,15 @@
 			<CommonError<T>>::TransferNotAllowed
 		);
 
+		let mut actual_weight = <SelfWeightOf<T>>::transfer();
 		let token_data =
 			<TokenData<T>>::get((collection.id, token)).ok_or(<CommonError<T>>::TokenNotFound)?;
 		ensure!(&token_data.owner == from, <CommonError<T>>::NoPermission);
 
-		let is_allow_list_mode = collection.permissions.access() == AccessMode::AllowList;
-		if is_allow_list_mode {
+		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)?;
 
@@ -887,15 +888,8 @@
 			1,
 		));
 
-		let actual_weight = match is_allow_list_mode {
-			true => Some(
-				<SelfWeightOf<T>>::transfer() + <PalletCommonWeightOf<T>>::check_accesslist() * 2,
-			),
-			false => Some(<SelfWeightOf<T>>::transfer()),
-		};
-
 		Ok(PostDispatchInfo {
-			actual_weight,
+			actual_weight: Some(actual_weight),
 			pays_fee: Pays::Yes,
 		})
 	}
@@ -1247,12 +1241,9 @@
 		// =========
 
 		// Allowance is reset in [`transfer`]
-		Self::transfer(collection, from, to, token, nesting_budget).map(|mut p| {
-			p.actual_weight = Some(
-				p.actual_weight.unwrap_or_default() + <SelfWeightOf<T>>::checks_for_transfer_from(),
-			);
-			p
-		})
+		let mut result = Self::transfer(collection, from, to, token, nesting_budget);
+		add_weight_to_post_info(&mut result, <SelfWeightOf<T>>::checks_allowed_raw());
+		result
 	}
 
 	/// Burn NFT token for `from` account.
modifiedpallets/nonfungible/src/weights.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/weights.rs
+++ b/pallets/nonfungible/src/weights.rs
@@ -43,7 +43,7 @@
 	fn transfer() -> Weight;
 	fn approve() -> Weight;
 	fn approve_from() -> Weight;
-	fn checks_for_transfer_from() -> Weight;
+	fn checks_allowed_raw() -> Weight;
 	fn burn_from() -> Weight;
 	fn set_token_property_permissions(b: u32, ) -> Weight;
 	fn set_token_properties(b: u32, ) -> Weight;
@@ -252,22 +252,10 @@
 			.saturating_add(T::DbWeight::get().reads(2_u64))
 			.saturating_add(T::DbWeight::get().writes(1_u64))
 	}
-	/// Storage: Nonfungible Allowance (r:1 w:1)
-	/// Proof: Nonfungible Allowance (max_values: None, max_size: Some(57), added: 2532, mode: MaxEncodedLen)
-	/// Storage: Nonfungible TokenData (r:1 w:1)
-	/// Proof: Nonfungible TokenData (max_values: None, max_size: Some(57), added: 2532, mode: MaxEncodedLen)
-	/// Storage: Nonfungible AccountBalance (r:2 w:2)
-	/// Proof: Nonfungible AccountBalance (max_values: None, max_size: Some(65), added: 2540, mode: MaxEncodedLen)
-	/// Storage: Nonfungible Owned (r:0 w:2)
-	/// Proof: Nonfungible Owned (max_values: None, max_size: Some(74), added: 2549, mode: MaxEncodedLen)
-	fn transfer_from() -> Weight {
-		// Proof Size summary in bytes:
-		//  Measured:  `527`
-		//  Estimated: `10144`
-		// Minimum execution time: 24_919_000 picoseconds.
-		Weight::from_parts(25_333_000, 10144)
-			.saturating_add(T::DbWeight::get().reads(4_u64))
-			.saturating_add(T::DbWeight::get().writes(6_u64))
+	// Storage: Nonfungible Allowance (r:1 w:0)
+	fn checks_allowed_raw() -> Weight {
+		Weight::from_ref_time(3_341_000 as u64)
+			.saturating_add(T::DbWeight::get().reads(1 as u64))
 	}
 	/// Storage: Nonfungible Allowance (r:1 w:1)
 	/// Proof: Nonfungible Allowance (max_values: None, max_size: Some(57), added: 2532, mode: MaxEncodedLen)
@@ -578,22 +566,10 @@
 			.saturating_add(RocksDbWeight::get().reads(2_u64))
 			.saturating_add(RocksDbWeight::get().writes(1_u64))
 	}
-	/// Storage: Nonfungible Allowance (r:1 w:1)
-	/// Proof: Nonfungible Allowance (max_values: None, max_size: Some(57), added: 2532, mode: MaxEncodedLen)
-	/// Storage: Nonfungible TokenData (r:1 w:1)
-	/// Proof: Nonfungible TokenData (max_values: None, max_size: Some(57), added: 2532, mode: MaxEncodedLen)
-	/// Storage: Nonfungible AccountBalance (r:2 w:2)
-	/// Proof: Nonfungible AccountBalance (max_values: None, max_size: Some(65), added: 2540, mode: MaxEncodedLen)
-	/// Storage: Nonfungible Owned (r:0 w:2)
-	/// Proof: Nonfungible Owned (max_values: None, max_size: Some(74), added: 2549, mode: MaxEncodedLen)
-	fn transfer_from() -> Weight {
-		// Proof Size summary in bytes:
-		//  Measured:  `527`
-		//  Estimated: `10144`
-		// Minimum execution time: 24_919_000 picoseconds.
-		Weight::from_parts(25_333_000, 10144)
-			.saturating_add(RocksDbWeight::get().reads(4_u64))
-			.saturating_add(RocksDbWeight::get().writes(6_u64))
+	// Storage: Nonfungible Allowance (r:1 w:0)
+	fn checks_allowed_raw() -> Weight {
+		Weight::from_ref_time(3_341_000 as u64)
+			.saturating_add(RocksDbWeight::get().reads(1 as u64))
 	}
 	/// Storage: Nonfungible Allowance (r:1 w:1)
 	/// Proof: Nonfungible Allowance (max_values: None, max_size: Some(57), added: 2532, mode: MaxEncodedLen)