difftreelog
feat split large fields out of Collection
in: master
7 files changed
client/rpc/src/lib.rsdiffbeforeafterboth--- a/client/rpc/src/lib.rs
+++ b/client/rpc/src/lib.rs
@@ -19,7 +19,7 @@
use codec::Decode;
use jsonrpc_core::{Error as RpcError, ErrorCode, Result};
use jsonrpc_derive::rpc;
-use up_data_structs::{Collection, CollectionId, CollectionStats, CollectionLimits, TokenId};
+use up_data_structs::{RpcCollection, Collection, CollectionId, CollectionStats, CollectionLimits, TokenId};
use sp_api::{BlockId, BlockT, ProvideRuntimeApi, ApiExt};
use sp_blockchain::HeaderBackend;
use up_rpc::UniqueApi as UniqueRuntimeApi;
@@ -116,7 +116,7 @@
&self,
collection: CollectionId,
at: Option<BlockHash>,
- ) -> Result<Option<Collection<AccountId>>>;
+ ) -> Result<Option<RpcCollection<AccountId>>>;
#[rpc(name = "unique_collectionStats")]
fn collection_stats(&self, at: Option<BlockHash>) -> Result<CollectionStats>;
@@ -235,7 +235,7 @@
pass_method!(allowlist(collection: CollectionId) -> Vec<CrossAccountId>);
pass_method!(allowed(collection: CollectionId, user: CrossAccountId) -> bool);
pass_method!(last_token_id(collection: CollectionId) -> TokenId);
- pass_method!(collection_by_id(collection: CollectionId) -> Option<Collection<AccountId>>);
+ pass_method!(collection_by_id(collection: CollectionId) -> Option<RpcCollection<AccountId>>);
pass_method!(collection_stats() -> CollectionStats);
pass_method!(next_sponsored(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Option<u64>);
pass_method!(effective_collection_limits(collection_id: CollectionId) -> Option<CollectionLimits>);
pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -29,11 +29,11 @@
};
use pallet_evm::GasWeightMapping;
use up_data_structs::{
- COLLECTION_NUMBER_LIMIT, Collection, CollectionId, CreateItemData, MAX_TOKEN_PREFIX_LENGTH,
+ COLLECTION_NUMBER_LIMIT, Collection, RpcCollection, CollectionId, CreateItemData, MAX_TOKEN_PREFIX_LENGTH,
COLLECTION_ADMINS_LIMIT, MetaUpdatePermission, TokenId, CollectionStats, MAX_TOKEN_OWNERSHIP,
CollectionMode, NFT_SPONSOR_TRANSFER_TIMEOUT, FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,
REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT, MAX_SPONSOR_TIMEOUT, CUSTOM_DATA_LIMIT, CollectionLimits,
- CustomDataLimit, CreateCollectionData, SponsorshipState, CreateItemExData, SponsoringRateLimit, budget::Budget,
+ CustomDataLimit, CreateCollectionData, SponsorshipState, CreateItemExData, SponsoringRateLimit, budget::Budget, COLLECTION_FIELD_LIMIT, CollectionField,
};
pub use pallet::*;
use sp_core::H160;
@@ -352,6 +352,9 @@
OnlyOwnerAllowedToNest,
/// Only tokens from specific collections may nest tokens under this
SourceCollectionIsNotAllowedToNest,
+
+ /// Tried to store more data than allowed in collection field
+ CollectionFieldSizeExceeded,
}
#[pallet::storage]
@@ -369,6 +372,17 @@
QueryKind = OptionQuery,
>;
+ /// Large variable-size collection fields are extracted here
+ #[pallet::storage]
+ pub type CollectionData<T> = StorageNMap<
+ Key = (
+ Key<Twox64Concat, CollectionId>,
+ Key<Twox64Concat, CollectionField>,
+ ),
+ Value = BoundedVec<u8, ConstU32<COLLECTION_FIELD_LIMIT>>,
+ QueryKind = ValueQuery,
+ >;
+
#[pallet::storage]
pub type AdminAmount<T> = StorageMap<
Hasher = Blake2_128Concat,
@@ -409,7 +423,26 @@
fn on_runtime_upgrade() -> Weight {
if StorageVersion::get::<Pallet<T>>() < StorageVersion::new(1) {
use up_data_structs::{CollectionVersion1, CollectionVersion2};
- <CollectionById<T>>::translate_values::<CollectionVersion1<T::AccountId>, _>(|v| {
+ <CollectionById<T>>::translate::<CollectionVersion1<T::AccountId>, _>(|id, v| {
+ Self::set_field_raw(
+ id,
+ CollectionField::OffchainSchema,
+ v.offchain_schema.clone().into_inner(),
+ )
+ .expect("data has lower bounds than field");
+ Self::set_field_raw(
+ id,
+ CollectionField::VariableOnChainSchema,
+ v.variable_on_chain_schema.clone().into_inner(),
+ )
+ .expect("data has lower bounds than field");
+ Self::set_field_raw(
+ id,
+ CollectionField::ConstOnChainSchema,
+ v.const_on_chain_schema.clone().into_inner(),
+ )
+ .expect("data has lower bounds than field");
+
Some(CollectionVersion2::from(v))
});
}
@@ -483,6 +516,50 @@
Some(effective_limits)
}
+
+ pub fn rpc_collection(collection: CollectionId) -> Option<RpcCollection<T::AccountId>> {
+ let Collection {
+ name,
+ description,
+ owner,
+ mode,
+ access,
+ token_prefix,
+ mint_mode,
+ schema_version,
+ sponsorship,
+ limits,
+ meta_update_permission,
+ } = <CollectionById<T>>::get(collection)?;
+ Some(RpcCollection {
+ name: name.into_inner(),
+ description: description.into_inner(),
+ owner,
+ mode,
+ access,
+ token_prefix: token_prefix.into_inner(),
+ mint_mode,
+ schema_version,
+ sponsorship,
+ limits,
+ meta_update_permission,
+ offchain_schema: <CollectionData<T>>::get((
+ collection,
+ CollectionField::OffchainSchema,
+ ))
+ .into_inner(),
+ const_on_chain_schema: <CollectionData<T>>::get((
+ collection,
+ CollectionField::ConstOnChainSchema,
+ ))
+ .into_inner(),
+ variable_on_chain_schema: <CollectionData<T>>::get((
+ collection,
+ CollectionField::VariableOnChainSchema,
+ ))
+ .into_inner(),
+ })
+ }
}
impl<T: Config> Pallet<T> {
@@ -520,14 +597,11 @@
access: data.access.unwrap_or_default(),
description: data.description,
token_prefix: data.token_prefix,
- offchain_schema: data.offchain_schema,
schema_version: data.schema_version.unwrap_or_default(),
sponsorship: data
.pending_sponsor
.map(SponsorshipState::Unconfirmed)
.unwrap_or_default(),
- variable_on_chain_schema: data.variable_on_chain_schema,
- const_on_chain_schema: data.const_on_chain_schema,
limits: data
.limits
.map(|limits| Self::clamp_limits(data.mode.clone(), &Default::default(), limits))
@@ -557,6 +631,24 @@
<CreatedCollectionCount<T>>::put(created_count);
<Pallet<T>>::deposit_event(Event::CollectionCreated(id, data.mode.id(), owner.clone()));
<CollectionById<T>>::insert(id, collection);
+ Self::set_field_raw(
+ id,
+ CollectionField::OffchainSchema,
+ data.offchain_schema.into_inner(),
+ )
+ .expect("data has lower bounds than field");
+ Self::set_field_raw(
+ id,
+ CollectionField::VariableOnChainSchema,
+ data.variable_on_chain_schema.into_inner(),
+ )
+ .expect("data has lower bounds than field");
+ Self::set_field_raw(
+ id,
+ CollectionField::ConstOnChainSchema,
+ data.const_on_chain_schema.into_inner(),
+ )
+ .expect("data has lower bounds than field");
Ok(id)
}
@@ -579,6 +671,7 @@
<DestroyedCollectionCount<T>>::put(destroyed_collections);
<CollectionById<T>>::remove(collection.id);
+ <CollectionData<T>>::remove_prefix((collection.id,), None);
<AdminAmount<T>>::remove(collection.id);
<IsAdmin<T>>::remove_prefix((collection.id,), None);
<Allowlist<T>>::remove_prefix((collection.id,), None);
@@ -587,6 +680,35 @@
Ok(())
}
+ fn set_field_raw(
+ collection_id: CollectionId,
+ field: CollectionField,
+ value: Vec<u8>,
+ ) -> DispatchResult {
+ if !value.is_empty() {
+ <CollectionData<T>>::insert(
+ (collection_id, field),
+ BoundedVec::try_from(value).map_err(|_| <Error<T>>::CollectionFieldSizeExceeded)?,
+ )
+ } else {
+ <CollectionData<T>>::remove((collection_id, field));
+ }
+ Ok(())
+ }
+
+ pub fn set_field(
+ collection: &CollectionHandle<T>,
+ sender: &T::CrossAccountId,
+ field: CollectionField,
+ value: Vec<u8>,
+ ) -> DispatchResult {
+ collection.check_is_owner_or_admin(sender)?;
+
+ // =========
+
+ Self::set_field_raw(collection.id, field, value)
+ }
+
pub fn toggle_allowlist(
collection: &CollectionHandle<T>,
sender: &T::CrossAccountId,
pallets/unique/src/lib.rsdiffbeforeafterboth--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -43,7 +43,7 @@
MAX_COLLECTION_NAME_LENGTH, MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH,
AccessMode, CreateItemData, CollectionLimits, CollectionId, CollectionMode, TokenId,
SchemaVersion, SponsorshipState, MetaUpdatePermission, CreateCollectionData, CustomDataLimit,
- CreateItemExData, budget,
+ CreateItemExData, budget, CollectionField,
};
use pallet_evm::account::CrossAccountId;
use pallet_common::{
@@ -1004,16 +1004,16 @@
schema: BoundedVec<u8, ConstU32<OFFCHAIN_SCHEMA_LIMIT>>,
) -> DispatchResult {
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
- let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
- target_collection.check_is_owner_or_admin(&sender)?;
+ let collection = <CollectionHandle<T>>::try_get(collection_id)?;
+
+ // =========
- target_collection.offchain_schema = schema;
+ <PalletCommon<T>>::set_field(&collection, &sender, CollectionField::OffchainSchema, schema.into_inner())?;
<Pallet<T>>::deposit_event(Event::<T>::OffchainSchemaSet(
collection_id
));
-
- target_collection.save()
+ Ok(())
}
/// Set const on-chain data schema.
@@ -1036,16 +1036,16 @@
schema: BoundedVec<u8, ConstU32<CONST_ON_CHAIN_SCHEMA_LIMIT>>
) -> DispatchResult {
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
- let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
- target_collection.check_is_owner_or_admin(&sender)?;
+ let collection = <CollectionHandle<T>>::try_get(collection_id)?;
+
+ // =========
- target_collection.const_on_chain_schema = schema;
+ <PalletCommon<T>>::set_field(&collection, &sender, CollectionField::ConstOnChainSchema, schema.into_inner())?;
<Pallet<T>>::deposit_event(Event::<T>::ConstOnChainSchemaSet(
collection_id
));
-
- target_collection.save()
+ Ok(())
}
/// Set variable on-chain data schema.
@@ -1068,16 +1068,16 @@
schema: BoundedVec<u8, ConstU32<VARIABLE_ON_CHAIN_SCHEMA_LIMIT>>
) -> DispatchResult {
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
- let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
- target_collection.check_is_owner_or_admin(&sender)?;
+ let collection = <CollectionHandle<T>>::try_get(collection_id)?;
+
+ // =========
- target_collection.variable_on_chain_schema = schema;
+ <PalletCommon<T>>::set_field(&collection, &sender, CollectionField::VariableOnChainSchema, schema.into_inner())?;
<Pallet<T>>::deposit_event(Event::<T>::VariableOnChainSchemaSet(
collection_id
));
-
- target_collection.save()
+ Ok(())
}
#[weight = <SelfWeightOf<T>>::set_collection_limits()]
primitives/data-structs/src/lib.rsdiffbeforeafterboth78pub const VARIABLE_ON_CHAIN_SCHEMA_LIMIT: u32 = 8192;78pub const VARIABLE_ON_CHAIN_SCHEMA_LIMIT: u32 = 8192;79pub const CONST_ON_CHAIN_SCHEMA_LIMIT: u32 = 32768;79pub const CONST_ON_CHAIN_SCHEMA_LIMIT: u32 = 32768;8081pub const COLLECTION_FIELD_LIMIT: u32 = CONST_ON_CHAIN_SCHEMA_LIMIT;82// u32::max is not const: OFFCHAIN_SCHEMA_LIMIT.max(VARIABLE_ON_CHAIN_SCHEMA_LIMIT).max(CONST_ON_CHAIN_SCHEMA_LIMIT);808381pub const MAX_COLLECTION_NAME_LENGTH: u32 = 64;84pub const MAX_COLLECTION_NAME_LENGTH: u32 = 64;82pub const MAX_COLLECTION_DESCRIPTION_LENGTH: u32 = 256;85pub const MAX_COLLECTION_DESCRIPTION_LENGTH: u32 = 256;248 }251 }249}252}250253254/// Used in storage251#[struct_versioning::versioned(version = 2, upper)]255#[struct_versioning::versioned(version = 2, upper)]252#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]256#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]253#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]254pub struct Collection<AccountId> {257pub struct Collection<AccountId> {255 pub owner: AccountId,258 pub owner: AccountId,256 pub mode: CollectionMode,259 pub mode: CollectionMode,257 pub access: AccessMode,260 pub access: AccessMode,258 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]259 pub name: BoundedVec<u16, ConstU32<MAX_COLLECTION_NAME_LENGTH>>,261 pub name: BoundedVec<u16, ConstU32<MAX_COLLECTION_NAME_LENGTH>>,260 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]261 pub description: BoundedVec<u16, ConstU32<MAX_COLLECTION_DESCRIPTION_LENGTH>>,262 pub description: BoundedVec<u16, ConstU32<MAX_COLLECTION_DESCRIPTION_LENGTH>>,262 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]263 pub token_prefix: BoundedVec<u8, ConstU32<MAX_TOKEN_PREFIX_LENGTH>>,263 pub token_prefix: BoundedVec<u8, ConstU32<MAX_TOKEN_PREFIX_LENGTH>>,264 pub mint_mode: bool,264 pub mint_mode: bool,265265 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]266 #[version(..2)]266 pub offchain_schema: BoundedVec<u8, ConstU32<OFFCHAIN_SCHEMA_LIMIT>>,267 pub offchain_schema: BoundedVec<u8, ConstU32<OFFCHAIN_SCHEMA_LIMIT>>,268267 pub schema_version: SchemaVersion,269 pub schema_version: SchemaVersion,272 #[version(2.., upper(limits.into()))]274 #[version(2.., upper(limits.into()))]273 pub limits: CollectionLimitsVersion2,275 pub limits: CollectionLimitsVersion2,274276275 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]277 #[version(..2)]276 pub variable_on_chain_schema: BoundedVec<u8, ConstU32<VARIABLE_ON_CHAIN_SCHEMA_LIMIT>>,278 pub variable_on_chain_schema: BoundedVec<u8, ConstU32<VARIABLE_ON_CHAIN_SCHEMA_LIMIT>>,277 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]279 #[version(..2)]278 pub const_on_chain_schema: BoundedVec<u8, ConstU32<CONST_ON_CHAIN_SCHEMA_LIMIT>>,280 pub const_on_chain_schema: BoundedVec<u8, ConstU32<CONST_ON_CHAIN_SCHEMA_LIMIT>>,281279 pub meta_update_permission: MetaUpdatePermission,282 pub meta_update_permission: MetaUpdatePermission,280}283}284285/// Used in RPC calls286#[derive(Encode, Decode, Clone, PartialEq, TypeInfo)]287#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]288pub struct RpcCollection<AccountId> {289 pub owner: AccountId,290 pub mode: CollectionMode,291 pub access: AccessMode,292 pub name: Vec<u16>,293 pub description: Vec<u16>,294 pub token_prefix: Vec<u8>,295 pub mint_mode: bool,296 pub offchain_schema: Vec<u8>,297 pub schema_version: SchemaVersion,298 pub sponsorship: SponsorshipState<AccountId>,299 pub limits: CollectionLimits,300 pub variable_on_chain_schema: Vec<u8>,301 pub const_on_chain_schema: Vec<u8>,302 pub meta_update_permission: MetaUpdatePermission,303}304305#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]306#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]307pub enum CollectionField {308 VariableOnChainSchema,309 ConstOnChainSchema,310 OffchainSchema,311}281312282#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, Debug, Derivative, MaxEncodedLen)]313#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, Debug, Derivative, MaxEncodedLen)]283#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]314#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]primitives/rpc/src/lib.rsdiffbeforeafterboth--- a/primitives/rpc/src/lib.rs
+++ b/primitives/rpc/src/lib.rs
@@ -16,7 +16,7 @@
#![cfg_attr(not(feature = "std"), no_std)]
-use up_data_structs::{CollectionId, TokenId, Collection, CollectionStats, CollectionLimits};
+use up_data_structs::{CollectionId, TokenId, RpcCollection, Collection, CollectionStats, CollectionLimits};
use sp_std::vec::Vec;
use codec::Decode;
use sp_runtime::DispatchError;
@@ -53,7 +53,7 @@
fn allowlist(collection: CollectionId) -> Result<Vec<CrossAccountId>>;
fn allowed(collection: CollectionId, user: CrossAccountId) -> Result<bool>;
fn last_token_id(collection: CollectionId) -> Result<TokenId>;
- fn collection_by_id(collection: CollectionId) -> Result<Option<Collection<AccountId>>>;
+ fn collection_by_id(collection: CollectionId) -> Result<Option<RpcCollection<AccountId>>>;
fn collection_stats() -> Result<CollectionStats>;
fn next_sponsored(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result<Option<u64>>;
fn effective_collection_limits(collection_id: CollectionId) -> Result<Option<CollectionLimits>>;
runtime/common/src/runtime_apis.rsdiffbeforeafterboth--- a/runtime/common/src/runtime_apis.rs
+++ b/runtime/common/src/runtime_apis.rs
@@ -58,8 +58,8 @@
fn last_token_id(collection: CollectionId) -> Result<TokenId, DispatchError> {
dispatch_unique_runtime!(collection.last_token_id())
}
- fn collection_by_id(collection: CollectionId) -> Result<Option<Collection<AccountId>>, DispatchError> {
- Ok(<pallet_common::CollectionById<Runtime>>::get(collection))
+ fn collection_by_id(collection: CollectionId) -> Result<Option<RpcCollection<AccountId>>, DispatchError> {
+ Ok(<pallet_common::Pallet<Runtime>>::rpc_collection(collection))
}
fn collection_stats() -> Result<CollectionStats, DispatchError> {
Ok(<pallet_common::Pallet<Runtime>>::collection_stats())
runtime/opal/src/lib.rsdiffbeforeafterboth--- a/runtime/opal/src/lib.rs
+++ b/runtime/opal/src/lib.rs
@@ -67,7 +67,7 @@
},
};
use up_data_structs::mapping::{EvmTokenAddressMapping, CrossTokenAddressMapping};
-use up_data_structs::{CollectionId, TokenId, CollectionStats, Collection};
+use up_data_structs::{CollectionId, TokenId, CollectionStats, Collection, RpcCollection};
// use pallet_contracts::weights::WeightInfo;
// #[cfg(any(feature = "std", test))]
use frame_system::{