Skip to content

Commit

Permalink
Benchmarking: Calculate weights for pallet-communities (#352)
Browse files Browse the repository at this point in the history
* [ci] calculate weights

* change(pallet-communities): weights sanity checks

* fix(pallet-communities): on mock, adjust scheduler weight, to make sure passes scheduling add_member tests with more realistic values.

* fix(pallet-communities): adjust governance tests since more scheduled tasks are allowed per block in mock

---------

Co-authored-by: pandres95 <[email protected]>
Co-authored-by: Pablo Andrés Dorado Suárez <[email protected]>
  • Loading branch information
3 people authored Apr 1, 2024
1 parent e13ff0a commit 328e540
Show file tree
Hide file tree
Showing 7 changed files with 467 additions and 265 deletions.
21 changes: 16 additions & 5 deletions pallets/communities/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use frame_support::{
fungible::HoldConsideration, tokens::nonfungible_v2::ItemOf, AsEnsureOriginWithArg, ConstU128, ConstU16,
ConstU32, ConstU64, EitherOf, EnsureOriginWithArg, EqualPrivilegeOnly, Footprint, OriginTrait,
},
weights::Weight,
weights::{constants::WEIGHT_REF_TIME_PER_NANOS, constants::WEIGHT_REF_TIME_PER_SECOND, Weight},
PalletId,
};
use frame_system::{EnsureRoot, EnsureRootWithSuccess, EnsureSigned};
Expand All @@ -15,7 +15,7 @@ use sp_core::H256;
use sp_io::TestExternalities;
use sp_runtime::{
traits::{BlakeTwo256, Convert, IdentifyAccount, IdentityLookup, Verify},
BuildStorage, MultiSignature,
BuildStorage, MultiSignature, Perbill,
};
pub use virto_common::{CommunityId, MembershipId};

Expand All @@ -25,6 +25,19 @@ use crate::{
types::{Tally, VoteWeight},
};

// Weights constants

// max block: 0.5s compute with 12s average block time
pub const MAX_BLOCK_REF_TIME: u64 = WEIGHT_REF_TIME_PER_SECOND.saturating_div(2); // https://github.com/paritytech/cumulus/blob/98e68bd54257b4039a5d5b734816f4a1b7c83a9d/parachain-template/runtime/src/lib.rs#L221
pub const MAX_BLOCK_POV_SIZE: u64 = 5 * 1024 * 1024; // https://github.com/paritytech/polkadot/blob/ba1f65493d91d4ab1787af2fd6fe880f1da90586/primitives/src/v4/mod.rs#L384
pub const MAX_BLOCK_WEIGHT: Weight = Weight::from_parts(MAX_BLOCK_REF_TIME, MAX_BLOCK_POV_SIZE);
// max extrinsics: 75% of block
pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); // https://github.com/paritytech/cumulus/blob/d20c4283fe85df0c1ef8cb7c9eb7c09abbcbfa31/parachain-template/runtime/src/lib.rs#L218
// max extrinsic: max total extrinsics less average on_initialize ratio and less
// base extrinsic weight
pub const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(5); // https://github.com/paritytech/cumulus/blob/d20c4283fe85df0c1ef8cb7c9eb7c09abbcbfa31/parachain-template/runtime/src/lib.rs#L214
pub const BASE_EXTRINSIC: Weight = Weight::from_parts(WEIGHT_REF_TIME_PER_NANOS.saturating_mul(125_000), 0); // https://github.com/paritytech/cumulus/blob/d20c4283fe85df0c1ef8cb7c9eb7c09abbcbfa31/parachain-template/runtime/src/weights/extrinsic_weights.rs#L26

type Block = frame_system::mocking::MockBlock<Test>;
type WeightInfo = ();

Expand Down Expand Up @@ -167,9 +180,8 @@ impl pallet_nfts::Config for Test {
}

// Governance at Communities

parameter_types! {
pub MaximumSchedulerWeight: Weight = Weight::from_parts(1_000_000_000, 1_048_576);
pub MaximumSchedulerWeight: Weight = Weight::from_parts(MAX_BLOCK_REF_TIME, MAX_BLOCK_POV_SIZE);
pub const MaxScheduledPerBlock: u32 = 512;
}
pub struct ConvertDeposit;
Expand Down Expand Up @@ -296,7 +308,6 @@ use {
frame_system::pallet_prelude::{OriginFor, RuntimeCallFor},
pallet_referenda::{BoundedCallOf, Curve, PalletsOriginOf, TrackInfo},
parity_scale_codec::Encode,
sp_runtime::Perbill,
};

