git.delta.rocks / unique-network / refs/commits / 54a17a4acbdb

difftreelog

feat(fungible) benchmarking

Yaroslav Bolyukin2021-10-22parent: #278f1b9.patch.diff
in: master

5 files changed

modifiedpallets/fungible/Cargo.tomldiffbeforeafterboth
--- a/pallets/fungible/Cargo.toml
+++ b/pallets/fungible/Cargo.toml
@@ -20,6 +20,7 @@
 evm-coder = { default-features = false, path = '../../crates/evm-coder' }
 ethereum = { default-features = false, version = "0.9.0" }
 pallet-evm-coder-substrate = { default-features = false, path = '../evm-coder-substrate' }
+frame-benchmarking = { default-features = false, optional = true, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.10' }
 
 [features]
 default = ["std"]
@@ -32,5 +33,9 @@
     "pallet-common/std",
     "evm-coder/std",
     "ethereum/std",
+    'frame-benchmarking/std',
 ]
-runtime-benchmarks = []
+runtime-benchmarks = [
+    'frame-benchmarking',
+    'pallet-common/runtime-benchmarks',
+]
modifiedpallets/fungible/src/benchmarking.rsdiffbeforeafterboth
--- a/pallets/fungible/src/benchmarking.rs
+++ b/pallets/fungible/src/benchmarking.rs
@@ -1 +1,61 @@
-#![cfg(feature = "runtime-benchmarking")]
+use super::*;
+use crate::{Pallet, Config, FungibleHandle};
+
+use sp_std::prelude::*;
+use pallet_common::benchmarking::create_collection_raw;
+use frame_benchmarking::{benchmarks, account};
+use nft_data_structs::{CollectionMode};
+use pallet_common::bench_init;
+
+const SEED: u32 = 1;
+
+fn create_collection<T: Config>(owner: T::AccountId) -> Result<FungibleHandle<T>, DispatchError> {
+	create_collection_raw(
+		owner,
+		CollectionMode::Fungible(0),
+		<Pallet<T>>::init_collection,
+		FungibleHandle::cast,
+	)
+}
+
+benchmarks! {
+	create_item {
+		bench_init!{
+			owner: sub; collection: collection(owner);
+			sender: cross_from_sub(owner); to: cross_sub;
+		};
+	}: {<Pallet<T>>::create_item(&collection, &sender, (to, 200))?}
+
+	burn_item {
+		bench_init!{
+			owner: sub; collection: collection(owner);
+			owner: cross_from_sub; burner: cross_sub;
+		};
+		<Pallet<T>>::create_item(&collection, &owner, (burner.clone(), 200))?;
+	}: {<Pallet<T>>::burn(&collection, &burner, 100)?}
+
+	transfer {
+		bench_init!{
+			owner: sub; collection: collection(owner);
+			owner: cross_from_sub; sender: cross_sub; to: cross_sub;
+		};
+		<Pallet<T>>::create_item(&collection, &owner, (sender.clone(), 200))?;
+	}: {<Pallet<T>>::transfer(&collection, &sender, &to, 200)?}
+
+	approve {
+		bench_init!{
+			owner: sub; collection: collection(owner);
+			owner: cross_from_sub; sender: cross_sub; spender: cross_sub;
+		};
+		<Pallet<T>>::create_item(&collection, &owner, (sender.clone(), 200))?;
+	}: {<Pallet<T>>::set_allowance(&collection, &sender, &spender, 100)?}
+
+	transfer_from {
+		bench_init!{
+			owner: sub; collection: collection(owner);
+			owner: cross_from_sub; sender: cross_sub; spender: cross_sub; receiver: cross_sub;
+		};
+		<Pallet<T>>::create_item(&collection, &owner, (sender.clone(), 200))?;
+		<Pallet<T>>::set_allowance(&collection, &sender, &spender, 200)?;
+	}: {<Pallet<T>>::transfer_from(&collection, &spender, &sender, &receiver, 100)?}
+}
modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
before · pallets/fungible/src/common.rs
1use core::marker::PhantomData;23use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight};4use nft_data_structs::TokenId;5use pallet_common::{6	CommonCollectionOperations, CommonWeightInfo, account::CrossAccountId, with_weight,7};8use sp_runtime::ArithmeticError;9use sp_std::{vec::Vec, vec};1011use crate::{12	Allowance, Balance, Config, Error, FungibleHandle, Pallet, SelfWeightOf, weights::WeightInfo,13};1415pub struct CommonWeights<T: Config>(PhantomData<T>);16impl<T: Config> CommonWeightInfo for CommonWeights<T> {17	fn create_item() -> Weight {18		<SelfWeightOf<T>>::create_item()19	}2021	fn create_multiple_items(_amount: u32) -> Weight {22		Self::create_item()23	}2425	fn burn_item() -> Weight {26		<SelfWeightOf<T>>::burn_item()27	}2829	fn transfer() -> Weight {30		<SelfWeightOf<T>>::transfer()31	}3233	fn approve() -> Weight {34		<SelfWeightOf<T>>::approve()35	}3637	fn transfer_from() -> Weight {38		<SelfWeightOf<T>>::transfer_from()39	}4041	fn set_variable_metadata(_bytes: u32) -> Weight {42		// Error43		044	}45}4647impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {48	fn create_item(49		&self,50		sender: T::CrossAccountId,51		to: T::CrossAccountId,52		data: nft_data_structs::CreateItemData,53	) -> DispatchResultWithPostInfo {54		match data {55			nft_data_structs::CreateItemData::Fungible(data) => with_weight(56				<Pallet<T>>::create_item(self, &sender, (to, data.value)),57				<SelfWeightOf<T>>::create_item(),58			),59			_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),60		}61	}6263	fn create_multiple_items(64		&self,65		sender: T::CrossAccountId,66		to: T::CrossAccountId,67		data: Vec<nft_data_structs::CreateItemData>,68	) -> DispatchResultWithPostInfo {69		let mut sum: u128 = 0;70		for data in data {71			match data {72				nft_data_structs::CreateItemData::Fungible(data) => {73					sum = sum74						.checked_add(data.value)75						.ok_or(ArithmeticError::Overflow)?;76				}77				_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),78			}79		}8081		with_weight(82			<Pallet<T>>::create_item(self, &sender, (to, sum)),83			<SelfWeightOf<T>>::create_item(),84		)85	}8687	fn burn_item(88		&self,89		sender: T::CrossAccountId,90		token: TokenId,91		amount: u128,92	) -> DispatchResultWithPostInfo {93		ensure!(94			token == TokenId::default(),95			<Error<T>>::FungibleItemsHaveNoId96		);9798		with_weight(99			<Pallet<T>>::burn(self, &sender, amount),100			<SelfWeightOf<T>>::burn_item(),101		)102	}103104	fn transfer(105		&self,106		from: T::CrossAccountId,107		to: T::CrossAccountId,108		token: TokenId,109		amount: u128,110	) -> DispatchResultWithPostInfo {111		ensure!(112			token == TokenId::default(),113			<Error<T>>::FungibleItemsHaveNoId114		);115116		with_weight(117			<Pallet<T>>::transfer(&self, &from, &to, amount),118			<SelfWeightOf<T>>::transfer(),119		)120	}121122	fn approve(123		&self,124		sender: T::CrossAccountId,125		spender: T::CrossAccountId,126		token: TokenId,127		amount: u128,128	) -> DispatchResultWithPostInfo {129		ensure!(130			token == TokenId::default(),131			<Error<T>>::FungibleItemsHaveNoId132		);133134		with_weight(135			<Pallet<T>>::set_allowance(&self, &sender, &spender, amount),136			<SelfWeightOf<T>>::approve(),137		)138	}139140	fn transfer_from(141		&self,142		sender: T::CrossAccountId,143		from: T::CrossAccountId,144		to: T::CrossAccountId,145		token: TokenId,146		amount: u128,147	) -> DispatchResultWithPostInfo {148		ensure!(149			token == TokenId::default(),150			<Error<T>>::FungibleItemsHaveNoId151		);152153		with_weight(154			<Pallet<T>>::transfer_from(&self, &sender, &from, &to, amount),155			<SelfWeightOf<T>>::transfer_from(),156		)157	}158159	fn set_variable_metadata(160		&self,161		_sender: T::CrossAccountId,162		_token: TokenId,163		_data: Vec<u8>,164	) -> DispatchResultWithPostInfo {165		fail!(<Error<T>>::FungibleItemsHaveData)166	}167168	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {169		if <Balance<T>>::get((self.id, account.as_sub())) != 0 {170			vec![TokenId::default()]171		} else {172			vec![]173		}174	}175176	fn token_exists(&self, token: TokenId) -> bool {177		token == TokenId::default()178	}179180	fn last_token_id(&self) -> TokenId {181		TokenId::default()182	}183184	fn token_owner(&self, _token: TokenId) -> T::CrossAccountId {185		T::CrossAccountId::default()186	}187	fn const_metadata(&self, _token: TokenId) -> Vec<u8> {188		Vec::new()189	}190	fn variable_metadata(&self, _token: TokenId) -> Vec<u8> {191		Vec::new()192	}193194	fn collection_tokens(&self) -> u32 {195		1196	}197198	fn account_balance(&self, account: T::CrossAccountId) -> u32 {199		if <Balance<T>>::get((self.id, account.as_sub())) != 0 {200			1201		} else {202			0203		}204	}205206	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {207		if token != TokenId::default() {208			return 0;209		}210		<Balance<T>>::get((self.id, account.as_sub()))211	}212213	fn allowance(214		&self,215		sender: T::CrossAccountId,216		spender: T::CrossAccountId,217		token: TokenId,218	) -> u128 {219		if token != TokenId::default() {220			return 0;221		}222		<Allowance<T>>::get((self.id, sender.as_sub(), spender.as_sub()))223	}224}
after · pallets/fungible/src/common.rs
1use core::marker::PhantomData;23use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight};4use nft_data_structs::TokenId;5use pallet_common::{6	CommonCollectionOperations, CommonWeightInfo, account::CrossAccountId, with_weight,7};8use sp_runtime::ArithmeticError;9use sp_std::{vec::Vec, vec};1011use crate::{12	Allowance, Balance, Config, Error, FungibleHandle, Pallet, SelfWeightOf, weights::WeightInfo,13};1415pub struct CommonWeights<T: Config>(PhantomData<T>);16impl<T: Config> CommonWeightInfo for CommonWeights<T> {17	fn create_item() -> Weight {18		<SelfWeightOf<T>>::create_item()19	}2021	fn create_multiple_items(_amount: u32) -> Weight {22		Self::create_item()23	}2425	fn burn_item() -> Weight {26		<SelfWeightOf<T>>::burn_item()27	}2829	fn transfer() -> Weight {30		<SelfWeightOf<T>>::transfer()31	}3233	fn approve() -> Weight {34		<SelfWeightOf<T>>::approve()35	}3637	fn transfer_from() -> Weight {38		<SelfWeightOf<T>>::transfer_from()39	}4041	fn set_variable_metadata(_bytes: u32) -> Weight {42		// Error43		044	}45}4647impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {48	fn create_item(49		&self,50		sender: T::CrossAccountId,51		to: T::CrossAccountId,52		data: nft_data_structs::CreateItemData,53	) -> DispatchResultWithPostInfo {54		match data {55			nft_data_structs::CreateItemData::Fungible(data) => with_weight(56				<Pallet<T>>::create_item(self, &sender, (to, data.value)),57				<CommonWeights<T>>::create_item(),58			),59			_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),60		}61	}6263	fn create_multiple_items(64		&self,65		sender: T::CrossAccountId,66		to: T::CrossAccountId,67		data: Vec<nft_data_structs::CreateItemData>,68	) -> DispatchResultWithPostInfo {69		let mut sum: u128 = 0;70		for data in data {71			match data {72				nft_data_structs::CreateItemData::Fungible(data) => {73					sum = sum74						.checked_add(data.value)75						.ok_or(ArithmeticError::Overflow)?;76				}77				_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),78			}79		}8081		with_weight(82			<Pallet<T>>::create_item(self, &sender, (to, sum)),83			<CommonWeights<T>>::create_item(),84		)85	}8687	fn burn_item(88		&self,89		sender: T::CrossAccountId,90		token: TokenId,91		amount: u128,92	) -> DispatchResultWithPostInfo {93		ensure!(94			token == TokenId::default(),95			<Error<T>>::FungibleItemsHaveNoId96		);9798		with_weight(99			<Pallet<T>>::burn(self, &sender, amount),100			<CommonWeights<T>>::burn_item(),101		)102	}103104	fn transfer(105		&self,106		from: T::CrossAccountId,107		to: T::CrossAccountId,108		token: TokenId,109		amount: u128,110	) -> DispatchResultWithPostInfo {111		ensure!(112			token == TokenId::default(),113			<Error<T>>::FungibleItemsHaveNoId114		);115116		with_weight(117			<Pallet<T>>::transfer(&self, &from, &to, amount),118			<CommonWeights<T>>::transfer(),119		)120	}121122	fn approve(123		&self,124		sender: T::CrossAccountId,125		spender: T::CrossAccountId,126		token: TokenId,127		amount: u128,128	) -> DispatchResultWithPostInfo {129		ensure!(130			token == TokenId::default(),131			<Error<T>>::FungibleItemsHaveNoId132		);133134		with_weight(135			<Pallet<T>>::set_allowance(&self, &sender, &spender, amount),136			<CommonWeights<T>>::approve(),137		)138	}139140	fn transfer_from(141		&self,142		sender: T::CrossAccountId,143		from: T::CrossAccountId,144		to: T::CrossAccountId,145		token: TokenId,146		amount: u128,147	) -> DispatchResultWithPostInfo {148		ensure!(149			token == TokenId::default(),150			<Error<T>>::FungibleItemsHaveNoId151		);152153		with_weight(154			<Pallet<T>>::transfer_from(&self, &sender, &from, &to, amount),155			<CommonWeights<T>>::transfer_from(),156		)157	}158159	fn set_variable_metadata(160		&self,161		_sender: T::CrossAccountId,162		_token: TokenId,163		_data: Vec<u8>,164	) -> DispatchResultWithPostInfo {165		fail!(<Error<T>>::FungibleItemsHaveData)166	}167168	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {169		if <Balance<T>>::get((self.id, account.as_sub())) != 0 {170			vec![TokenId::default()]171		} else {172			vec![]173		}174	}175176	fn token_exists(&self, token: TokenId) -> bool {177		token == TokenId::default()178	}179180	fn last_token_id(&self) -> TokenId {181		TokenId::default()182	}183184	fn token_owner(&self, _token: TokenId) -> T::CrossAccountId {185		T::CrossAccountId::default()186	}187	fn const_metadata(&self, _token: TokenId) -> Vec<u8> {188		Vec::new()189	}190	fn variable_metadata(&self, _token: TokenId) -> Vec<u8> {191		Vec::new()192	}193194	fn collection_tokens(&self) -> u32 {195		1196	}197198	fn account_balance(&self, account: T::CrossAccountId) -> u32 {199		if <Balance<T>>::get((self.id, account.as_sub())) != 0 {200			1201		} else {202			0203		}204	}205206	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {207		if token != TokenId::default() {208			return 0;209		}210		<Balance<T>>::get((self.id, account.as_sub()))211	}212213	fn allowance(214		&self,215		sender: T::CrossAccountId,216		spender: T::CrossAccountId,217		token: TokenId,218	) -> u128 {219		if token != TokenId::default() {220			return 0;221		}222		<Allowance<T>>::get((self.id, sender.as_sub(), spender.as_sub()))223	}224}
modifiedpallets/fungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/fungible/src/lib.rs
+++ b/pallets/fungible/src/lib.rs
@@ -13,6 +13,7 @@
 pub use pallet::*;
 
 use crate::erc::ERC20Events;
+#[cfg(feature = "runtime-benchmarks")]
 pub mod benchmarking;
 pub mod common;
 pub mod erc;
modifiedpallets/fungible/src/weights.rsdiffbeforeafterboth
--- a/pallets/fungible/src/weights.rs
+++ b/pallets/fungible/src/weights.rs
@@ -1,3 +1,27 @@
+// Template adopted from https://github.com/paritytech/substrate/blob/master/.maintain/frame-weight-template.hbs
+
+//! Autogenerated weights for pallet_fungible
+//!
+//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
+//! DATE: 2021-10-21, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
+//! EXECUTION: None, WASM-EXECUTION: Compiled, CHAIN: None, DB CACHE: 128
+
+// Executed Command:
+// target/release/nft
+// benchmark
+// --pallet
+// pallet-fungible
+// --wasm-execution
+// compiled
+// --extrinsic
+// *
+// --template
+// .maintain/frame-weight-template.hbs
+// --steps=50
+// --repeat=20
+// --output=./pallets/fungible/src/weights.rs
+
+
 #![cfg_attr(rustfmt, rustfmt_skip)]
 #![allow(unused_parens)]
 #![allow(unused_imports)]
@@ -5,6 +29,7 @@
 use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
 use sp_std::marker::PhantomData;
 
+/// Weight functions needed for pallet_fungible.
 pub trait WeightInfo {
 	fn create_item() -> Weight;
 	fn burn_item() -> Weight;
@@ -13,19 +38,79 @@
 	fn transfer_from() -> Weight;
 }
 
+/// Weights for pallet_fungible using the Substrate node and recommended hardware.
 pub struct SubstrateWeight<T>(PhantomData<T>);
 impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
-    fn create_item() -> Weight {0}
-	fn burn_item() -> Weight {0}
-	fn transfer() -> Weight {0}
-	fn approve() -> Weight {0}
-	fn transfer_from() -> Weight {0}
+	// Storage: Fungible Balance (r:1 w:1)
+	// Storage: Fungible TotalSupply (r:0 w:1)
+	fn create_item() -> Weight {
+		(12_069_000 as Weight)
+			.saturating_add(T::DbWeight::get().reads(1 as Weight))
+			.saturating_add(T::DbWeight::get().writes(2 as Weight))
+	}
+	// Storage: Fungible TotalSupply (r:1 w:1)
+	// Storage: Fungible Balance (r:1 w:1)
+	fn burn_item() -> Weight {
+		(14_096_000 as Weight)
+			.saturating_add(T::DbWeight::get().reads(2 as Weight))
+			.saturating_add(T::DbWeight::get().writes(2 as Weight))
+	}
+	// Storage: Fungible Balance (r:2 w:2)
+	fn transfer() -> Weight {
+		(15_436_000 as Weight)
+			.saturating_add(T::DbWeight::get().reads(2 as Weight))
+			.saturating_add(T::DbWeight::get().writes(2 as Weight))
+	}
+	// Storage: Fungible Balance (r:1 w:0)
+	// Storage: Fungible Allowance (r:0 w:1)
+	fn approve() -> Weight {
+		(12_867_000 as Weight)
+			.saturating_add(T::DbWeight::get().reads(1 as Weight))
+			.saturating_add(T::DbWeight::get().writes(1 as Weight))
+	}
+	// Storage: Fungible Allowance (r:1 w:1)
+	// Storage: Fungible Balance (r:2 w:2)
+	fn transfer_from() -> Weight {
+		(21_462_000 as Weight)
+			.saturating_add(T::DbWeight::get().reads(3 as Weight))
+			.saturating_add(T::DbWeight::get().writes(3 as Weight))
+	}
 }
 
+// For backwards compatibility and tests
 impl WeightInfo for () {
-    fn create_item() -> Weight {0}
-	fn burn_item() -> Weight {0}
-	fn transfer() -> Weight {0}
-	fn approve() -> Weight {0}
-	fn transfer_from() -> Weight {0}
+	// Storage: Fungible Balance (r:1 w:1)
+	// Storage: Fungible TotalSupply (r:0 w:1)
+	fn create_item() -> Weight {
+		(12_069_000 as Weight)
+			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(2 as Weight))
+	}
+	// Storage: Fungible TotalSupply (r:1 w:1)
+	// Storage: Fungible Balance (r:1 w:1)
+	fn burn_item() -> Weight {
+		(14_096_000 as Weight)
+			.saturating_add(RocksDbWeight::get().reads(2 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(2 as Weight))
+	}
+	// Storage: Fungible Balance (r:2 w:2)
+	fn transfer() -> Weight {
+		(15_436_000 as Weight)
+			.saturating_add(RocksDbWeight::get().reads(2 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(2 as Weight))
+	}
+	// Storage: Fungible Balance (r:1 w:0)
+	// Storage: Fungible Allowance (r:0 w:1)
+	fn approve() -> Weight {
+		(12_867_000 as Weight)
+			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
+	}
+	// Storage: Fungible Allowance (r:1 w:1)
+	// Storage: Fungible Balance (r:2 w:2)
+	fn transfer_from() -> Weight {
+		(21_462_000 as Weight)
+			.saturating_add(RocksDbWeight::get().reads(3 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(3 as Weight))
+	}
 }