Browse Source

benchmark: proposals/engine: add `veto_proposal` benchmark

conectado 4 years ago
parent
commit
c29051c6ac

+ 7 - 6
runtime-modules/proposals/engine/Cargo.toml

@@ -17,9 +17,13 @@ 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}
-recurringrewards = { package = 'pallet-recurring-reward', default-features = false, path = '../../recurring-reward', optional = true}
-minting = { package = 'pallet-token-mint', default-features = false, path = '../../token-minting', optional = true}
+# We need to add the following dependencies(governance, recurringrrewards and minting) for benchmarking
+# but since we also need them for the mock runtime for the benchmark tests, we need to implement the traits and events
+# in the Test runtime so we can't add them as optional dependncies(the test compilation without feature benchmark
+# breaks) and we can't add them as dev-dependencies since that would break the benchmarks
+governance = { package = 'pallet-governance', default-features = false, path = '../../governance'}
+recurringrewards = { package = 'pallet-recurring-reward', default-features = false, path = '../../recurring-reward'}
+minting = { package = 'pallet-token-mint', default-features = false, path = '../../token-minting'}
 
 [dev-dependencies]
 sp-io = { package = 'sp-io', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca'}
@@ -29,9 +33,6 @@ sp-core = { package = 'sp-core', default-features = false, git = 'https://github
 default = ['std']
 runtime-benchmarks = [
     'frame-benchmarking',
-    'governance',
-    'recurringrewards',
-    'minting',
 ]
 std = [
 	'serde',

+ 45 - 3
runtime-modules/proposals/engine/src/benchmarking.rs

@@ -79,7 +79,7 @@ fn create_proposal<T: Trait>(id: u32) -> (T::AccountId, T::MemberId, T::Proposal
         approval_threshold_percentage: 1,
         slashing_quorum_percentage: 0,
         slashing_threshold_percentage: 1,
-        required_stake: Some(One::one()),
+        required_stake: Some(T::Balance::max_value()),
         constitutionality: 0,
     };
 
@@ -190,7 +190,7 @@ benchmarks! {
             T::StakingHandler::set_stake(&locked_account_id, One::one()).unwrap();
         }
 
-    }: _ (RawOrigin::Signed(account_id), member_id, proposal_id)
+    }: _ (RawOrigin::Signed(account_id.clone()), member_id, proposal_id)
     verify {
         assert!(!Proposals::<T>::contains_key(proposal_id), "Proposal still in storage");
 
@@ -199,7 +199,42 @@ benchmarks! {
             "Proposal code still in storage"
         );
 
-        assert_eq!(ProposalsEngine::<T>::active_proposal_count(), i, "Proposal still active");
+        assert_eq!(ProposalsEngine::<T>::active_proposal_count(), 0, "Proposal still active");
+
+        assert_eq!(
+            Balances::<T>::usable_balance(account_id),
+            T::Balance::max_value() - T::CancellationFee::get(),
+            "Balance not slashed"
+        );
+
+        assert_last_event::<T>(
+            RawEvent::ProposalDecisionMade(proposal_id, ProposalDecision::Canceled).into()
+        );
+    }
+
+    veto_proposal {
+        let i in 0 .. 1;
+        let (account_id, _, proposal_id) = create_proposal::<T>(0);
+    }: _ (RawOrigin::Root, proposal_id)
+    verify {
+        assert!(!Proposals::<T>::contains_key(proposal_id), "Proposal still in storage");
+
+        assert!(
+            !DispatchableCallCode::<T>::contains_key(proposal_id),
+            "Proposal code still in storage"
+        );
+
+        assert_eq!(ProposalsEngine::<T>::active_proposal_count(), 0, "Proposal still active");
+
+        assert_eq!(
+            Balances::<T>::usable_balance(account_id),
+            T::Balance::max_value(),
+            "Vetoed proposals shouldn't be slashed"
+        );
+
+        assert_last_event::<T>(
+            RawEvent::ProposalDecisionMade(proposal_id, ProposalDecision::Vetoed).into()
+        );
     }
 
 }
@@ -223,4 +258,11 @@ mod tests {
             assert_ok!(test_benchmark_cancel_proposal::<Test>());
         });
     }
+
+    #[test]
+    fn test_veto_proposal() {
+        initial_test_ext().execute_with(|| {
+            assert_ok!(test_benchmark_veto_proposal::<Test>());
+        });
+    }
 }

+ 0 - 3
runtime-modules/proposals/engine/src/tests/mock/mod.rs

@@ -175,20 +175,17 @@ impl pallet_timestamp::Trait for Test {
     type WeightInfo = ();
 }
 
-#[cfg(feature = "runtime-benchmarks")]
 impl governance::council::Trait for Test {
     type Event = TestEvent;
     type CouncilTermEnded = ();
 }
 
-#[cfg(feature = "runtime-benchmarks")]
 impl recurringrewards::Trait for Test {
     type PayoutStatusHandler = ();
     type RecipientId = u64;
     type RewardRelationshipId = u64;
 }
 
-#[cfg(feature = "runtime-benchmarks")]
 impl minting::Trait for Test {
     type Currency = Balances;
     type MintId = u64;