Browse Source

Merge pull request #1906 from ondratra/council_intermediate_results

council - intermediate results passing
Bedeho Mender 4 years ago
parent
commit
7702e1a243
2 changed files with 51 additions and 30 deletions
  1. 39 7
      runtime-modules/council/src/lib.rs
  2. 12 23
      runtime-modules/council/src/mock.rs

+ 39 - 7
runtime-modules/council/src/lib.rs

@@ -113,11 +113,12 @@ pub struct CouncilStageElection {
 /// Candidate representation.
 #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
 #[derive(Encode, Decode, PartialEq, Eq, Debug, Default, Clone)]
-pub struct Candidate<AccountId, Balance, Hash> {
+pub struct Candidate<AccountId, Balance, Hash, VotePower> {
     staking_account_id: AccountId,
     reward_account_id: AccountId,
     cycle_id: u64,
     stake: Balance,
+    vote_power: VotePower,
     note_hash: Option<Hash>,
 }
 
@@ -134,9 +135,9 @@ pub struct CouncilMember<AccountId, MembershipId, Balance, BlockNumber> {
     unpaid_reward: Balance,
 }
 
-impl<AccountId, MembershipId, Balance, Hash, BlockNumber>
+impl<AccountId, MembershipId, Balance, Hash, VotePower, BlockNumber>
     From<(
-        Candidate<AccountId, Balance, Hash>,
+        Candidate<AccountId, Balance, Hash, VotePower>,
         MembershipId,
         BlockNumber,
         Balance,
@@ -144,7 +145,7 @@ impl<AccountId, MembershipId, Balance, Hash, BlockNumber>
 {
     fn from(
         from: (
-            Candidate<AccountId, Balance, Hash>,
+            Candidate<AccountId, Balance, Hash, VotePower>,
             MembershipId,
             BlockNumber,
             Balance,
@@ -182,8 +183,12 @@ pub type CouncilMemberOf<T> = CouncilMember<
     Balance<T>,
     <T as frame_system::Trait>::BlockNumber,
 >;
-pub type CandidateOf<T> =
-    Candidate<<T as frame_system::Trait>::AccountId, Balance<T>, <T as frame_system::Trait>::Hash>;
+pub type CandidateOf<T> = Candidate<
+    <T as frame_system::Trait>::AccountId,
+    Balance<T>,
+    <T as frame_system::Trait>::Hash,
+    VotePowerOf<T>,
+>;
 pub type CouncilStageUpdateOf<T> = CouncilStageUpdate<<T as frame_system::Trait>::BlockNumber>;
 
 /////////////////// Trait, Storage, Errors, and Events /////////////////////////
@@ -253,6 +258,12 @@ pub trait ReferendumConnection<T: Trait> {
 
     /// Checks that user is indeed candidating. This function MUST be called in runtime's implementation of referendum's `is_valid_option_id()`.
     fn is_valid_candidate_id(membership_id: &T::MembershipId) -> bool;
+
+    /// Return current voting power for a selected candidate.
+    fn get_option_power(membership_id: &T::MembershipId) -> VotePowerOf<T>;
+
+    /// Recieve vote (power) for a selected candidate.
+    fn increase_option_power(membership_id: &T::MembershipId, amount: &VotePowerOf<T>);
 }
 
 decl_storage! {
@@ -264,7 +275,7 @@ decl_storage! {
         pub CouncilMembers get(fn council_members) config(): Vec<CouncilMemberOf<T>>;
 
         /// Map of all candidates that ever candidated and haven't unstake yet.
-        pub Candidates get(fn candidates) config(): map hasher(blake2_128_concat) T::MembershipId => Candidate<T::AccountId, Balance::<T>, T::Hash>;
+        pub Candidates get(fn candidates) config(): map hasher(blake2_128_concat) T::MembershipId => Candidate<T::AccountId, Balance::<T>, T::Hash, VotePowerOf::<T>>;
 
         /// Index of the current candidacy period. It is incremented everytime announcement period starts.
         pub AnnouncementPeriodNr get(fn announcement_period_nr) config(): u64;
@@ -756,6 +767,7 @@ impl<T: Trait> Module<T> {
             reward_account_id,
             cycle_id: AnnouncementPeriodNr::get(),
             stake,
+            vote_power: 0.into(),
             note_hash: None,
         }
     }
@@ -823,6 +835,26 @@ impl<T: Trait> ReferendumConnection<T> for Module<T> {
 
         candidate.cycle_id == AnnouncementPeriodNr::get()
     }
+
+    /// Return current voting power for a selected candidate.
+    fn get_option_power(membership_id: &T::MembershipId) -> VotePowerOf<T> {
+        if !Candidates::<T>::contains_key(membership_id) {
+            return 0.into();
+        }
+
+        let candidate = Candidates::<T>::get(membership_id);
+
+        candidate.vote_power
+    }
+
+    /// Recieve vote (power) for a selected candidate.
+    fn increase_option_power(membership_id: &T::MembershipId, amount: &VotePowerOf<T>) {
+        if !Candidates::<T>::contains_key(membership_id) {
+            return;
+        }
+
+        Candidates::<T>::mutate(membership_id, |candidate| candidate.vote_power += *amount);
+    }
 }
 
 /////////////////// Calculations ///////////////////////////////////////////////

+ 12 - 23
runtime-modules/council/src/mock.rs

@@ -158,9 +158,6 @@ pub type ReferendumInstance = referendum::Instance0;
 thread_local! {
     pub static IS_UNSTAKE_ENABLED: RefCell<(bool, )> = RefCell::new((true, )); // global switch for stake locking features; use it to simulate lock fails
     pub static IS_OPTION_ID_VALID: RefCell<(bool, )> = RefCell::new((true, )); // global switch used to test is_valid_option_id()
-
-    pub static INTERMEDIATE_RESULTS: RefCell<BTreeMap<u64, <<Runtime as Trait>::Referendum as ReferendumManager<<Runtime as frame_system::Trait>::Origin, <Runtime as frame_system::Trait>::AccountId, <Runtime as frame_system::Trait>::Hash>>::VotePower>> = RefCell::new(BTreeMap::<u64,
-        <<Runtime as Trait>::Referendum as ReferendumManager<<Runtime as frame_system::Trait>::Origin, <Runtime as frame_system::Trait>::AccountId, <Runtime as frame_system::Trait>::Hash>>::VotePower>::new());
 }
 
 parameter_types! {
@@ -227,8 +224,6 @@ impl referendum::Trait<ReferendumInstance> for Runtime {
         <Module<Runtime> as ReferendumConnection<Runtime>>::recieve_referendum_results(
             tmp_winners.as_slice(),
         );
-
-        INTERMEDIATE_RESULTS.with(|value| value.replace(BTreeMap::new()));
     }
 
     fn is_valid_option_id(option_index: &u64) -> bool {
@@ -240,18 +235,13 @@ impl referendum::Trait<ReferendumInstance> for Runtime {
     }
 
     fn get_option_power(option_id: &u64) -> Self::VotePower {
-        INTERMEDIATE_RESULTS.with(|value| match value.borrow().get(option_id) {
-            Some(vote_power) => *vote_power,
-            None => 0,
-        })
+        <Module<Runtime> as ReferendumConnection<Runtime>>::get_option_power(option_id)
     }
 
     fn increase_option_power(option_id: &u64, amount: &Self::VotePower) {
-        INTERMEDIATE_RESULTS.with(|value| {
-            let current = Self::get_option_power(option_id);
-
-            value.borrow_mut().insert(*option_id, amount + current);
-        });
+        <Module<Runtime> as ReferendumConnection<Runtime>>::increase_option_power(
+            option_id, amount,
+        );
     }
 }
 
@@ -471,6 +461,7 @@ where
             reward_account_id: account_id.into(),
             cycle_id: AnnouncementPeriodNr::get(),
             stake,
+            vote_power: 0.into(),
             note_hash: None,
         };
 
@@ -623,15 +614,13 @@ where
             }),
         );
 
-        INTERMEDIATE_RESULTS.with(|value| {
-            assert_eq!(
-                *value.borrow(),
-                intermediate_results
-                    .iter()
-                    .map(|(key, value)| (*key, value.clone().into()))
-                    .collect(),
-            )
-        });
+        // check intermediate results
+        for (key, value) in intermediate_results {
+            let membership_id: T::MembershipId = key.into();
+
+            assert!(Candidates::<T>::contains_key(membership_id));
+            assert_eq!(Candidates::<T>::get(membership_id).vote_power, value);
+        }
     }
 
     pub fn check_announcing_stake(membership_id: &T::MembershipId, amount: Balance<T>) {