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
22use up_data_structs::{22use up_data_structs::{
23 AccessMode, CollectionId, CustomDataLimit, TokenId, CreateCollectionData, CreateNftExData,23 AccessMode, CollectionId, CustomDataLimit, TokenId, CreateCollectionData, CreateNftExData,
24 mapping::TokenAddressMapping, NestingRule, budget::Budget, Property, PropertyPermission,24 mapping::TokenAddressMapping, NestingRule, budget::Budget, Property, PropertyPermission,
25 PropertyKey, PropertyKeyPermission, Properties, TrySet,25 PropertyKey, PropertyKeyPermission, Properties, TrySetProperty,
26};26};
27use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};27use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};
28use pallet_common::{28use pallet_common::{
modifiedprimitives/data-structs/src/lib.rsdiffbeforeafterboth
649 NoSpaceForProperty,649 NoSpaceForProperty,
650 PropertyLimitReached,650 PropertyLimitReached,
651 InvalidCharacterInPropertyKey,651 InvalidCharacterInPropertyKey,
652 PropertyKeyIsTooLong,
652 EmptyPropertyKey,653 EmptyPropertyKey,
653}654}
655
656#[derive(Clone, Copy)]
657pub enum PropertyScope {
658 None,
659 Rmrk,
660}
661
662impl PropertyScope {
663 fn apply(self, key: PropertyKey) -> Result<PropertyKey, PropertiesError> {
664 let scope_str: &[u8] = match self {
665 Self::None => return Ok(key),
666 Self::Rmrk => b"rmrk",
667 };
668
669 [scope_str, b":", key.as_slice()]
670 .concat()
671 .try_into()
672 .map_err(|_| PropertiesError::PropertyKeyIsTooLong)
673 }
674}
654675
655pub trait TrySet: Sized {676pub trait TrySetProperty: Sized {
656 type Value;677 type Value;
657678
658 fn try_set(&mut self, key: PropertyKey, value: Self::Value) -> Result<(), PropertiesError>;679 fn try_scoped_set(
680 &mut self,
681 scope: PropertyScope,
682 key: PropertyKey,
683 value: Self::Value
684 ) -> Result<(), PropertiesError>;
659685
660 fn try_set_from_iter<I>(&mut self, iter: I) -> Result<(), PropertiesError>686 fn try_scoped_set_from_iter<I>(
687 &mut self,
688 scope: PropertyScope,
689 iter: I
690 ) -> Result<(), PropertiesError>
661 where691 where
662 I: Iterator<Item = (PropertyKey, Self::Value)>,692 I: Iterator<Item=(PropertyKey, Self::Value)>
663 {693 {
664 for (key, value) in iter {694 for (key, value) in iter {
665 self.try_set(key, value)?;695 self.try_scoped_set(scope, key, value)?;
666 }696 }
667697
668 Ok(())698 Ok(())
669 }699 }
700
701 fn try_set(&mut self, key: PropertyKey, value: Self::Value) -> Result<(), PropertiesError> {
702 self.try_scoped_set(PropertyScope::None, key, value)
703 }
704
705 fn try_set_from_iter<I>(&mut self, iter: I) -> Result<(), PropertiesError>
706 where
707 I: Iterator<Item = (PropertyKey, Self::Value)>,
708 {
709 self.try_scoped_set_from_iter(PropertyScope::None, iter)
710 }
670}711}
671712
672#[derive(Encode, Decode, TypeInfo, Derivative, Clone, PartialEq, MaxEncodedLen)]713#[derive(Encode, Decode, TypeInfo, Derivative, Clone, PartialEq, MaxEncodedLen)]
713 }754 }
714}755}
715756
716impl<Value> TrySet for PropertiesMap<Value> {757impl<Value> TrySetProperty for PropertiesMap<Value> {
717 type Value = Value;758 type Value = Value;
718759
719 fn try_set(&mut self, key: PropertyKey, value: Self::Value) -> Result<(), PropertiesError> {760 fn try_scoped_set(
761 &mut self,
762 scope: PropertyScope,
763 key: PropertyKey,
764 value: Self::Value
765 ) -> Result<(), PropertiesError> {
720 Self::check_property_key(&key)?;766 Self::check_property_key(&key)?;
721767
768 let key = scope.apply(key)?;
722 self.0769 self.0
723 .try_insert(key, value)770 .try_insert(key, value)
724 .map_err(|_| PropertiesError::PropertyLimitReached)?;771 .map_err(|_| PropertiesError::PropertyLimitReached)?;
765 }812 }
766}813}
767814
768impl TrySet for Properties {815impl TrySetProperty for Properties {
769 type Value = PropertyValue;816 type Value = PropertyValue;
770817
771 fn try_set(&mut self, key: PropertyKey, value: Self::Value) -> Result<(), PropertiesError> {818 fn try_scoped_set(
819 &mut self,
820 scope: PropertyScope,
821 key: PropertyKey,
822 value: Self::Value
823 ) -> Result<(), PropertiesError> {
775 return Err(PropertiesError::NoSpaceForProperty);827 return Err(PropertiesError::NoSpaceForProperty);
776 }828 }
777829
778 self.map.try_set(key, value)?;830 self.map.try_scoped_set(scope, key, value)?;
779831
780 self.consumed_space += value_len as u32;832 self.consumed_space += value_len as u32;
781833