difftreelog
fix manage properties of nested tokens
in: master
7 files changed
pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -1311,12 +1311,14 @@
sender: T::CrossAccountId,
token_id: TokenId,
property: Vec<Property>,
+ budget: &dyn Budget,
) -> DispatchResultWithPostInfo;
fn delete_token_properties(
&self,
sender: T::CrossAccountId,
token_id: TokenId,
property_keys: Vec<PropertyKey>,
+ budget: &dyn Budget,
) -> DispatchResultWithPostInfo;
fn set_token_property_permissions(
&self,
pallets/fungible/src/common.rsdiffbeforeafterboth--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -298,6 +298,7 @@
_sender: T::CrossAccountId,
_token_id: TokenId,
_property: Vec<Property>,
+ _budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
fail!(<Error<T>>::SettingPropertiesNotAllowed)
}
@@ -315,6 +316,7 @@
_sender: T::CrossAccountId,
_token_id: TokenId,
_property_keys: Vec<PropertyKey>,
+ _budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
fail!(<Error<T>>::SettingPropertiesNotAllowed)
}
pallets/nonfungible/src/common.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -220,11 +220,12 @@
sender: T::CrossAccountId,
token_id: TokenId,
properties: Vec<Property>,
+ budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
let weight = <CommonWeights<T>>::set_token_properties(properties.len() as u32);
with_weight(
- <Pallet<T>>::set_token_properties(self, &sender, token_id, properties, false),
+ <Pallet<T>>::set_token_properties(self, &sender, token_id, properties, false, budget),
weight,
)
}
@@ -234,11 +235,12 @@
sender: T::CrossAccountId,
token_id: TokenId,
property_keys: Vec<PropertyKey>,
+ budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
let weight = <CommonWeights<T>>::delete_token_properties(property_keys.len() as u32);
with_weight(
- <Pallet<T>>::delete_token_properties(self, &sender, token_id, property_keys),
+ <Pallet<T>>::delete_token_properties(self, &sender, token_id, property_keys, budget),
weight,
)
}
pallets/nonfungible/src/erc.rsdiffbeforeafterboth38use crate::{38use crate::{39 AccountBalance, Config, CreateItemData, NonfungibleHandle, Pallet, TokenData, TokensMinted,39 AccountBalance, Config, CreateItemData, NonfungibleHandle, Pallet, TokenData, TokensMinted,40 SelfWeightOf, weights::WeightInfo, TokenProperties,40 SelfWeightOf, weights::WeightInfo, TokenProperties,41 property_guard::PropertyGuard,41};42};424343#[solidity_interface(name = "TokenProperties")]44#[solidity_interface(name = "TokenProperties")]82 .map_err(|_| "key too long")?;83 .map_err(|_| "key too long")?;83 let value = value.try_into().map_err(|_| "value too long")?;84 let value = value.try_into().map_err(|_| "value too long")?;8586 let is_token_create = false;87 let budget = self88 .recorder89 .weight_calls_budget(<StructureWeight<T>>::find_parent());9091 let mut guard = PropertyGuard::new(92 &caller,93 self,94 TokenId(token_id),95 is_token_create,96 &budget,97 );849885 <Pallet<T>>::set_token_property(99 <Pallet<T>>::set_token_property(Property { key, value }, &mut guard)86 self,87 &caller,88 TokenId(token_id),89 Property { key, value },90 false,91 )92 .map_err(dispatch_to_evm::<T>)100 .map_err(dispatch_to_evm::<T>)93 }101 }99 .try_into()107 .try_into()100 .map_err(|_| "key too long")?;108 .map_err(|_| "key too long")?;109110 let is_token_create = false;111 let budget = self112 .recorder113 .weight_calls_budget(<StructureWeight<T>>::find_parent());114115 let mut guard = PropertyGuard::new(116 &caller,117 self,118 TokenId(token_id),119 is_token_create,120 &budget,121 );101122102 <Pallet<T>>::delete_token_property(self, &caller, TokenId(token_id), key)123 <Pallet<T>>::delete_token_property(key, &mut guard)103 .map_err(dispatch_to_evm::<T>)124 .map_err(dispatch_to_evm::<T>)104 }125 }105126pallets/nonfungible/src/lib.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -52,6 +52,10 @@
pub mod erc;
pub mod weights;
+mod property_guard;
+
+use property_guard::*;
+
pub type CreateItemData<T> = CreateNftExData<<T as pallet_evm::account::Config>::CrossAccountId>;
pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;
@@ -484,30 +488,18 @@
})
}
- pub fn set_token_property(
- collection: &NonfungibleHandle<T>,
- sender: &T::CrossAccountId,
- token_id: TokenId,
- property: Property,
- is_token_create: bool,
- ) -> DispatchResult {
- Self::check_token_change_permission(
- collection,
- sender,
- token_id,
- &property.key,
- is_token_create,
- )?;
+ pub fn set_token_property(property: Property, guard: &mut PropertyGuard<'_, T>) -> DispatchResult {
+ Self::check_token_change_permission(&property.key, guard)?;
- <TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {
+ <TokenProperties<T>>::try_mutate((guard.collection.id, guard.token), |properties| {
let property = property.clone();
properties.try_set(property.key, property.value)
})
.map_err(<CommonError<T>>::from)?;
<PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertySet(
- collection.id,
- token_id,
+ guard.collection.id,
+ guard.token,
property.key,
));
@@ -521,57 +513,47 @@
token_id: TokenId,
properties: Vec<Property>,
is_token_create: bool,
+ nesting_budget: &dyn Budget,
) -> DispatchResult {
+ let mut guard = PropertyGuard::new(
+ sender,
+ collection,
+ token_id,
+ is_token_create,
+ nesting_budget,
+ );
+
for property in properties {
- Self::set_token_property(collection, sender, token_id, property, is_token_create)?;
+ Self::set_token_property(property, &mut guard)?;
}
Ok(())
}
- pub fn delete_token_property(
- collection: &NonfungibleHandle<T>,
- sender: &T::CrossAccountId,
- token_id: TokenId,
- property_key: PropertyKey,
- ) -> DispatchResult {
- Self::check_token_change_permission(collection, sender, token_id, &property_key, false)?;
+ pub fn delete_token_property(property_key: PropertyKey, guard: &mut PropertyGuard<'_, T>) -> DispatchResult {
+ Self::check_token_change_permission(&property_key, guard)?;
- <TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {
+ <TokenProperties<T>>::try_mutate((guard.collection.id, guard.token), |properties| {
properties.remove(&property_key)
})
.map_err(<CommonError<T>>::from)?;
<PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertyDeleted(
- collection.id,
- token_id,
+ guard.collection.id,
+ guard.token,
property_key,
));
Ok(())
}
- fn check_token_change_permission(
- collection: &NonfungibleHandle<T>,
- sender: &T::CrossAccountId,
- token_id: TokenId,
- property_key: &PropertyKey,
- is_token_create: bool,
- ) -> DispatchResult {
- let permission = <PalletCommon<T>>::property_permissions(collection.id)
+ fn check_token_change_permission(property_key: &PropertyKey, guard: &mut PropertyGuard<'_, T>) -> DispatchResult {
+ let permission = <PalletCommon<T>>::property_permissions(guard.collection.id)
.get(property_key)
.cloned()
.unwrap_or_else(PropertyPermission::none);
- let token_data = <TokenData<T>>::get((collection.id, token_id))
- .ok_or(<CommonError<T>>::TokenNotFound)?;
-
- let check_token_owner = || -> DispatchResult {
- ensure!(&token_data.owner == sender, <CommonError<T>>::NoPermission);
- Ok(())
- };
-
- let is_property_exists = TokenProperties::<T>::get((collection.id, token_id))
+ let is_property_exists = TokenProperties::<T>::get((guard.collection.id, guard.token))
.get(property_key)
.is_some();
@@ -586,18 +568,18 @@
..
} => {
//TODO: investigate threats during public minting.
- if is_token_create && (collection_admin || token_owner) {
+ if guard.is_token_create && (collection_admin || token_owner) {
return Ok(());
}
let mut check_result = Err(<CommonError<T>>::NoPermission.into());
if collection_admin {
- check_result = collection.check_is_owner_or_admin(sender);
+ check_result = guard.check_collection_admin();
}
if token_owner {
- check_result.or_else(|_| check_token_owner())
+ check_result.or_else(|_| guard.check_token_owner())
} else {
check_result
}
@@ -611,9 +593,20 @@
sender: &T::CrossAccountId,
token_id: TokenId,
property_keys: Vec<PropertyKey>,
+ nesting_budget: &dyn Budget,
) -> DispatchResult {
+ let is_token_create = false;
+
+ let mut guard = PropertyGuard::new(
+ sender,
+ collection,
+ token_id,
+ is_token_create,
+ nesting_budget,
+ );
+
for key in property_keys {
- Self::delete_token_property(collection, sender, token_id, key)?;
+ Self::delete_token_property(key, &mut guard)?;
}
Ok(())
@@ -829,6 +822,7 @@
TokenId(token),
data.properties.clone().into_inner(),
true,
+ nesting_budget,
) {
return TransactionOutcome::Rollback(Err(e));
}
pallets/refungible/src/common.rsdiffbeforeafterboth--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -314,6 +314,7 @@
_sender: T::CrossAccountId,
_token_id: TokenId,
_property: Vec<Property>,
+ _budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
fail!(<Error<T>>::SettingPropertiesNotAllowed)
}
@@ -331,6 +332,7 @@
_sender: T::CrossAccountId,
_token_id: TokenId,
_property_keys: Vec<PropertyKey>,
+ _budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
fail!(<Error<T>>::SettingPropertiesNotAllowed)
}
pallets/unique/src/lib.rsdiffbeforeafterboth--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -654,8 +654,9 @@
ensure!(!properties.is_empty(), Error::<T>::EmptyArgument);
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
+ let budget = budget::Value::new(NESTING_BUDGET);
- dispatch_tx::<T, _>(collection_id, |d| d.set_token_properties(sender, token_id, properties))
+ dispatch_tx::<T, _>(collection_id, |d| d.set_token_properties(sender, token_id, properties, &budget))
}
#[weight = T::CommonWeightInfo::delete_token_properties(property_keys.len() as u32)]
@@ -669,8 +670,9 @@
ensure!(!property_keys.is_empty(), Error::<T>::EmptyArgument);
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
+ let budget = budget::Value::new(NESTING_BUDGET);
- dispatch_tx::<T, _>(collection_id, |d| d.delete_token_properties(sender, token_id, property_keys))
+ dispatch_tx::<T, _>(collection_id, |d| d.delete_token_properties(sender, token_id, property_keys, &budget))
}
#[weight = T::CommonWeightInfo::set_token_property_permissions(property_permissions.len() as u32)]