Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom metadata storage #450

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pallets/asset/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ benchmarks! {
pallet_chain_space::Pallet::<T>::approve(chain_space_origin, space_id, capacity).expect("Approval should not fail.");
Pallet::<T>::create(origin.clone(), entry, digest, authorization_id.clone())?;

}: _<T::RuntimeOrigin>(origin, issue_entry, issue_entry_digest, authorization_id)
}: _<T::RuntimeOrigin>(origin, issue_entry, issue_entry_digest, authorization_id, None)
verify {
assert_last_event::<T>(Event::Issue { identifier: asset_id, instance: instance_id }.into());
}
Expand Down Expand Up @@ -265,7 +265,7 @@ benchmarks! {
pallet_chain_space::Pallet::<T>::create(origin.clone(), space_digest )?;
pallet_chain_space::Pallet::<T>::approve(chain_space_origin, space_id, capacity).expect("Approval should not fail.");
Pallet::<T>::create(origin.clone(), entry, digest, authorization_id.clone())?;
Pallet::<T>::issue(origin.clone(), issue_entry, issue_entry_digest, authorization_id)?;
Pallet::<T>::issue(origin.clone(), issue_entry, issue_entry_digest, authorization_id, None)?;

}: _<T::RuntimeOrigin>(origin, transfer_entry, transfer_entry_digest)
verify {
Expand Down Expand Up @@ -346,7 +346,7 @@ benchmarks! {
pallet_chain_space::Pallet::<T>::create(origin.clone(), space_digest )?;
pallet_chain_space::Pallet::<T>::approve(chain_space_origin, space_id, capacity).expect("Approval should not fail.");
Pallet::<T>::create(origin.clone(), entry, digest, authorization_id.clone())?;
Pallet::<T>::issue(origin.clone(), issue_entry, issue_entry_digest, authorization_id)?;
Pallet::<T>::issue(origin.clone(), issue_entry, issue_entry_digest, authorization_id, None)?;

}: _<T::RuntimeOrigin>(origin, asset_id.clone(), Some(instance_id.clone()), new_status.clone())
verify {
Expand Down
31 changes: 31 additions & 0 deletions pallets/asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ pub mod pallet {
pub type AssetTagOf<T> = BoundedVec<u8, <T as Config>::MaxEncodedValueLength>;
pub type AssetMetadataOf<T> = BoundedVec<u8, <T as Config>::MaxEncodedValueLength>;

pub type AssetKeyOf<T> = BoundedVec<u8, <T as Config>::MaxEncodedValueLength>;
pub type AssetValueOf<T> = BoundedVec<u8, <T as Config>::MaxEncodedValueLength>;

pub type AssetInputEntryOf<T> =
AssetInputEntry<AssetDescriptionOf<T>, AssetTypeOf, AssetTagOf<T>, AssetMetadataOf<T>>;

Expand Down Expand Up @@ -128,6 +131,9 @@ pub mod pallet {
#[pallet::constant]
type MaxAssetDistribution: Get<u32>;

#[pallet::constant]
type MaxMetaPairLength: Get<u32>;

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}
Expand Down Expand Up @@ -185,6 +191,17 @@ pub mod pallet {
pub type AssetLookup<T> =
StorageMap<_, Blake2_128Concat, EntryHashOf<T>, AssetIdOf, OptionQuery>;

#[pallet::storage]
pub type AssetMeta<T> = StorageDoubleMap<
_,
Twox64Concat,
AssetIdOf,
Blake2_128Concat,
AssetKeyOf<T>,
AssetValueOf<T>,
OptionQuery,
>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
Expand Down Expand Up @@ -308,6 +325,9 @@ pub mod pallet {
entry: AssetIssuanceEntryOf<T>,
digest: EntryHashOf<T>,
authorization: AuthorizationIdOf,
meta: Option<
BoundedVec<(AssetKeyOf<T>, AssetValueOf<T>), <T as Config>::MaxMetaPairLength>,
>,
) -> DispatchResult {
let issuer = <T as Config>::EnsureOrigin::ensure_origin(origin)?.subject();
let space_id = pallet_chain_space::Pallet::<T>::ensure_authorization_origin(
Expand All @@ -320,6 +340,11 @@ pub mod pallet {

ensure!(asset.asset_issuer == issuer, Error::<T>::UnauthorizedOperation);

// ensure!(
// meta.as_ref().map_or(true, |m| m.len() <= T::MaxMetaPairLength as usize),
// Error::<T>::UnauthorizedOperation
// );

ensure!(AssetStatusOf::ACTIVE == asset.asset_status, Error::<T>::AssetNotActive);

let issuance_qty = entry.asset_issuance_qty.unwrap_or(1);
Expand Down Expand Up @@ -357,6 +382,12 @@ pub mod pallet {
.map_err(|_| Error::<T>::DistributionLimitExceeded)
})?;

if let Some(meta) = meta {
for (key, value) in meta.iter() {
<AssetMeta<T>>::insert(&entry.asset_id, &key, &value);
}
}

<AssetLookup<T>>::insert(digest, &entry.asset_id);

<Issuance<T>>::insert(
Expand Down
2 changes: 2 additions & 0 deletions pallets/asset/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl mock_origin::Config for Test {
parameter_types! {
pub const MaxEncodedValueLength: u32 = 1_024;
pub const MaxAssetDistribution: u32 = u32::MAX;
pub const MaxMetaPairLength: u32 = 5;
}

impl Config for Test {
Expand All @@ -73,6 +74,7 @@ impl Config for Test {
type OriginSuccess = mock_origin::DoubleOrigin<AccountId, SubjectId>;
type MaxEncodedValueLength = MaxEncodedValueLength;
type MaxAssetDistribution = MaxAssetDistribution;
type MaxMetaPairLength = MaxMetaPairLength;
type WeightInfo = ();
}

Expand Down
3 changes: 2 additions & 1 deletion pallets/asset/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ fn asset_issue_should_succeed() {
DoubleOrigin(author.clone(), creator.clone()).into(),
issue_entry.clone(),
issue_entry_digest,
authorization_id
authorization_id,
None
));
});
}
Expand Down
2 changes: 2 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@ impl pallet_network_score::Config for Runtime {
parameter_types! {
pub const MaxEncodedValueLength: u32 = 1_024;
pub const MaxAssetDistribution: u32 = u32::MAX;
pub const MaxMetaPairLength: u32 = 5;
}

impl pallet_asset::Config for Runtime {
Expand All @@ -806,6 +807,7 @@ impl pallet_asset::Config for Runtime {
type OriginSuccess = pallet_did::DidRawOrigin<AccountId, DidIdentifier>;
type MaxEncodedValueLength = MaxEncodedValueLength;
type MaxAssetDistribution = MaxAssetDistribution;
type MaxMetaPairLength = MaxMetaPairLength;
type WeightInfo = weights::pallet_asset::WeightInfo<Runtime>;
}

Expand Down