From efe1d7b297998b0056ed04954b275b334055a118 Mon Sep 17 00:00:00 2001 From: sotmorskiy Date: Thu, 10 Dec 2020 04:31:42 +0000 Subject: [PATCH] NFTPAR-183: Storage Refactoring. Decimal points u32 -> u8. Also extended decimal points limit 4 -> 30. --- --- a/pallets/nft/src/lib.rs +++ b/pallets/nft/src/lib.rs @@ -45,21 +45,25 @@ mod default_weights; +pub const MAX_DECIMAL_POINTS: DecimalPoints = 30; + // Structs // #region pub type CollectionId = u32; pub type TokenId = u32; +pub type DecimalPoints = u8; + #[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)] #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] pub enum CollectionMode { Invalid, NFT, // decimal points - Fungible(u32), + Fungible(DecimalPoints), // decimal points - ReFungible(u32), + ReFungible(DecimalPoints), } impl Into for CollectionMode { @@ -104,7 +108,7 @@ pub owner: AccountId, pub mode: CollectionMode, pub access: AccessMode, - pub decimal_points: u32, + pub decimal_points: DecimalPoints, pub name: Vec, // 64 include null escape char pub description: Vec, // 256 include null escape char pub token_prefix: Vec, // 16 include null escape char @@ -269,7 +273,7 @@ pub enum Error for Module { /// Total collections bound exceeded. TotalCollectionsLimitExceeded, - /// Decimal_points parameter must be lower than 4. + /// Decimal_points parameter must be lower than MAX_DECIMAL_POINTS constant, currently it is 30. CollectionDecimalPointLimitExceeded, /// Collection name can not be longer than 63 char. CollectionNameLimitExceeded, @@ -498,7 +502,7 @@ ensure!(CollectionCount::get() < ChainLimit::get().collection_numbers_limit, Error::::TotalCollectionsLimitExceeded); // check params - ensure!(decimal_points <= 4, Error::::CollectionDecimalPointLimitExceeded); + ensure!(decimal_points <= MAX_DECIMAL_POINTS, Error::::CollectionDecimalPointLimitExceeded); let mut name = collection_name.to_vec(); name.push(0); @@ -1461,14 +1465,14 @@ let item = FungibleItemType { collection: collection_id, owner, - value: (10 as u128).pow(collection.decimal_points) + value: (10 as u128).pow(collection.decimal_points as u32) }; Self::add_fungible_item(item)?; }, CreateItemData::ReFungible(data) => { let mut owner_list = Vec::new(); - let value = (10 as u128).pow(collection.decimal_points); + let value = (10 as u128).pow(collection.decimal_points as u32); owner_list.push(Ownership {owner: owner.clone(), fraction: value}); let item = ReFungibleItemType { @@ -1982,8 +1986,8 @@ fn init_collection(item: &CollectionType) { // check params assert!( - item.decimal_points <= 4, - "decimal_points parameter must be lower than 4" + item.decimal_points <= MAX_DECIMAL_POINTS, + "decimal_points parameter must be lower than MAX_DECIMAL_POINTS" ); assert!( item.name.len() <= 64, --- a/pallets/nft/src/tests.rs +++ b/pallets/nft/src/tests.rs @@ -3,7 +3,7 @@ use crate::mock::*; use crate::{AccessMode, ApprovePermissions, CollectionMode, Ownership, ChainLimits, CreateItemData, CreateNftData, CreateFungibleData, CreateReFungibleData, - CollectionId, TokenId}; //Err + CollectionId, TokenId, MAX_DECIMAL_POINTS}; //Err use frame_support::{assert_noop, assert_ok}; use frame_system::{ RawOrigin }; @@ -78,6 +78,46 @@ // Use cases tests region // #region #[test] +fn create_fungible_collection_fails_with_large_decimal_numbers() { + new_test_ext().execute_with(|| { + default_limits(); + + let col_name1: Vec = "Test1\0".encode_utf16().collect::>(); + let col_desc1: Vec = "TestDescription1\0".encode_utf16().collect::>(); + let token_prefix1: Vec = b"token_prefix1\0".to_vec(); + + let origin1 = Origin::signed(1); + assert_noop!(TemplateModule::create_collection( + origin1, + col_name1, + col_desc1, + token_prefix1, + CollectionMode::Fungible(MAX_DECIMAL_POINTS + 1) + ), Error::::CollectionDecimalPointLimitExceeded); + }); +} + +#[test] +fn create_re_fungible_collection_fails_with_large_decimal_numbers() { + new_test_ext().execute_with(|| { + default_limits(); + + let col_name1: Vec = "Test1\0".encode_utf16().collect::>(); + let col_desc1: Vec = "TestDescription1\0".encode_utf16().collect::>(); + let token_prefix1: Vec = b"token_prefix1\0".to_vec(); + + let origin1 = Origin::signed(1); + assert_noop!(TemplateModule::create_collection( + origin1, + col_name1, + col_desc1, + token_prefix1, + CollectionMode::ReFungible(MAX_DECIMAL_POINTS + 1) + ), Error::::CollectionDecimalPointLimitExceeded); + }); +} + +#[test] fn create_nft_item() { new_test_ext().execute_with(|| { default_limits(); -- gitstuff