forked from paritytech/polkadot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
628 additions
and
261 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,168 @@ | ||
#![cfg(feature = "runtime-benchmarks")] | ||
use super::*; | ||
|
||
#[allow(unused)] | ||
use crate::Pallet as Template; | ||
use super::*; | ||
|
||
use cumulus_primitives_core::ParaId; | ||
use frame_benchmarking::v2::*; | ||
use frame_system::RawOrigin; | ||
use sp_runtime::traits::Hash; | ||
use sp_runtime::traits::{Bounded, StaticLookup}; | ||
use pallet_nfts::{ | ||
CollectionSetting, CollectionSettings, CollectionConfig, | ||
CollectionConfigFor, MintSettings, BenchmarkHelper, DepositBalanceOf, | ||
}; | ||
use enumflags2::{BitFlag, BitFlags}; | ||
use frame_support::{traits::Currency, assert_ok, BoundedVec}; | ||
use sp_std::vec; | ||
|
||
fn make_collection_config<T: Config<I>, I: 'static>( | ||
disable_settings: BitFlags<CollectionSetting>, | ||
) -> CollectionConfigFor<T, I> { | ||
CollectionConfig { | ||
settings: CollectionSettings::from_disabled(disable_settings), | ||
max_supply: None, | ||
mint_settings: MintSettings::default(), | ||
} | ||
} | ||
|
||
fn default_collection_config<T: Config<I>, I: 'static>() -> CollectionConfigFor<T, I> { | ||
make_collection_config::<T, I>(CollectionSetting::empty()) | ||
} | ||
|
||
#[benchmarks] | ||
|
||
#[instance_benchmarks] | ||
mod benchmarks { | ||
use super::*; | ||
|
||
// Benchmark tries the first scenario of collection_x_transfer (transfering empty collection) | ||
#[benchmark] | ||
fn transfer_collection_empty<T: Config<I>, I: 'static>() { | ||
let caller: T::AccountId = whitelisted_caller(); | ||
let caller_lookup = T::Lookup::unlookup(caller.clone()); | ||
let collection = T::Helper::collection(0); | ||
|
||
T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value()); | ||
|
||
assert_ok!(pallet_nfts::Pallet::<T, I>::create(RawOrigin::Signed(caller.clone()).into(), caller_lookup.clone() ,default_collection_config::<T, I>())); | ||
|
||
#[extrinsic_call] | ||
collection_x_transfer(RawOrigin::Signed(caller.into()),collection.clone() , None, 1000.into(), None); | ||
} | ||
|
||
// Benchmark tries the second scenario of collection_x_transfer (transfering collection with items same owner) | ||
#[benchmark] | ||
fn mint_collection_large() { | ||
fn transfer_collection_same_owner<T: Config<I>, I: 'static>() { | ||
let caller: T::AccountId = whitelisted_caller(); | ||
let caller_lookup = T::Lookup::unlookup(caller.clone()); | ||
let collection = T::Helper::collection(0); | ||
let item = T::Helper::item(0); | ||
|
||
T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value()); | ||
|
||
assert_ok!(pallet_nfts::Pallet::<T, I>::create(RawOrigin::Signed(caller.clone()).into(), caller_lookup.clone() ,default_collection_config::<T, I>())); | ||
assert_ok!(pallet_nfts::Pallet::<T, I>::mint(RawOrigin::Signed(caller.clone()).into(), collection.clone(), item.clone(), caller_lookup.clone(), None)); | ||
|
||
|
||
#[extrinsic_call] | ||
mint_collection(RawOrigin::Signed(caller),b"pHgJCOTaAPedH0mtvRVVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(), b"pHgJCOTaAPEceLC69VVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap()); | ||
collection_x_transfer(RawOrigin::Signed(caller.into()),collection.clone() , None, 1000.into(), None); | ||
} | ||
|
||
// Benchmark tries the third scenario of collection_x_transfer (transfering collection with items different owners) | ||
#[benchmark] | ||
fn mint_non_fungible_large() { | ||
fn transfer_collection_other_owners<T: Config<I>, I: 'static>() { | ||
let caller: T::AccountId = whitelisted_caller(); | ||
let caller2: T::AccountId = account("caller2", 1, 1); | ||
let caller_lookup = T::Lookup::unlookup(caller.clone()); | ||
let caller_lookup2 = T::Lookup::unlookup(caller2.clone()); | ||
let collection = T::Helper::collection(0); | ||
let item = T::Helper::item(0); | ||
let item2 = T::Helper::item(1); | ||
|
||
let collection: pallet::Collection<T> = Collection { | ||
owner: caller.clone(), | ||
collection_name: b"pHgJCOTaAPedH0mtvRVVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(), | ||
collection_description: b"pHgJCOTaAPEceLC69VVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(), | ||
collection_origin_parachain_id: 0.into(), | ||
}; | ||
|
||
let collection_hash = T::Hashing::hash_of(&collection); | ||
T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value()); | ||
|
||
let collection_with_hash: pallet::CollectionWithHash<T> = CollectionWithHash { | ||
owner: caller.clone(), | ||
collection_name: b"pHgJCOTaAPedH0mtvRVVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(), | ||
collection_description: b"pHgJCOTaAPEceLC69VVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(), | ||
collection_origin_parachain_id: 0.into(), | ||
collection_hash: collection_hash, | ||
}; | ||
assert_ok!(pallet_nfts::Pallet::<T, I>::create(RawOrigin::Signed(caller.clone()).into(), caller_lookup.clone() ,default_collection_config::<T, I>())); | ||
assert_ok!(pallet_nfts::Pallet::<T, I>::mint(RawOrigin::Signed(caller.clone()).into(), collection.clone(), item.clone(), caller_lookup.clone(), None)); | ||
assert_ok!(pallet_nfts::Pallet::<T, I>::mint(RawOrigin::Signed(caller.clone()).into(), collection.clone(), item2.clone(), caller_lookup2.clone(), None)); | ||
|
||
pallet::Collections::<T>::insert(collection_hash, collection_with_hash); | ||
pallet::CollectionSize::<T>::insert(collection_hash, 0); | ||
|
||
#[extrinsic_call] | ||
mint_non_fungible(RawOrigin::Signed(caller),b"pHgJCOTaAPedH0mtvRVVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(), b"pHgJCOTaAPEceLC69VVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(),collection_hash); | ||
collection_x_transfer(RawOrigin::Signed(caller.into()),collection.clone() , None, 1000.into(), None); | ||
} | ||
|
||
//Benchmark tries nft transfer | ||
#[benchmark] | ||
fn mint_collection_received_large() { | ||
fn transfer_nft<T: Config<I>, I: 'static>() { | ||
let caller: T::AccountId = whitelisted_caller(); | ||
let caller_lookup = T::Lookup::unlookup(caller.clone()); | ||
let collection = T::Helper::collection(0); | ||
let item = T::Helper::item(0); | ||
|
||
T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value()); | ||
|
||
assert_ok!(pallet_nfts::Pallet::<T, I>::create(RawOrigin::Signed(caller.clone()).into(), caller_lookup.clone() ,default_collection_config::<T, I>())); | ||
assert_ok!(pallet_nfts::Pallet::<T, I>::mint(RawOrigin::Signed(caller.clone()).into(), collection.clone(), item.clone(), caller_lookup.clone(), None)); | ||
|
||
#[extrinsic_call] | ||
mint_collection_received(RawOrigin::Signed(caller),b"pHgJCOTaAPedH0mtvRVVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(), b"pHgJCOTaAPEceLC69VVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(),1000.into(), caller.clone()); | ||
nft_x_transfer(RawOrigin::Signed(caller.into()),collection.clone() , item.clone(), 1000.into(), collection.clone() , item.clone()); | ||
} | ||
|
||
//Benchmark tries collection empty parse | ||
#[benchmark] | ||
fn parse_empty_col<T: Config<I>, I: 'static>() { | ||
let caller: T::AccountId = whitelisted_caller(); | ||
let collection = T::Helper::collection(0); | ||
|
||
T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value()); | ||
|
||
#[extrinsic_call] | ||
parse_collection_empty(RawOrigin::Signed(caller.into()),collection.clone() , None, BoundedVec::new(), None); | ||
} | ||
|
||
//Benchmark tries collection parse with items | ||
#[benchmark] | ||
fn parse_same_owner_col<T: Config<I>, I: 'static>() { | ||
let caller: T::AccountId = whitelisted_caller(); | ||
let collection = T::Helper::collection(0); | ||
let item = T::Helper::item(0); | ||
let nfts = vec![(item.clone(), BoundedVec::new())]; | ||
|
||
T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value()); | ||
|
||
|
||
#[extrinsic_call] | ||
parse_collection_same_owner(RawOrigin::Signed(caller.into()),None, BoundedVec::new(), nfts.clone() ,1000.into(), collection.clone(), None); | ||
} | ||
|
||
//Benchmark tries collection parse with items and different owners | ||
#[benchmark] | ||
fn parse_diff_owner_col<T: Config<I>, I: 'static>() { | ||
let caller: T::AccountId = whitelisted_caller(); | ||
let caller2: T::AccountId = account("caller2", 1, 1); | ||
let caller_lookup2 = T::Lookup::unlookup(caller2.clone()); | ||
let collection = T::Helper::collection(0); | ||
let item = T::Helper::item(0); | ||
let nfts = vec![(item.clone(), caller_lookup2, BoundedVec::new())]; | ||
|
||
T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value()); | ||
|
||
#[extrinsic_call] | ||
parse_collection_diff_owners(RawOrigin::Signed(caller.into()),None, BoundedVec::new(), nfts.clone() ,1000.into(), collection.clone(), None); | ||
} | ||
|
||
//Benchmark tries collection parse item | ||
#[benchmark] | ||
fn mint_non_fungible_received_large() { | ||
fn parse_item<T: Config<I>, I: 'static>() { | ||
let caller: T::AccountId = whitelisted_caller(); | ||
let caller_lookup = T::Lookup::unlookup(caller.clone()); | ||
let collection = T::Helper::collection(0); | ||
let item = T::Helper::item(0); | ||
|
||
T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value()); | ||
|
||
let collection: pallet::Collection<T> = Collection { | ||
owner: caller.clone(), | ||
collection_name: b"pHgJCOTaAPedH0mtvRVVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(), | ||
collection_description: b"pHgJCOTaAPEceLC69VVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(), | ||
collection_origin_parachain_id: 0.into(), | ||
}; | ||
|
||
let collection_hash = T::Hashing::hash_of(&collection); | ||
|
||
let collection_with_hash: pallet::CollectionWithHash<T> = CollectionWithHash { | ||
owner: caller.clone(), | ||
collection_name: b"pHgJCOTaAPedH0mtvRVVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(), | ||
collection_description: b"pHgJCOTaAPEceLC69VVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(), | ||
collection_origin_parachain_id: 0.into(), | ||
collection_hash: collection_hash, | ||
}; | ||
|
||
let parachain_id: ParaId = 0.into(); | ||
let _ = ReceivedCollections::<T>::mutate(parachain_id, |x| -> Result<(), ()> { | ||
if let Some(x) = x { | ||
x.try_push(collection_with_hash).map_err(|_| ())?; | ||
Ok(()) | ||
} else { | ||
*x = Some(vec![collection_with_hash].try_into().map_err(|_| ())?); | ||
Ok(()) | ||
} | ||
}); | ||
|
||
pallet::CollectionSize::<T>::insert(collection_hash, 0); | ||
assert_ok!(pallet_nfts::Pallet::<T, I>::create(RawOrigin::Signed(caller.clone()).into(), caller_lookup.clone() ,default_collection_config::<T, I>())); | ||
|
||
#[extrinsic_call] | ||
mint_non_fungible_received(RawOrigin::Signed(caller),b"pHgJCOTaAPedH0mtvRVVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(), b"pHgJCOTaAPEceLC69VVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(),collection_hash, 0.into(), caller.clone()); | ||
parse_nft_transfer(RawOrigin::Signed(caller.into()),collection.clone(), item.clone(), BoundedVec::new(), collection.clone(), item.clone() ,1000.into()); | ||
} | ||
|
||
impl_benchmark_test_suite!(Template, crate::mock::new_test_ext(), crate::mock::Test); | ||
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.