Skip to content

Commit

Permalink
network-score: Updates to match specs of ONDC (#420)
Browse files Browse the repository at this point in the history
Signed-off-by: Shreevatsa N <[email protected]>
  • Loading branch information
vatsa287 authored Apr 10, 2024
1 parent 8e87479 commit 2cec4f2
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 109 deletions.
50 changes: 23 additions & 27 deletions pallets/network-score/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,13 @@ 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 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(),
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 +86,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 +97,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 +114,13 @@ 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 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(),
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 +129,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 +153,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 +172,13 @@ 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 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(),
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 +188,10 @@ benchmarks! {
);

let entry_revise = RatingInputEntryOf::<T> {
entity_uid: entity_uid.clone(),
provider_uid,
entity_id: entity_id.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 +200,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 +229,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
63 changes: 24 additions & 39 deletions pallets/network-score/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,11 @@ pub mod pallet {
pub type ProviderIdentifierOf<T> = BoundedVec<u8, <T as Config>::MaxEncodedValueLength>;

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

pub type RatingEntryOf<T> = RatingEntry<
EntityIdentifierOf<T>,
RatingProviderIdOf<T>,
EntityTypeOf,
RatingTypeOf,
RatingEntryIdOf,
RatingEntryHashOf<T>,
Expand All @@ -184,8 +183,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 @@ -300,8 +297,8 @@ pub mod pallet {
DigestAlreadyAnchored,
/// Rating idenfier already exist
RatingIdentifierAlreadyAdded,
/// Invalid rating or entry type
InvalidEntryOrRatingType,
/// Invalid rating type
InvalidRatingType,
/// Rating identifier not found
RatingIdentifierNotFound,
/// Referenced rating identifier not found
Expand Down Expand Up @@ -334,7 +331,7 @@ pub mod pallet {
/// # Errors
/// Returns `Error::<T>::InvalidRatingValue` if the rating value is not
/// within the expected range.
/// Returns `Error::<T>::InvalidEntryOrRatingType` if the entry type or
/// Returns `Error::<T>::InvalidRatingType` if the entry type or
/// rating type is not valid.
/// Returns `Error::<T>::MessageIdAlreadyExists` if the message
/// identifier is already used.
Expand Down Expand Up @@ -377,27 +374,23 @@ 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!(entry.rating_type.is_valid_rating_type(), Error::<T>::InvalidRatingType);

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>),
// Id Digest = concat (H(<scale_encoded_digest>,(<scale_encoded_entity_id>),
// (<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 +411,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 +509,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>),
// Id Digest = concat (H(<scale_encoded_digest>,(<scale_encoded_entity_id>),
// (<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 +538,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 @@ -596,7 +589,7 @@ pub mod pallet {
/// # Errors
/// Returns `Error::<T>::InvalidRatingValue` if the new rating value is
/// not within the expected range.
/// Returns `Error::<T>::InvalidEntryOrRatingType` if the entry type or
/// Returns `Error::<T>::InvalidRatingType` if the entry type or
/// rating type of the new rating is invalid.
/// Returns `Error::<T>::ReferenceIdentifierNotFound` if the original
/// rating reference identifier is not found.
Expand Down Expand Up @@ -648,19 +641,12 @@ 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!(entry.rating_type.is_valid_rating_type(), Error::<T>::InvalidRatingType);

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,14 +661,14 @@ pub mod pallet {
);

let provider_did = entry.provider_did.clone();
let entity_uid = entry.entity_uid.clone();
// Id Digest = concat (H(<scale_encoded_digest>, (<scale_encoded_entity_uid>),
let entity_id = entry.entity_id.clone();
// Id Digest = concat (H(<scale_encoded_digest>, (<scale_encoded_entity_id>),
// (<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 @@ -702,7 +688,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 +757,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 +774,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 +786,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

0 comments on commit 2cec4f2

Please sign in to comment.