Skip to content

Commit

Permalink
Vote benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
ndkazu committed Jan 21, 2025
1 parent a00888d commit 67f1c19
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 6 deletions.
168 changes: 168 additions & 0 deletions substrate/frame/opf/src/benchmarking.rs
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);
}
7 changes: 5 additions & 2 deletions substrate/frame/opf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
//! - **TemporaryRewards:**For test purposes only ⇒ used as a substitute for the inflation portion
//! used for the rewards.
//! - **PotId:** Pot containing the funds used to pay the rewards.
//! - **ClaimingPeriod:**Period during which allocated funds can be claimed
//! - **ClaimingPeriod:**Period during which allocated funds can be claimed
//!
//! ## Interface
//!
Expand All @@ -57,6 +57,9 @@ mod types;
pub use pallet_democracy as Democracy;
pub use types::*;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

#[cfg(test)]
mod mock;

Expand Down Expand Up @@ -537,7 +540,7 @@ pub mod pallet {
WhiteListedProjectAccounts::<T>::remove(&project_id);
Ok(())
} else {
// Claimin before proposal enactment
// Claimin before proposal enactment
Err(Error::<T>::NotClaimingPeriod.into())
}
}
Expand Down
10 changes: 6 additions & 4 deletions substrate/frame/opf/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,12 @@ fn spends_creation_works_but_not_executed_after_claim_period() {

run_to_block(round_end);

// Claim does not work before proposal enactment
assert_noop!(Opf::claim_reward_for(RawOrigin::Signed(EVE).into(), 102), Error::<Test>::InexistentSpend);

// Claim does not work before proposal enactment
assert_noop!(
Opf::claim_reward_for(RawOrigin::Signed(EVE).into(), 102),
Error::<Test>::InexistentSpend
);

next_block();
now = <Test as Config>::BlockNumberProvider::current_block_number();
let expire = now.saturating_add(<Test as Config>::ClaimingPeriod::get());
Expand All @@ -548,6 +551,5 @@ fn spends_creation_works_but_not_executed_after_claim_period() {
expired_when: expire,
project_id: 102,
})]);

})
}

0 comments on commit 67f1c19

Please sign in to comment.