فهرست منبع

benchmark: proposals/engine: add benchmarking for proposals/engine

This commit also includes the `vote` extrinsic benchmark
conectado 4 سال پیش
والد
کامیت
6a349301a9

+ 2 - 0
Cargo.lock

@@ -4085,10 +4085,12 @@ dependencies = [
 name = "pallet-proposals-engine"
 version = "4.0.0"
 dependencies = [
+ "frame-benchmarking",
  "frame-support",
  "frame-system",
  "pallet-balances",
  "pallet-common",
+ "pallet-governance",
  "pallet-membership",
  "pallet-timestamp",
  "parity-scale-codec",

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

@@ -17,12 +17,19 @@ balances = { package = 'pallet-balances', default-features = false, git = 'https
 membership = { package = 'pallet-membership', default-features = false, path = '../../membership'}
 common = { package = 'pallet-common', default-features = false, path = '../../common'}
 
+frame-benchmarking = { package = 'frame-benchmarking', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '00768a1f21a579c478fe5d4f51e1fa71f7db9fd4', optional = true}
+governance = { package = 'pallet-governance', default-features = false, path = '../../governance', 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'}
 
 [features]
 default = ['std']
+runtime-benchmarks = [
+    'frame-benchmarking',
+    'governance',
+]
 std = [
 	'serde',
 	'codec/std',
@@ -35,4 +42,4 @@ std = [
 	'balances/std',
     'membership/std',
     'common/std',
-]
+]

+ 120 - 0
runtime-modules/proposals/engine/src/benchmarking.rs

@@ -0,0 +1,120 @@
+#![cfg(feature = "runtime-benchmarks")]
+use super::*;
+use crate::Module as ProposalsEngine;
+use core::convert::TryInto;
+use frame_benchmarking::{account, benchmarks};
+use governance::council::Module as Council;
+use membership::Module as Membership;
+use sp_std::prelude::*;
+use system as frame_system;
+use system::EventRecord;
+use system::Module as System;
+use system::RawOrigin;
+
+const SEED: u32 = 0;
+
+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.
+// TODO: This will only work as long as max_handle_length >= 4
+fn handle_from_id<T: membership::Trait>(id: u32) -> Vec<u8> {
+    let min_handle_length = Membership::<T>::min_handle_length();
+    // If the index is ever different from u32 change this
+    let mut handle = vec![
+        get_byte(id, 0),
+        get_byte(id, 1),
+        get_byte(id, 2),
+        get_byte(id, 3),
+    ];
+
+    while handle.len() < (min_handle_length as usize) {
+        handle.push(0u8);
+    }
+
+    handle
+}
+
+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();
+    // compare to the last event record
+    let EventRecord { event, .. } = &events[events.len() - 1];
+    assert_eq!(event, &system_event);
+}
+
+fn member_account<T: 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::<T>(id);
+
+    let authority_account = account::<T::AccountId>(name, 0, SEED);
+
+    Membership::<T>::set_screening_authority(RawOrigin::Root.into(), authority_account.clone())
+        .unwrap();
+
+    Membership::<T>::add_screened_member(
+        RawOrigin::Signed(authority_account.clone()).into(),
+        account_id.clone(),
+        Some(handle),
+        None,
+        None,
+    )
+    .unwrap();
+
+    (account_id, T::MemberId::from(id.try_into().unwrap()))
+}
+
+const MAX_BYTES: u32 = 16384;
+
+benchmarks! {
+    where_clause {
+        where T: governance::council::Trait
+    }
+    _ { }
+
+    vote {
+        let i in 0 .. MAX_BYTES;
+
+        let (account_id, member_id) = member_account::<T>("member", 0);
+
+        let proposal_parameters = ProposalParameters {
+            voting_period: T::BlockNumber::from(1),
+            grace_period: Zero::zero(),
+            approval_quorum_percentage: 1,
+            approval_threshold_percentage: 1,
+            slashing_quorum_percentage: 0,
+            slashing_threshold_percentage: 1,
+            required_stake: None,
+            constitutionality: 0,
+        };
+
+        let proposal_creation_parameters = ProposalCreationParameters {
+            account_id,
+            proposer_id: member_id,
+            proposal_parameters,
+            title: vec![0u8],
+            description: vec![0u8],
+            staking_account_id: None,
+            encoded_dispatchable_call_code: vec![0u8],
+            exact_execution_block: None,
+        };
+
+        let proposal_id =
+            ProposalsEngine::<T>::create_proposal(proposal_creation_parameters).unwrap();
+
+        let (account_voter_id, member_voter_id) = member_account::<T>("voter", 1);
+
+        Council::<T>::set_council(RawOrigin::Root.into(), vec![account_voter_id.clone()]).unwrap();
+    }: _ (
+            RawOrigin::Signed(account_voter_id),
+            member_voter_id,
+            proposal_id,
+            VoteKind::Approve,
+            vec![0u8; i.try_into().unwrap()]
+        )
+}

+ 2 - 0
runtime-modules/proposals/engine/src/lib.rs

@@ -119,6 +119,8 @@ pub use types::{
 
 pub(crate) mod types;
 
+mod benchmarking;
+
 #[cfg(test)]
 mod tests;
 

+ 1 - 0
runtime/Cargo.toml

@@ -175,6 +175,7 @@ runtime-benchmarks = [
 	"pallet-session-benchmarking",
     "pallet-utility/runtime-benchmarks",
     "proposals-discussion/runtime-benchmarks",
+    "proposals-engine/runtime-benchmarks",
     "hex-literal",
 ]
 

+ 3 - 4
runtime/src/runtime_api.rs

@@ -255,20 +255,18 @@ impl_runtime_apis! {
         }
     }
 
-     #[cfg(feature = "runtime-benchmarks")]
+    #[cfg(feature = "runtime-benchmarks")]
     impl frame_benchmarking::Benchmark<Block> for Runtime {
         fn dispatch_benchmark(
             config: frame_benchmarking::BenchmarkConfig
         ) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, sp_runtime::RuntimeString> {
-            /*
-             * TODO: remember to benchhmark every pallet
-             */
             use sp_std::vec;
             use frame_benchmarking::{Benchmarking, BenchmarkBatch, add_benchmark, TrackedStorageKey};
             use frame_system_benchmarking::Module as SystemBench;
             impl frame_system_benchmarking::Trait for Runtime {}
 
             use crate::ProposalsDiscussion;
+            use crate::ProposalsEngine;
 
             let whitelist: Vec<TrackedStorageKey> = vec![
                 // Block Number
@@ -292,6 +290,7 @@ impl_runtime_apis! {
 
             add_benchmark!(params, batches, system, SystemBench::<Runtime>);
             add_benchmark!(params, batches, proposals_discussion, ProposalsDiscussion);
+            add_benchmark!(params, batches, proposals_engine, ProposalsEngine);
 
             if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }
             Ok(batches)