From 75756d0339278c97957e9300dec8cb864d5c550d Mon Sep 17 00:00:00 2001 From: Satish Mohan <54302767+smohan-dw@users.noreply.github.com> Date: Mon, 4 Dec 2023 10:06:46 +0530 Subject: [PATCH] scoring updates (#257) * score -> network-score and identifier integration * extrinsic timestamp addition * fix updating rating identifier status --- Cargo.lock | 1 + pallets/network-score/Cargo.toml | 3 + pallets/network-score/src/benchmarking.rs | 40 ++-- pallets/network-score/src/lib.rs | 220 +++++++++++++--------- pallets/network-score/src/mock.rs | 9 +- pallets/network-score/src/tests.rs | 3 +- pallets/network-score/src/types.rs | 23 ++- primitives/cord/src/curi.rs | 28 +-- runtime/src/lib.rs | 2 +- 9 files changed, 191 insertions(+), 138 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 22a623dfb..d1b4b659e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6212,6 +6212,7 @@ dependencies = [ "frame-system", "pallet-balances", "pallet-chain-space", + "pallet-timestamp", "parity-scale-codec", "scale-info", "sp-core", diff --git a/pallets/network-score/Cargo.toml b/pallets/network-score/Cargo.toml index 8c0436261..3cb4fbf03 100644 --- a/pallets/network-score/Cargo.toml +++ b/pallets/network-score/Cargo.toml @@ -49,6 +49,8 @@ sp-std = { git = "https://github.com/dhiway/substrate-sdk", branch = "release-v1 sp-core = { git = "https://github.com/dhiway/substrate-sdk", branch = "release-v1.3.0", default-features = false, optional = true } sp-io = { git = "https://github.com/dhiway/substrate-sdk", branch = "release-v1.3.0", default-features = false, optional = true } sp-keystore = { git = "https://github.com/dhiway/substrate-sdk", branch = "release-v1.3.0", default-features = false, optional = true } +timestamp = { package = "pallet-timestamp", git = "https://github.com/dhiway/substrate-sdk", default-features = false, branch = "release-v1.3.0" } + [features] default = ['std'] @@ -72,6 +74,7 @@ std = [ "sp-io/std", "sp-std/std", "pallet-chain-space/std", + "timestamp/std", "identifier/std", ] try-runtime = [ diff --git a/pallets/network-score/src/benchmarking.rs b/pallets/network-score/src/benchmarking.rs index c36ac8ef9..e92711124 100644 --- a/pallets/network-score/src/benchmarking.rs +++ b/pallets/network-score/src/benchmarking.rs @@ -59,10 +59,11 @@ benchmarks! { let entry = RatingInputEntryOf:: { entity_uid: entity_uid.clone(), provider_uid, - total_rating: 250u64, + total_encoded_rating: 250u64, count_of_txn: 7u64, entity_type: EntityTypeOf::Logistic, rating_type: RatingTypeOf::Overall, + provider_did: did1.clone(), }; let raw_space = [2u8; 256].to_vec(); @@ -77,12 +78,12 @@ benchmarks! { let authorization_id: AuthorizationIdOf = Ss58Identifier::to_authorization_id(&auth_digest.encode()[..]).unwrap(); - let origin = ::EnsureOrigin::generate_origin(caller, did1.clone()); + let origin = ::EnsureOrigin::generate_origin(caller.clone(), did1.clone()); let entry_digest = ::Hashing::hash(&entry.encode()[..]); let id_digest = ::Hashing::hash( - &[&(entry_digest.clone()).encode()[..], &message_id.encode()[..], &space_id.encode()[..], &did1.encode()[..]].concat()[..] + &[&(entry_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id.encode()[..], &space_id.encode()[..], &did1.encode()[..]].concat()[..] ); let identifier = Ss58Identifier::to_scoring_id(&id_digest.encode()[..]).unwrap(); @@ -93,10 +94,10 @@ benchmarks! { }: _(origin, entry, entry_digest, message_id, authorization_id) verify { - assert_last_event::(Event::RatingEntryAdded { identifier, entity: entity_uid}.into()); + assert_last_event::(Event::RatingEntryAdded { identifier, entity: entity_uid, provider: did1, creator: caller}.into()); } - amend_rating { + revoke_rating { let l in 1 .. MAX_PAYLOAD_BYTE_LENGTH; let caller: T::AccountId = account("caller", 0, SEED); @@ -115,22 +116,23 @@ benchmarks! { let entry = RatingInputEntryOf:: { entity_uid: entity_uid.clone(), provider_uid, - total_rating: 250u64, + total_encoded_rating: 250u64, count_of_txn: 7u64, entity_type: EntityTypeOf::Logistic, rating_type: RatingTypeOf::Overall, + provider_did: did1.clone(), }; let entry_digest = ::Hashing::hash( &[&entry.encode()[..]].concat()[..], ); let id_digest_add = ::Hashing::hash( - &[&(entry_digest.clone()).encode()[..], &message_id_add.encode()[..], &space_id.encode()[..], &did1.encode()[..]].concat()[..] + &[&(entry_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id_add.encode()[..], &space_id.encode()[..], &did1.encode()[..]].concat()[..] ); let identifier_add = Ss58Identifier::to_scoring_id(&id_digest_add.encode()[..]).unwrap(); let id_digest = ::Hashing::hash( - &[&(entry_digest.clone()).encode()[..], &message_id_revoke.encode()[..], &space_id.encode()[..], &did1.encode()[..]].concat()[..] + &[&(entry_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id_revoke.encode()[..], &space_id.encode()[..], &did1.encode()[..]].concat()[..] ); let identifier_revoke = Ss58Identifier::to_scoring_id(&id_digest.encode()[..]).unwrap(); @@ -140,7 +142,7 @@ benchmarks! { let authorization_id: AuthorizationIdOf = Ss58Identifier::to_authorization_id(&auth_digest.encode()[..]).unwrap(); - let origin = ::EnsureOrigin::generate_origin(caller, did1); + let origin = ::EnsureOrigin::generate_origin(caller.clone(), did1.clone()); let chain_space_origin = RawOrigin::Root.into(); pallet_chain_space::Pallet::::create(origin.clone(), space_digest )?; @@ -149,7 +151,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}.into()); + assert_last_event::(Event::RatingEntryRevoked { identifier: identifier_revoke, entity: entity_uid, provider: did1, creator: caller}.into()); } revise_rating { @@ -173,10 +175,11 @@ benchmarks! { let entry = RatingInputEntryOf:: { entity_uid: entity_uid.clone(), provider_uid: provider_uid.clone(), - total_rating: 250u64, + total_encoded_rating: 250u64, count_of_txn: 7u64, entity_type: EntityTypeOf::Logistic, rating_type: RatingTypeOf::Overall, + provider_did: did.clone(), }; let entry_digest = ::Hashing::hash( @@ -186,27 +189,28 @@ benchmarks! { let entry_revise = RatingInputEntryOf:: { entity_uid: entity_uid.clone(), provider_uid, - total_rating: 250u64, + total_encoded_rating: 250u64, count_of_txn: 6u64, entity_type: EntityTypeOf::Logistic, rating_type: RatingTypeOf::Overall, + provider_did: did.clone(), }; let entry_revise_digest = ::Hashing::hash( &[&entry.encode()[..]].concat()[..], ); let id_digest_add = ::Hashing::hash( - &[&(entry_digest.clone()).encode()[..], &message_id_add.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..] + &[&(entry_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id_add.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..] ); let identifier_add = Ss58Identifier::to_scoring_id(&id_digest_add.encode()[..]).unwrap(); let id_digest = ::Hashing::hash( - &[&(entry_digest.clone()).encode()[..], &message_id_revoke.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..] + &[&(entry_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id_revoke.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..] ); let identifier_revoke = Ss58Identifier::to_scoring_id(&id_digest.encode()[..]).unwrap(); let id_digest_revise = ::Hashing::hash( - &[&(entry_revise_digest.clone()).encode()[..], &message_id_revise.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..] + &[&(entry_revise_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id_revise.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..] ); let identifier_revise = Ss58Identifier::to_scoring_id(&id_digest_revise.encode()[..]).unwrap(); @@ -216,17 +220,17 @@ benchmarks! { let authorization_id: AuthorizationIdOf = Ss58Identifier::to_authorization_id(&auth_digest.encode()[..]).unwrap(); - let origin = ::EnsureOrigin::generate_origin(caller, did); + let origin = ::EnsureOrigin::generate_origin(caller.clone(), did.clone()); let chain_space_origin = RawOrigin::Root.into(); pallet_chain_space::Pallet::::create(origin.clone(), space_digest )?; pallet_chain_space::Pallet::::approve(chain_space_origin, space_id, 10u64 ).expect("Approval should not fail."); let _ = Pallet::::register_rating(origin.clone(), entry, entry_digest, message_id_add, authorization_id.clone()); - let _ = Pallet::::amend_rating(origin.clone(), identifier_add, message_id_revoke, entry_digest, authorization_id.clone()); + 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}.into()); + assert_last_event::(Event::RatingEntryRevised { identifier: identifier_revise, entity: entity_uid, 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 4b4f87b71..e5a8912a2 100644 --- a/pallets/network-score/src/lib.rs +++ b/pallets/network-score/src/lib.rs @@ -150,7 +150,7 @@ pub mod pallet { pub type RatingHashOf = ::Hash; /// Type of a creator identifier. - pub type RatingCreatorIdOf = pallet_chain_space::SpaceCreatorOf; + pub type RatingProviderIdOf = pallet_chain_space::SpaceCreatorOf; /// Hash of the Rating. pub type RatingEntryHashOf = ::Hash; @@ -171,35 +171,36 @@ pub mod pallet { pub type ProviderIdentifierOf = BoundedVec::MaxEncodedValueLength>; pub type RatingInputEntryOf = - RatingInputEntry, EntityTypeOf, RatingTypeOf>; + RatingInputEntry, RatingProviderIdOf, EntityTypeOf, RatingTypeOf>; pub type RatingEntryOf = RatingEntry< EntityIdentifierOf, + RatingProviderIdOf, EntityTypeOf, RatingTypeOf, RatingEntryIdOf, RatingEntryHashOf, MessageIdentifierOf, SpaceIdOf, - RatingCreatorIdOf, + AccountIdOf, EntryTypeOf, - BlockNumberFor, + ::Moment, >; // pub type AggregatedEntryOf = AggregatedEntry; #[pallet::config] pub trait Config: - frame_system::Config + pallet_chain_space::Config + identifier::Config + frame_system::Config + pallet_chain_space::Config + identifier::Config + timestamp::Config { type RuntimeEvent: From> + IsType<::RuntimeEvent>; type EnsureOrigin: EnsureOrigin< ::RuntimeOrigin, Success = ::OriginSuccess, >; - type OriginSuccess: CallSources, RatingCreatorIdOf>; + type OriginSuccess: CallSources, RatingProviderIdOf>; - type RatingCreatorIdOf: Parameter + MaxEncodedLen; + type RatingProviderIdOf: Parameter + MaxEncodedLen; #[pallet::constant] type MaxEncodedValueLength: Get; @@ -242,7 +243,7 @@ pub mod pallet { Twox64Concat, MessageIdentifierOf, Blake2_128Concat, - EntityIdentifierOf, + RatingProviderIdOf, RatingEntryIdOf, >; @@ -250,14 +251,29 @@ pub mod pallet { #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { /// A new rating entry has been added. - /// \[rating entry identifier, entity\] - RatingEntryAdded { identifier: RatingEntryIdOf, entity: EntityIdentifierOf }, + /// \[rating entry identifier, entity, provider, creator\] + RatingEntryAdded { + identifier: RatingEntryIdOf, + entity: EntityIdentifierOf, + provider: RatingProviderIdOf, + creator: AccountIdOf, + }, /// A rating entry has been amended. - /// \[rating entry identifier, entity, \] - RatingEntryRevoked { identifier: RatingEntryIdOf, entity: EntityIdentifierOf }, + /// \[rating entry identifier, entity, creator\] + RatingEntryRevoked { + identifier: RatingEntryIdOf, + entity: EntityIdentifierOf, + provider: RatingProviderIdOf, + creator: AccountIdOf, + }, /// A rating entry has been revised (after amend). - /// \[rating entry identifier, entity, \] - RatingEntryRevised { identifier: RatingEntryIdOf, entity: EntityIdentifierOf }, + /// \[rating entry identifier, entity, creator \] + RatingEntryRevised { + identifier: RatingEntryIdOf, + entity: EntityIdentifierOf, + provider: RatingProviderIdOf, + creator: AccountIdOf, + }, /// Aggregate scores has been updated. /// \[entity identifier\] AggregateScoreUpdated { entity: EntityIdentifierOf }, @@ -294,7 +310,7 @@ pub mod pallet { /// Referenced rating identifier not found ReferenceIdentifierNotFound, /// Refrenced identifer is not a debit transaction - ReferenceNotAmendIdentifier, + ReferenceNotDebitIdentifier, /// Rating Entity mismatch EntityMismatch, /// Rating Space mismatch @@ -348,17 +364,20 @@ pub mod pallet { message_id: MessageIdentifierOf, authorization: AuthorizationIdOf, ) -> DispatchResult { - let creator = ::EnsureOrigin::ensure_origin(origin)?.subject(); + let tx_authors = ::EnsureOrigin::ensure_origin(origin)?; + let provider = tx_authors.subject(); + let creator = tx_authors.sender(); let space_id = pallet_chain_space::Pallet::::ensure_authorization_origin( &authorization, - &creator, + &provider, ) .map_err(>::from)?; ensure!( - entry.total_rating > 0 && + entry.total_encoded_rating > 0 && entry.count_of_txn > 0 && - entry.total_rating <= entry.count_of_txn * T::MaxRatingValue::get() as u64, + entry.total_encoded_rating <= + entry.count_of_txn * T::MaxRatingValue::get() as u64, Error::::InvalidRatingValue ); @@ -369,18 +388,23 @@ pub mod pallet { ); ensure!( - !>::contains_key(&message_id, &entry.provider_uid), + !>::contains_key(&message_id, &provider), Error::::MessageIdAlreadyExists ); - // Id Digest = concat (H(, () - // , )) + let provider_did = entry.provider_did.clone(); + let entity_uid = entry.entity_uid.clone(); + + // Id Digest = concat (H(,(), + // ( , + // )) let id_digest = ::Hashing::hash( &[ &digest.encode()[..], + &entity_uid.encode()[..], &message_id.encode()[..], &space_id.encode()[..], - &creator.encode()[..], + &provider_did.encode()[..], ] .concat()[..], ); @@ -393,12 +417,10 @@ pub mod pallet { Error::::RatingIdentifierAlreadyAdded ); - let block_number = frame_system::Pallet::::block_number(); - Self::aggregate_score(&entry, EntryTypeOf::Credit)?; let entity = entry.entity_uid.clone(); - let provider = entry.provider_uid.clone(); + let created_at = Self::get_current_time(); >::insert( &identifier, @@ -407,18 +429,23 @@ pub mod pallet { digest, message_id: message_id.clone(), space: space_id, - creator, + creator_id: creator.clone(), entry_type: EntryTypeOf::Credit, reference_id: None, - created_at: block_number, + created_at, }, ); - >::insert(message_id, provider, &identifier); + >::insert(message_id, &provider_did, &identifier); Self::update_activity(&identifier, CallTypeOf::Genesis).map_err(Error::::from)?; - Self::deposit_event(Event::RatingEntryAdded { identifier, entity }); + Self::deposit_event(Event::RatingEntryAdded { + identifier, + entity, + provider: provider_did, + creator, + }); Ok(()) } @@ -463,17 +490,20 @@ pub mod pallet { /// ``` #[pallet::call_index(1)] #[pallet::weight({0})] - pub fn amend_rating( + pub fn revoke_rating( origin: OriginFor, entry_identifier: RatingEntryIdOf, message_id: MessageIdentifierOf, digest: RatingEntryHashOf, authorization: AuthorizationIdOf, ) -> DispatchResult { - let creator = ::EnsureOrigin::ensure_origin(origin)?.subject(); + let tx_authors = ::EnsureOrigin::ensure_origin(origin)?; + let provider = tx_authors.subject(); + let creator = tx_authors.sender(); + let space_id = pallet_chain_space::Pallet::::ensure_authorization_origin( &authorization, - &creator, + &provider, ) .map_err(>::from)?; @@ -483,21 +513,23 @@ pub mod pallet { ensure!(rating_details.space == space_id, Error::::UnauthorizedOperation); ensure!( - !>::contains_key( - &message_id, - &rating_details.entry.provider_uid - ), + !>::contains_key(&message_id, &provider), Error::::MessageIdAlreadyExists ); - // Id Digest = concat (H(, () - // , )) + let provider_did = rating_details.entry.provider_did.clone(); + let entity_uid = rating_details.entry.entity_uid.clone(); + + // Id Digest = concat (H(,(), + // () , + // )) let id_digest = ::Hashing::hash( &[ &digest.encode()[..], + &entity_uid.encode()[..], &message_id.encode()[..], &space_id.encode()[..], - &creator.encode()[..], + &provider_did.encode()[..], ] .concat()[..], ); @@ -510,32 +542,34 @@ pub mod pallet { Error::::RatingIdentifierAlreadyAdded ); - let block_number = frame_system::Pallet::::block_number(); - Self::aggregate_score(&rating_details.entry, EntryTypeOf::Debit)?; let entity = rating_details.entry.entity_uid.clone(); - let provider = rating_details.entry.provider_uid.clone(); + let created_at = Self::get_current_time(); >::insert( &identifier, RatingEntryOf:: { - creator, - + creator_id: creator.clone(), entry_type: EntryTypeOf::Debit, reference_id: Some(entry_identifier.clone()), - created_at: block_number, + created_at, ..rating_details }, ); - >::insert(&message_id, provider, &identifier); + >::insert(&message_id, &provider_did, &identifier); Self::update_activity(&identifier, CallTypeOf::Genesis).map_err(Error::::from)?; Self::update_activity(&entry_identifier, CallTypeOf::Debit) .map_err(Error::::from)?; - Self::deposit_event(Event::RatingEntryRevoked { identifier, entity }); + Self::deposit_event(Event::RatingEntryRevoked { + identifier, + entity, + provider: provider_did, + creator, + }); Ok(()) } @@ -593,20 +627,24 @@ pub mod pallet { entry: RatingInputEntryOf, digest: RatingEntryHashOf, message_id: MessageIdentifierOf, - amend_ref_id: RatingEntryIdOf, + debit_ref_id: RatingEntryIdOf, authorization: AuthorizationIdOf, ) -> DispatchResult { - let creator = ::EnsureOrigin::ensure_origin(origin)?.subject(); + let tx_authors = ::EnsureOrigin::ensure_origin(origin)?; + let provider = tx_authors.subject(); + let creator = tx_authors.sender(); + let space_id = pallet_chain_space::Pallet::::ensure_authorization_origin( &authorization, - &creator, + &provider, ) .map_err(>::from)?; ensure!( - entry.total_rating > 0 && + entry.total_encoded_rating > 0 && entry.count_of_txn > 0 && - entry.total_rating <= entry.count_of_txn * T::MaxRatingValue::get() as u64, + entry.total_encoded_rating <= + entry.count_of_txn * T::MaxRatingValue::get() as u64, Error::::InvalidRatingValue ); @@ -616,39 +654,38 @@ pub mod pallet { Error::::InvalidEntryOrRatingType ); - let rating_details = >::get(&amend_ref_id) + let rating_details = >::get(&debit_ref_id) .ok_or(Error::::ReferenceIdentifierNotFound)?; - let input_entity_uid = &entry.entity_uid; - let stored_entity_uid = &rating_details.entry.entity_uid; - ensure!(input_entity_uid == stored_entity_uid, Error::::EntityMismatch); - - let ref_space_id = &space_id; - let stored_space_id = &rating_details.space; - ensure!(ref_space_id == stored_space_id, Error::::SpaceMismatch); + ensure!( + entry.entity_uid == rating_details.entry.entity_uid, + Error::::EntityMismatch + ); + ensure!(space_id == rating_details.space, Error::::SpaceMismatch); let stored_entry_type: EntryTypeOf = rating_details.entry_type; ensure!( EntryTypeOf::Debit == stored_entry_type, - Error::::ReferenceNotAmendIdentifier + Error::::ReferenceNotDebitIdentifier ); ensure!( - !>::contains_key( - &message_id, - &rating_details.entry.provider_uid - ), + !>::contains_key(&message_id, &provider), Error::::MessageIdAlreadyExists ); - // Id Digest = concat (H(, () - // , )) + let provider_did = entry.provider_did.clone(); + let entity_uid = entry.entity_uid.clone(); + // Id Digest = concat (H(, (), + // (), , + // )) let id_digest = ::Hashing::hash( &[ &digest.encode()[..], + &entity_uid.encode()[..], &message_id.encode()[..], &space_id.encode()[..], - &creator.encode()[..], + &provider_did.encode()[..], ] .concat()[..], ); @@ -661,12 +698,10 @@ pub mod pallet { Error::::RatingIdentifierAlreadyAdded ); - let block_number = frame_system::Pallet::::block_number(); - - Self::aggregate_score(&rating_details.entry, EntryTypeOf::Debit)?; + Self::aggregate_score(&entry, EntryTypeOf::Credit)?; let entity = rating_details.entry.entity_uid.clone(); - let provider = rating_details.entry.provider_uid.clone(); - let reference_id = rating_details.reference_id.clone().unwrap(); + let reference_id_option = rating_details.reference_id; + let created_at = Self::get_current_time(); >::insert( &identifier, @@ -675,19 +710,26 @@ pub mod pallet { digest, space: space_id, message_id: message_id.clone(), - creator, + creator_id: creator.clone(), entry_type: EntryTypeOf::Credit, - reference_id: rating_details.reference_id, - created_at: block_number, + reference_id: reference_id_option.clone(), + created_at, }, ); - >::insert(message_id, provider, &identifier); + >::insert(message_id, &provider_did, &identifier); Self::update_activity(&identifier, CallTypeOf::Genesis).map_err(Error::::from)?; - Self::update_activity(&reference_id, CallTypeOf::Credit).map_err(Error::::from)?; - - Self::deposit_event(Event::RatingEntryRevised { identifier, entity }); + if let Some(reference_id) = reference_id_option { + Self::update_activity(&reference_id, CallTypeOf::Credit) + .map_err(Error::::from)?; + } + Self::deposit_event(Event::RatingEntryRevised { + identifier, + entity, + provider: provider_did, + creator, + }); Ok(()) } @@ -734,14 +776,14 @@ impl Pallet { EntryTypeOf::Credit => { aggregate.count_of_txn = aggregate.count_of_txn.saturating_add(entry.count_of_txn); - aggregate.total_rating = - aggregate.total_rating.saturating_add(entry.total_rating); + aggregate.total_encoded_rating = + aggregate.total_encoded_rating.saturating_add(entry.total_encoded_rating); }, EntryTypeOf::Debit => { aggregate.count_of_txn = aggregate.count_of_txn.saturating_sub(entry.count_of_txn); - aggregate.total_rating = - aggregate.total_rating.saturating_sub(entry.total_rating); + aggregate.total_encoded_rating = + aggregate.total_encoded_rating.saturating_sub(entry.total_encoded_rating); }, }; >::insert( @@ -749,13 +791,13 @@ impl Pallet { &entry.rating_type, AggregatedEntryOf { count_of_txn: aggregate.count_of_txn, - total_rating: aggregate.total_rating, + total_encoded_rating: aggregate.total_encoded_rating, }, ); } else { let new_score_entry = AggregatedEntryOf { count_of_txn: entry.count_of_txn, - total_rating: entry.total_rating, + total_encoded_rating: entry.total_encoded_rating, }; >::insert(&entry.entity_uid, &entry.rating_type, new_score_entry); } @@ -803,4 +845,8 @@ impl Pallet { index: frame_system::Pallet::::extrinsic_index().unwrap_or_default(), } } + + fn get_current_time() -> T::Moment { + timestamp::Pallet::::get() + } } diff --git a/pallets/network-score/src/mock.rs b/pallets/network-score/src/mock.rs index 304619a17..e6ce4d82f 100644 --- a/pallets/network-score/src/mock.rs +++ b/pallets/network-score/src/mock.rs @@ -39,6 +39,7 @@ construct_runtime!( Space: pallet_chain_space, Identifier: identifier, Score: pallet_score, + TimeStamp: timestamp, MockOrigin: mock_origin, } ); @@ -86,7 +87,7 @@ parameter_types! { impl pallet_score::Config for Test { type MaxEncodedValueLength = MaxEncodedValueLength; type MaxRatingValue = MaxRatingValue; - type RatingCreatorIdOf = SubjectId; + type RatingProviderIdOf = SubjectId; type EnsureOrigin = mock_origin::EnsureDoubleOrigin; type OriginSuccess = mock_origin::DoubleOrigin; type RuntimeEvent = RuntimeEvent; @@ -115,6 +116,12 @@ parameter_types! { impl identifier::Config for Test { type MaxEventsHistory = MaxEventsHistory; } +impl timestamp::Config for Test { + type Moment = u64; + type OnTimestampSet = (); + type MinimumPeriod = ConstU64<5>; + type WeightInfo = (); +} #[allow(dead_code)] pub(crate) fn new_test_ext() -> sp_io::TestExternalities { diff --git a/pallets/network-score/src/tests.rs b/pallets/network-score/src/tests.rs index 26b8a4282..85fe5ac0b 100644 --- a/pallets/network-score/src/tests.rs +++ b/pallets/network-score/src/tests.rs @@ -48,10 +48,11 @@ fn check_successful_rating_creation() { let entry = RatingInputEntryOf:: { entity_uid, provider_uid, - total_rating: 250u64, + total_encoded_rating: 250u64, count_of_txn: 7u64, entity_type: EntityTypeOf::Logistic, rating_type: RatingTypeOf::Overall, + provider_did: creator.clone(), }; let entry_digest = ::Hashing::hash(&[&entry.encode()[..]].concat()[..]); diff --git a/pallets/network-score/src/types.rs b/pallets/network-score/src/types.rs index ad14c99fe..10fe703ea 100644 --- a/pallets/network-score/src/types.rs +++ b/pallets/network-score/src/types.rs @@ -33,7 +33,7 @@ pub struct EntityDetails { #[derive( Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq, PartialOrd, Ord, TypeInfo, MaxEncodedLen, )] -pub struct RatingInputEntry { +pub struct RatingInputEntry { /// Unique Identifier (UID) for the entity being rated pub entity_uid: EntityIdentifier, /// Unique Identifier (UID) for the rating provider @@ -41,11 +41,13 @@ pub struct RatingInputEntry { /// Count of raing transactions for the entry pub count_of_txn: u64, /// Cumulative sum of ratings for the entity - pub total_rating: u64, + 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 + pub provider_did: RatingProviderId, } #[derive(Encode, Decode, MaxEncodedLen, Clone, RuntimeDebug, PartialEq, Eq, TypeInfo)] @@ -83,31 +85,32 @@ impl RatingTypeOf { )] pub struct RatingEntry< EntityIdentifier, + RatingProviderId, EntityTypeOf, RatingTypeOf, RatingEntryId, RatingEntryHash, MessageIdentifier, SpaceIdOf, - RatingCreatorId, + AccountId, EntryTypeOf, - BlockNumber, + Moment, > { - pub entry: RatingInputEntry, + pub entry: RatingInputEntry, /// rating digest pub digest: RatingEntryHash, /// messsage identifier of the rating entry pub message_id: MessageIdentifier, /// Space Identifier pub space: SpaceIdOf, - /// Rating creator. - pub creator: RatingCreatorId, + /// entity anchoring the transaction on-chain + pub creator_id: AccountId, /// Type of the rating entry (credit/debit) pub entry_type: EntryTypeOf, /// Rating reference entry pub reference_id: Option, - /// The block number in which journal entry is included - pub created_at: BlockNumber, + /// The block number in which rating entry is included + pub created_at: Moment, } #[derive( @@ -117,5 +120,5 @@ pub struct AggregatedEntryOf { /// aggregated transaction count pub count_of_txn: u64, /// aggregated rating - pub total_rating: u64, + pub total_encoded_rating: u64, } diff --git a/primitives/cord/src/curi.rs b/primitives/cord/src/curi.rs index e64a79a90..eb0000a1a 100644 --- a/primitives/cord/src/curi.rs +++ b/primitives/cord/src/curi.rs @@ -29,16 +29,15 @@ use sp_std::{fmt::Debug, prelude::Clone, str, vec}; /// CORD Identifier Prefix const PREFIX: &[u8] = b"CRDIDFR"; /// CORD idents -const IDENT_SPACE: u16 = 7101; -const IDENT_AUTH: u16 = 10447; -const IDENT_SCHEMA: u16 = 1424; +const IDENT_SPACE: u16 = 3390; +const IDENT_AUTH: u16 = 2092; +const IDENT_SCHEMA: u16 = 7366; const IDENT_STATEMENT: u16 = 8902; const IDENT_ENTITY: u16 = 6480; -const IDENT_TEMPLATE: u16 = 5035; -const IDENT_ASSET: u16 = 2604; -const IDENT_UNIQUE: u16 = 8903; -const IDENT_SCORING: u16 = 11034; -const IDENT_ASSET_INSTANCE: u16 = 13172; +const IDENT_TEMPLATE: u16 = 8911; +const IDENT_ASSET: u16 = 2348; +const IDENT_RATING: u16 = 6077; +const IDENT_ASSET_INSTANCE: u16 = 11380; /// The minimum length of a valid identifier. pub const MINIMUM_IDENTIFIER_LENGTH: usize = 2; @@ -170,11 +169,8 @@ impl Ss58Identifier { pub fn to_asset_instance_id(data: &[u8]) -> Result { Self::from_encoded(data, IDENT_ASSET_INSTANCE) } - pub fn to_unique_id(data: &[u8]) -> Result { - Self::from_encoded(data, IDENT_UNIQUE) - } pub fn to_scoring_id(data: &[u8]) -> Result { - Self::from_encoded(data, IDENT_SCORING) + Self::from_encoded(data, IDENT_RATING) } pub fn inner(&self) -> &[u8] { &self.0 @@ -216,14 +212,6 @@ impl Ss58Identifier { Ss58Identifier(bounded_error_value) } - // /// Returns a default `Ss58Identifi - // /// er` with an error state. - // pub fn default_error() -> Self { - // let error_state = vec![0; 49]; - // let bounded_error_state = BoundedVec::try_from(error_state) - // .expect("Should not fail as the length is within bounds"); - // Ss58Identifier(bounded_error_state) - // } } impl AsRef<[u8]> for Ss58Identifier { diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 59862b84d..5aeaa28f1 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -821,7 +821,7 @@ impl pallet_sudo::Config for Runtime { } impl pallet_network_score::Config for Runtime { - type RatingCreatorIdOf = DidIdentifier; + type RatingProviderIdOf = DidIdentifier; type EnsureOrigin = pallet_did::EnsureDidOrigin; type OriginSuccess = pallet_did::DidRawOrigin; type RuntimeEvent = RuntimeEvent;