difftreelog
feat(weight) Changed weight calculation system for transfer & transfer_from (FT)
in: master
9 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6558,7 +6558,7 @@
[[package]]
name = "pallet-fungible"
-version = "0.1.10"
+version = "0.1.11"
dependencies = [
"evm-coder",
"frame-benchmarking",
pallets/foreign-assets/src/impl_fungibles.rsdiffbeforeafterboth--- a/pallets/foreign-assets/src/impl_fungibles.rs
+++ b/pallets/foreign-assets/src/impl_fungibles.rs
@@ -452,7 +452,8 @@
&T::CrossAccountId::from_sub(dest.clone()),
amount.into(),
&Value::new(0),
- )?;
+ )
+ .map_err(|e| e.error)?;
Ok(amount)
}
pallets/fungible/CHANGELOG.mddiffbeforeafterboth--- a/pallets/fungible/CHANGELOG.md
+++ b/pallets/fungible/CHANGELOG.md
@@ -4,6 +4,12 @@
<!-- bureaucrate goes here -->
+## [0.1.11] - 2023-03-28
+
+### Fixed
+
+- The weight of `transfer` and `transfer_from`.
+
## [0.1.10] - 2023-02-01
### Added
pallets/fungible/Cargo.tomldiffbeforeafterboth--- a/pallets/fungible/Cargo.toml
+++ b/pallets/fungible/Cargo.toml
@@ -2,7 +2,7 @@
edition = "2021"
license = "GPLv3"
name = "pallet-fungible"
-version = "0.1.10"
+version = "0.1.11"
[dependencies]
# Note: `package = "parity-scale-codec"` must be supplied since the `Encode` macro searches for it.
pallets/fungible/src/benchmarking.rsdiffbeforeafterboth--- a/pallets/fungible/src/benchmarking.rs
+++ b/pallets/fungible/src/benchmarking.rs
@@ -92,14 +92,22 @@
<Pallet<T>>::create_item(&collection, &owner, (owner_eth.clone(), 200), &Unlimited)?;
}: {<Pallet<T>>::set_allowance_from(&collection, &sender, &owner_eth, &spender, 100)?}
- transfer_from {
+ check_allowed_raw {
bench_init!{
owner: sub; collection: collection(owner);
- owner: cross_from_sub; sender: cross_sub; spender: cross_sub; receiver: cross_sub;
+ owner: cross_from_sub; sender: cross_sub; spender: cross_sub;
};
<Pallet<T>>::create_item(&collection, &owner, (sender.clone(), 200), &Unlimited)?;
<Pallet<T>>::set_allowance(&collection, &sender, &spender, 200)?;
- }: {<Pallet<T>>::transfer_from(&collection, &spender, &sender, &receiver, 100, &Unlimited)?}
+ }: {<Pallet<T>>::check_allowed(&collection, &spender, &sender, 200, &Unlimited)?;}
+
+ set_allowance_unchecked_raw {
+ 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), &Unlimited)?;
+ }: {<Pallet<T>>::set_allowance_unchecked(&collection, &sender, &spender, 200);}
burn_from {
bench_init!{
pallets/fungible/src/common.rsdiffbeforeafterboth--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -22,7 +22,7 @@
};
use pallet_common::{
CommonCollectionOperations, CommonWeightInfo, RefungibleExtensions, with_weight,
- weights::WeightInfo as _,
+ weights::WeightInfo as _, SelfWeightOf as PalletCommonWeightOf,
};
use pallet_structure::Error as StructureError;
use sp_runtime::ArithmeticError;
@@ -78,7 +78,7 @@
}
fn transfer() -> Weight {
- <SelfWeightOf<T>>::transfer()
+ <SelfWeightOf<T>>::transfer() + <PalletCommonWeightOf<T>>::check_accesslist() * 2
}
fn approve() -> Weight {
@@ -90,7 +90,9 @@
}
fn transfer_from() -> Weight {
- <SelfWeightOf<T>>::transfer_from()
+ <SelfWeightOf<T>>::transfer()
+ + <SelfWeightOf<T>>::check_allowed_raw()
+ + <SelfWeightOf<T>>::set_allowance_unchecked_raw()
}
fn burn_from() -> Weight {
@@ -232,10 +234,7 @@
<Error<T>>::FungibleItemsHaveNoId
);
- with_weight(
- <Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget),
- <CommonWeights<T>>::transfer(),
- )
+ <Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget)
}
fn approve(
@@ -289,10 +288,7 @@
<Error<T>>::FungibleItemsHaveNoId
);
- with_weight(
- <Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget),
- <CommonWeights<T>>::transfer_from(),
- )
+ <Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget)
}
fn burn_from(
pallets/fungible/src/erc.rsdiffbeforeafterboth--- a/pallets/fungible/src/erc.rs
+++ b/pallets/fungible/src/erc.rs
@@ -26,6 +26,7 @@
CollectionHandle,
erc::{CommonEvmHandler, PrecompileResult, CollectionCall},
eth::CrossAddress,
+ CommonWeightInfo as _,
};
use sp_std::vec::Vec;
use pallet_evm::{account::CrossAccountId, PrecompileHandle};
@@ -39,7 +40,7 @@
use crate::{
Allowance, Balance, Config, FungibleHandle, Pallet, TotalSupply, SelfWeightOf,
- weights::WeightInfo,
+ weights::WeightInfo, common::CommonWeights,
};
frontier_contract! {
@@ -99,7 +100,7 @@
let balance = <Balance<T>>::get((self.id, owner));
Ok(balance.into())
}
- #[weight(<SelfWeightOf<T>>::transfer())]
+ #[weight(<CommonWeights<T>>::transfer())]
fn transfer(&mut self, caller: Caller, to: Address, amount: U256) -> Result<bool> {
let caller = T::CrossAccountId::from_eth(caller);
let to = T::CrossAccountId::from_eth(to);
@@ -112,7 +113,7 @@
Ok(true)
}
- #[weight(<SelfWeightOf<T>>::transfer_from())]
+ #[weight(<CommonWeights<T>>::transfer_from())]
fn transfer_from(
&mut self,
caller: Caller,
@@ -129,7 +130,7 @@
.weight_calls_budget(<StructureWeight<T>>::find_parent());
<Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)
- .map_err(dispatch_to_evm::<T>)?;
+ .map_err(|e| dispatch_to_evm::<T>(e.error))?;
Ok(true)
}
#[weight(<SelfWeightOf<T>>::approve())]
@@ -201,7 +202,7 @@
let budget = self
.recorder
.weight_calls_budget(<StructureWeight<T>>::find_parent());
- <Pallet<T>>::create_item(&self, &caller, (to, amount), &budget)
+ <Pallet<T>>::create_item(self, &caller, (to, amount), &budget)
.map_err(dispatch_to_evm::<T>)?;
Ok(true)
}
@@ -289,7 +290,7 @@
Ok(true)
}
- #[weight(<SelfWeightOf<T>>::transfer())]
+ #[weight(<CommonWeights<T>>::transfer())]
fn transfer_cross(&mut self, caller: Caller, to: CrossAddress, amount: U256) -> Result<bool> {
let caller = T::CrossAccountId::from_eth(caller);
let to = to.into_sub_cross_account::<T>()?;
@@ -302,7 +303,7 @@
Ok(true)
}
- #[weight(<SelfWeightOf<T>>::transfer_from())]
+ #[weight(<CommonWeights<T>>::transfer_from())]
fn transfer_from_cross(
&mut self,
caller: Caller,
@@ -319,7 +320,7 @@
.weight_calls_budget(<StructureWeight<T>>::find_parent());
<Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)
- .map_err(dispatch_to_evm::<T>)?;
+ .map_err(|e| dispatch_to_evm::<T>(e.error))?;
Ok(true)
}
pallets/fungible/src/lib.rsdiffbeforeafterboth808081use core::ops::Deref;81use core::ops::Deref;82use evm_coder::ToLog;82use evm_coder::ToLog;83use frame_support::ensure;83use frame_support::{84 ensure,85 pallet_prelude::{DispatchResultWithPostInfo, Pays},86 dispatch::PostDispatchInfo,87};84use pallet_evm::account::CrossAccountId;88use pallet_evm::account::CrossAccountId;85use up_data_structs::{89use up_data_structs::{86 AccessMode, CollectionId, CollectionFlags, TokenId, CreateCollectionData,90 AccessMode, CollectionId, CollectionFlags, TokenId, CreateCollectionData,87 mapping::TokenAddressMapping, budget::Budget, PropertyKey, Property,91 mapping::TokenAddressMapping, budget::Budget, PropertyKey, Property,88};92};89use pallet_common::{93use pallet_common::{90 Error as CommonError, Event as CommonEvent, Pallet as PalletCommon,94 Error as CommonError, Event as CommonEvent, Pallet as PalletCommon,91 eth::collection_id_to_address,95 eth::collection_id_to_address, SelfWeightOf as PalletCommonWeightOf,96 weights::WeightInfo as CommonWeightInfo, helpers::add_weight_to_post_info,92};97};93use pallet_evm::Pallet as PalletEvm;98use pallet_evm::Pallet as PalletEvm;94use pallet_structure::Pallet as PalletStructure;99use pallet_structure::Pallet as PalletStructure;95use pallet_evm_coder_substrate::WithRecorder;100use pallet_evm_coder_substrate::WithRecorder;96use sp_core::H160;101use sp_core::H160;97use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};102use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};98use sp_std::{collections::btree_map::BTreeMap, vec::Vec};103use sp_std::{collections::btree_map::BTreeMap, vec::Vec};99104use weights::WeightInfo;100pub use pallet::*;105pub use pallet::*;101106102use crate::erc::ERC20Events;107use crate::erc::ERC20Events;389 to: &T::CrossAccountId,394 to: &T::CrossAccountId,390 amount: u128,395 amount: u128,391 nesting_budget: &dyn Budget,396 nesting_budget: &dyn Budget,392 ) -> DispatchResult {397 ) -> DispatchResultWithPostInfo {393 ensure!(398 ensure!(394 collection.limits.transfers_enabled(),399 collection.limits.transfers_enabled(),395 <CommonError<T>>::TransferNotAllowed,400 <CommonError<T>>::TransferNotAllowed,396 );401 );402403 let mut actual_weight = <SelfWeightOf<T>>::transfer();397404398 if collection.permissions.access() == AccessMode::AllowList {405 if collection.permissions.access() == AccessMode::AllowList {399 collection.check_allowlist(from)?;406 collection.check_allowlist(from)?;400 collection.check_allowlist(to)?;407 collection.check_allowlist(to)?;408 actual_weight += <PalletCommonWeightOf<T>>::check_accesslist() * 2;401 }409 }402 <PalletCommon<T>>::ensure_correct_receiver(to)?;410 <PalletCommon<T>>::ensure_correct_receiver(to)?;403452 amount,459 amount,453 ));460 ));461454 Ok(())462 Ok(PostDispatchInfo {463 actual_weight: Some(actual_weight),464 pays_fee: Pays::Yes,465 })455 }466 }456467457 /// Minting tokens for multiple IDs.468 /// Minting tokens for multiple IDs.464 nesting_budget: &dyn Budget,475 nesting_budget: &dyn Budget,465 ) -> DispatchResult {476 ) -> DispatchResult {466 let total_supply = data477 let total_supply = data467 .iter()478 .values()468 .map(|(_, v)| *v)479 .copied()469 .try_fold(<TotalSupply<T>>::get(collection.id), |acc, v| {480 .try_fold(<TotalSupply<T>>::get(collection.id), |acc, v| {470 acc.checked_add(v)481 acc.checked_add(v)471 })482 })726 to: &T::CrossAccountId,736 to: &T::CrossAccountId,727 amount: u128,737 amount: u128,728 nesting_budget: &dyn Budget,738 nesting_budget: &dyn Budget,729 ) -> DispatchResult {739 ) -> DispatchResultWithPostInfo {730 let allowance = Self::check_allowed(collection, spender, from, amount, nesting_budget)?;740 let allowance = Self::check_allowed(collection, spender, from, amount, nesting_budget)?;731741732 // =========742 // =========733743734 Self::transfer(collection, from, to, amount, nesting_budget)?;744 let mut result = Self::transfer(collection, from, to, amount, nesting_budget);745 add_weight_to_post_info(&mut result, <SelfWeightOf<T>>::check_allowed_raw());746 result?;747735 if let Some(allowance) = allowance {748 if let Some(allowance) = allowance {736 Self::set_allowance_unchecked(collection, from, spender, allowance);749 Self::set_allowance_unchecked(collection, from, spender, allowance);750 add_weight_to_post_info(751 &mut result,752 <SelfWeightOf<T>>::set_allowance_unchecked_raw(),753 )737 }754 }738 Ok(())755 result739 }756 }740757741 /// Burn fungible tokens from the account.758 /// Burn fungible tokens from the account.pallets/fungible/src/weights.rsdiffbeforeafterboth--- a/pallets/fungible/src/weights.rs
+++ b/pallets/fungible/src/weights.rs
@@ -40,7 +40,8 @@
fn transfer() -> Weight;
fn approve() -> Weight;
fn approve_from() -> Weight;
- fn transfer_from() -> Weight;
+ fn check_allowed_raw() -> Weight;
+ fn set_allowance_unchecked_raw() -> Weight;
fn burn_from() -> Weight;
}
@@ -129,19 +130,17 @@
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
- /// Storage: Fungible Allowance (r:1 w:1)
- /// Proof: Fungible Allowance (max_values: None, max_size: Some(93), added: 2568, mode: MaxEncodedLen)
- /// Storage: Fungible Balance (r:2 w:2)
- /// Proof: Fungible Balance (max_values: None, max_size: Some(77), added: 2552, mode: MaxEncodedLen)
- fn transfer_from() -> Weight {
- // Proof Size summary in bytes:
- // Measured: `300`
- // Estimated: `7672`
- // Minimum execution time: 21_667_000 picoseconds.
- Weight::from_parts(22_166_000, 7672)
- .saturating_add(T::DbWeight::get().reads(3_u64))
- .saturating_add(T::DbWeight::get().writes(3_u64))
+ // Storage: Fungible Allowance (r:1 w:0)
+ fn check_allowed_raw() -> Weight {
+ Weight::from_ref_time(3_550_000 as u64)
+ .saturating_add(T::DbWeight::get().reads(1 as u64))
}
+ // Storage: Fungible Allowance (r:1 w:1)
+ fn set_allowance_unchecked_raw() -> Weight {
+ Weight::from_ref_time(10_682_000 as u64)
+ .saturating_add(T::DbWeight::get().reads(1 as u64))
+ .saturating_add(T::DbWeight::get().writes(1 as u64))
+ }
/// Storage: Fungible Allowance (r:1 w:1)
/// Proof: Fungible Allowance (max_values: None, max_size: Some(93), added: 2568, mode: MaxEncodedLen)
/// Storage: Fungible TotalSupply (r:1 w:1)
@@ -243,18 +242,16 @@
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
- /// Storage: Fungible Allowance (r:1 w:1)
- /// Proof: Fungible Allowance (max_values: None, max_size: Some(93), added: 2568, mode: MaxEncodedLen)
- /// Storage: Fungible Balance (r:2 w:2)
- /// Proof: Fungible Balance (max_values: None, max_size: Some(77), added: 2552, mode: MaxEncodedLen)
- fn transfer_from() -> Weight {
- // Proof Size summary in bytes:
- // Measured: `300`
- // Estimated: `7672`
- // Minimum execution time: 21_667_000 picoseconds.
- Weight::from_parts(22_166_000, 7672)
- .saturating_add(RocksDbWeight::get().reads(3_u64))
- .saturating_add(RocksDbWeight::get().writes(3_u64))
+ // Storage: Fungible Allowance (r:1 w:0)
+ fn check_allowed_raw() -> Weight {
+ Weight::from_ref_time(3_550_000 as u64)
+ .saturating_add(RocksDbWeight::get().reads(1 as u64))
+ }
+ // Storage: Fungible Allowance (r:1 w:1)
+ fn set_allowance_unchecked_raw() -> Weight {
+ Weight::from_ref_time(10_682_000 as u64)
+ .saturating_add(RocksDbWeight::get().reads(1 as u64))
+ .saturating_add(RocksDbWeight::get().writes(1 as u64))
}
/// Storage: Fungible Allowance (r:1 w:1)
/// Proof: Fungible Allowance (max_values: None, max_size: Some(93), added: 2568, mode: MaxEncodedLen)