git.delta.rocks / unique-network / refs/commits / 039108239552

difftreelog

feat add properties scopes

Daniel Shiposha2022-05-18parent: #9d232ae.patch.diff
in: master

3 files changed

modifiedpallets/common/src/lib.rsdiffbeforeafterboth
37 CUSTOM_DATA_LIMIT, CollectionLimits, CreateCollectionData, SponsorshipState, CreateItemExData,37 CUSTOM_DATA_LIMIT, CollectionLimits, CreateCollectionData, SponsorshipState, CreateItemExData,
38 SponsoringRateLimit, budget::Budget, COLLECTION_FIELD_LIMIT, CollectionField, PhantomType,38 SponsoringRateLimit, budget::Budget, COLLECTION_FIELD_LIMIT, CollectionField, PhantomType,
39 Property, Properties, PropertiesPermissionMap, PropertyKey, PropertyPermission,39 Property, Properties, PropertiesPermissionMap, PropertyKey, PropertyPermission,
40 PropertiesError, PropertyKeyPermission, TokenData, TrySet,40 PropertiesError, PropertyKeyPermission, TokenData, TrySetProperty,
41};41};
42pub use pallet::*;42pub use pallet::*;
43use sp_core::H160;43use sp_core::H160;
346 /// Tried to store more property keys than allowed346 /// Tried to store more property keys than allowed
347 PropertyLimitReached,347 PropertyLimitReached,
348348
349 /// Unable to read array of unbounded keys349 /// Property key is too long
350 UnableToReadUnboundedKeys,350 PropertyKeyIsTooLong,
351351
352 /// Only ASCII letters, digits, and '_', '-' are allowed352 /// Only ASCII letters, digits, and '_', '-' are allowed
353 InvalidCharacterInPropertyKey,353 InvalidCharacterInPropertyKey,
838 keys.into_iter()838 keys.into_iter()
839 .map(|key| -> Result<PropertyKey, DispatchError> {839 .map(|key| -> Result<PropertyKey, DispatchError> {
840 key.try_into()840 key.try_into()
841 .map_err(|_| <Error<T>>::UnableToReadUnboundedKeys.into())841 .map_err(|_| <Error<T>>::PropertyKeyIsTooLong.into())
842 })842 })
843 .collect::<Result<Vec<PropertyKey>, DispatchError>>()843 .collect::<Result<Vec<PropertyKey>, DispatchError>>()
844 }844 }
1181 PropertiesError::NoSpaceForProperty => Self::NoSpaceForProperty,1181 PropertiesError::NoSpaceForProperty => Self::NoSpaceForProperty,
1182 PropertiesError::PropertyLimitReached => Self::PropertyLimitReached,1182 PropertiesError::PropertyLimitReached => Self::PropertyLimitReached,
1183 PropertiesError::InvalidCharacterInPropertyKey => Self::InvalidCharacterInPropertyKey,1183 PropertiesError::InvalidCharacterInPropertyKey => Self::InvalidCharacterInPropertyKey,
1184 PropertiesError::PropertyKeyIsTooLong => Self::PropertyKeyIsTooLong,
1184 PropertiesError::EmptyPropertyKey => Self::EmptyPropertyKey,1185 PropertiesError::EmptyPropertyKey => Self::EmptyPropertyKey,
1185 }1186 }
1186 }1187 }
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -22,7 +22,7 @@
 use up_data_structs::{
 	AccessMode, CollectionId, CustomDataLimit, TokenId, CreateCollectionData, CreateNftExData,
 	mapping::TokenAddressMapping, NestingRule, budget::Budget, Property, PropertyPermission,
-	PropertyKey, PropertyKeyPermission, Properties, TrySet,
+	PropertyKey, PropertyKeyPermission, Properties, TrySetProperty,
 };
 use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};
 use pallet_common::{
modifiedprimitives/data-structs/src/lib.rsdiffbeforeafterboth
--- a/primitives/data-structs/src/lib.rs
+++ b/primitives/data-structs/src/lib.rs
@@ -649,24 +649,65 @@
 	NoSpaceForProperty,
 	PropertyLimitReached,
 	InvalidCharacterInPropertyKey,
+	PropertyKeyIsTooLong,
 	EmptyPropertyKey,
 }
 
