Skip to content

Commit

Permalink
pallet-asset: Added new test-case for DistributionLimitExceeded. (#522)
Browse files Browse the repository at this point in the history
This issue has been structured  similarly to the `asset_create_should_succeed` test.
This test will ensure that an asset can be issued successfully and will include coverage for reaching and exceeding the `MaxAssetDistribution` limit.

- Test Initialization:
The test sets up a space, approves it, and creates an asset similarly to the `asset_create_should_succeed` test.
- Asset Issuance:
The loop issues the asset up to the `MaxAssetDistribution` limit (set to 25 for testing). Each iteration simulates issuing the asset to a different recipient.
- Error Handling:
After the limit is reached, the test tries to issue the asset to one more recipient, which should trigger the `DistributionLimitExceeded` error. This ensures that the pallet's logic correctly handles and enforces the distribution limit.

This implementation checks that the asset issuance process is valid up to the `MaxAssetDistribution`  limit and correctly throws an error when the `limit` is exceeded.
  • Loading branch information
ritankarsaha authored Jan 6, 2025
1 parent 8351245 commit accb726
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
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 @@ -3057,3 +3057,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
);
});
}

0 comments on commit accb726

Please sign in to comment.