From 394666eec4587d5918fe3fdaacc2d14bbb36c912 Mon Sep 17 00:00:00 2001 From: 4meta5 Date: Wed, 9 Nov 2022 00:51:06 -0500 Subject: [PATCH 1/4] init --- frame/referenda/src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++ frame/referenda/src/types.rs | 24 +++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/frame/referenda/src/lib.rs b/frame/referenda/src/lib.rs index d060c3db3fa70..770fd2780496e 100644 --- a/frame/referenda/src/lib.rs +++ b/frame/referenda/src/lib.rs @@ -701,6 +701,48 @@ impl, I: 'static> Pallet { } } + /// Return the current approval, support requirements and the current tally + pub fn referendum_threshold(ref_index: ReferendumIndex) -> () { + let info = + if let Some(i) = ReferendumInfoFor::::get(ref_index) { i } else { return false }; + let track = if let Some(t) = Self::track(status.track) { t } else { return false }; + let elapsed = if let Some(deciding) = status.deciding { + frame_system::Pallet::::block_number().saturating_sub(deciding.since) + } else { + Zero::zero() + }; + let period = track.decision_period; + let x = Perbill::from_rational(elapsed.min(period), period); + // TODO + () + }// TODO: referendum_decision_time to expose fn decision_time + + /// Return whether the given referendum is in a passing state or already approved + pub fn is_referendum_passing(ref_index: ReferendumIndex) -> bool { + let info = + if let Some(i) = ReferendumInfoFor::::get(ref_index) { i } else { return false }; + match info { + ReferendumInfo::Ongoing(status) => { + let track = if let Some(t) = Self::track(status.track) { t } else { return false }; + let elapsed = if let Some(deciding) = status.deciding { + frame_system::Pallet::::block_number().saturating_sub(deciding.since) + } else { + Zero::zero() + }; + Self::is_passing( + &status.tally, + elapsed, + track.decision_period, + &track.min_support, + &track.min_approval, + status.track, + ) + }, + ReferendumInfo::Approved(..) => true, + _ => false, + } + } + // Enqueue a proposal from a referendum which has presumably passed. fn schedule_enactment( index: ReferendumIndex, diff --git a/frame/referenda/src/types.rs b/frame/referenda/src/types.rs index 48db0847edf2e..5b265fdaf3e01 100644 --- a/frame/referenda/src/types.rs +++ b/frame/referenda/src/types.rs @@ -195,6 +195,30 @@ pub struct ReferendumStatus< pub(crate) alarm: Option<(Moment, ScheduleAddress)>, } +impl< + TrackId: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, + RuntimeOrigin: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, + Moment: Parameter + Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone + EncodeLike, + Call: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, + Balance: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, + Tally: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, + AccountId: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, + ScheduleAddress: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, + > + ReferendumStatus +{ + pub fn is_deciding(&self) -> bool { + self.deciding.is_some() + } + pub fn is_confirming(&self) -> bool { + if let Some(deciding_status) = &self.deciding { + deciding_status.confirming.is_some() + } else { + false + } + } +} + /// Info regarding a referendum, present or past. #[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] pub enum ReferendumInfo< From a903666f4a45ed53f5efb9a1a263dfaf6077d757 Mon Sep 17 00:00:00 2001 From: 4meta5 Date: Wed, 9 Nov 2022 12:25:57 -0500 Subject: [PATCH 2/4] clean --- frame/referenda/src/lib.rs | 50 ++++++++++++++++++------------------ frame/referenda/src/types.rs | 3 +++ 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/frame/referenda/src/lib.rs b/frame/referenda/src/lib.rs index 770fd2780496e..65dc7d191736f 100644 --- a/frame/referenda/src/lib.rs +++ b/frame/referenda/src/lib.rs @@ -701,45 +701,45 @@ impl, I: 'static> Pallet { } } - /// Return the current approval, support requirements and the current tally - pub fn referendum_threshold(ref_index: ReferendumIndex) -> () { - let info = - if let Some(i) = ReferendumInfoFor::::get(ref_index) { i } else { return false }; - let track = if let Some(t) = Self::track(status.track) { t } else { return false }; - let elapsed = if let Some(deciding) = status.deciding { - frame_system::Pallet::::block_number().saturating_sub(deciding.since) - } else { - Zero::zero() - }; - let period = track.decision_period; - let x = Perbill::from_rational(elapsed.min(period), period); - // TODO - () - }// TODO: referendum_decision_time to expose fn decision_time - - /// Return whether the given referendum is in a passing state or already approved - pub fn is_referendum_passing(ref_index: ReferendumIndex) -> bool { - let info = - if let Some(i) = ReferendumInfoFor::::get(ref_index) { i } else { return false }; + /// Returns the referendum's current approval/support and TrackId. + /// Referendum must be ongoing. + pub fn current_referendum_threshold( + ref_index: ReferendumIndex, + ) -> Result<(Perbill, Perbill, Option, TrackIdOf), DispatchError> { + let info = ReferendumInfoFor::::get(ref_index).ok_or(Error::::BadReferendum)?; match info { ReferendumInfo::Ongoing(status) => { - let track = if let Some(t) = Self::track(status.track) { t } else { return false }; + let maybe_since = if let Some(d) = status.deciding { Some(d.since) } else { None }; + let (current_approval, current_support) = + (status.tally.approval(status.track), status.tally.support(status.track)); + Ok((current_approval, current_support, maybe_since, status.track)) + }, + _ => Err(Error::::NotOngoing.into()), + } + } + + /// Returns whether the referendum is passing. + /// Referendum must be ongoing and its track must exist. + pub fn is_referendum_passing(ref_index: ReferendumIndex) -> Result { + let info = ReferendumInfoFor::::get(ref_index).ok_or(Error::::BadReferendum)?; + match info { + ReferendumInfo::Ongoing(status) => { + let track = Self::track(status.track).ok_or(Error::::NoTrack)?; let elapsed = if let Some(deciding) = status.deciding { frame_system::Pallet::::block_number().saturating_sub(deciding.since) } else { Zero::zero() }; - Self::is_passing( + Ok(Self::is_passing( &status.tally, elapsed, track.decision_period, &track.min_support, &track.min_approval, status.track, - ) + )) }, - ReferendumInfo::Approved(..) => true, - _ => false, + _ => Err(Error::::NotOngoing.into()), } } diff --git a/frame/referenda/src/types.rs b/frame/referenda/src/types.rs index 5b265fdaf3e01..4b91dc9f60038 100644 --- a/frame/referenda/src/types.rs +++ b/frame/referenda/src/types.rs @@ -207,9 +207,12 @@ impl< > ReferendumStatus { + /// Return whether referendum status is deciding. + /// confirming => deciding so deciding does not preclude confirming pub fn is_deciding(&self) -> bool { self.deciding.is_some() } + /// Return whether referendum status is confirming. pub fn is_confirming(&self) -> bool { if let Some(deciding_status) = &self.deciding { deciding_status.confirming.is_some() From 8421d34c529d6bf7c12b1c59da2e017223649287 Mon Sep 17 00:00:00 2001 From: 4meta5 Date: Wed, 9 Nov 2022 21:57:44 -0500 Subject: [PATCH 3/4] remove manual getter for ReferendumStatus in favor of changing pub crate to pub for ReferendumStatus DecidingStatus Deposit types --- frame/referenda/src/lib.rs | 17 ----------------- frame/referenda/src/types.rs | 30 +++++++++++++++--------------- 2 files changed, 15 insertions(+), 32 deletions(-) diff --git a/frame/referenda/src/lib.rs b/frame/referenda/src/lib.rs index 65dc7d191736f..ba5f4aec956b1 100644 --- a/frame/referenda/src/lib.rs +++ b/frame/referenda/src/lib.rs @@ -701,23 +701,6 @@ impl, I: 'static> Pallet { } } - /// Returns the referendum's current approval/support and TrackId. - /// Referendum must be ongoing. - pub fn current_referendum_threshold( - ref_index: ReferendumIndex, - ) -> Result<(Perbill, Perbill, Option, TrackIdOf), DispatchError> { - let info = ReferendumInfoFor::::get(ref_index).ok_or(Error::::BadReferendum)?; - match info { - ReferendumInfo::Ongoing(status) => { - let maybe_since = if let Some(d) = status.deciding { Some(d.since) } else { None }; - let (current_approval, current_support) = - (status.tally.approval(status.track), status.tally.support(status.track)); - Ok((current_approval, current_support, maybe_since, status.track)) - }, - _ => Err(Error::::NotOngoing.into()), - } - } - /// Returns whether the referendum is passing. /// Referendum must be ongoing and its track must exist. pub fn is_referendum_passing(ref_index: ReferendumIndex) -> Result { diff --git a/frame/referenda/src/types.rs b/frame/referenda/src/types.rs index 4b91dc9f60038..8cb3a66c3ccc1 100644 --- a/frame/referenda/src/types.rs +++ b/frame/referenda/src/types.rs @@ -101,16 +101,16 @@ impl> InsertSorted for BoundedVec { pub struct DecidingStatus { /// When this referendum began being "decided". If confirming, then the /// end will actually be delayed until the end of the confirmation period. - pub(crate) since: BlockNumber, + pub since: BlockNumber, /// If `Some`, then the referendum has entered confirmation stage and will end at /// the block number as long as it doesn't lose its approval in the meantime. - pub(crate) confirming: Option, + pub confirming: Option, } #[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] pub struct Deposit { - pub(crate) who: AccountId, - pub(crate) amount: Balance, + pub who: AccountId, + pub amount: Balance, } #[derive(Clone, Encode, TypeInfo)] @@ -171,28 +171,28 @@ pub struct ReferendumStatus< ScheduleAddress: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, > { /// The track of this referendum. - pub(crate) track: TrackId, + pub track: TrackId, /// The origin for this referendum. - pub(crate) origin: RuntimeOrigin, + pub origin: RuntimeOrigin, /// The hash of the proposal up for referendum. - pub(crate) proposal: Call, + pub proposal: Call, /// The time the proposal should be scheduled for enactment. - pub(crate) enactment: DispatchTime, + pub enactment: DispatchTime, /// The time of submission. Once `UndecidingTimeout` passes, it may be closed by anyone if /// `deciding` is `None`. - pub(crate) submitted: Moment, + pub submitted: Moment, /// The deposit reserved for the submission of this referendum. - pub(crate) submission_deposit: Deposit, + pub submission_deposit: Deposit, /// The deposit reserved for this referendum to be decided. - pub(crate) decision_deposit: Option>, + pub decision_deposit: Option>, /// The status of a decision being made. If `None`, it has not entered the deciding period. - pub(crate) deciding: Option>, + pub deciding: Option>, /// The current tally of votes in this referendum. - pub(crate) tally: Tally, + pub tally: Tally, /// Whether we have been placed in the queue for being decided or not. - pub(crate) in_queue: bool, + pub in_queue: bool, /// The next scheduled wake-up, if `Some`. - pub(crate) alarm: Option<(Moment, ScheduleAddress)>, + pub alarm: Option<(Moment, ScheduleAddress)>, } impl< From b3ef2b1512bd36882727078d3f1c72d429ede5dc Mon Sep 17 00:00:00 2001 From: 4meta5 Date: Wed, 9 Nov 2022 22:09:59 -0500 Subject: [PATCH 4/4] rm status getters because fields are pub now --- frame/referenda/src/types.rs | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/frame/referenda/src/types.rs b/frame/referenda/src/types.rs index 8cb3a66c3ccc1..a97faca3bbfc2 100644 --- a/frame/referenda/src/types.rs +++ b/frame/referenda/src/types.rs @@ -195,33 +195,6 @@ pub struct ReferendumStatus< pub alarm: Option<(Moment, ScheduleAddress)>, } -impl< - TrackId: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, - RuntimeOrigin: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, - Moment: Parameter + Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone + EncodeLike, - Call: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, - Balance: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, - Tally: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, - AccountId: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, - ScheduleAddress: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, - > - ReferendumStatus -{ - /// Return whether referendum status is deciding. - /// confirming => deciding so deciding does not preclude confirming - pub fn is_deciding(&self) -> bool { - self.deciding.is_some() - } - /// Return whether referendum status is confirming. - pub fn is_confirming(&self) -> bool { - if let Some(deciding_status) = &self.deciding { - deciding_status.confirming.is_some() - } else { - false - } - } -} - /// Info regarding a referendum, present or past. #[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] pub enum ReferendumInfo<