From 03910823955206fb61d34b207ff60fca1aaac8b3 Mon Sep 17 00:00:00 2001 From: Daniel Shiposha Date: Wed, 18 May 2022 11:13:42 +0000 Subject: [PATCH] feat: add properties scopes --- --- a/pallets/common/src/lib.rs +++ b/pallets/common/src/lib.rs @@ -37,7 +37,7 @@ CUSTOM_DATA_LIMIT, CollectionLimits, CreateCollectionData, SponsorshipState, CreateItemExData, SponsoringRateLimit, budget::Budget, COLLECTION_FIELD_LIMIT, CollectionField, PhantomType, Property, Properties, PropertiesPermissionMap, PropertyKey, PropertyPermission, - PropertiesError, PropertyKeyPermission, TokenData, TrySet, + PropertiesError, PropertyKeyPermission, TokenData, TrySetProperty, }; pub use pallet::*; use sp_core::H160; @@ -346,8 +346,8 @@ /// Tried to store more property keys than allowed PropertyLimitReached, - /// Unable to read array of unbounded keys - UnableToReadUnboundedKeys, + /// Property key is too long + PropertyKeyIsTooLong, /// Only ASCII letters, digits, and '_', '-' are allowed InvalidCharacterInPropertyKey, @@ -838,7 +838,7 @@ keys.into_iter() .map(|key| -> Result { key.try_into() - .map_err(|_| >::UnableToReadUnboundedKeys.into()) + .map_err(|_| >::PropertyKeyIsTooLong.into()) }) .collect::, DispatchError>>() } @@ -1181,6 +1181,7 @@ PropertiesError::NoSpaceForProperty => Self::NoSpaceForProperty, PropertiesError::PropertyLimitReached => Self::PropertyLimitReached, PropertiesError::InvalidCharacterInPropertyKey => Self::InvalidCharacterInPropertyKey, + PropertiesError::PropertyKeyIsTooLong => Self::PropertyKeyIsTooLong, PropertiesError::EmptyPropertyKey => Self::EmptyPropertyKey, } } --- 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::{ --- 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 { + 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(&mut self, iter: I) -> Result<(), PropertiesError> + fn try_scoped_set_from_iter( + &mut self, + scope: PropertyScope, + iter: I + ) -> Result<(), PropertiesError> where - I: Iterator, + I: Iterator { 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(&mut self, iter: I) -> Result<(), PropertiesError> + where + I: Iterator, + { + self.try_scoped_set_from_iter(PropertyScope::None, iter) + } } #[derive(Encode, Decode, TypeInfo, Derivative, Clone, PartialEq, MaxEncodedLen)] @@ -713,12 +754,18 @@ } } -impl TrySet for PropertiesMap { +impl TrySetProperty for PropertiesMap { 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; -- gitstuff