git.delta.rocks / unique-network / refs/commits / 73817f448a72

difftreelog

test upgrade benchmarks for new substrate

Yaroslav Bolyukin2022-02-26parent: #c9e84df.patch.diff
in: master

12 files changed

modified.maintain/frame-weight-template.hbsdiffbeforeafterboth
7//! EXECUTION: {{cmd.execution}}, WASM-EXECUTION: {{cmd.wasm_execution}}, CHAIN: {{cmd.chain}}, DB CACHE: {{cmd.db_cache}}7//! EXECUTION: {{cmd.execution}}, WASM-EXECUTION: {{cmd.wasm_execution}}, CHAIN: {{cmd.chain}}, DB CACHE: {{cmd.db_cache}}
88
9// Executed Command:9// Executed Command:
10{{#each args as |arg|~}}10{{#each args as |arg|}}
11// {{arg}}11// {{arg}}
12{{/each}}12{{/each}}
1313
2121
22/// Weight functions needed for {{pallet}}.22/// Weight functions needed for {{pallet}}.
23pub trait WeightInfo {23pub trait WeightInfo {
24 {{~#each benchmarks as |benchmark|}}24 {{#each benchmarks as |benchmark|}}
25 fn {{benchmark.name~}}25 fn {{benchmark.name~}}
26 (26 (
27 {{~#each benchmark.components as |c| ~}}27 {{~#each benchmark.components as |c| ~}}
28 {{c.name}}: u32, {{/each~}}28 {{c.name}}: u32, {{/each~}}
29 ) -> Weight;29 ) -> Weight;
30 {{~/each}}30 {{/each}}
31}31}
3232
33/// Weights for {{pallet}} using the Substrate node and recommended hardware.33/// Weights for {{pallet}} using the Substrate node and recommended hardware.
34pub struct SubstrateWeight<T>(PhantomData<T>);34pub struct SubstrateWeight<T>(PhantomData<T>);
35{{#if (eq pallet "frame_system")}}
36impl<T: crate::Config> WeightInfo for SubstrateWeight<T> {
37{{else}}
35impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {38impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
39{{/if}}
36 {{~#each benchmarks as |benchmark|}}40 {{#each benchmarks as |benchmark|}}
37 {{~#each benchmark.comments as |comment|}}41 {{#each benchmark.comments as |comment|}}
38 // {{comment}}42 // {{comment}}
39 {{~/each}}43 {{/each}}
40 fn {{benchmark.name~}}44 fn {{benchmark.name~}}
41 (45 (
42 {{~#each benchmark.components as |c| ~}}46 {{~#each benchmark.components as |c| ~}}
43 {{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}}47 {{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}}
44 ) -> Weight {48 ) -> Weight {
45 ({{underscore benchmark.base_weight}} as Weight)49 ({{underscore benchmark.base_weight}} as Weight)
46 {{~#each benchmark.component_weight as |cw|}}50 {{#each benchmark.component_weight as |cw|}}
47 // Standard Error: {{underscore cw.error}}51 // Standard Error: {{underscore cw.error}}
48 .saturating_add(({{underscore cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight))52 .saturating_add(({{underscore cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight))
49 {{~/each}}53 {{/each}}
50 {{~#if (ne benchmark.base_reads "0")}}54 {{#if (ne benchmark.base_reads "0")}}
51 .saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}} as Weight))55 .saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}} as Weight))
52 {{~/if}}56 {{/if}}
53 {{~#each benchmark.component_reads as |cr|}}57 {{#each benchmark.component_reads as |cr|}}
54 .saturating_add(T::DbWeight::get().reads(({{cr.slope}} as Weight).saturating_mul({{cr.name}} as Weight)))58 .saturating_add(T::DbWeight::get().reads(({{cr.slope}} as Weight).saturating_mul({{cr.name}} as Weight)))
55 {{~/each}}59 {{/each}}
56 {{~#if (ne benchmark.base_writes "0")}}60 {{#if (ne benchmark.base_writes "0")}}
57 .saturating_add(T::DbWeight::get().writes({{benchmark.base_writes}} as Weight))61 .saturating_add(T::DbWeight::get().writes({{benchmark.base_writes}} as Weight))
58 {{~/if}}62 {{/if}}
59 {{~#each benchmark.component_writes as |cw|}}63 {{#each benchmark.component_writes as |cw|}}
60 .saturating_add(T::DbWeight::get().writes(({{cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight)))64 .saturating_add(T::DbWeight::get().writes(({{cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight)))
61 {{~/each}}65 {{/each}}
62 }66 }
63 {{~/each}}67 {{/each}}
64}68}
6569
66// For backwards compatibility and tests70// For backwards compatibility and tests
67impl WeightInfo for () {71impl WeightInfo for () {
68 {{~#each benchmarks as |benchmark|}}72 {{#each benchmarks as |benchmark|}}
69 {{~#each benchmark.comments as |comment|}}73 {{#each benchmark.comments as |comment|}}
70 // {{comment}}74 // {{comment}}
71 {{~/each}}75 {{/each}}
72 fn {{benchmark.name~}}76 fn {{benchmark.name~}}
73 (77 (
74 {{~#each benchmark.components as |c| ~}}78 {{~#each benchmark.components as |c| ~}}
75 {{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}}79 {{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}}
76 ) -> Weight {80 ) -> Weight {
77 ({{underscore benchmark.base_weight}} as Weight)81 ({{underscore benchmark.base_weight}} as Weight)
78 {{~#each benchmark.component_weight as |cw|}}82 {{#each benchmark.component_weight as |cw|}}
79 // Standard Error: {{underscore cw.error}}83 // Standard Error: {{underscore cw.error}}
80 .saturating_add(({{underscore cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight))84 .saturating_add(({{underscore cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight))
81 {{~/each}}85 {{/each}}
82 {{~#if (ne benchmark.base_reads "0")}}86 {{#if (ne benchmark.base_reads "0")}}
83 .saturating_add(RocksDbWeight::get().reads({{benchmark.base_reads}} as Weight))87 .saturating_add(RocksDbWeight::get().reads({{benchmark.base_reads}} as Weight))
84 {{~/if}}88 {{/if}}
85 {{~#each benchmark.component_reads as |cr|}}89 {{#each benchmark.component_reads as |cr|}}
86 .saturating_add(RocksDbWeight::get().reads(({{cr.slope}} as Weight).saturating_mul({{cr.name}} as Weight)))90 .saturating_add(RocksDbWeight::get().reads(({{cr.slope}} as Weight).saturating_mul({{cr.name}} as Weight)))
87 {{~/each}}91 {{/each}}
88 {{~#if (ne benchmark.base_writes "0")}}92 {{#if (ne benchmark.base_writes "0")}}
89 .saturating_add(RocksDbWeight::get().writes({{benchmark.base_writes}} as Weight))93 .saturating_add(RocksDbWeight::get().writes({{benchmark.base_writes}} as Weight))
90 {{~/if}}94 {{/if}}
91 {{~#each benchmark.component_writes as |cw|}}95 {{#each benchmark.component_writes as |cw|}}
92 .saturating_add(RocksDbWeight::get().writes(({{cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight)))96 .saturating_add(RocksDbWeight::get().writes(({{cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight)))
93 {{~/each}}97 {{/each}}
94 }98 }
95 {{~/each}}99 {{/each}}
96}100}
97101
modifiedpallets/common/src/benchmarking.rsdiffbeforeafterboth
1use sp_std::vec::Vec;1use sp_std::vec::Vec;
2use crate::{Config, CollectionHandle};2use crate::{Config, CollectionHandle};
3use up_data_structs::{3use up_data_structs::{
4 CollectionMode, Collection, CollectionId, MAX_COLLECTION_NAME_LENGTH,4 CollectionMode, CreateCollectionData, CollectionId, MAX_COLLECTION_NAME_LENGTH,
5 MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH, OFFCHAIN_SCHEMA_LIMIT,5 MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH, OFFCHAIN_SCHEMA_LIMIT,
6 VARIABLE_ON_CHAIN_SCHEMA_LIMIT, CONST_ON_CHAIN_SCHEMA_LIMIT,6 VARIABLE_ON_CHAIN_SCHEMA_LIMIT, CONST_ON_CHAIN_SCHEMA_LIMIT,
7};7};
8use frame_support::traits::{Currency, Get};8use frame_support::{
9 traits::{Currency, Get},
10 pallet_prelude::ConstU32,
11 BoundedVec,
12};
9use core::convert::TryInto;13use core::convert::TryInto;
10use sp_runtime::DispatchError;14use sp_runtime::DispatchError;
1115
12pub fn create_data(size: usize) -> Vec<u8> {16pub fn create_data<const S: u32>() -> BoundedVec<u8, ConstU32<S>> {
17 create_var_data::<S>(S)
18}
19pub fn create_u16_data<const S: u32>() -> BoundedVec<u16, ConstU32<S>> {
20 (0..S)
13 (0..size).map(|v| (v & 0xff) as u8).collect()21 .map(|v| (v & 0xffff) as u16)
22 .collect::<Vec<_>>()
23 .try_into()
24 .unwrap()
14}25}
15pub fn create_u16_data(size: usize) -> Vec<u16> {26pub fn create_var_data<const S: u32>(size: u32) -> BoundedVec<u8, ConstU32<S>> {
27 assert!(
28 size <= S,
29 "size ({}) should be less within bound ({})",
30 size,
31 S
32 );
16 (0..size).map(|v| (v & 0xffff) as u16).collect()33 (0..size)
34 .map(|v| (v & 0xff) as u8)
35 .collect::<Vec<_>>()
36 .try_into()
37 .unwrap()
17}38}
1839
19pub fn create_collection_raw<T: Config, R>(40pub fn create_collection_raw<T: Config, R>(
20 owner: T::AccountId,41 owner: T::AccountId,
21 mode: CollectionMode,42 mode: CollectionMode,
22 handler: impl FnOnce(Collection<T::AccountId>) -> Result<CollectionId, DispatchError>,43 handler: impl FnOnce(
44 T::AccountId,
45 CreateCollectionData<T::AccountId>,
46 ) -> Result<CollectionId, DispatchError>,
23 cast: impl FnOnce(CollectionHandle<T>) -> R,47 cast: impl FnOnce(CollectionHandle<T>) -> R,
24) -> Result<R, DispatchError> {48) -> Result<R, DispatchError> {
25 T::Currency::deposit_creating(&owner, T::CollectionCreationPrice::get());49 T::Currency::deposit_creating(&owner, T::CollectionCreationPrice::get());
26 let name = create_u16_data(MAX_COLLECTION_NAME_LENGTH)50 let name = create_u16_data::<MAX_COLLECTION_NAME_LENGTH>();
27 .try_into()
28 .unwrap();
29 let description = create_u16_data(MAX_COLLECTION_DESCRIPTION_LENGTH)51 let description = create_u16_data::<MAX_COLLECTION_DESCRIPTION_LENGTH>();
30 .try_into()
31 .unwrap();
32 let token_prefix = create_data(MAX_TOKEN_PREFIX_LENGTH).try_into().unwrap();52 let token_prefix = create_data::<MAX_TOKEN_PREFIX_LENGTH>();
33 let offchain_schema = create_data(OFFCHAIN_SCHEMA_LIMIT as usize)53 let offchain_schema = create_data::<OFFCHAIN_SCHEMA_LIMIT>();
34 .try_into()
35 .unwrap();
36 let variable_on_chain_schema = create_data(VARIABLE_ON_CHAIN_SCHEMA_LIMIT as usize)54 let variable_on_chain_schema = create_data::<VARIABLE_ON_CHAIN_SCHEMA_LIMIT>();
37 .try_into()
38 .unwrap();
39 let const_on_chain_schema = create_data(CONST_ON_CHAIN_SCHEMA_LIMIT as usize)55 let const_on_chain_schema = create_data::<CONST_ON_CHAIN_SCHEMA_LIMIT>();
40 .try_into()
41 .unwrap();
42 handler(Collection {56 handler(
43 owner,57 owner,
58 CreateCollectionData {
44 mode,59 mode,
45 access: Default::default(),
46 name,60 name,
47 description,61 description,
48 token_prefix,62 token_prefix,
49 mint_mode: true,
50 offchain_schema,63 offchain_schema,
51 schema_version: Default::default(),
52 sponsorship: Default::default(),
53 limits: Default::default(),
54 variable_on_chain_schema,64 variable_on_chain_schema,
55 const_on_chain_schema,65 const_on_chain_schema,
56 meta_update_permission: Default::default(),66 ..Default::default()
67 },
57 })68 )
58 .and_then(CollectionHandle::try_get)69 .and_then(CollectionHandle::try_get)
59 .map(cast)70 .map(cast)
60}71}
6172
73/// Helper macros, which handles all benchmarking preparation in semi-declarative way
74///
75/// `name` is a substrate account
76/// - name: sub[(id)]
77/// `name` is a collection with owner `owner`
78/// - name: collection(owner)
79/// `name` is a cross account based on substrate
80/// - name: cross_sub[(id)]
81/// `name` is a cross account, which maps to substrate account `name`
82/// - name: cross_from_sub
83/// `name` is a cross account, which maps to substrate account `other_name`
84/// - name: cross_from_sub(other_name)
62#[macro_export]85#[macro_export]
63macro_rules! bench_init {86macro_rules! bench_init {
64 ($name:ident: sub $(($id:expr))?; $($rest:tt)*) => {87 ($name:ident: sub $(($id:expr))?; $($rest:tt)*) => {
modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
12};12};
1313
14pub struct CommonWeights<T: Config>(PhantomData<T>);14pub struct CommonWeights<T: Config>(PhantomData<T>);
15impl<T: Config> CommonWeightInfo for CommonWeights<T> {15impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {
16 fn create_item() -> Weight {16 fn create_item() -> Weight {
17 <SelfWeightOf<T>>::create_item()17 <SelfWeightOf<T>>::create_item()
18 }18 }
modifiedpallets/fungible/src/lib.rsdiffbeforeafterboth
9use pallet_evm_coder_substrate::WithRecorder;9use pallet_evm_coder_substrate::WithRecorder;
10use sp_core::H160;10use sp_core::H160;
11use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};11use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};
12use sp_std::{vec::Vec, vec, collections::btree_map::BTreeMap};12use sp_std::collections::btree_map::BTreeMap;
1313
14pub use pallet::*;14pub use pallet::*;
1515
222 pub fn create_multiple_items(222 pub fn create_multiple_items(
223 collection: &FungibleHandle<T>,223 collection: &FungibleHandle<T>,
224 sender: &T::CrossAccountId,224 sender: &T::CrossAccountId,
225 data: Vec<CreateItemData<T>>,225 data: BTreeMap<T::CrossAccountId, u128>,
226 ) -> DispatchResult {226 ) -> DispatchResult {
227 if !collection.is_owner_or_admin(sender) {227 if !collection.is_owner_or_admin(sender) {
228 ensure!(228 ensure!(
236 }236 }
237 }237 }
238
239 let mut balances = BTreeMap::new();
240238
241 let total_supply = data239 let total_supply = data
242 .iter()240 .iter()
243 .map(|u| u.1)241 .map(|(_, v)| *v)
244 .try_fold(<TotalSupply<T>>::get(collection.id), |acc, v| {242 .try_fold(<TotalSupply<T>>::get(collection.id), |acc, v| {
245 acc.checked_add(v)243 acc.checked_add(v)
246 })244 })
247 .ok_or(ArithmeticError::Overflow)?;245 .ok_or(ArithmeticError::Overflow)?;
248246
247 let mut balances = data;
249 for (user, amount) in data.into_iter() {248 for (k, v) in balances.iter_mut() {
250 let balance = balances
251 .entry(user.clone())
252 .or_insert_with(|| <Balance<T>>::get((collection.id, user)));
253 *balance = (*balance)249 *v = <Balance<T>>::get((collection.id, &k))
254 .checked_add(amount)250 .checked_add(*v)
255 .ok_or(ArithmeticError::Overflow)?;251 .ok_or(ArithmeticError::Overflow)?;
256 }252 }
257253
258 // =========254 // =========
259255
modifiedpallets/inflation/src/benchmarking.rsdiffbeforeafterboth
1#![cfg(feature = "runtime-benchmarks")]1#![cfg(feature = "runtime-benchmarks")]
22
3use super::*;3use super::*;
4use crate::Module as Inflation;4use crate::Pallet as Inflation;
55
6use frame_benchmarking::{benchmarks};6use frame_benchmarking::{benchmarks};
7use frame_support::traits::OnInitialize;7use frame_support::traits::OnInitialize;
modifiedpallets/nonfungible/src/benchmarking.rsdiffbeforeafterboth
2use crate::{Pallet, Config, NonfungibleHandle};2use crate::{Pallet, Config, NonfungibleHandle};
33
4use sp_std::prelude::*;4use sp_std::prelude::*;
5use pallet_common::benchmarking::{create_collection_raw, create_data};5use pallet_common::benchmarking::{create_collection_raw, create_data, create_var_data};
6use frame_benchmarking::{benchmarks, account};6use frame_benchmarking::{benchmarks, account};
7use up_data_structs::{CollectionMode, MAX_ITEMS_PER_BATCH};7use up_data_structs::{CollectionMode, MAX_ITEMS_PER_BATCH, CUSTOM_DATA_LIMIT};
8use pallet_common::bench_init;8use pallet_common::bench_init;
9use core::convert::TryInto;9use core::convert::TryInto;
1010
11const SEED: u32 = 1;11const SEED: u32 = 1;
1212
13fn create_max_item_data<T: Config>(owner: T::CrossAccountId) -> CreateItemData<T> {13fn create_max_item_data<T: Config>(owner: T::CrossAccountId) -> CreateItemData<T> {
14 let const_data = create_data(CUSTOM_DATA_LIMIT as usize).try_into().unwrap();14 let const_data = create_data::<CUSTOM_DATA_LIMIT>();
15 let variable_data = create_data(CUSTOM_DATA_LIMIT as usize).try_into().unwrap();15 let variable_data = create_data::<CUSTOM_DATA_LIMIT>();
16 CreateItemData {16 CreateItemData::<T> {
17 const_data,17 const_data,
18 variable_data,18 variable_data,
19 owner,19 owner,
24 sender: &T::CrossAccountId,24 sender: &T::CrossAccountId,
25 owner: T::CrossAccountId,25 owner: T::CrossAccountId,
26) -> Result<TokenId, DispatchError> {26) -> Result<TokenId, DispatchError> {
27 <Pallet<T>>::create_item(&collection, sender, create_max_item_data(owner))?;27 <Pallet<T>>::create_item(&collection, sender, create_max_item_data::<T>(owner))?;
28 Ok(TokenId(<TokensMinted<T>>::get(&collection.id)))28 Ok(TokenId(<TokensMinted<T>>::get(&collection.id)))
29}29}
3030
53 owner: sub; collection: collection(owner);53 owner: sub; collection: collection(owner);
54 sender: cross_from_sub(owner); to: cross_sub;54 sender: cross_from_sub(owner); to: cross_sub;
55 };55 };
56 let data = (0..b).map(|_| create_max_item_data(to.clone())).collect();56 let data = (0..b).map(|_| create_max_item_data::<T>(to.clone())).collect();
57 }: {<Pallet<T>>::create_multiple_items(&collection, &sender, data)?}57 }: {<Pallet<T>>::create_multiple_items(&collection, &sender, data)?}
5858
59 }: {<Pallet<T>>::create_multiple_items(&collection, &sender, data)?}
60
59 burn_item {61 burn_item {
60 bench_init!{62 bench_init!{
61 owner: sub; collection: collection(owner);63 owner: sub; collection: collection(owner);
105 owner: cross_from_sub; sender: cross_sub;107 owner: cross_from_sub; sender: cross_sub;
106 };108 };
107 let item = create_max_item(&collection, &owner, sender.clone())?;109 let item = create_max_item(&collection, &owner, sender.clone())?;
108 let data = create_data(b as usize);110 let data = create_var_data(b).try_into().unwrap();
109 }: {<Pallet<T>>::set_variable_metadata(&collection, &sender, item, data)?}111 }: {<Pallet<T>>::set_variable_metadata(&collection, &sender, item, data)?}
110}112}
111113
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
12};12};
1313
14pub struct CommonWeights<T: Config>(PhantomData<T>);14pub struct CommonWeights<T: Config>(PhantomData<T>);
15impl<T: Config> CommonWeightInfo for CommonWeights<T> {15impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {
16 fn create_item() -> Weight {16 fn create_item() -> Weight {
17 <SelfWeightOf<T>>::create_item()17 <SelfWeightOf<T>>::create_item()
18 }18 }
51 to: &T::CrossAccountId,51 to: &T::CrossAccountId,
52) -> Result<CreateItemData<T>, DispatchError> {52) -> Result<CreateItemData<T>, DispatchError> {
53 match data {53 match data {
54 up_data_structs::CreateItemData::NFT(data) => Ok(CreateItemData {54 up_data_structs::CreateItemData::NFT(data) => Ok(CreateItemData::<T> {
55 const_data: data.const_data,55 const_data: data.const_data,
56 variable_data: data.variable_data,56 variable_data: data.variable_data,
57 owner: to.clone(),57 owner: to.clone(),
68 data: up_data_structs::CreateItemData,68 data: up_data_structs::CreateItemData,
69 ) -> DispatchResultWithPostInfo {69 ) -> DispatchResultWithPostInfo {
70 with_weight(70 with_weight(
71 <Pallet<T>>::create_item(self, &sender, map_create_data(data, &to)?),71 <Pallet<T>>::create_item(self, &sender, map_create_data::<T>(data, &to)?),
72 <CommonWeights<T>>::create_item(),72 <CommonWeights<T>>::create_item(),
73 )73 )
74 }74 }
modifiedpallets/nonfungible/src/erc.rsdiffbeforeafterboth
232 <Pallet<T>>::create_item(232 <Pallet<T>>::create_item(
233 self,233 self,
234 &caller,234 &caller,
235 CreateItemData {235 CreateItemData::<T> {
236 const_data: BoundedVec::default(),236 const_data: BoundedVec::default(),
237 variable_data: BoundedVec::default(),237 variable_data: BoundedVec::default(),
238 owner: to,238 owner: to,
268 <Pallet<T>>::create_item(268 <Pallet<T>>::create_item(
269 self,269 self,
270 &caller,270 &caller,
271 CreateItemData {271 CreateItemData::<T> {
272 const_data: Vec::<u8>::from(token_uri)272 const_data: Vec::<u8>::from(token_uri)
273 .try_into()273 .try_into()
274 .map_err(|_| "token uri is too long")?,274 .map_err(|_| "token uri is too long")?,
376 expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;376 expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;
377 }377 }
378 let data = (0..total_tokens)378 let data = (0..total_tokens)
379 .map(|_| CreateItemData {379 .map(|_| CreateItemData::<T> {
380 const_data: BoundedVec::default(),380 const_data: BoundedVec::default(),
381 variable_data: BoundedVec::default(),381 variable_data: BoundedVec::default(),
382 owner: to.clone(),382 owner: to.clone(),
409 }409 }
410 expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;410 expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;
411411
412 data.push(CreateItemData {412 data.push(CreateItemData::<T> {
413 const_data: Vec::<u8>::from(token_uri)413 const_data: Vec::<u8>::from(token_uri)
414 .try_into()414 .try_into()
415 .map_err(|_| "token uri is too long")?,415 .map_err(|_| "token uri is too long")?,
modifiedpallets/refungible/src/benchmarking.rsdiffbeforeafterboth
2use crate::{Pallet, Config, RefungibleHandle};2use crate::{Pallet, Config, RefungibleHandle};
33
4use sp_std::prelude::*;4use sp_std::prelude::*;
5use pallet_common::benchmarking::{create_collection_raw, create_data};5use pallet_common::benchmarking::{create_collection_raw, create_data, create_var_data};
6use frame_benchmarking::{benchmarks, account};6use frame_benchmarking::{benchmarks, account};
7use up_data_structs::{CollectionMode, MAX_ITEMS_PER_BATCH};7use up_data_structs::{CollectionMode, MAX_ITEMS_PER_BATCH, CUSTOM_DATA_LIMIT};
8use pallet_common::bench_init;8use pallet_common::bench_init;
9use core::convert::TryInto;9use core::convert::TryInto;
10use core::iter::IntoIterator;10use core::iter::IntoIterator;
1111
12const SEED: u32 = 1;12const SEED: u32 = 1;
1313
14fn create_max_item_data<T: Config>(14fn create_max_item_data<CrossAccountId: Ord>(
15 users: impl IntoIterator<Item = (T::CrossAccountId, u128)>,15 users: impl IntoIterator<Item = (CrossAccountId, u128)>,
16) -> CreateItemData<T> {16) -> CreateRefungibleExData<CrossAccountId> {
17 let const_data = create_data(CUSTOM_DATA_LIMIT as usize).try_into().unwrap();17 let const_data = create_data::<CUSTOM_DATA_LIMIT>();
18 let variable_data = create_data(CUSTOM_DATA_LIMIT as usize).try_into().unwrap();18 let variable_data = create_data::<CUSTOM_DATA_LIMIT>();
19 CreateItemData {19 CreateRefungibleExData {
20 const_data,20 const_data,
21 variable_data,21 variable_data,
22 users: users.into_iter().collect(),22 users: users
23 .into_iter()
24 .collect::<BTreeMap<_, _>>()
25 .try_into()
26 .unwrap(),
23 }27 }
24}28}
25fn create_max_item<T: Config>(29fn create_max_item<T: Config>(
modifiedpallets/refungible/src/common.rsdiffbeforeafterboth
8use sp_std::vec::Vec;8use sp_std::vec::Vec;
99
10use crate::{10use crate::{
11 AccountBalance, Allowance, Balance, Config, CreateItemData, Error, Owned, Pallet,11 AccountBalance, Allowance, Balance, Config, Error, Owned, Pallet, RefungibleHandle,
12 RefungibleHandle, SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,12 SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,
13};13};
1414
22}22}
2323
24pub struct CommonWeights<T: Config>(PhantomData<T>);24pub struct CommonWeights<T: Config>(PhantomData<T>);
25impl<T: Config> CommonWeightInfo for CommonWeights<T> {25impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {
26 fn create_item() -> Weight {26 fn create_item() -> Weight {
27 <SelfWeightOf<T>>::create_item()27 <SelfWeightOf<T>>::create_item()
28 }28 }
modifiedpallets/unique/src/benchmarking.rsdiffbeforeafterboth
5use frame_system::RawOrigin;5use frame_system::RawOrigin;
6use frame_benchmarking::{benchmarks, account};6use frame_benchmarking::{benchmarks, account};
7use up_data_structs::*;7use up_data_structs::*;
8use core::convert::TryInto;
9use sp_runtime::DispatchError;8use sp_runtime::DispatchError;
10use pallet_common::benchmarking::{create_data, create_u16_data};9use pallet_common::benchmarking::{create_data, create_var_data, create_u16_data};
1110
12const SEED: u32 = 1;11const SEED: u32 = 1;
1312
16 mode: CollectionMode,15 mode: CollectionMode,
17) -> Result<CollectionId, DispatchError> {16) -> Result<CollectionId, DispatchError> {
18 T::Currency::deposit_creating(&owner, T::CollectionCreationPrice::get());17 T::Currency::deposit_creating(&owner, T::CollectionCreationPrice::get());
19 let col_name = create_u16_data(MAX_COLLECTION_NAME_LENGTH)18 let col_name = create_u16_data::<MAX_COLLECTION_NAME_LENGTH>();
20 .try_into()
21 .unwrap();
22 let col_desc = create_u16_data(MAX_COLLECTION_DESCRIPTION_LENGTH)19 let col_desc = create_u16_data::<MAX_COLLECTION_DESCRIPTION_LENGTH>();
23 .try_into()
24 .unwrap();
25 let token_prefix = create_data(MAX_TOKEN_PREFIX_LENGTH).try_into().unwrap();20 let token_prefix = create_data::<MAX_TOKEN_PREFIX_LENGTH>();
26 <Pallet<T>>::create_collection(21 <Pallet<T>>::create_collection(
27 RawOrigin::Signed(owner).into(),22 RawOrigin::Signed(owner).into(),
28 col_name,23 col_name,
39benchmarks! {34benchmarks! {
40
41 create_collection {35 create_collection {
42 let col_name: Vec<u16> = create_u16_data(MAX_COLLECTION_NAME_LENGTH);36 let col_name = create_u16_data::<MAX_COLLECTION_NAME_LENGTH>();
43 let col_desc: Vec<u16> = create_u16_data(MAX_COLLECTION_DESCRIPTION_LENGTH);37 let col_desc = create_u16_data::<MAX_COLLECTION_DESCRIPTION_LENGTH>();
44 let token_prefix: Vec<u8> = create_data(MAX_TOKEN_PREFIX_LENGTH);38 let token_prefix = create_data::<MAX_TOKEN_PREFIX_LENGTH>();
45 let mode: CollectionMode = CollectionMode::NFT;39 let mode: CollectionMode = CollectionMode::NFT;
46 let caller: T::AccountId = account("caller", 0, SEED);40 let caller: T::AccountId = account("caller", 0, SEED);
47 T::Currency::deposit_creating(&caller, T::CollectionCreationPrice::get());41 T::Currency::deposit_creating(&caller, T::CollectionCreationPrice::get());
125119
126 let caller: T::AccountId = account("caller", 0, SEED);120 let caller: T::AccountId = account("caller", 0, SEED);
127 let collection = create_nft_collection::<T>(caller.clone())?;121 let collection = create_nft_collection::<T>(caller.clone())?;
128 let data = create_data(b as usize);122 let data = create_var_data(b);
129 }: set_offchain_schema(RawOrigin::Signed(caller.clone()), collection, data)123 }: set_offchain_schema(RawOrigin::Signed(caller.clone()), collection, data)
130124
131 set_const_on_chain_schema {125 set_const_on_chain_schema {
132 let b in 0..CONST_ON_CHAIN_SCHEMA_LIMIT;126 let b in 0..CONST_ON_CHAIN_SCHEMA_LIMIT;
133127
134 let caller: T::AccountId = account("caller", 0, SEED);128 let caller: T::AccountId = account("caller", 0, SEED);
135 let collection = create_nft_collection::<T>(caller.clone())?;129 let collection = create_nft_collection::<T>(caller.clone())?;
136 let data = create_data(b as usize);130 let data = create_var_data(b);
137 }: set_const_on_chain_schema(RawOrigin::Signed(caller.clone()), collection, data)131 }: set_const_on_chain_schema(RawOrigin::Signed(caller.clone()), collection, data)
138132
139 set_variable_on_chain_schema {133 set_variable_on_chain_schema {
140 let b in 0..VARIABLE_ON_CHAIN_SCHEMA_LIMIT;134 let b in 0..VARIABLE_ON_CHAIN_SCHEMA_LIMIT;
141135
142 let caller: T::AccountId = account("caller", 0, SEED);136 let caller: T::AccountId = account("caller", 0, SEED);
143 let collection = create_nft_collection::<T>(caller.clone())?;137 let collection = create_nft_collection::<T>(caller.clone())?;
144 let data = create_data(b as usize);138 let data = create_var_data(b);
145 }: set_variable_on_chain_schema(RawOrigin::Signed(caller.clone()), collection, data)139 }: set_variable_on_chain_schema(RawOrigin::Signed(caller.clone()), collection, data)
146140
147 set_schema_version {141 set_schema_version {
modifiedprimitives/data-structs/src/lib.rsdiffbeforeafterboth
76/// create_many call76/// create_many call
77pub const MAX_ITEMS_PER_BATCH: u32 = 200;77pub const MAX_ITEMS_PER_BATCH: u32 = 200;
7878
79parameter_types! {
80 pub const CustomDataLimit: u32 = CUSTOM_DATA_LIMIT;79pub type CustomDataLimit = ConstU32<CUSTOM_DATA_LIMIT>;
81}
8280
83#[derive(81#[derive(
84 Encode,82 Encode,
417 }415 }
418}416}
417
418fn bounded_debug<V, S>(v: &BoundedVec<V, S>, f: &mut fmt::Formatter) -> Result<(), fmt::Error>
419where
420 V: fmt::Debug,
421{
422 use core::fmt::Debug;
423 (&v as &Vec<V>).fmt(f)
424}
425
426#[cfg(feature = "serde1")]
427#[allow(dead_code)]
428mod bounded_map_serde {
429 use core::convert::TryFrom;
430 use sp_std::collections::btree_map::BTreeMap;
431 use frame_support::{traits::Get, storage::bounded_btree_map::BoundedBTreeMap};
432 use serde::{
433 ser::{self, Serialize},
434 de::{self, Deserialize, Error},
435 };
436 pub fn serialize<D, K, V, S>(
437 value: &BoundedBTreeMap<K, V, S>,
438 serializer: D,
439 ) -> Result<D::Ok, D::Error>
440 where
441 D: ser::Serializer,
442 K: Serialize + Ord,
443 V: Serialize,
444 {
445 (value as &BTreeMap<_, _>).serialize(serializer)
446 }
447
448 pub fn deserialize<'de, D, K, V, S>(
449 deserializer: D,
450 ) -> Result<BoundedBTreeMap<K, V, S>, D::Error>
451 where
452 D: de::Deserializer<'de>,
453 K: de::Deserialize<'de> + Ord,
454 V: de::Deserialize<'de>,
455 S: Get<u32>,
456 {
457 let map = <BTreeMap<K, V>>::deserialize(deserializer)?;
458 let len = map.len();
459 TryFrom::try_from(map).map_err(|_| D::Error::invalid_length(len, &"lesser size"))
460 }
461}
462
463fn bounded_map_debug<K, V, S>(
464 v: &BoundedBTreeMap<K, V, S>,
465 f: &mut fmt::Formatter,
466) -> Result<(), fmt::Error>
467where
468 K: fmt::Debug + Ord,
469 V: fmt::Debug,
470{
471 use core::fmt::Debug;
472 (&v as &BTreeMap<K, V>).fmt(f)
473}
419474
420#[derive(Encode, Decode, MaxEncodedLen, Default, PartialEq, Clone, Derivative, TypeInfo)]475#[derive(Encode, Decode, MaxEncodedLen, Default, PartialEq, Clone, Derivative, TypeInfo)]
421#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]476#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
422#[derivative(Debug)]477#[derivative(Debug)]
423pub struct CreateNftData {478pub struct CreateNftData {
424 #[cfg_attr(feature = "serde1", serde(with = "bounded_serde"))]479 #[cfg_attr(feature = "serde1", serde(with = "bounded_serde"))]
425 #[derivative(Debug = "ignore")]480 #[derivative(Debug(format_with = "bounded_debug"))]
426 pub const_data: BoundedVec<u8, CustomDataLimit>,481 pub const_data: BoundedVec<u8, CustomDataLimit>,
427 #[cfg_attr(feature = "serde1", serde(with = "bounded_serde"))]482 #[cfg_attr(feature = "serde1", serde(with = "bounded_serde"))]
428 #[derivative(Debug = "ignore")]483 #[derivative(Debug(format_with = "bounded_debug"))]
429 pub variable_data: BoundedVec<u8, CustomDataLimit>,484 pub variable_data: BoundedVec<u8, CustomDataLimit>,
430}485}
431486
440#[derivative(Debug)]495#[derivative(Debug)]
441pub struct CreateReFungibleData {496pub struct CreateReFungibleData {
442 #[cfg_attr(feature = "serde1", serde(with = "bounded_serde"))]497 #[cfg_attr(feature = "serde1", serde(with = "bounded_serde"))]
443 #[derivative(Debug = "ignore")]498 #[derivative(Debug(format_with = "bounded_debug"))]
444 pub const_data: BoundedVec<u8, CustomDataLimit>,499 pub const_data: BoundedVec<u8, CustomDataLimit>,
445 #[cfg_attr(feature = "serde1", serde(with = "bounded_serde"))]500 #[cfg_attr(feature = "serde1", serde(with = "bounded_serde"))]
446 #[derivative(Debug = "ignore")]501 #[derivative(Debug(format_with = "bounded_debug"))]
447 pub variable_data: BoundedVec<u8, CustomDataLimit>,502 pub variable_data: BoundedVec<u8, CustomDataLimit>,
448 pub pieces: u128,503 pub pieces: u128,
449}504}