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.rsdiffbeforeafterboth--- 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 @@
<CommonError<T>>::TransferNotAllowed
);
+ let mut actual_weight = <SelfWeightOf<T>>::transfer();
let token_data =
<TokenData<T>>::get((collection.id, token)).ok_or(<CommonError<T>>::TokenNotFound)?;
ensure!(&token_data.owner == from, <CommonError<T>>::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 += <PalletCommonWeightOf<T>>::check_accesslist() * 2;
}
<PalletCommon<T>>::ensure_correct_receiver(to)?;
@@ -887,15 +888,8 @@
1,
));
- let actual_weight = match is_allow_list_mode {
- true => Some(
- <SelfWeightOf<T>>::transfer() + <PalletCommonWeightOf<T>>::check_accesslist() * 2,
- ),
- false => Some(<SelfWeightOf<T>>::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() + <SelfWeightOf<T>>::checks_for_transfer_from(),
- );
- p
- })
+ let mut result = Self::transfer(collection, from, to, token, nesting_budget);
+ add_weight_to_post_info(&mut result, <SelfWeightOf<T>>::checks_allowed_raw());
+ result
}
/// Burn NFT token for `from` account.
pallets/nonfungible/src/weights.rsdiffbeforeafterboth43 fn transfer() -> Weight;43 fn transfer() -> Weight;44 fn approve() -> Weight;44 fn approve() -> Weight;45 fn approve_from() -> Weight;45 fn approve_from() -> Weight;46 fn checks_for_transfer_from() -> Weight;46 fn checks_allowed_raw() -> Weight;47 fn burn_from() -> Weight;47 fn burn_from() -> Weight;48 fn set_token_property_permissions(b: u32, ) -> Weight;48 fn set_token_property_permissions(b: u32, ) -> Weight;49 fn set_token_properties(b: u32, ) -> Weight;49 fn set_token_properties(b: u32, ) -> Weight;252 .saturating_add(T::DbWeight::get().reads(2_u64))252 .saturating_add(T::DbWeight::get().reads(2_u64))253 .saturating_add(T::DbWeight::get().writes(1_u64))253 .saturating_add(T::DbWeight::get().writes(1_u64))254 }254 }255 /// Storage: Nonfungible Allowance (r:1 w:1)255 // Storage: Nonfungible Allowance (r:1 w:0)256 /// Proof: Nonfungible Allowance (max_values: None, max_size: Some(57), added: 2532, mode: MaxEncodedLen)257 /// Storage: Nonfungible TokenData (r:1 w:1)258 /// Proof: Nonfungible TokenData (max_values: None, max_size: Some(57), added: 2532, mode: MaxEncodedLen)259 /// Storage: Nonfungible AccountBalance (r:2 w:2)260 /// Proof: Nonfungible AccountBalance (max_values: None, max_size: Some(65), added: 2540, mode: MaxEncodedLen)261 /// Storage: Nonfungible Owned (r:0 w:2)262 /// Proof: Nonfungible Owned (max_values: None, max_size: Some(74), added: 2549, mode: MaxEncodedLen)263 fn transfer_from() -> Weight {256 fn checks_allowed_raw() -> Weight {264 // Proof Size summary in bytes:265 // Measured: `527`266 // Estimated: `10144`267 // Minimum execution time: 24_919_000 picoseconds.268 Weight::from_parts(25_333_000, 10144)257 Weight::from_ref_time(3_341_000 as u64)269 .saturating_add(T::DbWeight::get().reads(4_u64))270 .saturating_add(T::DbWeight::get().writes(6_u64))258 .saturating_add(T::DbWeight::get().reads(1 as u64))271 }259 }272 /// Storage: Nonfungible Allowance (r:1 w:1)260 /// Storage: Nonfungible Allowance (r:1 w:1)273 /// Proof: Nonfungible Allowance (max_values: None, max_size: Some(57), added: 2532, mode: MaxEncodedLen)261 /// Proof: Nonfungible Allowance (max_values: None, max_size: Some(57), added: 2532, mode: MaxEncodedLen)578 .saturating_add(RocksDbWeight::get().reads(2_u64))566 .saturating_add(RocksDbWeight::get().reads(2_u64))579 .saturating_add(RocksDbWeight::get().writes(1_u64))567 .saturating_add(RocksDbWeight::get().writes(1_u64))580 }568 }581 /// Storage: Nonfungible Allowance (r:1 w:1)569 // Storage: Nonfungible Allowance (r:1 w:0)582 /// Proof: Nonfungible Allowance (max_values: None, max_size: Some(57), added: 2532, mode: MaxEncodedLen)583 /// Storage: Nonfungible TokenData (r:1 w:1)584 /// Proof: Nonfungible TokenData (max_values: None, max_size: Some(57), added: 2532, mode: MaxEncodedLen)585 /// Storage: Nonfungible AccountBalance (r:2 w:2)586 /// Proof: Nonfungible AccountBalance (max_values: None, max_size: Some(65), added: 2540, mode: MaxEncodedLen)587 /// Storage: Nonfungible Owned (r:0 w:2)588 /// Proof: Nonfungible Owned (max_values: None, max_size: Some(74), added: 2549, mode: MaxEncodedLen)589 fn transfer_from() -> Weight {570 fn checks_allowed_raw() -> Weight {590 // Proof Size summary in bytes:591 // Measured: `527`592 // Estimated: `10144`593 // Minimum execution time: 24_919_000 picoseconds.594 Weight::from_parts(25_333_000, 10144)571 Weight::from_ref_time(3_341_000 as u64)595 .saturating_add(RocksDbWeight::get().reads(4_u64))596 .saturating_add(RocksDbWeight::get().writes(6_u64))572 .saturating_add(RocksDbWeight::get().reads(1 as u64))597 }573 }598 /// Storage: Nonfungible Allowance (r:1 w:1)574 /// Storage: Nonfungible Allowance (r:1 w:1)599 /// Proof: Nonfungible Allowance (max_values: None, max_size: Some(57), added: 2532, mode: MaxEncodedLen)575 /// Proof: Nonfungible Allowance (max_values: None, max_size: Some(57), added: 2532, mode: MaxEncodedLen)