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

difftreelog

feat(rmrk) benchmark accept_nft

Daniel Shiposha2022-06-09parent: #5f417e8.patch.diff
in: master

1 file changed

modifiedpallets/proxy-rmrk-core/src/benchmarking.rsdiffbeforeafterboth
before · pallets/proxy-rmrk-core/src/benchmarking.rs
1use sp_std::vec;23use frame_benchmarking::{benchmarks, account};4use frame_system::RawOrigin;5use frame_support::{6	traits::{Currency, Get},7	BoundedVec,8};9use sp_runtime::Permill;1011use up_data_structs::*;1213use super::*;1415const SEED: u32 = 1;1617fn create_data<S: Get<u32>>() -> BoundedVec<u8, S> {18	vec![0; S::get() as usize].try_into().expect("size == S")19}2021fn create_max_collection<T: Config>(owner: &T::AccountId) -> DispatchResult {22	<T as pallet_common::Config>::Currency::deposit_creating(owner, T::CollectionCreationPrice::get());2324	let metadata = create_data();25	let max = None;26	let symbol = create_data();2728	<Pallet<T>>::create_collection(RawOrigin::Signed(owner.clone()).into(), metadata, max, symbol)29}3031fn create_max_nft<T: Config>(owner: &T::AccountId, collection_id: RmrkCollectionId) -> DispatchResult {32	let royalty_recipient = Some(owner.clone());33	let royalty_amount = Some(Permill::from_percent(25));34	let metadata = create_data();35	let transferable = true;3637	<Pallet<T>>::mint_nft(38		RawOrigin::Signed(owner.clone()).into(),39		owner.clone(),40		collection_id,41		royalty_recipient,42		royalty_amount,43		metadata,44		transferable45	)46}4748struct NftTowerBuilder {49	collection_id: RmrkCollectionId,50	current_nft_id: RmrkNftId51}5253impl NftTowerBuilder {54	fn new(collection_id: RmrkCollectionId) -> Self {55		Self {56			collection_id,57			current_nft_id: 058		}59	}6061	fn build<T: Config>(&mut self, owner: &T::AccountId, height: u32) -> Result<RmrkNftId, DispatchError> {62		create_max_nft::<T>(owner, self.collection_id)?;63		self.current_nft_id += 1;6465		let mut prev_nft_id = self.current_nft_id;6667		for _ in 0..height {68			create_max_nft::<T>(owner, self.collection_id)?;69			self.current_nft_id += 1;70			71			let new_owner = <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(72				self.collection_id,73				prev_nft_id74			);7576			<Pallet<T>>::send(77				RawOrigin::Signed(owner.clone()).into(),78				self.collection_id,79				self.current_nft_id,80				new_owner,81			)?;8283			prev_nft_id = self.current_nft_id;84		}8586		let deepest_nft_id = self.current_nft_id;8788		Ok(deepest_nft_id)89	}90}9192benchmarks! {93	create_collection {94		let caller = account("caller", 0, SEED);95		<T as pallet_common::Config>::Currency::deposit_creating(&caller, T::CollectionCreationPrice::get());96		let metadata = create_data();97		let max = None;98		let symbol = create_data();99	}: _(RawOrigin::Signed(caller), metadata, max, symbol)100101	destroy_collection {102		let caller = account("caller", 0, SEED);103		104		create_max_collection::<T>(&caller)?;105		let collection_id = 0;106	}: _(RawOrigin::Signed(caller), collection_id)107108	change_collection_issuer {109		let caller: T::AccountId = account("caller", 0, SEED);110111		create_max_collection::<T>(&caller)?;112		let collection_id = 0;113114		let new_owner: T::AccountId = account("new_owner", 0, SEED);115116		let new_owner_source = T::Lookup::unlookup(new_owner);117	}: _(RawOrigin::Signed(caller), collection_id, new_owner_source)118119	lock_collection {120		let caller: T::AccountId = account("caller", 0, SEED);121122		create_max_collection::<T>(&caller)?;123		let collection_id = 0;124	}: _(RawOrigin::Signed(caller), collection_id)125126	mint_nft {127		let caller: T::AccountId = account("caller", 0, SEED);128129		create_max_collection::<T>(&caller)?;130		let collection_id = 0;131		let owner = caller.clone();132		133		let royalty_recipient = Some(caller.clone());134		let royalty_amount = Some(Permill::from_percent(25));135		let metadata = create_data();136		let transferable = true;137	}:  _(138		RawOrigin::Signed(caller),139		owner,140		collection_id,141		royalty_recipient,142		royalty_amount,143		metadata,144		transferable145	)146147	send {148		let caller: T::AccountId = account("caller", 0, SEED);149		create_max_collection::<T>(&caller)?;150		let collection_id = 0;151152		let mut nft_tower_builder = NftTowerBuilder::new(collection_id);153		let src_nft_id = nft_tower_builder.build::<T>(&caller, 0)?;154		let target_nft_id = nft_tower_builder.build::<T>(&caller, NESTING_BUDGET - 2)?;155	}: _(156		RawOrigin::Signed(caller),157		collection_id,158		src_nft_id,159		<RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(collection_id, target_nft_id)160	)161}
after · pallets/proxy-rmrk-core/src/benchmarking.rs
1use sp_std::vec;23use frame_benchmarking::{benchmarks, account};4use frame_system::RawOrigin;5use frame_support::{6	traits::{Currency, Get},7	BoundedVec,8};9use sp_runtime::Permill;1011use up_data_structs::*;1213use super::*;1415const SEED: u32 = 1;1617fn create_data<S: Get<u32>>() -> BoundedVec<u8, S> {18	vec![0; S::get() as usize].try_into().expect("size == S")19}2021fn create_max_collection<T: Config>(owner: &T::AccountId) -> DispatchResult {22	<T as pallet_common::Config>::Currency::deposit_creating(owner, T::CollectionCreationPrice::get());2324	let metadata = create_data();25	let max = None;26	let symbol = create_data();2728	<Pallet<T>>::create_collection(RawOrigin::Signed(owner.clone()).into(), metadata, max, symbol)29}3031fn create_max_nft<T: Config>(owner: &T::AccountId, collection_id: RmrkCollectionId) -> DispatchResult {32	let royalty_recipient = Some(owner.clone());33	let royalty_amount = Some(Permill::from_percent(25));34	let metadata = create_data();35	let transferable = true;3637	<Pallet<T>>::mint_nft(38		RawOrigin::Signed(owner.clone()).into(),39		owner.clone(),40		collection_id,41		royalty_recipient,42		royalty_amount,43		metadata,44		transferable45	)46}4748struct NftBuilder {49	collection_id: RmrkCollectionId,50	current_nft_id: RmrkNftId51}5253impl NftBuilder {54	fn new(collection_id: RmrkCollectionId) -> Self {55		Self {56			collection_id,57			current_nft_id: 058		}59	}6061	fn build<T: Config>(&mut self, owner: &T::AccountId) -> Result<RmrkNftId, DispatchError> {62		create_max_nft::<T>(owner, self.collection_id)?;63		self.current_nft_id += 1;6465		Ok(self.current_nft_id)66	}6768	fn build_tower<T: Config>(&mut self, owner: &T::AccountId, height: u32) -> Result<RmrkNftId, DispatchError> {69		self.build::<T>(owner)?;7071		let mut prev_nft_id = self.current_nft_id;7273		for _ in 0..height {74			self.build::<T>(owner)?;75			76			let new_owner = <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(77				self.collection_id,78				prev_nft_id79			);8081			<Pallet<T>>::send(82				RawOrigin::Signed(owner.clone()).into(),83				self.collection_id,84				self.current_nft_id,85				new_owner,86			)?;8788			prev_nft_id = self.current_nft_id;89		}9091		let deepest_nft_id = self.current_nft_id;9293		Ok(deepest_nft_id)94	}95}9697benchmarks! {98	create_collection {99		let caller = account("caller", 0, SEED);100		<T as pallet_common::Config>::Currency::deposit_creating(&caller, T::CollectionCreationPrice::get());101		let metadata = create_data();102		let max = None;103		let symbol = create_data();104	}: _(RawOrigin::Signed(caller), metadata, max, symbol)105106	destroy_collection {107		let caller = account("caller", 0, SEED);108		109		create_max_collection::<T>(&caller)?;110		let collection_id = 0;111	}: _(RawOrigin::Signed(caller), collection_id)112113	change_collection_issuer {114		let caller: T::AccountId = account("caller", 0, SEED);115116		create_max_collection::<T>(&caller)?;117		let collection_id = 0;118119		let new_owner: T::AccountId = account("new_owner", 0, SEED);120121		let new_owner_source = T::Lookup::unlookup(new_owner);122	}: _(RawOrigin::Signed(caller), collection_id, new_owner_source)123124	lock_collection {125		let caller: T::AccountId = account("caller", 0, SEED);126127		create_max_collection::<T>(&caller)?;128		let collection_id = 0;129	}: _(RawOrigin::Signed(caller), collection_id)130131	mint_nft {132		let caller: T::AccountId = account("caller", 0, SEED);133134		create_max_collection::<T>(&caller)?;135		let collection_id = 0;136		let owner = caller.clone();137		138		let royalty_recipient = Some(caller.clone());139		let royalty_amount = Some(Permill::from_percent(25));140		let metadata = create_data();141		let transferable = true;142	}:  _(143		RawOrigin::Signed(caller),144		owner,145		collection_id,146		royalty_recipient,147		royalty_amount,148		metadata,149		transferable150	)151152	send {153		let caller: T::AccountId = account("caller", 0, SEED);154		create_max_collection::<T>(&caller)?;155		let collection_id = 0;156157		let mut nft_builder = NftBuilder::new(collection_id);158		let src_nft_id = nft_builder.build::<T>(&caller)?;159		let target_nft_id = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 2)?;160	}: _(161		RawOrigin::Signed(caller),162		collection_id,163		src_nft_id,164		<RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(collection_id, target_nft_id)165	)166167	accept_nft {168		let caller: T::AccountId = account("caller", 0, SEED);169		let sender: T::AccountId = account("sender", 0, SEED);170171		create_max_collection::<T>(&sender)?;172		let src_collection_id = 0;173174		create_max_collection::<T>(&caller)?;175		let target_collection_id = 1;176177		let mut src_nft_builder = NftBuilder::new(src_collection_id);178		let src_nft_id = src_nft_builder.build::<T>(&sender)?;179180		let mut target_nft_builder = NftBuilder::new(target_collection_id);181		let fake_target_nft_id = target_nft_builder.build::<T>(&caller)?;182		let target_nft_id = target_nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;183184		let new_owner = <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(185			target_collection_id,186			fake_target_nft_id187		);188189		let actual_new_owner = <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(190			target_collection_id,191			target_nft_id192		);193194		<Pallet<T>>::send(195			RawOrigin::Signed(sender.clone()).into(),196			src_collection_id,197			src_nft_id,198			new_owner,199		)?;200	}: _(201		RawOrigin::Signed(caller),202		src_collection_id,203		src_nft_id,204		actual_new_owner205	)206}