Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add base weight on LaunchPeriod
  • Loading branch information
zjb0807 committed Sep 29, 2021
commit d1849d6af4ccf0d51c9ec3ccb7985b604ea7a67e
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
12 changes: 8 additions & 4 deletions frame/democracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1738,19 +1738,23 @@ 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. 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