git.delta.rocks / unique-network / refs/commits / 9d51561e5bd0

difftreelog

Revert "feat: burn children when destroying a collection"

Daniel Shiposha2022-05-27parent: #2c83d97.patch.diff
in: master
This reverts commit 4b7f4d90a16f3a5ab26bed0511dabae078429724.

12 files changed

modifiedpallets/common/src/dispatch.rsdiffbeforeafterboth
--- a/pallets/common/src/dispatch.rs
+++ b/pallets/common/src/dispatch.rs
@@ -6,7 +6,7 @@
 	weights::Pays,
 	traits::Get,
 };
-use up_data_structs::{CollectionId, CreateCollectionData, budget::Budget};
+use up_data_structs::{CollectionId, CreateCollectionData};
 
 use crate::{pallet::Config, CommonCollectionOperations, CollectionHandle};
 
@@ -57,11 +57,7 @@
 
 pub trait CollectionDispatch<T: Config> {
 	fn create(sender: T::AccountId, data: CreateCollectionData<T::AccountId>) -> DispatchResult;
-	fn destroy(
-		sender: T::CrossAccountId,
-		handle: CollectionHandle<T>,
-		nesting_budget: &dyn Budget,
-	) -> DispatchResult;
+	fn destroy(sender: T::CrossAccountId, handle: CollectionHandle<T>) -> DispatchResult;
 
 	fn dispatch(handle: CollectionHandle<T>) -> Self;
 	fn into_inner(self) -> CollectionHandle<T>;
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -1169,12 +1169,6 @@
 		token: TokenId,
 		amount: u128,
 	) -> DispatchResultWithPostInfo;
-	fn burn_item_unchecked(
-		&self,
-		owner: &T::CrossAccountId,
-		token: TokenId,
-		amount: u128,
-	) -> DispatchResult;
 	fn set_collection_properties(
 		&self,
 		sender: T::CrossAccountId,
modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -170,17 +170,6 @@
 		)
 	}
 
-	fn burn_item_unchecked(
-		&self,
-		owner: &T::CrossAccountId,
-		_token: TokenId,
-		amount: u128,
-	) -> sp_runtime::DispatchResult {
-		<Pallet<T>>::burn_item_unchecked(self, owner, amount)?;
-
-		Ok(())
-	}
-
 	fn transfer(
 		&self,
 		from: T::CrossAccountId,
modifiedpallets/fungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/fungible/src/lib.rs
+++ b/pallets/fungible/src/lib.rs
@@ -160,36 +160,6 @@
 		owner: &T::CrossAccountId,
 		amount: u128,
 	) -> DispatchResult {
-		if collection.access == AccessMode::AllowList {
-			collection.check_allowlist(owner)?;
-		}
-
-		// =========
-
-		Self::burn_item_unchecked(collection, owner, amount)?;
-
-		<PalletEvm<T>>::deposit_log(
-			ERC20Events::Transfer {
-				from: *owner.as_eth(),
-				to: H160::default(),
-				value: amount.into(),
-			}
-			.to_log(collection_id_to_address(collection.id)),
-		);
-		<PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(
-			collection.id,
-			TokenId::default(),
-			owner.clone(),
-			amount,
-		));
-		Ok(())
-	}
-
-	pub fn burn_item_unchecked(
-		collection: &FungibleHandle<T>,
-		owner: &T::CrossAccountId,
-		amount: u128,
-	) -> DispatchResult {
 		let total_supply = <TotalSupply<T>>::get(collection.id)
 			.checked_sub(amount)
 			.ok_or(<CommonError<T>>::TokenValueTooLow)?;
@@ -216,6 +186,20 @@
 		}
 		<TotalSupply<T>>::insert(collection.id, total_supply);
 
+		<PalletEvm<T>>::deposit_log(
+			ERC20Events::Transfer {
+				from: *owner.as_eth(),
+				to: H160::default(),
+				value: amount.into(),
+			}
+			.to_log(collection_id_to_address(collection.id)),
+		);
+		<PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(
+			collection.id,
+			TokenId::default(),
+			owner.clone(),
+			amount,
+		));
 		Ok(())
 	}
 
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -264,19 +264,6 @@
 		}
 	}
 
