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

37 improve reputation #116

Open
wants to merge 19 commits into
base: universal-develop
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,6 @@ fn testnet_genesis(
},
transaction_payment: Default::default(),
treasury: Default::default(),

}
}
1 change: 1 addition & 0 deletions pallets/dao/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ sp-core = { version = "6.0.0", default-features = false, git = "https://github.c
# universal
pallet-did = { path = "../did", package = "pallet-did", default-features = false }
pallet-profile = { path = "../profile", version = "0.7.0", default-features = false }
pallet-reputation = { path = "../reputation", version = "0.7.0", default-features = false }

# dev dependencies
[dev-dependencies]
Expand Down
16 changes: 16 additions & 0 deletions pallets/dao/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ frame_support::construct_runtime!(
Did: pallet_did::{Pallet, Call, Storage, Event<T>},
Dao: pallet_dao::{Pallet, Call, Storage, Event<T>},
Profile: pallet_profile::{Pallet, Call, Storage, Event<T>},
Reputation: pallet_reputation,
}
);
pub type Moment = u64;
Expand Down Expand Up @@ -160,6 +161,21 @@ impl pallet_balances::Config for Test {
type WeightInfo = ();
}

parameter_types! {
#[derive(TypeInfo, MaxEncodedLen, Encode)]
pub MaximumRatingsPer: u32 = 5;

pub DefaultReputation: i32 = 0;
}

impl pallet_reputation::Config for Test {
type Event = Event;
type ReputationHandler = pallet_reputation::impls::ReputationHandler;
type DefaultReputation = DefaultReputation;
type MaximumRatingsPer = MaximumRatingsPer;
}


// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
let storage = system::GenesisConfig::default().build_storage::<Test>().unwrap();
Expand Down
3 changes: 2 additions & 1 deletion pallets/profile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ frame-support = { version = "4.0.0-dev", default-features = false, git = "https:
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }
sp-std = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }
log = { version = "0.4.14", default-features = false }
pallet-balances = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }
pallet-balances = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }
pallet-reputation = { version = "0.7.0", default-features = false, path = "../reputation"}

[dev-dependencies]
sp-core = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }
Expand Down
1 change: 0 additions & 1 deletion pallets/profile/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ fn create_profile_info<T: Config>(_num_fields: u32) -> Profile<T> {
name: username.try_into().unwrap(),
interests: interests.try_into().unwrap(),
balance: Some(balance),
reputation: u32::MAX,
available_hours_per_week,
additional_information: None,
}
Expand Down
38 changes: 11 additions & 27 deletions pallets/profile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ mod tests;
mod benchmarking;
pub mod weights;