-pub trait TrySet: Sized {
+#[derive(Clone, Copy)]
+pub enum PropertyScope {
+	None,
+	Rmrk,
+}
+
+impl PropertyScope {
+	fn apply(self, key: PropertyKey) -> Result<PropertyKey, PropertiesError> {
+		let scope_str: &[u8] = match self {
+			Self::None => return Ok(key),
+			Self::Rmrk => b"rmrk",
+		};
+
+		[scope_str, b":", key.as_slice()]
+			.concat()
+			.try_into()
+			.map_err(|_| PropertiesError::PropertyKeyIsTooLong)
+	}
+}
+
+pub trait TrySetProperty: Sized {
 	type Value;
 
-	fn try_set(&mut self, key: PropertyKey, value: Self::Value) -> Result<(), PropertiesError>;
+	fn try_scoped_set(
+		&mut self,
+		scope: PropertyScope,
+		key: PropertyKey,
+		value: Self::Value
+	) -> Result<(), PropertiesError>;
 
-	fn try_set_from_iter<I>(&mut self, iter: I) -> Result<(), PropertiesError>
+	fn try_scoped_set_from_iter<I>(
+		&mut self,
+		scope: PropertyScope,
+		iter: I
+	) -> Result<(), PropertiesError>
 	where
-		I: Iterator<Item = (PropertyKey, Self::Value)>,
+		I: Iterator<Item=(PropertyKey, Self::Value)>
 	{
 		for (key, value) in iter {
-			self.try_set(key, value)?;
+			self.try_scoped_set(scope, key, value)?;
 		}
 
 		Ok(())
 	}
+
+	fn try_set(&mut self, key: PropertyKey, value: Self::Value) -> Result<(), PropertiesError> {
+		self.try_scoped_set(PropertyScope::None, key, value)
+	}
+
+	fn try_set_from_iter<I>(&mut self, iter: I) -> Result<(), PropertiesError>
+	where
+		I: Iterator<Item = (PropertyKey, Self::Value)>,
+	{
+		self.try_scoped_set_from_iter(PropertyScope::None, iter)
+	}
 }
 
 #[derive(Encode, Decode, TypeInfo, Derivative, Clone, PartialEq, MaxEncodedLen)]
@@ -713,12 +754,18 @@
 	}
 }
 
-impl<Value> TrySet for PropertiesMap<Value> {
+impl<Value> TrySetProperty for PropertiesMap<Value> {
 	type Value = Value;
 
-	fn try_set(&mut self, key: PropertyKey, value: Self::Value) -> Result<(), PropertiesError> {
+	fn try_scoped_set(
+		&mut self,
+		scope: PropertyScope,
+		key: PropertyKey,
+		value: Self::Value
+	) -> Result<(), PropertiesError> {
 		Self::check_property_key(&key)?;
 
+		let key = scope.apply(key)?;
 		self.0
 			.try_insert(key, value)
 			.map_err(|_| PropertiesError::PropertyLimitReached)?;
@@ -765,17 +812,22 @@
 	}
 }
 
-impl TrySet for Properties {
+impl TrySetProperty for Properties {
 	type Value = PropertyValue;
 
-	fn try_set(&mut self, key: PropertyKey, value: Self::Value) -> Result<(), PropertiesError> {
+	fn try_scoped_set(
+		&mut self,
+		scope: PropertyScope,
+		key: PropertyKey,
+		value: Self::Value
+	) -> Result<(), PropertiesError> {
 		let value_len = value.len();
 
 		if self.consumed_space as usize + value_len > self.space_limit as usize {
 			return Err(PropertiesError::NoSpaceForProperty);
 		}
 
-		self.map.try_set(key, value)?;
+		self.map.try_scoped_set(scope, key, value)?;
 
 		self.consumed_space += value_len as u32;