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.rsdiffbeforeafterboth298 _sender: T::CrossAccountId,298 _sender: T::CrossAccountId,299 _token_id: TokenId,299 _token_id: TokenId,300 _property: Vec<Property>,300 _property: Vec<Property>,301 _budget: &dyn Budget,301 ) -> DispatchResultWithPostInfo {302 ) -> DispatchResultWithPostInfo {302 fail!(<Error<T>>::SettingPropertiesNotAllowed)303 fail!(<Error<T>>::SettingPropertiesNotAllowed)303 }304 }315 _sender: T::CrossAccountId,316 _sender: T::CrossAccountId,316 _token_id: TokenId,317 _token_id: TokenId,317 _property_keys: Vec<PropertyKey>,318 _property_keys: Vec<PropertyKey>,319 _budget: &dyn Budget,318 ) -> DispatchResultWithPostInfo {320 ) -> DispatchResultWithPostInfo {319 fail!(<Error<T>>::SettingPropertiesNotAllowed)321 fail!(<Error<T>>::SettingPropertiesNotAllowed)320 }322 }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.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/erc.rs
+++ b/pallets/nonfungible/src/erc.rs
@@ -38,6 +38,7 @@
use crate::{
AccountBalance, Config, CreateItemData, NonfungibleHandle, Pallet, TokenData, TokensMinted,
SelfWeightOf, weights::WeightInfo, TokenProperties,
+ property_guard::PropertyGuard,
};
#[solidity_interface(name = "TokenProperties")]
@@ -82,14 +83,21 @@
.map_err(|_| "key too long")?;
let value = value.try_into().map_err(|_| "value too long")?;
- <Pallet<T>>::set_token_property(
+ let is_token_create = false;
+ let budget = self
+ .recorder
+ .weight_calls_budget(<StructureWeight<T>>::find_parent());
+
+ let mut guard = PropertyGuard::new(
+ &caller,
self,
- &caller,
TokenId(token_id),
- Property { key, value },
- false,
- )
- .map_err(dispatch_to_evm::<T>)
+ is_token_create,
+ &budget,
+ );
+
+ <Pallet<T>>::set_token_property(Property { key, value }, &mut guard)
+ .map_err(dispatch_to_evm::<T>)
}
fn delete_property(&mut self, token_id: uint256, caller: caller, key: string) -> Result<()> {
@@ -99,7 +107,20 @@
.try_into()
.map_err(|_| "key too long")?;
- <Pallet<T>>::delete_token_property(self, &caller, TokenId(token_id), key)
+ let is_token_create = false;
+ let budget = self
+ .recorder
+ .weight_calls_budget(<StructureWeight<T>>::find_parent());
+
+ let mut guard = PropertyGuard::new(
+ &caller,
+ self,
+ TokenId(token_id),
+ is_token_create,
+ &budget,
+ );
+
+ <Pallet<T>>::delete_token_property(key, &mut guard)
.map_err(dispatch_to_evm::<T>)
}
pallets/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)]