-
Notifications
You must be signed in to change notification settings - Fork 790
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
179 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) Parity Technologies (UK) Ltd. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//! OPF pallet benchmarking. | ||
#![cfg(feature = "runtime-benchmarks")] | ||
use super::*; | ||
|
||
use crate::{Democracy::Conviction, Pallet as Opf}; | ||
//use pallet_distribution as Distribution; | ||
use frame_benchmarking::{ | ||
v1::{account, BenchmarkError}, | ||
v2::*, | ||
}; | ||
use frame_support::ensure; | ||
use frame_system::RawOrigin; | ||
use sp_runtime::traits::One; | ||
|
||
const SEED: u32 = 0; | ||
|
||
fn run_to_block<T: Config>(n: BlockNumberFor<T>) { | ||
while frame_system::Pallet::<T>::block_number() < n { | ||
let b = frame_system::Pallet::<T>::block_number(); | ||
crate::Pallet::<T>::on_finalize(b); | ||
frame_system::Pallet::<T>::on_finalize(b); | ||
frame_system::Pallet::<T>::set_block_number(b + One::one()); | ||
frame_system::Pallet::<T>::on_initialize(b); | ||
crate::Pallet::<T>::on_initialize(b); | ||
} | ||
} | ||
|
||
fn on_idle_full_block<T: Config>() { | ||
let remaining_weight = <T as frame_system::Config>::BlockWeights::get().max_block; | ||
let when = frame_system::Pallet::<T>::block_number(); | ||
frame_system::Pallet::<T>::on_idle(when, remaining_weight); | ||
crate::Pallet::<T>::on_idle(when, remaining_weight); | ||
} | ||
|
||
fn add_whitelisted_project<T: Config>(n: u32, caller: T::AccountId) -> Result<(), &'static str> { | ||
let mut batch: Vec<_> = Vec::new(); | ||
for _i in 0..n { | ||
let project_id = account("project", n, SEED); | ||
batch.push(project_id); | ||
} | ||
let _ = crate::Pallet::<T>::register_projects_batch(RawOrigin::Signed(caller).into(), batch); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[benchmarks] | ||
mod benchmarks { | ||
use super::*; | ||
|
||
#[benchmark] | ||
fn vote(r: Linear<1, 1000>) -> Result<(), BenchmarkError> { | ||
let caller: T::AccountId = whitelisted_caller(); | ||
let account: T::AccountId = account("project", r, SEED); | ||
add_whitelisted_project::<T>(r, caller.clone())?; | ||
ensure!( | ||
WhiteListedProjectAccounts::<T>::contains_key(account.clone()) == true, | ||
"Project_id not set up correctly." | ||
); | ||
|
||
on_idle_full_block::<T>(); | ||
let when = frame_system::Pallet::<T>::block_number() + One::one(); | ||
run_to_block::<T>(when); | ||
|
||
ensure!(VotingRounds::<T>::get(0).is_some(), "Round not created!"); | ||
let caller_balance = T::NativeBalance::minimum_balance() * 100000000u32.into(); | ||
|
||
let _ = T::NativeBalance::mint_into(&caller, caller_balance); | ||
|
||
let value: BalanceOf<T> = T::NativeBalance::minimum_balance() * 10u32.into() * (r).into(); | ||
#[extrinsic_call] | ||
_(RawOrigin::Signed(caller.clone()), account, value, true, Conviction::Locked1x); | ||
|
||
Ok(()) | ||
} | ||
|
||
/*#[benchmark] | ||
fn remove_vote( | ||
r: Linear<1, { T::MaxWhitelistedProjects::get() }>, | ||
) -> Result<(), BenchmarkError> { | ||
add_whitelisted_project::<T>(r)?; | ||
ensure!( | ||
WhiteListedProjectAccounts::<T>::get().len() as u32 == r, | ||
"Project_id not set up correctly." | ||
); | ||
on_idle_full_block::<T>(); | ||
let when = T::BlockNumberProvider::current_block_number() + One::one(); | ||
run_to_block::<T>(when); | ||
ensure!(VotingRounds::<T>::get(0).is_some(), "Round not created!"); | ||
let caller_balance = T::NativeBalance::minimum_balance() * 10000u32.into(); | ||
let caller: T::AccountId = whitelisted_caller(); | ||
let _ = T::NativeBalance::mint_into(&caller, caller_balance); | ||
let account = WhiteListedProjectAccounts::<T>::get()[(r - 1) as usize].clone(); | ||
let value: BalanceOf<T> = T::NativeBalance::minimum_balance() * 100u32.into() * (r).into(); | ||
Opf::<T>::vote( | ||
RawOrigin::Signed(caller.clone()).into(), | ||
account.clone(), | ||
value, | ||
true, | ||
Conviction::Locked1x, | ||
)?; | ||
#[extrinsic_call] | ||
_(RawOrigin::Signed(caller.clone()), account); | ||
Ok(()) | ||
} | ||
#[benchmark] | ||
fn unlock_funds( | ||
r: Linear<1, { T::MaxWhitelistedProjects::get() }>, | ||
) -> Result<(), BenchmarkError> { | ||
add_whitelisted_project::<T>(r)?; | ||
ensure!( | ||
WhiteListedProjectAccounts::<T>::get().len() as u32 == r, | ||
"Project_id not set up correctly." | ||
); | ||
on_idle_full_block::<T>(); | ||
let mut when = T::BlockNumberProvider::current_block_number() + One::one(); | ||
run_to_block::<T>(when); | ||
ensure!(VotingRounds::<T>::get(0).is_some(), "Round not created!"); | ||
let caller_balance = T::NativeBalance::minimum_balance() * 1000000u32.into(); | ||
let caller: T::AccountId = whitelisted_caller(); | ||
let _ = T::NativeBalance::mint_into(&caller, caller_balance); | ||
let account = WhiteListedProjectAccounts::<T>::get()[(r - 1) as usize].clone(); | ||
let value: BalanceOf<T> = T::NativeBalance::minimum_balance() * 100u32.into() * (r).into(); | ||
Opf::<T>::vote( | ||
RawOrigin::Signed(caller.clone()).into(), | ||
account.clone(), | ||
value, | ||
true, | ||
Conviction::Locked1x, | ||
)?; | ||
when = Votes::<T>::get(account.clone(), caller.clone()).unwrap().funds_unlock_block; | ||
run_to_block::<T>(when); | ||
on_idle_full_block::<T>(); | ||
#[extrinsic_call] | ||
_(RawOrigin::Signed(caller.clone()), account); | ||
Ok(()) | ||
}*/ | ||
|
||
impl_benchmark_test_suite!(Opf, crate::mock::new_test_ext(), crate::mock::Test); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters