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
2 changes: 1 addition & 1 deletion pallets/asset/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl mock_origin::Config for Test {

parameter_types! {
pub const MaxEncodedValueLength: u32 = 1_024;
pub const MaxAssetDistribution: u32 = u32::MAX;
pub const MaxAssetDistribution: u32 = 25;
}

impl Config for Test {
Expand Down
97 changes: 96 additions & 1 deletion pallets/asset/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use crate::mock::*;
use crate::{mock::*, types::AssetIssuanceEntry, Error};
use codec::Encode;
use cord_utilities::mock::{mock_origin::DoubleOrigin, SubjectId};
use frame_support::{assert_err, assert_ok, BoundedVec};
Expand Down Expand Up @@ -2839,3 +2839,98 @@ fn asset_vc_status_change_with_wrong_asset_id_should_fail() {
);
});
}

#[test]
fn asset_issue_should_fail_when_distribution_limit_exceeds() {
let creator = DID_00;
let author = ACCOUNT_00;
let capacity = 200u64;

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 = 1000;
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()[..]);

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

let asset_id: Ss58Identifier = generate_asset_id::<Test>(&issue_id_digest);

new_test_ext().execute_with(|| {
// Create and approve space
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()
));

let max_distribution = <Test as Config>::MaxAssetDistribution::get();

for _ in 0..max_distribution {
let issuance_entry = AssetIssuanceEntry {
asset_id: asset_id.clone(),
asset_owner: creator.clone(),
asset_issuance_qty: Some(1),
};

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,
issuance_digest,
authorization_id.clone()
));
}
let exceeding_entry = AssetIssuanceEntry {
asset_id: asset_id.clone(),
asset_owner: creator.clone(),
asset_issuance_qty: Some(1),
};

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

assert_err!(
Asset::issue(
DoubleOrigin(author.clone(), creator.clone()).into(),
exceeding_entry,
exceeding_digest,
authorization_id.clone()
),
Error::<Test>::DistributionLimitExceeded
);
});
}
Loading