difftreelog
Revert "fix: find_parent"
in: master
This reverts commit e0035410299d589d1232ce7c17dfd86b7d8a3f45.
8 files changed
pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -155,11 +155,6 @@
}
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 {
@@ -1877,9 +1872,6 @@
/// 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.
pallets/fungible/src/common.rsdiffbeforeafterboth--- 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<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {
- fn mode(&self) -> up_data_structs::CollectionMode {
- self.0.mode()
- }
-
fn create_item(
&self,
sender: T::CrossAccountId,
pallets/nonfungible/src/common.rsdiffbeforeafterboth--- 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<T: Config> CommonCollectionOperations<T> for NonfungibleHandle<T> {
- fn mode(&self) -> up_data_structs::CollectionMode {
- self.0.mode()
- }
-
fn create_item(
&self,
sender: T::CrossAccountId,
pallets/proxy-rmrk-core/src/lib.rsdiffbeforeafterboth--- 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::<DispatchError>(<Error<T>>::NoPermission.into())?;
+ .map_err(Self::map_unique_err_to_proxy)?;
approval_required = cross_sender != target_nft_owner;
@@ -990,8 +989,7 @@
let nft_owner =
<PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)
- .map_err(|_| <Error<T>>::ResourceDoesntExist)?
- .ok_or::<DispatchError>(<Error<T>>::NoPermission.into())?;
+ .map_err(|_| <Error<T>>::ResourceDoesntExist)?;
Self::try_mutate_resource_info(collection_id, nft_id, resource_id, |res| {
ensure!(res.pending, <Error<T>>::ResourceNotPending);
@@ -1046,8 +1044,7 @@
let nft_owner =
<PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)
- .map_err(|_| <Error<T>>::ResourceDoesntExist)?
- .ok_or::<DispatchError>(<Error<T>>::NoPermission.into())?;
+ .map_err(|_| <Error<T>>::ResourceDoesntExist)?;
ensure!(cross_sender == nft_owner, <Error<T>>::NoPermission);
@@ -1669,8 +1666,7 @@
let budget = budget::Value::new(NESTING_BUDGET);
let nft_owner = <PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)
- .map_err(Self::map_unique_err_to_proxy)?
- .ok_or::<DispatchError>(<Error<T>>::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 =
- <PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)?
- .ok_or::<DispatchError>(<Error<T>>::NoPermission.into())?;
+ <PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)?;
let sender = T::CrossAccountId::from_sub(sender);
if topmost_owner == sender {
pallets/refungible/src/common.rsdiffbeforeafterboth--- 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<T: Config> CommonCollectionOperations<T> for RefungibleHandle<T> {
- fn mode(&self) -> up_data_structs::CollectionMode {
- self.0.mode()
- }
-
fn create_item(
&self,
sender: T::CrossAccountId,
pallets/structure/src/lib.rsdiffbeforeafterboth61use frame_support::fail;61use frame_support::fail;62pub use pallet::*;62pub use pallet::*;63use pallet_common::{dispatch::CollectionDispatch, CollectionHandle};63use pallet_common::{dispatch::CollectionDispatch, CollectionHandle};64use up_data_structs::CollectionMode;65use up_data_structs::{CollectionId, TokenId, mapping::TokenAddressMapping, budget::Budget};64use up_data_structs::{CollectionId, TokenId, mapping::TokenAddressMapping, budget::Budget};666567#[cfg(feature = "runtime-benchmarks")]66#[cfg(feature = "runtime-benchmarks")]136 User(CrossAccountId),135 User(CrossAccountId),137 /// Could not find the token provided as the owner.136 /// Could not find the token provided as the owner.138 TokenNotFound,137 TokenNotFound,139 /// Nested token has multiple owners.140 MultipleOwners,141 /// Token owner is another token (still, the target token may not exist).138 /// Token owner is another token (still, the target token may not exist).142 Token(CollectionId, TokenId),139 Token(CollectionId, TokenId),143}140}166 Some((collection, token)) => Parent::Token(collection, token),163 Some((collection, token)) => Parent::Token(collection, token),167 None => Parent::User(owner),164 None => Parent::User(owner),168 },165 },169 None if handle.mode() == CollectionMode::ReFungible => handle170 .total_pieces(token)171 .map(|_| Parent::MultipleOwners)172 .unwrap_or(Parent::TokenNotFound),173 None => Parent::TokenNotFound,166 None => Parent::TokenNotFound,174 })167 })175 }168 }209 /// Try to dereference address, until finding top level owner202 /// Try to dereference address, until finding top level owner210 ///203 ///211 /// May return token address if parent token not yet exists204 /// May return token address if parent token not yet exists212 ///213 /// Returns `None` if the token has multiple owners.214 ///205 ///215 /// - `budget`: Limit for searching parents in depth.206 /// - `budget`: Limit for searching parents in depth.216 pub fn find_topmost_owner(207 pub fn find_topmost_owner(217 collection: CollectionId,208 collection: CollectionId,218 token: TokenId,209 token: TokenId,219 budget: &dyn Budget,210 budget: &dyn Budget,220 ) -> Result<Option<T::CrossAccountId>, DispatchError> {211 ) -> Result<T::CrossAccountId, DispatchError> {221 let owner = Self::parent_chain(collection, token)212 let owner = Self::parent_chain(collection, token)222 .take_while(|_| budget.consume())213 .take_while(|_| budget.consume())223 .find(|p| {214 .find(|p| matches!(p, Ok(Parent::User(_) | Parent::TokenNotFound)))224 matches!(225 p,226 Ok(Parent::User(_) | Parent::TokenNotFound | Parent::MultipleOwners)227 )228 })229 .ok_or(<Error<T>>::DepthLimit)??;215 .ok_or(<Error<T>>::DepthLimit)??;230216231 Ok(match owner {217 Ok(match owner {232 Parent::User(v) => Some(v),218 Parent::User(v) => v,233 Parent::MultipleOwners => None,234 _ => fail!(<Error<T>>::TokenNotFound),219 _ => fail!(<Error<T>>::TokenNotFound),235 })220 })236 }221 }237222238 /// Find the topmost parent and check that assigning `for_nest` token as a child for223 /// Find the topmost parent and check that assigning `for_nest` token as a child for239 /// `token` wouldn't create a cycle.224 /// `token` wouldn't create a cycle.240 ///241 /// Returns `None` if the token has multiple owners.242 ///225 ///243 /// - `budget`: Limit for searching parents in depth.226 /// - `budget`: Limit for searching parents in depth.244 pub fn get_checked_topmost_owner(227 pub fn get_checked_topmost_owner(245 collection: CollectionId,228 collection: CollectionId,246 token: TokenId,229 token: TokenId,247 for_nest: Option<(CollectionId, TokenId)>,230 for_nest: Option<(CollectionId, TokenId)>,248 budget: &dyn Budget,231 budget: &dyn Budget,249 ) -> Result<Option<T::CrossAccountId>, DispatchError> {232 ) -> Result<T::CrossAccountId, DispatchError> {250 // Tried to nest token in itself233 // Tried to nest token in itself251 if Some((collection, token)) == for_nest {234 if Some((collection, token)) == for_nest {252 return Err(<Error<T>>::OuroborosDetected.into());235 return Err(<Error<T>>::OuroborosDetected.into());259 return Err(<Error<T>>::OuroborosDetected.into())242 return Err(<Error<T>>::OuroborosDetected.into())260 }243 }261 // Token is owned by other user244 // Token is owned by other user262 Parent::User(user) => return Ok(Some(user)),245 Parent::User(user) => return Ok(user),263 Parent::TokenNotFound => return Err(<Error<T>>::TokenNotFound.into()),246 Parent::TokenNotFound => return Err(<Error<T>>::TokenNotFound.into()),264 Parent::MultipleOwners => return Ok(None),265 // Continue parent chain247 // Continue parent chain266 Parent::Token(_, _) => {}248 Parent::Token(_, _) => {}267 }249 }302 budget: &dyn Budget,284 budget: &dyn Budget,303 ) -> Result<bool, DispatchError> {285 ) -> Result<bool, DispatchError> {304 let target_parent = match T::CrossTokenAddressMapping::address_to_token(&user) {286 let target_parent = match T::CrossTokenAddressMapping::address_to_token(&user) {305 Some((collection, token)) => match Self::find_topmost_owner(collection, token, budget)?287 Some((collection, token)) => Self::find_topmost_owner(collection, token, budget)?,306 {307 Some(topmost_owner) => topmost_owner,308 None => return Ok(false),309 },310 None => user,288 None => user,311 };289 };312290313 Self::get_checked_topmost_owner(collection, token, for_nest, budget).map(|indirect_owner| {291 Self::get_checked_topmost_owner(collection, token, for_nest, budget)314 indirect_owner.map_or(false, |indirect_owner| indirect_owner == target_parent)292 .map(|indirect_owner| indirect_owner == target_parent)315 })316 }293 }317294318 /// Checks that `under` is valid token and that `token_id` could be nested under it295 /// Checks that `under` is valid token and that `token_id` could be nested under itprimitives/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, 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.
runtime/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(<pallet_structure::Pallet<Runtime>>::find_topmost_owner(collection, token, &budget)?)
+ Ok(Some(<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))