From f50beaa0d302525385b290819e15e6c8adfcc1da Mon Sep 17 00:00:00 2001 From: Daniel Olano Date: Fri, 5 Jul 2024 15:36:16 +0200 Subject: [PATCH] Include submitting origin in PollStatus --- prdoc/pr_4961.prdoc | 17 +++ substrate/frame/conviction-voting/src/lib.rs | 9 +- .../frame/conviction-voting/src/tests.rs | 104 +++++++++--------- substrate/frame/ranked-collective/src/lib.rs | 2 +- .../frame/ranked-collective/src/tests.rs | 28 +++-- substrate/frame/referenda/src/benchmarking.rs | 6 +- substrate/frame/referenda/src/lib.rs | 17 ++- substrate/frame/support/src/traits/voting.rs | 28 +++-- 8 files changed, 124 insertions(+), 87 deletions(-) create mode 100644 prdoc/pr_4961.prdoc diff --git a/prdoc/pr_4961.prdoc b/prdoc/pr_4961.prdoc new file mode 100644 index 0000000000000..78f37df0a9aff --- /dev/null +++ b/prdoc/pr_4961.prdoc @@ -0,0 +1,17 @@ +title: Include executing origin in PollStatus + +doc: + - audience: Runtime Dev + description: | + Adds an extra field to `PollStatus::Ongoing` variant `Origin`. It allows + users of the `Polling` trait know what origin is executing the poll. + +crates: + - name: frame-support + bump: minor + - name: pallet-referenda + bump: minor + - name: pallet-conviction-voting + bump: patch + - name: pallet-ranked-collective + bump: patch diff --git a/substrate/frame/conviction-voting/src/lib.rs b/substrate/frame/conviction-voting/src/lib.rs index fda97281f16bd..2d76347c62f35 100644 --- a/substrate/frame/conviction-voting/src/lib.rs +++ b/substrate/frame/conviction-voting/src/lib.rs @@ -411,7 +411,8 @@ impl, I: 'static> Pallet { Error::::InsufficientFunds ); T::Polls::try_access_poll(poll_index, |poll_status| { - let (tally, class) = poll_status.ensure_ongoing().ok_or(Error::::NotOngoing)?; + let (tally, class, _) = + poll_status.ensure_ongoing().ok_or(Error::::NotOngoing)?; VotingFor::::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) { @@ -469,7 +470,7 @@ impl, I: 'static> Pallet { let v = votes.remove(i); T::Polls::try_access_poll(poll_index, |poll_status| match poll_status { - PollStatus::Ongoing(tally, _) => { + PollStatus::Ongoing(tally, _, _) => { ensure!(matches!(scope, UnvoteScope::Any), Error::::NoPermission); // Shouldn't be possible to fail, but we handle it gracefully. tally.remove(v.1).ok_or(ArithmeticError::Underflow)?; @@ -520,7 +521,7 @@ impl, I: 'static> Pallet { for &(poll_index, account_vote) in votes.iter() { if let AccountVote::Standard { vote, .. } = account_vote { T::Polls::access_poll(poll_index, |poll_status| { - if let PollStatus::Ongoing(tally, _) = poll_status { + if let PollStatus::Ongoing(tally, _, _) = poll_status { tally.increase(vote.aye, amount); } }); @@ -548,7 +549,7 @@ impl, I: 'static> Pallet { for &(poll_index, account_vote) in votes.iter() { if let AccountVote::Standard { vote, .. } = account_vote { T::Polls::access_poll(poll_index, |poll_status| { - if let PollStatus::Ongoing(tally, _) = poll_status { + if let PollStatus::Ongoing(tally, _, _) = poll_status { tally.reduce(vote.aye, amount); } }); diff --git a/substrate/frame/conviction-voting/src/tests.rs b/substrate/frame/conviction-voting/src/tests.rs index 8e8b41ba63eb4..0763329b38111 100644 --- a/substrate/frame/conviction-voting/src/tests.rs +++ b/substrate/frame/conviction-voting/src/tests.rs @@ -59,9 +59,12 @@ impl pallet_balances::Config for Test { type AccountStore = System; } +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Origin; + #[derive(Clone, PartialEq, Eq, Debug)] pub enum TestPollState { - Ongoing(TallyOf, u8), + Ongoing(TallyOf, u8, Origin), Completed(u64, bool), } use TestPollState::*; @@ -70,7 +73,7 @@ parameter_types! { pub static Polls: BTreeMap = vec![ (1, Completed(1, true)), (2, Completed(2, false)), - (3, Ongoing(Tally::from_parts(0, 0, 0), 0)), + (3, Ongoing(Tally::from_parts(0, 0, 0), 0, Origin)), ].into_iter().collect(); } @@ -80,13 +83,14 @@ impl Polling> for TestPolls { type Votes = u64; type Moment = u64; type Class = u8; + type Origin = Origin; fn classes() -> Vec { vec![0, 1, 2] } - fn as_ongoing(index: u8) -> Option<(TallyOf, Self::Class)> { + fn as_ongoing(index: u8) -> Option<(TallyOf, Self::Class, Self::Origin)> { Polls::get().remove(&index).and_then(|x| { - if let TestPollState::Ongoing(t, c) = x { - Some((t, c)) + if let TestPollState::Ongoing(t, c, o) = x { + Some((t, c, o)) } else { None } @@ -94,13 +98,13 @@ impl Polling> for TestPolls { } fn access_poll( index: Self::Index, - f: impl FnOnce(PollStatus<&mut TallyOf, u64, u8>) -> R, + f: impl FnOnce(PollStatus<&mut TallyOf, u64, u8, &Origin>) -> R, ) -> R { let mut polls = Polls::get(); let entry = polls.get_mut(&index); let r = match entry { - Some(Ongoing(ref mut tally_mut_ref, class)) => - f(PollStatus::Ongoing(tally_mut_ref, *class)), + Some(Ongoing(ref mut tally_mut_ref, class, origin)) => + f(PollStatus::Ongoing(tally_mut_ref, *class, &origin)), Some(Completed(when, succeeded)) => f(PollStatus::Completed(*when, *succeeded)), None => f(PollStatus::None), }; @@ -109,13 +113,13 @@ impl Polling> for TestPolls { } fn try_access_poll( index: Self::Index, - f: impl FnOnce(PollStatus<&mut TallyOf, u64, u8>) -> Result, + f: impl FnOnce(PollStatus<&mut TallyOf, u64, u8, &Origin>) -> Result, ) -> Result { let mut polls = Polls::get(); let entry = polls.get_mut(&index); let r = match entry { - Some(Ongoing(ref mut tally_mut_ref, class)) => - f(PollStatus::Ongoing(tally_mut_ref, *class)), + Some(Ongoing(ref mut tally_mut_ref, class, origin)) => + f(PollStatus::Ongoing(tally_mut_ref, *class, &origin)), Some(Completed(when, succeeded)) => f(PollStatus::Completed(*when, *succeeded)), None => f(PollStatus::None), }?; @@ -127,7 +131,7 @@ impl Polling> for TestPolls { fn create_ongoing(class: Self::Class) -> Result { let mut polls = Polls::get(); let i = polls.keys().rev().next().map_or(0, |x| x + 1); - polls.insert(i, Ongoing(Tally::new(0), class)); + polls.insert(i, Ongoing(Tally::new(0), class, Origin)); Polls::set(polls); Ok(i) } @@ -455,10 +459,10 @@ fn classwise_delegation_works() { new_test_ext().execute_with(|| { Polls::set( vec![ - (0, Ongoing(Tally::new(0), 0)), - (1, Ongoing(Tally::new(0), 1)), - (2, Ongoing(Tally::new(0), 2)), - (3, Ongoing(Tally::new(0), 2)), + (0, Ongoing(Tally::new(0), 0, Origin)), + (1, Ongoing(Tally::new(0), 1, Origin)), + (2, Ongoing(Tally::new(0), 2, Origin)), + (3, Ongoing(Tally::new(0), 2, Origin)), ] .into_iter() .collect(), @@ -482,10 +486,10 @@ fn classwise_delegation_works() { assert_eq!( Polls::get(), vec![ - (0, Ongoing(Tally::from_parts(6, 2, 15), 0)), - (1, Ongoing(Tally::from_parts(6, 2, 15), 1)), - (2, Ongoing(Tally::from_parts(6, 2, 15), 2)), - (3, Ongoing(Tally::from_parts(0, 0, 0), 2)), + (0, Ongoing(Tally::from_parts(6, 2, 15), 0, Origin)), + (1, Ongoing(Tally::from_parts(6, 2, 15), 1, Origin)), + (2, Ongoing(Tally::from_parts(6, 2, 15), 2, Origin)), + (3, Ongoing(Tally::from_parts(0, 0, 0), 2, Origin)), ] .into_iter() .collect() @@ -496,10 +500,10 @@ fn classwise_delegation_works() { assert_eq!( Polls::get(), vec![ - (0, Ongoing(Tally::from_parts(6, 2, 15), 0)), - (1, Ongoing(Tally::from_parts(6, 2, 15), 1)), - (2, Ongoing(Tally::from_parts(6, 2, 15), 2)), - (3, Ongoing(Tally::from_parts(0, 6, 0), 2)), + (0, Ongoing(Tally::from_parts(6, 2, 15), 0, Origin)), + (1, Ongoing(Tally::from_parts(6, 2, 15), 1, Origin)), + (2, Ongoing(Tally::from_parts(6, 2, 15), 2, Origin)), + (3, Ongoing(Tally::from_parts(0, 6, 0), 2, Origin)), ] .into_iter() .collect() @@ -511,10 +515,10 @@ fn classwise_delegation_works() { assert_eq!( Polls::get(), vec![ - (0, Ongoing(Tally::from_parts(6, 2, 15), 0)), - (1, Ongoing(Tally::from_parts(6, 2, 15), 1)), - (2, Ongoing(Tally::from_parts(1, 7, 10), 2)), - (3, Ongoing(Tally::from_parts(0, 1, 0), 2)), + (0, Ongoing(Tally::from_parts(6, 2, 15), 0, Origin)), + (1, Ongoing(Tally::from_parts(6, 2, 15), 1, Origin)), + (2, Ongoing(Tally::from_parts(1, 7, 10), 2, Origin)), + (3, Ongoing(Tally::from_parts(0, 1, 0), 2, Origin)), ] .into_iter() .collect() @@ -530,10 +534,10 @@ fn classwise_delegation_works() { assert_eq!( Polls::get(), vec![ - (0, Ongoing(Tally::from_parts(4, 2, 13), 0)), - (1, Ongoing(Tally::from_parts(4, 2, 13), 1)), - (2, Ongoing(Tally::from_parts(4, 2, 13), 2)), - (3, Ongoing(Tally::from_parts(0, 4, 0), 2)), + (0, Ongoing(Tally::from_parts(4, 2, 13), 0, Origin)), + (1, Ongoing(Tally::from_parts(4, 2, 13), 1, Origin)), + (2, Ongoing(Tally::from_parts(4, 2, 13), 2, Origin)), + (3, Ongoing(Tally::from_parts(0, 4, 0), 2, Origin)), ] .into_iter() .collect() @@ -562,10 +566,10 @@ fn classwise_delegation_works() { assert_eq!( Polls::get(), vec![ - (0, Ongoing(Tally::from_parts(7, 2, 16), 0)), - (1, Ongoing(Tally::from_parts(8, 2, 17), 1)), - (2, Ongoing(Tally::from_parts(9, 2, 18), 2)), - (3, Ongoing(Tally::from_parts(0, 9, 0), 2)), + (0, Ongoing(Tally::from_parts(7, 2, 16), 0, Origin)), + (1, Ongoing(Tally::from_parts(8, 2, 17), 1, Origin)), + (2, Ongoing(Tally::from_parts(9, 2, 18), 2, Origin)), + (3, Ongoing(Tally::from_parts(0, 9, 0), 2, Origin)), ] .into_iter() .collect() @@ -576,7 +580,7 @@ fn classwise_delegation_works() { #[test] fn redelegation_after_vote_ending_should_keep_lock() { new_test_ext().execute_with(|| { - Polls::set(vec![(0, Ongoing(Tally::new(0), 0))].into_iter().collect()); + Polls::set(vec![(0, Ongoing(Tally::new(0), 0, Origin))].into_iter().collect()); assert_ok!(Voting::delegate(RuntimeOrigin::signed(1), 0, 2, Conviction::Locked1x, 5)); assert_ok!(Voting::vote(RuntimeOrigin::signed(2), 0, aye(10, 1))); Polls::set(vec![(0, Completed(1, true))].into_iter().collect()); @@ -594,9 +598,9 @@ fn lock_amalgamation_valid_with_multiple_removed_votes() { new_test_ext().execute_with(|| { Polls::set( vec![ - (0, Ongoing(Tally::new(0), 0)), - (1, Ongoing(Tally::new(0), 0)), - (2, Ongoing(Tally::new(0), 0)), + (0, Ongoing(Tally::new(0), 0, Origin)), + (1, Ongoing(Tally::new(0), 0, Origin)), + (2, Ongoing(Tally::new(0), 0, Origin)), ] .into_iter() .collect(), @@ -666,7 +670,7 @@ fn lock_amalgamation_valid_with_multiple_delegations() { #[test] fn lock_amalgamation_valid_with_move_roundtrip_to_delegation() { new_test_ext().execute_with(|| { - Polls::set(vec![(0, Ongoing(Tally::new(0), 0))].into_iter().collect()); + Polls::set(vec![(0, Ongoing(Tally::new(0), 0, Origin))].into_iter().collect()); assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 0, aye(5, 1))); Polls::set(vec![(0, Completed(1, true))].into_iter().collect()); assert_ok!(Voting::remove_vote(RuntimeOrigin::signed(1), Some(0), 0)); @@ -678,7 +682,7 @@ fn lock_amalgamation_valid_with_move_roundtrip_to_delegation() { assert_ok!(Voting::unlock(RuntimeOrigin::signed(1), 0, 1)); assert_eq!(Balances::usable_balance(1), 0); - Polls::set(vec![(1, Ongoing(Tally::new(0), 0))].into_iter().collect()); + Polls::set(vec![(1, Ongoing(Tally::new(0), 0, Origin))].into_iter().collect()); assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 1, aye(5, 2))); Polls::set(vec![(1, Completed(1, true))].into_iter().collect()); assert_ok!(Voting::remove_vote(RuntimeOrigin::signed(1), Some(0), 1)); @@ -706,7 +710,7 @@ fn lock_amalgamation_valid_with_move_roundtrip_to_casting() { assert_ok!(Voting::unlock(RuntimeOrigin::signed(1), 0, 1)); assert_eq!(Balances::usable_balance(1), 5); - Polls::set(vec![(0, Ongoing(Tally::new(0), 0))].into_iter().collect()); + Polls::set(vec![(0, Ongoing(Tally::new(0), 0, Origin))].into_iter().collect()); assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 0, aye(10, 1))); Polls::set(vec![(0, Completed(1, true))].into_iter().collect()); assert_ok!(Voting::remove_vote(RuntimeOrigin::signed(1), Some(0), 0)); @@ -767,9 +771,9 @@ fn lock_aggregation_over_different_classes_with_casting_works() { new_test_ext().execute_with(|| { Polls::set( vec![ - (0, Ongoing(Tally::new(0), 0)), - (1, Ongoing(Tally::new(0), 1)), - (2, Ongoing(Tally::new(0), 2)), + (0, Ongoing(Tally::new(0), 0, Origin)), + (1, Ongoing(Tally::new(0), 1, Origin)), + (2, Ongoing(Tally::new(0), 2, Origin)), ] .into_iter() .collect(), @@ -835,10 +839,10 @@ fn errors_with_vote_work() { assert_ok!(Voting::undelegate(RuntimeOrigin::signed(1), 0)); Polls::set( vec![ - (0, Ongoing(Tally::new(0), 0)), - (1, Ongoing(Tally::new(0), 0)), - (2, Ongoing(Tally::new(0), 0)), - (3, Ongoing(Tally::new(0), 0)), + (0, Ongoing(Tally::new(0), 0, Origin)), + (1, Ongoing(Tally::new(0), 0, Origin)), + (2, Ongoing(Tally::new(0), 0, Origin)), + (3, Ongoing(Tally::new(0), 0, Origin)), ] .into_iter() .collect(), diff --git a/substrate/frame/ranked-collective/src/lib.rs b/substrate/frame/ranked-collective/src/lib.rs index 4b1b3d9010db1..9a973fffe1b33 100644 --- a/substrate/frame/ranked-collective/src/lib.rs +++ b/substrate/frame/ranked-collective/src/lib.rs @@ -640,7 +640,7 @@ pub mod pallet { match status { PollStatus::None | PollStatus::Completed(..) => Err(Error::::NotPolling)?, - PollStatus::Ongoing(ref mut tally, class) => { + PollStatus::Ongoing(ref mut tally, class, _) => { match Voting::::get(&poll, &who) { Some(Aye(votes)) => { tally.bare_ayes.saturating_dec(); diff --git a/substrate/frame/ranked-collective/src/tests.rs b/substrate/frame/ranked-collective/src/tests.rs index 3a85928b76d5e..9bb3cd51f5fb6 100644 --- a/substrate/frame/ranked-collective/src/tests.rs +++ b/substrate/frame/ranked-collective/src/tests.rs @@ -48,9 +48,12 @@ impl frame_system::Config for Test { type Block = Block; } +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Origin; + #[derive(Clone, PartialEq, Eq, Debug)] pub enum TestPollState { - Ongoing(TallyOf, Rank), + Ongoing(TallyOf, Rank, Origin), Completed(u64, bool), } use TestPollState::*; @@ -59,7 +62,7 @@ parameter_types! { pub static Polls: BTreeMap = vec![ (1, Completed(1, true)), (2, Completed(2, false)), - (3, Ongoing(Tally::from_parts(0, 0, 0), 1)), + (3, Ongoing(Tally::from_parts(0, 0, 0), 1, Origin)), ].into_iter().collect(); } @@ -69,13 +72,14 @@ impl Polling> for TestPolls { type Votes = Votes; type Moment = u64; type Class = Class; + type Origin = Origin; fn classes() -> Vec { vec![0, 1, 2] } - fn as_ongoing(index: u8) -> Option<(TallyOf, Self::Class)> { + fn as_ongoing(index: u8) -> Option<(TallyOf, Self::Class, Self::Origin)> { Polls::get().remove(&index).and_then(|x| { - if let TestPollState::Ongoing(t, c) = x { - Some((t, c)) + if let TestPollState::Ongoing(t, c, o) = x { + Some((t, c, o)) } else { None } @@ -83,13 +87,13 @@ impl Polling> for TestPolls { } fn access_poll( index: Self::Index, - f: impl FnOnce(PollStatus<&mut TallyOf, Self::Moment, Self::Class>) -> R, + f: impl FnOnce(PollStatus<&mut TallyOf, Self::Moment, Self::Class, &Self::Origin>) -> R, ) -> R { let mut polls = Polls::get(); let entry = polls.get_mut(&index); let r = match entry { - Some(Ongoing(ref mut tally_mut_ref, class)) => - f(PollStatus::Ongoing(tally_mut_ref, *class)), + Some(Ongoing(ref mut tally_mut_ref, class, origin)) => + f(PollStatus::Ongoing(tally_mut_ref, *class, &origin)), Some(Completed(when, succeeded)) => f(PollStatus::Completed(*when, *succeeded)), None => f(PollStatus::None), }; @@ -99,14 +103,14 @@ impl Polling> for TestPolls { fn try_access_poll( index: Self::Index, f: impl FnOnce( - PollStatus<&mut TallyOf, Self::Moment, Self::Class>, + PollStatus<&mut TallyOf, Self::Moment, Self::Class, &Self::Origin>, ) -> Result, ) -> Result { let mut polls = Polls::get(); let entry = polls.get_mut(&index); let r = match entry { - Some(Ongoing(ref mut tally_mut_ref, class)) => - f(PollStatus::Ongoing(tally_mut_ref, *class)), + Some(Ongoing(ref mut tally_mut_ref, class, origin)) => + f(PollStatus::Ongoing(tally_mut_ref, *class, &origin)), Some(Completed(when, succeeded)) => f(PollStatus::Completed(*when, *succeeded)), None => f(PollStatus::None), }?; @@ -118,7 +122,7 @@ impl Polling> for TestPolls { fn create_ongoing(class: Self::Class) -> Result { let mut polls = Polls::get(); let i = polls.keys().rev().next().map_or(0, |x| x + 1); - polls.insert(i, Ongoing(Tally::new(class), class)); + polls.insert(i, Ongoing(Tally::new(class), class, Origin)); Polls::set(polls); Ok(i) } diff --git a/substrate/frame/referenda/src/benchmarking.rs b/substrate/frame/referenda/src/benchmarking.rs index 59499d9c8bf08..e890cf32aee1e 100644 --- a/substrate/frame/referenda/src/benchmarking.rs +++ b/substrate/frame/referenda/src/benchmarking.rs @@ -127,7 +127,7 @@ fn make_passing_after, I: 'static>(index: ReferendumIndex, period_p .threshold(period_portion) .saturating_add(Perbill::from_percent(1)); Referenda::::access_poll(index, |status| { - if let PollStatus::Ongoing(tally, class) = status { + if let PollStatus::Ongoing(tally, class, _) = status { T::Tally::setup(class, Perbill::from_rational(1u32, 1000u32)); *tally = T::Tally::from_requirements(support, approval, class); } @@ -136,7 +136,7 @@ fn make_passing_after, I: 'static>(index: ReferendumIndex, period_p fn make_passing, I: 'static>(index: ReferendumIndex) { Referenda::::access_poll(index, |status| { - if let PollStatus::Ongoing(tally, class) = status { + if let PollStatus::Ongoing(tally, class, _) = status { T::Tally::setup(class, Perbill::from_rational(1u32, 1000u32)); *tally = T::Tally::unanimity(class); } @@ -145,7 +145,7 @@ fn make_passing, I: 'static>(index: ReferendumIndex) { fn make_failing, I: 'static>(index: ReferendumIndex) { Referenda::::access_poll(index, |status| { - if let PollStatus::Ongoing(tally, class) = status { + if let PollStatus::Ongoing(tally, class, _) = status { T::Tally::setup(class, Perbill::from_rational(1u32, 1000u32)); *tally = T::Tally::rejection(class); } diff --git a/substrate/frame/referenda/src/lib.rs b/substrate/frame/referenda/src/lib.rs index b58baa341cf59..24fe34c32424b 100644 --- a/substrate/frame/referenda/src/lib.rs +++ b/substrate/frame/referenda/src/lib.rs @@ -735,6 +735,7 @@ impl, I: 'static> Polling for Pallet { type Votes = VotesOf; type Moment = BlockNumberFor; type Class = TrackIdOf; + type Origin = PalletsOriginOf; fn classes() -> Vec { T::Tracks::track_ids().collect() @@ -742,11 +743,14 @@ impl, I: 'static> Polling for Pallet { fn access_poll( index: Self::Index, - f: impl FnOnce(PollStatus<&mut T::Tally, BlockNumberFor, TrackIdOf>) -> R, + f: impl FnOnce( + PollStatus<&mut T::Tally, BlockNumberFor, TrackIdOf, &PalletsOriginOf>, + ) -> R, ) -> R { match ReferendumInfoFor::::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 = T::BlockNumberProvider::current_block_number(); Self::ensure_alarm_at(&mut status, index, now + One::one()); ReferendumInfoFor::::insert(index, ReferendumInfo::Ongoing(status)); @@ -761,12 +765,13 @@ impl, I: 'static> Polling for Pallet { fn try_access_poll( index: Self::Index, f: impl FnOnce( - PollStatus<&mut T::Tally, BlockNumberFor, TrackIdOf>, + PollStatus<&mut T::Tally, BlockNumberFor, TrackIdOf, &PalletsOriginOf>, ) -> Result, ) -> Result { match ReferendumInfoFor::::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 = T::BlockNumberProvider::current_block_number(); Self::ensure_alarm_at(&mut status, index, now + One::one()); ReferendumInfoFor::::insert(index, ReferendumInfo::Ongoing(status)); @@ -778,8 +783,8 @@ impl, I: 'static> Polling for Pallet { } } - fn as_ongoing(index: Self::Index) -> Option<(T::Tally, TrackIdOf)> { - Self::ensure_ongoing(index).ok().map(|x| (x.tally, x.track)) + fn as_ongoing(index: Self::Index) -> Option<(T::Tally, TrackIdOf, Self::Origin)> { + Self::ensure_ongoing(index).ok().map(|x| (x.tally, x.track, x.origin)) } #[cfg(feature = "runtime-benchmarks")] diff --git a/substrate/frame/support/src/traits/voting.rs b/substrate/frame/support/src/traits/voting.rs index 697134e4ca474..210666a1de9e3 100644 --- a/substrate/frame/support/src/traits/voting.rs +++ b/substrate/frame/support/src/traits/voting.rs @@ -59,16 +59,16 @@ pub trait VoteTally { /// users. fn setup(class: Class, granularity: Perbill); } -pub enum PollStatus { +pub enum PollStatus { None, - Ongoing(Tally, Class), + Ongoing(Tally, Class, Origin), Completed(Moment, bool), } -impl PollStatus { - pub fn ensure_ongoing(self) -> Option<(Tally, Class)> { +impl PollStatus { + pub fn ensure_ongoing(self) -> Option<(Tally, Class, Origin)> { match self { - Self::Ongoing(t, c) => Some((t, c)), + Self::Ongoing(t, c, o) => Some((t, c, o)), _ => None, } } @@ -85,6 +85,7 @@ pub trait Polling { 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 Origin; type Moment; /// Provides a vec of values that `T` may take. @@ -94,16 +95,18 @@ pub trait Polling { /// referendum. /// /// Don't use this if you might mutate - use `try_access_poll` instead. - fn as_ongoing(index: Self::Index) -> Option<(Tally, Self::Class)>; + fn as_ongoing(index: Self::Index) -> Option<(Tally, Self::Class, Self::Origin)>; fn access_poll( 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::Origin>) -> R, ) -> R; fn try_access_poll( index: Self::Index, - f: impl FnOnce(PollStatus<&mut Tally, Self::Moment, Self::Class>) -> Result, + f: impl FnOnce( + PollStatus<&mut Tally, Self::Moment, Self::Class, &Self::Origin>, + ) -> Result, ) -> Result; /// Create an ongoing majority-carries poll of given class lasting given period for the purpose @@ -134,25 +137,28 @@ impl Polling for NoOpPoll { type Votes = u32; type Class = u16; type Moment = u64; + type Origin = (); fn classes() -> Vec { vec![] } - fn as_ongoing(_index: Self::Index) -> Option<(Tally, Self::Class)> { + fn as_ongoing(_index: Self::Index) -> Option<(Tally, Self::Class, Self::Origin)> { None } fn access_poll( _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::Origin>) -> R, ) -> R { f(PollStatus::None) } fn try_access_poll( _index: Self::Index, - f: impl FnOnce(PollStatus<&mut Tally, Self::Moment, Self::Class>) -> Result, + f: impl FnOnce( + PollStatus<&mut Tally, Self::Moment, Self::Class, &Self::Origin>, + ) -> Result, ) -> Result { f(PollStatus::None) }