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

difftreelog

fix manage properties of nested tokens

Daniel Shiposha2022-06-30parent: #2360ffd.patch.diff
in: master

7 files changed

modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -1311,12 +1311,14 @@
 		sender: T::CrossAccountId,
 		token_id: TokenId,
 		property: Vec<Property>,
+		budget: &dyn Budget,
 	) -> DispatchResultWithPostInfo;
 	fn delete_token_properties(
 		&self,
 		sender: T::CrossAccountId,
 		token_id: TokenId,
 		property_keys: Vec<PropertyKey>,
+		budget: &dyn Budget,
 	) -> DispatchResultWithPostInfo;
 	fn set_token_property_permissions(
 		&self,
modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -298,6 +298,7 @@
 		_sender: T::CrossAccountId,
 		_token_id: TokenId,
 		_property: Vec<Property>,
+		_budget: &dyn Budget,
 	) -> DispatchResultWithPostInfo {
 		fail!(<Error<T>>::SettingPropertiesNotAllowed)
 	}
@@ -315,6 +316,7 @@
 		_sender: T::CrossAccountId,
 		_token_id: TokenId,
 		_property_keys: Vec<PropertyKey>,
+		_budget: &dyn Budget,
 	) -> DispatchResultWithPostInfo {
 		fail!(<Error<T>>::SettingPropertiesNotAllowed)
 	}
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -220,11 +220,12 @@
 		sender: T::CrossAccountId,
 		token_id: TokenId,
 		properties: Vec<Property>,
+		budget: &dyn Budget,
 	) -> DispatchResultWithPostInfo {
 		let weight = <CommonWeights<T>>::set_token_properties(properties.len() as u32);
 
 		with_weight(
-			<Pallet<T>>::set_token_properties(self, &sender, token_id, properties, false),
+			<Pallet<T>>::set_token_properties(self, &sender, token_id, properties, false, budget),
 			weight,
 		)
 	}
@@ -234,11 +235,12 @@
 		sender: T::CrossAccountId,
 		token_id: TokenId,
 		property_keys: Vec<PropertyKey>,
+		budget: &dyn Budget,
 	) -> DispatchResultWithPostInfo {
 		let weight = <CommonWeights<T>>::delete_token_properties(property_keys.len() as u32);
 
 		with_weight(
-			<Pallet<T>>::delete_token_properties(self, &sender, token_id, property_keys),
+			<Pallet<T>>::delete_token_properties(self, &sender, token_id, property_keys, budget),
 			weight,
 		)
 	}
modifiedpallets/nonfungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/erc.rs
+++ b/pallets/nonfungible/src/erc.rs
@@ -38,6 +38,7 @@
 use crate::{
 	AccountBalance, Config, CreateItemData, NonfungibleHandle, Pallet, TokenData, TokensMinted,
 	SelfWeightOf, weights::WeightInfo, TokenProperties,
+	property_guard::PropertyGuard,
 };
 
 #[solidity_interface(name = "TokenProperties")]
@@ -82,14 +83,21 @@
 			.map_err(|_| "key too long")?;
 		let value = value.try_into().map_err(|_| "value too long")?;
 
-		<Pallet<T>>::set_token_property(
+		let is_token_create = false;
+		let budget = self
+			.recorder
+			.weight_calls_budget(<StructureWeight<T>>::find_parent());
+
+		let mut guard = PropertyGuard::new(
+			&caller,
 			self,
-			&caller,
 			TokenId(token_id),
-			Property { key, value },
-			false,
-		)
-		.map_err(dispatch_to_evm::<T>)
+			is_token_create,
+			&budget,
+		);
+
+		<Pallet<T>>::set_token_property(Property { key, value }, &mut guard)
+			.map_err(dispatch_to_evm::<T>)
 	}
 
 	fn delete_property(&mut self, token_id: uint256, caller: caller, key: string) -> Result<()> {
@@ -99,7 +107,20 @@
 			.try_into()
 			.map_err(|_| "key too long")?;
 
-		<Pallet<T>>::delete_token_property(self, &caller, TokenId(token_id), key)
+		let is_token_create = false;
+		let budget = self
+			.recorder
+			.weight_calls_budget(<StructureWeight<T>>::find_parent());
+
+		let mut guard = PropertyGuard::new(
+			&caller,
+			self,
+			TokenId(token_id),
+			is_token_create,
+			&budget,
+		);
+
+		<Pallet<T>>::delete_token_property(key, &mut guard)
 			.map_err(dispatch_to_evm::<T>)
 	}
 
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -52,6 +52,10 @@
 pub mod erc;
 pub mod weights;
 
