difftreelog
Add properties key chars check
in: master
4 files changed
pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -18,7 +18,7 @@
use core::ops::{Deref, DerefMut};
use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};
-use sp_std::{vec::Vec, collections::btree_map::BTreeMap};
+use sp_std::vec::Vec;
use pallet_evm::account::CrossAccountId;
use frame_support::{
dispatch::{DispatchErrorWithPostInfo, DispatchResultWithPostInfo, Weight, PostDispatchInfo},
@@ -36,7 +36,7 @@
CUSTOM_DATA_LIMIT, CollectionLimits, CustomDataLimit, CreateCollectionData, SponsorshipState,
CreateItemExData, SponsoringRateLimit, budget::Budget, COLLECTION_FIELD_LIMIT, CollectionField,
PhantomType, Property, Properties, PropertiesPermissionMap, PropertyKey, PropertyPermission,
- PropertiesError, PropertyKeyPermission, TokenData, CollectionPropertiesPermissionsVec,
+ PropertiesError, PropertyKeyPermission, TokenData, TrySet,
};
pub use pallet::*;
use sp_core::H160;
@@ -374,6 +374,9 @@
/// Unable to read array of unbounded keys
UnableToReadUnboundedKeys,
+
+ /// Only ASCII letters, digits, and '_', '-' are allowed
+ InvalidCharacterInPropertyKey,
}
#[pallet::storage]
@@ -676,19 +679,20 @@
meta_update_permission: data.meta_update_permission.unwrap_or_default(),
};
- CollectionProperties::<T>::insert(
- id,
- Properties::from_collection_props_vec(data.properties)
- .map_err(|e| -> Error<T> { e.into() })?,
- );
+ let mut collection_properties = up_data_structs::CollectionProperties::get();
+ collection_properties.try_set_from_iter(
+ data.properties.into_iter()
+ .map(|p| (p.key, p.value))
+ ).map_err(|e| -> Error<T> { e.into() })?;
+
+ CollectionProperties::<T>::insert(id, collection_properties);
- let token_props_permissions: PropertiesPermissionMap = data
- .token_property_permissions
+ let mut token_props_permissions = PropertiesPermissionMap::new();
+ token_props_permissions.try_set_from_iter(
+ data.token_property_permissions
.into_iter()
.map(|property| (property.key, property.permission))
- .collect::<BTreeMap<_, _>>()
- .try_into()
- .map_err(|_| -> Error<T> { PropertiesError::PropertyLimitReached.into() })?;
+ ).map_err(|e| -> Error<T> { e.into() })?;
CollectionPropertyPermissions::<T>::insert(id, token_props_permissions);
@@ -771,7 +775,8 @@
collection.check_is_owner_or_admin(sender)?;
CollectionProperties::<T>::try_mutate(collection.id, |properties| {
- properties.try_set_property(property.clone())
+ let property = property.clone();
+ properties.try_set(property.key, property.value)
})
.map_err(|e| -> Error<T> { e.into() })?;
@@ -799,9 +804,9 @@
) -> DispatchResult {
collection.check_is_owner_or_admin(sender)?;
- CollectionProperties::<T>::mutate(collection.id, |properties| {
- properties.remove_property(&property_key);
- });
+ CollectionProperties::<T>::try_mutate(collection.id, |properties| {
+ properties.remove(&property_key)
+ }).map_err(|e| -> Error<T> { e.into() })?;
Self::deposit_event(Event::CollectionPropertyDeleted(
collection.id,
@@ -841,7 +846,7 @@
CollectionPropertyPermissions::<T>::try_mutate(collection.id, |permissions| {
let property_permission = property_permission.clone();
- permissions.try_insert(property_permission.key, property_permission.permission)
+ permissions.try_set(property_permission.key, property_permission.permission)
})
.map_err(|_| -> Error<T> { PropertiesError::PropertyLimitReached.into() })?;
@@ -876,6 +881,19 @@
.collect::<Result<Vec<PropertyKey>, DispatchError>>()
}
+ pub fn check_property_key(key: &PropertyKey) -> Result<(), DispatchError> {
+ let key_str = sp_std::str::from_utf8(key.as_slice())
+ .map_err(|_| <Error<T>>::InvalidCharacterInPropertyKey)?;
+
+ for ch in key_str.chars() {
+ if !ch.is_ascii_alphanumeric() && ch != '_' && ch != '-' {
+ return Err(<Error<T>>::InvalidCharacterInPropertyKey.into());
+ }
+ }
+
+ Ok(())
+ }
+
pub fn filter_collection_properties(
collection_id: CollectionId,
keys: Vec<PropertyKey>,
@@ -885,10 +903,11 @@
let properties = keys
.into_iter()
.filter_map(|key| {
- properties.get_property(&key).map(|value| Property {
- key,
- value: value.clone(),
- })
+ properties.get(&key)
+ .map(|value| Property {
+ key,
+ value: value.clone(),
+ })
})
.collect();
@@ -1222,6 +1241,7 @@
match error {
PropertiesError::NoSpaceForProperty => Self::NoSpaceForProperty,
PropertiesError::PropertyLimitReached => Self::PropertyLimitReached,
+ PropertiesError::InvalidCharacterInPropertyKey => Self::InvalidCharacterInPropertyKey,
}
}
}
pallets/nonfungible/src/common.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -391,10 +391,11 @@
keys.into_iter()
.filter_map(|key| {
- properties.get_property(&key).map(|value| Property {
- key,
- value: value.clone(),
- })
+ properties.get(&key)
+ .map(|value| Property {
+ key,
+ value: value.clone(),
+ })
})
.collect()
}
pallets/nonfungible/src/lib.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -21,7 +21,7 @@
use up_data_structs::{
AccessMode, CollectionId, CustomDataLimit, TokenId, CreateCollectionData, CreateNftExData,
mapping::TokenAddressMapping, NestingRule, budget::Budget, Property, PropertyPermission,
- PropertyKey, PropertyKeyPermission, Properties,
+ PropertyKey, PropertyKeyPermission, Properties, TrySet,
};
use pallet_evm::account::CrossAccountId;
use pallet_common::{
@@ -265,7 +265,8 @@
Self::check_token_change_permission(collection, sender, token_id, &property.key)?;
<TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {
- properties.try_set_property(property.clone())
+ let property = property.clone();
+ properties.try_set(property.key, property.value)
})
.map_err(|e| -> CommonError<T> { e.into() })?;
@@ -299,9 +300,9 @@
) -> DispatchResult {
Self::check_token_change_permission(collection, sender, token_id, &property_key)?;
- <TokenProperties<T>>::mutate((collection.id, token_id), |properties| {
- properties.remove_property(&property_key);
- });
+ <TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {
+ properties.remove(&property_key)
+ }).map_err(|e| -> CommonError<T> { e.into() })?;
<PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertyDeleted(
collection.id,
@@ -332,7 +333,7 @@
};
let is_property_exists = TokenProperties::<T>::get((collection.id, token_id))
- .get_property(property_key)
+ .get(property_key)
.is_some();
match permission {
primitives/data-structs/src/lib.rsdiffbeforeafterboth689pub enum PropertiesError {689pub enum PropertiesError {690 NoSpaceForProperty,690 NoSpaceForProperty,691 PropertyLimitReached,691 PropertyLimitReached,692 InvalidCharacterInPropertyKey,692}693}693694694pub type PropertiesMap =695pub trait TrySet: Sized {695 BoundedBTreeMap<PropertyKey, PropertyValue, ConstU32<MAX_PROPERTIES_PER_ITEM>>;696pub type PropertiesPermissionMap =696 type Value;697 BoundedBTreeMap<PropertyKey, PropertyPermission, ConstU32<MAX_PROPERTIES_PER_ITEM>>;697698699#[derive(Encode, Decode, TypeInfo, Clone, PartialEq, MaxEncodedLen)]700pub struct Properties {701 map: PropertiesMap,698 fn try_set(&mut self, key: PropertyKey, value: Self::Value) -> Result<(), PropertiesError>;702 consumed_space: u32,703 space_limit: u32,704}705706impl Properties {707 pub fn new(space_limit: u32) -> Self {708 Self {709 map: BoundedBTreeMap::new(),710 consumed_space: 0,711 space_limit,712 }713 }714699715 pub fn from_collection_props_vec(700 fn try_set_from_iter<I>(&mut self, iter: I) -> Result<(), PropertiesError>716 data: CollectionPropertiesVec,701 where717 ) -> Result<Self, PropertiesError> {718 let mut props = Self::new(MAX_COLLECTION_PROPERTIES_SIZE);702 I: Iterator<Item=(PropertyKey, Self::Value)>719703 {720 for property in data.into_iter() {704 for (key, value) in iter {721 props.try_set_property(property)?;705 self.try_set(key, value)?;722 }706 }723707724 Ok(props)708 Ok(())725 }709 }710}711712#[derive(Encode, Decode, TypeInfo, Derivative, Clone, PartialEq, MaxEncodedLen)]713#[derivative(Default(bound = ""))]714pub struct PropertiesMap<Value>(BoundedBTreeMap<PropertyKey, Value, ConstU32<MAX_PROPERTIES_PER_ITEM>>);715716impl<Value> PropertiesMap<Value> {717 pub fn new() -> Self {718 Self(BoundedBTreeMap::new())719 }720721 pub fn remove(&mut self, key: &PropertyKey) -> Result<Option<Value>, PropertiesError> {722 Self::check_property_key(key)?;723724 Ok(self.0.remove(key))725 }726727 pub fn get(&self, key: &PropertyKey) -> Option<&Value> {728 self.0.get(key)729 }730731 pub fn iter(&self) -> impl Iterator<Item = (&PropertyKey, &Value)> {732 self.0.iter()733 }734735 fn check_property_key(key: &PropertyKey) -> Result<(), PropertiesError> {736 let key_str = sp_std::str::from_utf8(key.as_slice())737 .map_err(|_| PropertiesError::InvalidCharacterInPropertyKey)?;738739 for ch in key_str.chars() {740 if !ch.is_ascii_alphanumeric() && ch != '_' && ch != '-' {741 return Err(PropertiesError::InvalidCharacterInPropertyKey);742 }743 }744745 Ok(())746 }747}748749impl<Value> TrySet for PropertiesMap<Value> {750 type Value = Value;726751727 pub fn try_set_property(&mut self, property: Property) -> Result<(), PropertiesError> {752 fn try_set(&mut self, key: PropertyKey, value: Self::Value) -> Result<(), PropertiesError> {728 let value_len = property.value.len();729730 if self.consumed_space as usize + value_len > self.space_limit as usize {731 return Err(PropertiesError::NoSpaceForProperty);753 Self::check_property_key(&key)?;732 }733754734 self.map755 self.0735 .try_insert(property.key, property.value)756 .try_insert(key, value)736 .map_err(|_| PropertiesError::PropertyLimitReached)?;757 .map_err(|_| PropertiesError::PropertyLimitReached)?;737738 self.consumed_space += value_len as u32;739758740 Ok(())759 Ok(())741 }760 }761}762763pub type PropertiesPermissionMap = PropertiesMap<PropertyPermission>;764765#[derive(Encode, Decode, TypeInfo, Clone, PartialEq, MaxEncodedLen)]766pub struct Properties {767 map: PropertiesMap<PropertyValue>,768 consumed_space: u32,769 space_limit: u32,770}771772impl Properties {773 pub fn new(space_limit: u32) -> Self {774 Self {775 map: PropertiesMap::new(),776 consumed_space: 0,777 space_limit,778 }779 }742780743 pub fn remove_property(&mut self, key: &PropertyKey) {781 pub fn remove(&mut self, key: &PropertyKey) -> Result<Option<PropertyValue>, PropertiesError> {744 let property = self.map.get(key);782 let value = self.map.remove(key)?;745783746 if let Some(value) = property {784 if let Some(ref value) = value {747 let value_len = value.len() as u32;785 let value_len = value.len() as u32;748749 self.map.remove(key);750 self.consumed_space -= value_len;786 self.consumed_space -= value_len;751 }787 }788789 Ok(value)752 }790 }753791754 pub fn get_property(&self, key: &PropertyKey) -> Option<&PropertyValue> {792 pub fn get(&self, key: &PropertyKey) -> Option<&PropertyValue> {755 self.map.get(key)793 self.map.get(key)756 }794 }757795758 pub fn iter(&self) -> impl Iterator<Item = (&PropertyKey, &PropertyValue)> {796 pub fn iter(&self) -> impl Iterator<Item = (&PropertyKey, &PropertyValue)> {759 self.map.iter()797 self.map.iter()760 }798 }761}799}800801impl TrySet for Properties {802 type Value = PropertyValue;803804 fn try_set(&mut self, key: PropertyKey, value: Self::Value) -> Result<(), PropertiesError> {805 let value_len = value.len();806807 if self.consumed_space as usize + value_len > self.space_limit as usize {808 return Err(PropertiesError::NoSpaceForProperty);809 }810811 self.map.try_set(key, value)?;812813 self.consumed_space += value_len as u32;814815 Ok(())816 }817}762818763pub struct CollectionProperties;819pub struct CollectionProperties;764820