#[cfg(feature = "runtime-benchmarks")]
Expand Down
12 changes: 3 additions & 9 deletions pallets/communities/src/tests/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,6 @@ mod vote {
DecisionMethod::Membership
));

tick_block();

// Before voting, the poll is ongoing
System::assert_has_event(
pallet_referenda::Event::<Test>::DecisionStarted {
Expand Down Expand Up @@ -594,7 +592,7 @@ mod vote {

System::assert_has_event(pallet_referenda::Event::<Test>::ConfirmStarted { index: 1 }.into());

tick_blocks(2);
tick_block();

System::assert_has_event(
pallet_referenda::Event::<Test>::Confirmed {
Expand Down Expand Up @@ -655,7 +653,7 @@ mod vote {
Vote::AssetBalance(true, COMMUNITY_B_ASSET_ID, 8)
));

tick_blocks(4);
tick_blocks(3);

System::assert_has_event(
pallet_referenda::Event::<Test>::Confirmed {
Expand Down Expand Up @@ -714,8 +712,6 @@ mod vote {
#[test]
fn rejects_on_most_nays() {
new_test_ext().execute_with(|| {
tick_block();

// Before voting, the poll is ongoing
System::assert_has_event(
pallet_referenda::Event::<Test>::DecisionStarted {
Expand Down Expand Up @@ -769,8 +765,6 @@ mod vote {
#[test]
fn voter_can_change_decision() {
new_test_ext().execute_with(|| {
tick_block();

// Before voting, the poll is ongoing
System::assert_has_event(
pallet_referenda::Event::<Test>::DecisionStarted {
Expand Down Expand Up @@ -907,7 +901,7 @@ mod vote {
Vote::Standard(true)
));

tick_blocks(3);
tick_blocks(2);

System::assert_has_event(pallet_referenda::Event::<Test>::ConfirmStarted { index: 3 }.into());
});
Expand Down
17 changes: 3 additions & 14 deletions pallets/communities/src/tests/weights.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
use crate::mock::Test;
use super::*;
use crate::weights::{SubstrateWeight, WeightInfo};
use frame_support::weights::{constants::WEIGHT_REF_TIME_PER_NANOS, constants::WEIGHT_REF_TIME_PER_SECOND, Weight};
use sp_runtime::Perbill;
use frame_support::weights::Weight;

#[test]
fn weights() {
// max block: 0.5s compute with 12s average block time
const MAX_BLOCK_REF_TIME: u64 = WEIGHT_REF_TIME_PER_SECOND.saturating_div(2); // https://github.com/paritytech/cumulus/blob/98e68bd54257b4039a5d5b734816f4a1b7c83a9d/parachain-template/runtime/src/lib.rs#L221
const MAX_BLOCK_POV_SIZE: u64 = 5 * 1024 * 1024; // https://github.com/paritytech/polkadot/blob/ba1f65493d91d4ab1787af2fd6fe880f1da90586/primitives/src/v4/mod.rs#L384
const MAX_BLOCK_WEIGHT: Weight = Weight::from_parts(MAX_BLOCK_REF_TIME, MAX_BLOCK_POV_SIZE);
// max extrinsics: 75% of block
const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); // https://github.com/paritytech/cumulus/blob/d20c4283fe85df0c1ef8cb7c9eb7c09abbcbfa31/parachain-template/runtime/src/lib.rs#L218
let max_total_extrinsics = MAX_BLOCK_WEIGHT * NORMAL_DISPATCH_RATIO;
// max extrinsic: max total extrinsics less average on_initialize ratio and less
// base extrinsic weight
const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(5); // https://github.com/paritytech/cumulus/blob/d20c4283fe85df0c1ef8cb7c9eb7c09abbcbfa31/parachain-template/runtime/src/lib.rs#L214
const BASE_EXTRINSIC: Weight = Weight::from_parts(WEIGHT_REF_TIME_PER_NANOS.saturating_mul(125_000), 0); // https://github.com/paritytech/cumulus/blob/d20c4283fe85df0c1ef8cb7c9eb7c09abbcbfa31/parachain-template/runtime/src/weights/extrinsic_weights.rs#L26

let max_extrinsic_weight = max_total_extrinsics
.saturating_sub(MAX_BLOCK_WEIGHT * AVERAGE_ON_INITIALIZE_RATIO)
.saturating_sub(BASE_EXTRINSIC);

assert_eq!(max_extrinsic_weight, Weight::from_parts(349_875_000_000, 3_670_016));

println!("max block weight: {MAX_BLOCK_WEIGHT}");
Expand Down
Loading

0 comments on commit 328e540

Please sign in to comment.