diff --git a/substrate/frame/referenda/src/lib.rs b/substrate/frame/referenda/src/lib.rs index 054d3775a6680..6229967ab3e74 100644 --- a/substrate/frame/referenda/src/lib.rs +++ b/substrate/frame/referenda/src/lib.rs @@ -628,10 +628,7 @@ pub mod pallet { ensure_root(origin)?; let now = frame_system::Pallet::::block_number(); - let mut status = match ReferendumInfoFor::::get(index) { - Some(ReferendumInfo::Ongoing(status)) => Ok(status), - _ => Err(Error::::NotOngoing), - }?; + let mut status = Self::ensure_ongoing(index)?; // This is our wake-up, so we can disregard the alarm. status.alarm = None; let (info, dirty, branch) = Self::service_referendum(now, index, status); @@ -744,6 +741,19 @@ pub mod pallet { } } +#[inline] +fn is_finalizing, I: 'static>(status: &ReferendumStatusOf) { + status + .clone() + .deciding + .map(|d| { + d.confirming + .map(|x| frame_system::Pallet::::block_number() >= x) + .unwrap_or(false) + }) + .unwrap_or(false) +} + impl, I: 'static> Polling for Pallet { type Index = ReferendumIndex; type Votes = VotesOf; @@ -760,13 +770,7 @@ impl, I: 'static> Polling for Pallet { ) -> R { match ReferendumInfoFor::::get(index) { Some(ReferendumInfo::Ongoing(mut status)) => { - let now = frame_system::Pallet::::block_number(); - let is_finalizing = status - .clone() - .deciding - .map(|d| d.confirming.map(|x| now >= x).unwrap_or(false)) - .unwrap_or(false); - if is_finalizing { + if is_finalizing(&status) { return f(PollStatus::None); } @@ -789,13 +793,7 @@ impl, I: 'static> Polling for Pallet { ) -> Result { match ReferendumInfoFor::::get(index) { Some(ReferendumInfo::Ongoing(mut status)) => { - let now = frame_system::Pallet::::block_number(); - let is_finalizing = status - .clone() - .deciding - .map(|d| d.confirming.map(|x| now >= x).unwrap_or(false)) - .unwrap_or(false); - if is_finalizing { + if is_finalizing(&status) { return f(PollStatus::None); } @@ -876,16 +874,7 @@ impl, I: 'static> Pallet { index: ReferendumIndex, ) -> Result, DispatchError> { match ReferendumInfoFor::::get(index) { - Some(ReferendumInfo::Ongoing(status)) => { - match status.clone().deciding.map(|d| d.confirming) { - Some(Some(confirming)) - if frame_system::Pallet::::block_number() >= confirming => - { - Err(Error::::NotOngoing.into()) - }, - _ => Ok(status), - } - }, + Some(ReferendumInfo::Ongoing(status)) => Ok(status), _ => Err(Error::::NotOngoing.into()), } } @@ -946,8 +935,8 @@ impl, I: 'static> Pallet { let alarm_interval = T::AlarmInterval::get().max(One::one()); // Alarm must go off no earlier than `when`. // This rounds `when` upwards to the next multiple of `alarm_interval`. - let when = (when.saturating_add(alarm_interval.saturating_sub(One::one())) - / alarm_interval) + let when = (when.saturating_add(alarm_interval.saturating_sub(One::one())) / + alarm_interval) .saturating_mul(alarm_interval); let result = T::Scheduler::schedule( DispatchTime::At(when), @@ -1060,7 +1049,7 @@ impl, I: 'static> Pallet { Ok(c) => c, Err(_) => { debug_assert!(false, "Unable to create a bounded call from `one_fewer_deciding`??",); - return; + return }, }; Self::set_alarm(call, next_block); @@ -1087,7 +1076,7 @@ impl, I: 'static> Pallet { false, "Unable to create a bounded call from `nudge_referendum`??", ); - return false; + return false }, }; status.alarm = Self::set_alarm(call, alarm); @@ -1201,7 +1190,7 @@ impl, I: 'static> Pallet { ), true, ServiceBranch::TimedOut, - ); + ) } }, Some(deciding) => { @@ -1230,7 +1219,7 @@ impl, I: 'static> Pallet { now.saturating_less_one(), is_passing, ); - return Self::service_auction(now, index, status); + return Self::wait_for_candle(now, index, status); }, Some(_) => { // We don't care if failing within confirm period. @@ -1272,7 +1261,7 @@ impl, I: 'static> Pallet { now.saturating_less_one(), is_passing, ); - return Self::service_auction(now, index, status); + return Self::wait_for_candle(now, index, status); }, Some(_) => { // We don't care if failing within confirm period. @@ -1326,7 +1315,7 @@ impl, I: 'static> Pallet { /// The "candle": passing or failing of a referendum is ultimately decided as a candle auction /// where, given a random point in time (defined as `t`), the definitive status of the the /// referendum is decided by the last status registered before `t`. - fn service_auction( + fn wait_for_candle( now: BlockNumberFor, index: ReferendumIndex, mut status: ReferendumStatusOf, @@ -1340,9 +1329,9 @@ impl, I: 'static> Pallet { let confirming_until = status .clone() .deciding - .expect("having passed ongoing, we should have times for decision; qed") + .expect("called after having deciding, we have deciding.since time; qed") .confirming - .expect("having finished confirming, we should have a confirming_until time; qed"); + .expect("called after finished confirming, we have a confirming_until time; qed"); let confirming_since = confirming_until.saturating_sub(track.confirm_period); @@ -1370,9 +1359,12 @@ impl, I: 'static> Pallet { let statuses = PassingStatusInConfirmPeriod::::get(index); let statuses = statuses.range((Included(&confirming_since), Included(&candle_block_number))); - let (_, winning_status) = statuses.last().unwrap_or((&now, &true)); + let (_, is_confirmed) = statuses.last().unwrap_or((&now, &true)); + + // Cleanup passing status + PassingStatusInConfirmPeriod::::remove(index); - if *winning_status { + if *is_confirmed { // Passed! Self::ensure_no_alarm(&mut status); Self::note_one_fewer_deciding(status.track);