Browse Source

runtime: fix todos

conectado 4 years ago
parent
commit
47137f56d6

+ 2 - 0
Cargo.lock

@@ -3991,7 +3991,9 @@ dependencies = [
  "frame-system",
  "pallet-balances",
  "pallet-common",
+ "pallet-council",
  "pallet-membership",
+ "pallet-referendum",
  "pallet-staking-handler",
  "pallet-timestamp",
  "parity-scale-codec",

+ 0 - 2
runtime-modules/council/src/lib.rs

@@ -1,5 +1,3 @@
-// TODO: adjust all extrinsic weights
-
 //! # Council module
 //! Council module for the the Joystream platform.
 //!

+ 8 - 1
runtime-modules/proposals/discussion/Cargo.toml

@@ -16,6 +16,9 @@ common = { package = 'pallet-common', default-features = false, path = '../../co
 frame-benchmarking = { package = 'frame-benchmarking', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca', optional = true}
 balances = { package = 'pallet-balances', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca', optional = true}
 membership = { package = 'pallet-membership', default-features = false, path = '../../membership', optional = true}
+council = { package = 'pallet-council', default-features = false, path = '../../council', optional = true}
+referendum = { package = 'pallet-referendum', default-features = false, path = '../../referendum', optional = true}
+
 
 [dev-dependencies]
 sp-io = { package = 'sp-io', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca'}
@@ -25,13 +28,17 @@ pallet-timestamp = { package = 'pallet-timestamp', default-features = false, git
 balances = { package = 'pallet-balances', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca'}
 membership = { package = 'pallet-membership', default-features = false, path = '../../membership'}
 staking-handler = { package = 'pallet-staking-handler', default-features = false, path = '../../staking-handler'}
+council = { package = 'pallet-council', default-features = false, path = '../../council'}
+referendum = { package = 'pallet-referendum', default-features = false, path = '../../referendum'}
 
 [features]
 default = ['std']
 runtime-benchmarks = [
     'frame-benchmarking',
     'balances',
-	'membership'
+	'membership',
+    'referendum',
+    'council',
 ]
 std = [
 	'serde',

+ 154 - 11
runtime-modules/proposals/discussion/src/benchmarking.rs

@@ -2,16 +2,21 @@
 use super::*;
 use crate::Module as ProposalsDiscussion;
 use balances::Module as Balances;
+use council::Module as Council;
 use frame_benchmarking::{account, benchmarks};
 use frame_support::sp_runtime::traits::Bounded;
-use frame_support::traits::Currency;
+use frame_support::traits::{Currency, OnFinalize, OnInitialize};
 use frame_system::EventRecord;
 use frame_system::Module as System;
 use frame_system::RawOrigin;
 use membership::Module as Membership;
+use referendum::Module as Referendum;
+use referendum::ReferendumManager;
 use sp_std::convert::TryInto;
 use sp_std::prelude::*;
 
+type ReferendumInstance = referendum::Instance1;
+
 const SEED: u32 = 0;
 
 fn get_byte(num: u32, byte_number: u8) -> u8 {
@@ -36,6 +41,27 @@ fn handle_from_id<T: membership::Trait>(id: u32) -> Vec<u8> {
     handle
 }
 
+fn run_to_block<T: Trait + council::Trait + referendum::Trait<ReferendumInstance>>(
+    n: T::BlockNumber,
+) {
+    while System::<T>::block_number() < n {
+        let mut current_block_number = System::<T>::block_number();
+
+        System::<T>::on_finalize(current_block_number);
+        ProposalsDiscussion::<T>::on_finalize(current_block_number);
+        Council::<T>::on_finalize(current_block_number);
+        Referendum::<T, ReferendumInstance>::on_finalize(current_block_number);
+
+        current_block_number += 1.into();
+        System::<T>::set_block_number(current_block_number);
+
+        System::<T>::on_initialize(current_block_number);
+        ProposalsDiscussion::<T>::on_initialize(current_block_number);
+        Council::<T>::on_initialize(current_block_number);
+        Referendum::<T, ReferendumInstance>::on_initialize(current_block_number);
+    }
+}
+
 fn assert_last_event<T: Trait>(generic_event: <T as Trait>::Event) {
     let events = System::<T>::events();
     let system_event: <T as frame_system::Trait>::Event = generic_event.into();
@@ -71,13 +97,129 @@ fn member_account<T: common::Trait + balances::Trait + membership::Trait>(
 
     Membership::<T>::buy_membership(RawOrigin::Signed(account_id.clone()).into(), params).unwrap();
 
-    (account_id, T::MemberId::from(id.try_into().unwrap()))
+    let member_id = T::MemberId::from(id.try_into().unwrap());
+
+    Membership::<T>::add_staking_account_candidate(
+        RawOrigin::Signed(account_id.clone()).into(),
+        member_id,
+    )
+    .unwrap();
+
+    Membership::<T>::confirm_staking_account(
+        RawOrigin::Signed(account_id.clone()).into(),
+        member_id,
+        account_id.clone(),
+    )
+    .unwrap();
+
+    assert_eq!(
+        Membership::<T>::staking_account_id_member_status(account_id.clone()).member_id,
+        member_id
+    );
+    assert_eq!(
+        Membership::<T>::staking_account_id_member_status(account_id.clone()).confirmed,
+        true
+    );
+
+    (account_id, member_id)
+}
+
+fn elect_council<
+    T: Trait + membership::Trait + council::Trait + referendum::Trait<ReferendumInstance>,
+>(
+    start_id: u32,
+) -> (Vec<(T::AccountId, T::MemberId)>, u32) {
+    let council_size = <T as council::Trait>::CouncilSize::get();
+    let number_of_extra_candidates = <T as council::Trait>::MinNumberOfExtraCandidates::get();
+
+    let councilor_stake = <T as council::Trait>::MinCandidateStake::get();
+
+    let mut voters = Vec::new();
+    let mut candidates = Vec::new();
+    let last_id = start_id as usize + (council_size + number_of_extra_candidates) as usize;
+
+    for i in start_id as usize..last_id {
+        let (account_id, member_id) = member_account::<T>("councilor", i.try_into().unwrap());
+        Council::<T>::announce_candidacy(
+            RawOrigin::Signed(account_id.clone()).into(),
+            member_id,
+            account_id.clone(),
+            account_id.clone(),
+            councilor_stake,
+        )
+        .unwrap();
+
+        candidates.push((account_id, member_id));
+    }
+
+    for i in last_id..last_id + council_size as usize {
+        voters.push(member_account::<T>("voter", i.try_into().unwrap()));
+    }
+
+    let current_block = System::<T>::block_number();
+    run_to_block::<T>(current_block + <T as council::Trait>::AnnouncingPeriodDuration::get());
+
+    let voter_stake = <T as referendum::Trait<ReferendumInstance>>::MinimumStake::get();
+    let mut council = Vec::new();
+    for i in 0..council_size as usize {
+        council.push(candidates[i].clone());
+        let commitment = Referendum::<T, ReferendumInstance>::calculate_commitment(
+            &voters[i].0,
+            &[0u8],
+            &0,
+            &candidates[i].1,
+        );
+        Referendum::<T, ReferendumInstance>::vote(
+            RawOrigin::Signed(voters[i].0.clone()).into(),
+            commitment,
+            voter_stake,
+        )
+        .unwrap();
+    }
+
+    let current_block = System::<T>::block_number();
+    run_to_block::<T>(
+        current_block + <T as referendum::Trait<ReferendumInstance>>::VoteStageDuration::get(),
+    );
+
+    for i in 0..council_size as usize {
+        Referendum::<T, ReferendumInstance>::reveal_vote(
+            RawOrigin::Signed(voters[i].0.clone()).into(),
+            vec![0u8],
+            candidates[i].1.clone(),
+        )
+        .unwrap();
+    }
+
+    let current_block = System::<T>::block_number();
+    run_to_block::<T>(
+        current_block + <T as referendum::Trait<ReferendumInstance>>::RevealStageDuration::get(),
+    );
+
+    let council_members = Council::<T>::council_members();
+    assert_eq!(
+        council_members
+            .iter()
+            .map(|m| *m.member_id())
+            .collect::<Vec<_>>(),
+        council.iter().map(|c| c.1).collect::<Vec<_>>()
+    );
+
+    (
+        council,
+        (2 * (council_size + number_of_extra_candidates))
+            .try_into()
+            .unwrap(),
+    )
 }
 
 const MAX_BYTES: u32 = 16384;
 
 benchmarks! {
-    where_clause { where T: balances::Trait, T: membership::Trait }
+    where_clause {
+        where T: balances::Trait, T: membership::Trait, T: council::Trait,
+              T: referendum::Trait<ReferendumInstance>
+    }
     _ { }
 
     add_post {
@@ -99,6 +241,9 @@ benchmarks! {
             whitelisted_members.push(member_id);
         }
 
+        // Worst case scenario there is a council
+        elect_council::<T>(i+1);
+
         let thread_id = ProposalsDiscussion::<T>::create_thread(
             caller_member_id,
             ThreadMode::Closed(whitelisted_members)
@@ -130,6 +275,9 @@ benchmarks! {
         let (_, _) = member_account::<T>("caller_member", 0);
         let (account_id, caller_member_id) = member_account::<T>("caller_member", 1);
 
+        // Worst case scenario there is a council
+        elect_council::<T>(2);
+
         let thread_id = ProposalsDiscussion::<T>::create_thread(
             caller_member_id,
             ThreadMode::Open
@@ -154,14 +302,6 @@ benchmarks! {
         assert_last_event::<T>(RawEvent::PostUpdated(post_id, caller_member_id).into());
     }
 
-    // TODO: Review this after changes to the governance/council are merged:
-    // this extrinsic uses `T::CouncilOriginValidator::ensure_member_controller_account_origin`
-    // this is a hook to the runtime. Since the pallet implementation shouldn't have any
-    // information on the runtime this hooks should be constant.
-    // However, the implementation in the runtime is linear in the number of council members.
-    // But since the size of the council should be completely filled over time we could
-    // always use the worst case scenario, still this would require to create an artificial
-    // dependency with the `governance` pallet to correctly benchmark this.
     change_thread_mode {
         let i in 1 .. T::MaxWhiteListSize::get();
 
@@ -186,6 +326,9 @@ benchmarks! {
             whitelisted_members.push(member_id);
         }
 
+        // Worst case scenario there is a council
+        elect_council::<T>(i+1);
+
         let mode = ThreadMode::Closed(whitelisted_members);
     }: _ (RawOrigin::Signed(account_id), caller_member_id, thread_id, mode.clone())
     verify {

+ 194 - 11
runtime-modules/proposals/discussion/src/tests/mock.rs

@@ -4,6 +4,7 @@ pub use frame_system;
 
 use frame_support::traits::{LockIdentifier, OnFinalize, OnInitialize};
 use frame_support::{impl_outer_event, impl_outer_origin, parameter_types, weights::Weight};
+use frame_system::{EnsureOneOf, EnsureRoot, EnsureSigned};
 use sp_core::H256;
 use sp_runtime::{
     testing::Header,
@@ -16,7 +17,7 @@ use crate::MemberOriginValidator;
 use crate::WeightInfo;
 use frame_support::dispatch::DispatchError;
 
-use staking_handler::LockComparator;
+use staking_handler::{LockComparator, StakingManager};
 
 impl_outer_origin! {
     pub enum Origin for Test {}
@@ -46,12 +47,23 @@ mod membership_mod {
     pub use membership::Event;
 }
 
+mod referendum_mod {
+    pub use referendum::Event;
+    pub use referendum::Instance1;
+}
+
+mod council_mod {
+    pub use council::Event;
+}
+
 impl_outer_event! {
     pub enum TestEvent for Test {
         discussion<T>,
         balances<T>,
         membership_mod<T>,
         frame_system<T>,
+        referendum_mod Instance1 <T>,
+        council_mod<T>,
     }
 }
 
@@ -168,16 +180,8 @@ impl MemberOriginValidator<Origin, u64, u64> for () {
             return Ok(1);
         }
 
-        if actor_id == 1 {
-            return Ok(1);
-        }
-
-        if actor_id == 2 {
-            return Ok(2);
-        }
-
-        if actor_id == 11 {
-            return Ok(11);
+        if actor_id < 12 {
+            return Ok(actor_id);
         }
 
         if actor_id == 12 && frame_system::ensure_signed(origin).unwrap_or_default() == 12 {
@@ -192,6 +196,91 @@ impl MemberOriginValidator<Origin, u64, u64> for () {
     }
 }
 
+parameter_types! {
+    pub const MinNumberOfExtraCandidates: u64 = 1;
+    pub const AnnouncingPeriodDuration: u64 = 15;
+    pub const IdlePeriodDuration: u64 = 27;
+    pub const CouncilSize: u64 = 4;
+    pub const MinCandidateStake: u64 = 11000;
+    pub const CandidacyLockId: LockIdentifier = *b"council1";
+    pub const CouncilorLockId: LockIdentifier = *b"council2";
+    pub const ElectedMemberRewardPeriod: u64 = 10;
+    pub const BudgetRefillAmount: u64 = 1000;
+    // intentionally high number that prevents side-effecting tests other than  budget refill tests
+    pub const BudgetRefillPeriod: u64 = 1000;
+}
+
+type ReferendumInstance = referendum::Instance1;
+
+impl council::Trait for Test {
+    type Event = TestEvent;
+
+    type Referendum = referendum::Module<Test, ReferendumInstance>;
+
+    type MinNumberOfExtraCandidates = MinNumberOfExtraCandidates;
+    type CouncilSize = CouncilSize;
+    type AnnouncingPeriodDuration = AnnouncingPeriodDuration;
+    type IdlePeriodDuration = IdlePeriodDuration;
+    type MinCandidateStake = MinCandidateStake;
+
+    type CandidacyLock = StakingManager<Self, CandidacyLockId>;
+    type CouncilorLock = StakingManager<Self, CouncilorLockId>;
+
+    type ElectedMemberRewardPeriod = ElectedMemberRewardPeriod;
+
+    type BudgetRefillPeriod = BudgetRefillPeriod;
+
+    type StakingAccountValidator = membership::Module<Test>;
+    type WeightInfo = CouncilWeightInfo;
+
+    fn new_council_elected(_: &[council::CouncilMemberOf<Self>]) {}
+
+    type MemberOriginValidator = ();
+}
+
+pub struct CouncilWeightInfo;
+impl council::WeightInfo for CouncilWeightInfo {
+    fn try_process_budget() -> Weight {
+        0
+    }
+    fn try_progress_stage_idle() -> Weight {
+        0
+    }
+    fn try_progress_stage_announcing_start_election(_: u32) -> Weight {
+        0
+    }
+    fn try_progress_stage_announcing_restart() -> Weight {
+        0
+    }
+    fn announce_candidacy() -> Weight {
+        0
+    }
+    fn release_candidacy_stake() -> Weight {
+        0
+    }
+    fn set_candidacy_note(_: u32) -> Weight {
+        0
+    }
+    fn withdraw_candidacy() -> Weight {
+        0
+    }
+    fn set_budget() -> Weight {
+        0
+    }
+    fn plan_budget_refill() -> Weight {
+        0
+    }
+    fn set_budget_increment() -> Weight {
+        0
+    }
+    fn set_councilor_reward() -> Weight {
+        0
+    }
+    fn funding_request(_: u32) -> Weight {
+        0
+    }
+}
+
 pub struct CouncilMock;
 impl CouncilOriginValidator<Origin, u64, u64> for CouncilMock {
     fn ensure_member_consulate(origin: Origin, actor_id: u64) -> DispatchResult {
@@ -231,6 +320,100 @@ impl frame_system::Trait for Test {
     type SystemWeightInfo = ();
 }
 
+parameter_types! {
+    pub const VoteStageDuration: u64 = 19;
+    pub const RevealStageDuration: u64 = 23;
+    pub const MinimumVotingStake: u64 = 10000;
+    pub const MaxSaltLength: u64 = 32; // use some multiple of 8 for ez testing
+    pub const VotingLockId: LockIdentifier = *b"referend";
+    pub const MaxWinnerTargetCount: u64 = 10;
+}
+
+impl referendum::Trait<ReferendumInstance> for Test {
+    type Event = TestEvent;
+
+    type MaxSaltLength = MaxSaltLength;
+
+    type StakingHandler = staking_handler::StakingManager<Self, VotingLockId>;
+    type ManagerOrigin =
+        EnsureOneOf<Self::AccountId, EnsureSigned<Self::AccountId>, EnsureRoot<Self::AccountId>>;
+
+    type VotePower = u64;
+
+    type VoteStageDuration = VoteStageDuration;
+    type RevealStageDuration = RevealStageDuration;
+
+    type MinimumStake = MinimumVotingStake;
+
+    type WeightInfo = ReferendumWeightInfo;
+
+    type MaxWinnerTargetCount = MaxWinnerTargetCount;
+
+    fn calculate_vote_power(
+        _: &<Self as frame_system::Trait>::AccountId,
+        _: &Self::Balance,
+    ) -> Self::VotePower {
+        1
+    }
+
+    fn can_unlock_vote_stake(
+        _: &referendum::CastVote<Self::Hash, Self::Balance, Self::MemberId>,
+    ) -> bool {
+        true
+    }
+
+    fn process_results(winners: &[referendum::OptionResult<Self::MemberId, Self::VotePower>]) {
+        let tmp_winners: Vec<referendum::OptionResult<Self::MemberId, Self::VotePower>> = winners
+            .iter()
+            .map(|item| referendum::OptionResult {
+                option_id: item.option_id,
+                vote_power: item.vote_power.into(),
+            })
+            .collect();
+        <council::Module<Test> as council::ReferendumConnection<Test>>::recieve_referendum_results(
+            tmp_winners.as_slice(),
+        );
+    }
+
+    fn is_valid_option_id(_: &u64) -> bool {
+        true
+    }
+
+    fn get_option_power(_: &u64) -> Self::VotePower {
+        1
+    }
+
+    fn increase_option_power(_: &u64, _: &Self::VotePower) {}
+}
+
+pub struct ReferendumWeightInfo;
+impl referendum::WeightInfo for ReferendumWeightInfo {
+    fn on_initialize_revealing(_: u32) -> Weight {
+        0
+    }
+    fn on_initialize_voting() -> Weight {
+        0
+    }
+    fn vote() -> Weight {
+        0
+    }
+    fn reveal_vote_space_for_new_winner(_: u32) -> Weight {
+        0
+    }
+    fn reveal_vote_space_not_in_winners(_: u32) -> Weight {
+        0
+    }
+    fn reveal_vote_space_replace_last_winner(_: u32) -> Weight {
+        0
+    }
+    fn reveal_vote_already_existing(_: u32) -> Weight {
+        0
+    }
+    fn release_vote_stake() -> Weight {
+        0
+    }
+}
+
 impl pallet_timestamp::Trait for Test {
     type Moment = u64;
     type OnTimestampSet = ();

+ 0 - 2
runtime-modules/referendum/src/lib.rs

@@ -1,5 +1,3 @@
-// TODO: adjust all extrinsic weights
-
 //! # Referendum module
 //! General voting engine module for the the Joystream platform. Component of the council frame_system.
 //!

+ 3 - 8
runtime/src/lib.rs

@@ -122,7 +122,9 @@ parameter_types! {
 }
 const AVERAGE_ON_INITIALIZE_WEIGHT: Perbill = Perbill::from_percent(10);
 
-// TODO: adjust weight
+// TODO: We need to adjust weight of this pallet
+// once we move to a newer version of substrate where parameters
+// are not discarded. See the comment in 'scripts/generate-weights.sh'
 impl frame_system::Trait for Runtime {
     type BaseCallFilter = ();
     type Origin = Origin;
@@ -844,13 +846,6 @@ impl pallet_constitution::Trait for Runtime {
     type WeightInfo = weights::pallet_constitution::WeightInfo;
 }
 
-parameter_types! {
-    pub const TombstoneDeposit: Balance = 1; // TODO: adjust fee
-    pub const RentByteFee: Balance = 1; // TODO: adjust fee
-    pub const RentDepositOffset: Balance = 0; // no rent deposit
-    pub const SurchargeReward: Balance = 0; // no reward
-}
-
 /// Forum identifier for category
 pub type CategoryId = u64;