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
155}155}
156156
157impl<T: Config> CollectionHandle<T> {157impl<T: Config> CollectionHandle<T> {
158 /// Get the mode of the collection: NFT/FT/RFT.
159 pub fn mode(&self) -> CollectionMode {
160 self.mode
161 }
162
158 /// Same as [CollectionHandle::new] but with an explicit gas limit.163 /// Same as [CollectionHandle::new] but with an explicit gas limit.
159 pub fn new_with_gas_limit(id: CollectionId, gas_limit: u64) -> Option<Self> {164 pub fn new_with_gas_limit(id: CollectionId, gas_limit: u64) -> Option<Self> {
1872/// It wraps methods in Fungible, Nonfungible and Refungible pallets1877/// It wraps methods in Fungible, Nonfungible and Refungible pallets
1873/// and adds weight info.1878/// and adds weight info.
1874pub trait CommonCollectionOperations<T: Config> {1879pub trait CommonCollectionOperations<T: Config> {
1880 /// Get the mode of the collection: NFT/FT/RFT.
1881 fn mode(&self) -> CollectionMode;
1882
1875 /// Create token.1883 /// Create token.
1876 ///1884 ///
modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
125/// Implementation of `CommonCollectionOperations` for `FungibleHandle`. It wraps FungibleHandle Pallete125/// Implementation of `CommonCollectionOperations` for `FungibleHandle`. It wraps FungibleHandle Pallete
126/// methods and adds weight info.126/// methods and adds weight info.
127impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {127impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {
128 fn mode(&self) -> up_data_structs::CollectionMode {
129 self.0.mode()
130 }
131
128 fn create_item(132 fn create_item(
129 &self,133 &self,
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
152/// Implementation of `CommonCollectionOperations` for `NonfungibleHandle`. It wraps Nonfungible Pallete152/// Implementation of `CommonCollectionOperations` for `NonfungibleHandle`. It wraps Nonfungible Pallete
153/// methods and adds weight info.153/// methods and adds weight info.
154impl<T: Config> CommonCollectionOperations<T> for NonfungibleHandle<T> {154impl<T: Config> CommonCollectionOperations<T> for NonfungibleHandle<T> {
155 fn mode(&self) -> up_data_structs::CollectionMode {
156 self.0.mode()
157 }
158
155 fn create_item(159 fn create_item(
156 &self,160 &self,
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
186/// Implementation of `CommonCollectionOperations` for `RefungibleHandle`. It wraps Refungible Pallete186/// Implementation of `CommonCollectionOperations` for `RefungibleHandle`. It wraps Refungible Pallete
187/// methods and adds weight info.187/// methods and adds weight info.
188impl<T: Config> CommonCollectionOperations<T> for RefungibleHandle<T> {188impl<T: Config> CommonCollectionOperations<T> for RefungibleHandle<T> {
189 fn mode(&self) -> up_data_structs::CollectionMode {
190 self.0.mode()
191 }
192
189 fn create_item(193 fn create_item(
190 &self,194 &self,
modifiedpallets/structure/src/lib.rsdiffbeforeafterboth
61use 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;
64use up_data_structs::{CollectionId, TokenId, mapping::TokenAddressMapping, budget::Budget};65use up_data_structs::{CollectionId, TokenId, mapping::TokenAddressMapping, budget::Budget};
6566
66#[cfg(feature = "runtime-benchmarks")]67#[cfg(feature = "runtime-benchmarks")]
135 User(CrossAccountId),136 User(CrossAccountId),
136 /// Could not find the token provided as the owner.137 /// Could not find the token provided as the owner.
137 TokenNotFound,138 TokenNotFound,
139 /// Nested token has multiple owners.
140 MultipleOwners,
138 /// Token owner is another token (still, the target token may not exist).141 /// Token owner is another token (still, the target token may not exist).
139 Token(CollectionId, TokenId),142 Token(CollectionId, TokenId),
140}143}
163 Some((collection, token)) => Parent::Token(collection, token),166 Some((collection, token)) => Parent::Token(collection, token),
164 None => Parent::User(owner),167 None => Parent::User(owner),
165 },168 },
169 None if handle.mode() == CollectionMode::ReFungible => handle
170 .total_pieces(token)
171 .map(|_| Parent::MultipleOwners)
172 .unwrap_or(Parent::TokenNotFound),
166 None => Parent::TokenNotFound,173 None => Parent::TokenNotFound,
167 })174 })
168 }175 }
202 /// Try to dereference address, until finding top level owner209 /// Try to dereference address, until finding top level owner
203 ///210 ///
204 /// May return token address if parent token not yet exists211 /// May return token address if parent token not yet exists
212 ///
213 /// Returns `None` if the token has multiple owners.
205 ///214 ///
206 /// - `budget`: Limit for searching parents in depth.215 /// - `budget`: Limit for searching parents in depth.
207 pub fn find_topmost_owner(216 pub fn find_topmost_owner(
208 collection: CollectionId,217 collection: CollectionId,
209 token: TokenId,218 token: TokenId,
210 budget: &dyn Budget,219 budget: &dyn Budget,
211 ) -> Result<T::CrossAccountId, DispatchError> {220 ) -> Result<Option<T::CrossAccountId>, DispatchError> {
212 let owner = Self::parent_chain(collection, token)221 let owner = Self::parent_chain(collection, token)
213 .take_while(|_| budget.consume())222 .take_while(|_| budget.consume())
214 .find(|p| matches!(p, Ok(Parent::User(_) | Parent::TokenNotFound)))223 .find(|p| {
224 matches!(
225 p,
226 Ok(Parent::User(_) | Parent::TokenNotFound | Parent::MultipleOwners)
227 )
228 })
215 .ok_or(<Error<T>>::DepthLimit)??;229 .ok_or(<Error<T>>::DepthLimit)??;
216230
217 Ok(match owner {231 Ok(match owner {
218 Parent::User(v) => v,232 Parent::User(v) => Some(v),
233 Parent::MultipleOwners => None,
219 _ => fail!(<Error<T>>::TokenNotFound),234 _ => fail!(<Error<T>>::TokenNotFound),
220 })235 })
221 }236 }
222237
223 /// Find the topmost parent and check that assigning `for_nest` token as a child for238 /// Find the topmost parent and check that assigning `for_nest` token as a child for
224 /// `token` wouldn't create a cycle.239 /// `token` wouldn't create a cycle.
240 ///
241 /// Returns `None` if the token has multiple owners.
225 ///242 ///
226 /// - `budget`: Limit for searching parents in depth.243 /// - `budget`: Limit for searching parents in depth.
227 pub fn get_checked_topmost_owner(244 pub fn get_checked_topmost_owner(
228 collection: CollectionId,245 collection: CollectionId,
229 token: TokenId,246 token: TokenId,
230 for_nest: Option<(CollectionId, TokenId)>,247 for_nest: Option<(CollectionId, TokenId)>,
231 budget: &dyn Budget,248 budget: &dyn Budget,
232 ) -> Result<T::CrossAccountId, DispatchError> {249 ) -> Result<Option<T::CrossAccountId>, DispatchError> {
233 // Tried to nest token in itself250 // Tried to nest token in itself
234 if Some((collection, token)) == for_nest {251 if Some((collection, token)) == for_nest {
235 return Err(<Error<T>>::OuroborosDetected.into());252 return Err(<Error<T>>::OuroborosDetected.into());
242 return Err(<Error<T>>::OuroborosDetected.into())259 return Err(<Error<T>>::OuroborosDetected.into())
243 }260 }
244 // Token is owned by other user261 // Token is owned by other user
245 Parent::User(user) => return Ok(user),262 Parent::User(user) => return Ok(Some(user)),
246 Parent::TokenNotFound => return Err(<Error<T>>::TokenNotFound.into()),263 Parent::TokenNotFound => return Err(<Error<T>>::TokenNotFound.into()),
264 Parent::MultipleOwners => return Ok(None),
247 // Continue parent chain265 // Continue parent chain
248 Parent::Token(_, _) => {}266 Parent::Token(_, _) => {}
249 }267 }
284 budget: &dyn Budget,302 budget: &dyn Budget,
285 ) -> Result<bool, DispatchError> {303 ) -> Result<bool, DispatchError> {
286 let target_parent = match T::CrossTokenAddressMapping::address_to_token(&user) {304 let target_parent = match T::CrossTokenAddressMapping::address_to_token(&user) {
287 Some((collection, token)) => Self::find_topmost_owner(collection, token, budget)?,305 Some((collection, token)) => match Self::find_topmost_owner(collection, token, budget)?
306 {
307 Some(topmost_owner) => topmost_owner,
308 None => return Ok(false),
309 },
288 None => user,310 None => user,
289 };311 };
290312
291 Self::get_checked_topmost_owner(collection, token, for_nest, budget)313 Self::get_checked_topmost_owner(collection, token, for_nest, budget).map(|indirect_owner| {
292 .map(|indirect_owner| indirect_owner == target_parent)314 indirect_owner.map_or(false, |indirect_owner| indirect_owner == target_parent)
315 })
293 }316 }
294317
295 /// Checks that `under` is valid token and that `token_id` could be nested under it318 /// Checks that `under` is valid token and that `token_id` could be nested under it
modifiedprimitives/data-structs/src/lib.rsdiffbeforeafterboth
252/// Collection can represent various types of tokens.252/// Collection can represent various types of tokens.
253/// Each collection can contain only one type of tokens at a time.253/// Each collection can contain only one type of tokens at a time.
254/// This type helps to understand which tokens the collection contains.254/// This type helps to understand which tokens the collection contains.
255#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)]255#[derive(Encode, Decode, Eq, Debug, Clone, Copy, PartialEq, TypeInfo, MaxEncodedLen)]
256#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]256#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
257pub enum CollectionMode {257pub enum CollectionMode {
258 /// Non fungible tokens.258 /// Non fungible tokens.
modifiedruntime/common/runtime_apis.rsdiffbeforeafterboth
83 fn topmost_token_owner(collection: CollectionId, token: TokenId) -> Result<Option<CrossAccountId>, DispatchError> {83 fn topmost_token_owner(collection: CollectionId, token: TokenId) -> Result<Option<CrossAccountId>, DispatchError> {
84 let budget = up_data_structs::budget::Value::new(10);84 let budget = up_data_structs::budget::Value::new(10);
8585
86 Ok(Some(<pallet_structure::Pallet<Runtime>>::find_topmost_owner(collection, token, &budget)?))86 Ok(<pallet_structure::Pallet<Runtime>>::find_topmost_owner(collection, token, &budget)?)
87 }87 }
88 fn token_children(collection: CollectionId, token: TokenId) -> Result<Vec<TokenChild>, DispatchError> {88 fn token_children(collection: CollectionId, token: TokenId) -> Result<Vec<TokenChild>, DispatchError> {
89 Ok(<pallet_nonfungible::Pallet<Runtime>>::token_children_ids(collection, token))89 Ok(<pallet_nonfungible::Pallet<Runtime>>::token_children_ids(collection, token))