diff --git a/pallets/democracy/src/benchmarking.rs b/pallets/democracy/src/benchmarking.rs index ff314d515..c447ea722 100644 --- a/pallets/democracy/src/benchmarking.rs +++ b/pallets/democracy/src/benchmarking.rs @@ -114,6 +114,8 @@ benchmarks! { let caller = funded_account::("caller", 0); let account_vote = account_vote::(100u32.into()); + T::DemocracyHooks::on_vote_worst_case(&caller); + // We need to create existing direct votes for i in 0 .. T::MaxVotes::get() - 1 { let ref_index = add_referendum::(i).0; @@ -140,6 +142,8 @@ benchmarks! { let caller = funded_account::("caller", 0); let account_vote = account_vote::(100u32.into()); + T::DemocracyHooks::on_vote_worst_case(&caller); + // We need to create existing direct votes for i in 0..T::MaxVotes::get() { let ref_index = add_referendum::(i).0; @@ -624,6 +628,8 @@ benchmarks! { let caller = funded_account::("caller", 0); let account_vote = account_vote::(100u32.into()); + T::DemocracyHooks::on_remove_vote_worst_case(&caller); + for i in 0 .. r { let ref_index = add_referendum::(i).0; Democracy::::vote(RawOrigin::Signed(caller.clone()).into(), ref_index, account_vote)?; @@ -654,6 +660,8 @@ benchmarks! { let caller_lookup = T::Lookup::unlookup(caller.clone()); let account_vote = account_vote::(100u32.into()); + T::DemocracyHooks::on_remove_vote_worst_case(&caller); + for i in 0 .. r { let ref_index = add_referendum::(i).0; Democracy::::vote(RawOrigin::Signed(caller.clone()).into(), ref_index, account_vote)?; diff --git a/pallets/democracy/src/traits.rs b/pallets/democracy/src/traits.rs index 3873aa4bd..bc390a78b 100644 --- a/pallets/democracy/src/traits.rs +++ b/pallets/democracy/src/traits.rs @@ -4,6 +4,12 @@ use frame_support::dispatch::DispatchResult; pub trait DemocracyHooks { fn on_vote(who: &AccountId, ref_index: ReferendumIndex, vote: AccountVote) -> DispatchResult; fn on_remove_vote(who: &AccountId, ref_index: ReferendumIndex) -> DispatchResult; + + #[cfg(feature = "runtime-benchmarks")] + fn on_vote_worst_case(_who: &AccountId); + + #[cfg(feature = "runtime-benchmarks")] + fn on_remove_vote_worst_case(_who: &AccountId); } impl DemocracyHooks for () { @@ -14,4 +20,10 @@ impl DemocracyHooks for () { fn on_remove_vote(_who: &AccountId, _ref_index: ReferendumIndex) -> DispatchResult { Ok(()) } + + #[cfg(feature = "runtime-benchmarks")] + fn on_vote_worst_case(_who: &AccountId) {} + + #[cfg(feature = "runtime-benchmarks")] + fn on_remove_vote_worst_case(_who: &AccountId) {} } diff --git a/pallets/staking/Cargo.toml b/pallets/staking/Cargo.toml index 914435a53..da71dc4f4 100644 --- a/pallets/staking/Cargo.toml +++ b/pallets/staking/Cargo.toml @@ -52,15 +52,16 @@ std = [ "sp-runtime/std", "sp-std/std", "sp-core/std", - "pallet-balances/std", - "pallet-uniques/std", - "orml-tokens/std", - "pallet-democracy/std", + "pallet-balances/std", + "pallet-uniques/std", + "orml-tokens/std", + "pallet-democracy/std", ] runtime-benchmarks = [ "frame-benchmarking/runtime-benchmarks", "frame-support/runtime-benchmarks", "frame-system/runtime-benchmarks", "sp-runtime/runtime-benchmarks", + "pallet-uniques/runtime-benchmarks", ] try-runtime = ["frame-support/try-runtime",] diff --git a/pallets/staking/src/integrations/democracy.rs b/pallets/staking/src/integrations/democracy.rs index d19c8531b..896543794 100644 --- a/pallets/staking/src/integrations/democracy.rs +++ b/pallets/staking/src/integrations/democracy.rs @@ -3,12 +3,18 @@ use crate::traits::DemocracyReferendum; use crate::types::{Balance, Conviction, Vote}; use crate::{Config, Error, Pallet}; use frame_support::dispatch::DispatchResult; +use frame_system::Origin; +use orml_traits::MultiCurrencyExtended; use pallet_democracy::traits::DemocracyHooks; use pallet_democracy::{AccountVote, ReferendumIndex, ReferendumInfo}; +use sp_core::Get; pub struct StakingDemocracy(sp_std::marker::PhantomData); -impl DemocracyHooks for StakingDemocracy { +impl DemocracyHooks for StakingDemocracy +where + T::Currency: MultiCurrencyExtended, +{ fn on_vote(who: &T::AccountId, ref_index: ReferendumIndex, vote: AccountVote) -> DispatchResult { let position_id = if let Some(position_id) = Pallet::::get_user_position_id(who)? { position_id @@ -74,6 +80,32 @@ impl DemocracyHooks for StakingDemocracy { Ok(()) } + + #[cfg(feature = "runtime-benchmarks")] + fn on_vote_worst_case(who: &T::AccountId) { + T::Currency::update_balance( + T::HdxAssetId::get(), + &Pallet::::pot_account_id(), + 10_000_000_000_000i128, + ) + .unwrap(); + Pallet::::initialize_staking(Origin::::Root.into()).unwrap(); + T::Currency::update_balance(T::HdxAssetId::get(), who, 1000_000_000_000_000i128).unwrap(); + Pallet::::stake(Origin::::Signed(who.clone()).into(), 1000_000_000_000_000u128).unwrap(); + } + + #[cfg(feature = "runtime-benchmarks")] + fn on_remove_vote_worst_case(who: &T::AccountId) { + T::Currency::update_balance( + T::HdxAssetId::get(), + &Pallet::::pot_account_id(), + 10_000_000_000_000i128, + ) + .unwrap(); + Pallet::::initialize_staking(Origin::::Root.into()).unwrap(); + T::Currency::update_balance(T::HdxAssetId::get(), who, 1000_000_000_000_000i128).unwrap(); + Pallet::::stake(Origin::::Signed(who.clone()).into(), 1000_000_000_000_000u128).unwrap(); + } } pub struct ReferendumStatus(sp_std::marker::PhantomData); diff --git a/scripts/benchmark.all.sh b/scripts/benchmark.all.sh index c22bd9b56..ab54dcc2b 100644 --- a/scripts/benchmark.all.sh +++ b/scripts/benchmark.all.sh @@ -30,6 +30,7 @@ pallets=("frame-system:system" "pallet-ema-oracle:ema_oracle" "pallet-otc:otc" "pallet-route-executor:route_executor" +"pallet-staking:staking" ) command="cargo run --bin hydradx --release --features=runtime-benchmarks -- benchmark pallet --pallet=[pallet] --execution=wasm --wasm-execution=compiled --heap-pages=4096 --chain=dev --extrinsic='*' --steps=5 --repeat=20 --output [output].rs --template .maintain/pallet-weight-template-no-back.hbs"