From 477aa6921eb1b104f9f61675e170472aa128e1a8 Mon Sep 17 00:00:00 2001 From: Suraj Atreya Date: Thu, 13 Jun 2024 12:28:58 +0530 Subject: [PATCH] Asset: added test cases for asset and VC over issuance (#465) --- pallets/asset/src/tests.rs | 183 +++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) diff --git a/pallets/asset/src/tests.rs b/pallets/asset/src/tests.rs index ee52717f6..1e417fb9f 100644 --- a/pallets/asset/src/tests.rs +++ b/pallets/asset/src/tests.rs @@ -495,6 +495,7 @@ fn asset_vc_status_change_should_succeed() { )); }); } + #[test] fn changing_status_of_asset_instance_with_same_status_should_fail() { // Set up test parameters @@ -874,3 +875,185 @@ fn changing_status_of_vc_asset_with_same_status_should_fail() { ); }); } + +#[test] +fn asset_over_issuance_should_not_succeed() { + let creator = DID_00; + let author = ACCOUNT_00; + let capacity = 5u64; + + let raw_space = [2u8; 256].to_vec(); + let space_digest = ::Hashing::hash(&raw_space.encode()[..]); + let space_id_digest = ::Hashing::hash( + &[&space_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + let space_id: SpaceIdOf = generate_space_id::(&space_id_digest); + + let auth_digest = ::Hashing::hash( + &[&space_id.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let authorization_id: Ss58Identifier = generate_authorization_id::(&auth_digest); + + let asset_desc = BoundedVec::try_from([72u8; 10].to_vec()).unwrap(); + let asset_tag = BoundedVec::try_from([72u8; 10].to_vec()).unwrap(); + let asset_meta = BoundedVec::try_from([72u8; 10].to_vec()).unwrap(); + let asset_qty = 10; + let asset_value = 10; + let asset_type = AssetTypeOf::MF; + + let entry = AssetInputEntryOf:: { + asset_desc, + asset_qty, + asset_type, + asset_value, + asset_tag, + asset_meta, + }; + + let digest = ::Hashing::hash(&[&entry.encode()[..]].concat()[..]); + + let issue_id_digest = ::Hashing::hash( + &[&digest.encode()[..], &space_id.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let asset_id: Ss58Identifier = generate_asset_id::(&issue_id_digest); + + let issue_entry = AssetIssuanceEntryOf:: { + asset_id, + asset_owner: creator.clone(), + asset_issuance_qty: Some(20), + }; + + let issue_entry_digest = + ::Hashing::hash(&[&issue_entry.encode()[..]].concat()[..]); + + new_test_ext().execute_with(|| { + assert_ok!(Space::create( + DoubleOrigin(author.clone(), creator.clone()).into(), + space_digest, + )); + + assert_ok!(Space::approve(RawOrigin::Root.into(), space_id, capacity)); + + assert_ok!(Asset::create( + DoubleOrigin(author.clone(), creator.clone()).into(), + entry, + digest, + authorization_id.clone() + )); + + assert_err!( + Asset::issue( + DoubleOrigin(author.clone(), creator.clone()).into(), + issue_entry.clone(), + issue_entry_digest, + authorization_id + ), + Error::::OverIssuanceLimit + ); + }); +} + +#[test] +fn asset_over_issuance_vc_status_change_should_not_succeed() { + let creator = DID_00; + let author = ACCOUNT_00; + + let capacity = 5u64; + + let raw_space = [2u8; 256].to_vec(); + let space_digest = ::Hashing::hash(&raw_space.encode()[..]); + let space_id_digest = ::Hashing::hash( + &[&space_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + let space_id: SpaceIdOf = generate_space_id::(&space_id_digest); + + let auth_digest = ::Hashing::hash( + &[&space_id.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let authorization_id: Ss58Identifier = generate_authorization_id::(&auth_digest); + + let asset_desc = BoundedVec::try_from([72u8; 10].to_vec()).unwrap(); + let asset_tag = BoundedVec::try_from([72u8; 10].to_vec()).unwrap(); + let asset_meta = BoundedVec::try_from([72u8; 10].to_vec()).unwrap(); + let asset_qty = 10; + let asset_value = 10; + let asset_type = AssetTypeOf::MF; + + let entry = AssetInputEntryOf:: { + asset_desc, + asset_qty, + asset_type, + asset_value, + asset_tag, + asset_meta, + }; + + let digest = ::Hashing::hash(&[&entry.encode()[..]].concat()[..]); + + let issue_id_digest = ::Hashing::hash( + &[&digest.encode()[..], &space_id.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let asset_id: Ss58Identifier = generate_asset_id::(&issue_id_digest); + + let issue_entry = AssetIssuanceEntryOf:: { + asset_id: asset_id.clone(), + asset_owner: creator.clone(), + asset_issuance_qty: Some(30), + }; + + let issue_entry_digest = + ::Hashing::hash(&[&issue_entry.encode()[..]].concat()[..]); + + let instance_id_digest = ::Hashing::hash( + &[ + &asset_id.encode()[..], + &creator.encode()[..], + &space_id.encode()[..], + &creator.encode()[..], + &issue_entry_digest.encode()[..], + ] + .concat()[..], + ); + + let instance_id = generate_asset_instance_id::(&instance_id_digest); + + new_test_ext().execute_with(|| { + assert_ok!(Space::create( + DoubleOrigin(author.clone(), creator.clone()).into(), + space_digest, + )); + + assert_ok!(Space::approve(RawOrigin::Root.into(), space_id, capacity)); + + assert_ok!(Asset::vc_create( + DoubleOrigin(author.clone(), creator.clone()).into(), + asset_qty, + digest, + authorization_id.clone() + )); + + assert_err!( + Asset::vc_issue( + DoubleOrigin(author.clone(), creator.clone()).into(), + issue_entry.clone(), + issue_entry_digest, + authorization_id + ), + Error::::OverIssuanceLimit + ); + + assert_err!( + Asset::vc_status_change( + DoubleOrigin(author.clone(), creator.clone()).into(), + asset_id.clone(), + Some(instance_id.clone()), + AssetStatusOf::INACTIVE + ), + Error::::AssetInstanceNotFound + ); + }); +}