From 6a7ab3b8105595c418a56338f2faa7faf1292d7f Mon Sep 17 00:00:00 2001 From: Daniel Shiposha Date: Thu, 19 Jan 2023 17:01:55 +0000 Subject: [PATCH] Revert "fix: find_parent" This reverts commit e0035410299d589d1232ce7c17dfd86b7d8a3f45. --- --- a/pallets/common/src/lib.rs +++ b/pallets/common/src/lib.rs @@ -155,11 +155,6 @@ } impl CollectionHandle { - /// 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 { >::get(id).map(|collection| Self { @@ -1877,9 +1872,6 @@ /// It wraps methods in Fungible, Nonfungible and Refungible pallets /// and adds weight info. pub trait CommonCollectionOperations { - /// 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. --- a/pallets/fungible/src/common.rs +++ b/pallets/fungible/src/common.rs @@ -125,10 +125,6 @@ /// Implementation of `CommonCollectionOperations` for `FungibleHandle`. It wraps FungibleHandle Pallete /// methods and adds weight info. impl CommonCollectionOperations for FungibleHandle { - fn mode(&self) -> up_data_structs::CollectionMode { - self.0.mode() - } - fn create_item( &self, sender: T::CrossAccountId, --- a/pallets/nonfungible/src/common.rs +++ b/pallets/nonfungible/src/common.rs @@ -152,10 +152,6 @@ /// Implementation of `CommonCollectionOperations` for `NonfungibleHandle`. It wraps Nonfungible Pallete /// methods and adds weight info. impl CommonCollectionOperations for NonfungibleHandle { - fn mode(&self) -> up_data_structs::CollectionMode { - self.0.mode() - } - fn create_item( &self, sender: T::CrossAccountId, --- a/pallets/proxy-rmrk-core/src/lib.rs +++ b/pallets/proxy-rmrk-core/src/lib.rs @@ -741,8 +741,7 @@ Some((collection_id, nft_id)), &target_nft_budget, ) - .map_err(Self::map_unique_err_to_proxy)? - .ok_or::(>::NoPermission.into())?; + .map_err(Self::map_unique_err_to_proxy)?; approval_required = cross_sender != target_nft_owner; @@ -990,8 +989,7 @@ let nft_owner = >::find_topmost_owner(collection_id, nft_id, &budget) - .map_err(|_| >::ResourceDoesntExist)? - .ok_or::(>::NoPermission.into())?; + .map_err(|_| >::ResourceDoesntExist)?; Self::try_mutate_resource_info(collection_id, nft_id, resource_id, |res| { ensure!(res.pending, >::ResourceNotPending); @@ -1046,8 +1044,7 @@ let nft_owner = >::find_topmost_owner(collection_id, nft_id, &budget) - .map_err(|_| >::ResourceDoesntExist)? - .ok_or::(>::NoPermission.into())?; + .map_err(|_| >::ResourceDoesntExist)?; ensure!(cross_sender == nft_owner, >::NoPermission); @@ -1669,8 +1666,7 @@ let budget = budget::Value::new(NESTING_BUDGET); let nft_owner = >::find_topmost_owner(collection_id, nft_id, &budget) - .map_err(Self::map_unique_err_to_proxy)? - .ok_or::(>::NoPermission.into())?; + .map_err(Self::map_unique_err_to_proxy)?; let pending = sender != nft_owner; @@ -1724,8 +1720,7 @@ let budget = up_data_structs::budget::Value::new(NESTING_BUDGET); let topmost_owner = - >::find_topmost_owner(collection_id, nft_id, &budget)? - .ok_or::(>::NoPermission.into())?; + >::find_topmost_owner(collection_id, nft_id, &budget)?; let sender = T::CrossAccountId::from_sub(sender); if topmost_owner == sender { --- a/pallets/refungible/src/common.rs +++ b/pallets/refungible/src/common.rs @@ -186,10 +186,6 @@ /// Implementation of `CommonCollectionOperations` for `RefungibleHandle`. It wraps Refungible Pallete /// methods and adds weight info. impl CommonCollectionOperations for RefungibleHandle { - fn mode(&self) -> up_data_structs::CollectionMode { - self.0.mode() - } - fn create_item( &self, sender: T::CrossAccountId, --- a/pallets/structure/src/lib.rs +++ b/pallets/structure/src/lib.rs @@ -61,7 +61,6 @@ 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")] @@ -136,8 +135,6 @@ 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), } @@ -166,10 +163,6 @@ 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, }) } @@ -210,35 +203,25 @@ /// /// 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, DispatchError> { + ) -> Result { let owner = Self::parent_chain(collection, token) .take_while(|_| budget.consume()) - .find(|p| { - matches!( - p, - Ok(Parent::User(_) | Parent::TokenNotFound | Parent::MultipleOwners) - ) - }) + .find(|p| matches!(p, Ok(Parent::User(_) | Parent::TokenNotFound))) .ok_or(>::DepthLimit)??; Ok(match owner { - Parent::User(v) => Some(v), - Parent::MultipleOwners => None, + Parent::User(v) => v, _ => fail!(>::TokenNotFound), }) } /// 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( @@ -246,7 +229,7 @@ token: TokenId, for_nest: Option<(CollectionId, TokenId)>, budget: &dyn Budget, - ) -> Result, DispatchError> { + ) -> Result { // Tried to nest token in itself if Some((collection, token)) == for_nest { return Err(>::OuroborosDetected.into()); @@ -259,9 +242,8 @@ return Err(>::OuroborosDetected.into()) } // Token is owned by other user - Parent::User(user) => return Ok(Some(user)), + Parent::User(user) => return Ok(user), Parent::TokenNotFound => return Err(>::TokenNotFound.into()), - Parent::MultipleOwners => return Ok(None), // Continue parent chain Parent::Token(_, _) => {} } @@ -302,17 +284,12 @@ budget: &dyn Budget, ) -> Result { let target_parent = match T::CrossTokenAddressMapping::address_to_token(&user) { - Some((collection, token)) => match Self::find_topmost_owner(collection, token, budget)? - { - Some(topmost_owner) => topmost_owner, - None => return Ok(false), - }, + Some((collection, token)) => Self::find_topmost_owner(collection, token, budget)?, None => user, }; - Self::get_checked_topmost_owner(collection, token, for_nest, budget).map(|indirect_owner| { - indirect_owner.map_or(false, |indirect_owner| indirect_owner == target_parent) - }) + Self::get_checked_topmost_owner(collection, token, for_nest, budget) + .map(|indirect_owner| indirect_owner == target_parent) } /// Checks that `under` is valid token and that `token_id` could be nested under it --- 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, Copy, PartialEq, TypeInfo, MaxEncodedLen)] +#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)] #[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub enum CollectionMode { /// Non fungible tokens. --- 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, DispatchError> { let budget = up_data_structs::budget::Value::new(10); - Ok(>::find_topmost_owner(collection, token, &budget)?) + Ok(Some(>::find_topmost_owner(collection, token, &budget)?)) } fn token_children(collection: CollectionId, token: TokenId) -> Result, DispatchError> { Ok(>::token_children_ids(collection, token)) -- gitstuff