Selaa lähdekoodia

referendum - formatting/clippy

ondratra 4 vuotta sitten
vanhempi
commit
3318a01024

+ 19 - 15
runtime-modules/referendum/src/lib.rs

@@ -5,22 +5,22 @@
 // No default instance is provided.
 
 /////////////////// Configuration //////////////////////////////////////////////
+#![allow(clippy::type_complexity)]
 #![cfg_attr(not(feature = "std"), no_std)]
 
 // used dependencies
 use codec::{Codec, Decode, Encode};
-use sp_runtime::traits::{MaybeSerialize, Member};
+use frame_support::{
+    decl_error, decl_event, decl_module, decl_storage, error::BadOrigin, traits::Get, Parameter,
+    StorageValue,
+};
 use sp_arithmetic::traits::{BaseArithmetic, One};
-use frame_support::{decl_error, decl_event, decl_module, decl_storage, traits::Get, Parameter, StorageValue, error::BadOrigin};
+use sp_runtime::traits::{MaybeSerialize, Member};
 use std::marker::PhantomData;
 use system::ensure_signed;
 
 use std::collections::HashSet;
 
-// conditioned dependencies
-//#[cfg(feature = "std")]
-//use serde_derive::{Deserialize, Serialize};
-
 // declared modules
 mod mock;
 mod tests;