+mod property_guard;
+
+use property_guard::*;
+
 pub type CreateItemData<T> = CreateNftExData<<T as pallet_evm::account::Config>::CrossAccountId>;
 pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;
 
@@ -484,30 +488,18 @@
 		})
 	}
 
-	pub fn set_token_property(
-		collection: &NonfungibleHandle<T>,
-		sender: &T::CrossAccountId,
-		token_id: TokenId,
-		property: Property,
-		is_token_create: bool,
-	) -> DispatchResult {
-		Self::check_token_change_permission(
-			collection,
-			sender,
-			token_id,
-			&property.key,
-			is_token_create,
-		)?;
+	pub fn set_token_property(property: Property, guard: &mut PropertyGuard<'_, T>) -> DispatchResult {
+		Self::check_token_change_permission(&property.key, guard)?;
 
-		<TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {
+		<TokenProperties<T>>::try_mutate((guard.collection.id, guard.token), |properties| {
 			let property = property.clone();
 			properties.try_set(property.key, property.value)
 		})
 		.map_err(<CommonError<T>>::from)?;
 
 		<PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertySet(
-			collection.id,
-			token_id,
+			guard.collection.id,
+			guard.token,
 			property.key,
 		));
 
@@ -521,57 +513,47 @@
 		token_id: TokenId,
 		properties: Vec<Property>,
 		is_token_create: bool,
+		nesting_budget: &dyn Budget,
 	) -> DispatchResult {
+		let mut guard = PropertyGuard::new(
+			sender,
+			collection,
+			token_id,
+			is_token_create,
+			nesting_budget,
+		);
+
 		for property in properties {
-			Self::set_token_property(collection, sender, token_id, property, is_token_create)?;
+			Self::set_token_property(property, &mut guard)?;
 		}
 
 		Ok(())
 	}
 
-	pub fn delete_token_property(
-		collection: &NonfungibleHandle<T>,
-		sender: &T::CrossAccountId,
-		token_id: TokenId,
-		property_key: PropertyKey,
-	) -> DispatchResult {
-		Self::check_token_change_permission(collection, sender, token_id, &property_key, false)?;
+	pub fn delete_token_property(property_key: PropertyKey, guard: &mut PropertyGuard<'_, T>) -> DispatchResult {
+		Self::check_token_change_permission(&property_key, guard)?;
 
-		<TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {
+		<TokenProperties<T>>::try_mutate((guard.collection.id, guard.token), |properties| {
 			properties.remove(&property_key)
 		})
 		.map_err(<CommonError<T>>::from)?;
 
 		<PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertyDeleted(
-			collection.id,
-			token_id,
+			guard.collection.id,
+			guard.token,
 			property_key,
 		));
 
 		Ok(())
 	}
 
-	fn check_token_change_permission(
-		collection: &NonfungibleHandle<T>,
-		sender: &T::CrossAccountId,
-		token_id: TokenId,
-		property_key: &PropertyKey,
-		is_token_create: bool,
-	) -> DispatchResult {
-		let permission = <PalletCommon<T>>::property_permissions(collection.id)
+	fn check_token_change_permission(property_key: &PropertyKey, guard: &mut PropertyGuard<'_, T>) -> DispatchResult {
+		let permission = <PalletCommon<T>>::property_permissions(guard.collection.id)
 			.get(property_key)
 			.cloned()
 			.unwrap_or_else(PropertyPermission::none);
 
-		let token_data = <TokenData<T>>::get((collection.id, token_id))
-			.ok_or(<CommonError<T>>::TokenNotFound)?;
-
-		let check_token_owner = || -> DispatchResult {
-			ensure!(&token_data.owner == sender, <CommonError<T>>::NoPermission);
-			Ok(())
-		};
-
-		let is_property_exists = TokenProperties::<T>::get((collection.id, token_id))
+		let is_property_exists = TokenProperties::<T>::get((guard.collection.id, guard.token))
 			.get(property_key)
 			.is_some();
 
@@ -586,18 +568,18 @@
 				..
 			} => {
 				//TODO: investigate threats during public minting.
-				if is_token_create && (collection_admin || token_owner) {
+				if guard.is_token_create && (collection_admin || token_owner) {
 					return Ok(());
 				}
 
 				let mut check_result = Err(<CommonError<T>>::NoPermission.into());
 
 				if collection_admin {
-					check_result = collection.check_is_owner_or_admin(sender);
+					check_result = guard.check_collection_admin();
 				}
 
 				if token_owner {
-					check_result.or_else(|_| check_token_owner())
+					check_result.or_else(|_| guard.check_token_owner())
 				} else {
 					check_result
 				}
@@ -611,9 +593,20 @@
 		sender: &T::CrossAccountId,
 		token_id: TokenId,
 		property_keys: Vec<PropertyKey>,
+		nesting_budget: &dyn Budget,
 	) -> DispatchResult {
+		let is_token_create = false;
+
+		let mut guard = PropertyGuard::new(
+			sender,
+			collection,
+			token_id,
+			is_token_create,
+			nesting_budget,
+		);
+
 		for key in property_keys {
-			Self::delete_token_property(collection, sender, token_id, key)?;
+			Self::delete_token_property(key, &mut guard)?;
 		}
 
 		Ok(())
@@ -829,6 +822,7 @@
 					TokenId(token),
 					data.properties.clone().into_inner(),
 					true,
+					nesting_budget,
 				) {
 					return TransactionOutcome::Rollback(Err(e));
 				}
modifiedpallets/refungible/src/common.rsdiffbeforeafterboth
--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -314,6 +314,7 @@
 		_sender: T::CrossAccountId,
 		_token_id: TokenId,
 		_property: Vec<Property>,
+		_budget: &dyn Budget,
 	) -> DispatchResultWithPostInfo {
 		fail!(<Error<T>>::SettingPropertiesNotAllowed)
 	}
@@ -331,6 +332,7 @@
 		_sender: T::CrossAccountId,
 		_token_id: TokenId,
 		_property_keys: Vec<PropertyKey>,
+		_budget: &dyn Budget,
 	) -> DispatchResultWithPostInfo {
 		fail!(<Error<T>>::SettingPropertiesNotAllowed)
 	}
modifiedpallets/unique/src/lib.rsdiffbeforeafterboth
654 ensure!(!properties.is_empty(), Error::<T>::EmptyArgument);654 ensure!(!properties.is_empty(), Error::<T>::EmptyArgument);
655655
656 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);656 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
657 let budget = budget::Value::new(NESTING_BUDGET);
657658
658 dispatch_tx::<T, _>(collection_id, |d| d.set_token_properties(sender, token_id, properties))659 dispatch_tx::<T, _>(collection_id, |d| d.set_token_properties(sender, token_id, properties, &budget))
659 }660 }
660661
661 #[weight = T::CommonWeightInfo::delete_token_properties(property_keys.len() as u32)]662 #[weight = T::CommonWeightInfo::delete_token_properties(property_keys.len() as u32)]
669 ensure!(!property_keys.is_empty(), Error::<T>::EmptyArgument);670 ensure!(!property_keys.is_empty(), Error::<T>::EmptyArgument);
670671
671 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);672 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
673 let budget = budget::Value::new(NESTING_BUDGET);
672674
673 dispatch_tx::<T, _>(collection_id, |d| d.delete_token_properties(sender, token_id, property_keys))675 dispatch_tx::<T, _>(collection_id, |d| d.delete_token_properties(sender, token_id, property_keys, &budget))
674 }676 }
675677
676 #[weight = T::CommonWeightInfo::set_token_property_permissions(property_permissions.len() as u32)]678 #[weight = T::CommonWeightInfo::set_token_property_permissions(property_permissions.len() as u32)]