From 2cec4f261d986dc9f406e87654b14f733a04af4c Mon Sep 17 00:00:00 2001 From: Shree Vatsa N Date: Wed, 10 Apr 2024 10:04:43 +0530 Subject: [PATCH] network-score: Updates to match specs of ONDC (#420) Signed-off-by: Shreevatsa N --- pallets/network-score/src/benchmarking.rs | 50 +++++++++--------- pallets/network-score/src/lib.rs | 63 +++++++++-------------- pallets/network-score/src/tests.rs | 40 +++++++------- pallets/network-score/src/types.rs | 25 ++------- runtime/src/lib.rs | 2 +- 5 files changed, 71 insertions(+), 109 deletions(-) diff --git a/pallets/network-score/src/benchmarking.rs b/pallets/network-score/src/benchmarking.rs index a4fb36fd8..3a1c0b123 100644 --- a/pallets/network-score/src/benchmarking.rs +++ b/pallets/network-score/src/benchmarking.rs @@ -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:: { - 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(), }; @@ -87,7 +86,7 @@ benchmarks! { let entry_digest = ::Hashing::hash(&entry.encode()[..]); let id_digest = ::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::(&id_digest); @@ -98,7 +97,7 @@ benchmarks! { }: _(origin, entry, entry_digest, message_id, authorization_id) verify { - assert_last_event::(Event::RatingEntryAdded { identifier, entity: entity_uid, provider: did1, creator: caller}.into()); + assert_last_event::(Event::RatingEntryAdded { identifier, entity: entity_id, provider: did1, creator: caller}.into()); } revoke_rating { @@ -115,14 +114,13 @@ benchmarks! { let space_id: SpaceIdOf = generate_space_id::(&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:: { - 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(), }; @@ -131,12 +129,12 @@ benchmarks! { ); let id_digest_add = ::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::(&id_digest_add); let id_digest = ::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::(&id_digest); @@ -155,7 +153,7 @@ benchmarks! { let _ = Pallet::::register_rating(origin.clone(), entry, entry_digest, message_id_add, authorization_id.clone()); }: _(origin, identifier_add, message_id_revoke, entry_digest, authorization_id) verify { - assert_last_event::(Event::RatingEntryRevoked { identifier: identifier_revoke, entity: entity_uid, provider: did1, creator: caller}.into()); + assert_last_event::(Event::RatingEntryRevoked { identifier: identifier_revoke, entity: entity_id, provider: did1, creator: caller}.into()); } revise_rating { @@ -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:: { - 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(), }; @@ -191,11 +188,10 @@ benchmarks! { ); let entry_revise = RatingInputEntryOf:: { - 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(), }; @@ -204,17 +200,17 @@ benchmarks! { ); let id_digest_add = ::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::(&id_digest_add); let id_digest = ::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::(&id_digest); let id_digest_revise = ::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::(&id_digest_revise); @@ -233,7 +229,7 @@ benchmarks! { let _ = Pallet::::revoke_rating(origin.clone(), identifier_add, message_id_revoke, entry_digest, authorization_id.clone()); }: _(origin, entry_revise, entry_revise_digest, message_id_revise, identifier_revoke, authorization_id) verify { - assert_last_event::(Event::RatingEntryRevised { identifier: identifier_revise, entity: entity_uid, provider: did, creator: caller}.into()); + assert_last_event::(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) diff --git a/pallets/network-score/src/lib.rs b/pallets/network-score/src/lib.rs index d10dbdec0..cca848daf 100644 --- a/pallets/network-score/src/lib.rs +++ b/pallets/network-score/src/lib.rs @@ -168,12 +168,11 @@ pub mod pallet { pub type ProviderIdentifierOf = BoundedVec::MaxEncodedValueLength>; pub type RatingInputEntryOf = - RatingInputEntry, RatingProviderIdOf, EntityTypeOf, RatingTypeOf>; + RatingInputEntry, RatingProviderIdOf, RatingTypeOf>; pub type RatingEntryOf = RatingEntry< EntityIdentifierOf, RatingProviderIdOf, - EntityTypeOf, RatingTypeOf, RatingEntryIdOf, RatingEntryHashOf, @@ -184,8 +183,6 @@ pub mod pallet { ::Moment, >; - // pub type AggregatedEntryOf = AggregatedEntry; - #[pallet::config] pub trait Config: frame_system::Config + pallet_chain_space::Config + identifier::Config + timestamp::Config @@ -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 @@ -334,7 +331,7 @@ pub mod pallet { /// # Errors /// Returns `Error::::InvalidRatingValue` if the rating value is not /// within the expected range. - /// Returns `Error::::InvalidEntryOrRatingType` if the entry type or + /// Returns `Error::::InvalidRatingType` if the entry type or /// rating type is not valid. /// Returns `Error::::MessageIdAlreadyExists` if the message /// identifier is already used. @@ -377,11 +374,7 @@ pub mod pallet { Error::::InvalidRatingValue ); - ensure!( - entry.entity_type.is_valid_entity_type() && - entry.rating_type.is_valid_rating_type(), - Error::::InvalidEntryOrRatingType - ); + ensure!(entry.rating_type.is_valid_rating_type(), Error::::InvalidRatingType); ensure!( !>::contains_key(&message_id, &provider), @@ -389,15 +382,15 @@ pub mod pallet { ); 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(,(), + // Id Digest = concat (H(,(), // ( , // )) let id_digest = ::Hashing::hash( &[ &digest.encode()[..], - &entity_uid.encode()[..], + &entity_id.encode()[..], &message_id.encode()[..], &space_id.encode()[..], &provider_did.encode()[..], @@ -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(); >::insert( @@ -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(,(), + // Id Digest = concat (H(,(), // () , // )) let id_digest = ::Hashing::hash( &[ &digest.encode()[..], - &entity_uid.encode()[..], + &entity_id.encode()[..], &message_id.encode()[..], &space_id.encode()[..], &provider_did.encode()[..], @@ -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(); >::insert( @@ -596,7 +589,7 @@ pub mod pallet { /// # Errors /// Returns `Error::::InvalidRatingValue` if the new rating value is /// not within the expected range. - /// Returns `Error::::InvalidEntryOrRatingType` if the entry type or + /// Returns `Error::::InvalidRatingType` if the entry type or /// rating type of the new rating is invalid. /// Returns `Error::::ReferenceIdentifierNotFound` if the original /// rating reference identifier is not found. @@ -648,19 +641,12 @@ pub mod pallet { Error::::InvalidRatingValue ); - ensure!( - entry.entity_type.is_valid_entity_type() && - entry.rating_type.is_valid_rating_type(), - Error::::InvalidEntryOrRatingType - ); + ensure!(entry.rating_type.is_valid_rating_type(), Error::::InvalidRatingType); let rating_details = >::get(&debit_ref_id) .ok_or(Error::::ReferenceIdentifierNotFound)?; - ensure!( - entry.entity_uid == rating_details.entry.entity_uid, - Error::::EntityMismatch - ); + ensure!(entry.entity_id == rating_details.entry.entity_id, Error::::EntityMismatch); ensure!(space_id == rating_details.space, Error::::SpaceMismatch); let stored_entry_type: EntryTypeOf = rating_details.entry_type; @@ -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(, (), + let entity_id = entry.entity_id.clone(); + // Id Digest = concat (H(, (), // (), , // )) let id_digest = ::Hashing::hash( &[ &digest.encode()[..], - &entity_uid.encode()[..], + &entity_id.encode()[..], &message_id.encode()[..], &space_id.encode()[..], &provider_did.encode()[..], @@ -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(); @@ -771,8 +757,7 @@ impl Pallet { entry: &RatingInputEntryOf, rtype: EntryTypeOf, ) -> Result<(), pallet::Error> { - if let Some(mut aggregate) = - >::get(&entry.entity_uid, &entry.rating_type) + if let Some(mut aggregate) = >::get(&entry.entity_id, &entry.rating_type) { match rtype { EntryTypeOf::Credit => { @@ -789,7 +774,7 @@ impl Pallet { }, }; >::insert( - &entry.entity_uid, + &entry.entity_id, &entry.rating_type, AggregatedEntryOf { count_of_txn: aggregate.count_of_txn, @@ -801,9 +786,9 @@ impl Pallet { count_of_txn: entry.count_of_txn, total_encoded_rating: entry.total_encoded_rating, }; - >::insert(&entry.entity_uid, &entry.rating_type, new_score_entry); + >::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(()) } diff --git a/pallets/network-score/src/tests.rs b/pallets/network-score/src/tests.rs index 348a5fe7f..f657ff8ec 100644 --- a/pallets/network-score/src/tests.rs +++ b/pallets/network-score/src/tests.rs @@ -43,14 +43,13 @@ fn check_successful_rating_creation() { let author = ACCOUNT_00; 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:: { - entity_uid, - provider_uid, + entity_id, + provider_id, total_encoded_rating: 250u64, count_of_txn: 7u64, - entity_type: EntityTypeOf::Logistic, rating_type: RatingTypeOf::Overall, provider_did: creator.clone(), }; @@ -98,14 +97,13 @@ fn check_duplicate_message_id() { let author = ACCOUNT_00.clone(); 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:: { - entity_uid, - provider_uid, + entity_id, + provider_id, total_encoded_rating: 250u64, count_of_txn: 7u64, - entity_type: EntityTypeOf::Logistic, rating_type: RatingTypeOf::Overall, provider_did: creator.clone(), }; @@ -166,14 +164,13 @@ fn revise_rating_with_entry_entity_mismatch_should_fail() { let author = ACCOUNT_00; 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:: { - 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: creator.clone(), }; @@ -197,7 +194,7 @@ fn revise_rating_with_entry_entity_mismatch_should_fail() { let id_digest = ::Hashing::hash( &[ &entry_digest.encode()[..], - &entity_uid.encode()[..], + &entity_id.encode()[..], &message_id.encode()[..], &space_id.encode()[..], &creator.clone().encode()[..], @@ -229,7 +226,7 @@ fn revise_rating_with_entry_entity_mismatch_should_fail() { //error let mut mismatched_entry = entry.clone(); - mismatched_entry.entity_uid = BoundedVec::try_from([80u8; 10].to_vec()).unwrap(); + mismatched_entry.entity_id = BoundedVec::try_from([80u8; 10].to_vec()).unwrap(); let mismatched_entry_digest = ::Hashing::hash(&[&entry.encode()[..]].concat()[..]); assert_err!( @@ -252,14 +249,13 @@ fn test_register_rating_id_already_exists() { let creator = DID_00.clone(); let author = ACCOUNT_00.clone(); 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:: { - entity_uid, - provider_uid, + entity_id, + provider_id, total_encoded_rating: 250u64, count_of_txn: 7u64, - entity_type: EntityTypeOf::Logistic, rating_type: RatingTypeOf::Overall, provider_did: creator.clone(), }; diff --git a/pallets/network-score/src/types.rs b/pallets/network-score/src/types.rs index 10fe703ea..f3fbf5997 100644 --- a/pallets/network-score/src/types.rs +++ b/pallets/network-score/src/types.rs @@ -33,17 +33,15 @@ pub struct EntityDetails { #[derive( Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq, PartialOrd, Ord, TypeInfo, MaxEncodedLen, )] -pub struct RatingInputEntry { - /// Unique Identifier (UID) for the entity being rated - pub entity_uid: EntityIdentifier, +pub struct RatingInputEntry { + /// Identifier for the entity being rated + pub entity_id: EntityIdentifier, /// Unique Identifier (UID) for the rating provider - pub provider_uid: EntityIdentifier, + pub provider_id: EntityIdentifier, /// Count of raing transactions for the entry pub count_of_txn: u64, /// Cumulative sum of ratings for the entity pub total_encoded_rating: u64, - /// Type of the entity (seller/logistic) - pub entity_type: EntityTypeOf, /// Type of rating (overall/delivery) pub rating_type: RatingTypeOf, /// DID identifier of the provider @@ -56,24 +54,12 @@ pub enum RatingTypeOf { Delivery, } -#[derive(Encode, Decode, MaxEncodedLen, Clone, RuntimeDebug, PartialEq, Eq, TypeInfo)] -pub enum EntityTypeOf { - Retail, - Logistic, -} - #[derive(Encode, Decode, MaxEncodedLen, Clone, RuntimeDebug, PartialEq, Eq, TypeInfo)] pub enum EntryTypeOf { Credit, Debit, } -impl EntityTypeOf { - pub fn is_valid_entity_type(&self) -> bool { - matches!(self, Self::Retail | Self::Logistic) - } -} - impl RatingTypeOf { pub fn is_valid_rating_type(&self) -> bool { matches!(self, Self::Overall | Self::Delivery) @@ -86,7 +72,6 @@ impl RatingTypeOf { pub struct RatingEntry< EntityIdentifier, RatingProviderId, - EntityTypeOf, RatingTypeOf, RatingEntryId, RatingEntryHash, @@ -96,7 +81,7 @@ pub struct RatingEntry< EntryTypeOf, Moment, > { - pub entry: RatingInputEntry, + pub entry: RatingInputEntry, /// rating digest pub digest: RatingEntryHash, /// messsage identifier of the rating entry diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 81e471f0a..221eb5b08 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -127,7 +127,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("cord"), impl_name: create_runtime_str!("dhiway-cord"), authoring_version: 0, - spec_version: 9100, + spec_version: 9200, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 2,