From 0377f283ad4c264b6ccfac5ef1f1dffa93c2781c Mon Sep 17 00:00:00 2001 From: Amiya Behera Date: Thu, 28 Nov 2024 12:18:37 +0530 Subject: [PATCH] shared-storage link --- Cargo.lock | 2 ++ custom-pallets/profile-validation/Cargo.toml | 11 ++++++----- custom-pallets/profile-validation/src/lib.rs | 3 +++ custom-pallets/shared-storage/src/extras.rs | 15 +++++++++++++++ custom-pallets/shared-storage/src/lib.rs | 7 +++++++ runtime/src/lib.rs | 1 + traits/trait-shared-storage/src/lib.rs | 2 ++ 7 files changed, 36 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5e0c957..302e7ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5407,6 +5407,7 @@ dependencies = [ "frame-system", "pallet-balances", "pallet-schelling-game-shared", + "pallet-shared-storage", "pallet-sortition-sum-game", "pallet-support", "pallet-timestamp", @@ -5417,6 +5418,7 @@ dependencies = [ "sp-runtime", "sp-std 14.0.0 (git+https://github.com/paritytech/polkadot-sdk.git?tag=polkadot-v1.9.0)", "trait-schelling-game-shared", + "trait-shared-storage", ] [[package]] diff --git a/custom-pallets/profile-validation/Cargo.toml b/custom-pallets/profile-validation/Cargo.toml index 39015fd..a50cd9a 100644 --- a/custom-pallets/profile-validation/Cargo.toml +++ b/custom-pallets/profile-validation/Cargo.toml @@ -18,14 +18,15 @@ scale-info = { workspace = true } frame-benchmarking = { workspace = true, optional = true } frame-support = { workspace = true } frame-system = { workspace = true } -sp-std = { workspace = true} +sp-std = { workspace = true } pallet-timestamp = { workspace = true } -pallet-balances = { workspace = true } +pallet-balances = { workspace = true } pallet-support = { workspace = true } pallet-schelling-game-shared = { workspace = true } -pallet-sortition-sum-game = { workspace = true} -trait-schelling-game-shared= { workspace = true } - +pallet-sortition-sum-game = { workspace = true } +trait-schelling-game-shared = { workspace = true } +pallet-shared-storage = { workspace = true } +trait-shared-storage = { workspace = true } [dev-dependencies] diff --git a/custom-pallets/profile-validation/src/lib.rs b/custom-pallets/profile-validation/src/lib.rs index ea25831..c7bee25 100644 --- a/custom-pallets/profile-validation/src/lib.rs +++ b/custom-pallets/profile-validation/src/lib.rs @@ -45,6 +45,7 @@ use pallet_schelling_game_shared::types::{ use pallet_sortition_sum_game::types::SumTreeName; use pallet_support::{new_who_and_when, Content, WhoAndWhenOf}; use trait_schelling_game_shared::SchellingGameSharedLink; +use trait_shared_storage::SharedStorageLink; pub use types::{CitizenDetailsPost, FIRST_CHALLENGE_POST_ID, FIRST_CITIZEN_ID}; type AccountIdOf = ::AccountId; type BalanceOf = <::Currency as Currency>>::Balance; @@ -92,6 +93,8 @@ pub mod pallet { PhaseData = PhaseData, >; + type SharedStorageSource: SharedStorageLink>; + type Currency: ReservableCurrency; /// Handler for the unbalanced increment when rewarding (minting rewards) type Reward: OnUnbalanced>; diff --git a/custom-pallets/shared-storage/src/extras.rs b/custom-pallets/shared-storage/src/extras.rs index 7697c48..865aa2a 100644 --- a/custom-pallets/shared-storage/src/extras.rs +++ b/custom-pallets/shared-storage/src/extras.rs @@ -6,6 +6,9 @@ use trait_shared_storage::SharedStorageLink; impl SharedStorageLink for Pallet { type AccountId = AccountIdOf; + fn add_approved_citizen_address(new_member: Self::AccountId) -> DispatchResult { + Self::add_approved_citizen_address(new_member) + } fn check_citizen_is_approved_link(address: Self::AccountId) -> DispatchResult { Self::check_citizen_is_approved(address) } @@ -41,6 +44,18 @@ impl SharedStorageLink for Pallet { } impl Pallet { + pub(super) fn add_approved_citizen_address(new_member: T::AccountId) -> DispatchResult { + let mut members = ApprovedCitizenAddress::::get(); + + match members.binary_search(&new_member) { + Ok(_) => Err(Error::::AlreadyMember.into()), + Err(index) => { + members.insert(index, new_member.clone()); + ApprovedCitizenAddress::::put(members); + Ok(()) + }, + } + } pub(super) fn check_citizen_is_approved(address: T::AccountId) -> DispatchResult { let members = ApprovedCitizenAddress::::get(); diff --git a/custom-pallets/shared-storage/src/lib.rs b/custom-pallets/shared-storage/src/lib.rs index 68068bc..71c7d16 100644 --- a/custom-pallets/shared-storage/src/lib.rs +++ b/custom-pallets/shared-storage/src/lib.rs @@ -20,6 +20,7 @@ use types::ReputationScore; type AccountIdOf = ::AccountId; type Score = i64; +type DepartmentId = u64; #[frame_support::pallet] pub mod pallet { @@ -41,6 +42,11 @@ pub mod pallet { #[pallet::getter(fn approved_citizen_address)] pub type ApprovedCitizenAddress = StorageValue<_, Vec, ValueQuery>; // Its set, add element through binary_search + #[pallet::storage] + #[pallet::getter(fn approved_citizen_address_by_department)] + pub type ApprovedCitizenAddressByDepartment = + StorageMap<_, Blake2_128Concat, DepartmentId, Vec, ValueQuery>; + #[pallet::storage] #[pallet::getter(fn positive_externality_score)] pub type PositiveExternalityScore = @@ -89,5 +95,6 @@ pub mod pallet { /// Errors should have helpful documentation associated with them. StorageOverflow, CitizenNotApproved, + AlreadyMember, } } diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 7da1967..48b75f0 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -278,6 +278,7 @@ impl pallet_profile_validation::Config for Runtime { type WeightInfo = pallet_profile_validation::weights::SubstrateWeight; type Currency = Balances; type SchellingGameSharedSource = SchellingGameShared; + type SharedStorageSource = SharedStorage; type Slash = (); type Reward = (); } diff --git a/traits/trait-shared-storage/src/lib.rs b/traits/trait-shared-storage/src/lib.rs index 543305c..6e62932 100644 --- a/traits/trait-shared-storage/src/lib.rs +++ b/traits/trait-shared-storage/src/lib.rs @@ -5,6 +5,8 @@ use sp_std::vec::Vec; pub trait SharedStorageLink { type AccountId; + fn add_approved_citizen_address(new_member: Self::AccountId) -> DispatchResult; + fn check_citizen_is_approved_link(address: Self::AccountId) -> DispatchResult; fn get_approved_citizen_count_link() -> u64;