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

network-score: Updates to match specs of ONDC #420

Merged
merged 4 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
57 changes: 30 additions & 27 deletions pallets/network-score/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ benchmarks! {
let did1: T::SpaceCreatorId = account("did1", 0, SEED);

let message_id = BoundedVec::try_from([72u8; 10].to_vec()).unwrap();
let entity_uid = BoundedVec::try_from([73u8; 10].to_vec()).unwrap();
let provider_uid = BoundedVec::try_from([74u8; 10].to_vec()).unwrap();
let entity_id = BoundedVec::try_from([73u8; 10].to_vec()).unwrap();
let entity_name = BoundedVec::try_from([73u8; 10].to_vec()).unwrap();
let provider_id = BoundedVec::try_from([74u8; 10].to_vec()).unwrap();
let entry = RatingInputEntryOf::<T> {
entity_uid: entity_uid.clone(),
provider_uid,
entity_id: entity_id.clone(),
entity_name: entity_name.clone(),
provider_id,
total_encoded_rating: 250u64,
count_of_txn: 7u64,
entity_type: EntityTypeOf::Logistic,
rating_type: RatingTypeOf::Overall,
provider_did: did1.clone(),
};
Expand All @@ -87,7 +88,7 @@ benchmarks! {
let entry_digest = <T as frame_system::Config>::Hashing::hash(&entry.encode()[..]);

let id_digest = <T as frame_system::Config>::Hashing::hash(
&[&(entry_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id.encode()[..], &space_id.encode()[..], &did1.encode()[..]].concat()[..]
&[&(entry_digest.clone()).encode()[..], &entity_id.encode()[..], &message_id.encode()[..], &space_id.encode()[..], &did1.encode()[..]].concat()[..]
);
let identifier = generate_rating_id::<T>(&id_digest);

Expand All @@ -98,7 +99,7 @@ benchmarks! {

}: _<T::RuntimeOrigin>(origin, entry, entry_digest, message_id, authorization_id)
verify {
assert_last_event::<T>(Event::RatingEntryAdded { identifier, entity: entity_uid, provider: did1, creator: caller}.into());
assert_last_event::<T>(Event::RatingEntryAdded { identifier, entity: entity_id, provider: did1, creator: caller}.into());
}

revoke_rating {
Expand All @@ -115,14 +116,15 @@ benchmarks! {
let space_id: SpaceIdOf = generate_space_id::<T>(&space_id_digest);
let message_id_add = BoundedVec::try_from([82u8; 10].to_vec()).unwrap();
let message_id_revoke = BoundedVec::try_from([85u8; 10].to_vec()).unwrap();
let entity_uid = BoundedVec::try_from([83u8; 10].to_vec()).unwrap();
let provider_uid = BoundedVec::try_from([84u8; 10].to_vec()).unwrap();
let entity_id = BoundedVec::try_from([83u8; 10].to_vec()).unwrap();
let entity_name = BoundedVec::try_from([83u8; 10].to_vec()).unwrap();
let provider_id = BoundedVec::try_from([84u8; 10].to_vec()).unwrap();
let entry = RatingInputEntryOf::<T> {
entity_uid: entity_uid.clone(),
provider_uid,
entity_id: entity_id.clone(),
entity_name: entity_name.clone(),
provider_id,
total_encoded_rating: 250u64,
count_of_txn: 7u64,
entity_type: EntityTypeOf::Logistic,
rating_type: RatingTypeOf::Overall,
provider_did: did1.clone(),
};
Expand All @@ -131,12 +133,12 @@ benchmarks! {
);

let id_digest_add = <T as frame_system::Config>::Hashing::hash(
&[&(entry_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id_add.encode()[..], &space_id.encode()[..], &did1.encode()[..]].concat()[..]
&[&(entry_digest.clone()).encode()[..], &entity_id.encode()[..], &message_id_add.encode()[..], &space_id.encode()[..], &did1.encode()[..]].concat()[..]
);
let identifier_add = generate_rating_id::<T>(&id_digest_add);

let id_digest = <T as frame_system::Config>::Hashing::hash(
&[&(entry_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id_revoke.encode()[..], &space_id.encode()[..], &did1.encode()[..]].concat()[..]
&[&(entry_digest.clone()).encode()[..], &entity_id.encode()[..], &message_id_revoke.encode()[..], &space_id.encode()[..], &did1.encode()[..]].concat()[..]
);
let identifier_revoke = generate_rating_id::<T>(&id_digest);

Expand All @@ -155,7 +157,7 @@ benchmarks! {
let _ = Pallet::<T>::register_rating(origin.clone(), entry, entry_digest, message_id_add, authorization_id.clone());
}: _<T::RuntimeOrigin>(origin, identifier_add, message_id_revoke, entry_digest, authorization_id)
verify {
assert_last_event::<T>(Event::RatingEntryRevoked { identifier: identifier_revoke, entity: entity_uid, provider: did1, creator: caller}.into());
assert_last_event::<T>(Event::RatingEntryRevoked { identifier: identifier_revoke, entity: entity_id, provider: did1, creator: caller}.into());
}

revise_rating {
Expand All @@ -174,14 +176,15 @@ benchmarks! {
let message_id_add = BoundedVec::try_from([82u8; 10].to_vec()).unwrap();
let message_id_revoke = BoundedVec::try_from([85u8; 10].to_vec()).unwrap();
let message_id_revise = BoundedVec::try_from([86u8; 10].to_vec()).unwrap();
let entity_uid = BoundedVec::try_from([83u8; 10].to_vec()).unwrap();
let provider_uid = BoundedVec::try_from([84u8; 10].to_vec()).unwrap();
let entity_id = BoundedVec::try_from([83u8; 10].to_vec()).unwrap();
let entity_name = BoundedVec::try_from([83u8; 10].to_vec()).unwrap();
let provider_id = BoundedVec::try_from([84u8; 10].to_vec()).unwrap();
let entry = RatingInputEntryOf::<T> {
entity_uid: entity_uid.clone(),
provider_uid: provider_uid.clone(),
entity_id: entity_id.clone(),
entity_name: entity_name.clone(),
provider_id: provider_id.clone(),
total_encoded_rating: 250u64,
count_of_txn: 7u64,
entity_type: EntityTypeOf::Logistic,
rating_type: RatingTypeOf::Overall,
provider_did: did.clone(),
};
Expand All @@ -191,11 +194,11 @@ benchmarks! {
);

let entry_revise = RatingInputEntryOf::<T> {
entity_uid: entity_uid.clone(),
provider_uid,
entity_id: entity_id.clone(),
entity_name: entity_name.clone(),
provider_id,
total_encoded_rating: 250u64,
count_of_txn: 6u64,
entity_type: EntityTypeOf::Logistic,
rating_type: RatingTypeOf::Overall,
provider_did: did.clone(),
};
Expand All @@ -204,17 +207,17 @@ benchmarks! {
);

let id_digest_add = <T as frame_system::Config>::Hashing::hash(
&[&(entry_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id_add.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..]
&[&(entry_digest.clone()).encode()[..], &entity_id.encode()[..], &message_id_add.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..]
);
let identifier_add = generate_rating_id::<T>(&id_digest_add);

let id_digest = <T as frame_system::Config>::Hashing::hash(
&[&(entry_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id_revoke.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..]
&[&(entry_digest.clone()).encode()[..], &entity_id.encode()[..], &message_id_revoke.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..]
);
let identifier_revoke = generate_rating_id::<T>(&id_digest);

let id_digest_revise = <T as frame_system::Config>::Hashing::hash(
&[&(entry_revise_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id_revise.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..]
&[&(entry_revise_digest.clone()).encode()[..], &entity_id.encode()[..], &message_id_revise.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..]
);
let identifier_revise = generate_rating_id::<T>(&id_digest_revise);

Expand All @@ -233,7 +236,7 @@ benchmarks! {
let _ = Pallet::<T>::revoke_rating(origin.clone(), identifier_add, message_id_revoke, entry_digest, authorization_id.clone());
}: _<T::RuntimeOrigin>(origin, entry_revise, entry_revise_digest, message_id_revise, identifier_revoke, authorization_id)
verify {
assert_last_event::<T>(Event::RatingEntryRevised { identifier: identifier_revise, entity: entity_uid, provider: did, creator: caller}.into());
assert_last_event::<T>(Event::RatingEntryRevised { identifier: identifier_revise, entity: entity_id, provider: did, creator: caller}.into());
}

impl_benchmark_test_suite! (Pallet, crate::mock::new_test_ext(), crate::mock::Test)
Expand Down
55 changes: 21 additions & 34 deletions pallets/network-score/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,19 @@ pub mod pallet {
pub type EntityIdentifierOf<T> = BoundedVec<u8, <T as Config>::MaxEncodedValueLength>;
pub type EntityIdentityOf<T> = BoundedVec<u8, <T as Config>::MaxEncodedValueLength>;
pub type ProviderIdentifierOf<T> = BoundedVec<u8, <T as Config>::MaxEncodedValueLength>;
pub type EntityNameOf<T> = BoundedVec<u8, <T as Config>::MaxEncodedValueLength>;

pub type RatingInputEntryOf<T> =
RatingInputEntry<EntityIdentifierOf<T>, RatingProviderIdOf<T>, EntityTypeOf, RatingTypeOf>;
pub type RatingInputEntryOf<T> = RatingInputEntry<
EntityIdentifierOf<T>,
EntityNameOf<T>,
RatingProviderIdOf<T>,
RatingTypeOf,
>;

pub type RatingEntryOf<T> = RatingEntry<
EntityIdentifierOf<T>,
EntityNameOf<T>,
RatingProviderIdOf<T>,
EntityTypeOf,
RatingTypeOf,
RatingEntryIdOf,
RatingEntryHashOf<T>,
Expand All @@ -184,8 +189,6 @@ pub mod pallet {
<T as timestamp::Config>::Moment,
>;

// pub type AggregatedEntryOf = AggregatedEntry;

#[pallet::config]
pub trait Config:
frame_system::Config + pallet_chain_space::Config + identifier::Config + timestamp::Config
Expand Down Expand Up @@ -377,27 +380,21 @@ pub mod pallet {
Error::<T>::InvalidRatingValue
);

ensure!(
entry.entity_type.is_valid_entity_type() &&
entry.rating_type.is_valid_rating_type(),
Error::<T>::InvalidEntryOrRatingType
);

ensure!(
!<MessageIdentifiers<T>>::contains_key(&message_id, &provider),
Error::<T>::MessageIdAlreadyExists
);

let provider_did = entry.provider_did.clone();
let entity_uid = entry.entity_uid.clone();
let entity_id = entry.entity_id.clone();

// Id Digest = concat (H(<scale_encoded_digest>,(<scale_encoded_entity_uid>),
// (<scale_encoded_message_id> <scale_encoded_space_identifier>,
// <scale_encoded_provider_identifier>))
let id_digest = <T as frame_system::Config>::Hashing::hash(
&[
&digest.encode()[..],
&entity_uid.encode()[..],
&entity_id.encode()[..],
&message_id.encode()[..],
&space_id.encode()[..],
&provider_did.encode()[..],
Expand All @@ -418,7 +415,7 @@ pub mod pallet {

Self::aggregate_score(&entry, EntryTypeOf::Credit)?;

let entity = entry.entity_uid.clone();
let entity = entry.entity_id.clone();
let created_at = Self::get_current_time();

<RatingEntries<T>>::insert(
Expand Down Expand Up @@ -516,15 +513,15 @@ pub mod pallet {
);

let provider_did = rating_details.entry.provider_did.clone();
let entity_uid = rating_details.entry.entity_uid.clone();
let entity_id = rating_details.entry.entity_id.clone();

// Id Digest = concat (H(<scale_encoded_digest>,(<scale_encoded_entity_uid>),
// (<scale_encoded_message_id>) <scale_encoded_space_identifier>,
// <scale_encoded_provider_identifier>))
let id_digest = <T as frame_system::Config>::Hashing::hash(
&[
&digest.encode()[..],
&entity_uid.encode()[..],
&entity_id.encode()[..],
&message_id.encode()[..],
&space_id.encode()[..],
&provider_did.encode()[..],
Expand All @@ -545,7 +542,7 @@ pub mod pallet {

Self::aggregate_score(&rating_details.entry, EntryTypeOf::Debit)?;

let entity = rating_details.entry.entity_uid.clone();
let entity = rating_details.entry.entity_id.clone();
let created_at = Self::get_current_time();

<RatingEntries<T>>::insert(
Expand Down Expand Up @@ -648,19 +645,10 @@ pub mod pallet {
Error::<T>::InvalidRatingValue
);

ensure!(
entry.entity_type.is_valid_entity_type() &&
entry.rating_type.is_valid_rating_type(),
Error::<T>::InvalidEntryOrRatingType
);

let rating_details = <RatingEntries<T>>::get(&debit_ref_id)
.ok_or(Error::<T>::ReferenceIdentifierNotFound)?;

ensure!(
entry.entity_uid == rating_details.entry.entity_uid,
Error::<T>::EntityMismatch
);
ensure!(entry.entity_id == rating_details.entry.entity_id, Error::<T>::EntityMismatch);
ensure!(space_id == rating_details.space, Error::<T>::SpaceMismatch);

let stored_entry_type: EntryTypeOf = rating_details.entry_type;
Expand All @@ -675,7 +663,7 @@ pub mod pallet {
);

let provider_did = entry.provider_did.clone();
let entity_uid = entry.entity_uid.clone();
let entity_uid = entry.entity_id.clone();
// Id Digest = concat (H(<scale_encoded_digest>, (<scale_encoded_entity_uid>),
// (<scale_encoded_message_id>), <scale_encoded_space_identifier>,
// <scale_encoded_provider_identifier>))
Expand All @@ -702,7 +690,7 @@ pub mod pallet {
);

Self::aggregate_score(&entry, EntryTypeOf::Credit)?;
let entity = rating_details.entry.entity_uid.clone();
let entity = rating_details.entry.entity_id.clone();
let reference_id_option = rating_details.reference_id;
let created_at = Self::get_current_time();

Expand Down Expand Up @@ -771,8 +759,7 @@ impl<T: Config> Pallet<T> {
entry: &RatingInputEntryOf<T>,
rtype: EntryTypeOf,
) -> Result<(), pallet::Error<T>> {
if let Some(mut aggregate) =
<AggregateScores<T>>::get(&entry.entity_uid, &entry.rating_type)
if let Some(mut aggregate) = <AggregateScores<T>>::get(&entry.entity_id, &entry.rating_type)
{
match rtype {
EntryTypeOf::Credit => {
Expand All @@ -789,7 +776,7 @@ impl<T: Config> Pallet<T> {
},
};
<AggregateScores<T>>::insert(
&entry.entity_uid,
&entry.entity_id,
&entry.rating_type,
AggregatedEntryOf {
count_of_txn: aggregate.count_of_txn,
Expand All @@ -801,9 +788,9 @@ impl<T: Config> Pallet<T> {
count_of_txn: entry.count_of_txn,
total_encoded_rating: entry.total_encoded_rating,
};
<AggregateScores<T>>::insert(&entry.entity_uid, &entry.rating_type, new_score_entry);
<AggregateScores<T>>::insert(&entry.entity_id, &entry.rating_type, new_score_entry);
}
Self::deposit_event(Event::AggregateScoreUpdated { entity: entry.entity_uid.clone() });
Self::deposit_event(Event::AggregateScoreUpdated { entity: entry.entity_id.clone() });
Ok(())
}

Expand Down
Loading