Browse Source

benchmark: proposals/codex: add benchmarks and weights

conectado 4 years ago
parent
commit
be15094bdf

+ 1 - 0
Cargo.lock

@@ -3955,6 +3955,7 @@ dependencies = [
 name = "pallet-proposals-codex"
 version = "4.0.0"
 dependencies = [
+ "frame-benchmarking",
  "frame-support",
  "frame-system",
  "pallet-balances",

+ 4 - 0
runtime-modules/proposals/codex/Cargo.toml

@@ -23,6 +23,9 @@ constitution = { package = 'pallet-constitution', default-features = false, path
 membership = { package = 'pallet-membership', default-features = false, path = '../../membership'}
 council = { package = 'pallet-council', default-features = false, path = '../../council'}
 
+# Benchmarking dependencies
+frame-benchmarking = { package = 'frame-benchmarking', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca', optional = true}
+
 [dev-dependencies]
 sp-io = { package = 'sp-io', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca'}
 sp-core = { package = 'sp-core', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca'}
@@ -34,6 +37,7 @@ referendum = { package = 'pallet-referendum', default-features = false, path = '
 
 [features]
 default = ['std']
+runtime-benchmarks = ['frame-benchmarking']
 std = [
     'serde',
     'codec/std',

+ 560 - 0
runtime-modules/proposals/codex/src/benchmarking.rs

@@ -0,0 +1,560 @@
+#![cfg(feature = "runtime-benchmarks")]
+use super::*;
+use crate::Module as Codex;
+use balances::Module as Balances;
+use frame_benchmarking::{account, benchmarks};
+use frame_support::sp_runtime::traits::Bounded;
+use frame_support::traits::Currency;
+use frame_system::EventRecord;
+use frame_system::Module as System;
+use frame_system::RawOrigin;
+use membership::Module as Membership;
+use proposals_discussion::Module as Discussion;
+use proposals_engine::Module as Engine;
+use sp_runtime::traits::One;
+use sp_std::convert::TryInto;
+use sp_std::prelude::*;
+
+const SEED: u32 = 0;
+const MAX_BYTES: u32 = 16384;
+
+// Note: We use proposals_engine::Trait::Event here because crate::Trait
+// doesn't implement Event and we only use this function to assert events
+// from the proposals_engine pallet
+fn assert_last_event<T: Trait>(generic_event: <T as proposals_engine::Trait>::Event) {
+    let events = System::<T>::events();
+    let system_event: <T as frame_system::Trait>::Event = generic_event.into();
+    assert!(
+        events.len() > 0,
+        "If you are checking for last event there must be at least 1 event"
+    );
+    let EventRecord { event, .. } = &events[events.len() - 1];
+    assert_eq!(event, &system_event);
+}
+
+fn get_byte(num: u32, byte_number: u8) -> u8 {
+    ((num & (0xff << (8 * byte_number))) >> 8 * byte_number) as u8
+}
+
+// Method to generate a distintic valid handle
+// for a membership. For each index.
+fn handle_from_id(id: u32) -> Vec<u8> {
+    let mut handle = vec![];
+
+    for i in 0..4 {
+        handle.push(get_byte(id, i));
+    }
+
+    handle
+}
+
+fn member_funded_account<T: Trait + membership::Trait>(
+    name: &'static str,
+    id: u32,
+) -> (T::AccountId, T::MemberId) {
+    let account_id = account::<T::AccountId>(name, id, SEED);
+    let handle = handle_from_id(id);
+
+    // Give balance for buying membership
+    let _ = Balances::<T>::make_free_balance_be(&account_id, T::Balance::max_value());
+
+    let params = membership::BuyMembershipParameters {
+        root_account: account_id.clone(),
+        controller_account: account_id.clone(),
+        name: None,
+        handle: Some(handle),
+        avatar_uri: None,
+        about: None,
+        referrer_id: None,
+    };
+
+    Membership::<T>::buy_membership(RawOrigin::Signed(account_id.clone()).into(), params).unwrap();
+
+    let _ = Balances::<T>::make_free_balance_be(&account_id, T::Balance::max_value());
+
+    (account_id, T::MemberId::from(id.try_into().unwrap()))
+}
+
+fn create_proposal_parameters<T: Trait + membership::Trait>(
+    title_length: u32,
+    description_length: u32,
+) -> (T::AccountId, T::MemberId, GeneralProposalParameters<T>) {
+    let (account_id, member_id) = member_funded_account::<T>("account", 0);
+
+    let general_proposal_paramters = GeneralProposalParameters::<T> {
+        member_id,
+        title: vec![0u8; title_length.try_into().unwrap()],
+        description: vec![0u8; description_length.try_into().unwrap()],
+        staking_account_id: Some(account_id.clone()),
+        exact_execution_block: None,
+    };
+
+    (account_id, member_id, general_proposal_paramters)
+}
+
+fn create_proposal_verify<T: Trait>(
+    account_id: T::AccountId,
+    member_id: T::MemberId,
+    proposal_details: ProposalDetailsOf<T>,
+) {
+    assert_eq!(Discussion::<T>::thread_count(), 1, "No threads created");
+    let thread_id = T::ThreadId::from(1);
+    assert!(
+        proposals_discussion::ThreadById::<T>::contains_key(thread_id),
+        "No thread created"
+    );
+
+    let proposal_id = T::ProposalId::from(1);
+    assert!(
+        proposals_engine::Proposals::<T>::contains_key(proposal_id),
+        "Proposal not inserted in engine"
+    );
+
+    assert_eq!(
+        Engine::<T>::proposals(proposal_id),
+        proposals_engine::Proposal {
+            activated_at: System::<T>::block_number(),
+            parameters: Codex::<T>::get_proposal_parameters(&proposal_details),
+            proposer_id: member_id,
+            status: proposals_engine::ProposalStatus::Active,
+            voting_results: proposals_engine::VotingResults::default(),
+            exact_execution_block: None,
+            nr_of_council_confirmations: 0,
+            staking_account_id: Some(account_id)
+        },
+        "Proposal not correctly created"
+    );
+
+    assert!(
+        proposals_engine::DispatchableCallCode::<T>::contains_key(proposal_id),
+        "Dispatchable code not stored"
+    );
+
+    assert_eq!(
+        Engine::<T>::proposal_codes(proposal_id),
+        T::ProposalEncoder::encode_proposal(proposal_details.clone()),
+        "Stored proposal code doesn't match"
+    );
+
+    assert_eq!(
+        Engine::<T>::proposal_count(),
+        1,
+        "Proposal count not updated"
+    );
+    assert_eq!(
+        Engine::<T>::active_proposal_count(),
+        1,
+        "Active proposal count not updated"
+    );
+    assert_last_event::<T>(
+        proposals_engine::RawEvent::ProposalCreated(member_id, proposal_id).into(),
+    );
+
+    assert!(
+        ThreadIdByProposalId::<T>::contains_key(proposal_id),
+        "Proposal thread not stored"
+    );
+    assert_eq!(
+        Codex::<T>::thread_id_by_proposal_id(proposal_id),
+        thread_id,
+        "Proposal and thread ID doesn't match"
+    );
+
+    assert!(
+        ProposalDetailsByProposalId::<T>::contains_key(proposal_id),
+        "Proposal details not stored"
+    );
+
+    assert_eq!(
+        ProposalDetailsByProposalId::<T>::get(proposal_id),
+        proposal_details,
+        "Proposal details doesn't match"
+    );
+}
+
+benchmarks! {
+    where_clause { where T: membership::Trait }
+    _ {
+        let t in 1 .. T::TitleMaxLength::get() => ();
+        let d in 1 .. T::DescriptionMaxLength::get() => ();
+    }
+
+    // Note: No verify since there is no side effect to test
+    execute_signal_proposal {
+        let i in 1 .. MAX_BYTES;
+    }: _(RawOrigin::Root, vec![0u8; i.try_into().unwrap()])
+
+    create_proposal_signal {
+        let i in 1 .. MAX_BYTES;
+        let t in ...;
+        let d in ...;
+
+        let (account_id, member_id, general_proposal_paramters) = create_proposal_parameters::<T>(t, d);
+
+        let proposal_details = ProposalDetails::Signal(vec![0u8; i.try_into().unwrap()]);
+    }: create_proposal(RawOrigin::Signed(account_id.clone()), general_proposal_paramters, proposal_details.clone())
+    verify {
+        create_proposal_verify::<T>(account_id, member_id, proposal_details);
+    }
+
+    create_proposal_runtime_upgrade {
+        let i in 1 .. MAX_BYTES;
+        let t in ...;
+        let d in ...;
+
+        let (account_id, member_id, general_proposal_paramters) = create_proposal_parameters::<T>(t, d);
+
+        let proposal_details = ProposalDetails::RuntimeUpgrade(vec![0u8; i.try_into().unwrap()]);
+    }: create_proposal(RawOrigin::Signed(account_id.clone()), general_proposal_paramters, proposal_details.clone())
+    verify {
+        create_proposal_verify::<T>(account_id, member_id, proposal_details);
+    }
+
+    create_proposal_funding_request {
+        let t in ...;
+        let d in ...;
+
+        let (account_id, member_id, general_proposal_paramters) = create_proposal_parameters::<T>(t, d);
+
+        council::Module::<T>::set_budget(RawOrigin::Root.into(), council::Balance::<T>::max_value()).unwrap();
+
+        let proposal_details = ProposalDetails::FundingRequest(
+                crate::BalanceOf::<T>::one(),
+                account::<T::AccountId>("reciever", 1, SEED)
+            );
+    }: create_proposal(RawOrigin::Signed(account_id.clone()), general_proposal_paramters, proposal_details.clone())
+    verify {
+        create_proposal_verify::<T>(account_id, member_id, proposal_details);
+    }
+
+    create_proposal_set_max_validator_count {
+        let t in ...;
+        let d in ...;
+
+        let (account_id, member_id, general_proposal_paramters) = create_proposal_parameters::<T>(t, d);
+
+        let proposal_details = ProposalDetails::SetMaxValidatorCount(MAX_VALIDATOR_COUNT);
+    }: create_proposal(RawOrigin::Signed(account_id.clone()), general_proposal_paramters, proposal_details.clone())
+    verify {
+        create_proposal_verify::<T>(account_id, member_id, proposal_details);
+    }
+
+    create_proposal_create_working_group_lead_opening {
+        let i in 1 .. MAX_BYTES;
+        let t in ...;
+        let d in ...;
+
+        let (account_id, member_id, general_proposal_paramters) = create_proposal_parameters::<T>(t, d);
+
+        let proposal_details = ProposalDetails::CreateWorkingGroupLeadOpening(CreateOpeningParameters {
+            description: vec![0u8; i.try_into().unwrap()],
+            stake_policy: None,
+            reward_per_block: None,
+            group: WorkingGroup::Forum,
+        });
+    }: create_proposal(RawOrigin::Signed(account_id.clone()), general_proposal_paramters, proposal_details.clone())
+    verify {
+        create_proposal_verify::<T>(account_id, member_id, proposal_details);
+    }
+
+    create_proposal_fill_working_group_lead_opening {
+        let t in ...;
+        let d in ...;
+
+        let (account_id, member_id, general_proposal_paramters) = create_proposal_parameters::<T>(t, d);
+
+        let proposal_details = ProposalDetails::FillWorkingGroupLeadOpening(FillOpeningParameters {
+            opening_id: working_group::OpeningId::zero(),
+            application_id: working_group::ApplicationId::zero(),
+            working_group: WorkingGroup::Forum,
+        });
+    }: create_proposal(RawOrigin::Signed(account_id.clone()), general_proposal_paramters, proposal_details.clone())
+    verify {
+        create_proposal_verify::<T>(account_id, member_id, proposal_details);
+    }
+
+    create_proposal_update_working_group_budget {
+        let t in ...;
+        let d in ...;
+
+        let (account_id, member_id, general_proposal_paramters) = create_proposal_parameters::<T>(t, d);
+
+        let proposal_details = ProposalDetails::UpdateWorkingGroupBudget(
+            BalanceOf::<T>::one(),
+            WorkingGroup::Forum,
+            BalanceKind::Positive
+        );
+    }: create_proposal(RawOrigin::Signed(account_id.clone()), general_proposal_paramters, proposal_details.clone())
+    verify {
+        create_proposal_verify::<T>(account_id, member_id, proposal_details);
+    }
+
+    create_proposal_decrease_working_group_lead_stake {
+        let t in ...;
+        let d in ...;
+
+        let (account_id, member_id, general_proposal_paramters) = create_proposal_parameters::<T>(t, d);
+
+        let proposal_details = ProposalDetails::DecreaseWorkingGroupLeadStake(
+            working_group::WorkerId::<T>::zero(),
+            BalanceOf::<T>::one(),
+            WorkingGroup::Forum,
+        );
+    }: create_proposal(RawOrigin::Signed(account_id.clone()), general_proposal_paramters, proposal_details.clone())
+    verify {
+        create_proposal_verify::<T>(account_id, member_id, proposal_details);
+    }
+
+    create_proposal_slash_working_group_lead {
+        let t in ...;
+        let d in ...;
+
+        let (account_id, member_id, general_proposal_paramters) = create_proposal_parameters::<T>(t, d);
+
+        let proposal_details = ProposalDetails::SlashWorkingGroupLead(
+            working_group::WorkerId::<T>::zero(),
+            BalanceOf::<T>::one(),
+            WorkingGroup::Forum,
+        );
+    }: create_proposal(RawOrigin::Signed(account_id.clone()), general_proposal_paramters, proposal_details.clone())
+    verify {
+        create_proposal_verify::<T>(account_id, member_id, proposal_details);
+    }
+
+    create_proposal_set_working_group_lead_reward {
+        let t in ...;
+        let d in ...;
+
+        let (account_id, member_id, general_proposal_paramters) = create_proposal_parameters::<T>(t, d);
+
+        let proposal_details = ProposalDetails::SetWorkingGroupLeadReward(
+            working_group::WorkerId::<T>::zero(),
+            None,
+            WorkingGroup::Forum,
+        );
+    }: create_proposal(RawOrigin::Signed(account_id.clone()), general_proposal_paramters, proposal_details.clone())
+    verify {
+        create_proposal_verify::<T>(account_id, member_id, proposal_details);
+    }
+
+    create_proposal_terminate_working_group_lead {
+        let t in ...;
+        let d in ...;
+
+        let (account_id, member_id, general_proposal_paramters) = create_proposal_parameters::<T>(t, d);
+
+        let proposal_details = ProposalDetails::TerminateWorkingGroupLead(
+            TerminateRoleParameters {
+                worker_id: working_group::WorkerId::<T>::zero(),
+                slashing_amount: None,
+                group: WorkingGroup::Forum,
+            }
+        );
+    }: create_proposal(RawOrigin::Signed(account_id.clone()), general_proposal_paramters, proposal_details.clone())
+    verify {
+        create_proposal_verify::<T>(account_id, member_id, proposal_details);
+    }
+
+    create_proposal_amend_constitution {
+        let i in 1 .. MAX_BYTES;
+        let t in ...;
+        let d in ...;
+
+        let (account_id, member_id, general_proposal_paramters) = create_proposal_parameters::<T>(t, d);
+
+        let proposal_details = ProposalDetails::AmendConstitution(vec![0u8; i.try_into().unwrap()]);
+    }: create_proposal(RawOrigin::Signed(account_id.clone()), general_proposal_paramters, proposal_details.clone())
+    verify {
+        create_proposal_verify::<T>(account_id, member_id, proposal_details);
+    }
+
+    create_proposal_cancel_working_group_lead_opening {
+        let t in ...;
+        let d in ...;
+
+        let (account_id, member_id, general_proposal_paramters) = create_proposal_parameters::<T>(t, d);
+
+        let proposal_details = ProposalDetails::CancelWorkingGroupLeadOpening(
+            working_group::OpeningId::zero(),
+            WorkingGroup::Forum);
+    }: create_proposal(RawOrigin::Signed(account_id.clone()), general_proposal_paramters, proposal_details.clone())
+    verify {
+        create_proposal_verify::<T>(account_id, member_id, proposal_details);
+    }
+
+    create_proposal_set_membership_price {
+        let t in ...;
+        let d in ...;
+
+        let (account_id, member_id, general_proposal_paramters) = create_proposal_parameters::<T>(t, d);
+
+        let proposal_details = ProposalDetails::SetMembershipPrice(BalanceOf::<T>::one());
+    }: create_proposal(RawOrigin::Signed(account_id.clone()), general_proposal_paramters, proposal_details.clone())
+    verify {
+        create_proposal_verify::<T>(account_id, member_id, proposal_details);
+    }
+
+    create_proposal_set_council_budget_increment {
+        let t in ...;
+        let d in ...;
+
+        let (account_id, member_id, general_proposal_paramters) = create_proposal_parameters::<T>(t, d);
+
+        let proposal_details = ProposalDetails::SetCouncilBudgetIncrement(BalanceOf::<T>::one());
+    }: create_proposal(RawOrigin::Signed(account_id.clone()), general_proposal_paramters, proposal_details.clone())
+    verify {
+        create_proposal_verify::<T>(account_id, member_id, proposal_details);
+    }
+
+    create_proposal_set_councilor_reward {
+        let t in ...;
+        let d in ...;
+
+        let (account_id, member_id, general_proposal_paramters) = create_proposal_parameters::<T>(t, d);
+
+        let proposal_details = ProposalDetails::SetCouncilorReward(BalanceOf::<T>::one());
+    }: create_proposal(RawOrigin::Signed(account_id.clone()), general_proposal_paramters, proposal_details.clone())
+    verify {
+        create_proposal_verify::<T>(account_id, member_id, proposal_details);
+    }
+
+    create_proposal_set_initial_invitation_balance {
+        let t in ...;
+        let d in ...;
+
+        let (account_id, member_id, general_proposal_paramters) = create_proposal_parameters::<T>(t, d);
+
+        let proposal_details = ProposalDetails::SetInitialInvitationBalance(BalanceOf::<T>::one());
+    }: create_proposal(RawOrigin::Signed(account_id.clone()), general_proposal_paramters, proposal_details.clone())
+    verify {
+        create_proposal_verify::<T>(account_id, member_id, proposal_details);
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use crate::tests::{initial_test_ext, Test};
+    use frame_support::assert_ok;
+
+    #[test]
+    fn test_execute_signal_proposal() {
+        initial_test_ext().execute_with(|| {
+            assert_ok!(test_benchmark_execute_signal_proposal::<Test>());
+        });
+    }
+
+    #[test]
+    fn test_create_proposal_signal() {
+        initial_test_ext().execute_with(|| {
+            assert_ok!(test_benchmark_create_proposal_signal::<Test>());
+        });
+    }
+
+    #[test]
+    fn test_create_proposal_funding_request() {
+        initial_test_ext().execute_with(|| {
+            assert_ok!(test_benchmark_create_proposal_funding_request::<Test>());
+        });
+    }
+
+    #[test]
+    fn test_create_proposal_set_max_validator_count() {
+        initial_test_ext().execute_with(|| {
+            assert_ok!(test_benchmark_create_proposal_set_max_validator_count::<Test>());
+        });
+    }
+
+    #[test]
+    fn test_create_proposal_create_working_group_lead_opening() {
+        initial_test_ext().execute_with(|| {
+            assert_ok!(test_benchmark_create_proposal_create_working_group_lead_opening::<Test>());
+        });
+    }
+
+    #[test]
+    fn test_create_proposal_fill_working_group_lead_opening() {
+        initial_test_ext().execute_with(|| {
+            assert_ok!(test_benchmark_create_proposal_fill_working_group_lead_opening::<Test>());
+        });
+    }
+
+    #[test]
+    fn test_create_proposal_update_working_group_budget() {
+        initial_test_ext().execute_with(|| {
+            assert_ok!(test_benchmark_create_proposal_update_working_group_budget::<Test>());
+        });
+    }
+
+    #[test]
+    fn test_create_proposal_decrease_working_group_lead_stake() {
+        initial_test_ext().execute_with(|| {
+            assert_ok!(test_benchmark_create_proposal_decrease_working_group_lead_stake::<Test>());
+        });
+    }
+
+    #[test]
+    fn test_create_proposal_slash_working_group_lead() {
+        initial_test_ext().execute_with(|| {
+            assert_ok!(test_benchmark_create_proposal_slash_working_group_lead::<
+                Test,
+            >());
+        });
+    }
+
+    #[test]
+    fn test_create_proposal_set_working_group_lead_reward() {
+        initial_test_ext().execute_with(|| {
+            assert_ok!(test_benchmark_create_proposal_set_working_group_lead_reward::<Test>());
+        });
+    }
+
+    #[test]
+    fn test_create_proposal_terminate_working_group_lead() {
+        initial_test_ext().execute_with(|| {
+            assert_ok!(test_benchmark_create_proposal_terminate_working_group_lead::<Test>());
+        });
+    }
+
+    #[test]
+    fn test_create_proposal_amend_constitution() {
+        initial_test_ext().execute_with(|| {
+            assert_ok!(test_benchmark_create_proposal_amend_constitution::<Test>());
+        });
+    }
+
+    #[test]
+    fn test_create_proposal_cancel_working_group_lead_opening() {
+        initial_test_ext().execute_with(|| {
+            assert_ok!(test_benchmark_create_proposal_amend_constitution::<Test>());
+        });
+    }
+
+    #[test]
+    fn test_create_proposal_set_membership_price() {
+        initial_test_ext().execute_with(|| {
+            assert_ok!(test_benchmark_create_proposal_set_membership_price::<Test>());
+        });
+    }
+
+    #[test]
+    fn test_create_proposal_set_council_budget_increment() {
+        initial_test_ext().execute_with(|| {
+            assert_ok!(test_benchmark_create_proposal_set_council_budget_increment::<Test>());
+        });
+    }
+
+    #[test]
+    fn test_create_proposal_set_councior_reward() {
+        initial_test_ext().execute_with(|| {
+            assert_ok!(test_benchmark_create_proposal_set_councilor_reward::<Test>());
+        });
+    }
+
+    #[test]
+    fn test_create_proposal_set_initial_invitation_balance() {
+        initial_test_ext().execute_with(|| {
+            assert_ok!(test_benchmark_create_proposal_set_initial_invitation_balance::<Test>());
+        });
+    }
+}

+ 136 - 4
runtime-modules/proposals/codex/src/lib.rs

@@ -58,11 +58,15 @@ mod types;
 #[cfg(test)]
 mod tests;
 
+mod benchmarking;
+
 use frame_support::dispatch::DispatchResult;
 use frame_support::traits::Get;
+use frame_support::weights::Weight;
 use frame_support::{decl_error, decl_module, decl_storage, ensure, print};
 use frame_system::ensure_root;
 use sp_arithmetic::traits::Zero;
+use sp_runtime::SaturatedConversion;
 use sp_std::clone::Clone;
 use sp_std::vec::Vec;
 
@@ -84,6 +88,31 @@ const MAX_SPENDING_PROPOSAL_VALUE: u32 = 5_000_000_u32;
 // Max validator count for the 'Set Max Validator Count' proposal
 const MAX_VALIDATOR_COUNT: u32 = 100;
 
+/// Proposal codex WeightInfo.
+/// Note: This was auto generated through the benchmark CLI using the `--weight-trait` flag
+pub trait WeightInfo {
+    fn execute_signal_proposal(i: u32) -> Weight;
+    fn create_proposal_signal(i: u32) -> Weight; // Extra parameters not used
+    fn create_proposal_runtime_upgrade(i: u32, t: u32, d: u32) -> Weight;
+    fn create_proposal_funding_request() -> Weight; // Extra parameters not used
+    fn create_proposal_set_max_validator_count(t: u32, d: u32) -> Weight;
+    fn create_proposal_create_working_group_lead_opening(i: u32, t: u32, d: u32) -> Weight;
+    fn create_proposal_fill_working_group_lead_opening(t: u32, d: u32) -> Weight;
+    fn create_proposal_update_working_group_budget(d: u32) -> Weight; // Extra parameters not used
+    fn create_proposal_decrease_working_group_lead_stake() -> Weight; // Extra parameters not used
+    fn create_proposal_slash_working_group_lead(d: u32) -> Weight; // Extra parameters not used
+    fn create_proposal_set_working_group_lead_reward(d: u32) -> Weight; // Extra parameters not used
+    fn create_proposal_terminate_working_group_lead(t: u32, d: u32) -> Weight;
+    fn create_proposal_amend_constitution(i: u32, t: u32, d: u32) -> Weight;
+    fn create_proposal_cancel_working_group_lead_opening(t: u32) -> Weight; // Extra parameters not used
+    fn create_proposal_set_membership_price(t: u32, d: u32) -> Weight;
+    fn create_proposal_set_council_budget_increment(d: u32) -> Weight; // Extra parameters not used
+    fn create_proposal_set_councilor_reward(t: u32) -> Weight; // Extra parameters not used
+    fn create_proposal_set_initial_invitation_balance(t: u32, d: u32) -> Weight;
+}
+
+type WeightInfoCodex<T> = <T as Trait>::WeightInfo;
+
 /// 'Proposals codex' substrate module Trait
 pub trait Trait:
     frame_system::Trait
@@ -107,6 +136,9 @@ pub trait Trait:
     /// Encodes the proposal usint its details.
     type ProposalEncoder: ProposalEncoder<Self>;
 
+    /// Weight information for extrinsics in this pallet.
+    type WeightInfo: WeightInfo;
+
     /// 'Set Max Validator Count' proposal parameters.
     type SetMaxValidatorCountProposalParameters: Get<
         ProposalParameters<Self::BlockNumber, BalanceOf<Self>>,
@@ -340,7 +372,7 @@ decl_module! {
             ProposalParameters<T::BlockNumber, BalanceOf<T>> = T::SetInitialInvitationBalanceProposalParameters::get();
 
         /// Create a proposal, the type of proposal depends on the `proposal_details` variant
-        #[weight = 10_000_000] // TODO: adjust weight
+        #[weight = Module::<T>::get_create_proposal_weight(&general_proposal_parameters, &proposal_details)]
         pub fn create_proposal(
             origin,
             general_proposal_parameters: GeneralProposalParameters<T>,
@@ -393,9 +425,8 @@ decl_module! {
 
 // *************** Extrinsic to execute
 
-        /// Text proposal extrinsic.
-        /// Should be used as callable object to pass to the `engine` module.
-        #[weight = 10_000_000] // TODO: adjust weight
+        /// Signal proposal extrinsic. Should be used as callable object to pass to the `engine` module.
+        #[weight = WeightInfoCodex::<T>::execute_signal_proposal(signal.len().saturated_into())]
         pub fn execute_signal_proposal(
             origin,
             signal: Vec<u8>,
@@ -590,6 +621,107 @@ impl<T: Trait> Module<T> {
             }
         }
     }
+
+    // Returns weight for the proposal creatin according to parameters
+    fn get_create_proposal_weight(
+        general: &GeneralProposalParameters<T>,
+        details: &ProposalDetailsOf<T>,
+    ) -> Weight {
+        let title_length = general.title.len();
+        let description_length = general.description.len();
+        match details {
+            ProposalDetails::Signal(signal) => {
+                WeightInfoCodex::<T>::create_proposal_signal(signal.len().saturated_into())
+            }
+            ProposalDetails::RuntimeUpgrade(blob) => {
+                WeightInfoCodex::<T>::create_proposal_runtime_upgrade(
+                    blob.len().saturated_into(),
+                    title_length.saturated_into(),
+                    description_length.saturated_into(),
+                )
+            }
+            ProposalDetails::FundingRequest(..) => {
+                WeightInfoCodex::<T>::create_proposal_funding_request()
+            }
+            ProposalDetails::SetMaxValidatorCount(..) => {
+                WeightInfoCodex::<T>::create_proposal_set_max_validator_count(
+                    title_length.saturated_into(),
+                    description_length.saturated_into(),
+                )
+            }
+            ProposalDetails::CreateWorkingGroupLeadOpening(opening_params) => {
+                WeightInfoCodex::<T>::create_proposal_create_working_group_lead_opening(
+                    opening_params.description.len().saturated_into(),
+                    title_length.saturated_into(),
+                    description_length.saturated_into(),
+                )
+            }
+            ProposalDetails::FillWorkingGroupLeadOpening(..) => {
+                WeightInfoCodex::<T>::create_proposal_fill_working_group_lead_opening(
+                    title_length.saturated_into(),
+                    description_length.saturated_into(),
+                )
+            }
+            ProposalDetails::UpdateWorkingGroupBudget(..) => {
+                WeightInfoCodex::<T>::create_proposal_update_working_group_budget(
+                    description_length.saturated_into(),
+                )
+            }
+            ProposalDetails::DecreaseWorkingGroupLeadStake(..) => {
+                WeightInfoCodex::<T>::create_proposal_decrease_working_group_lead_stake()
+            }
+            ProposalDetails::SlashWorkingGroupLead(..) => {
+                WeightInfoCodex::<T>::create_proposal_slash_working_group_lead(
+                    description_length.saturated_into(),
+                )
+            }
+            ProposalDetails::SetWorkingGroupLeadReward(..) => {
+                WeightInfoCodex::<T>::create_proposal_set_working_group_lead_reward(
+                    description_length.saturated_into(),
+                )
+            }
+            ProposalDetails::TerminateWorkingGroupLead(..) => {
+                WeightInfoCodex::<T>::create_proposal_terminate_working_group_lead(
+                    title_length.saturated_into(),
+                    description_length.saturated_into(),
+                )
+            }
+            ProposalDetails::AmendConstitution(new_constitution) => {
+                WeightInfoCodex::<T>::create_proposal_amend_constitution(
+                    new_constitution.len().saturated_into(),
+                    title_length.saturated_into(),
+                    description_length.saturated_into(),
+                )
+            }
+            ProposalDetails::SetMembershipPrice(..) => {
+                WeightInfoCodex::<T>::create_proposal_set_membership_price(
+                    title_length.saturated_into(),
+                    description_length.saturated_into(),
+                )
+            }
+            ProposalDetails::CancelWorkingGroupLeadOpening(..) => {
+                WeightInfoCodex::<T>::create_proposal_cancel_working_group_lead_opening(
+                    title_length.saturated_into(),
+                )
+            }
+            ProposalDetails::SetCouncilBudgetIncrement(..) => {
+                WeightInfoCodex::<T>::create_proposal_set_council_budget_increment(
+                    description_length.saturated_into(),
+                )
+            }
+            ProposalDetails::SetCouncilorReward(..) => {
+                WeightInfoCodex::<T>::create_proposal_set_councilor_reward(
+                    title_length.saturated_into(),
+                )
+            }
+            ProposalDetails::SetInitialInvitationBalance(..) => {
+                WeightInfoCodex::<T>::create_proposal_set_initial_invitation_balance(
+                    title_length.saturated_into(),
+                    description_length.saturated_into(),
+                )
+            }
+        }
+    }
 }
 
 impl<T: Trait> ProposalObserver<T> for Module<T> {

+ 58 - 0
runtime-modules/proposals/codex/src/tests/mock.rs

@@ -460,6 +460,7 @@ pub(crate) fn default_proposal_parameters() -> ProposalParameters<u64, u64> {
 impl crate::Trait for Test {
     type MembershipOriginValidator = ();
     type ProposalEncoder = ();
+    type WeightInfo = ();
     type SetMaxValidatorCountProposalParameters = DefaultProposalParameters;
     type RuntimeUpgradeProposalParameters = DefaultProposalParameters;
     type SignalProposalParameters = DefaultProposalParameters;
@@ -664,6 +665,63 @@ impl referendum::WeightInfo for ReferendumWeightInfo {
     }
 }
 
+impl crate::WeightInfo for () {
+    fn execute_signal_proposal(_: u32) -> Weight {
+        0
+    }
+    fn create_proposal_signal(_: u32) -> Weight {
+        0
+    }
+    fn create_proposal_runtime_upgrade(_: u32, _: u32, _: u32) -> Weight {
+        0
+    }
+    fn create_proposal_funding_request() -> Weight {
+        0
+    }
+    fn create_proposal_set_max_validator_count(_: u32, _: u32) -> Weight {
+        0
+    }
+    fn create_proposal_create_working_group_lead_opening(_: u32, _: u32, _: u32) -> Weight {
+        0
+    }
+    fn create_proposal_fill_working_group_lead_opening(_: u32, _: u32) -> Weight {
+        0
+    }
+    fn create_proposal_update_working_group_budget(_: u32) -> Weight {
+        0
+    }
+    fn create_proposal_decrease_working_group_lead_stake() -> Weight {
+        0
+    }
+    fn create_proposal_slash_working_group_lead(_: u32) -> Weight {
+        0
+    }
+    fn create_proposal_set_working_group_lead_reward(_: u32) -> Weight {
+        0
+    }
+    fn create_proposal_terminate_working_group_lead(_: u32, _: u32) -> Weight {
+        0
+    }
+    fn create_proposal_amend_constitution(_: u32, _: u32, _: u32) -> Weight {
+        0
+    }
+    fn create_proposal_cancel_working_group_lead_opening(_: u32) -> Weight {
+        0
+    }
+    fn create_proposal_set_membership_price(_: u32, _: u32) -> Weight {
+        0
+    }
+    fn create_proposal_set_council_budget_increment(_: u32) -> Weight {
+        0
+    }
+    fn create_proposal_set_councilor_reward(_: u32) -> Weight {
+        0
+    }
+    fn create_proposal_set_initial_invitation_balance(_: u32, _: u32) -> Weight {
+        0
+    }
+}
+
 impl ProposalEncoder<Test> for () {
     fn encode_proposal(_proposal_details: ProposalDetailsOf<Test>) -> Vec<u8> {
         Vec::new()

+ 2 - 44
runtime-modules/proposals/codex/src/tests/mod.rs

@@ -310,8 +310,8 @@ fn create_funding_request_proposal_call_fails_with_incorrect_balance() {
             exact_execution_block: None,
         };
 
-        let mint_capacity = 100;
-        council::Module::<Test>::set_budget(RawOrigin::Root.into(), mint_capacity).unwrap();
+        let budget_capacity = 100;
+        council::Module::<Test>::set_budget(RawOrigin::Root.into(), budget_capacity).unwrap();
 
         assert_eq!(
             ProposalCodex::create_proposal(
@@ -322,15 +322,6 @@ fn create_funding_request_proposal_call_fails_with_incorrect_balance() {
             Err(Error::<Test>::InvalidFundingRequestProposalBalance.into())
         );
 
-        assert_eq!(
-            ProposalCodex::create_proposal(
-                RawOrigin::Signed(1).into(),
-                general_proposal_parameters.clone(),
-                ProposalDetails::FundingRequest(mint_capacity + 1, 2),
-            ),
-            Err(Error::<Test>::InvalidFundingRequestProposalBalance.into())
-        );
-
         let exceeded_budget = MAX_SPENDING_PROPOSAL_VALUE + 1;
 
         assert_eq!(
@@ -764,14 +755,6 @@ fn run_create_slash_working_group_leader_stake_proposal_common_checks_succeed(
     });
 }
 
-#[test]
-fn slash_stake_with_zero_staking_balance_fails() {
-    // This uses strum crate for enum iteration
-    for group in WorkingGroup::iter() {
-        run_slash_stake_with_zero_staking_balance_fails(group);
-    }
-}
-
 pub fn run_to_block(n: u64) {
     while System::block_number() < n {
         System::on_finalize(System::block_number());
@@ -859,31 +842,6 @@ fn setup_council(start_id: u64) {
     );
 }
 
-fn run_slash_stake_with_zero_staking_balance_fails(working_group: WorkingGroup) {
-    initial_test_ext().execute_with(|| {
-        increase_total_balance_issuance_using_account_id(1, 500000);
-
-        let general_proposal_parameters = GeneralProposalParameters::<Test> {
-            member_id: 1,
-            title: b"title".to_vec(),
-            description: b"body".to_vec(),
-            staking_account_id: Some(1),
-            exact_execution_block: None,
-        };
-
-        setup_council(2);
-
-        assert_eq!(
-            ProposalCodex::create_proposal(
-                RawOrigin::Signed(1).into(),
-                general_proposal_parameters.clone(),
-                ProposalDetails::SlashWorkingGroupLead(10, 0, working_group)
-            ),
-            Err(Error::<Test>::SlashingStakeIsZero.into())
-        );
-    });
-}
-
 #[test]
 fn decrease_stake_with_zero_staking_balance_fails() {
     // This uses strum crate for enum iteration

+ 1 - 0
runtime/Cargo.toml

@@ -165,6 +165,7 @@ runtime-benchmarks = [
     "pallet-utility/runtime-benchmarks",
     "proposals-discussion/runtime-benchmarks",
     "proposals-engine/runtime-benchmarks",
+    "proposals-codex/runtime-benchmarks",
     "pallet_constitution/runtime-benchmarks",
     "working-group/runtime-benchmarks",
     "forum/runtime-benchmarks",

+ 1 - 0
runtime/src/lib.rs

@@ -817,6 +817,7 @@ impl proposals_codex::Trait for Runtime {
     type SetCouncilorRewardProposalParameters = SetCouncilorRewardProposalParameters;
     type SetInitialInvitationBalanceProposalParameters =
         SetInitialInvitationBalanceProposalParameters;
+    type WeightInfo = weights::proposals_codex::WeightInfo;
 }
 
 impl pallet_constitution::Trait for Runtime {

+ 2 - 0
runtime/src/runtime_api.rs

@@ -284,6 +284,7 @@ impl_runtime_apis! {
             use frame_system::RawOrigin;
             use crate::ProposalsDiscussion;
             use crate::ProposalsEngine;
+            use crate::ProposalsCodex;
             use crate::Constitution;
             use crate::Forum;
             use crate::ContentDirectoryWorkingGroup;
@@ -351,6 +352,7 @@ impl_runtime_apis! {
 
             // Joystream Benchmarks
             add_benchmark!(params, batches, proposals_discussion, ProposalsDiscussion);
+            add_benchmark!(params, batches, proposals_codex, ProposalsCodex);
             add_benchmark!(params, batches, proposals_engine, ProposalsEngine);
             add_benchmark!(params, batches, forum, Forum);
             add_benchmark!(params, batches, pallet_constitution, Constitution);

+ 1 - 0
runtime/src/weights/mod.rs

@@ -27,6 +27,7 @@ pub mod pallet_utility;
 pub mod council;
 pub mod forum;
 pub mod pallet_constitution;
+pub mod proposals_codex;
 pub mod proposals_discussion;
 pub mod proposals_engine;
 pub mod referendum;

+ 133 - 0
runtime/src/weights/proposals_codex.rs

@@ -0,0 +1,133 @@
+//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0
+
+#![allow(unused_parens)]
+#![allow(unused_imports)]
+
+use frame_support::weights::{constants::RocksDbWeight as DbWeight, Weight};
+
+pub struct WeightInfo;
+impl proposals_codex::WeightInfo for WeightInfo {
+    fn execute_signal_proposal(i: u32) -> Weight {
+        (16_059_000 as Weight).saturating_add((41_000 as Weight).saturating_mul(i as Weight))
+    }
+    // WARNING! Some components were not used: ["t", "d"]
+    fn create_proposal_signal(i: u32) -> Weight {
+        (663_482_000 as Weight)
+            .saturating_add((246_000 as Weight).saturating_mul(i as Weight))
+            .saturating_add(DbWeight::get().reads(6 as Weight))
+            .saturating_add(DbWeight::get().writes(10 as Weight))
+    }
+    fn create_proposal_runtime_upgrade(i: u32, t: u32, d: u32) -> Weight {
+        (0 as Weight)
+            .saturating_add((298_000 as Weight).saturating_mul(i as Weight))
+            .saturating_add((13_792_000 as Weight).saturating_mul(t as Weight))
+            .saturating_add((140_000 as Weight).saturating_mul(d as Weight))
+            .saturating_add(DbWeight::get().reads(6 as Weight))
+            .saturating_add(DbWeight::get().writes(10 as Weight))
+    }
+    // WARNING! Some components were not used: ["t", "d"]
+    fn create_proposal_funding_request() -> Weight {
+        (1_011_708_000 as Weight)
+            .saturating_add(DbWeight::get().reads(8 as Weight))
+            .saturating_add(DbWeight::get().writes(10 as Weight))
+    }
+    fn create_proposal_set_max_validator_count(t: u32, d: u32) -> Weight {
+        (737_501_000 as Weight)
+            .saturating_add((3_119_000 as Weight).saturating_mul(t as Weight))
+            .saturating_add((20_000 as Weight).saturating_mul(d as Weight))
+            .saturating_add(DbWeight::get().reads(7 as Weight))
+            .saturating_add(DbWeight::get().writes(10 as Weight))
+    }
+    fn create_proposal_create_working_group_lead_opening(i: u32, t: u32, d: u32) -> Weight {
+        (393_931_000 as Weight)
+            .saturating_add((383_000 as Weight).saturating_mul(i as Weight))
+            .saturating_add((6_783_000 as Weight).saturating_mul(t as Weight))
+            .saturating_add((164_000 as Weight).saturating_mul(d as Weight))
+            .saturating_add(DbWeight::get().reads(6 as Weight))
+            .saturating_add(DbWeight::get().writes(10 as Weight))
+    }
+    fn create_proposal_fill_working_group_lead_opening(t: u32, d: u32) -> Weight {
+        (566_718_000 as Weight)
+            .saturating_add((3_073_000 as Weight).saturating_mul(t as Weight))
+            .saturating_add((20_000 as Weight).saturating_mul(d as Weight))
+            .saturating_add(DbWeight::get().reads(6 as Weight))
+            .saturating_add(DbWeight::get().writes(10 as Weight))
+    }
+    // WARNING! Some components were not used: ["t"]
+    fn create_proposal_update_working_group_budget(d: u32) -> Weight {
+        (697_884_000 as Weight)
+            .saturating_add((4_000 as Weight).saturating_mul(d as Weight))
+            .saturating_add(DbWeight::get().reads(6 as Weight))
+            .saturating_add(DbWeight::get().writes(10 as Weight))
+    }
+    // WARNING! Some components were not used: ["t", "d"]
+    fn create_proposal_decrease_working_group_lead_stake() -> Weight {
+        (660_344_000 as Weight)
+            .saturating_add(DbWeight::get().reads(6 as Weight))
+            .saturating_add(DbWeight::get().writes(10 as Weight))
+    }
+    // WARNING! Some components were not used: ["t"]
+    fn create_proposal_slash_working_group_lead(d: u32) -> Weight {
+        (650_009_000 as Weight)
+            .saturating_add((4_000 as Weight).saturating_mul(d as Weight))
+            .saturating_add(DbWeight::get().reads(6 as Weight))
+            .saturating_add(DbWeight::get().writes(10 as Weight))
+    }
+    // WARNING! Some components were not used: ["t"]
+    fn create_proposal_set_working_group_lead_reward(d: u32) -> Weight {
+        (641_391_000 as Weight)
+            .saturating_add((1_000 as Weight).saturating_mul(d as Weight))
+            .saturating_add(DbWeight::get().reads(6 as Weight))
+            .saturating_add(DbWeight::get().writes(10 as Weight))
+    }
+    fn create_proposal_terminate_working_group_lead(t: u32, d: u32) -> Weight {
+        (640_488_000 as Weight)
+            .saturating_add((55_000 as Weight).saturating_mul(t as Weight))
+            .saturating_add((1_000 as Weight).saturating_mul(d as Weight))
+            .saturating_add(DbWeight::get().reads(6 as Weight))
+            .saturating_add(DbWeight::get().writes(10 as Weight))
+    }
+    fn create_proposal_amend_constitution(i: u32, t: u32, d: u32) -> Weight {
+        (527_420_000 as Weight)
+            .saturating_add((278_000 as Weight).saturating_mul(i as Weight))
+            .saturating_add((1_785_000 as Weight).saturating_mul(t as Weight))
+            .saturating_add((21_000 as Weight).saturating_mul(d as Weight))
+            .saturating_add(DbWeight::get().reads(6 as Weight))
+            .saturating_add(DbWeight::get().writes(10 as Weight))
+    }
+    // WARNING! Some components were not used: ["d"]
+    fn create_proposal_cancel_working_group_lead_opening(t: u32) -> Weight {
+        (644_736_000 as Weight)
+            .saturating_add((50_000 as Weight).saturating_mul(t as Weight))
+            .saturating_add(DbWeight::get().reads(6 as Weight))
+            .saturating_add(DbWeight::get().writes(10 as Weight))
+    }
+    fn create_proposal_set_membership_price(t: u32, d: u32) -> Weight {
+        (638_989_000 as Weight)
+            .saturating_add((53_000 as Weight).saturating_mul(t as Weight))
+            .saturating_add((1_000 as Weight).saturating_mul(d as Weight))
+            .saturating_add(DbWeight::get().reads(6 as Weight))
+            .saturating_add(DbWeight::get().writes(10 as Weight))
+    }
+    // WARNING! Some components were not used: ["t"]
+    fn create_proposal_set_council_budget_increment(d: u32) -> Weight {
+        (635_709_000 as Weight)
+            .saturating_add((16_000 as Weight).saturating_mul(d as Weight))
+            .saturating_add(DbWeight::get().reads(6 as Weight))
+            .saturating_add(DbWeight::get().writes(10 as Weight))
+    }
+    // WARNING! Some components were not used: ["d"]
+    fn create_proposal_set_councilor_reward(t: u32) -> Weight {
+        (648_346_000 as Weight)
+            .saturating_add((110_000 as Weight).saturating_mul(t as Weight))
+            .saturating_add(DbWeight::get().reads(6 as Weight))
+            .saturating_add(DbWeight::get().writes(10 as Weight))
+    }
+    fn create_proposal_set_initial_invitation_balance(t: u32, d: u32) -> Weight {
+        (632_164_000 as Weight)
+            .saturating_add((271_000 as Weight).saturating_mul(t as Weight))
+            .saturating_add((1_000 as Weight).saturating_mul(d as Weight))
+            .saturating_add(DbWeight::get().reads(6 as Weight))
+            .saturating_add(DbWeight::get().writes(10 as Weight))
+    }
+}