Skip to content

Commit

Permalink
Include submitting origin in PollStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
olanod committed Jul 6, 2024
1 parent 221eddc commit 3a90bfd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion substrate/frame/conviction-voting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
Error::<T, I>::InsufficientFunds
);
T::Polls::try_access_poll(poll_index, |poll_status| {
let (tally, class) = poll_status.ensure_ongoing().ok_or(Error::<T, I>::NotOngoing)?;
let (tally, class, _) = poll_status.ensure_ongoing().ok_or(Error::<T, I>::NotOngoing)?;
VotingFor::<T, I>::try_mutate(who, &class, |voting| {
if let Voting::Casting(Casting { ref mut votes, delegations, .. }) = voting {
match votes.binary_search_by_key(&poll_index, |i| i.0) {
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/ranked-collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ pub mod pallet {
match status {
PollStatus::None | PollStatus::Completed(..) =>
Err(Error::<T, I>::NotPolling)?,
PollStatus::Ongoing(ref mut tally, class) => {
PollStatus::Ongoing(ref mut tally, class, _) => {
match Voting::<T, I>::get(&poll, &who) {
Some(Aye(votes)) => {
tally.bare_ayes.saturating_dec();
Expand Down
13 changes: 9 additions & 4 deletions substrate/frame/referenda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,18 +743,22 @@ impl<T: Config<I>, I: 'static> Polling<T::Tally> for Pallet<T, I> {
type Votes = VotesOf<T, I>;
type Moment = BlockNumberFor<T>;
type Class = TrackIdOf<T, I>;
type Who = PalletsOriginOf<T>;

fn classes() -> Vec<Self::Class> {
T::Tracks::tracks().iter().map(|x| x.0).collect()
}

fn access_poll<R>(
index: Self::Index,
f: impl FnOnce(PollStatus<&mut T::Tally, BlockNumberFor<T>, TrackIdOf<T, I>>) -> R,
f: impl FnOnce(
PollStatus<&mut T::Tally, BlockNumberFor<T>, TrackIdOf<T, I>, &PalletsOriginOf<T>>,
) -> R,
) -> R {
match ReferendumInfoFor::<T, I>::get(index) {
Some(ReferendumInfo::Ongoing(mut status)) => {
let result = f(PollStatus::Ongoing(&mut status.tally, status.track));
let result =
f(PollStatus::Ongoing(&mut status.tally, status.track, &status.origin));
let now = frame_system::Pallet::<T>::block_number();
Self::ensure_alarm_at(&mut status, index, now + One::one());
ReferendumInfoFor::<T, I>::insert(index, ReferendumInfo::Ongoing(status));
Expand All @@ -769,12 +773,13 @@ impl<T: Config<I>, I: 'static> Polling<T::Tally> for Pallet<T, I> {
fn try_access_poll<R>(
index: Self::Index,
f: impl FnOnce(
PollStatus<&mut T::Tally, BlockNumberFor<T>, TrackIdOf<T, I>>,
PollStatus<&mut T::Tally, BlockNumberFor<T>, TrackIdOf<T, I>, &PalletsOriginOf<T>>,
) -> Result<R, DispatchError>,
) -> Result<R, DispatchError> {
match ReferendumInfoFor::<T, I>::get(index) {
Some(ReferendumInfo::Ongoing(mut status)) => {
let result = f(PollStatus::Ongoing(&mut status.tally, status.track))?;
let result =
f(PollStatus::Ongoing(&mut status.tally, status.track, &status.origin))?;
let now = frame_system::Pallet::<T>::block_number();
Self::ensure_alarm_at(&mut status, index, now + One::one());
ReferendumInfoFor::<T, I>::insert(index, ReferendumInfo::Ongoing(status));
Expand Down
17 changes: 10 additions & 7 deletions substrate/frame/support/src/traits/voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ pub trait VoteTally<Votes, Class> {
/// users.
fn setup(class: Class, granularity: Perbill);
}
pub enum PollStatus<Tally, Moment, Class> {
pub enum PollStatus<Tally, Moment, Class, Who> {
None,
Ongoing(Tally, Class),
Ongoing(Tally, Class, Who),
Completed(Moment, bool),
}

impl<Tally, Moment, Class> PollStatus<Tally, Moment, Class> {
pub fn ensure_ongoing(self) -> Option<(Tally, Class)> {
impl<Tally, Moment, Class, Who> PollStatus<Tally, Moment, Class, Who> {
pub fn ensure_ongoing(self) -> Option<(Tally, Class, Who)> {
match self {
Self::Ongoing(t, c) => Some((t, c)),
Self::Ongoing(t, c, w) => Some((t, c, w)),
_ => None,
}
}
Expand All @@ -85,6 +85,7 @@ pub trait Polling<Tally> {
type Index: Parameter + Member + Ord + PartialOrd + Copy + HasCompact + MaxEncodedLen;
type Votes: Parameter + Member + Ord + PartialOrd + Copy + HasCompact + MaxEncodedLen;
type Class: Parameter + Member + Ord + PartialOrd + MaxEncodedLen;
type Who;
type Moment;

/// Provides a vec of values that `T` may take.
Expand All @@ -98,12 +99,14 @@ pub trait Polling<Tally> {

fn access_poll<R>(
index: Self::Index,
f: impl FnOnce(PollStatus<&mut Tally, Self::Moment, Self::Class>) -> R,
f: impl FnOnce(PollStatus<&mut Tally, Self::Moment, Self::Class, &Self::Who>) -> R,
) -> R;

fn try_access_poll<R>(
index: Self::Index,
f: impl FnOnce(PollStatus<&mut Tally, Self::Moment, Self::Class>) -> Result<R, DispatchError>,
f: impl FnOnce(
PollStatus<&mut Tally, Self::Moment, Self::Class, &Self::Who>,
) -> Result<R, DispatchError>,
) -> Result<R, DispatchError>;

/// Create an ongoing majority-carries poll of given class lasting given period for the purpose
Expand Down

0 comments on commit 3a90bfd

Please sign in to comment.