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

Fixes: #304 : Add Test Cases for SpaceMismatch Error in pallet-network-score #534

Merged
merged 6 commits into from
Jan 12, 2025
Merged
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
117 changes: 117 additions & 0 deletions pallets/network-score/src/tests.rs
Original file line number Diff line number Diff line change
@@ -915,3 +915,120 @@ fn reference_identifier_not_found_test() {
<MessageIdentifiers<Test>>::remove(message_id_revise.clone(), creator.clone());
});
}

#[test]
fn revise_rating_with_space_mismatch_should_fail() {
let creator = DID_00.clone();
let author = ACCOUNT_00.clone();

let message_id = BoundedVec::try_from([72u8; 10].to_vec()).unwrap();
let entity_id = BoundedVec::try_from([73u8; 10].to_vec()).unwrap();
let provider_id = BoundedVec::try_from([74u8; 10].to_vec()).unwrap();

// Valid rating entry
let entry = RatingInputEntryOf::<Test> {
entity_id: entity_id.clone(),
provider_id: provider_id.clone(),
total_encoded_rating: 250u64,
count_of_txn: 7u64,
rating_type: RatingTypeOf::Overall,
provider_did: creator.clone(),
};

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

// Create "main" space:
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);

// Create "mismatch" space:
let mismatch_raw_space = [3u8; 256].to_vec();
let mismatch_space_digest =
<Test as frame_system::Config>::Hashing::hash(&mismatch_raw_space.encode()[..]);
let mismatch_space_id_digest = <Test as frame_system::Config>::Hashing::hash(
&[&mismatch_space_digest.encode()[..], &creator.encode()[..]].concat()[..],
);
let mismatch_space_id: SpaceIdOf = generate_space_id::<Test>(&mismatch_space_id_digest);

// Auth ID for main space
let auth_digest = <Test as frame_system::Config>::Hashing::hash(
&[&space_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..],
);
let authorization_id: AuthorizationIdOf =
Ss58Identifier::create_identifier(&auth_digest.encode()[..], IdentifierType::Authorization)
.unwrap();

// Auth ID for mismatch space
let mismatch_auth_digest = <Test as frame_system::Config>::Hashing::hash(
&[&mismatch_space_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..],
);
let mismatch_authorization_id: AuthorizationIdOf = Ss58Identifier::create_identifier(
&mismatch_auth_digest.encode()[..],
IdentifierType::Authorization,
)
.unwrap();

new_test_ext().execute_with(|| {
System::set_block_number(1);

// Create & approve main space
assert_ok!(Space::create(
DoubleOrigin(author.clone(), creator.clone()).into(),
space_digest
));
assert_ok!(Space::approve(RawOrigin::Root.into(), space_id, 3u64));

// Register a 'credit' rating in main space
assert_ok!(Score::register_rating(
DoubleOrigin(author.clone(), creator.clone()).into(),
entry.clone(),
entry_digest,
message_id.clone(),
authorization_id.clone(),
));

// Create & approve mismatch space
assert_ok!(Space::create(
DoubleOrigin(author.clone(), creator.clone()).into(),
mismatch_space_digest
));
assert_ok!(Space::approve(RawOrigin::Root.into(), mismatch_space_id, 3u64));

// Revoke the original rating to produce a 'debit' entry
let revoke_msg_id = BoundedVec::try_from([80u8; 10].to_vec()).unwrap();
let revoke_digest = <Test as frame_system::Config>::Hashing::hash(&[50u8; 16].to_vec()[..]);
let orig_rating_id = <MessageIdentifiers<Test>>::get(&message_id, &creator).unwrap();

assert_ok!(Score::revoke_rating(
DoubleOrigin(author.clone(), creator.clone()).into(),
orig_rating_id.clone(),
revoke_msg_id.clone(),
revoke_digest,
authorization_id.clone(),
));

// Attempt to revise using the mismatch space
let mismatch_msg_id = BoundedVec::try_from([88u8; 10].to_vec()).unwrap();
let mismatch_digest =
<Test as frame_system::Config>::Hashing::hash(&[90u8; 16].to_vec()[..]);

// This call should fail with SpaceMismatch, because the rating was made in 'space_id',
// but we pass 'mismatch_authorization_id' that points to 'mismatch_space_id'.
assert_err!(
Score::revise_rating(
DoubleOrigin(author.clone(), creator.clone()).into(),
entry.clone(),
mismatch_digest,
mismatch_msg_id,
orig_rating_id.clone(),
mismatch_authorization_id,
),
Error::<Test>::SpaceMismatch
);
});
}
Loading