-	fn burn_item_unchecked(
-		&self,
-		owner:& T::CrossAccountId,
-		token: TokenId,
-		amount: u128,
-	) -> sp_runtime::DispatchResult {
-		if amount == 1 {
-			<Pallet<T>>::burn_item_unchecked(self, owner, token)
-		} else {
-			Ok(())
-		}
-	}
-
 	fn transfer(
 		&self,
 		from: T::CrossAccountId,
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -27,7 +27,6 @@
 use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};
 use pallet_common::{
 	Error as CommonError, Pallet as PalletCommon, Event as CommonEvent, CollectionHandle,
-	dispatch::CollectionDispatch,
 	eth::collection_id_to_address,
 };
 use pallet_structure::Pallet as PalletStructure;
@@ -80,8 +79,6 @@
 		NonfungibleItemsHaveNoAmount,
 		/// Unable to burn NFT with children
 		CantBurnNftWithChildren,
-		/// Too many children to burn when destroying a collection
-		TooManyChildrenToBurn,
 	}
 
 	#[pallet::config]
@@ -293,14 +290,13 @@
 	pub fn destroy_collection(
 		collection: NonfungibleHandle<T>,
 		sender: &T::CrossAccountId,
-		nesting_budget: &dyn Budget,
 	) -> DispatchResult {
 		let id = collection.id;
 
 		// =========
 
-		Self::burn_children_in_collection(id, nesting_budget)?;
 		PalletCommon::destroy_collection(collection.0, sender)?;
+
 		<TokenData<T>>::remove_prefix((id,), None);
 		<TokenChildren<T>>::remove_prefix((id,), None);
 		<Owned<T>>::remove_prefix((id,), None);
@@ -308,47 +304,9 @@
 		<TokensBurnt<T>>::remove(id);
 		<Allowance<T>>::remove_prefix((id,), None);
 		<AccountBalance<T>>::remove_prefix((id,), None);
-		Ok(())
-	}
-
-	#[transactional]
-	fn burn_children_in_collection(collection_id: CollectionId, nesting_budget: &dyn Budget) -> DispatchResult {
-		for (parent_id, child) in <TokenChildren<T>>::drain_prefix((collection_id,))
-			.map(|((parent_id, child), _)| (parent_id, child)) {
-
-			let parent_address = T::CrossTokenAddressMapping::token_to_address(collection_id, parent_id);
-			Self::burn_tree(parent_address, child.0, child.1, nesting_budget)?;
-		}
-
 		Ok(())
 	}
 
-	fn burn_tree(
-		parent: T::CrossAccountId,
-		collection_id: CollectionId,
-		token_id: TokenId,
-		nesting_budget: &dyn Budget
-	) -> DispatchResult {
-		if !nesting_budget.consume() {
-			return Err(<Error<T>>::TooManyChildrenToBurn.into());
-		}
-
-		let handle = <CollectionHandle<T>>::try_get(collection_id)?;
-		let handle = T::CollectionDispatch::dispatch(handle);
-		let handle = handle.as_dyn();
-
-		let amount = handle.balance(parent.clone(), token_id);
-
-		handle.burn_item_unchecked(&parent, token_id, amount)?;
-
-		for child in <TokenChildren<T>>::drain_prefix((collection_id, token_id)).map(|(child, _)| child) {
-			let parent = T::CrossTokenAddressMapping::token_to_address(collection_id, token_id);
-			Self::burn_tree(parent, child.0, child.1, nesting_budget)?;
-		}
-
-		Ok(())
-	}
-
 	pub fn burn(
 		collection: &NonfungibleHandle<T>,
 		sender: &T::CrossAccountId,
@@ -370,11 +328,31 @@
 			return Err(<Error<T>>::CantBurnNftWithChildren.into());
 		}
 
-		let old_spender = <Allowance<T>>::get((collection.id, token));
+		let burnt = <TokensBurnt<T>>::get(collection.id)
+			.checked_add(1)
+			.ok_or(ArithmeticError::Overflow)?;
+
+		let balance = <AccountBalance<T>>::get((collection.id, token_data.owner.clone()))
+			.checked_sub(1)
+			.ok_or(ArithmeticError::Overflow)?;
+
+		if balance == 0 {
+			<AccountBalance<T>>::remove((collection.id, token_data.owner.clone()));
+		} else {
+			<AccountBalance<T>>::insert((collection.id, token_data.owner.clone()), balance);
+		}
+
+		if let Some(owner) = T::CrossTokenAddressMapping::address_to_token(&token_data.owner) {
+			Self::unnest(owner, (collection.id, token));
+		}
 
 		// =========
 
