Skip to content

Commit

Permalink
Merge branch 'feat/staking' of github.com:galacticcouncil/HydraDX-nod…
Browse files Browse the repository at this point in the history
…e into feat/staking
  • Loading branch information
martinfridrich committed Jul 19, 2023
2 parents 4e7c30c + 51b2455 commit 2059b03
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
8 changes: 8 additions & 0 deletions pallets/democracy/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ benchmarks! {
let caller = funded_account::<T>("caller", 0);
let account_vote = account_vote::<T>(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::<T>(i).0;
Expand All @@ -140,6 +142,8 @@ benchmarks! {
let caller = funded_account::<T>("caller", 0);
let account_vote = account_vote::<T>(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::<T>(i).0;
Expand Down Expand Up @@ -624,6 +628,8 @@ benchmarks! {
let caller = funded_account::<T>("caller", 0);
let account_vote = account_vote::<T>(100u32.into());

T::DemocracyHooks::on_remove_vote_worst_case(&caller);

for i in 0 .. r {
let ref_index = add_referendum::<T>(i).0;
Democracy::<T>::vote(RawOrigin::Signed(caller.clone()).into(), ref_index, account_vote)?;
Expand Down Expand Up @@ -654,6 +660,8 @@ benchmarks! {
let caller_lookup = T::Lookup::unlookup(caller.clone());
let account_vote = account_vote::<T>(100u32.into());

T::DemocracyHooks::on_remove_vote_worst_case(&caller);

for i in 0 .. r {
let ref_index = add_referendum::<T>(i).0;
Democracy::<T>::vote(RawOrigin::Signed(caller.clone()).into(), ref_index, account_vote)?;
Expand Down
12 changes: 12 additions & 0 deletions pallets/democracy/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ use frame_support::dispatch::DispatchResult;
pub trait DemocracyHooks<AccountId, Balance> {
fn on_vote(who: &AccountId, ref_index: ReferendumIndex, vote: AccountVote<Balance>) -> 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<AccountId, Balance> DemocracyHooks<AccountId, Balance> for () {
Expand All @@ -14,4 +20,10 @@ impl<AccountId, Balance> DemocracyHooks<AccountId, Balance> 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) {}
}
9 changes: 5 additions & 4 deletions pallets/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",]
34 changes: 33 additions & 1 deletion pallets/staking/src/integrations/democracy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(sp_std::marker::PhantomData<T>);

impl<T: Config> DemocracyHooks<T::AccountId, Balance> for StakingDemocracy<T> {
impl<T: Config> DemocracyHooks<T::AccountId, Balance> for StakingDemocracy<T>
where
T::Currency: MultiCurrencyExtended<T::AccountId, Amount = i128>,
{
fn on_vote(who: &T::AccountId, ref_index: ReferendumIndex, vote: AccountVote<Balance>) -> DispatchResult {
let position_id = if let Some(position_id) = Pallet::<T>::get_user_position_id(who)? {
position_id
Expand Down Expand Up @@ -74,6 +80,32 @@ impl<T: Config> DemocracyHooks<T::AccountId, Balance> for StakingDemocracy<T> {

Ok(())
}

#[cfg(feature = "runtime-benchmarks")]
fn on_vote_worst_case(who: &T::AccountId) {
T::Currency::update_balance(
T::HdxAssetId::get(),
&Pallet::<T>::pot_account_id(),
10_000_000_000_000i128,
)
.unwrap();
Pallet::<T>::initialize_staking(Origin::<T>::Root.into()).unwrap();
T::Currency::update_balance(T::HdxAssetId::get(), who, 1000_000_000_000_000i128).unwrap();
Pallet::<T>::stake(Origin::<T>::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::<T>::pot_account_id(),
10_000_000_000_000i128,
)
.unwrap();
Pallet::<T>::initialize_staking(Origin::<T>::Root.into()).unwrap();
T::Currency::update_balance(T::HdxAssetId::get(), who, 1000_000_000_000_000i128).unwrap();
Pallet::<T>::stake(Origin::<T>::Signed(who.clone()).into(), 1000_000_000_000_000u128).unwrap();
}
}

pub struct ReferendumStatus<T>(sp_std::marker::PhantomData<T>);
Expand Down
1 change: 1 addition & 0 deletions scripts/benchmark.all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 2059b03

Please sign in to comment.