--- a/pallets/common/src/benchmarking.rs +++ b/pallets/common/src/benchmarking.rs @@ -215,17 +215,9 @@ true, )?; - >::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)?;} } --- /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, + _ => {} + } +} --- 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 = ::WeightInfo; --- 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())?; }: {>::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; --- a/pallets/nonfungible/src/common.rs +++ b/pallets/nonfungible/src/common.rs @@ -103,7 +103,7 @@ } fn transfer_from() -> Weight { - Self::transfer() + >::checks_for_transfer_from() + Self::transfer() + >::checks_allowed_raw() } fn burn_from() -> Weight { --- a/pallets/nonfungible/src/lib.rs +++ b/pallets/nonfungible/src/lib.rs @@ -109,7 +109,7 @@ use pallet_common::{ Error as CommonError, Pallet as PalletCommon, Event as CommonEvent, CollectionHandle, eth::collection_id_to_address, SelfWeightOf as PalletCommonWeightOf, - weights::WeightInfo as CommonWeightInfo, + weights::WeightInfo as CommonWeightInfo, helpers::add_weight_to_post_info, }; use pallet_structure::{Pallet as PalletStructure, Error as StructureError}; use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder}; @@ -809,14 +809,15 @@ >::TransferNotAllowed ); + let mut actual_weight = >::transfer(); let token_data = >::get((collection.id, token)).ok_or(>::TokenNotFound)?; ensure!(&token_data.owner == from, >::NoPermission); - let is_allow_list_mode = collection.permissions.access() == AccessMode::AllowList; - if is_allow_list_mode { + if collection.permissions.access() == AccessMode::AllowList { collection.check_allowlist(from)?; collection.check_allowlist(to)?; + actual_weight += >::check_accesslist() * 2; } >::ensure_correct_receiver(to)?; @@ -887,15 +888,8 @@ 1, )); - let actual_weight = match is_allow_list_mode { - true => Some( - >::transfer() + >::check_accesslist() * 2, - ), - false => Some(>::transfer()), - }; - Ok(PostDispatchInfo { - actual_weight, + actual_weight: Some(actual_weight), pays_fee: Pays::Yes, }) } @@ -1247,12 +1241,9 @@ // ========= // Allowance is reset in [`transfer`] - Self::transfer(collection, from, to, token, nesting_budget).map(|mut p| { - p.actual_weight = Some( - p.actual_weight.unwrap_or_default() + >::checks_for_transfer_from(), - ); - p - }) + let mut result = Self::transfer(collection, from, to, token, nesting_budget); + add_weight_to_post_info(&mut result, >::checks_allowed_raw()); + result } /// Burn NFT token for `from` account. --- 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)