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

pallet-asset: Added new test-case for DistributionLimitExceeded. #522

Merged
merged 10 commits into from
Jan 6, 2025
107 changes: 106 additions & 1 deletion pallets/asset/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use super::*;
use crate::mock::*;
use codec::Encode;
use cord_utilities::mock::{mock_origin::DoubleOrigin, SubjectId};
use frame_support::{assert_err, assert_ok, BoundedVec};
use frame_support::{assert_err, assert_ok, assert_noop, BoundedVec};
use crate::{Error, types::{ AssetIssuanceEntry}};
use frame_system::RawOrigin;
use pallet_chain_space::{SpaceCodeOf, SpaceIdOf};
use sp_runtime::{traits::Hash, AccountId32};
Expand Down Expand Up @@ -226,6 +227,110 @@ fn asset_issue_should_succeed() {
});
}

#[test]
fn asset_issue_should_succeed_until_limit() {
ritankarsaha marked this conversation as resolved.
Show resolved Hide resolved
let creator = DID_00;
let author = ACCOUNT_00;
let capacity = 5u64;

let raw_space = [2u8; 256].to_vec();
let space_digest = <Test as frame_system::Config>::Hashing::hash(&raw_space.encode()[..]);
let space_id_digest = <Test as frame_system::Config>::Hashing::hash(
&[&space_digest.encode()[..], &creator.encode()[..]].concat()[..],
);
let space_id: SpaceIdOf = generate_space_id::<Test>(&space_id_digest);

let auth_digest = <Test as frame_system::Config>::Hashing::hash(
&[&space_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..],
);
let authorization_id: Ss58Identifier = generate_authorization_id::<Test>(&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::<Test> {
asset_desc,
asset_qty,
asset_type,
asset_value,
asset_tag,
asset_meta,
};

let digest = <Test as frame_system::Config>::Hashing::hash(&[&entry.encode()[..]].concat()[..]);

new_test_ext().execute_with(|| {
let space_id_clone = space_id.clone();
let _authorization_id_clone = authorization_id.clone();

assert_ok!(Space::create(
DoubleOrigin(author.clone(), creator.clone()).into(),
space_digest,
));

assert_ok!(Space::approve(RawOrigin::Root.into(), space_id_clone, capacity));

assert_ok!(Asset::create(
DoubleOrigin(author.clone(), creator.clone()).into(),
entry.clone(),
digest,
authorization_id.clone()
));

for i in 1..=25 {
let _recipient = SubjectId(AccountId32::new([i as u8; 32]));
let asset_id = space_id.clone();
ritankarsaha marked this conversation as resolved.
Show resolved Hide resolved
let asset_owner = creator.clone();
let asset_issuance_qty = asset_qty;

let issuance_entry = AssetIssuanceEntry {
asset_id,
asset_owner,
asset_issuance_qty: Some(asset_issuance_qty),
};

let issuance_digest = <Test as frame_system::Config>::Hashing::hash(&issuance_entry.encode()[..]);


assert_ok!(Asset::issue(
DoubleOrigin(author.clone(), creator.clone()).into(),
issuance_entry.clone(),
issuance_digest,
authorization_id.clone()
));
}

let _exceeding_recipient = SubjectId(AccountId32::new([26u8; 32]));
ritankarsaha marked this conversation as resolved.
Show resolved Hide resolved
let exceeding_asset_id = space_id.clone();
let exceeding_asset_owner = creator.clone();
let exceeding_asset_issuance_qty = asset_qty;

let exceeding_entry = AssetIssuanceEntry {
asset_id: exceeding_asset_id,
asset_owner: exceeding_asset_owner,
asset_issuance_qty: Some(exceeding_asset_issuance_qty),
};

let exceeding_digest = <Test as frame_system::Config>::Hashing::hash(&exceeding_entry.encode()[..]);

assert_noop!(
ritankarsaha marked this conversation as resolved.
Show resolved Hide resolved
Asset::issue(
DoubleOrigin(author.clone(), creator.clone()).into(),
exceeding_entry,
exceeding_digest,
authorization_id.clone()
),
Error::<Test>::DistributionLimitExceeded
);
});
}



#[test]
fn asset_overissuance_should_fail() {
let creator = DID_00;
Expand Down
Loading