-		Self::burn_item_unchecked(collection, &token_data.owner, token)?;
+		<Owned<T>>::remove((collection.id, &token_data.owner, token));
+		<TokensBurnt<T>>::insert(collection.id, burnt);
+		<TokenData<T>>::remove((collection.id, token));
+		<TokenProperties<T>>::remove((collection.id, token));
+		let old_spender = <Allowance<T>>::take((collection.id, token));
 
 		if let Some(old_spender) = old_spender {
 			<PalletCommon<T>>::deposit_event(CommonEvent::Approved(
@@ -400,40 +378,6 @@
 			token_data.owner,
 			1,
 		));
-		Ok(())
-	}
-
-	pub fn burn_item_unchecked(
-		collection: &NonfungibleHandle<T>,
-		owner: &T::CrossAccountId,
-		token: TokenId,
-	) -> DispatchResult {
-		let burnt = <TokensBurnt<T>>::get(collection.id)
-			.checked_add(1)
-			.ok_or(ArithmeticError::Overflow)?;
-
-		let balance = <AccountBalance<T>>::get((collection.id, owner.clone()))
-			.checked_sub(1)
-			.ok_or(ArithmeticError::Overflow)?;
-
-		// =========
-
-		if let Some(owner) = T::CrossTokenAddressMapping::address_to_token(owner) {
-			Self::unnest(owner, (collection.id, token));
-		}
-
-		if balance == 0 {
-			<AccountBalance<T>>::remove((collection.id, owner.clone()));
-		} else {
-			<AccountBalance<T>>::insert((collection.id, owner.clone()), balance);
-		}
-
-		<Owned<T>>::remove((collection.id, owner, token));
-		<TokensBurnt<T>>::insert(collection.id, burnt);
-		<TokenData<T>>::remove((collection.id, token));
-		<TokenProperties<T>>::remove((collection.id, token));
-		<Allowance<T>>::remove((collection.id, token));
-
 		Ok(())
 	}
 
modifiedpallets/proxy-rmrk-core/src/lib.rsdiffbeforeafterboth
--- a/pallets/proxy-rmrk-core/src/lib.rs
+++ b/pallets/proxy-rmrk-core/src/lib.rs
@@ -179,8 +179,7 @@
 
             ensure!(collection.total_supply() == 0, <Error<T>>::CollectionNotEmpty);
 
-            let empty_budget = budget::Value::new(0);
-            <PalletNft<T>>::destroy_collection(collection, &cross_sender, &empty_budget)
+            <PalletNft<T>>::destroy_collection(collection, &cross_sender)
                 .map_err(Self::map_common_err_to_proxy)?;
 
             Self::deposit_event(Event::CollectionDestroyed { issuer: sender, collection_id });
modifiedpallets/refungible/src/common.rsdiffbeforeafterboth
--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -205,15 +205,6 @@
 		)
 	}
 
-	fn burn_item_unchecked(
-		&self,
-		owner: &T::CrossAccountId,
-		token: TokenId,
-		amount: u128,
-	) -> sp_runtime::DispatchResult {
-		<Pallet<T>>::burn_item_unchecked(self, owner, token, amount)
-	}
-
 	fn transfer(
 		&self,
 		from: T::CrossAccountId,
modifiedpallets/refungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -245,25 +245,6 @@
 		token: TokenId,
 		amount: u128,
 	) -> DispatchResult {
-		Self::burn_item_unchecked(collection, owner, token, amount)?;
-
-		// TODO: ERC20 transfer event
-		<PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(
-			collection.id,
-			token,
-			owner.clone(),
-			amount,
-		));
-
-		Ok(())
-	}
-
-	pub fn burn_item_unchecked(
-		collection: &RefungibleHandle<T>,
-		owner: &T::CrossAccountId,
-		token: TokenId,
-		amount: u128,
-	) -> DispatchResult {
 		let total_supply = <TotalSupply<T>>::get((collection.id, token))
 			.checked_sub(amount)
 			.ok_or(<CommonError<T>>::TokenValueTooLow)?;
@@ -318,6 +299,13 @@
 			<Balance<T>>::insert((collection.id, token, owner), balance);
 		}
 		<TotalSupply<T>>::insert((collection.id, token), total_supply);
+		// TODO: ERC20 transfer event
+		<PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(
+			collection.id,
+			token,
+			owner.clone(),
+			amount,
+		));
 		Ok(())
 	}
 
modifiedpallets/unique/src/lib.rsdiffbeforeafterboth
--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -332,24 +332,15 @@
 		/// # Arguments
 		///
 		/// * collection_id: collection to destroy.
