difftreelog
chore fix `token_owner` weight
in: master
3 files changed
pallets/nonfungible/src/common.rsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use core::marker::PhantomData;1819use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight};20use up_data_structs::{21 TokenId, CreateItemExData, CollectionId, budget::Budget, Property, PropertyKey,22 PropertyKeyPermission, PropertyValue,23};24use pallet_common::{25 CommonCollectionOperations, CommonWeightInfo, RefungibleExtensions, with_weight,26 weights::WeightInfo as _,27};28use sp_runtime::DispatchError;29use sp_std::{vec::Vec, vec};3031use crate::{32 AccountBalance, Allowance, Config, CreateItemData, Error, NonfungibleHandle, Owned, Pallet,33 SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,34};3536pub struct CommonWeights<T: Config>(PhantomData<T>);37impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {38 fn create_item() -> Weight {39 <SelfWeightOf<T>>::create_item()40 }4142 fn create_multiple_items_ex(data: &CreateItemExData<T::CrossAccountId>) -> Weight {43 match data {44 CreateItemExData::NFT(t) => {45 <SelfWeightOf<T>>::create_multiple_items_ex(t.len() as u32)46 + t.iter()47 .map(|t| {48 if t.properties.len() > 0 {49 Self::set_token_properties(t.properties.len() as u32)50 } else {51 052 }53 })54 .sum::<u64>()55 }56 _ => 0,57 }58 }5960 fn create_multiple_items(data: &[up_data_structs::CreateItemData]) -> Weight {61 <SelfWeightOf<T>>::create_multiple_items(data.len() as u32)62 + data63 .iter()64 .filter_map(|t| match t {65 up_data_structs::CreateItemData::NFT(n) if n.properties.len() > 0 => {66 Some(Self::set_token_properties(n.properties.len() as u32))67 }68 _ => None,69 })70 .sum::<u64>()71 }7273 fn burn_item() -> Weight {74 <SelfWeightOf<T>>::burn_item()75 }7677 fn set_collection_properties(amount: u32) -> Weight {78 <pallet_common::SelfWeightOf<T>>::set_collection_properties(amount)79 }8081 fn delete_collection_properties(amount: u32) -> Weight {82 <pallet_common::SelfWeightOf<T>>::delete_collection_properties(amount)83 }8485 fn set_token_properties(amount: u32) -> Weight {86 <SelfWeightOf<T>>::set_token_properties(amount)87 }8889 fn delete_token_properties(amount: u32) -> Weight {90 <SelfWeightOf<T>>::delete_token_properties(amount)91 }9293 fn set_token_property_permissions(amount: u32) -> Weight {94 <SelfWeightOf<T>>::set_token_property_permissions(amount)95 }9697 fn transfer() -> Weight {98 <SelfWeightOf<T>>::transfer()99 }100101 fn approve() -> Weight {102 <SelfWeightOf<T>>::approve()103 }104105 fn transfer_from() -> Weight {106 <SelfWeightOf<T>>::transfer_from()107 }108109 fn burn_from() -> Weight {110 <SelfWeightOf<T>>::burn_from()111 }112113 fn burn_recursively_self_raw() -> Weight {114 <SelfWeightOf<T>>::burn_recursively_self_raw()115 }116117 fn burn_recursively_breadth_raw(amount: u32) -> Weight {118 <SelfWeightOf<T>>::burn_recursively_breadth_plus_self_plus_self_per_each_raw(amount)119 .saturating_sub(Self::burn_recursively_self_raw().saturating_mul(amount as u64 + 1))120 }121122 fn token_owner() -> Weight {123 0 //<SelfWeightOf<T>>::token_owner()124 }125}126127fn map_create_data<T: Config>(128 data: up_data_structs::CreateItemData,129 to: &T::CrossAccountId,130) -> Result<CreateItemData<T>, DispatchError> {131 match data {132 up_data_structs::CreateItemData::NFT(data) => Ok(CreateItemData::<T> {133 properties: data.properties,134 owner: to.clone(),135 }),136 _ => fail!(<Error<T>>::NotNonfungibleDataUsedToMintFungibleCollectionToken),137 }138}139140/// Implementation of `CommonCollectionOperations` for `NonfungibleHandle`. It wraps Nonfungible Pallete141/// methods and adds weight info.142impl<T: Config> CommonCollectionOperations<T> for NonfungibleHandle<T> {143 fn create_item(144 &self,145 sender: T::CrossAccountId,146 to: T::CrossAccountId,147 data: up_data_structs::CreateItemData,148 nesting_budget: &dyn Budget,149 ) -> DispatchResultWithPostInfo {150 with_weight(151 <Pallet<T>>::create_item(152 self,153 &sender,154 map_create_data::<T>(data, &to)?,155 nesting_budget,156 ),157 <CommonWeights<T>>::create_item(),158 )159 }160161 fn create_multiple_items(162 &self,163 sender: T::CrossAccountId,164 to: T::CrossAccountId,165 data: Vec<up_data_structs::CreateItemData>,166 nesting_budget: &dyn Budget,167 ) -> DispatchResultWithPostInfo {168 let weight = <CommonWeights<T>>::create_multiple_items(&data);169 let data = data170 .into_iter()171 .map(|d| map_create_data::<T>(d, &to))172 .collect::<Result<Vec<_>, DispatchError>>()?;173174 with_weight(175 <Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),176 weight,177 )178 }179180 fn create_multiple_items_ex(181 &self,182 sender: <T>::CrossAccountId,183 data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,184 nesting_budget: &dyn Budget,185 ) -> DispatchResultWithPostInfo {186 let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);187 let data = match data {188 up_data_structs::CreateItemExData::NFT(nft) => nft,189 _ => fail!(Error::<T>::NotNonfungibleDataUsedToMintFungibleCollectionToken),190 };191192 with_weight(193 <Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),194 weight,195 )196 }197198 fn set_collection_properties(199 &self,200 sender: T::CrossAccountId,201 properties: Vec<Property>,202 ) -> DispatchResultWithPostInfo {203 let weight = <CommonWeights<T>>::set_collection_properties(properties.len() as u32);204205 with_weight(206 <Pallet<T>>::set_collection_properties(self, &sender, properties),207 weight,208 )209 }210211 fn delete_collection_properties(212 &self,213 sender: &T::CrossAccountId,214 property_keys: Vec<PropertyKey>,215 ) -> DispatchResultWithPostInfo {216 let weight = <CommonWeights<T>>::delete_collection_properties(property_keys.len() as u32);217218 with_weight(219 <Pallet<T>>::delete_collection_properties(self, sender, property_keys),220 weight,221 )222 }223224 fn set_token_properties(225 &self,226 sender: T::CrossAccountId,227 token_id: TokenId,228 properties: Vec<Property>,229 nesting_budget: &dyn Budget,230 ) -> DispatchResultWithPostInfo {231 let weight = <CommonWeights<T>>::set_token_properties(properties.len() as u32);232233 with_weight(234 <Pallet<T>>::set_token_properties(235 self,236 &sender,237 token_id,238 properties.into_iter(),239 false,240 nesting_budget,241 ),242 weight,243 )244 }245246 fn delete_token_properties(247 &self,248 sender: T::CrossAccountId,249 token_id: TokenId,250 property_keys: Vec<PropertyKey>,251 nesting_budget: &dyn Budget,252 ) -> DispatchResultWithPostInfo {253 let weight = <CommonWeights<T>>::delete_token_properties(property_keys.len() as u32);254255 with_weight(256 <Pallet<T>>::delete_token_properties(257 self,258 &sender,259 token_id,260 property_keys.into_iter(),261 nesting_budget,262 ),263 weight,264 )265 }266267 fn set_token_property_permissions(268 &self,269 sender: &T::CrossAccountId,270 property_permissions: Vec<PropertyKeyPermission>,271 ) -> DispatchResultWithPostInfo {272 let weight =273 <CommonWeights<T>>::set_token_property_permissions(property_permissions.len() as u32);274275 with_weight(276 <Pallet<T>>::set_token_property_permissions(self, sender, property_permissions),277 weight,278 )279 }280281 fn burn_item(282 &self,283 sender: T::CrossAccountId,284 token: TokenId,285 amount: u128,286 ) -> DispatchResultWithPostInfo {287 ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);288 if amount == 1 {289 with_weight(290 <Pallet<T>>::burn(self, &sender, token),291 <CommonWeights<T>>::burn_item(),292 )293 } else {294 Ok(().into())295 }296 }297298 fn burn_item_recursively(299 &self,300 sender: T::CrossAccountId,301 token: TokenId,302 self_budget: &dyn Budget,303 breadth_budget: &dyn Budget,304 ) -> DispatchResultWithPostInfo {305 <Pallet<T>>::burn_recursively(self, &sender, token, self_budget, breadth_budget)306 }307308 fn transfer(309 &self,310 from: T::CrossAccountId,311 to: T::CrossAccountId,312 token: TokenId,313 amount: u128,314 nesting_budget: &dyn Budget,315 ) -> DispatchResultWithPostInfo {316 ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);317 if amount == 1 {318 with_weight(319 <Pallet<T>>::transfer(self, &from, &to, token, nesting_budget),320 <CommonWeights<T>>::transfer(),321 )322 } else {323 Ok(().into())324 }325 }326327 fn approve(328 &self,329 sender: T::CrossAccountId,330 spender: T::CrossAccountId,331 token: TokenId,332 amount: u128,333 ) -> DispatchResultWithPostInfo {334 ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);335336 with_weight(337 if amount == 1 {338 <Pallet<T>>::set_allowance(self, &sender, token, Some(&spender))339 } else {340 <Pallet<T>>::set_allowance(self, &sender, token, None)341 },342 <CommonWeights<T>>::approve(),343 )344 }345346 fn transfer_from(347 &self,348 sender: T::CrossAccountId,349 from: T::CrossAccountId,350 to: T::CrossAccountId,351 token: TokenId,352 amount: u128,353 nesting_budget: &dyn Budget,354 ) -> DispatchResultWithPostInfo {355 ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);356357 if amount == 1 {358 with_weight(359 <Pallet<T>>::transfer_from(self, &sender, &from, &to, token, nesting_budget),360 <CommonWeights<T>>::transfer_from(),361 )362 } else {363 Ok(().into())364 }365 }366367 fn burn_from(368 &self,369 sender: T::CrossAccountId,370 from: T::CrossAccountId,371 token: TokenId,372 amount: u128,373 nesting_budget: &dyn Budget,374 ) -> DispatchResultWithPostInfo {375 ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);376377 if amount == 1 {378 with_weight(379 <Pallet<T>>::burn_from(self, &sender, &from, token, nesting_budget),380 <CommonWeights<T>>::burn_from(),381 )382 } else {383 Ok(().into())384 }385 }386387 fn check_nesting(388 &self,389 sender: T::CrossAccountId,390 from: (CollectionId, TokenId),391 under: TokenId,392 nesting_budget: &dyn Budget,393 ) -> sp_runtime::DispatchResult {394 <Pallet<T>>::check_nesting(self, sender, from, under, nesting_budget)395 }396397 fn nest(&self, under: TokenId, to_nest: (CollectionId, TokenId)) {398 <Pallet<T>>::nest((self.id, under), to_nest);399 }400401 fn unnest(&self, under: TokenId, to_unnest: (CollectionId, TokenId)) {402 <Pallet<T>>::unnest((self.id, under), to_unnest);403 }404405 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {406 <Owned<T>>::iter_prefix((self.id, account))407 .map(|(id, _)| id)408 .collect()409 }410411 fn collection_tokens(&self) -> Vec<TokenId> {412 <TokenData<T>>::iter_prefix((self.id,))413 .map(|(id, _)| id)414 .collect()415 }416417 fn token_exists(&self, token: TokenId) -> bool {418 <Pallet<T>>::token_exists(self, token)419 }420421 fn last_token_id(&self) -> TokenId {422 TokenId(<TokensMinted<T>>::get(self.id))423 }424425 fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId> {426 <TokenData<T>>::get((self.id, token)).map(|t| t.owner)427 }428429 /// Returns token owners.430 fn token_owners(&self, token: TokenId) -> Vec<T::CrossAccountId> {431 self.token_owner(token).map_or_else(|| vec![], |t| vec![t])432 }433434 fn token_property(&self, token_id: TokenId, key: &PropertyKey) -> Option<PropertyValue> {435 <Pallet<T>>::token_properties((self.id, token_id))436 .get(key)437 .cloned()438 }439440 fn token_properties(&self, token_id: TokenId, keys: Option<Vec<PropertyKey>>) -> Vec<Property> {441 let properties = <Pallet<T>>::token_properties((self.id, token_id));442443 keys.map(|keys| {444 keys.into_iter()445 .filter_map(|key| {446 properties.get(&key).map(|value| Property {447 key,448 value: value.clone(),449 })450 })451 .collect()452 })453 .unwrap_or_else(|| {454 properties455 .into_iter()456 .map(|(key, value)| Property { key, value })457 .collect()458 })459 }460461 fn total_supply(&self) -> u32 {462 <Pallet<T>>::total_supply(self)463 }464465 fn account_balance(&self, account: T::CrossAccountId) -> u32 {466 <AccountBalance<T>>::get((self.id, account))467 }468469 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {470 if <TokenData<T>>::get((self.id, token))471 .map(|a| a.owner == account)472 .unwrap_or(false)473 {474 1475 } else {476 0477 }478 }479480 fn allowance(481 &self,482 sender: T::CrossAccountId,483 spender: T::CrossAccountId,484 token: TokenId,485 ) -> u128 {486 if <TokenData<T>>::get((self.id, token))487 .map(|a| a.owner != sender)488 .unwrap_or(true)489 {490 0491 } else if <Allowance<T>>::get((self.id, token)) == Some(spender) {492 1493 } else {494 0495 }496 }497498 fn refungible_extensions(&self) -> Option<&dyn RefungibleExtensions<T>> {499 None500 }501502 fn total_pieces(&self, token: TokenId) -> Option<u128> {503 if <TokenData<T>>::contains_key((self.id, token)) {504 Some(1)505 } else {506 None507 }508 }509}pallets/refungible/src/benchmarking.rsdiffbeforeafterboth--- a/pallets/refungible/src/benchmarking.rs
+++ b/pallets/refungible/src/benchmarking.rs
@@ -36,9 +36,7 @@
fn create_max_item_data<CrossAccountId: Ord>(
users: impl IntoIterator<Item = (CrossAccountId, u128)>,
) -> CreateRefungibleExData<CrossAccountId> {
- let const_data = create_data::<CUSTOM_DATA_LIMIT>();
CreateRefungibleExData {
- const_data,
users: users
.into_iter()
.collect::<BTreeMap<_, _>>()
pallets/refungible/src/common.rsdiffbeforeafterboth--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -150,7 +150,7 @@
}
fn token_owner() -> Weight {
- 0 //<SelfWeightOf<T>>::token_owner()
+ <SelfWeightOf<T>>::token_owner()
}
}