From f6003be6e3adc99add2954bda384f06dd83e818b Mon Sep 17 00:00:00 2001 From: Amiya Behera Date: Sun, 1 Dec 2024 16:28:14 +0530 Subject: [PATCH] profile validation location --- .../profile-validation/src/extras.rs | 8 +- custom-pallets/profile-validation/src/lib.rs | 24 +++- .../profile-validation/src/tests.rs | 114 ++++++++++++++++-- .../profile-validation/src/types.rs | 10 ++ 4 files changed, 139 insertions(+), 17 deletions(-) diff --git a/custom-pallets/profile-validation/src/extras.rs b/custom-pallets/profile-validation/src/extras.rs index 6c9040d..f85bee6 100644 --- a/custom-pallets/profile-validation/src/extras.rs +++ b/custom-pallets/profile-validation/src/extras.rs @@ -1,10 +1,16 @@ use crate::*; impl CitizenDetailsPost { - pub fn new(citizen_id: CitizenId, created_by: T::AccountId, content: Content) -> Self { + pub fn new( + citizen_id: CitizenId, + created_by: T::AccountId, + content: Content, + location: LocationDetails, + ) -> Self { CitizenDetailsPost { created: new_who_and_when::(created_by.clone()), content, + location, citizen_id, owner: created_by, edited: false, diff --git a/custom-pallets/profile-validation/src/lib.rs b/custom-pallets/profile-validation/src/lib.rs index 6265f60..6281823 100644 --- a/custom-pallets/profile-validation/src/lib.rs +++ b/custom-pallets/profile-validation/src/lib.rs @@ -27,7 +27,7 @@ mod extras; mod permissions; mod types; -use crate::types::{ChallengeEvidencePost, ChallengerFundInfo, ProfileFundInfo}; +use crate::types::{ChallengeEvidencePost, ChallengerFundInfo, LocationDetails, ProfileFundInfo}; use frame_support::sp_runtime::traits::AccountIdConversion; use frame_support::sp_runtime::traits::{CheckedAdd, CheckedSub}; use frame_support::sp_runtime::SaturatedConversion; @@ -268,15 +268,23 @@ pub mod pallet { #[pallet::call_index(0)] #[pallet::weight(0)] - pub fn add_citizen(origin: OriginFor, content: Content) -> DispatchResult { + pub fn add_citizen( + origin: OriginFor, + content: Content, + location: LocationDetails, + ) -> DispatchResult { let who = ensure_signed(origin)?; let count = Self::next_citizen_id(); match >::get(&who) { Some(citizen_id) => { let total_funded = >::get(who.clone()); if total_funded == 0u128.saturated_into::>() { - let new_post: CitizenDetailsPost = - CitizenDetailsPost::new(citizen_id, who.clone(), content.clone()); + let new_post: CitizenDetailsPost = CitizenDetailsPost::new( + citizen_id, + who.clone(), + content.clone(), + location.clone(), + ); >::insert(who.clone(), new_post); Ok(()) } else { @@ -286,8 +294,12 @@ pub mod pallet { None => { >::insert(&who, count); - let new_post: CitizenDetailsPost = - CitizenDetailsPost::new(count, who.clone(), content.clone()); + let new_post: CitizenDetailsPost = CitizenDetailsPost::new( + count, + who.clone(), + content.clone(), + location.clone(), + ); >::insert(who.clone(), new_post); NextCitizenId::::mutate(|n| { diff --git a/custom-pallets/profile-validation/src/tests.rs b/custom-pallets/profile-validation/src/tests.rs index bb9fafb..30e95d7 100644 --- a/custom-pallets/profile-validation/src/tests.rs +++ b/custom-pallets/profile-validation/src/tests.rs @@ -1,4 +1,4 @@ -use crate::types::CitizenDetailsPost; +use crate::types::{CitizenDetailsPost, LocationDetails}; use crate::{mock::*, Error, Event}; use frame_support::{assert_noop, assert_ok}; use pallet_schelling_game_shared::types::Period; @@ -16,11 +16,22 @@ fn add_citizen_profile_check() { .as_bytes() .to_vec(), ); - assert_ok!(ProfileValidation::add_citizen(RuntimeOrigin::signed(1), content.clone())); + let location: LocationDetails = LocationDetails { + country: "India".as_bytes().to_vec(), + state: "Odisha".as_bytes().to_vec(), + city: "Bhubaneswar".as_bytes().to_vec(), + street: None, + }; + assert_ok!(ProfileValidation::add_citizen( + RuntimeOrigin::signed(1), + content.clone(), + location.clone() + )); let data = ProfileValidation::citizen_profile(1); let profile = Some(CitizenDetailsPost:: { created: WhoAndWhen { account: 1, block: 1, time: 0 }, content, + location, citizen_id: 1, owner: 1, edited: false, @@ -35,11 +46,23 @@ fn add_citizen_profile_check() { .as_bytes() .to_vec(), ); - assert_ok!(ProfileValidation::add_citizen(RuntimeOrigin::signed(1), content.clone())); + + let location: LocationDetails = LocationDetails { + country: "India".as_bytes().to_vec(), + state: "Odisha".as_bytes().to_vec(), + city: "Bhubaneswar".as_bytes().to_vec(), + street: None, + }; + assert_ok!(ProfileValidation::add_citizen( + RuntimeOrigin::signed(1), + content.clone(), + location.clone() + )); let data = ProfileValidation::citizen_profile(1); let profile = Some(CitizenDetailsPost:: { created: WhoAndWhen { account: 1, block: 5, time: 0 }, content, + location, citizen_id: 1, owner: 1, edited: false, @@ -61,11 +84,22 @@ fn check_fund_addition() { .as_bytes() .to_vec(), ); - assert_ok!(ProfileValidation::add_citizen(RuntimeOrigin::signed(1), content.clone())); + let location: LocationDetails = LocationDetails { + country: "India".as_bytes().to_vec(), + state: "Odisha".as_bytes().to_vec(), + city: "Bhubaneswar".as_bytes().to_vec(), + street: None, + }; + assert_ok!(ProfileValidation::add_citizen( + RuntimeOrigin::signed(1), + content.clone(), + location.clone() + )); let data = ProfileValidation::citizen_profile(1); let profile = Some(CitizenDetailsPost:: { created: WhoAndWhen { account: 1, block: 10, time: 0 }, content, + location, citizen_id: 1, owner: 1, edited: false, @@ -84,8 +118,18 @@ fn check_fund_addition() { .as_bytes() .to_vec(), ); + let location: LocationDetails = LocationDetails { + country: "India".as_bytes().to_vec(), + state: "Odisha".as_bytes().to_vec(), + city: "Bhubaneswar".as_bytes().to_vec(), + street: None, + }; assert_noop!( - ProfileValidation::add_citizen(RuntimeOrigin::signed(1), content.clone()), + ProfileValidation::add_citizen( + RuntimeOrigin::signed(1), + content.clone(), + location.clone() + ), Error::::NoMoreUpdates ); let data = ProfileValidation::profile_fund_details(1, 3).unwrap(); @@ -124,7 +168,17 @@ fn challenge_evidence() { .as_bytes() .to_vec(), ); - assert_ok!(ProfileValidation::add_citizen(RuntimeOrigin::signed(1), content.clone())); + let location: LocationDetails = LocationDetails { + country: "India".as_bytes().to_vec(), + state: "Odisha".as_bytes().to_vec(), + city: "Bhubaneswar".as_bytes().to_vec(), + street: None, + }; + assert_ok!(ProfileValidation::add_citizen( + RuntimeOrigin::signed(1), + content.clone(), + location.clone() + )); assert_ok!(ProfileValidation::add_profile_stake(RuntimeOrigin::signed(3), 1, 1000)); let key = SumTreeName::ProfileValidation { citizen_address: 1, block_number: 1 }; let period = SchellingGameShared::get_period(key.clone()); @@ -181,7 +235,17 @@ fn challenge_profile_after_time_for_staking_over_test() { .as_bytes() .to_vec(), ); - assert_ok!(ProfileValidation::add_citizen(RuntimeOrigin::signed(1), content.clone())); + let location: LocationDetails = LocationDetails { + country: "India".as_bytes().to_vec(), + state: "Odisha".as_bytes().to_vec(), + city: "Bhubaneswar".as_bytes().to_vec(), + street: None, + }; + assert_ok!(ProfileValidation::add_citizen( + RuntimeOrigin::signed(1), + content.clone(), + location.clone() + )); assert_ok!(ProfileValidation::add_profile_stake(RuntimeOrigin::signed(3), 1, 1000)); let key = SumTreeName::ProfileValidation { citizen_address: 1, block_number: 1 }; let period = SchellingGameShared::get_period(key.clone()); @@ -225,7 +289,17 @@ fn return_profile_stake_test() { .as_bytes() .to_vec(), ); - assert_ok!(ProfileValidation::add_citizen(RuntimeOrigin::signed(1), content.clone())); + let location: LocationDetails = LocationDetails { + country: "India".as_bytes().to_vec(), + state: "Odisha".as_bytes().to_vec(), + city: "Bhubaneswar".as_bytes().to_vec(), + street: None, + }; + assert_ok!(ProfileValidation::add_citizen( + RuntimeOrigin::signed(1), + content.clone(), + location.clone() + )); let balance = Balances::free_balance(3); assert_eq!(300000, balance); assert_ok!(ProfileValidation::add_profile_stake(RuntimeOrigin::signed(3), 1, 400)); @@ -279,7 +353,17 @@ fn schelling_game_test() { .as_bytes() .to_vec(), ); - assert_ok!(ProfileValidation::add_citizen(RuntimeOrigin::signed(1), content.clone())); + let location: LocationDetails = LocationDetails { + country: "India".as_bytes().to_vec(), + state: "Odisha".as_bytes().to_vec(), + city: "Bhubaneswar".as_bytes().to_vec(), + street: None, + }; + assert_ok!(ProfileValidation::add_citizen( + RuntimeOrigin::signed(1), + content.clone(), + location.clone() + )); assert_ok!(ProfileValidation::add_profile_stake(RuntimeOrigin::signed(3), 1, 1000)); let challenge_content: Content = Content::IPFS( "bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhabc" @@ -443,7 +527,17 @@ fn test_draw_juror() { .as_bytes() .to_vec(), ); - assert_ok!(ProfileValidation::add_citizen(RuntimeOrigin::signed(1), content.clone())); + let location: LocationDetails = LocationDetails { + country: "India".as_bytes().to_vec(), + state: "Odisha".as_bytes().to_vec(), + city: "Bhubaneswar".as_bytes().to_vec(), + street: None, + }; + assert_ok!(ProfileValidation::add_citizen( + RuntimeOrigin::signed(1), + content.clone(), + location.clone() + )); assert_ok!(ProfileValidation::add_profile_stake(RuntimeOrigin::signed(3), 1, 1000)); let challenge_content: Content = Content::IPFS( diff --git a/custom-pallets/profile-validation/src/types.rs b/custom-pallets/profile-validation/src/types.rs index 0bcf118..6c9c572 100644 --- a/custom-pallets/profile-validation/src/types.rs +++ b/custom-pallets/profile-validation/src/types.rs @@ -13,6 +13,7 @@ pub struct CitizenDetailsPost { pub created: WhoAndWhenOf, pub content: Content, pub citizen_id: CitizenId, + pub location: LocationDetails, pub owner: T::AccountId, pub edited: bool, pub hidden: bool, @@ -20,6 +21,15 @@ pub struct CitizenDetailsPost { pub downvotes_count: u32, } +#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)] +#[scale_info(skip_type_params(T))] +pub struct LocationDetails { + pub country: Vec, + pub state: Vec, + pub city: Vec, + pub street: Option>, +} + #[derive( PartialEq, Eq, PartialOrd, Ord, Default, Clone, Encode, Decode, MaxEncodedLen, TypeInfo, )]