-		#[weight =
-			<SelfWeightOf<T>>::destroy_collection()
-			+ <SelfWeightOf<T>>::burn_children_in_collection(*max_children_to_burn)
-		]
+		#[weight = <SelfWeightOf<T>>::destroy_collection()]
 		#[transactional]
-		pub fn destroy_collection(
-			origin,
-			collection_id: CollectionId,
-			max_children_to_burn: u32,
-		) -> DispatchResult {
+		pub fn destroy_collection(origin, collection_id: CollectionId) -> DispatchResult {
 			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
 			let collection = <CollectionHandle<T>>::try_get(collection_id)?;
 
-			let budget = budget::Value::new(max_children_to_burn);
-
 			// =========
 
-			T::CollectionDispatch::destroy(sender, collection, &budget)?;
+			T::CollectionDispatch::destroy(sender, collection)?;
 
 			<NftTransferBasket<T>>::remove_prefix(collection_id, None);
 			<FungibleTransferBasket<T>>::remove_prefix(collection_id, None);
modifiedpallets/unique/src/weights.rsdiffbeforeafterboth
--- a/pallets/unique/src/weights.rs
+++ b/pallets/unique/src/weights.rs
@@ -34,7 +34,6 @@
 pub trait WeightInfo {
 	fn create_collection() -> Weight;
 	fn destroy_collection() -> Weight;
-	fn burn_children_in_collection(max: u32) -> Weight;
 	fn add_to_allow_list() -> Weight;
 	fn remove_from_allow_list() -> Weight;
 	fn set_public_access_mode() -> Weight;
@@ -74,12 +73,6 @@
 			.saturating_add(T::DbWeight::get().reads(2 as Weight))
 			.saturating_add(T::DbWeight::get().writes(5 as Weight))
 	}
-
-	fn burn_children_in_collection(max: u32) -> Weight {
-		// TODO
-		(50_000_000 as Weight).saturating_mul(max as Weight)
-	}
-
 	// Storage: Common CollectionById (r:1 w:0)
 	// Storage: Common Allowlist (r:0 w:1)
 	fn add_to_allow_list() -> Weight {
@@ -199,12 +192,6 @@
 			.saturating_add(RocksDbWeight::get().reads(2 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(5 as Weight))
 	}
-
-	fn burn_children_in_collection(max: u32) -> Weight {
-		// TODO
-		(50_000_000 as Weight).saturating_mul(max as Weight)
-	}
-
 	// Storage: Common CollectionById (r:1 w:0)
 	// Storage: Common Allowlist (r:0 w:1)
 	fn add_to_allow_list() -> Weight {
modifiedruntime/common/src/dispatch.rsdiffbeforeafterboth
before · runtime/common/src/dispatch.rs
1use frame_support::{dispatch::{DispatchResult}, ensure};2use pallet_evm::PrecompileResult;3use sp_core::{H160, U256};4use sp_std::{borrow::ToOwned, vec::Vec};5use pallet_common::{6	CollectionById, CollectionHandle, CommonCollectionOperations, erc::CommonEvmHandler,7	eth::map_eth_to_id,8};9pub use pallet_common::dispatch::CollectionDispatch;10use pallet_fungible::{Pallet as PalletFungible, FungibleHandle};11use pallet_nonfungible::{Pallet as PalletNonfungible, NonfungibleHandle};12use pallet_refungible::{Pallet as PalletRefungible, RefungibleHandle, erc::RefungibleTokenHandle};13use up_data_structs::{14	CollectionMode, CreateCollectionData, MAX_DECIMAL_POINTS, mapping::TokenAddressMapping,15	budget::Budget,16};1718pub enum CollectionDispatchT<T>19where20	T: pallet_fungible::Config + pallet_nonfungible::Config + pallet_refungible::Config,21{22	Fungible(FungibleHandle<T>),23	Nonfungible(NonfungibleHandle<T>),24	Refungible(RefungibleHandle<T>),25}26impl<T> CollectionDispatch<T> for CollectionDispatchT<T>27where28	T: pallet_common::Config29		+ pallet_unique::Config30		+ pallet_fungible::Config31		+ pallet_nonfungible::Config32		+ pallet_refungible::Config,33{34	fn create(sender: T::AccountId, data: CreateCollectionData<T::AccountId>) -> DispatchResult {35		let _id = match data.mode {36			CollectionMode::NFT => <PalletNonfungible<T>>::init_collection(sender, data)?,37			CollectionMode::Fungible(decimal_points) => {38				// check params39				ensure!(40					decimal_points <= MAX_DECIMAL_POINTS,41					pallet_unique::Error::<T>::CollectionDecimalPointLimitExceeded42				);43				<PalletFungible<T>>::init_collection(sender, data)?44			}45			CollectionMode::ReFungible => <PalletRefungible<T>>::init_collection(sender, data)?,46		};47		Ok(())48	}4950	fn destroy(51		sender: T::CrossAccountId,52		collection: CollectionHandle<T>,53		nesting_budget: &dyn Budget,54	) -> DispatchResult {55		match collection.mode {56			CollectionMode::ReFungible => {57				PalletRefungible::destroy_collection(RefungibleHandle::cast(collection), &sender)?58			}59			CollectionMode::Fungible(_) => {60				PalletFungible::destroy_collection(FungibleHandle::cast(collection), &sender)?61			}62			CollectionMode::NFT => {63				PalletNonfungible::destroy_collection(64					NonfungibleHandle::cast(collection),65					&sender,66					nesting_budget,67				)?68			}69		}70		Ok(())71	}7273	fn dispatch(handle: CollectionHandle<T>) -> Self {74		match handle.mode {75			CollectionMode::Fungible(_) => Self::Fungible(FungibleHandle::cast(handle)),76			CollectionMode::NFT => Self::Nonfungible(NonfungibleHandle::cast(handle)),77			CollectionMode::ReFungible => Self::Refungible(RefungibleHandle::cast(handle)),78		}79	}8081	fn into_inner(self) -> CollectionHandle<T> {82		match self {83			Self::Fungible(f) => f.into_inner(),84			Self::Nonfungible(f) => f.into_inner(),85			Self::Refungible(f) => f.into_inner(),86		}87	}8889	fn as_dyn(&self) -> &dyn CommonCollectionOperations<T> {90		match self {91			Self::Fungible(h) => h,92			Self::Nonfungible(h) => h,93			Self::Refungible(h) => h,94		}95	}96}9798impl<T> pallet_evm::OnMethodCall<T> for CollectionDispatchT<T>99where100	T: pallet_common::Config101		+ pallet_unique::Config102		+ pallet_fungible::Config103		+ pallet_nonfungible::Config104		+ pallet_refungible::Config,105{106	fn is_reserved(target: &H160) -> bool {107		map_eth_to_id(target).is_some()108	}109	fn is_used(target: &H160) -> bool {110		map_eth_to_id(target)111			.map(<CollectionById<T>>::contains_key)112			.unwrap_or(false)113	}114	fn get_code(target: &H160) -> Option<Vec<u8>> {115		if let Some(collection_id) = map_eth_to_id(target) {116			let collection = <CollectionById<T>>::get(collection_id)?;117			Some(118				match collection.mode {119					CollectionMode::NFT => <NonfungibleHandle<T>>::CODE,120					CollectionMode::Fungible(_) => <FungibleHandle<T>>::CODE,121					CollectionMode::ReFungible => <RefungibleHandle<T>>::CODE,122				}123				.to_owned(),124			)125		} else if let Some((collection_id, _token_id)) =126			<T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(target)127		{128			let collection = <CollectionById<T>>::get(collection_id)?;129			if collection.mode != CollectionMode::ReFungible {130				return None;131			}132			// TODO: check token existence133			Some(<RefungibleTokenHandle<T>>::CODE.to_owned())134		} else {135			None136		}137	}138	fn call(139		source: &H160,140		target: &H160,141		gas_limit: u64,142		input: &[u8],143		value: U256,144	) -> Option<PrecompileResult> {145		if let Some(collection_id) = map_eth_to_id(target) {146			let collection = <CollectionHandle<T>>::new_with_gas_limit(collection_id, gas_limit)?;147			let dispatched = Self::dispatch(collection);148149			match dispatched {150				Self::Fungible(h) => h.call(source, input, value),151				Self::Nonfungible(h) => h.call(source, input, value),152				Self::Refungible(h) => h.call(source, input, value),153			}154		} else if let Some((collection_id, token_id)) =155			<T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(target)156		{157			let collection = <CollectionHandle<T>>::new_with_gas_limit(collection_id, gas_limit)?;158			if collection.mode != CollectionMode::ReFungible {159				return None;160			}161162			let handle = RefungibleHandle::cast(collection);163			// TODO: check token existence164			RefungibleTokenHandle(handle, token_id).call(source, input, value)165		} else {166			None167		}168	}169}