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

difftreelog

fix find_parent

Daniel Shiposha2023-01-19parent: #030bc0d.patch.diff
in: master

8 files changed

modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -155,6 +155,11 @@
 }
 
 impl<T: Config> CollectionHandle<T> {
+	/// Get the mode of the collection: NFT/FT/RFT.
+	pub fn mode(&self) -> CollectionMode {
+		self.mode
+	}
+
 	/// Same as [CollectionHandle::new] but with an explicit gas limit.
 	pub fn new_with_gas_limit(id: CollectionId, gas_limit: u64) -> Option<Self> {
 		<CollectionById<T>>::get(id).map(|collection| Self {
@@ -1872,6 +1877,9 @@
 /// It wraps methods in Fungible, Nonfungible and Refungible pallets
 /// and adds weight info.
 pub trait CommonCollectionOperations<T: Config> {
+	/// Get the mode of the collection: NFT/FT/RFT.
+	fn mode(&self) -> CollectionMode;
+
 	/// Create token.
 	///
 	/// * `sender` - The user who mint the token and pays for the transaction.
modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -125,6 +125,10 @@
 /// Implementation of `CommonCollectionOperations` for `FungibleHandle`. It wraps FungibleHandle Pallete
 /// methods and adds weight info.
 impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {
+	fn mode(&self) -> up_data_structs::CollectionMode {
+		self.0.mode()
+	}
+
 	fn create_item(
 		&self,
 		sender: T::CrossAccountId,
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -152,6 +152,10 @@
 /// Implementation of `CommonCollectionOperations` for `NonfungibleHandle`. It wraps Nonfungible Pallete
 /// methods and adds weight info.
 impl<T: Config> CommonCollectionOperations<T> for NonfungibleHandle<T> {
+	fn mode(&self) -> up_data_structs::CollectionMode {
+		self.0.mode()
+	}
+
 	fn create_item(
 		&self,
 		sender: T::CrossAccountId,
modifiedpallets/proxy-rmrk-core/src/lib.rsdiffbeforeafterboth
742 &target_nft_budget,742 &target_nft_budget,
743 )743 )
744 .map_err(Self::map_unique_err_to_proxy)?;744 .map_err(Self::map_unique_err_to_proxy)?
745 .ok_or::<DispatchError>(<Error<T>>::NoPermission.into())?;
745746
746 approval_required = cross_sender != target_nft_owner;747 approval_required = cross_sender != target_nft_owner;
747748
990 let nft_owner =991 let nft_owner =
991 <PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)992 <PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)
992 .map_err(|_| <Error<T>>::ResourceDoesntExist)?;993 .map_err(|_| <Error<T>>::ResourceDoesntExist)?
994 .ok_or::<DispatchError>(<Error<T>>::NoPermission.into())?;
993995
994 Self::try_mutate_resource_info(collection_id, nft_id, resource_id, |res| {996 Self::try_mutate_resource_info(collection_id, nft_id, resource_id, |res| {
995 ensure!(res.pending, <Error<T>>::ResourceNotPending);997 ensure!(res.pending, <Error<T>>::ResourceNotPending);
1045 let nft_owner =1047 let nft_owner =
1046 <PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)1048 <PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)
1047 .map_err(|_| <Error<T>>::ResourceDoesntExist)?;1049 .map_err(|_| <Error<T>>::ResourceDoesntExist)?
1050 .ok_or::<DispatchError>(<Error<T>>::NoPermission.into())?;
10481051
1049 ensure!(cross_sender == nft_owner, <Error<T>>::NoPermission);1052 ensure!(cross_sender == nft_owner, <Error<T>>::NoPermission);
10501053
16671670
1668 let nft_owner = <PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)1671 let nft_owner = <PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)
1669 .map_err(Self::map_unique_err_to_proxy)?;1672 .map_err(Self::map_unique_err_to_proxy)?
1673 .ok_or::<DispatchError>(<Error<T>>::NoPermission.into())?;
16701674
1671 let pending = sender != nft_owner;1675 let pending = sender != nft_owner;
16721676
1721 let budget = up_data_structs::budget::Value::new(NESTING_BUDGET);1725 let budget = up_data_structs::budget::Value::new(NESTING_BUDGET);
1722 let topmost_owner =1726 let topmost_owner =
1723 <PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)?;1727 <PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)?
1728 .ok_or::<DispatchError>(<Error<T>>::NoPermission.into())?;
17241729
1725 let sender = T::CrossAccountId::from_sub(sender);1730 let sender = T::CrossAccountId::from_sub(sender);
1726 if topmost_owner == sender {1731 if topmost_owner == sender {
modifiedpallets/refungible/src/common.rsdiffbeforeafterboth
--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -186,6 +186,10 @@
 /// Implementation of `CommonCollectionOperations` for `RefungibleHandle`. It wraps Refungible Pallete
 /// methods and adds weight info.
 impl<T: Config> CommonCollectionOperations<T> for RefungibleHandle<T> {
+	fn mode(&self) -> up_data_structs::CollectionMode {
+		self.0.mode()
+	}
+
 	fn create_item(
 		&self,
 		sender: T::CrossAccountId,
modifiedpallets/structure/src/lib.rsdiffbeforeafterboth
--- a/pallets/structure/src/lib.rs
+++ b/pallets/structure/src/lib.rs
@@ -61,6 +61,7 @@
 use frame_support::fail;
 pub use pallet::*;
 use pallet_common::{dispatch::CollectionDispatch, CollectionHandle};
+use up_data_structs::CollectionMode;
 use up_data_structs::{CollectionId, TokenId, mapping::TokenAddressMapping, budget::Budget};
 
 #[cfg(feature = "runtime-benchmarks")]
@@ -135,6 +136,8 @@
 	User(CrossAccountId),
 	/// Could not find the token provided as the owner.
 	TokenNotFound,
+	/// Nested token has multiple owners.
+	MultipleOwners,
 	/// Token owner is another token (still, the target token may not exist).
 	Token(CollectionId, TokenId),
 }
@@ -163,6 +166,10 @@
 				Some((collection, token)) => Parent::Token(collection, token),
 				None => Parent::User(owner),
 			},
+			None if handle.mode() == CollectionMode::ReFungible => handle
+				.total_pieces(token)
+				.map(|_| Parent::MultipleOwners)
+				.unwrap_or(Parent::TokenNotFound),
 			None => Parent::TokenNotFound,
 		})
 	}
@@ -203,19 +210,27 @@
 	///
 	/// May return token address if parent token not yet exists
 	///
+	/// Returns `None` if the token has multiple owners.
+	///
 	/// - `budget`: Limit for searching parents in depth.
 	pub fn find_topmost_owner(
 		collection: CollectionId,
 		token: TokenId,
 		budget: &dyn Budget,
-	) -> Result<T::CrossAccountId, DispatchError> {
+	) -> Result<Option<T::CrossAccountId>, DispatchError> {
 		let owner = Self::parent_chain(collection, token)
 			.take_while(|_| budget.consume())
-			.find(|p| matches!(p, Ok(Parent::User(_) | Parent::TokenNotFound)))
+			.find(|p| {
+				matches!(
+					p,
+					Ok(Parent::User(_) | Parent::TokenNotFound | Parent::MultipleOwners)
+				)
+			})
 			.ok_or(<Error<T>>::DepthLimit)??;
 
 		Ok(match owner {
-			Parent::User(v) => v,
+			Parent::User(v) => Some(v),
+			Parent::MultipleOwners => None,
 			_ => fail!(<Error<T>>::TokenNotFound),
 		})
 	}
@@ -223,13 +238,15 @@
 	/// Find the topmost parent and check that assigning `for_nest` token as a child for
 	/// `token` wouldn't create a cycle.
 	///
+	/// Returns `None` if the token has multiple owners.
+	///
 	/// - `budget`: Limit for searching parents in depth.
 	pub fn get_checked_topmost_owner(
 		collection: CollectionId,
 		token: TokenId,
 		for_nest: Option<(CollectionId, TokenId)>,
 		budget: &dyn Budget,
-	) -> Result<T::CrossAccountId, DispatchError> {
+	) -> Result<Option<T::CrossAccountId>, DispatchError> {
 		// Tried to nest token in itself
 		if Some((collection, token)) == for_nest {
 			return Err(<Error<T>>::OuroborosDetected.into());
@@ -242,8 +259,9 @@
 					return Err(<Error<T>>::OuroborosDetected.into())
 				}
 				// Token is owned by other user
-				Parent::User(user) => return Ok(user),
+				Parent::User(user) => return Ok(Some(user)),
 				Parent::TokenNotFound => return Err(<Error<T>>::TokenNotFound.into()),
+				Parent::MultipleOwners => return Ok(None),
 				// Continue parent chain
 				Parent::Token(_, _) => {}
 			}
@@ -284,12 +302,17 @@
 		budget: &dyn Budget,
 	) -> Result<bool, DispatchError> {
 		let target_parent = match T::CrossTokenAddressMapping::address_to_token(&user) {
-			Some((collection, token)) => Self::find_topmost_owner(collection, token, budget)?,
+			Some((collection, token)) => match Self::find_topmost_owner(collection, token, budget)?
+			{
+				Some(topmost_owner) => topmost_owner,
+				None => return Ok(false),
+			},
 			None => user,
 		};
 
-		Self::get_checked_topmost_owner(collection, token, for_nest, budget)
-			.map(|indirect_owner| indirect_owner == target_parent)
+		Self::get_checked_topmost_owner(collection, token, for_nest, budget).map(|indirect_owner| {
+			indirect_owner.map_or(false, |indirect_owner| indirect_owner == target_parent)
+		})
 	}
 
 	/// Checks that `under` is valid token and that `token_id` could be nested under it
modifiedprimitives/data-structs/src/lib.rsdiffbeforeafterboth
--- a/primitives/data-structs/src/lib.rs
+++ b/primitives/data-structs/src/lib.rs
@@ -252,7 +252,7 @@
 /// Collection can represent various types of tokens.
 /// Each collection can contain only one type of tokens at a time.
 /// This type helps to understand which tokens the collection contains.
-#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)]
+#[derive(Encode, Decode, Eq, Debug, Clone, Copy, PartialEq, TypeInfo, MaxEncodedLen)]
 #[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
 pub enum CollectionMode {
 	/// Non fungible tokens.
modifiedruntime/common/runtime_apis.rsdiffbeforeafterboth
--- a/runtime/common/runtime_apis.rs
+++ b/runtime/common/runtime_apis.rs
@@ -83,7 +83,7 @@
                 fn topmost_token_owner(collection: CollectionId, token: TokenId) -> Result<Option<CrossAccountId>, DispatchError> {
                     let budget = up_data_structs::budget::Value::new(10);
 
-                    Ok(Some(<pallet_structure::Pallet<Runtime>>::find_topmost_owner(collection, token, &budget)?))
+                    Ok(<pallet_structure::Pallet<Runtime>>::find_topmost_owner(collection, token, &budget)?)
                 }
                 fn token_children(collection: CollectionId, token: TokenId) -> Result<Vec<TokenChild>, DispatchError> {
                     Ok(<pallet_nonfungible::Pallet<Runtime>>::token_children_ids(collection, token))