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

update: pallet/network-score: type update to match specs #412

Closed
Closed
Show file tree
Hide file tree
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
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
28 changes: 8 additions & 20 deletions pallets/network-score/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ pub struct EntityDetails<EntityIdentifier> {
#[derive(
Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq, PartialOrd, Ord, TypeInfo, MaxEncodedLen,
)]
pub struct RatingInputEntry<EntityIdentifier, RatingProviderId, EntityTypeOf, RatingTypeOf> {
/// Unique Identifier (UID) for the entity being rated
pub entity_uid: EntityIdentifier,
pub struct RatingInputEntry<EntityIdentifier, EntityName, RatingProviderId, RatingTypeOf> {
/// Identifier for the entity being rated
pub entity_id: EntityIdentifier,
/// Name of the entity being rated
pub entity_name: EntityName,
/// 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
Expand All @@ -56,24 +56,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)
Expand All @@ -85,8 +73,8 @@ impl RatingTypeOf {
)]
pub struct RatingEntry<
EntityIdentifier,
EntityName,
RatingProviderId,
EntityTypeOf,
RatingTypeOf,
RatingEntryId,
RatingEntryHash,
Expand All @@ -96,7 +84,7 @@ pub struct RatingEntry<
EntryTypeOf,
Moment,
> {
pub entry: RatingInputEntry<EntityIdentifier, RatingProviderId, EntityTypeOf, RatingTypeOf>,
pub entry: RatingInputEntry<EntityIdentifier, EntityName, RatingProviderId, RatingTypeOf>,
/// rating digest
pub digest: RatingEntryHash,
/// messsage identifier of the rating entry
Expand Down
Loading