@@ -365,12 +365,13 @@ struct Mutations<T: Trait<I>, I: Instance> {
 }
 
 impl<T: Trait<I>, I: Instance> Mutations<T, I> {
-    fn start_voting_period(options: &Vec<T::ReferendumOption>, winning_target_count: &u64) -> () {
+    fn start_voting_period(options: &[T::ReferendumOption], winning_target_count: &u64) {
         // change referendum state
         Stage::<T, I>::put((ReferendumStage::Voting, <system::Module<T>>::block_number()));
 
         // store new options
-        ReferendumOptions::<T, I>::put(options.clone());
+        //ReferendumOptions::<T, I>::put(options.clone());
+        ReferendumOptions::<T, I>::put(options);
 
         // store winning target
         WinningTargetCount::<I>::put(winning_target_count);
@@ -409,12 +410,12 @@ impl<T: Trait<I>, I: Instance> Mutations<T, I> {
                         continue;
                     }
 
-                    winning_order.push((option.clone(), vote_sum));
+                    winning_order.push((*option, vote_sum));
                 }
             }
 
             // no votes revealed?
-            if winning_order.len() == 0 {
+            if winning_order.is_empty() {
                 return ReferendumResult::NoVotesRevealed;
             }
 
@@ -561,7 +562,7 @@ impl<T: Trait<I>, I: Instance> EnsureChecks<T, I> {
         }
 
         // ensure some options were given
-        if options.len() == 0 {
+        if options.is_empty() {
             return Err(Error::NoReferendumOptions);
         }
 
@@ -624,7 +625,10 @@ impl<T: Trait<I>, I: Instance> EnsureChecks<T, I> {
         Ok(())
     }
 
-    fn can_vote(origin: T::Origin, stake: &T::CurrencyBalance) -> Result<T::AccountId, Error<T, I>> {
+    fn can_vote(
+        origin: T::Origin,
+        stake: &T::CurrencyBalance,
+    ) -> Result<T::AccountId, Error<T, I>> {
         // ensure superuser requested action
         let account_id = Self::ensure_regular_user(origin)?;
 
@@ -662,17 +666,17 @@ impl<T: Trait<I>, I: Instance> EnsureChecks<T, I> {
 
     fn can_reveal_vote(
         origin: T::Origin,
-        salt: &Vec<u8>,
+        salt: &[u8],
         vote_option: &T::ReferendumOption,
     ) -> Result<(T::AccountId, SealedVote<T::Hash, T::CurrencyBalance>), Error<T, I>> {
         fn calculate_commitment<T: Trait<I>, I: Instance>(
             account_id: &T::AccountId,
-            salt: &Vec<u8>,
+            salt: &[u8],
             vote_option: &T::ReferendumOption,
         ) -> T::Hash {
             let mut payload = account_id.encode();
             let mut mut_option = vote_option.clone().into().to_be_bytes().to_vec();
-            let mut salt_tmp = salt.clone();
+            let mut salt_tmp = salt.to_vec();
 
             payload.append(&mut salt_tmp);
             payload.append(&mut mut_option);

+ 47 - 23
runtime-modules/referendum/src/mock.rs

@@ -2,24 +2,25 @@
 
 /////////////////// Configuration //////////////////////////////////////////////
 use crate::{
-    Error, RawEvent, Instance, Module, ReferendumOptions, ReferendumResult, ReferendumStage,
-    RevealedVotes, SealedVote, Stage, WinningTargetCount, Trait, Votes,
+    Error, Instance, Module, RawEvent, ReferendumOptions, ReferendumResult, ReferendumStage,
+    RevealedVotes, SealedVote, Stage, Trait, Votes, WinningTargetCount,
 };
 
 use codec::Encode;
-use sp_core::H256;
 use rand::Rng;
+use sp_core::H256;
 //use sp_io;
+use frame_support::{
+    impl_outer_event, impl_outer_origin, parameter_types, StorageMap, StoragePrefixedMap,
+    StorageValue,
+};
 use sp_runtime::{
     testing::Header,
     traits::{BlakeTwo256, IdentityLookup},
     Perbill,
 };
-use frame_support::{
-    impl_outer_event, impl_outer_origin, parameter_types, StorageMap, StoragePrefixedMap, StorageValue,
-};
-use std::marker::PhantomData;
 use std::cell::RefCell;
+use std::marker::PhantomData;
 use system::RawOrigin;
 
 use crate::GenesisConfig;
@@ -134,7 +135,9 @@ impl Runtime {
         lock_enabled: bool,
         free_enabled: bool,
     ) -> () {
-        IS_LOCKING_ENABLED.with(|value| {*value.borrow_mut() = (ensure_check_enabled, lock_enabled, free_enabled);});
+        IS_LOCKING_ENABLED.with(|value| {
+            *value.borrow_mut() = (ensure_check_enabled, lock_enabled, free_enabled);
+        });
     }
 }
 
@@ -228,7 +231,6 @@ pub fn build_test_externalities(
 
     config.assimilate_storage(&mut t).unwrap();
 
-
     // reset the static lock feature state
     Runtime::feature_stack_lock(true, true, true);
 
@@ -295,7 +297,6 @@ pub struct InstanceMocks<T: Trait<I>, I: Instance> {
 }
 
 impl InstanceMocks<Runtime, Instance0> {
-
     pub fn start_referendum(
         origin: OriginType<<Runtime as system::Trait>::AccountId>,
         options: Vec<<Runtime as Trait<Instance0>>::ReferendumOption>,
@@ -316,16 +317,19 @@ impl InstanceMocks<Runtime, Instance0> {
             return;
         }
 
-        assert_eq!(Stage::<Runtime, Instance0>::get().0, ReferendumStage::Voting,);
+        assert_eq!(
+            Stage::<Runtime, Instance0>::get().0,
+            ReferendumStage::Voting,
+        );
 
-        assert_eq!(ReferendumOptions::<Runtime, Instance0>::get(), Some(options.clone()),);
+        assert_eq!(
+            ReferendumOptions::<Runtime, Instance0>::get(),
+            Some(options.clone()),
+        );
 
         assert_eq!(
             system::Module::<Runtime>::events().last().unwrap().event,
-            TestEvent::from(RawEvent::ReferendumStarted(
-                options,
-                winning_target_count,
-            ))
+            TestEvent::from(RawEvent::ReferendumStarted(options, winning_target_count,))
         );
     }
 
@@ -335,7 +339,10 @@ impl InstanceMocks<Runtime, Instance0> {
     ) -> () {
         // check method returns expected result
         assert_eq!(
-            Module::<Runtime, Instance0>::finish_voting_start_revealing(InstanceMockUtils::<Runtime, Instance0>::mock_origin(
+            Module::<Runtime, Instance0>::finish_voting_start_revealing(InstanceMockUtils::<
+                Runtime,
+                Instance0,
+            >::mock_origin(
                 origin
             ),),
             expected_result,
@@ -345,7 +352,10 @@ impl InstanceMocks<Runtime, Instance0> {
             return;
         }
 
-        assert_eq!(Stage::<Runtime, Instance0>::get().0, ReferendumStage::Revealing,);
+        assert_eq!(
+            Stage::<Runtime, Instance0>::get().0,
+            ReferendumStage::Revealing,
+        );
 
         // check event was emitted
         assert_eq!(
@@ -357,11 +367,21 @@ impl InstanceMocks<Runtime, Instance0> {
     pub fn finish_revealing_period(
         origin: OriginType<<Runtime as system::Trait>::AccountId>,
         expected_result: Result<(), Error<Runtime, Instance0>>,
-        expected_referendum_result: Option<ReferendumResult<<Runtime as Trait<Instance0>>::ReferendumOption, <Runtime as Trait<Instance0>>::VotePower>>,
+        expected_referendum_result: Option<
+            ReferendumResult<
+                <Runtime as Trait<Instance0>>::ReferendumOption,
+                <Runtime as Trait<Instance0>>::VotePower,
+            >,
+        >,
     ) -> () {
         // check method returns expected result
         assert_eq!(
-            Module::<Runtime, Instance0>::finish_revealing_period(InstanceMockUtils::<Runtime, Instance0>::mock_origin(origin),),
+            Module::<Runtime, Instance0>::finish_revealing_period(InstanceMockUtils::<
+                Runtime,
+                Instance0,
+            >::mock_origin(
+                origin
+            ),),
             expected_result,
         );
 
@@ -372,14 +392,18 @@ impl InstanceMocks<Runtime, Instance0> {
         assert_eq!(Stage::<Runtime, Instance0>::get().0, ReferendumStage::Void,);
         assert_eq!(ReferendumOptions::<Runtime, Instance0>::get(), None,);
         assert_eq!(Votes::<Runtime, Instance0>::iter_values().count(), 0,);
-        assert_eq!(RevealedVotes::<Runtime, Instance0>::iter_values().count(), 0,);
+        assert_eq!(
+            RevealedVotes::<Runtime, Instance0>::iter_values().count(),
+            0,
+        );
         assert_eq!(WinningTargetCount::<Instance0>::get(), 0,);
 
-
         // check event was emitted
         assert_eq!(
             system::Module::<Runtime>::events().last().unwrap().event,
-            TestEvent::event_mod_Instance0(RawEvent::ReferendumFinished(expected_referendum_result.unwrap()))
+            TestEvent::event_mod_Instance0(RawEvent::ReferendumFinished(
+                expected_referendum_result.unwrap()
+            ))
         );
     }
 

+ 93 - 17
runtime-modules/referendum/src/tests.rs

@@ -558,7 +558,10 @@ fn finish_revealing_period() {
         Mocks::finish_revealing_period(
             origin.clone(),
             Ok(()),
-            Some(ReferendumResult::Winners(vec![(option_to_vote_for, 1 * stake)])),
+            Some(ReferendumResult::Winners(vec![(
+                option_to_vote_for,
+                1 * stake,
+            )])),
         );
     });
 }
@@ -666,7 +669,10 @@ fn finish_revealing_period_vote_power() {
         Mocks::finish_revealing_period(
             origin.clone(),
             Ok(()),
-            Some(ReferendumResult::Winners(vec![(option_to_vote_for2, 1 * stake_smaller * POWER_VOTE_STRENGTH)])),
+            Some(ReferendumResult::Winners(vec![(
+                option_to_vote_for2,
+                1 * stake_smaller * POWER_VOTE_STRENGTH,
+            )])),
         );
     });
 }
@@ -727,15 +733,51 @@ fn winners_multiple_winners() {
             winning_target_count,
             Ok(()),
         );
-        Mocks::vote(origin_voter1.clone(), account_id1, commitment1, stake, Ok(()));
-        Mocks::vote(origin_voter2.clone(), account_id2, commitment2, stake, Ok(()));
-        Mocks::vote(origin_voter3.clone(), account_id3, commitment3, stake, Ok(()));
+        Mocks::vote(
+            origin_voter1.clone(),
+            account_id1,
+            commitment1,
+            stake,
+            Ok(()),
+        );
+        Mocks::vote(
+            origin_voter2.clone(),
+            account_id2,
+            commitment2,
+            stake,
+            Ok(()),
+        );
+        Mocks::vote(
+            origin_voter3.clone(),
+            account_id3,
+            commitment3,
+            stake,
+            Ok(()),
+        );
         MockUtils::increase_block_number(voting_stage_duration + 1);
 
         Mocks::finish_voting(origin.clone(), Ok(()));
-        Mocks::reveal_vote(origin_voter1.clone(), account_id1, salt1, option_to_vote_for1, Ok(()));
-        Mocks::reveal_vote(origin_voter2.clone(), account_id2, salt2, option_to_vote_for1, Ok(()));
-        Mocks::reveal_vote(origin_voter3.clone(), account_id3, salt3, option_to_vote_for2, Ok(()));
+        Mocks::reveal_vote(
+            origin_voter1.clone(),
+            account_id1,
+            salt1,
+            option_to_vote_for1,
+            Ok(()),
+        );
+        Mocks::reveal_vote(
+            origin_voter2.clone(),
+            account_id2,
+            salt2,
+            option_to_vote_for1,
+            Ok(()),
+        );
+        Mocks::reveal_vote(
+            origin_voter3.clone(),
+            account_id3,
+            salt3,
+            option_to_vote_for2,
+            Ok(()),
+        );
         MockUtils::increase_block_number(reveal_stage_duration + 1);
 
         let expected_winners = vec![
@@ -780,13 +822,37 @@ fn winners_multiple_winners_extra() {
             winning_target_count,
             Ok(()),
         );
-        Mocks::vote(origin_voter1.clone(), account_id1, commitment1, stake, Ok(()));
-        Mocks::vote(origin_voter2.clone(), account_id2, commitment2, stake, Ok(()));
+        Mocks::vote(
+            origin_voter1.clone(),
+            account_id1,
+            commitment1,
+            stake,
+            Ok(()),
+        );
+        Mocks::vote(
+            origin_voter2.clone(),
+            account_id2,
+            commitment2,
+            stake,
+            Ok(()),
+        );
         MockUtils::increase_block_number(voting_stage_duration + 1);
 
         Mocks::finish_voting(origin.clone(), Ok(()));
-        Mocks::reveal_vote(origin_voter1.clone(), account_id1, salt1, option_to_vote_for1, Ok(()));
-        Mocks::reveal_vote(origin_voter2.clone(), account_id2, salt2, option_to_vote_for2, Ok(()));
+        Mocks::reveal_vote(
+            origin_voter1.clone(),
+            account_id1,
+            salt1,
+            option_to_vote_for1,
+            Ok(()),
+        );
+        Mocks::reveal_vote(
+            origin_voter2.clone(),
+            account_id2,
+            salt2,
+            option_to_vote_for2,
+            Ok(()),
+        );
         MockUtils::increase_block_number(reveal_stage_duration + 1);
 
         let expected_winners = vec![
@@ -826,16 +892,26 @@ fn winners_multiple_not_enough() {
             winning_target_count,
             Ok(()),
         );
-        Mocks::vote(origin_voter1.clone(), account_id1, commitment1, stake, Ok(()));
+        Mocks::vote(
+            origin_voter1.clone(),
+            account_id1,
+            commitment1,
+            stake,
+            Ok(()),
+        );
         MockUtils::increase_block_number(voting_stage_duration + 1);
 
         Mocks::finish_voting(origin.clone(), Ok(()));
-        Mocks::reveal_vote(origin_voter1.clone(), account_id1, salt1, option_to_vote_for1, Ok(()));
+        Mocks::reveal_vote(
+            origin_voter1.clone(),
+            account_id1,
+            salt1,
+            option_to_vote_for1,
+            Ok(()),
+        );
         MockUtils::increase_block_number(reveal_stage_duration + 1);
 
-        let expected_winners = vec![
-            (option_to_vote_for1, 1 * stake),
-        ];
+        let expected_winners = vec![(option_to_vote_for1, 1 * stake)];
 
         Mocks::finish_revealing_period(
             origin.clone(),