difftreelog
refactor impl of `transfer` `transfer_from` functions, bencmarks for `Common` & `NFT` pallets
in: master
7 files changed
pallets/common/src/benchmarking.rsdiffbeforeafterboth--- a/pallets/common/src/benchmarking.rs
+++ b/pallets/common/src/benchmarking.rs
@@ -215,17 +215,9 @@
true,
)?;
- <Pallet<T>>::toggle_allowlist(
- &collection,
- &sender,
- &receiver,
- true,
- )?;
-
assert_eq!(collection_handle.permissions.access(), AccessMode::AllowList);
collection_handle.check_allowlist(&sender)?;
- collection_handle.check_allowlist(&receiver)?;
}: {collection_handle.check_allowlist(&sender)?;}
}
pallets/common/src/helpers.rsdiffbeforeafterboth--- /dev/null
+++ b/pallets/common/src/helpers.rs
@@ -0,0 +1,30 @@
+//! # Helpers module
+//!
+//! The module contains helpers.
+//!
+use frame_support::{
+ pallet_prelude::DispatchResultWithPostInfo,
+ weights::Weight,
+ dispatch::{DispatchErrorWithPostInfo, PostDispatchInfo},
+};
+
+/// Add weight for a `DispatchResultWithPostInfo`
+///
+/// - `target`: DispatchResultWithPostInfo to which weight will be added
+/// - `additional_weight`: Weight to be added
+pub fn add_weight_to_post_info(target: &mut DispatchResultWithPostInfo, additional_weight: Weight) {
+ match target {
+ Ok(PostDispatchInfo {
+ actual_weight: Some(weight),
+ ..
+ })
+ | Err(DispatchErrorWithPostInfo {
+ post_info: PostDispatchInfo {
+ actual_weight: Some(weight),
+ ..
+ },
+ ..
+ }) => *weight += additional_weight,
+ _ => {}
+ }
+}
pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -92,9 +92,9 @@
pub mod dispatch;
pub mod erc;
pub mod eth;
+pub mod helpers;
#[allow(missing_docs)]
pub mod weights;
-
/// Weight info.
pub type SelfWeightOf<T> = <T as Config>::WeightInfo;
pallets/nonfungible/src/benchmarking.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/benchmarking.rs
+++ b/pallets/nonfungible/src/benchmarking.rs
@@ -146,7 +146,7 @@
let item = create_max_item(&collection, &owner, owner_eth.clone())?;
}: {<Pallet<T>>::set_allowance_from(&collection, &sender, &owner_eth, item, Some(&spender))?}
- checks_for_transfer_from {
+ checks_allowed_raw {
bench_init!{
owner: sub; collection: collection(owner);
owner: cross_from_sub; sender: cross_sub; spender: cross_sub; receiver: cross_sub;
pallets/nonfungible/src/common.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -103,7 +103,7 @@
}
fn transfer_from() -> Weight {
- Self::transfer() + <SelfWeightOf<T>>::checks_for_transfer_from()
+ Self::transfer() + <SelfWeightOf<T>>::checks_allowed_raw()
}
fn burn_from() -> Weight {
pallets/nonfungible/src/lib.rsdiffbeforeafterboth109use pallet_common::{109use pallet_common::{110 Error as CommonError, Pallet as PalletCommon, Event as CommonEvent, CollectionHandle,110 Error as CommonError, Pallet as PalletCommon, Event as CommonEvent, CollectionHandle,111 eth::collection_id_to_address, SelfWeightOf as PalletCommonWeightOf,111 eth::collection_id_to_address, SelfWeightOf as PalletCommonWeightOf,112 weights::WeightInfo as CommonWeightInfo,112 weights::WeightInfo as CommonWeightInfo, helpers::add_weight_to_post_info,113};113};114use pallet_structure::{Pallet as PalletStructure, Error as StructureError};114use pallet_structure::{Pallet as PalletStructure, Error as StructureError};115use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};115use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};809 <CommonError<T>>::TransferNotAllowed809 <CommonError<T>>::TransferNotAllowed810 );810 );811811812 let mut actual_weight = <SelfWeightOf<T>>::transfer();812 let token_data =813 let token_data =813 <TokenData<T>>::get((collection.id, token)).ok_or(<CommonError<T>>::TokenNotFound)?;814 <TokenData<T>>::get((collection.id, token)).ok_or(<CommonError<T>>::TokenNotFound)?;814 ensure!(&token_data.owner == from, <CommonError<T>>::NoPermission);815 ensure!(&token_data.owner == from, <CommonError<T>>::NoPermission);815816816 let is_allow_list_mode = collection.permissions.access() == AccessMode::AllowList;817 if collection.permissions.access() == AccessMode::AllowList {817 if is_allow_list_mode {818 collection.check_allowlist(from)?;818 collection.check_allowlist(from)?;819 collection.check_allowlist(to)?;819 collection.check_allowlist(to)?;820 actual_weight += <PalletCommonWeightOf<T>>::check_accesslist() * 2;820 }821 }821 <PalletCommon<T>>::ensure_correct_receiver(to)?;822 <PalletCommon<T>>::ensure_correct_receiver(to)?;822823887 1,888 1,888 ));889 ));889890 let actual_weight = match is_allow_list_mode {891 true => Some(892 <SelfWeightOf<T>>::transfer() + <PalletCommonWeightOf<T>>::check_accesslist() * 2,893 ),894 false => Some(<SelfWeightOf<T>>::transfer()),895 };896890897 Ok(PostDispatchInfo {891 Ok(PostDispatchInfo {898 actual_weight,892 actual_weight: Some(actual_weight),899 pays_fee: Pays::Yes,893 pays_fee: Pays::Yes,900 })894 })901 }895 }1247 // =========1241 // =========124812421249 // Allowance is reset in [`transfer`]1243 // Allowance is reset in [`transfer`]1250 Self::transfer(collection, from, to, token, nesting_budget).map(|mut p| {1244 let mut result = Self::transfer(collection, from, to, token, nesting_budget);1251 p.actual_weight = Some(1245 add_weight_to_post_info(&mut result, <SelfWeightOf<T>>::checks_allowed_raw());1252 p.actual_weight.unwrap_or_default() + <SelfWeightOf<T>>::checks_for_transfer_from(),1246 result1253 );1254 p1255 })1256 }1247 }125712481258 /// Burn NFT token for `from` account.1249 /// Burn NFT token for `from` account.pallets/nonfungible/src/weights.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/weights.rs
+++ b/pallets/nonfungible/src/weights.rs
@@ -43,7 +43,7 @@
fn transfer() -> Weight;
fn approve() -> Weight;
fn approve_from() -> Weight;
- fn checks_for_transfer_from() -> Weight;
+ fn checks_allowed_raw() -> Weight;
fn burn_from() -> Weight;
fn set_token_property_permissions(b: u32, ) -> Weight;
fn set_token_properties(b: u32, ) -> Weight;
@@ -252,22 +252,10 @@
.saturating_add(T::DbWeight::get().reads(2_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
- /// Storage: Nonfungible Allowance (r:1 w:1)
- /// Proof: Nonfungible Allowance (max_values: None, max_size: Some(57), added: 2532, mode: MaxEncodedLen)
- /// Storage: Nonfungible TokenData (r:1 w:1)
- /// Proof: Nonfungible TokenData (max_values: None, max_size: Some(57), added: 2532, mode: MaxEncodedLen)
- /// Storage: Nonfungible AccountBalance (r:2 w:2)
- /// Proof: Nonfungible AccountBalance (max_values: None, max_size: Some(65), added: 2540, mode: MaxEncodedLen)
- /// Storage: Nonfungible Owned (r:0 w:2)
- /// Proof: Nonfungible Owned (max_values: None, max_size: Some(74), added: 2549, mode: MaxEncodedLen)
- fn transfer_from() -> Weight {
- // Proof Size summary in bytes:
- // Measured: `527`
- // Estimated: `10144`
- // Minimum execution time: 24_919_000 picoseconds.
- Weight::from_parts(25_333_000, 10144)
- .saturating_add(T::DbWeight::get().reads(4_u64))
- .saturating_add(T::DbWeight::get().writes(6_u64))
+ // Storage: Nonfungible Allowance (r:1 w:0)
+ fn checks_allowed_raw() -> Weight {
+ Weight::from_ref_time(3_341_000 as u64)
+ .saturating_add(T::DbWeight::get().reads(1 as u64))
}
/// Storage: Nonfungible Allowance (r:1 w:1)
/// Proof: Nonfungible Allowance (max_values: None, max_size: Some(57), added: 2532, mode: MaxEncodedLen)
@@ -578,22 +566,10 @@
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
- /// Storage: Nonfungible Allowance (r:1 w:1)
- /// Proof: Nonfungible Allowance (max_values: None, max_size: Some(57), added: 2532, mode: MaxEncodedLen)
- /// Storage: Nonfungible TokenData (r:1 w:1)
- /// Proof: Nonfungible TokenData (max_values: None, max_size: Some(57), added: 2532, mode: MaxEncodedLen)
- /// Storage: Nonfungible AccountBalance (r:2 w:2)
- /// Proof: Nonfungible AccountBalance (max_values: None, max_size: Some(65), added: 2540, mode: MaxEncodedLen)
- /// Storage: Nonfungible Owned (r:0 w:2)
- /// Proof: Nonfungible Owned (max_values: None, max_size: Some(74), added: 2549, mode: MaxEncodedLen)
- fn transfer_from() -> Weight {
- // Proof Size summary in bytes:
- // Measured: `527`
- // Estimated: `10144`
- // Minimum execution time: 24_919_000 picoseconds.
- Weight::from_parts(25_333_000, 10144)
- .saturating_add(RocksDbWeight::get().reads(4_u64))
- .saturating_add(RocksDbWeight::get().writes(6_u64))
+ // Storage: Nonfungible Allowance (r:1 w:0)
+ fn checks_allowed_raw() -> Weight {
+ Weight::from_ref_time(3_341_000 as u64)
+ .saturating_add(RocksDbWeight::get().reads(1 as u64))
}
/// Storage: Nonfungible Allowance (r:1 w:1)
/// Proof: Nonfungible Allowance (max_values: None, max_size: Some(57), added: 2532, mode: MaxEncodedLen)