#[frame_support::pallet]
pub mod pallet {
use frame_support::{dispatch::DispatchResult, storage::bounded_vec::BoundedVec, pallet_prelude::*};
Expand All @@ -85,7 +86,11 @@ pub mod pallet {
use frame_support::traits::Currency;
use scale_info::TypeInfo;
use crate::weights::WeightInfo;

use pallet_reputation::{
traits::ReputationHandler,
Pallet as ReputationPallet,
Rating
};

// Account, Balance are used in Profile Struct
type AccountOf<T> = <T as frame_system::Config>::AccountId;
Expand All @@ -101,14 +106,13 @@ pub mod pallet {
pub name: BoundedVec<u8, T::MaxUsernameLen>,
pub interests: BoundedVec<u8, T::MaxInterestsLen>,
pub balance: Option<BalanceOf<T>>,
pub reputation: u32,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to keep the reputation as part of the profile struct.

pub available_hours_per_week: u8,
pub additional_information: Option<BoundedVec<u8, T::MaxAdditionalInformationLen>>,
}

/// Configure the pallet by specifying the parameters and types on which it depends.
#[pallet::config]
pub trait Config: frame_system::Config {
pub trait Config: frame_system::Config + pallet_reputation::Config {
/// Because this pallet emits events, it depends on the runtime's definition of an event.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;

Expand Down Expand Up @@ -262,7 +266,6 @@ pub mod pallet {
name,
interests,
balance: Some(balance),
reputation: 0,
available_hours_per_week,
additional_information,
};
Expand All @@ -276,6 +279,7 @@ pub mod pallet {
// Initialize completed tasks list with default value.
<CompletedTasks<T>>::insert(owner, BoundedVec::default());

ReputationPallet::<T>::create_reputation_record(owner);

// Increase profile count
let new_count = Self::profile_count().checked_add(1).ok_or(<Error<T>>::ProfileCountOverflow)?;
Expand Down Expand Up @@ -317,28 +321,16 @@ pub mod pallet {
// Remove profile from storage
<Profiles<T>>::remove(owner);

ReputationPallet::<T>::remove_reputation_record(owner.clone());

// Reduce profile count
let new_count = Self::profile_count().saturating_sub(1);
<ProfileCount<T>>::put(new_count);

Ok(())
}

// Public function that adds reputation to a profile
pub fn add_reputation(owner: &T::AccountId) -> Result<(), DispatchError> {

// Get current profile
let mut profile = Self::profiles(owner).ok_or(<Error<T>>::NoProfileCreated)?;

// Increase reputation
profile.increase_reputation();

// Insert into storage a new profile
<Profiles<T>>::insert(owner, profile);

Ok(())
}


// Public function that check if user has a profile
pub fn has_profile(owner: &T::AccountId) -> Result<bool, DispatchError> {

Expand All @@ -364,14 +356,6 @@ pub mod pallet {

// Change the reputation on a Profile (TODO MVP2: Improve reputation functions)
impl<T:Config> Profile<T> {
pub fn increase_reputation(&mut self) {
self.reputation += 1;
}

pub fn decrease_reputation(&mut self) {
self.reputation -= 1;
}

pub fn change_interests(&mut self, new_interests: BoundedVec<u8, T::MaxInterestsLen>) {
self.interests = new_interests;
}
Expand Down
22 changes: 19 additions & 3 deletions pallets/profile/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ frame_support::construct_runtime!(
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Profile: pallet_profile::{Pallet, Call, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
System: frame_system,
Profile: pallet_profile,
Balances: pallet_balances,
Reputation: pallet_reputation,
}
);

Expand Down Expand Up @@ -97,6 +98,21 @@ impl pallet_balances::Config for Test {
type WeightInfo = ();
}

parameter_types! {
#[derive(TypeInfo, MaxEncodedLen, Encode)]
pub MaximumRatingsPer: u32 = 5;

pub DefaultReputation: i32 = 0;
}

impl pallet_reputation::Config for Test {
type Event = Event;
type ReputationHandler = pallet_reputation::impls::ReputationHandler;
type DefaultReputation = DefaultReputation;
type MaximumRatingsPer = MaximumRatingsPer;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need max ratings?

}


// Build genesis storage according to the mock runtime.
pub(crate) fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
Expand Down
8 changes: 7 additions & 1 deletion pallets/profile/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use core::convert::TryInto;
use frame_support::storage::bounded_vec::BoundedVec;
use crate::{mock::*, Error};
use frame_support::{assert_noop, assert_ok};
use pallet_reputation::{
RepInfoOf,
Reputable,
};


// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Constants and Functions used in TESTS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Expand Down Expand Up @@ -51,12 +55,14 @@ fn verify_inputs_outputs_to_profile(){

// Get profile for current account
let profile = Profile::profiles(10).expect("should found the profile");
let maybe_reputation: Option<Reputable<Test>> = RepInfoOf::<Test>::get(10);

assert!(maybe_reputation.is_some());
// Ensure that profile properties are assigned correctly
assert_eq!(profile.name.into_inner(), &[1, 4]);
assert_eq!(profile.reputation, 0);
assert_eq!(profile.interests.into_inner(), &[2,4]);
assert_eq!(profile.available_hours_per_week, 40_u8);
assert_eq!(maybe_reputation.unwrap().reputation, 0i32);
});
}

Expand Down
39 changes: 39 additions & 0 deletions pallets/reputation/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "pallet-reputation"
version = "0.7.0"
description = "FRAME pallet for deriving the reputation of a given entity."
authors = ["UNIVERSALDOT FOUNDATION <https://github.com/UniversalDot>"]
homepage = "https://universaldot.foundation"
edition = "2021"
license = "Apache-2.0"
publish = false
repository = "https://github.com/UniversalDot/pallets"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [
"derive",
] }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }

[dev-dependencies]
sp-core = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }
sp-io = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }
sp-runtime = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }

[features]
default = ["std"]
std = [
"codec/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
]
runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"]
try-runtime = ["frame-support/try-runtime"]
63 changes: 63 additions & 0 deletions pallets/reputation/src/impls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

use crate::{
pallet::{CredibilityUnit, ReputationUnit, Reputable, Rating},
traits::{HasReputation, HasCredibility, HasAccountId}

};
use frame_support::{
pallet_prelude::*,
inherent::Vec,
BoundedVec
};

pub struct ReputationHandler;

impl<T> crate::traits::ReputationHandler<T> for ReputationHandler
where T: frame_system::Config + crate::Config
{

fn calculate_credibility<N>(entity: &N, ratings: &BoundedVec<Rating, T::MaximumRatingsPer>) -> CredibilityUnit
where N: HasCredibility
{
CredibilityUnit::default()
}

fn calculate_reputation<N>(entity: &N, ratings: &BoundedVec<Rating, T::MaximumRatingsPer>) -> ReputationUnit
where N: HasCredibility + HasReputation + HasAccountId<T>
{
let mut rep = entity.get_reputation();

let _: Vec<_> = ratings.iter().map(|r|{
let diff: i32 = *r as i32 - 3i32;
rep += diff;
}).collect::<_>();

rep.try_into().expect("input vec is bounded, output is same length; qed")
}
}


impl<T> HasCredibility for Reputable<T>
where T: frame_system::Config
{
fn get_credibility(&self) -> CredibilityUnit {
self.credibility
}

}

impl<T> HasReputation for Reputable<T>
where T: frame_system::Config
{
fn get_reputation(&self) -> ReputationUnit {
self.reputation
}
}

impl<T> HasAccountId<T> for Reputable<T>
where T: frame_system::Config
{
fn get_account_id(&self) -> &T::AccountId {
&self.account
}
}
Loading