Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Fix democracy on-initialize weight #9890

Merged
8 commits merged into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions frame/democracy/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,38 @@ benchmarks! {
}
}

on_initialize_base_with_launch_period {
let r in 1 .. MAX_REFERENDUMS;

for i in 0..r {
add_referendum::<T>(i)?;
}

for (key, mut info) in ReferendumInfoOf::<T>::iter() {
if let ReferendumInfo::Ongoing(ref mut status) = info {
status.end += 100u32.into();
}
ReferendumInfoOf::<T>::insert(key, info);
}

assert_eq!(Democracy::<T>::referendum_count(), r, "referenda not created");
assert_eq!(Democracy::<T>::lowest_unbaked(), 0, "invalid referenda init");

let block_number = T::LaunchPeriod::get();

}: { Democracy::<T>::on_initialize(block_number) }
verify {
// All should be on going
for i in 0 .. r {
if let Some(value) = ReferendumInfoOf::<T>::get(i) {
match value {
ReferendumInfo::Finished { .. } => return Err("Referendum has been finished".into()),
ReferendumInfo::Ongoing(_) => (),
}
}
}
}

delegate {
let r in 1 .. MAX_REFERENDUMS;

Expand Down
25 changes: 16 additions & 9 deletions frame/democracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1726,7 +1726,8 @@ impl<T: Config> Pallet<T> {
///
///
/// # <weight>
/// If a referendum is launched or maturing, this will take full block weight. Otherwise:
/// If a referendum is launched or maturing, this will take full block weight if queue is not
/// empty. Otherwise:
/// - Complexity: `O(R)` where `R` is the number of unbaked referenda.
/// - Db reads: `LastTabledWasExternal`, `NextExternal`, `PublicProps`, `account`,
/// `ReferendumCount`, `LowestUnbaked`
Expand All @@ -1737,18 +1738,24 @@ impl<T: Config> Pallet<T> {
let max_block_weight = T::BlockWeights::get().max_block;
let mut weight = 0;

let next = Self::lowest_unbaked();
let last = Self::referendum_count();
let r = last.saturating_sub(next);

// pick out another public referendum if it's time.
if (now % T::LaunchPeriod::get()).is_zero() {
// Errors come from the queue being empty. we don't really care about that, and even if
// we did, there is nothing we can do here.
let _ = Self::launch_next(now);
weight = max_block_weight;
// Errors come from the queue being empty. If the queue is not empty, it will take
// full block weight.
if Self::launch_next(now).is_ok() {
weight = max_block_weight;
} else {
weight =
weight.saturating_add(T::WeightInfo::on_initialize_base_with_launch_period(r));
}
} else {
weight = weight.saturating_add(T::WeightInfo::on_initialize_base(r));
}

let next = Self::lowest_unbaked();
let last = Self::referendum_count();
let r = last.saturating_sub(next);
weight = weight.saturating_add(T::WeightInfo::on_initialize_base(r));
// tally up votes for any expiring referenda.
for (index, info) in Self::maturing_referenda_at_inner(now, next..last).into_iter() {
let approved = Self::bake_referendum(now, index, info)?;
Expand Down
Loading