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
52pub mod erc;52pub mod erc;
53pub mod weights;53pub mod weights;
54
55mod property_guard;
56
57use property_guard::*;
5458
55pub type CreateItemData<T> = CreateNftExData<<T as pallet_evm::account::Config>::CrossAccountId>;59pub type CreateItemData<T> = CreateNftExData<<T as pallet_evm::account::Config>::CrossAccountId>;
56pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;60pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;
484 })488 })
485 }489 }
486490
487 pub fn set_token_property(491 pub fn set_token_property(property: Property, guard: &mut PropertyGuard<'_, T>) -> DispatchResult {
488 collection: &NonfungibleHandle<T>,
489 sender: &T::CrossAccountId,
490 token_id: TokenId,
491 property: Property,
492 is_token_create: bool,
493 ) -> DispatchResult {
494 Self::check_token_change_permission(492 Self::check_token_change_permission(&property.key, guard)?;
495 collection,
496 sender,
497 token_id,
498 &property.key,
499 is_token_create,
500 )?;
501493
502 <TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {494 <TokenProperties<T>>::try_mutate((guard.collection.id, guard.token), |properties| {
503 let property = property.clone();495 let property = property.clone();
504 properties.try_set(property.key, property.value)496 properties.try_set(property.key, property.value)
505 })497 })
506 .map_err(<CommonError<T>>::from)?;498 .map_err(<CommonError<T>>::from)?;
507499
508 <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertySet(500 <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertySet(
509 collection.id,501 guard.collection.id,
510 token_id,502 guard.token,
511 property.key,503 property.key,
512 ));504 ));
513505
521 token_id: TokenId,513 token_id: TokenId,
522 properties: Vec<Property>,514 properties: Vec<Property>,
523 is_token_create: bool,515 is_token_create: bool,
516 nesting_budget: &dyn Budget,
524 ) -> DispatchResult {517 ) -> DispatchResult {
518 let mut guard = PropertyGuard::new(
519 sender,
520 collection,
521 token_id,
522 is_token_create,
523 nesting_budget,
524 );
525
525 for property in properties {526 for property in properties {
526 Self::set_token_property(collection, sender, token_id, property, is_token_create)?;527 Self::set_token_property(property, &mut guard)?;
527 }528 }
528529
529 Ok(())530 Ok(())
530 }531 }
531532
532 pub fn delete_token_property(533 pub fn delete_token_property(property_key: PropertyKey, guard: &mut PropertyGuard<'_, T>) -> DispatchResult {
533 collection: &NonfungibleHandle<T>,
534 sender: &T::CrossAccountId,
535 token_id: TokenId,
536 property_key: PropertyKey,
537 ) -> DispatchResult {
538 Self::check_token_change_permission(collection, sender, token_id, &property_key, false)?;534 Self::check_token_change_permission(&property_key, guard)?;
539535
540 <TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {536 <TokenProperties<T>>::try_mutate((guard.collection.id, guard.token), |properties| {
541 properties.remove(&property_key)537 properties.remove(&property_key)
542 })538 })
543 .map_err(<CommonError<T>>::from)?;539 .map_err(<CommonError<T>>::from)?;
544540
545 <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertyDeleted(541 <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertyDeleted(
546 collection.id,542 guard.collection.id,
547 token_id,543 guard.token,
548 property_key,544 property_key,
549 ));545 ));
550546
551 Ok(())547 Ok(())
552 }548 }
553549
554 fn check_token_change_permission(550 fn check_token_change_permission(property_key: &PropertyKey, guard: &mut PropertyGuard<'_, T>) -> DispatchResult {
555 collection: &NonfungibleHandle<T>,
556 sender: &T::CrossAccountId,
557 token_id: TokenId,
558 property_key: &PropertyKey,
559 is_token_create: bool,
560 ) -> DispatchResult {
561 let permission = <PalletCommon<T>>::property_permissions(collection.id)551 let permission = <PalletCommon<T>>::property_permissions(guard.collection.id)
562 .get(property_key)552 .get(property_key)
563 .cloned()553 .cloned()
564 .unwrap_or_else(PropertyPermission::none);554 .unwrap_or_else(PropertyPermission::none);
565
566 let token_data = <TokenData<T>>::get((collection.id, token_id))
567 .ok_or(<CommonError<T>>::TokenNotFound)?;
568
569 let check_token_owner = || -> DispatchResult {
570 ensure!(&token_data.owner == sender, <CommonError<T>>::NoPermission);
571 Ok(())
572 };
573555
574 let is_property_exists = TokenProperties::<T>::get((collection.id, token_id))556 let is_property_exists = TokenProperties::<T>::get((guard.collection.id, guard.token))
575 .get(property_key)557 .get(property_key)
576 .is_some();558 .is_some();
577559
586 ..568 ..
587 } => {569 } => {
588 //TODO: investigate threats during public minting.570 //TODO: investigate threats during public minting.
589 if is_token_create && (collection_admin || token_owner) {571 if guard.is_token_create && (collection_admin || token_owner) {
590 return Ok(());572 return Ok(());
591 }573 }
592574
593 let mut check_result = Err(<CommonError<T>>::NoPermission.into());575 let mut check_result = Err(<CommonError<T>>::NoPermission.into());
594576
595 if collection_admin {577 if collection_admin {
596 check_result = collection.check_is_owner_or_admin(sender);578 check_result = guard.check_collection_admin();
597 }579 }
598580
599 if token_owner {581 if token_owner {
600 check_result.or_else(|_| check_token_owner())582 check_result.or_else(|_| guard.check_token_owner())
601 } else {583 } else {
602 check_result584 check_result
603 }585 }
611 sender: &T::CrossAccountId,593 sender: &T::CrossAccountId,
612 token_id: TokenId,594 token_id: TokenId,
613 property_keys: Vec<PropertyKey>,595 property_keys: Vec<PropertyKey>,
596 nesting_budget: &dyn Budget,
614 ) -> DispatchResult {597 ) -> DispatchResult {
598 let is_token_create = false;
599
600 let mut guard = PropertyGuard::new(
601 sender,
602 collection,
603 token_id,
604 is_token_create,
605 nesting_budget,
606 );
607
615 for key in property_keys {608 for key in property_keys {
616 Self::delete_token_property(collection, sender, token_id, key)?;609 Self::delete_token_property(key, &mut guard)?;
617 }610 }
618611
619 Ok(())612 Ok(())
829 TokenId(token),822 TokenId(token),
830 data.properties.clone().into_inner(),823 data.properties.clone().into_inner(),
831 true,824 true,
825 nesting_budget,
832 ) {826 ) {
833 return TransactionOutcome::Rollback(Err(e));827 return TransactionOutcome::Rollback(Err(e));
834 }828 }
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
--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -654,8 +654,9 @@
 			ensure!(!properties.is_empty(), Error::<T>::EmptyArgument);
 
 			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
+			let budget = budget::Value::new(NESTING_BUDGET);
 
-			dispatch_tx::<T, _>(collection_id, |d| d.set_token_properties(sender, token_id, properties))
+			dispatch_tx::<T, _>(collection_id, |d| d.set_token_properties(sender, token_id, properties, &budget))
 		}
 
 		#[weight = T::CommonWeightInfo::delete_token_properties(property_keys.len() as u32)]
@@ -669,8 +670,9 @@
 			ensure!(!property_keys.is_empty(), Error::<T>::EmptyArgument);
 
 			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
+			let budget = budget::Value::new(NESTING_BUDGET);
 
-			dispatch_tx::<T, _>(collection_id, |d| d.delete_token_properties(sender, token_id, property_keys))
+			dispatch_tx::<T, _>(collection_id, |d| d.delete_token_properties(sender, token_id, property_keys, &budget))
 		}
 
 		#[weight = T::CommonWeightInfo::set_token_property_permissions(property_permissions.len() as u32)]